Sorts mass and intensities arrays in descending intensity order,
        then removes low-intensity peaks from the spectrum.

        :param n_peaks: number of peaks to keep
341303
def trim(self, n_peaks):
        
        self.sortByIntensity()
        ims.spectrum_trim(self.ptr, n_peaks)
983805
def _sampler_n_samples(self, n_samples):
        
        sampler_indices = self.rng_.choice(range(len(self.samplers)),
                                           size=n_samples,
                                           replace=True,
                                           p=self.weights)
        return [
            (self.samplers[idx], freq)
            for idx, freq in itemfreq(sampler_indices)
        ]
1361351
def sanitize(self):
        
        name = self[self._KEYS.NAME]

        aliases = self.get_aliases(includename=False)
        if name not in aliases:
            # Assign the first source to alias, if not available assign us.
            if self._KEYS.SOURCES in self:
                self.add_quantity(self._KEYS.ALIAS, name, '1')
                if self._KEYS.ALIAS not in self:
                    source = self.add_self_source()
                    self.add_quantity(self._KEYS.ALIAS, name, source)
            else:
                source = self.add_self_source()
                self.add_quantity(self._KEYS.ALIAS, name, source)

        if self._KEYS.ALIAS in self:
            self[self._KEYS.ALIAS].sort(
                key=lambda key: alias_priority(name, key[QUANTITY.VALUE]))
        else:
            self._log.error(
                'There should be at least one alias for `{}`.'.format(name))

        if self._KEYS.PHOTOMETRY in self:
            self[self._KEYS.PHOTOMETRY].sort(
                key=lambda x: ((float(x[PHOTOMETRY.TIME]) if
                                isinstance(x[PHOTOMETRY.TIME],
                                           (basestring, float, int))
                                else min([float(y) for y in
                                          x[PHOTOMETRY.TIME]])) if
                               PHOTOMETRY.TIME in x else 0.0,
                               x[PHOTOMETRY.BAND] if PHOTOMETRY.BAND in
                               x else '',
                               float(x[PHOTOMETRY.MAGNITUDE]) if
                               PHOTOMETRY.MAGNITUDE in x else ''))

        if (self._KEYS.SPECTRA in self and list(
                filter(None, [
                    SPECTRUM.TIME in x for x in self[self._KEYS.SPECTRA]
                ]))):
            self[self._KEYS.SPECTRA].sort(
                key=lambda x: (float(x[SPECTRUM.TIME]) if
                               SPECTRUM.TIME in x else 0.0,
                               x[SPECTRUM.FILENAME] if
                               SPECTRUM.FILENAME in x else '')
            )

        if self._KEYS.SOURCES in self:
            # Remove orphan sources
            source_aliases = [
                x[SOURCE.ALIAS] for x in self[self._KEYS.SOURCES]
            ]
            # Sources with the `PRIVATE` attribute are always retained
            source_list = [
                x[SOURCE.ALIAS] for x in self[self._KEYS.SOURCES]
                if SOURCE.PRIVATE in x
            ]
            for key in self:
                # if self._KEYS.get_key_by_name(key).no_source:
                if (key in [
                        self._KEYS.NAME, self._KEYS.SCHEMA, self._KEYS.SOURCES,
                        self._KEYS.ERRORS
                ]):
                    continue
                for item in self[key]:
                    source_list += item[item._KEYS.SOURCE].split(',')
            new_src_list = sorted(
                list(set(source_aliases).intersection(source_list)))
            new_sources = []
            for source in self[self._KEYS.SOURCES]:
                if source[SOURCE.ALIAS] in new_src_list:
                    new_sources.append(source)
                else:
                    self._log.info('Removing orphaned source from `{}`.'
                                   .format(name))

            if not new_sources:
                del self[self._KEYS.SOURCES]

            self[self._KEYS.SOURCES] = new_sources
1368984
def mask_to_n_loudest_clustered_events(self, n_loudest=10,
                                           ranking_statistic="newsnr",
                                           cluster_window=10):
        """"""
        # If this becomes memory intensive we can optimize
        stat_instance = sngl_statistic_dict[ranking_statistic]([])
        stat = stat_instance.single(self.trigs)[self.mask]

        # Used for naming in plots ... Seems an odd place for this to live!
        if ranking_statistic == "newsnr":
            self.stat_name = "Reweighted SNR"
        elif ranking_statistic == "newsnr_sgveto":
            self.stat_name = "Reweighted SNR (+sgveto)"
        elif ranking_statistic == "newsnr_sgveto_psdvar":
            self.stat_name = "Reweighted SNR (+sgveto+psdvar)"
        elif ranking_statistic == "snr":
            self.stat_name = "SNR"
        else:
            self.stat_name = ranking_statistic

        times = self.end_time
        index = stat.argsort()[::-1]
        new_times = []
        new_index = []
        for curr_idx in index:
            curr_time = times[curr_idx]
            for time in new_times:
                if abs(curr_time - time) < cluster_window:
                    break
            else:
                # Only get here if no other triggers within cluster window
                new_index.append(curr_idx)
                new_times.append(curr_time)
            if len(new_index) >= n_loudest:
                break
        index = np.array(new_index)
        self.stat = stat[index]
        if self.mask.dtype == 'bool':
            orig_indices = np.flatnonzero(self.mask)[index]
            self.mask[:] = False
            self.mask[orig_indices] = True
        else:
            self.mask = self.mask[index]
570855
def find_peak(data, cutoff=.9):
    '''
    
    '''
    total_length = sum(data.values())
    count_cutoff = cutoff * total_length
    cum_sum = 0
    for i, count in sorted(data.items()):
        cum_sum += count
        if cum_sum > count_cutoff:
            return i
371348
def sort_top_targets(self, top, orders):
        '''
        
        '''
        sorted_top = collections.defaultdict(OrderedDict)
        # pylint: disable=cell-var-from-loop
        for saltenv, targets in six.iteritems(top):
            sorted_targets = sorted(targets,
                    key=lambda target: orders[saltenv][target])
            for target in sorted_targets:
                sorted_top[saltenv][target] = targets[target]
        # pylint: enable=cell-var-from-loop
        return sorted_top
380058
def min_max_indexes(seq):
    
    l = sorted(enumerate(seq), key=lambda s: s[1])
    return l[0][0], l[-1][0]
83401
def stack(S):
    
    S, nreps = zip(*S)
    S = np.array([list(x) for x in S])
    rows, cols = S.shape
    counts = []
    for c in xrange(cols):
        freq = [0] * NBASES
        for b, nrep in zip(S[:, c], nreps):
            freq[BASES.index(b)] += nrep
        counts.append(freq)
    return counts
150931
def select_spikes(cluster_ids=None,
                  max_n_spikes_per_cluster=None,
                  spikes_per_cluster=None,
                  batch_size=None,
                  subset=None,
                  ):
    """"""
    subset = subset or 'regular'
    assert _is_array_like(cluster_ids)
    if not len(cluster_ids):
        return np.array([], dtype=np.int64)
    if max_n_spikes_per_cluster in (None, 0):
        selection = {c: spikes_per_cluster(c) for c in cluster_ids}
    else:
        assert max_n_spikes_per_cluster > 0
        selection = {}
        n_clusters = len(cluster_ids)
        for cluster in cluster_ids:
            # Decrease the number of spikes per cluster when there
            # are more clusters.
            n = int(max_n_spikes_per_cluster * exp(-.1 * (n_clusters - 1)))
            n = max(1, n)
            spike_ids = spikes_per_cluster(cluster)
            if subset == 'regular':
                # Regular subselection.
                if batch_size is None or len(spike_ids) <= max(batch_size, n):
                    spike_ids = regular_subset(spike_ids, n_spikes_max=n)
                else:
                    # Batch selections of spikes.
                    spike_ids = get_excerpts(spike_ids,
                                             n // batch_size,
                                             batch_size)
            elif subset == 'random' and len(spike_ids) > n:
                # Random subselection.
                spike_ids = np.random.choice(spike_ids, n, replace=False)
                spike_ids = np.unique(spike_ids)
            selection[cluster] = spike_ids
    return _flatten_per_cluster(selection)
682904
def get_signal_peaks_and_prominences(data):
    
    peaks, _ = sig.find_peaks(data)
    prominences = sig.peak_prominences(data, peaks)[0]
        
    return peaks, prominences
169217
def band_reorder(self):
        
        eiv = self.eigendisplacements
        eig = self.bands
    
        nphonons,nqpoints = self.bands.shape
        order = np.zeros([nqpoints,nphonons],dtype=int)
        order[0] = np.array(range(nphonons))
        
        #get the atomic masses
        atomic_masses = [ site.specie.atomic_mass for site in self.structure.sites ]
 
        #get order
        for nq in range(1,nqpoints):
            old_eiv = eigenvectors_from_displacements(eiv[:,nq-1],atomic_masses)
            new_eiv = eigenvectors_from_displacements(eiv[:,nq],  atomic_masses)
            order[nq] = estimate_band_connection(old_eiv.reshape([nphonons,nphonons]).T,
                                                 new_eiv.reshape([nphonons,nphonons]).T,
                                                 order[nq-1])

        #reorder
        for nq in range(1,nqpoints):
            eivq=eiv[:,nq]
            eigq=eig[:,nq]
            eiv[:,nq] = eivq[order[nq]]
            eig[:,nq] = eigq[order[nq]]
883695
def _extract_peaks(specgram, neighborhood, threshold):
    
    kernel = np.ones(shape=neighborhood)
    local_averages = convolve(specgram, kernel / kernel.sum(), mode="constant", cval=0)

    # suppress all points below the floor value
    floor = (1 + threshold) * local_averages
    candidates = np.where(specgram > floor, specgram, 0)

    # grayscale dilation is equivalent to non-maximal suppression
    local_maximums = grey_dilation(candidates, footprint=kernel)
    peak_coords = np.argwhere(specgram == local_maximums)
    peaks = zip(peak_coords[:, 0], peak_coords[:, 1])

    return peaks
1092487
def sortmwarai(datablock, exp_type):
    
    first_Z, first_I, ptrm_check, ptrm_tail, zptrm_check = [], [], [], [], []
    field, phi, theta = "", "", ""
    POWT_I, POWT_Z, POWT_PZ, POWT_PI, POWT_M = [], [], [], [], []
    ISteps, ZSteps, PZSteps, PISteps, MSteps = [], [], [], [], []
    rad = old_div(np.pi, 180.)
    ThetaChecks = []
    DeltaChecks = []
    GammaChecks = []
# first find all the steps
    for k in range(len(datablock)):
        rec = datablock[k]
        powt = int(float(rec["treatment_mw_energy"]))
        methcodes = []
        tmp = rec["magic_method_codes"].split(":")
        for meth in tmp:
            methcodes.append(meth.strip())
        if 'LT-M-I' in methcodes and 'LP-MRM' not in methcodes:
            POWT_I.append(powt)
            ISteps.append(k)
            if field == "":
                field = float(rec['treatment_dc_field'])
            if phi == "":
                phi = float(rec['treatment_dc_field_phi'])
                theta = float(rec['treatment_dc_field_theta'])
        if 'LT-M-Z' in methcodes:
            POWT_Z.append(powt)
            ZSteps.append(k)
        if 'LT-PMRM-Z' in methcodes:
            POWT_PZ.append(powt)
            PZSteps.append(k)
        if 'LT-PMRM-I' in methcodes:
            POWT_PI.append(powt)
            PISteps.append(k)
        if 'LT-PMRM-MD' in methcodes:
            POWT_M.append(powt)
            MSteps.append(k)
        if 'LT-NO' in methcodes:
            dec = float(rec["measurement_dec"])
            inc = float(rec["measurement_inc"])
            st = float(rec["measurement_magn_moment"])
            first_I.append([0, 0., 0., 0., 1])
            first_Z.append([0, dec, inc, st, 1])  # NRM step
    if exp_type == "LP-PI-M-D":
        # now look trough infield steps and  find matching Z step
        for powt in POWT_I:
            if powt in POWT_Z:
                istep = ISteps[POWT_I.index(powt)]
                irec = datablock[istep]
                methcodes = []
                tmp = irec["magic_method_codes"].split(":")
                for meth in tmp:
                    methcodes.append(meth.strip())
                # take last record as baseline to subtract
                brec = datablock[istep - 1]
                zstep = ZSteps[POWT_Z.index(powt)]
                zrec = datablock[zstep]
    # sort out first_Z records
                if "LP-PI-M-IZ" in methcodes:
                    ZI = 0
                else:
                    ZI = 1
                dec = float(zrec["measurement_dec"])
                inc = float(zrec["measurement_inc"])
                st = float(zrec["measurement_magn_moment"])
                first_Z.append([powt, dec, inc, st, ZI])
    # sort out first_I records
                idec = float(irec["measurement_dec"])
                iinc = float(irec["measurement_inc"])
                istr = float(irec["measurement_magn_moment"])
                X = dir2cart([idec, iinc, istr])
                BL = dir2cart([dec, inc, st])
                I = []
                for c in range(3):
                    I.append((X[c] - BL[c]))
                iDir = cart2dir(I)
                first_I.append([powt, iDir[0], iDir[1], iDir[2], ZI])
# put in Gamma check (infield trm versus lab field)
                gamma = angle([iDir[0], iDir[1]], [phi, theta])
                GammaChecks.append([powt, gamma])
    elif exp_type == "LP-PI-M-S":
        # find last zero field step before first infield step
        lzrec = datablock[ISteps[0] - 1]
        irec = datablock[ISteps[0]]
        ndec = float(lzrec["measurement_dec"])
        ninc = float(lzrec["measurement_inc"])
        nstr = float(lzrec["measurement_magn_moment"])
        NRM = dir2cart([ndec, ninc, nstr])
        fdec = float(irec["treatment_dc_field_phi"])
        finc = float(irec["treatment_dc_field_theta"])
        Flab = dir2cart([fdec, finc, 1.])
        for step in ISteps:
            irec = datablock[step]
            rdec = float(irec["measurement_dec"])
            rinc = float(irec["measurement_inc"])
            rstr = float(irec["measurement_magn_moment"])
            theta1 = angle([ndec, ninc], [rdec, rinc])
            theta2 = angle([rdec, rinc], [fdec, finc])
            powt = int(float(irec["treatment_mw_energy"]))
            ThetaChecks.append([powt, theta1 + theta2])
            p = (180. - (theta1 + theta2))
            nstr = rstr * (old_div(np.sin(theta2 * rad), np.sin(p * rad)))
            tmstr = rstr * (old_div(np.sin(theta1 * rad), np.sin(p * rad)))
            first_Z.append([powt, ndec, ninc, nstr, 1])
            first_I.append([powt, dec, inc, tmstr, 1])
# check if zero field steps are parallel to assumed NRM
        for step in ZSteps:
            zrec = datablock[step]
            powt = int(float(zrec["treatment_mw_energy"]))
            zdec = float(zrec["measurement_dec"])
            zinc = float(zrec["measurement_inc"])
            delta = angle([ndec, ninc], [zdec, zinc])
            DeltaChecks.append([powt, delta])
    # get pTRMs together - take previous record and subtract
    for powt in POWT_PI:
        step = PISteps[POWT_PI.index(powt)]
        rec = datablock[step]
        dec = float(rec["measurement_dec"])
        inc = float(rec["measurement_inc"])
        st = float(rec["measurement_magn_moment"])
        brec = datablock[step - 1]  # take last record as baseline to subtract
        pdec = float(brec["measurement_dec"])
        pinc = float(brec["measurement_inc"])
        pint = float(brec["measurement_magn_moment"])
        X = dir2cart([dec, inc, st])
        prevX = dir2cart([pdec, pinc, pint])
        I = []
        for c in range(3):
            I.append(X[c] - prevX[c])
        dir1 = cart2dir(I)
        ptrm_check.append([powt, dir1[0], dir1[1], dir1[2]])
    # get zero field pTRM  checks together
    for powt in POWT_PZ:
        step = PZSteps[POWT_PZ.index(powt)]
        rec = datablock[step]
        dec = float(rec["measurement_dec"])
        inc = float(rec["measurement_inc"])
        st = float(rec["measurement_magn_moment"])
        brec = datablock[step - 1]
        pdec = float(brec["measurement_dec"])
        pinc = float(brec["measurement_inc"])
        pint = float(brec["measurement_magn_moment"])
        X = dir2cart([dec, inc, st])
        prevX = dir2cart([pdec, pinc, pint])
        I = []
        for c in range(3):
            I.append(X[c] - prevX[c])
        dir2 = cart2dir(I)
        zptrm_check.append([powt, dir2[0], dir2[1], dir2[2]])
    # get pTRM tail checks together -
    for powt in POWT_M:
        step = MSteps[POWT_M.index(powt)]  # tail check step
        rec = datablock[step]
#        dec=float(rec["measurement_dec"])
#        inc=float(rec["measurement_inc"])
        st = float(rec["measurement_magn_moment"])
        step = ZSteps[POWT_Z.index(powt)]
        brec = datablock[step]
#        pdec=float(brec["measurement_dec"])
#        pinc=float(brec["measurement_inc"])
        pint = float(brec["measurement_magn_moment"])
#        X=dir2cart([dec,inc,st])
#        prevX=dir2cart([pdec,pinc,pint])
#        I=[]
#        for c in range(3):I.append(X[c]-prevX[c])
#        d=cart2dir(I)
 #       ptrm_tail.append([powt,d[0],d[1],d[2]])
        # just do absolute magnitude difference # not vector diff
        ptrm_tail.append([powt, 0, 0, st - pint])
    #  check
    #
        if len(first_Z) != len(first_I):
            print(len(first_Z), len(first_I))
            print(" Something wrong with this specimen! Better fix it or delete it ")
            input(" press return to acknowledge message")
            print(MaxRec)
    araiblock = (first_Z, first_I, ptrm_check, ptrm_tail,
                 zptrm_check, GammaChecks, ThetaChecks, DeltaChecks)
    return araiblock, field
446359
def median(array):
    
    n = len(array)

    if n < 1:
        return 0
    elif n == 1:
        return array[0]

    sorted_vals = sorted(array)
    midpoint = int(n / 2)
    if n % 2 == 1:
        return sorted_vals[midpoint]
    else:
        return (sorted_vals[midpoint - 1] + sorted_vals[midpoint]) / 2.0
468510
def peaks(x, y, lookahead=20, delta=0.00003):
    
    _max, _min = peakdetect(y, x, lookahead, delta)
    x_peaks = [p[0] for p in _max]
    y_peaks = [p[1] for p in _max]
    x_valleys = [p[0] for p in _min]
    y_valleys = [p[1] for p in _min]
    
    _peaks = [x_peaks, y_peaks]
    _valleys = [x_valleys, y_valleys]
    return {"peaks": _peaks, "valleys": _valleys}
633775
def sort_values(self, return_indexer=False, ascending=True):
        
        if return_indexer:
            _as = self.argsort()
            if not ascending:
                _as = _as[::-1]
            sorted_index = self.take(_as)
            return sorted_index, _as
        else:
            sorted_values = np.sort(self._ndarray_values)
            attribs = self._get_attributes_dict()
            freq = attribs['freq']

            if freq is not None and not is_period_dtype(self):
                if freq.n > 0 and not ascending:
                    freq = freq * -1
                elif freq.n < 0 and ascending:
                    freq = freq * -1
            attribs['freq'] = freq

            if not ascending:
                sorted_values = sorted_values[::-1]

            return self._simple_new(sorted_values, **attribs)
207781
def median(values):
    
    values.sort()
    n = int(len(values) / 2)
    return values[n]