def add(self, *args):
     """
 
     Add new elements to the list
     
     :param \*args: List of new elements 
 
     """
     for arg in args:
         _id = getattr(arg, 'id')
         if _id in self.map['id']:
             continue
         self.objects[_id] = arg
         _main = reduce(self._iter_func, self.main_sort.split('.'), arg)
         _sec = reduce(self._iter_func, self.break_tie_sort.split('.'), arg)
         _pos = bisect_left(self.main, _main)
         main_pos_r = bisect_right(self.main, _main)
         if _pos == main_pos_r:
             self.list.insert(_pos, _id)
             self.main.insert(_pos, _main)
             self.secondary.insert(_pos, _sec)
         else:
             _pos = bisect_left(self.secondary[_pos:main_pos_r],
                                _sec) + _pos
             self.list.insert(_pos, _id)
             self.main.insert(_pos, _main)
             self.secondary.insert(_pos, _sec)
         self.map_insert(self.map['id'], self.map['pos'], _pos, _id)
Example #2
0
    def get_xic(self, min_mz, max_mz, min_rt=0.0, max_rt=0.0, ms_level=1):
        """
        eXtracted Ion Chromatogram

        :param min_mz:
        :param max_mz:
        :param min_rt:
        :param max_rt:
        :param ms_level:
        :return:
        """
        min_mz_rs = self.run_slices_begin_mz[bisect_left(self.run_slices_begin_mz, min_mz) - 1]
        max_mz_rs = self.run_slices_begin_mz[bisect_left(self.run_slices_begin_mz, max_mz) - 1]

        # print "min max rs", min_mz_rs, max_mz_rs

        if min_mz_rs == max_mz_rs:
            sql_query = """SELECT bounding_box.id, data, run_slice_id, first_spectrum_id
                             FROM bounding_box, run_slice WHERE run_slice.ms_level = ?
                             AND bounding_box.run_slice_id = run_slice.id
                             AND run_slice.begin_mz = ?;"""

            self.cursor.execute(sql_query, (ms_level, min_mz_rs))
        else:
            sql_query = """SELECT bounding_box.id, data, run_slice_id, first_spectrum_id
                             FROM bounding_box, run_slice WHERE run_slice.ms_level = ?
                             AND bounding_box.run_slice_id = run_slice.id
                             AND (run_slice.begin_mz = ? OR run_slice.begin_mz = ?);"""
            self.cursor.execute(sql_query, (ms_level, min_mz_rs, max_mz_rs))

        intensities_by_scan_time = ddict(int)

        for row in self.cursor:
            scan_slices = bb_to_scan_slices(row[1][:], row[2], self.struct_by_scan_id)
            for ss in scan_slices:
                time = self.rt_by_scan_id_by_ms_level[ms_level][ss.scan_id]
                mzs = ss.mzs
                min_idx = bisect_left(mzs, min_mz)
                max_idx = bisect_left(mzs, max_mz)

                ints = ss.ints[min_idx: max_idx]

                if not ints:
                    continue

                m = max(ints)

                # if m > intensities_by_scan_time[time]:
                intensities_by_scan_time[time] = m

        times = sorted(intensities_by_scan_time.viewkeys())
        intensities = [intensities_by_scan_time[t] for t in times]
        return times, intensities
Example #3
0
def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    else:
        return -1
Example #4
0
 def removePeakFromOneCluster(self):
     idx=self.view.treeView_3.selectedIndexes()[0]#supposed the selection exists 
     if not idx.isValid():
         return
     model=idx.model()
     #copy the item
     itemToDelete = model.itemFromIndex(idx)
     item = QStandardItem(itemToDelete)
     #no need to select already selected, then removing
     self.view.treeView_3.removeSelected()
     #putting the copied item in view
     parentIndex = idx.parent().parent().parent()
     sample = self.currentSample[3]#self.model.sample(parentIndex.data().toString(), 
                                #fullNameEntry=False)
     if sample is None:
         print "Unknown error"            
         return
     
     data = map(str, item.text().split('/'))[:2]
     dataParent = map(str, idx.parent().parent().data().toString().split('/'))
     sample.mappedPeaks.sample=sample; sample.rawPeaks.sample=sample#du to one bug don't know why        
     goodPeak = sample.mappedPeaks.peakAt(*map(float, dataParent))
     fragPeak = sample.rawPeaks.peakAt(*map(float, data))
     try:        
         goodPeak.fragCluster.remove(fragPeak)
         goodPeak.isoCluster.remove(fragPeak)
     except ValueError:
         pass            
     #adding item to the treeView
     parentItem = model.itemFromIndex(parentIndex)   
     index = bisect_left(sample.mappedPeaks.masses(), float(item.text().split('/')[0]))
     #model.insertRow(index, item)
     parentItem.insertRow(index, item)      
     self.view.treeView_3.update()
Example #5
0
    def compareTwoCellForFirstAndGetComment(scale, Nodes1, Nodes2,threshold):
        yKey = [item[1] for item in Nodes2]
        newNodes = []
        for item in Nodes1:
            left=bisect_left(yKey,item[1]-threshold)
            right=bisect_right(yKey,item[1]+threshold)
            ySatisList = Nodes2[left: right+1];
            for node in ySatisList:
                if node[2]<(item[2]+threshold) and node[2]>(item[2]-threshold) and node[0]<(item[0]+threshold) and node[0]>(item[0]-threshold):
                    #z= int((item[0])/scale[0])
                   # y= int((item[1])/scale[1])
                    #x= int((item[2])/scale[2])
                    #ide= int(item[3])
                    #cellname = item[4]
                    newnode = []
                    #newnode.append(z)
                    #newnode.append(y)
                    #newnode.append(x)
                    newnode.append(item[3])
                    #newnode.append(cellname)
                    newnode.append(" from " + node[4])
            
                    
                    newNodes.append(newnode)

                    
        return newNodes
Example #6
0
 def compareTwoCellandReturnMidPoint(Nodes1, Nodes2,threshold, i):
     yKey = [item[1] for item in Nodes2]
     newNodes = []
     for item in Nodes1:
         left=bisect_left(yKey,item[1]-threshold)
         right=bisect_right(yKey,item[1]+threshold)
         ySatisList = Nodes2[left: right+1];
         for node in ySatisList:
             if node[2]<(item[2]+threshold) and node[2]>(item[2]-threshold) and node[0]<(item[0]+threshold) and node[0]>(item[0]-threshold):
                 z= int((node[0]+item[0])/52)
                 y= int((node[1]+item[1])/26.4) #IMPORT FIX!!!!!!!!!
                 x= int((node[2]+item[2])/26.4)
                 ide= i
                 cellname = item[4]+"--"+node[4]
                 newnode = []
                 newnode.append(z)
                 newnode.append(y)
                 newnode.append(x)
                 newnode.append(ide)
                 newnode.append(cellname)
         
                 
                 newNodes.append(newnode)
                 i = i+1
     return newNodes
Example #7
0
def insort_left(a, x, lo=0, hi=-1):
    """Insert item x in list a, and keep it sorted assuming a is sorted.

If x is already in a, insert it to the left of the leftmost x.

Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched."""
    n = bisect_left(a, x, lo, hi)
    a.insert(n, x)
Example #8
0
def can_place_point(point, W, D):
    checkW = W[:]
    checkW.insert(bisect_left(W, point), point)
    CWD = distances(checkW)
    UCWD = unique_elements(CWD)
    for i in UCWD:
        if (count(i, CWD) > count(i, D)):
            return False
    return True
Example #9
0
def autoc_matrix(v, D):
    A = numpy.zeros((len(v), len(v)))
    Dl = list(set(D))
    for i in range(0, len(v)):
        for j in range(0, len(v)):
            z = abs(v[j][1] - v[i][1])
            k = bisect_left(Dl, z)
            if (k < len(v) and v[k][1] == z):
                A[k][j] += v[i][0]
    return A
Example #10
0
def get_substring(k):
    insertion = bisect_left(rank, k)
    if insertion == len(rank):
        return None
    suffix_index = rank[insertion]
    suffix = arr_suffix[insertion]
    if suffix_index == k:
        return suffix
    else:
        return suffix[:k - suffix_index]
Example #11
0
 def lookup(self, prefix, lo=0, hi=None):
     """See if prefix is in dictionary, as a full word or as a prefix.
     Return two values: the first is the lowest i such that
     words[i].startswith(prefix), or is None; the second is
     True iff prefix itself is in the Wordlist."""
     words = self.words
     i = bisect.bisect_left(words, prefix, lo, hi)
     if i < len(words) and words[i].startswith(prefix): 
         return i, (words[i] == prefix)
     else: 
         return None, False
Example #12
0
 def takeClosest(self, myList, myNumber):
     pos = bisect_left(myList, myNumber)
     if pos == 0:
         return myList[0], myList[1]
     if pos == len(myList):
         return myList[-1], myList[-2]
     before = myList[pos - 1]
     after = myList[pos]
     if after - myNumber < myNumber - before:
         return after, before
     else:
         return before, after
Example #13
0
def solution(line):
    data, total = line.split(';')
    data = [int(x) for x in data.split(',')]
    total = int(total)
    result = []
    for i in range(len(data)):
        if data[i] * 2 >= total:
            break
        target = total - data[i]
        j = bisect_left(data, target, i+1)
        if j != len(data) and data[j] == target:
            result.append((data[i], data[j]))
    if not result:
        return 'NULL'
    return ';'.join('{},{}'.format(x[0], x[1]) for x in result)
Example #14
0
def solution(line):
    data, total = line.split(';')
    data = [int(x) for x in data.split(',')]
    total = int(total)
    result = []
    for i in range(len(data)):
        if data[i] * 2 >= total:
            break
        target = total - data[i]
        j = bisect_left(data, target, i + 1)
        if j != len(data) and data[j] == target:
            result.append((data[i], data[j]))
    if not result:
        return 'NULL'
    return ';'.join('{},{}'.format(x[0], x[1]) for x in result)
Example #15
0
    def cutCell2(nodes, ratio1, ratio2):
       
        aArray =  nodes.sort(key = lambda x:x[2])
        #print aArray 
        xKey = []
        
        for item in aArray:
            xKey.append(item[2])
        
        
        #xKey = [thing[2] for thing in aArray]
            
        xTop = bisect_left(xKey, int(ratio2 * len(aArray)))
        xBot = bisect_left(xKey, int(ratio1 * len(aArray)))
        
            #xSatisList = aArray[0: cutVal]        
        
        xSatisList1 = aArray[:xTop]
 
 
        xSatisList2 = xSatisList1[xBot:]
 
        
        return xSatisList2
Example #16
0
    def get_scan_for_time(self, time, ms_level=1):
        """
        retrun the closest scan in time specified by time parameter
        ms_level is kw argument if a ms2 or ms1 scan is wanted

        :param time:
        :param ms_level:
        :return:
        """
        rt_by_scan_id = self.rt_by_scan_id_by_ms_level[ms_level]
        pairs = rt_by_scan_id.items()
        times = [t[1] for t in pairs]
        idx = bisect_left(times, time)
        idx_p = idx - 1
        scan_id = pairs[idx][0] if abs(times[idx] - time) < abs(times[idx_p] - time) else pairs[idx_p][0]
        return self.get_scan(scan_id)
Example #17
0
 def _sub_paths(self, path):
     """
     Generator for sub-paths of path in the index, in index order.  This
     assumes that the index key order matches python's sort order.
     """
     keys = self.index.keys()
     # bisect to find the first index with key "greater than or equal to"
     # path, then scan forward checking that sub-paths start with path
     i = bisect_left(keys, path)
     while i < len(keys):
         key = keys[i]
         if key.startswith(path):
             yield key
             i += 1
         else:
             break
Example #18
0
 def cutCellX(nodes, cutVal):
     newDict = {}
     
     for item in nodes.keys():
         newDict.setdefault(item)
         
         aArray =  nodes[item].sort(key = lambda x:x[2])
         xKey = [thing[2] for thing in aArray]
         x = bisect_left(xKey, cutVal)
         
         #xSatisList = aArray[0: cutVal]        
     
         xSatisList = aArray[x:]
         
         newDict[item] = xSatisList
 
     return newDict
Example #19
0
    def _prefiltering(self,
                      candidates,
                      spectrum,
                      AAmass=constants.AA_MASSES,
                      tol=0.2):
        """
        Filter candidates prior to validation. The best 1000 candidates
        will be returned for further validation.
        """
        if len(candidates) <= 1000:
            return candidates

        mz, n = np.sort(spectrum[:, 0]), spectrum.shape[0]
        mh, mh2o = constants.FIXED_MASSES["H"], constants.FIXED_MASSES["H2O"]
        # quick annotations simply using b and y ions.
        num_ions = []
        for candidate in candidates:
            # get residue masses
            seq_mass = np.array([AAmass[a].mono for a in candidate.seq])
            # singly charged terminal adducts in [N-terminus, C-terminus]
            term_mass = [mh, mh2o + mh]
            for mod in candidate.mods:
                if isinstance(mod.site, int):
                    seq_mass[mod.site - 1] += mod.mass
                else:
                    term_mass[0 if mod.site == "nterm" else 1] += mod.mass
            # singly charged y and b ion m/z
            ybs = np.concatenate(
                (np.cumsum(seq_mass[:-1]) + term_mass[0],
                 np.cumsum(seq_mass[::-1][:-1]) + term_mass[1]),
                axis=0)
            ybs.sort()

            # do quick annotation
            mix = [bisect_left(mz, m) for m in ybs]
            nk = sum((k > 0 and m - mz[k - 1] <= tol) or (
                k < n and mz[k] - m <= tol) for k, m in zip(mix, ybs))
            num_ions.append(nk)

        # retain candidates with 1000 highest number of y and b ions annotated.
        sorted_ix = np.argsort(num_ions)[::-1]

        return [candidates[i] for i in sorted_ix[:1000]]
Example #20
0
File: logic.py Project: borls/wac
def get_results(query):
    query = make_normal(query)
    index = defaultdict(list)

    for k, q in enumerate(query):  # находим положения слов в источнике
        try:
            w = Word.objects.get(pk=q)
            l = Lexeme.objects.get(pk=w.lexeme)

            t = list(map(int, l.index.split()))
            for i in range(0, len(t), 2):
                index[t[i]].append((t[i + 1], k))

        except ObjectDoesNotExist:
            try:
                w = Dict.objects.get(pk=q)

                l = Lexeme.objects.get(pk=w.lexeme)  # если такая лексема уже существует
                Word(word=w, lexeme=w.lexeme).save()  # добавляем слово в базу

                t = list(map(int, l.index.split()))
                for i in range(0, len(t), 2):
                    index[t[i]].append((t[i + 1], k))

            except ObjectDoesNotExist:
                pass

    results = []
    for src in index:  # для каждого источника определяем релевантность запросу и позицию
        index[src].sort()
        r, p = check(index[src], len(query))
        results.append((r, src, p))

    if results:
        results.sort()  # упорядочиваем результаты по релевантности
        results = results[results_limit:]  # нас интересуют только надежные

        t = (results[0][0] // 2,)  # порог отсечения по релевантности
        results = results[bisect_left(results, t):]  # (можно и не отсекать)

    return results[::-1]  # в порядке убывания релевантности
Example #21
0
    def removePeakFromOneCluster(self):
        idx = self.view.treeView_3.selectedIndexes()[
            0]  #supposed the selection exists
        if not idx.isValid():
            return
        model = idx.model()
        #copy the item
        itemToDelete = model.itemFromIndex(idx)
        item = QStandardItem(itemToDelete)
        #no need to select already selected, then removing
        self.view.treeView_3.removeSelected()
        #putting the copied item in view
        parentIndex = idx.parent().parent().parent()
        sample = self.currentSample[
            3]  #self.model.sample(parentIndex.data().toString(),
        #fullNameEntry=False)
        if sample is None:
            print "Unknown error"
            return

        data = map(str, item.text().split('/'))[:2]
        dataParent = map(str,
                         idx.parent().parent().data().toString().split('/'))
        sample.mappedPeaks.sample = sample
        sample.rawPeaks.sample = sample  #du to one bug don't know why
        goodPeak = sample.mappedPeaks.peakAt(*map(float, dataParent))
        fragPeak = sample.rawPeaks.peakAt(*map(float, data))
        try:
            goodPeak.fragCluster.remove(fragPeak)
            goodPeak.isoCluster.remove(fragPeak)
        except ValueError:
            pass
        #adding item to the treeView
        parentItem = model.itemFromIndex(parentIndex)
        index = bisect_left(sample.mappedPeaks.masses(),
                            float(item.text().split('/')[0]))
        #model.insertRow(index, item)
        parentItem.insertRow(index, item)
        self.view.treeView_3.update()
Example #22
0
 def __contains__(self, word): 
     return self.words[bisect.bisect_left(self.words, word)] == word
Example #23
0
import bisect
from _bisect import bisect_left, bisect_right


def GenerateFibNumbers(n):
    a, b = 1, 1
    while b < n:
        yield b
        a, b = b, a + b


FibNumbers = list(GenerateFibNumbers(10**100))

while True:
    start, end = map(int, input().split())

    if start == 0 and end == 0:  # invalid input
        break

    left = bisect_left(FibNumbers, f)  # finds start position
    right = bisect_right(FibNumbers, s)  # finds end position

    # calculate the difference to get the number of values bewtween them
    print(right - left)
Example #24
0
#https://www.hackerrank.com/challenges/playing-with-numbers
from _bisect import bisect_left

def calcSum(start, end, query):
    result = 0
    if end == start : return 0
    if start == 0 : result = abs(pre_sum[end - 1] + query * end)
    else : result = abs(pre_sum[end - 1] - pre_sum[start - 1] + query * (end - start ))
    return result

n = input()
nums = map(int, raw_input().split(' '))
nums.sort()
pos_zero = bisect_left(nums, 0, 0, n)
pre_sum = [0] * n
total = 0

for idx, el in enumerate(nums):
    total += el
    pre_sum[idx] = total

qn = input()
queries = map(int, raw_input().split(' '))
t_q = 0
for q in queries:
    t_q += q

    pos = bisect_left(nums, t_q, 0, n)
    pos_abs = bisect_left(nums, abs(t_q), 0, n)
    pos_abs_negative = bisect_left(nums, - abs(t_q), 0, n)
    
Example #25
0
def CLM_NcRead_1file(ncfile, varnames_print, keep_vars, chunk_keys, \
                     startdays, enddays, adspinup, vars_all):
    odata = {}
    odata_dims = {}
    odata_tunits = ''

    try:
        startdays = float(startdays)
    except ValueError:
        startdays = -9999

    try:
        enddays = float(enddays)
    except ValueError:
        enddays = -9999

    try:
        f = Dataset(ncfile, 'r')
        if varnames_print:
            print('FILE: ' + ncfile + ' ------- ')

    except:
        return odata, odata_dims

    # If key is on the keep list and in nc file, uniquely add to a dictionary
    for key in f.variables.keys():

        # print out all variables contained in nc files
        if varnames_print:
            print(key)

        if (key not in keep_vars) and (not vars_all):
            continue  #cycle the loop, if not required by users and NOT all_vars option
        if (key not in f.variables or f.variables[key].size <= 0):
            continue  #cycle the loop, if no data

        if (len(chunk_keys) <= 0) or (
                key not in chunk_keys
        ):  # only needs to read data once, if more than one available
            odata_dims[key] = f.variables[key].dimensions

            key_val = np.asarray(f.variables[key])
            if (hasattr(f.variables[key], '_FillValue')):
                v_missing = f.variables[key]._FillValue
                if isinstance(v_missing, (np.float, np.float16, np.float32)):
                    key_val[key_val == v_missing] = np.nan
            odata[key] = key_val

        else:
            continue

        if ('time' not in odata_dims[key]):
            continue  #cycle the loop, if not time-series dataset
        # timing option - we do checking thereafter
        # because we want to have those CONSTANTs read-out, some of which ONLY available in the first CLM nc file.
        tt = np.asarray(
            f.variables['time'])  # days since model simulation starting time
        if (odata_tunits == ''): odata_tunits = f.variables['time'].units
        tdays1 = min(tt)
        tdays2 = max(tt)
        if (startdays >= 0):
            if (startdays > tdays2):
                odata = {}
                odata_dims = {}
                return odata, odata_dims, odata_tunits
            else:
                s_index = int(bisect_left(tt, startdays))
                odata[key] = odata[key][s_index:, ]

        if (enddays > 0):
            if (enddays < tdays1):
                odata = {}
                odata_dims = {}
                return odata, odata_dims, odata_tunits
            else:
                e_index = int(bisect_right(tt, enddays))
                odata[key] = odata[key][:e_index, ]

        #ad-spinup run, recal. each component of totsomc_vr, if needed
        if adspinup:
            if key in totsomc_vr:
                sub_indx = totsomc_vr.index(key)
                odata[key] = odata[key] * ad_factor[sub_indx]

            if key in totsomn_vr:
                sub_indx = totsomn_vr.index(key)
                odata[key] = odata[key] * ad_factor[sub_indx]

    # summing up of total liter/som C/N, if needed and available
    if 'TOTLITC_vr' in keep_vars:
        indx = keep_vars.index('TOTLITC_vr')
        subs = totlitc_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    if 'TOTLITN_vr' in keep_vars:
        indx = keep_vars.index('TOTLITN_vr')
        subs = totlitn_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    if 'TOTSOMC_vr' in keep_vars:
        indx = keep_vars.index('TOTSOMC_vr')
        subs = totsomc_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    if 'TOTSOMN_vr' in keep_vars:
        indx = keep_vars.index('TOTSOMN_vr')
        subs = totsomn_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    # convert 'soilliq' to 'saturation_lia', if available
    # sat = h2osoi_liq(c,j) / (watsat(c,j)*dz(c,j)*denh2o)
    denh2o = 1000.0  #kg/m3
    if 'SOILSAT_LIQ' in keep_vars:
        if 'SOILLIQ' in odata.keys():
            dary = np.array(odata['SOILLIQ'])
            dvec = np.array(porosity * dz * denh2o)
            if (len(dvec.shape) == 2):
                odata['SOILSAT_LIQ'] = dary / dvec[None, :, :]
            elif (len(dvec.shape) == 3):
                odata['SOILSAT_LIQ'] = dary / dvec[None, :, :, :]
            else:
                odata['SOILSAT_LIQ'] = dary / dvec

            odata_dims['SOILSAT_LIQ'] = odata_dims['SOILLIQ']

    denice = 917.0  #kg/m3
    if 'SOILSAT_ICE' in keep_vars:
        if 'SOILICE' in odata.keys():
            dary = np.array(odata['SOILICE'])
            dvec = np.array(porosity * dz * denice)
            if (len(dvec.shape) == 2):
                odata['SOILSAT_ICE'] = dary / dvec[None, :, :]
            elif (len(dvec.shape) == 3):
                odata['SOILSAT_ICE'] = dary / dvec[None, :, :, :]
            else:
                odata['SOILSAT_ICE'] = dary / dvec

            odata_dims['SOILSAT_ICE'] = odata_dims['SOILICE']

    # out datasets
    return odata, odata_dims, odata_tunits
def preprocess(path, electrode_list, frequency_component):
    """
    1. reads the mat files
    2. extract the coherence spectra and comprehension and frequencies scores
    3. restricts the coherence spectra to only the specified electrodes and frequencies and to be z-scored
    Args:
        path: location of the .mat files
        electrode_list: list of electrodes to be used for classification
        frequency_component: the frequency component to be used (+-1)

    Returns:
        standardized and filtered coherence spectrum, comprehension scores
    """
    coherence_spectra = []
    comprehension_scores = []
    frequencies = []

    # extract the data set from .mat files
    for mat_file in sorted(os.listdir(path)):
        mat_contents = sio.loadmat(os.sep.join([path, mat_file]))
        for i in range(0, 2):
            electrodes = []
            cluster_indexes = []
            session_i = mat_contents['conn'][0, i][0]

            # get the electrode names
            label_struct = session_i['labelcmb'][0]
            for label in label_struct:
                electrodes = np.append(electrodes, label[0])

            # filter for the electrodes to be used
            for el in electrode_list:
                cluster_indexes.append(np.where(electrodes == el)[0][0])

            # get the coherence spectra and slice to use the specified electrodes only
            coherence_spectra.append(
                session_i['cohspctrm'][0][:, cluster_indexes, :])

            # get the comprehension scores
            for j in range(0, len(session_i['trialinfo'][0])):
                comprehension_scores.append(session_i['trialinfo'][0][j][2])

            # get the available frequencies
            if len(frequencies) == 0:
                frequencies = session_i['freq']
                frequencies = np.concatenate([x
                                              for x in frequencies]).flatten()

    # adjust the shape to be (no_trials, no_electrodes, no_frequencies)
    coherence_spectra = np.concatenate([x for x in coherence_spectra])
    comprehension_scores = np.array(comprehension_scores)

    # filter for the desired frequencies using binary search
    if frequency_component is not None:
        if frequency_component < 1 or frequency_component > 50:
            raise Exception("Select a frequency between 1 and 50 Hz!")
        else:
            lower = frequency_component - 1
            upper = frequency_component + 1

            idx_lower = bisect_right(frequencies, lower)
            idx_upper = bisect_left(frequencies, upper)

            # slice the coherence spectra according to the frequencies to be used
            coherence_spectra = coherence_spectra[:, :, [
                x for x in range(idx_lower, idx_upper + 1)
            ]]

        # standardize the coherence spectra
        coherence_spectra = (coherence_spectra - np.mean(coherence_spectra)
                             ) / np.std(coherence_spectra)
    return coherence_spectra, comprehension_scores
Example #27
0
# 判断元素是否是列表中
from _bisect import bisect_left

lst = [1, 3, 4, 5, 7, 8]
for i in lst:
    if i == 8:
        print('存在')

if 4 in lst:
    print('存在')

# set / bisect ==============================
list_set = [1, 6, 3, 5, 3, 4]
list_bisect = [1, 6, 3, 5, 3, 4]

lst = set(list_set)
print(lst)
for i in lst:
    if i == 6:
        print('存在')

if 4 in lst:
    print('存在')

list_bisect.sort()
if bisect_left(list_bisect, 6):
    print('存在')
Example #28
0
    def _extract(self):
        run_slice_it = RunSliceIterator(self.reader, ms_level=1)

        # init some variables
        all_peaks = []

        peaks_idx_by_scan_id = {scan_id: PeakIndex() for scan_id in self.scan_ids}

        curr_rsh, curr_ss = run_slice_it.next()
        prev_rsh, prev_ss = None, None
        # next_rsh, next_ss = None, None

        # init with first rs peaks
        for curr_s in curr_ss:
            curr_peaks = curr_s.to_peaks(self.rt_by_scan_id)
            peaks_idx_by_scan_id[curr_s.scan_id].update(curr_peaks, update_min=True)
            map(all_peaks.append, curr_peaks)

        mass_traces = []
        already_used_peaks = set()

        # i = 0
        while run_slice_it.has_next():

            logging.info("Processing runslice id #{}".format(curr_rsh.id))

            next_rsh, next_ss = run_slice_it.next()

            for next_s in next_ss:
                next_peaks = next_s.to_peaks(self.rt_by_scan_id)
                peaks_idx_by_scan_id[next_s.scan_id].update(next_peaks, update_max=True)
                map(all_peaks.append, next_peaks)

            # slightly more efficient to iterate over this variable than allpeaks
            # since it contains less data points
            not_seen_yet = list(set(all_peaks) - already_used_peaks)
            # heapq.heapify(not_seen_yet)
            # logging.info("Sort #{} peaks by intensity".format(len(not_seen_yet)))
            not_seen_yet.sort(key=lambda p: p.intensity, reverse=True)
            # all_peaks.sort(key=lambda p: p.intensity, reverse=True) #attrgetter('intensity'), reverse=True)

            for peak in not_seen_yet:  # heapq.nlargest(len(not_seen_yet), not_seen_yet):

                if peak in already_used_peaks:
                    continue

                mass_trace = MassTrace()
                mass_trace.apex_peak = peak
                already_used_peaks.add(peak)
                scan_id = peak.scan_id

                curr_mz = mass_trace.mz_centroid  # peak.mz
                tol = curr_mz * self.mz_tol_ppm * self.PPM

                # note: surprisingly the dict approach is slower than working directly on the index
                # with bisect:
                # next_scan_id, prev_scan_id = self.prev_next_scan_ids_by_scan_id[scan_id]
                scan_id_idx = bisect_left(self.scan_ids, scan_id)

                # previous index
                prev_scan_id_idx = scan_id_idx - 1
                prev_scan_id = self.scan_ids[prev_scan_id_idx] if prev_scan_id_idx >= 0 else None

                # next index
                next_scan_id_idx = scan_id_idx + 1
                next_scan_id = self.scan_ids[next_scan_id_idx] if next_scan_id_idx < self.n else None

                # to the right

                right_gap = 0
                while next_scan_id is not None:
                    peaks_idx = peaks_idx_by_scan_id[next_scan_id]
                    p = peaks_idx.get_nearest_entity(curr_mz, tol, already_used_peaks)

                    if p is None:  # or p in already_used_peaks:
                        right_gap += 1
                        if right_gap > self.gap_allowed:
                            break
                    else:
                        mass_trace.append(p)
                        already_used_peaks.add(p)
                    next_scan_id_idx -= 1
                    next_scan_id = self.scan_ids[next_scan_id_idx] if next_scan_id_idx < self.n else None

                # to the left
                left_gap = 0
                while prev_scan_id is not None:
                    peaks_idx = peaks_idx_by_scan_id[prev_scan_id]
                    p = peaks_idx.get_nearest_entity(curr_mz, tol, already_used_peaks)

                    if p is None:  # or p in already_used_peaks:
                        left_gap += 1
                        if left_gap > self.gap_allowed:
                            break
                    else:
                        mass_trace.append_left(p)
                        already_used_peaks.add(p)

                    prev_scan_id_idx -= 1
                    prev_scan_id = self.scan_ids[prev_scan_id_idx] if prev_scan_id_idx >= 0 else None

                if mass_trace.length > 4: # and mass_trace.snr > self.min_snr:
                    mass_traces.append(mass_trace)

            # end for
            logging.info("#{} detected mass traces".format(len(mass_traces)))

            # clear some data points in the set buffer
            peaks_to_remove = {p for p in all_peaks if prev_rsh is not None and p.rs_id == prev_rsh.id}

            # clean already used peaks
            already_used_peaks = already_used_peaks - peaks_to_remove

            # remove prev rs peaks from all peaks
            all_peaks = list(set(all_peaks) - peaks_to_remove)

            # remove peak from the index
            if prev_rsh is not None:
                min_mz, max_mz = self.rs_mzs_by_rs_id[prev_rsh.id]
                for idx in peaks_idx_by_scan_id.viewvalues():
                    idx.remove_by_mass(min_mz, max_mz)

            # reassignment to good variables preparing for the next turn of the loop
            prev_rsh, prev_ss = curr_rsh, curr_ss
            curr_rsh, curr_ss = next_rsh, next_ss
            # i += 1
        # end while

        return mass_traces
    return summations


def find_contiguous_set(keys: list, our_sum: int) -> int:
    for number in itertools.count(2):
        for count in range(len(keys) - 1):
            group = keys[count:count + number]

            if sum(group) == our_sum:
                return min(group) + max(group)


if __name__ == '__main__':
    puzzle = open_puzzle("09-encoding-error.txt")
    puzzle = [int(line.strip()) for line in puzzle]

    window = 25
    current_decryption_window = sorted(puzzle[:window])
    part_one_answer = 0

    for count in range(window, len(puzzle)):
        current_summation = calc_sum(current_decryption_window)
        del current_decryption_window[bisect_left(current_decryption_window,
                                                  puzzle[count - window])]
        if puzzle[count] not in current_summation:
            part_one_answer = puzzle[count]
        insort_left(current_decryption_window, puzzle[count])

    print(part_one_answer)
    print(find_contiguous_set(puzzle, part_one_answer))
'''
Created on Aug 1, 2016

@author: Md. Rezwanul Haque
'''

import bisect
from _bisect import bisect_left, bisect_right
def Fib(n):
    a,b = 1,1
    while b<n:
        yield b 
        a,b = b, a+b 
FibA = list(Fib(10**100))
while True:
    f,s = map(int, input().split())
    if f == 0 and s == 0:
        break
    left = bisect_left(FibA,f)
    right = bisect_right(FibA,s)
    print(right - left) 
    
Example #31
0
# list 에서 아이템을 검색하는 작업은 index 메서드를 호출할 때 리스트의 길이에 비례한 선형적 시간이 걸린다.
from _bisect import bisect_left

x = list(range(10**6))
i = x.index(991234)

# bisect_left 같은 bisect 모듈의 함수는 정렬된 아이템 시퀀스를 대상으로 한 효율적인 바이너리 검색을 제공한다.
# bisect_left 가 반환한 인덱스는 시퀀스에 들어간 값의 삽입 지점이다.

i = bisect_left(x, 991234)
print(i)

# 바이너리 검색의 복잡도는 로그 형태로 증가한다. 다시 말해 아이템 백만 개를 담은 리스트를 bisect 로 검색할 때
# 걸리는 시간은 아이템 14개를 담은 리스트를 index 로 순차 검색할 떄 걸리는 시간과 거의 같다.
Example #32
0
    def test_bisect_left(self):
        from _bisect import bisect_left
        a = [0, 5, 6, 6, 6, 7]
        assert bisect_left(a, None) == 0
        assert bisect_left(a, -3) == 0
        assert bisect_left(a, 0) == 0
        assert bisect_left(a, 3) == 1
        assert bisect_left(a, 5) == 1
        assert bisect_left(a, 5.5) == 2
        assert bisect_left(a, 6) == 2
        assert bisect_left(a, 6.0) == 2
        assert bisect_left(a, 6.1) == 5
        assert bisect_left(a, 7) == 5
        assert bisect_left(a, 8) == 6
        a = []
        assert bisect_left(a, 123) == 0
        a = [9]
        assert bisect_left(a, -123) == 0
        assert bisect_left(a, 9) == 0
        assert bisect_left(a, 123) == 1
        a = [9, 9]
        assert bisect_left(a, -123) == 0
        assert bisect_left(a, 9) == 0
        assert bisect_left(a, 123) == 2
        a = [4, 6, 6, 9]
        assert bisect_left(a, 6, 0) == 1
        assert bisect_left(a, 6, 1) == 1
        assert bisect_left(a, 6, 2) == 2
        assert bisect_left(a, 6, 3) == 3
        assert bisect_left(a, 6, 4) == 4
        assert bisect_left(a, 6, 0, 0) == 0
        assert bisect_left(a, 6, 0, 1) == 1
        assert bisect_left(a, 6, 0, 2) == 1
        assert bisect_left(a, 6, 0, 3) == 1
        assert bisect_left(a, 6, 0, 4) == 1

        raises(ValueError, bisect_left, [1, 2, 3], 5, -1, 3)
Example #33
0
    def test_bisect_left(self):
        from _bisect import bisect_left
        a = [0, 5, 6, 6, 6, 7]
        assert bisect_left(a, None) == 0
        assert bisect_left(a, -3) == 0
        assert bisect_left(a, 0) == 0
        assert bisect_left(a, 3) == 1
        assert bisect_left(a, 5) == 1
        assert bisect_left(a, 5.5) == 2
        assert bisect_left(a, 6) == 2
        assert bisect_left(a, 6.0) == 2
        assert bisect_left(a, 6.1) == 5
        assert bisect_left(a, 7) == 5
        assert bisect_left(a, 8) == 6
        a = []
        assert bisect_left(a, 123) == 0
        a = [9]
        assert bisect_left(a, -123) == 0
        assert bisect_left(a, 9) == 0
        assert bisect_left(a, 123) == 1
        a = [9, 9]
        assert bisect_left(a, -123) == 0
        assert bisect_left(a, 9) == 0
        assert bisect_left(a, 123) == 2
        a = [4, 6, 6, 9]
        assert bisect_left(a, 6, 0) == 1
        assert bisect_left(a, 6, 1) == 1
        assert bisect_left(a, 6, 2) == 2
        assert bisect_left(a, 6, 3) == 3
        assert bisect_left(a, 6, 4) == 4
        assert bisect_left(a, 6, 0, 0) == 0
        assert bisect_left(a, 6, 0, 1) == 1
        assert bisect_left(a, 6, 0, 2) == 1
        assert bisect_left(a, 6, 0, 3) == 1
        assert bisect_left(a, 6, 0, 4) == 1

        raises(ValueError, bisect_left, [1, 2, 3], 5, -1, 3)
Example #34
0
 def delete_from_leaf(self, key):
     i = bisect_left(self.keys, key)
     val = self.values[i]
     del self.keys[i]
     del self.values[i]
     return val