Calculate downsampling value.
509131
def calc_downsample(w, h, target=400):
    """"""
    if w > h:
        return h / target
    elif h >= w:
        return w / target
357596
def resample(self,N,**kwargs):
        
        lovals = self.mu - np.absolute(rand.normal(size=N)*self.siglo)
        hivals = self.mu + np.absolute(rand.normal(size=N)*self.sighi)

        u = rand.random(size=N)
        hi = (u < float(self.sighi)/(self.sighi + self.siglo))
        lo = (u >= float(self.sighi)/(self.sighi + self.siglo))

        vals = np.zeros(N)
        vals[hi] = hivals[hi]
        vals[lo] = lovals[lo]
        return vals
410631
def _rescale(ar):
    """"""
    max = np.nanmax(ar)
    min = np.nanmin(ar)
    midpoint = (max + min) / 2.0
    return 2.0 * (ar - midpoint) / (max - min)
469384
def create_downsampled_data(params):
    '''
    
    '''
    maxsamples = 1

    for data_type in ['LFP','CSD']:

        if RANK == 0:
            if not os.path.isdir(os.path.join(params.savefolder, 'populations', 'subsamples')):
                os.mkdir((os.path.join(params.savefolder,'populations','subsamples')))
        COMM.Barrier()

        try:
            assert(ana_params.scaling <= params.recordSingleContribFrac)
        except AssertionError as ae:
            raise ae, 'scaling parameter must be less than simulation recordSingleContribFrac'
        
        samples = int(1. / ana_params.scaling)
        if samples > maxsamples:
            samples = maxsamples
        
        COUNTER = 0
        for j, layer in enumerate(params.y_in_Y): # loop over layers
            for k, pop in enumerate(layer): # loop over populations
                for i, y in enumerate(pop): # loop over cell types
                    if COUNTER % SIZE == RANK:
                        # Load data
                        fname = os.path.join(params.savefolder, 'populations', '%s_%ss.h5' \
                                             % (y, data_type))
                        f = h5py.File(fname)
                        print 'Load %s' % str(f.filename)
                        raw_data = f['data'].value
                        srate = f['srate'].value
                        f.close()

                        ## shuffle data
                        #np.random.shuffle(raw_data)

                        # sample size
                        N = int(params.N_y[np.array(params.y) == y]*ana_params.scaling)
                        try:
                            assert(N <= raw_data.shape[0])
                        except AssertionError as ae:
                            raise ae, 'shape mismatch with sample size'


                        for sample in range(samples): # loop over samples

                            # slice data
                            data = raw_data[sample*N:(sample+1)*N]

                            # create cell resolved file
                            fname = os.path.join(params.savefolder,'populations','subsamples',
                                                 '%s_%ss_%i_%i.h5' \
                                                 % (y, data_type, ana_params.scaling*100, sample))
                            f = h5py.File(fname, 'w')
                            print 'Write %s' % str(f.filename)
                            f['data'] = data
                            f['srate'] = srate
                            f.close()

                            # create cell type resolved file
                            fname = os.path.join(params.savefolder,'populations','subsamples',
                                                 '%s_population_%s_%i_%i.h5' \
                                                 % (y,data_type,ana_params.scaling*100,sample))
                            f = h5py.File(fname, 'w')
                            print 'Write %s' % str(f.filename)
                            f['data'] = data.sum(axis=0)
                            f['srate'] = srate
                            f.close()

                    COUNTER += 1

        COMM.Barrier()
        f = h5py.File(os.path.join(params.savefolder,'populations', '%s_%ss.h5' % (y,data_type)), 'r')
        datashape = f['data'].shape
        f.close()

        COUNTER = 0
        for sample in range(samples): # loop over samples
            if COUNTER % SIZE == RANK:
                # initialize full sum signal
                data_full = np.zeros(datashape[1:])
                for j,layer in enumerate(params.y_in_Y): # loop over layers
                    for k,pop in enumerate(layer): # loop over populations

                        # initialize population resolved sum signal
                        data_Y = np.zeros(datashape[1:])

                        for i,y in enumerate(pop): # loop over cell types

                            # Load data
                            fname = os.path.join(params.savefolder, 'populations',
                                                 'subsamples', '%s_population_%s_%i_%i.h5' \
                                                 % (y, data_type, ana_params.scaling*100,
                                                    sample))
                            f = h5py.File(fname, 'r')    
                            # Update population sum:
                            data_Y += f['data'].value
                            srate = f['srate'].value
                            f.close()

                        # write population sum
                        fname = os.path.join(params.savefolder,'populations','subsamples',
                                             '%s_population_%s_%i_%i.h5' \
                                             % (params.Y[2*j+k], data_type,
                                                ana_params.scaling*100, sample))
                        f = h5py.File(fname,'w')
                        print 'Write %s' % str(f.filename)
                        f['data'] = data_Y
                        f['srate'] = srate
                        f.close() 

                        # update full sum
                        data_full += data_Y

                # write sum
                fname = os.path.join(params.savefolder,'populations','subsamples',
                                     '%ssum_%i_%i.h5' % (data_type,ana_params.scaling*100,sample))
                f = h5py.File(fname,'w')
                print 'Write %s' % str(f.filename)
                f['data'] = data_full
                f['srate'] = srate
                f.close()

            COUNTER += 1

        COMM.Barrier()
13294
def min(self):
        """"""
        if "min" not in self.attrs.keys():

            def f(dataset, s):
                return np.nanmin(dataset[s])

            self.attrs["min"] = np.nanmin(list(self.chunkwise(f).values()))
        return self.attrs["min"]
36109
def downsample_with_striding(array, factor):
    
    return array[tuple(np.s_[::f] for f in factor)]
347171
def _get_noise(self, residuals: np.ndarray) -> float:
        
        return np.mean(np.abs(residuals))
1286730
def downsample(f1, f2, data, N, quick=False):
    
    if quick:
        rand_records = range(N)
    else:
        records = sum(1 for _ in open(f1)) / 4
        N = records if N > records else N
        rand_records = sorted(random.sample(xrange(records), N))

    fh1 = open_possible_gzip(f1)
    fh2 = open_possible_gzip(f2) if f2 else None
    outf1 = os.path.splitext(f1)[0] + ".subset" + os.path.splitext(f1)[1]
    outf2 = os.path.splitext(f2)[0] + ".subset" + os.path.splitext(f2)[1] if f2 else None

    if utils.file_exists(outf1):
        if not outf2:
            return outf1, outf2
        elif utils.file_exists(outf2):
            return outf1, outf2

    out_files = (outf1, outf2) if outf2 else (outf1)

    with file_transaction(out_files) as tx_out_files:
        if isinstance(tx_out_files, six.string_types):
            tx_out_f1 = tx_out_files
        else:
            tx_out_f1, tx_out_f2 = tx_out_files
        sub1 = open_possible_gzip(tx_out_f1, "w")
        sub2 = open_possible_gzip(tx_out_f2, "w") if outf2 else None
        rec_no = - 1
        for rr in rand_records:
            while rec_no < rr:
                rec_no += 1
                for i in range(4): fh1.readline()
                if fh2:
                    for i in range(4): fh2.readline()
            for i in range(4):
                sub1.write(fh1.readline())
                if sub2:
                    sub2.write(fh2.readline())
            rec_no += 1
        fh1.close()
        sub1.close()
        if f2:
            fh2.close()
            sub2.close()

    return outf1, outf2
660523
def _secondary_min(self):
        """"""
        return (
            self.secondary_range[0]
            if (self.secondary_range
                and self.secondary_range[0] is not None) else
            (min(self._secondary_values) if self._secondary_values else None)
        )
962851
def _get_a2_value(bbar, dbar, slip, beta, mmax):
        
        return ((dbar - bbar) / bbar) * (slip / beta) *\
            np.exp(-(dbar / 2.) * mmax)
713467
def resample(self,N):
        
        inds = rand.randint(len(self.samples),size=N)
        return self.samples[inds]
496977
def calculate_upsample_output_shapes(operator):
    '''
    
    '''
    check_input_and_output_numbers(operator, input_count_range=1, output_count_range=1)
    check_input_and_output_types(operator, good_input_types=[FloatTensorType])

    scales = operator.raw_operator.upsample.scalingFactor

    output_shape = copy.deepcopy(operator.inputs[0].type.shape)
    output_shape[2] *= scales[0]
    output_shape[3] *= scales[1]

    operator.outputs[0].type = FloatTensorType(output_shape, doc_string=operator.outputs[0].type.doc_string)
1015282
def Downsampled(cls, stats, interval=None):
    
    interval = interval or cls.DEFAULT_SAMPLING_INTERVAL

    result = cls(stats)
    result.cpu_samples = cls._Downsample(
        kind=CpuSample, samples=stats.cpu_samples, interval=interval)
    result.io_samples = cls._Downsample(
        kind=IOSample, samples=stats.io_samples, interval=interval)
    return result
732587
def subsample(self, down_to=1, new_path=None):
        """"""
        # Auto path #
        if new_path is None: subsampled = self.__class__(new_temp_path())
        elif isinstance(new_path, FASTA): subsampled = new_path
        else:                subsampled = self.__class__(new_path)
        # Check size #
        if down_to > len(self):
            message = "Can't subsample %s down to %i. Only down to %i."
            print Color.ylw + message % (self, down_to, len(self)) + Color.end
            self.copy(new_path)
            return
        # Do it #
        subsampled.create()
        for seq in isubsample(self, down_to): subsampled.add_seq(seq)
        subsampled.close()
        # Did it work #
        assert len(subsampled) == down_to
        return subsampled
490148
def downsample(self, df=None, ds_type='kmeans', axis='row', num_samples=100, random_state=1000):
    '''
    
    '''

    return downsample_fun.main(self, df, ds_type, axis, num_samples, random_state)
666584
def downsample(in_bam, data, target_counts, work_dir=None):
    
    index(in_bam, data["config"], check_timestamp=False)
    ds_pct = get_downsample_pct(in_bam, target_counts, data)
    if ds_pct:
        out_file = "%s-downsample%s" % os.path.splitext(in_bam)
        if work_dir:
            out_file = os.path.join(work_dir, os.path.basename(out_file))
        if not utils.file_exists(out_file):
            with file_transaction(data, out_file) as tx_out_file:
                samtools = config_utils.get_program("samtools", data["config"])
                num_cores = dd.get_num_cores(data)
                ds_pct = "42." + "{ds_pct:.3}".format(ds_pct=ds_pct).replace("0.", "")
                cmd = ("{samtools} view -O BAM -@ {num_cores} -o {tx_out_file} "
                       "-s {ds_pct} {in_bam}")
                do.run(cmd.format(**locals()), "Downsample BAM file: %s" % os.path.basename(in_bam))
        return out_file
67067
def downsample(self, factor):
    
    if int(factor) != factor or factor < 1:
      raise ValueError("Argument `factor` must be a positive integer greater than or equal to 1. Got: <{}>({})", type(factor), factor)

    paths = self.interjoint_paths()

    for i, path in enumerate(paths):
      paths[i] = np.concatenate(
        (path[0::factor, :], path[-1:, :]) # preserve endpoints
      )

    ds_skel = PrecomputedSkeleton.simple_merge(
      [ PrecomputedSkeleton.from_path(path) for path in paths ]
    ).consolidate()
    ds_skel.id = self.id

    # TODO: I'm sure this could be sped up if need be.
    index = {}
    for i, vert in enumerate(self.vertices):
      vert = tuple(vert)
      index[vert] = i

    for i, vert in enumerate(ds_skel.vertices):
      vert = tuple(vert)
      ds_skel.radii[i] = self.radii[index[vert]]
      ds_skel.vertex_types[i] = self.vertex_types[index[vert]]

    return ds_skel