TODO: add pad_sequences, make_sampling_table, skipgrams utils
543354
public static TimeSeriesGenerator fromJson(String jsonFileName)
            throws IOException, InvalidKerasConfigurationException {
        String json = new String(Files.readAllBytes(Paths.get(jsonFileName)));
        Map<String, Object> timeSeriesBaseConfig = parseJsonString(json);
        Map<String, Object> timeSeriesConfig;
        if (timeSeriesBaseConfig.containsKey("config"))
            timeSeriesConfig = (Map<String, Object>) timeSeriesBaseConfig.get("config");
        else
            throw new InvalidKerasConfigurationException("No configuration found for Keras tokenizer");


        int length = (int) timeSeriesConfig.get("length");
        int samplingRate = (int) timeSeriesConfig.get("sampling_rate");
        int stride = (int) timeSeriesConfig.get("stride");
        int startIndex = (int) timeSeriesConfig.get("start_index");
        int endIndex = (int) timeSeriesConfig.get("end_index");
        int batchSize = (int) timeSeriesConfig.get("batch_size");

        boolean shuffle = (boolean) timeSeriesConfig.get("shuffle");
        boolean reverse = (boolean) timeSeriesConfig.get("reverse");


        Gson gson = new Gson();
        List<List<Double>> dataList = gson.fromJson((String) timeSeriesConfig.get("data"), new TypeToken<List<List<Double>>>() {}.getType());
        List<List<Double>> targetsList = gson.fromJson((String) timeSeriesConfig.get("targets"), new TypeToken<List<List<Double>>>() {}.getType());

        int dataPoints = dataList.size();
        int dataPointsPerRow = dataList.get(0).size();


        INDArray data = Nd4j.create(dataPoints, dataPointsPerRow);
        INDArray targets = Nd4j.create(dataPoints, dataPointsPerRow);
        for (int i = 0; i < dataPoints; i ++) {
            data.put(i, Nd4j.create(dataList.get(i)));
            targets.put(i, Nd4j.create(targetsList.get(i)));
        }


        TimeSeriesGenerator gen = new TimeSeriesGenerator(data, targets, length,
                samplingRate, stride, startIndex, endIndex, shuffle, reverse, batchSize);

        return gen;
    }
139759
def compile_output(self, input_sample, num_samples, num_params,
                       maximum_combo, num_groups=None):
        

        if num_groups is None:
            num_groups = num_params

        self.check_input_sample(input_sample, num_groups, num_samples)

        index_list = self._make_index_list(num_samples, num_params, num_groups)

        output = np.zeros(
            (np.size(maximum_combo) * (num_groups + 1), num_params))
        for counter, combo in enumerate(maximum_combo):
            output[index_list[counter]] = np.array(
                input_sample[index_list[combo]])
        return output
1046904
function rpad(str, len, padstr) {
	var str = ""+str;
	if(str.length >= len) return str;
	return str + Array(len + 1 - str.length).join(padstr);
}
1331616
def subsample_input

      if @options[:skip_subsample]
        logger.info "Skipping subsample step (--skip-subsample is on)"
        @options[:left_subset] = @options[:left]
        @options[:right_subset] = @options[:right]
        return
      end

      logger.info "Subsampling reads"

      seed = @options[:seed]
      seed = Time.now.to_i if seed == -1
      logger.info "Using random seed #{seed}"
      l = @options[:left]
      r = @options[:right]
      size = @options[:subsample_size]

      sampler = Sampler.new
      if @options[:sampler] == "stream"
        ls, rs = sampler.sample_stream(l, r, size, seed)
      elsif @options[:sampler] == "graph"
        ls, rs = sampler.sample_graph(l, r, size, seed)
      else
        logger.error "sampler #{@options[:sampler]} was not a valid choice"
        logger.error "please user --help to see the options"
        exit 1
      end

      @options[:left_subset] = ls
      @options[:right_subset] = rs

    end
167216
function createSamples(src) {
  var result = [];

  src
    .replace(/…/, '')
    .trim()
    .replace(/,$/, '')
    .split(',')
    .map(function (val) { return val.trim(); })
    .forEach(function (val) {
      if (val.indexOf('~') !== -1) {
        result = result.concat(fillRange(val));
      } else {
        // push test data as String
        result.push(val);
        // push test data as Number if the same
        if (String(+val) === val) { result.push(+val); }
      }
    });

  return result;
}
311907
def generate_samples(label, num_splits, sampler):
    
    def listify(x):
        return x if isinstance(x, list) else [x]
    label_splits = listify(label.split(num_splits, axis=0))
    prob_samples = []
    prob_targets = []
    samples = []
    for label_split in label_splits:
        label_split_2d = label_split.reshape((-1,1))
        sampled_value = sampler.draw(label_split_2d)
        sampled_classes, exp_cnt_true, exp_cnt_sampled = sampled_value
        samples.append(sampled_classes.astype(np.float32))
        prob_targets.append(exp_cnt_true.astype(np.float32).reshape((-1,1)))
        prob_samples.append(exp_cnt_sampled.astype(np.float32))
    return samples, prob_samples, prob_targets
403785
def downsample_completeness_table(comp_table, sample_width=0.1, mmax=None):
    
    new_comp_table = []
    for i in range(comp_table.shape[0] - 1):
        mvals = np.arange(comp_table[i, 1],
                          comp_table[i + 1, 1], d_m)  # FIXME: d_m is undefined!
        new_comp_table.extend([[comp_table[i, 0], mval] for mval in mvals])
    # If mmax > last magnitude in completeness table
    if mmax and (mmax > comp_table[-1, 1]):
        new_comp_table.extend(
            [[comp_table[-1, 0], mval]
             for mval in np.arange(comp_table[-1, 1], mmax + d_m, d_m)])
    return np.array(new_comp_table)
687177
def sample(path, start, length):
    
    # initialize vocabulary and sequence length
    param.seq_len = 1
    ds = CharRNNData(param.corpus, 100000)

    pred = OfflinePredictor(PredictConfig(
        model=Model(),
        session_init=SaverRestore(path),
        input_names=['input', 'c0', 'h0', 'c1', 'h1'],
        output_names=['prob', 'last_state']))

    # feed the starting sentence
    initial = np.zeros((1, param.rnn_size))
    for c in start[:-1]:
        x = np.array([[ds.char2idx[c]]], dtype='int32')
        _, state = pred(x, initial, initial, initial, initial)

    def pick(prob):
        t = np.cumsum(prob)
        s = np.sum(prob)
        return(int(np.searchsorted(t, np.random.rand(1) * s)))

    # generate more
    ret = start
    c = start[-1]
    for k in range(length):
        x = np.array([[ds.char2idx[c]]], dtype='int32')
        prob, state = pred(x, state[0, 0], state[0, 1], state[1, 0], state[1, 1])
        c = ds.chars[pick(prob[0])]
        ret += c
    print(ret)
10997
def experimentVaryingSynapseSampling(expParams,
                                     sampleSizeDistalList,
                                     sampleSizeProximalList):
  
  numRpts = 20
  df = None
  args = []
  for sampleSizeProximal in sampleSizeProximalList:
    for sampleSizeDistal in sampleSizeDistalList:

      for rpt in range(numRpts):
        l4Params = getL4Params()
        l2Params = getL2Params()
        l2Params["sampleSizeProximal"] = sampleSizeProximal
        l2Params["minThresholdProximal"] = sampleSizeProximal
        l2Params["sampleSizeDistal"] = sampleSizeDistal
        l2Params["activationThresholdDistal"] = sampleSizeDistal

        args.append(
          {
            "numObjects": expParams['numObjects'],
            "numLocations": expParams['numLocations'],
            "numFeatures": expParams['numFeatures'],
            "numColumns": expParams['numColumns'],
            "trialNum": rpt,
            "l4Params": l4Params,
            "l2Params": l2Params,
            "profile": True,
            "objectSeed": rpt,
          }
        )

  pool = Pool(processes=expParams['numWorkers'])
  result = pool.map(runExperiment, args)

  #
  #       if df is None:
  #         df = pd.DataFrame.from_dict(result, orient='index')
  #       else:
  #         df = pd.concat([df, pd.DataFrame.from_dict(result, orient='index')], axis=1)
  #
  # df = df.transpose()
  return result
95798
def filter_samples(names, seqs, sep='.'):
    
    seen = set()
    filtered_names, filtered_seqs = [], []
    for name, seq in zip(names, seqs):
        samp = name.split(sep, 1)[0]
        if samp in seen:
            continue
        seen.add(samp)
        filtered_names.append(name)
        filtered_seqs.append(seq)

    nfiltered, nnames = len(filtered_names), len(names)
    assert nfiltered == len(seen)

    return filtered_names, filtered_seqs, seen
204873
function buildSample (sampleNumber, rawData, sendCounts) {
  let sample;
  if (sendCounts) {
    sample = newSampleNoScale(sampleNumber);
    sample.channelDataCounts = rawData;
  } else {
    sample = newSample(sampleNumber);
    for (let j = 0; j < k.OBCINumberOfChannelsGanglion; j++) {
      sample.channelData.push(rawData[j] * k.OBCIGanglionScaleFactorPerCountVolts);
    }
  }
  sample.timestamp = Date.now();
  return sample;
}
1360442
def _pad_arrays(t, arrays, indices, span, period):
    """"""
    N = len(t)

    if indices is None:
        indices = np.arange(N)
    pad_left = max(0, 0 - np.min(indices - span // 2))
    pad_right = max(0, np.max(indices + span - span // 2) - (N - 1))

    if pad_left + pad_right > 0:
        Nright, pad_right = divmod(pad_right, N)
        Nleft, pad_left = divmod(pad_left, N)
        t = np.concatenate([t[N - pad_left:] - (Nleft + 1) * period]
                           + [t + i * period
                              for i in range(-Nleft, Nright + 1)]
                           + [t[:pad_right] + (Nright + 1) * period])
        arrays = [np.concatenate([a[N - pad_left:]]
                                 + (Nleft + Nright + 1) * [a]
                                 + [a[:pad_right]])
                  for a in arrays]
        pad_left = pad_left % N
        Nright = pad_right / N
        pad_right = pad_right % N

        return (t, arrays, slice(pad_left + Nleft * N,
                                 pad_left + (Nleft + 1) * N))
    else:
        return (t, arrays, slice(None))
86617
def bb_pad_collate(samples:BatchSamples, pad_idx:int=0) -> Tuple[FloatTensor, Tuple[LongTensor, LongTensor]]:
    ""
    if isinstance(samples[0][1], int): return data_collate(samples)
    max_len = max([len(s[1].data[1]) for s in samples])
    bboxes = torch.zeros(len(samples), max_len, 4)
    labels = torch.zeros(len(samples), max_len).long() + pad_idx
    imgs = []
    for i,s in enumerate(samples):
        imgs.append(s[0].data[None])
        bbs, lbls = s[1].data
        if not (bbs.nelement() == 0):
            bboxes[i,-len(lbls):] = bbs
            labels[i,-len(lbls):] = tensor(lbls)
    return torch.cat(imgs,0), (bboxes,labels)
728449
def generateSequences(n=2048, w=40, sequenceLength=5, sequenceCount=2,
                      sharedRange=None, seed=42):
  
  # Lots of room for noise sdrs
  patternAlphabetSize = 10*(sequenceLength * sequenceCount)
  patternMachine = PatternMachine(n, w, patternAlphabetSize, seed)
  sequenceMachine = SequenceMachine(patternMachine, seed)
  numbers = sequenceMachine.generateNumbers(sequenceCount, sequenceLength,
                                            sharedRange=sharedRange )
  generatedSequences = sequenceMachine.generateFromNumbers(numbers)

  return sequenceMachine, generatedSequences, numbers
344807
def make_sample():
    
    if md_sample:
        # user has set another:
        return md_sample
    _md = []
    for hl in range(1, 7): _md.append('#' * hl + ' ' + 'Header %s' % hl)
    this = open(__file__).read().split('"""', 3)[2].splitlines()[:10]
    _md.append('```python\n""" Test """\n%s\n```' % '\n'.join(this).strip())
    _md.append("""
| Tables        | Fmt |
| -- | -- |
| !!! hint: wrapped | 0.1 **strong** |
    """)
    for ad in admons.keys()[:1]:
        _md.append('!!! %s: title\n    this is a %s\n' % (ad, ad.capitalize()))
    # 'this theme' replaced in the roller (but not at mdv w/o args):
    globals()['md_sample'] = \
            '\n'.join(_md) + '\n----\n!!! question: %s' % you_like
55733
def char_pad_trunc(data, maxlen):
    
    new_dataset = []
    for sample in data:
        if len(sample) > maxlen:
            new_data = sample[:maxlen]
        elif len(sample) < maxlen:
            pads = maxlen - len(sample)
            new_data = sample + ['PAD'] * pads
        else:
            new_data = sample
        new_dataset.append(new_data)
    return new_dataset
766624
def replace_random_tokens(self,
                              n_samples,  # type: int
                              replacement='',  # type: str
                              random_state=None,
                              min_replace=1,  # type: Union[int, float]
                              max_replace=1.0,  # type: Union[int, float]
                              group_size=1  # type: int
                              ):
        # type: (...) -> List[Tuple[str, int, np.ndarray]]
        
        n_tokens = len(self.tokens)
        indices = np.arange(n_tokens)
        if not n_tokens:
            nomask = np.array([], dtype=int)
            return [('', 0, nomask)] * n_samples

        min_replace, max_replace = self._get_min_max(min_replace, max_replace,
                                                     n_tokens)
        rng = check_random_state(random_state)
        replace_sizes = rng.randint(low=min_replace, high=max_replace + 1,
                                    size=n_samples)
        res = []
        for num_to_replace in replace_sizes:
            idx_to_replace = rng.choice(indices, num_to_replace, replace=False)
            idx_to_replace = np.array([idx_to_replace] + [
                idx_to_replace + shift for shift in range(1, group_size)
            ]).ravel()
            padded_size = n_tokens + group_size - 1
            mask = indices_to_bool_mask(idx_to_replace, padded_size)[:n_tokens]
            s = self.split.masked(mask, replacement)
            res.append((s.text, num_to_replace, mask))
        return res