utility function to generate wavesurfer instances for testing
19321
function __createWaveform(options = {}) {
        waveformDiv = document.createElement('div');
        document.getElementsByTagName('body')[0].appendChild(waveformDiv);

        wavesurfer = WaveSurfer.create(
            Object.assign(
                {
                    container: waveformDiv
                },
                options
            )
        );
        wavesurfer.load(TestHelpers.EXAMPLE_FILE_PATH);
    }
614116
def run_crbox(self,spstring,form,output="",wavecat="INDEF",
                  lowave=0,hiwave=30000):
        """"""

        range=hiwave-lowave
        midwave=range/2.0
        iraf.countrate(spectrum=spstring, magnitude="",
                       instrument="box(%f,%f)"%(midwave,range),
                       form=form,
                       wavecat=wavecat,
                       output=output)
794878
def generate_examples(self):
        
        data = javabridge.call(self.jobject, "generateExamples", "()Lweka/core/Instances;")
        if data is None:
            return None
        else:
            return Instances(data)
1364189
def from_arrays(cls, name, verts, normals, n_verts=3):

        """"""

        # Put header in string
        wavefront_str = "o {name}\n".format(name=name)

        # Write Vertex data from vert_dict
        for vert in verts:
            wavefront_str += "v {0} {1} {2}\n".format(*vert)

        # Write (false) UV Texture data
        wavefront_str += "vt 1.0 1.0\n"

        for norm in normals:
            wavefront_str += "vn {0} {1} {2}\n".format(*norm)


        # Write Face Indices (1-indexed)
        for wall_idx, vert_indices in enumerate(grouper(n_verts, range(1, len(verts) + 1))):
            face_str = 'f '
            for idx in vert_indices:
                face_str += "{v}/1/{n} ".format(v=idx, n=wall_idx + 1)
            wavefront_str += face_str + '\n'

        return WavefrontWriter(string=wavefront_str)
985417
def _single_mp_run(x, Phi, bound, max_iter, verbose=False, pad=0,
                   random_state=None, memory=Memory(None)):
    

    rng = check_random_state(random_state)
    pad = int(pad)
    x = np.concatenate((np.zeros(pad), x, np.zeros(pad)))

    n = x.size
    m = Phi.doth(x).size
    err_mse = []

    # Initialisation
    residual = np.concatenate((x.copy(), np.zeros(max(Phi.sizes) // 2)))

    s = np.zeros(m)
    x_est = np.zeros(n)
    # Main algorithm
    coeffs = np.zeros(m)
    it_number = 0
    current_lambda = 1
    err_mse.append(linalg.norm(residual))

    # Decomposition loop: stopping criteria is either SNR or iteration number
    while (current_lambda > bound) & (it_number < max_iter):

        # pick a shift at random : in each size
        rndshifts = []
        for scale_idx, size in enumerate(Phi.sizes):
            shift = rng.randint(low=0, high=size // 4)
            coeffs[scale_idx * n:(scale_idx + 1) * n] = mdct(
                residual[shift:shift + n], size).ravel()
            rndshifts.append(shift)

        # Select a new element
        idx = np.argmax(np.abs(coeffs))

        # Update coefficients
        s[idx] += coeffs[idx]

        # Only one method now : local update via a cached waveform
        # find scale and frequency bin of selected atom
        mdct_wf = memory.cache(mdct_waveform)

        scale_idx = idx // n
        size = Phi.sizes[scale_idx]
        F = n // (size // 2)
        frame = (idx - (scale_idx * n)) % F
        freq_bin = ((idx - (scale_idx * n))) // F
        pos = (frame * size // 2) - size // 4 + rndshifts[scale_idx]
        residual[pos:pos + size] -= coeffs[idx] * mdct_wf(size, freq_bin)

        # also add it to the reconstruction
        x_est[pos:pos + size] += coeffs[idx] * mdct_wf(size, freq_bin)

        # error computation (err_mse)
        err_mse.append(linalg.norm(residual))

        current_lambda = np.sqrt(1 - err_mse[-1] / err_mse[-2])
        if current_lambda <= bound:
            x_est[pos:pos + size] -= coeffs[idx] * mdct_wf(size, freq_bin)
        if verbose:
            print("Iteration %d : Current lambda of %1.4f" % (
                  it_number, current_lambda))
        it_number += 1

    return x_est, err_mse
47294
def write_wave(path, audio, sample_rate):
    
    with contextlib.closing(wave.open(path, 'wb')) as wf:
        wf.setnchannels(1)
        wf.setsampwidth(2)
        wf.setframerate(sample_rate)
        wf.writeframes(audio)
1095588
function cloneBuffer(buffer, offset = 0, length = buffer.length) {
    if (!context || context.isFake) {
        return buffer;
    }
    const numChannels = buffer.numberOfChannels;
    const cloned = context.createBuffer(numChannels, length, buffer.sampleRate);
    for (let i = 0; i < numChannels; i++) {
        cloned.getChannelData(i)
            .set(buffer.getChannelData(i).slice(offset, offset + length));
    }
    return cloned;
}
291241
function createMotionBlur (render, opts = {}) {
  const {
    samplesPerFrame = 4,
    shutterAngle = 1
  } = opts;
  const otherCanvas = document.createElement('canvas');
  const otherContext = otherCanvas.getContext('2d');

  return (props = {}) => {
    const {
      context,
      canvasWidth,
      canvasHeight,
      totalFrames,
      exporting,
      frame
    } = props;

    // Ignore blur if not exporting, it is too slow...
    if (!exporting) {
      return render(props);
    }

    otherCanvas.width = canvasWidth;
    otherCanvas.height = canvasHeight;
    otherContext.clearRect(0, 0, canvasWidth, canvasHeight);

    const size = canvasWidth * canvasHeight * 4;
    const composite = new Float32Array(size);
    for (let i = 0; i < size; i++) {
      composite[i] = 0;
    }

    // For every sample, run the render function and add the new pixel values to
    // the composite array
    for (let sample = 0; sample < samplesPerFrame; sample++) {
      const sampleFrame = frame + sample * shutterAngle / samplesPerFrame;
      render(Object.assign({}, props, {
        frame: sampleFrame % totalFrames,
        playhead: (sampleFrame / (totalFrames - 1)) % 1
      }));

      const pixels = context.getImageData(0, 0, canvasWidth, canvasHeight).data;
      for (let idx = 0; idx < pixels.length; idx++) {
        composite[idx] += pixels[idx];
      }
    }

    // Fill the pixels in `imageData` with averaged values from `composite`
    const imageData = context.createImageData(canvasWidth, canvasHeight);
    for (let idx = 0; idx < imageData.data.length; idx++) {
      imageData.data[idx] = composite[idx] / samplesPerFrame;
    }

    // Draw the averaged image data to the canvas
    context.putImageData(imageData, 0, 0);
  };
}
734915
def wave_at_location(self, l):
        
        cterm = 1j * self._wave_nums[l.index] * l.depth_within

        if l.wave_field == WaveField.within:
            return (self._waves_a[l.index] * np.exp(cterm) +
                    self._waves_b[l.index] * np.exp(-cterm))
        elif l.wave_field == WaveField.outcrop:
            return 2 * self._waves_a[l.index] * np.exp(cterm)
        elif l.wave_field == WaveField.incoming_only:
            return self._waves_a[l.index] * np.exp(cterm)
        else:
            raise NotImplementedError
860720
def generate_instance(self, timestamp, value, meta_data):
        
        if not self.filters or meta_data in self.filters:
            if self.filters:
                logging.debug('Filtered in  for {} from {}'.format(meta_data, self.filters))
            return StreamMetaInstance(StreamInstance(timestamp=timestamp, value=value), meta_data)
        if self.filters:
            logging.debug('Filtered out for {} from {}'.format(meta_data, self.filters))
        return None
1008266
def _generate_examples(self, archive_paths, objects_getter, bboxes_getter,
                         prefixes=None):
    """"""
    trainable_classes = set(
        self.info.features['objects_trainable']['label'].names)
    for i, archive_path in enumerate(archive_paths):
      prefix = prefixes[i] if prefixes else None
      objects = objects_getter(prefix)
      bboxes = bboxes_getter(prefix)
      logging.info('Opening archive %s ...', archive_path)
      archive = tfds.download.iter_archive(
          archive_path, tfds.download.ExtractMethod.TAR_STREAM)
      for fpath, fobj in archive:
        fname = os.path.basename(fpath)
        image_id = int(os.path.splitext(fname)[0], 16)
        image_objects = [obj._asdict() for obj in objects.get(image_id, [])]
        image_bboxes = [bbox._asdict() for bbox in bboxes.get(image_id, [])]
        image_objects_trainable = [
            obj for obj in image_objects if obj['label'] in trainable_classes
        ]
        yield {
            'image': _resize_image_if_necessary(
                fobj, target_pixels=self.builder_config.target_pixels),
            'image/filename': fname,
            'objects': image_objects,
            'objects_trainable': image_objects_trainable,
            'bobjects': image_bboxes,
        }
199311
function initRepeater () {
		// simulate network latency
		var loadDelays = ['300', '600', '900', '1200'];
		var sort = function (data, sortProperty, sortDirection) {
			var sortedData = _.sortBy(data, function (item) {
				return item[sortProperty];
			});

			// sort direction
			if (sortDirection === 'desc') {
				sortedData = sortedData.reverse();
			}

			return sortedData;
		};

		// list view setup
		var list = function (options, callback) {
			// build dataSource based with options
			var resp = {
				count: data.repeater.listData.length,
				items: [],
				page: options.pageIndex
			};

			// get start and end limits for JSON
			var i, l;
			resp.pages = Math.ceil(resp.count / (options.pageSize || 50));

			i = options.pageIndex * (options.pageSize || 50);
			l = i + (options.pageSize || 50);
			l = (l <= resp.count) ? l : resp.count;
			resp.start = i + 1;
			resp.end = l;

			// setup columns for list view
			resp.columns = [
				{
					label: 'Common Name',
					property: 'commonName',
					sortable: true,
					width: 600
				},
				{
					label: 'Latin Name',
					property: 'latinName',
					sortable: true,
					width: 600
				},
				{
					label: 'Appearance',
					property: 'appearance',
					sortable: true
				},
				{
					label: 'Sound',
					property: 'sound',
					sortable: true
				}
			];

			// add sample items to datasource
			for (i; i < l; i++) {
				// from data.js
				resp.items.push(data.repeater.listData[i]);
			}

			resp.items = sort(resp.items, options.sortProperty, options.sortDirection);

			// call and simulate latency
			setTimeout(function () {
				callback(resp);
			}, loadDelays[Math.floor(Math.random() * 4)]);
		};


		// thumbnail view setup
		var thumbnail = function (options, callback) {
			var sampleImageCategories = ['abstract', 'animals', 'business', 'cats', 'city', 'food', 'nature', 'technics', 'transport'];
			var numItems = 200;

			// build dataSource based with options
			var resp = {
				count: numItems,
				items: [],
				pages: (Math.ceil(numItems / (options.pageSize || 30))),
				page: options.pageIndex
			};

			// get start and end limits for JSON
			var i, l;
			i = options.pageIndex * (options.pageSize || 30);
			l = i + (options.pageSize || 30);
			resp.start = i + 1;
			resp.end = l;

			// add sample items to datasource
			for (i; i < l; i++) {
				resp.items.push({
					name: ('Thumbnail ' + (i + 1)),
					src: 'http://lorempixel.com/65/65/' + sampleImageCategories[Math.floor(Math.random() * 9)] + '/?_=' + i
				});
			}

			// call and simulate latency
			setTimeout(function () {
				callback(resp);
			}, loadDelays[Math.floor(Math.random() * 4)]);
		};

		// initialize repater
		$('#myRepeater').repeater({
			searchOnKeyPress: true,
			dataSource: function (options, callback) {
				if (options.view === 'list') {
					list(options, callback);
				} else if (options.view === 'thumbnail') {
					thumbnail(options, callback);
				}
			},
			list_noItemsHTML: 'no items found',
			thumbnail_noItemsHTML: 'no items found',
			views: {
				'list.list': {
					dataSource: function (options, callback) {
						list(options, callback);
					},
				},
				'thumbnail': {
					dataSource: function (options, callback) {
						thumbnail(options, callback);
					},
					thumbnail_infiniteScroll: {
						hybrid: true
					}
				},
				'list.frozen': {
					dataSource: function (options, callback) {
						list(options, callback);
					},
					list_selectable: false, // (single | multi)
					list_frozenColumns: 1
				}
			}
		});
	}
991829
def create(output_prefix, grid_flux_filename, wavelength_filenames,
    clobber=False, grid_flux_filename_format="csv", **kwargs):
    

    if not clobber:
        # Check to make sure the output files won't exist already.
        output_suffixes = (".yaml", ".pkl", "-wavelengths.memmap",
            "-intensities.memmap")
        for path in [output_prefix + suffix for suffix in output_suffixes]:
            if os.path.exists(path):
                raise IOError("output filename {} already exists".format(path))

    # Read the grid_flux filename.
    # param1 param2 param3 param4 channelname1 channelname2
    kwds = kwargs.pop("__grid_flux_filename_kwargs", {})
    kwds.update({"format": grid_flux_filename_format})
    grid_flux_tbl = Table.read(grid_flux_filename, **kwds)

    # Distinguish column names between parameters (real numbers) and filenames
    str_columns = \
        np.array([_[1].startswith("|S") for _ in grid_flux_tbl.dtype.descr])

    # Check the number of channels provided.
    if str_columns.sum() != len(wavelength_filenames):
        raise ValueError("expected {0} wavelength filenames because {1} has {0}"
            " string columns ({2}) but found {3} wavelength filenames".format(
                sum(str_columns), grid_flux_filename, 
                ", ".join(np.array(grid_flux_tbl.colnames)[str_columns]), 
                len(wavelength_filenames)))

    # Create a record array of the grid points.
    grid_points = \
        grid_flux_tbl.as_array()[np.array(grid_flux_tbl.colnames)[~str_columns]]

    # To-do: make sure they are all floats.

    # Sort the grid points.
    grid_indices = grid_points.argsort(order=grid_points.dtype.names)
    grid_points = grid_points[grid_indices]
    grid_flux_tbl = grid_flux_tbl[grid_indices]

    # Check the wavelength filenames.
    channel_wavelengths = np.array(map(load_simple_data, wavelength_filenames))

    # Sort the channels by starting wavelength.
    c_indices = np.argsort([each.min() for each in channel_wavelengths])
    channel_names = np.array(grid_flux_tbl.colnames)[str_columns][c_indices]
    channel_wavelengths = channel_wavelengths[c_indices]
    channel_sizes = [len(_) for _ in channel_wavelengths]
    num_pixels = sum(channel_sizes)

    # Create the model YAML file.
    with open(output_prefix + ".yaml", "w") as fp:
        header = "\n".join([
            "# Model created on {0}".format(strftime("%Y-%m-%d %H:%M:%S")),
            "# Grid parameters: {0}".format(", ".join(grid_points.dtype.names)),
            "# Channel names: {0}".format(", ".join(channel_names))
            ])
        fp.write(header + "\n" + yaml.safe_dump({ "model_grid": {
                "grid_points": output_prefix + ".pkl",
                "intensities": output_prefix + "-intensities.memmap",
                "wavelengths": output_prefix + "-wavelengths.memmap"
            }}, stream=None, allow_unicode=True, default_flow_style=False))

    # Create the pickled model file, with meta data.
    metadata = {
        "grid_flux_filename": grid_flux_filename,
        "wavelength_filenames": wavelength_filenames,
        "channel_names": channel_names,
        "channel_sizes": channel_sizes,
        "channel_resolutions": [float("inf")] * len(channel_names),
        "sick_version": sick_version
    }
    logger.debug("Dumping grid points and metadata to file")
    with open(output_prefix + ".pkl", "wb") as fp:
        pickle.dump((grid_points, metadata), fp, -1)

    # Create the memory-mapped dispersion file.
    logger.debug("Creating memory-mapped dispersion file.")
    wavelengths_memmap = np.memmap(output_prefix + "-wavelengths.memmap",
        dtype="float32", mode="w+", shape=(num_pixels, ))
    wavelengths_memmap[:] = np.hstack(channel_wavelengths)
    wavelengths_memmap.flush()
    del wavelengths_memmap

    # Create the memory-mapped intensities file.
    logger.debug("Creating memory-mapped intensities file.")
    intensities_memmap = np.memmap(output_prefix + "-intensities.memmap",
        shape=(grid_points.size, num_pixels), dtype="float32",
        mode="w+")
    
    n = len(grid_flux_tbl)
    for i, row in enumerate(grid_flux_tbl):
        logger.debug("Loading point {0}/{1} into the intensities map"\
            .format(i + 1, n))
        j = 0
        for channel_name in channel_names:
            try:
                data = load_simple_data(row[channel_name])
            except:
                logger.exception("Could not load data from {0} for channel {1}"\
                    .format(row[channel_name], channel_name))
                raise
            intensities_memmap[i, j:j + data.size] = data
            j += data.size

    intensities_memmap.flush()
    del intensities_memmap

    return True
864136
function(fs, ourGlob, negatives, opt) {

    // remove path relativity to make globs make sense
    ourGlob = resolveGlob(fs, ourGlob, opt);
    var ourOpt = extend({}, opt);
    delete ourOpt.root;

    // create globbing stuff
    var globber = new glob.Glob(fs, ourGlob, ourOpt);
    // extract base path from glob
    var basePath = opt.base || glob2base(globber);

    // create stream and map events from globber to it
    var stream = through2.obj(negatives.length ? filterNegatives : undefined);

    var found = false;

    globber.on('error', stream.emit.bind(stream, 'error'));
    globber.once('end', function(){
      if (opt.allowEmpty !== true && !found && globIsSingular(globber)) {
        stream.emit('error', new Error('File not found with singular glob'));
      }

      stream.end();
    });
    globber.on('match', function(filename) {
      found = true;

      stream.write({
        cwd: opt.cwd,
        base: basePath,
        path: fs.resolve(opt.cwd, filename)
      });
    });

    return stream;

    function filterNegatives(filename, enc, cb) {
      var matcha = isMatch.bind(null, filename);
      if (negatives.every(matcha)) {
        cb(null, filename); // pass
      } else {
        cb(); // ignore
      }
    }
  }
72181
def waverange(self):
        """"""
        if self.waveset is None:
            x = [None, None]
        else:
            x = u.Quantity([self.waveset.min(), self.waveset.max()])
        return x
383350
private void trainOnOneSample(Instance instance, int partialTreeIter, int i, int dataCount, CompletionService<ArrayList<BeamElement>> pool) throws InterruptedException, ExecutionException
    {
        boolean isPartial = instance.isPartial(options.rootFirst);

        if (partialTreeIter > i && isPartial)
            return;

        Configuration initialConfiguration = new Configuration(instance.getSentence(), options.rootFirst);
        Configuration firstOracle = initialConfiguration.clone();
        ArrayList<Configuration> beam = new ArrayList<Configuration>(options.beamWidth);
        beam.add(initialConfiguration);

        /**
         * The float is the oracle's cost
         * For more information see:
         * Yoav Goldberg and Joakim Nivre. "Training Deterministic Parsers with Non-Deterministic Oracles."
         * TACL 1 (2013): 403-414.
         * for the mean while we just use zero-cost oracles
         */
        Collection<Configuration> oracles = new HashSet<Configuration>();

        oracles.add(firstOracle);

        /**
         * For keeping track of the violations
         * For more information see:
         * Liang Huang, Suphan Fayong and Yang Guo. "Structured perceptron with inexact search."
         * In Proceedings of the 2012 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies,
         * pp. 142-151. Association for Computational Linguistics, 2012.
         */
        float maxViol = Float.NEGATIVE_INFINITY;
        Pair<Configuration, Configuration> maxViolPair = null;

        Configuration bestScoringOracle = null;
        boolean oracleInBeam = false;

        while (!ArcEager.isTerminal(beam) && beam.size() > 0)
        {
            /**
             *  generating new oracles
             *  it keeps the oracles which are in the terminal state
             */
            Collection<Configuration> newOracles = new HashSet<Configuration>();

            if (options.useDynamicOracle || isPartial)
            {
                bestScoringOracle = zeroCostDynamicOracle(instance, oracles, newOracles);
            }
            else
            {
                bestScoringOracle = staticOracle(instance, oracles, newOracles);
            }
            // try explore non-optimal transitions

            if (newOracles.size() == 0)
            {
//                System.err.println("...no oracle(" + dataCount + ")...");
                bestScoringOracle = staticOracle(instance, oracles, newOracles);
            }
            oracles = newOracles;

            TreeSet<BeamElement> beamPreserver = new TreeSet<BeamElement>();

            if (options.numOfThreads == 1 || beam.size() == 1)
            {
                beamSortOneThread(beam, beamPreserver);
            }
            else
            {
                for (int b = 0; b < beam.size(); b++)
                {
                    pool.submit(new BeamScorerThread(false, classifier, beam.get(b),
                                                     dependencyRelations, featureLength, b, options.rootFirst));
                }
                for (int b = 0; b < beam.size(); b++)
                {
                    for (BeamElement element : pool.take().get())
                    {
                        beamPreserver.add(element);
                        if (beamPreserver.size() > options.beamWidth)
                            beamPreserver.pollFirst();
                    }
                }
            }

            if (beamPreserver.size() == 0 || beam.size() == 0)
            {
                break;
            }
            else
            {
                oracleInBeam = false;

                ArrayList<Configuration> repBeam = new ArrayList<Configuration>(options.beamWidth);
                for (BeamElement beamElement : beamPreserver.descendingSet())
                {
//                    if (repBeam.size() >= options.beamWidth) // 只要beamWidth个configuration（这句是多余的）
//                        break;
                    int b = beamElement.index;
                    int action = beamElement.action;
                    int label = beamElement.label;
                    float score = beamElement.score;

                    Configuration newConfig = beam.get(b).clone();

                    ArcEager.commitAction(action, label, score, dependencyRelations, newConfig);
                    repBeam.add(newConfig);

                    if (!oracleInBeam && oracles.contains(newConfig))
                        oracleInBeam = true;
                }
                beam = repBeam;

                if (beam.size() > 0 && oracles.size() > 0)
                {
                    Configuration bestConfig = beam.get(0);
                    if (oracles.contains(bestConfig)) // 模型认为的最大分值configuration是zero cost
                    {
                        oracles = new HashSet<Configuration>();
                        oracles.add(bestConfig);
                    }
                    else // 否则
                    {
                        if (options.useRandomOracleSelection) // 随机选择一个 oracle
                        { // choosing randomly, otherwise using latent structured Perceptron
                            List<Configuration> keys = new ArrayList<Configuration>(oracles);
                            Configuration randomKey = keys.get(randGen.nextInt(keys.size()));
                            oracles = new HashSet<Configuration>();
                            oracles.add(randomKey);
                            bestScoringOracle = randomKey;
                        }
                        else // 选择 oracle中被模型认为分值最大的那个
                        {
                            oracles = new HashSet<Configuration>();
                            oracles.add(bestScoringOracle);
                        }
                    }

                    // do early update
                    if (!oracleInBeam && updateMode.equals("early"))
                        break;

                    // keep violations
                    if (!oracleInBeam && updateMode.equals("max_violation"))
                    {
                        float violation = bestConfig.getScore(true) - bestScoringOracle.getScore(true);//Math.abs(beam.get(0).getScore(true) - bestScoringOracle.getScore(true));
                        if (violation > maxViol)
                        {
                            maxViol = violation;
                            maxViolPair = new Pair<Configuration, Configuration>(bestConfig, bestScoringOracle);
                        }
                    }
                }
                else
                    break;
            }
        }

        // updating weights
        if (!oracleInBeam ||
                !bestScoringOracle.equals(beam.get(0)) // 虽然oracle在beam里面，但在最后时刻，它的得分不是最高
                )
        {
            updateWeights(initialConfiguration, maxViol, isPartial, bestScoringOracle, maxViolPair, beam);
        }
    }
1214940
def generate_samples(self):
        """"""
        filenames = glob_all(self.args.random_data_folder, '*.wav')
        shuffle(filenames)
        while True:
            for fn in filenames:
                for x, y in self.vectors_from_fn(fn):
                    yield x, y