Ejemplo n.º 1
0
def doIt(infile):
    f = open(infile)
    n = int(f.readline())
    for i in range(1,n+1):
        n, k = f.readline().split(' ')
        n, k = int(n), int(k)
        a, b, c, r = f.readline().split(' ')
        a, b, c, r = int(a), int(b), int(c), int(r)

        m = [a]
        prev = a
        for j in range(1, k):
            e = (b*prev+c)%r
            m.append(e)
            prev = e

        msorted = m[:]
        msorted.sort()
        for j in range(k, n):
            for l in range(n):
                if contains(msorted, l) == False:
                    break
            m.append(l)
            x = m.pop(0)
            del msorted[index(msorted, x)]
            bisect.insort_left(msorted, l)
        
        ans = m[-1]
        print("Case #{0}: {1}".format(i, ans))
Ejemplo n.º 2
0
 def add(self, individual: Individual):
     """Add an Individaul to the population."""
     if individual.total_error is None:
         self.unevaluated.append(individual)
     else:
         insort_left(self.evaluated, individual)
     return self
Ejemplo n.º 3
0
    def __add(self, member, score):
        """ Private method to add the member and score to the sorted set.
        This trusts that the member does not already exist in the sorted set.

        """
        bisect.insort_left(self._members, (member, score))
        bisect.insort_left(self._scores, (score, member))
Ejemplo n.º 4
0
    def connect(self, func, filter=None, priority=0, count=0):
        """ Add a listener for the event.


        Parameters
        ----------
        cls : class
            The class of events for which the listener is registered.

        func : callable
            A callable to be called when the event is emitted.  The function
            should expect one argument which is the event instance which was
            emitted.

        filter : dict
            Filters to match for before calling the listener. The listener is
            called only when the event matches all of the filter .
            
            Filter specification:
                - key: string which is extended name of an attribute of the
                    event instance. For example string 'source.name' will be
                    the attribute `event.source.name`
                - value: the value of the specified attribute.

        priority : int
            The priority of the listener. Higher priority listeners are called
            before lower priority listeners.

        count : int
            A unique integer to break a tie in priority. This is generally
            an incremental number assigned by EventManager in order of
            registration.

        Note
        ----
       
        Reconnecting an already connected listener will disconnect the
        old listener. This may have rammifications in changing the filters
        and the priority.

        The filtering is added so that future optimizations can be done
        on specific events with large number of handlers. For example there
        should be a fast way to filter key events to specific listeners rather
        than iterating through all listeners.

        """
        id = self.get_id(func)
        if id in self._priority_info:
            # Ensure a function is connected only once.
            # Reconnecting will update its sequence and filters.
            self._disconnect(id)
        with self._priority_list_lock:
            sub = self._get_notifier(func, self._listener_deleted)
            if filter:
                self._listener_filters[id] = filter
                for key in filter:
                    self._filter_keys.add(key)
            key = (-priority, count, sub)
            bisect.insort_left(self._priority_list, key)
            self._priority_info[id] = key
Ejemplo n.º 5
0
def get_tree_freq_from(filename, chunk_size=1):
    if chunk_size == 1:
        f = open(filename, 'rb') # input file
        a = bytearray(f.read()) # creates an array of bytes with contents of 'filename'
        c = Counter(a) # counts the occurrences of each byte in the given file
        c_ord = sorted(c.iteritems(), key=operator.itemgetter(1))
        L = get_list_from_dict(c_ord) # list of node objects that will remain sorted during the algorithm

        for e in L:
            print e,
        print

        n = len(L)
        for i in xrange(n-1):
            new_node = HuffmanNode('*') # non-terminal node.
            new_node.left = L.pop(0) # take the the two with the lowest frequency
            print 'poping(L):', new_node.left
            new_node.right = L.pop(0)
            print 'poping(R):', new_node.right
            new_node.freq = new_node.left.freq + new_node.right.freq
            bisect.insort_left(L, new_node) # append the new node in the right place so the list will remain sorted.
            print '->Insering:', new_node
            print 'New list:'
            for e in L:
                print e,
            print
        return L.pop(0) # returns the root of the huffman tree
        
        #print 'Most freq.:', c_ord[len(c_ord)-1] # shows the most frequent byte
        #print 'Least freq.:', c_ord[0] # shows the least frequent byte
    else: # @TODO: compression using blocks of bytes as words for higer compression ratio com bigger files.
        pass
Ejemplo n.º 6
0
 def add(self, playerStat):
     if id(playerStat) in self.__playerId :
         return False
     key = self.key(playerStat.playerId)
     bisect.insort_left(self.__playerStats, [key, playerStat])
     self.__playerId[id(playerStat)] = playerStat
     return True
Ejemplo n.º 7
0
    def redo(self):
        part = self.part
        id_num = self.id_num
        origin_pt = self.origin_pt
        self.old_limits = part.getVirtualHelixOriginLimits()
        # Set direction to (0, 0, 1) for now
        vh = part._createHelix(id_num, origin_pt, self.direction, self.length, self.color)

        if self.safe:   # update all neighbors
            if not self.neighbors:
                self.neighbors = list(
                    part._getVirtualHelixOriginNeighbors(id_num, self.threshold))

            neighbors = self.neighbors
            part.vh_properties.loc[id_num, 'neighbors'] = str(list(neighbors))
            for neighbor_id in neighbors:
                nneighbors = literal_eval(
                    part.getVirtualHelixProperties(neighbor_id, 'neighbors')
                )
                bisect.insort_left(nneighbors, id_num)
                part.vh_properties.loc[neighbor_id, 'neighbors'] = str(list(nneighbors))
        else:
            neighbors = self.neighbors
        if self.keys is not None:
            '''NOTE: DON'T bother UndoCommand-ing this because the helix is
            deleted on the undo.  Causes a segfault on MacOS if you do
            '''
            part._setVirtualHelixProperties(id_num,
                                           self.keys, self.values,
                                           emit_signals=False)
            part.resetCoordinates(id_num)
        part.partVirtualHelixAddedSignal.emit(part, id_num, vh, neighbors)
Ejemplo n.º 8
0
   def getDirEntries(self):
      """ returns dictionary of dir entries, keyed by dir name """
      entriesDict = {}

      # First, we grab a dir listing of the target, setting entry attributes
      for entryName in self.entries:
         if entryName == rdiffDataDirName: continue
         entryPath = joinPaths(self.repo, self.dirPath, entryName)
         newEntry = dirEntry(entryName, os.path.isdir(entryPath), os.lstat(entryPath)[6], True,
                             [self._getLastChangedBackupTime(entryName)])
         entriesDict[entryName] = newEntry

      # Go through the increments dir.  If we find any files that didn't exist in dirPath (i.e. have been deleted), add them
      for entryFile in self.incrementEntries:
         entry = incrementEntry(entryFile)
         entryName = entry.getFilename()
         if entry.shouldShowIncrement() or entry.isMissingIncrement():
            entryDate = entry.getDate()
            if not entry.isSnapshotIncrement():
               if entry.isMissingIncrement():
                  entryDate = self._getFirstBackupAfterDate(entry.getDate())
               else:
                  entryDate = entry.getDate()
            if not entryName in entriesDict.keys():
               entryPath = joinPaths(self.repo, rdiffIncrementsDirName, self.dirPath, entryName)
               newEntry = dirEntry(entryName, os.path.isdir(entryPath), 0, False, [entryDate])
               entriesDict[entryName] = newEntry
            else:
               if not entryDate in entriesDict[entryName].changeDates:
                  bisect.insort_left(entriesDict[entryName].changeDates, entryDate)

      return entriesDict
Ejemplo n.º 9
0
    def dump_subset(self,filename,append=False,verbose=False):
        """
        dump a textual representation of the decoded
        data in the currently loaded subset to a file.

        If `append=True`, append to an existing file
        (otherwise over-write file).

        If `verbose=True`, more complete but harder to read info is written.

        `ncepbufr.open.load_subset` must be called before
        trying to print the decoded subset using `ncepbufr.open.dump_subset`.
        """
        lunout = random.choice(_funits)
        if not append:
            iret = _bufrlib.fortran_open(filename,lunout,'formatted','rewind')
        else:
            iret = _bufrlib.fortran_open(filename,lunout,'formatted','append')
        if iret != 0:
            msg='error opening %s' % filename
        if not verbose:
            _bufrlib.ufdump(self.lunit,lunout)
        else:
            _bufrlib.ufbdmp(self.lunit,lunout)
        iret = _bufrlib.fortran_close(lunout)
        if iret == 0:
            bisect.insort_left(_funits,lunout)
        else:
            raise IOError('error closing %s' % filename)
Ejemplo n.º 10
0
    def get_index(self, requestContext):
        matches = []

        for root, _, files in walk(settings.WHISPER_DIR):
          root = root.replace(settings.WHISPER_DIR, '')
          for base_name in files:
            if fnmatch.fnmatch(base_name, '*.wsp'):
              match = join(root, base_name).replace('.wsp', '').replace('/', '.').lstrip('.')
              bisect.insort_left(matches, match)

        # unlike 0.9.x, we're going to use os.walk with followlinks
        # since we require Python 2.7 and newer that supports it
        if RRDReader.supported:
          for root, _, files in walk(settings.RRD_DIR, followlinks=True):
            root = root.replace(settings.RRD_DIR, '')
            for base_name in files:
              if fnmatch.fnmatch(base_name, '*.rrd'):
                absolute_path = join(settings.RRD_DIR, root, base_name)
                base_name = splitext(base_name)[0]
                metric_path = join(root, base_name)
                rrd = RRDReader(absolute_path, metric_path)
                for datasource_name in rrd.get_datasources(absolute_path):
                  match = join(metric_path, datasource_name).replace('.rrd', '').replace('/', '.').lstrip('.')
                  if match not in matches:
                    bisect.insort_left(matches, match)

        return matches
Ejemplo n.º 11
0
    def inval(self, oid, tid):
        if tid == z64:
            # This is part of startup cache verification:  forget everything
            # about this oid.
            self._remove_noncurrent_revisions(oid)

        if oid in self.evicted:
            del self.evicted[oid]

        cur_tid = self.current.get(oid)
        if cur_tid is None:
            # We don't have current data, so nothing more to do.
            return

        # We had current data for oid, but no longer.
        self.invals += 1
        self.total_invals += 1
        del self.current[oid]
        if tid == z64:
            # Startup cache verification:  forget this oid entirely.
            self._remove(oid, cur_tid)
            return

        # Our current data becomes non-current data.
        # Add the validity range to the list of non-current data for oid.
        assert cur_tid < tid
        L = self.noncurrent.setdefault(oid, [])
        bisect.insort_left(L, (cur_tid, tid))
        # Update the end of oid's validity range in its CircularCacheEntry.
        e = self.key2entry[oid, cur_tid]
        assert e.end_tid == z64
        e.end_tid = tid
Ejemplo n.º 12
0
 def write(self, oid, size, start_tid, end_tid, evhit=0):
     if end_tid == z64:
         # Storing current revision.
         if oid in self.current:  # we already have it in cache
             if evhit:
                 import pdb; pdb.set_trace()
                 raise ValueError('WTF')
             return
         self.current[oid] = start_tid
         self.writes += 1
         self.total_writes += 1
         self.add(oid, size, start_tid)
         return
     if evhit:
         import pdb; pdb.set_trace()
         raise ValueError('WTF')
     # Storing non-current revision.
     L = self.noncurrent.setdefault(oid, [])
     p = start_tid, end_tid
     if p in L:
         return  # we already have it in cache
     bisect.insort_left(L, p)
     self.writes += 1
     self.total_writes += 1
     self.add(oid, size, start_tid, end_tid)
Ejemplo n.º 13
0
def main():
    n = int(input())
    a = list(map(int, input().split()))

    d = Counter(a)
    min_value = min(a)

    h = math.log(n, 2)
    if len(set(a)) != n or d[min_value] != h + 1:
        print("NO1")
        return 0

    num_key = defaultdict(list)
    for key, num in d.items():
        if num > h + 1:
            print("NO2")
            return 0
        bisect.insort_left(num_key[num], key)

    ans = [0] * len(a)
    if not dfs(0, min_value, h + 1, num_key, ans):
        print("NO3")
        return

    print("YES")
    print(*ans)

    return 0
Ejemplo n.º 14
0
def weights_matrix(x_query, X, alpha):
  if isinstance(x_query, np.matrix):
    x_query = x_query.A

  m = len(X)                # number of data points
  r = int(round(alpha * m)) # size of local region
  W = np.identity(m)        # skeleton for weights matrix

  sorted_list = []
  for i,row in enumerate(X):
    delta_norm = vector_norm(row - x_query)
    insort_left(sorted_list, delta_norm)
    W[i][i] = delta_norm

  # computing normalization constant based on alpha
  h_i = 1 / sorted_list[r - 1]

  # normalizing weights matrix
  W = W * h_i

  # applying tricube weight function to weights matrix
  for i in range(0, len(W)):
    W[i][i] = (1 - (W[i][i] ** 3)) ** 3 if W[i][i] < 1 else 0

  return np.mat(W)
Ejemplo n.º 15
0
    def setdefault(self, key, value):
        """If key is in the dictionary, returns its value;
        otherwise adds the key with the given value which is also
        returned

        >>> d = SortedDict(dict(s=1, a=2, n=3, i=4, t=5, y=6))
        >>> d.setdefault("n", 99)
        3
        >>> d.values()
        [2, 4, 3, 1, 5, 6]
        >>> d.setdefault("r", -20)
        -20
        >>> d.items()[2:]
        [('n', 3), ('r', -20), ('s', 1), ('t', 5), ('y', 6)]
        >>> d.setdefault("@", -11)
        -11
        >>> d.setdefault("z", 99)
        99
        >>> d.setdefault("m", 50)
        50
        >>> d.keys()
        ['@', 'a', 'i', 'm', 'n', 'r', 's', 't', 'y', 'z']
        """
        if key not in self.__dict:
            bisect.insort_left(self.__keys, key)
        return self.__dict.setdefault(key, value)
Ejemplo n.º 16
0
    def __sort_registry(self, svc_ref):
        """
        Sorts the registry, after the update of the sort key of given service
        reference

        :param svc_ref: A service reference with a modified sort key
        """
        with self.__svc_lock:
            if svc_ref not in self.__svc_registry:
                raise BundleException("Unknown service: {0}".format(svc_ref))

            # Remove current references
            bundle_services = self.__bundle_svc[svc_ref.get_bundle()]
            idx = bisect.bisect_left(bundle_services, svc_ref)
            del bundle_services[idx]
            for spec in svc_ref.get_property(OBJECTCLASS):
                # Use bisect to remove the reference (faster)
                spec_refs = self.__svc_specs[spec]
                idx = bisect.bisect_left(spec_refs, svc_ref)
                del spec_refs[idx]

            # ... use the new sort key
            svc_ref.update_sort_key()

            bisect.insort_left(bundle_services, svc_ref)
            for spec in svc_ref.get_property(OBJECTCLASS):
                # ... and insert it again
                spec_refs = self.__svc_specs[spec]
                bisect.insort_left(spec_refs, svc_ref)
Ejemplo n.º 17
0
def construct_db(assets_dir):
  ## Creating a database of text labels from game assets dir given
  ## the database has a following structure:
  ## {"section": { "label" :
  ##   { "files were it used" : [list of fields were it used in file] } } }
  print("Scanning assets at " + assets_dir)
  db = dict()
  db[""] = dict()
  foi = list()
  endings = tuple(files_of_interest.keys())
  for subdir, dirs, files in walk(assets_dir):
    for thefile in files:
      if thefile.endswith(endings):
        foi.append(normpath(join(subdir, thefile)))
  with Pool() as p:
    r = p.imap_unordered(parseFile, foi)
    for chunk in r:
      for sec, val, fname, path in chunk:
        if sec not in db:
          db[sec] = dict()
        if val not in db[sec]:
          db[sec][val] = dict()
        filename = normpath(relpath(abspath(fname), abspath(assets_dir)))
        if filename not in db[sec][val]:
          db[sec][val][filename] = list()
        if path not in db[sec][val][filename]:
          insort_left(db[sec][val][filename], path)
  return db
Ejemplo n.º 18
0
 def insert(self, srtkey_hndl, allkeyonly=False):
     """
     Insert a node. Given is a tuple (sortkey, handle), and this is added
     in the correct place, while the hndl2index map is updated.
     Returns the path of the inserted row
     
     :param srtkey_hndl: the (sortkey, handle) tuple that must be inserted
     :type srtkey_hndl: sortkey key already transformed by self.sort_func, object handle
     
     :Returns: path of the row inserted in the treeview
     :Returns type: Gtk.TreePath or None
     """
     if srtkey_hndl[1] in self._hndl2index:
         print(('WARNING: Attempt to add row twice to the model (%s)' %
                 srtkey_hndl[1]))
         return
     if not self._identical:
         bisect.insort_left(self._fullhndl, srtkey_hndl)
         if allkeyonly:
             #key is not part of the view
             return None
     insert_pos = bisect.bisect_left(self._index2hndl, srtkey_hndl)
     self._index2hndl.insert(insert_pos, srtkey_hndl)
     #make sure the index map is updated
     for srt_key,hndl in self._index2hndl[insert_pos+1:]:
         self._hndl2index[hndl] += 1
     self._hndl2index[srtkey_hndl[1]] = insert_pos
     #update self.__corr so it remains correct
     if self._reverse:
         self.__corr = (len(self._index2hndl) - 1, -1)
     return Gtk.TreePath((self.real_path(insert_pos),))
Ejemplo n.º 19
0
 def translocate_brownian(self):
     # if self.on_promoter:
     #     if random.uniform(0.0, 1.0) <= self.Pforward:
     #         newdraw = random.uniform(0.0,1.0) 
     #         moves = self.forward_moves
     #         bisect.insort_left(moves, [newdraw, newdraw])
     #         self.position += moves[zip(*moves)[1].index(newdraw) + 1][1]
     #     return 
     # else:
     draw = random.uniform(0.0, 1.0)
     if draw <= self.Pforward:
         if self.frontblocked:
             return
         if len(self.forward_moves) > 1:
             newdraw = random.uniform(0.0,1.0) 
             moves = self.forward_moves
             bisect.insort_left(moves, [newdraw, newdraw])
             self.position += moves[zip(*moves)[1].index(newdraw) + 1][1]
         return
     elif self.Pforward < draw <= self.Pforward + self.Pbackward:
         if self.backblocked:
             return
         if len(self.backward_moves) > 1:
             newdraw = random.uniform(0.0,1.0) 
             moves = self.backward_moves
             bisect.insort_left(moves, [newdraw, newdraw])
             self.position -= moves[zip(*moves)[1].index(newdraw) + 1][1]
         return 
     else:
         return 
Ejemplo n.º 20
0
Archivo: tests.py Proyecto: trtikm/E2
    def on_time_step(self, t, dt):
        if self._next_spike_time is None:
            self._next_spike_time = t
            if self._pivot.get_next_spike_time() is None:
                self._pivot.on_time_step(t, dt)
        elif self._next_spike_time > t + 1.5 * dt:
            return False
        else:
            self._spikes_history.append(t + dt)
        while len(self._event_buffer) < self._ideal_buffer_size:
            while self._last_pivot_index >= len(self._pivot.get_spikes_history()):
                t0 = t
                while t0 + dt < self._pivot.get_next_spike_time():
                    t0 += dt
                old_len = len(self._pivot.get_spikes_history())
                self._pivot.on_time_step(t0, dt)
                assert old_len + 1 == len(self._pivot.get_spikes_history())
            prev = self._pivot.get_spikes_history()[self._last_pivot_index - 1] if self._last_pivot_index > 0 else 0.0
            bisect.insort_left(self._event_buffer, self._pivot.get_spikes_history()[self._last_pivot_index] - prev)
            self._last_pivot_index += 1
        pivot_next_time = self._pivot.get_spikes_history()[
            bisect.bisect_left(self._pivot.get_spikes_history(), self._next_spike_time + dt)
            ]
        to_pivot_time = pivot_next_time - self._next_spike_time
        assert to_pivot_time > 0
        correlation_coefficient = self._deviation.next_event()
        idx = self.choose_event_index(self._event_buffer, to_pivot_time, correlation_coefficient)
        assert idx >= 0 and idx < len(self._event_buffer)
        # assert abs(self._next_spike_time + self._event_buffer[idx] - pivot_next_time) < 0.0001
        self._next_spike_time += self._event_buffer[idx]

        self._event_buffer.pop(idx)
        assert self._next_spike_time > t + dt
        return True
Ejemplo n.º 21
0
 def setMenu(self, component, menu, label, help, index=999999):
     """Set pulldown menu data."""
     if menu not in self.menuNames:
         self.menuNames.append(menu)
     if menu not in self.menus:
         self.menus[menu] = []
     bisect.insort_left(self.menus[menu], (index, component, label, help))
Ejemplo n.º 22
0
    def add(self, fn, rootdir):
        pf = _PicFile(fn,rootdir)
        samefile = self.findsamefile(pf)
        #根据MD5去找
        if samefile:
            pf.exif=samefile.exif
            #更新其日期而已这样后面才能找到

        if len(self.groups)==0:
            self.groups.append(_PicGroup.newone(self.path,pf))
            return
            
        for g in self.groups:
            if pf<=g:
            #如果pf比第一个组还要小,那么就加入到第一个组了
            #如果pf在两个组中间,则添加到后一个组里面
            #如果pf比任何一个组都要大,则添加到最后一个组
                break
        
        re, refpf = g.add(pf)
        if re=="Old":
            gprint(DIM+"O")
            self.old.append( (pf,refpf) )
        elif re=="Overwrite":
            gprint(YELLOW+"W")
            self.overwrite.append( (pf,refpf) )
        else: #'New'
            self.md5dict[pf.size].append( pf )
            gprint(GREEN+"N")
            if(g.count()>=self.max):
                ng=g.split()
                if ng is not None:
                    bisect.insort_left(self.groups,ng)
Ejemplo n.º 23
0
def hhistplot(counter, highlighted, indent = "", pipe = sys.stdout, w = 20):
    BLOCK = "█"
    LEFT_HALF_BLOCK = "▌"

    try:
        W, H = os.get_terminal_size()
    except (OSError, AttributeError):
        W, H = 80, 24

    plot_w = min(w, W - 10 - len(indent))
    scale = plot_w / max(counter.values())
    data = sorted(counter.items())

    if highlighted  != None and highlighted not in counter:
        bisect.insort_left(data, (highlighted, 0))

    header_width = max(len(str(value)) for _, value in data)

    for key, value in data:
        label = str(key)
        bar_size = int(value * scale)
        header = indent + "[" + str(value).rjust(header_width) + "] "
        bar = (BLOCK * bar_size if bar_size > 0 else LEFT_HALF_BLOCK) + " "

        label_avail_space = W - 2 - len(bar) - len(header)
        if len(label) > label_avail_space:
            label = label[:label_avail_space - 3] + "..."

        line = bar + label
        if key == highlighted:
            line = color.highlight(line, color.term.PLAIN, color.term.RED)

        pipe.write(header + line + "\n")
Ejemplo n.º 24
0
    def register(
        self, bundle, classes, properties, svc_instance, factory, prototype
    ):
        """
        Registers a service.

        :param bundle: The bundle that registers the service
        :param classes: The classes implemented by the service
        :param properties: The properties associated to the service
        :param svc_instance: The instance of the service
        :param factory: If True, the given service is a service factory
        :param prototype: If True, the given service is a prototype service
                          factory (the factory argument is considered True)
        :return: The ServiceRegistration object
        """
        with self.__svc_lock:
            # Prepare properties
            service_id = self.__next_service_id
            self.__next_service_id += 1
            properties[OBJECTCLASS] = classes
            properties[SERVICE_ID] = service_id
            properties[SERVICE_BUNDLEID] = bundle.get_bundle_id()

            # Compute service scope
            if prototype:
                properties[SERVICE_SCOPE] = SCOPE_PROTOTYPE
            elif factory:
                properties[SERVICE_SCOPE] = SCOPE_BUNDLE
            else:
                properties[SERVICE_SCOPE] = SCOPE_SINGLETON

            # Force to have a valid service ranking
            try:
                properties[SERVICE_RANKING] = int(properties[SERVICE_RANKING])
            except (KeyError, ValueError, TypeError):
                properties[SERVICE_RANKING] = 0

            # Make the service reference
            svc_ref = ServiceReference(bundle, properties)

            # Make the service registration
            svc_registration = ServiceRegistration(
                self.__framework, svc_ref, properties, self.__sort_registry
            )

            # Store service information
            if prototype or factory:
                self.__svc_factories[svc_ref] = (svc_instance, svc_registration)

            # Also store factories, as they must appear like any other service
            self.__svc_registry[svc_ref] = svc_instance

            for spec in classes:
                spec_refs = self.__svc_specs.setdefault(spec, [])
                bisect.insort_left(spec_refs, svc_ref)

            # Reverse map, to ease bundle/service association
            bundle_services = self.__bundle_svc.setdefault(bundle, set())
            bundle_services.add(svc_ref)
            return svc_registration
Ejemplo n.º 25
0
def getnum():
    for c in A:
        bisect.insort_left(result, c)

    while len(A) > 0:
        minvalue = min(A)
        A.remove(minvalue)

        tm_ti = time.clock()
        keypos = bisect.bisect_right(result, minvalue)
        cp_result = copy.copy(result[keypos:])
        tm_ti = time.clock()
        for c in cp_result:
            tmp = c | minvalue
            use_tmp = gmpy2.digits(tmp, 2)
            use_tmp = "0" * (genroot.prp_num - len(use_tmp)) + use_tmp

            if (genroot.getsup(use_tmp))[1] < sup_f:
                continue

            if tmp == c:
                continue
            if tmp >= MAXVALUE:
                break
            pos = bisect.bisect_right(result, tmp)
            if result[pos - 1] != tmp:
                bisect.insort_left(result, tmp)
        print("Topology numbers:", len(result),  'used time for sort:%.3f seconds' % (time.clock() - tm_ti))
Ejemplo n.º 26
0
    def add_message(self, room_id, message):
        assert room_id in self.volt
        entry = self.volt[room_id]

        bisect.insort_left(entry, message) #Tempolary.  O(n), slow.

        self.volt[room_id] = entry
Ejemplo n.º 27
0
    def __init__(self, db, filename):
        Binary.__init__(self)

        self.db = db

        self.pe = PE2(filename, fast_load=True)
        self.__data_sections = []
        self.__data_sections_content = []
        self.__exec_sections = []

        self.set_arch_name()

        base = self.pe.OPTIONAL_HEADER.ImageBase

        for s in self.pe.sections:
            start = base + s.VirtualAddress

            is_data = self.__section_is_data(s)
            is_exec = self.__section_is_exec(s)

            if is_data or is_exec:
                bisect.insort_left(self._sorted_sections, start)

            self._abs_sections[start] = SectionAbs(
                    s.Name.decode().rstrip(' \0'),
                    start,
                    s.Misc_VirtualSize,
                    s.SizeOfRawData,
                    is_exec,
                    is_data,
                    s.get_data())
Ejemplo n.º 28
0
    def _add_handler(
        self, pattern, handler_class, path=None, kwargs=None, priority=100, code_filename=None, code_lineno=None
    ):

        if not self.handlers:
            host_pattern = re.compile(r".*$")
            handlers = []
            self.handlers.append((host_pattern, handlers))
        else:
            host_pattern, handlers = self.handlers[0]
            assert host_pattern.pattern == ".*$"

        assert pattern is not None and handler_class is not None

        urlspec = OrderedURLSpec(
            priority,
            pattern,
            handler_class,
            path=path,
            kwargs=kwargs,
            code_filename=code_filename,
            code_lineno=code_lineno,
        )

        bisect.insort_left(handlers, urlspec)
Ejemplo n.º 29
0
    def zincrby(self, member, amount=1):
        """ Finds member in the sorted set and increments its score.

        If no member is found, then member is insert and given a score of
        ``amount``.
        """
        i = bisect.bisect_left(self._members, (member, None))
        try:
            # Check if the next member is the same.  If it is, then update the score
            if self._members[i][0] == member:
                j = self._scores.index((self._members[i][1], self._members[i][0]))
                new_score = self._members[i][1] + amount
                self._scores[j] = (new_score, member)
                self._members[i] = (member, new_score)
            elif self._members[i + 1][0] == member:
                j = self._scores.index((self._members[i + 1][1], self._members[i + 1][0]))
                new_score = self._members[i][1] + amount
                self._scores[j] = (new_score, member)
                self._members[i + 1] = (member, new_score)
            else:
                new_score = amount
                self._members.insert(i, (member, amount))
                bisect.insort_left(self._scores, (amount, member))
        except IndexError:
            new_score = amount
            self._members.insert(i, (member, amount))
            bisect.insort_left(self._scores, (amount, member))
        return new_score
Ejemplo n.º 30
0
    def redo(self):
        part = self.part
        id_num = self.id_num
        origin_pt = self.origin_pt
        # need to always reserve an id
        vh = part._createHelix(id_num, origin_pt, (0, 0, 1), self.length, self.color)

        if self.safe:   # update all neighbors
            if not self.neighbors:
                self.neighbors = part._getVirtualHelixOriginNeighbors(id_num, self.threshold)

            neighbors = self.neighbors
            part.vh_properties.loc[id_num, 'neighbors'] = str(list(neighbors))
            for neighbor_id in neighbors:
                nneighbors = literal_eval(
                    part.getVirtualHelixProperties(neighbor_id, 'neighbors')
                )
                bisect.insort_left(nneighbors, id_num)
                part.vh_properties.loc[neighbor_id, 'neighbors'] = str(list(nneighbors))
        else:
            neighbors = self.neighbors
        if self.keys is not None:
            part.setVirtualHelixProperties(id_num,
                                           self.keys, self.values,
                                           safe=False)
            part.resetCoordinates(id_num)
        part.partVirtualHelixAddedSignal.emit(part, id_num, vh, neighbors)
Ejemplo n.º 31
0
def simulate_population(population, NRUNS, fixed_parameters, stylized_facts_real_life):
    """
    Simulate a population of parameter spaces for the sim-fin model
    :param population: population of parameter spaces used to simulate model
    :param number_of_runs: number of times the simulation should be run
    :param simulation_time: amount of days which will be simulated for each run
    :param fixed_parameters: dictionary of parameters which will not be changed
    :return: simulated population, average population fitness
    """
    simulated_population = []
    for idx, individual in enumerate(population):
        # combine individual parameters with fixed parameters
        parameters = individual.parameters.copy()
        params = fixed_parameters.copy()
        params.update(parameters)

        stylized_facts = {'autocorrelation': np.inf, 'kurtosis': np.inf, 'autocorrelation_abs': np.inf,
                          'hurst': np.inf, 'av_dev_from_fund': np.inf}

        # simulate the model
        #traders = []
        obs = []
        for seed in range(NRUNS):
            traders, orderbook = init_objects.init_objects(params, seed)
            traders, orderbook = simfinmodel.sim_fin_model(traders, orderbook, params, seed)
            # traders.append(traders)
            obs.append(orderbook)

        # store simulated stylized facts
        mc_prices, mc_returns, mc_autocorr_returns, mc_autocorr_abs_returns, mc_volatility, mc_volume, mc_fundamentals = organise_data(obs)
        mc_dev_fundamentals = (mc_prices - mc_fundamentals) / mc_fundamentals

        mean_autocor = []
        mean_autocor_abs = []
        mean_kurtosis = []
        long_memory = []
        #long_memory_deviation_fundamentals = []
        av_deviation_fundamental = []
        for col in mc_returns:
            mean_autocor.append(np.mean(mc_autocorr_returns[col][1:])) #correct?
            mean_autocor_abs.append(np.mean(mc_autocorr_abs_returns[col][1:]))
            mean_kurtosis.append(mc_returns[col][2:].kurtosis())
            long_memory.append(hurst(mc_prices[col][2:]))
            av_deviation_fundamental.append(np.mean(mc_dev_fundamentals[col][1:]))
            #long_memory_deviation_fundamentals.append(hurst(mc_dev_fundaments[col][2:]))


        #stylized_facts['autocorrelation'] = np.mean(mean_autocor)
        stylized_facts['kurtosis'] = np.mean(mean_kurtosis)
        stylized_facts['autocorrelation_abs'] = np.mean(mean_autocor_abs)
        stylized_facts['hurst'] = np.mean(long_memory)
        stylized_facts['av_dev_from_fund'] = np.mean(av_deviation_fundamental)

        # create next generation individual
        cost = cost_function(stylized_facts_real_life, stylized_facts)
        next_gen_individual = Individual(parameters, stylized_facts, cost)
        # insert into next generation population, lowest score (best) to the left
        bisect.insort_left(simulated_population, next_gen_individual)

    average_population_fitness = average_fitness(simulated_population)

    return simulated_population, average_population_fitness
Ejemplo n.º 32
0
 def insort(self, arr):
     bisect.insort_left(self._arrs, arr)
Ejemplo n.º 33
0
 def add_article(self, article: Article):
     insort_left(self._articles, article)
     self._articles_index[article.id] = article
Ejemplo n.º 34
0
 def add_major(self, state: State) -> None:
     # NOTE: major means the interval stubs
     self.add(state)
     bisect.insort_left(self.major_index, state.index)
Ejemplo n.º 35
0
    def append(self, value):
        '''
		Add a new value to the internal list of values.
		'''
        with self._s:
            bisect.insort_left(self._d, value)
Ejemplo n.º 36
0
 def add(self, item: Asset):
     """
     Add asset to bank
     :return: Nothing
     """
     bisect.insort_left(self.asset_list, item)
complexity_relationship =\
    ['MAX_Complexity',
     'MAX_Complexity_1',
     'MAX_Complexity_3',
     'MAX_Complexity_6',
     'MAX_Complexity_12']
# complexity variables
for variable in complexity_relationship:
    input_data.loc[:, variable] = input_data.loc[:, variable].astype(int)
# transform categorical variable using Label encoder
list_label_enc = []
for column in categorical_variables:
    le = preprocessing.LabelEncoder()
    le.fit(input_data[column].astype(str))
    le_classes = le.classes_.tolist()
    bisect.insort_left(le_classes, '<unknown>')
    le.classes_ = le_classes
    list_label_enc.append(le)
    input_data[column] = le.transform(input_data[column].astype(str))
    default_values_for_missing = \
        default_values_for_missing.append(
            {'column_name': variable,
             'value': le.classes_.index('<unknown>')},
            ignore_index=True)
print(default_values_for_missing)

# save encoders list to disk
with open(root_folder + model_object_folder + encoders_pickle_file,
          "wb") as pickle_encoders_file:
    pickle.dump(list_label_enc, pickle_encoders_file)
Ejemplo n.º 38
0
 def recordTweet(self, tweetName: str, time: int) -> None:
     bisect.insort_left(self.user[tweetName], time)
Ejemplo n.º 39
0
 def append(self, line_info):
     # self.__lines.append(line_info)
     insort_left(self.__lines, line_info)
Ejemplo n.º 40
0
  def _find_series(self, tags, requestContext=None):
    selector = None
    selector_cnt = None
    filters = []

    # loop through tagspecs, look for best spec to use as selector
    for tagspec in tags:
      (tag, operator, spec) = self.parse_tagspec(tagspec)

      if operator == '=':
        matches_empty = spec == ''
        if not matches_empty:
          cnt = self.r.scard('tags:' + tag + ':values:' + spec)
          if not selector or selector[1] != '=' or selector_cnt > cnt:
            if selector:
              filters.append(selector)
            selector = (tag, operator, spec)
            selector_cnt = cnt
            continue
        filters.append((tag, operator, spec))

      elif operator == '=~':
        pattern = re.compile(spec)
        matches_empty = bool(pattern.match(''))
        if not matches_empty and (not selector or selector[1] != '='):
          cnt = self.r.scard('tags:' + tag + ':values')
          if not selector or selector_cnt > cnt:
            if selector:
              filters.append(selector)
            selector = (tag, operator, pattern)
            selector_cnt = cnt
            continue
        filters.append((tag, operator, pattern))

      elif operator == '!=':
        matches_empty = spec != ''
        if not matches_empty and (not selector or selector[1] != '='):
          cnt = self.r.scard('tags:' + tag + ':values')
          if not selector or selector_cnt > cnt:
            if selector:
              filters.append(selector)
            selector = (tag, operator, spec)
            selector_cnt = cnt
            continue
        filters.append((tag, operator, spec))

      elif operator == '!=~':
        pattern = re.compile(spec)
        matches_empty = not pattern.match('')
        if not matches_empty and (not selector or selector[1] != '='):
          cnt = self.r.scard('tags:' + tag + ':values')
          if not selector or selector_cnt > cnt:
            if selector:
              filters.append(selector)
            selector = (tag, operator, pattern)
            selector_cnt = cnt
            continue
        filters.append((tag, operator, pattern))

      else:
        raise ValueError("Invalid operator %s" % operator)

    if not selector:
      raise ValueError("At least one tagspec must not match the empty string")

    # get initial list of series
    (tag, operator, spec) = selector

    # find list of values that match the tagspec
    values = None
    if operator == '=':
      values = [spec]
    elif operator == '=~':
      # see if we can identify a literal prefix to filter by in redis
      match = None
      m = re.match('([a-z0-9]+)([^*?|][^|]*)?$', spec.pattern)
      if m:
        match = m.group(1) + '*'
      values = [value for value in self.r.sscan_iter('tags:' + tag + ':values', match=match) if spec.match(value) is not None]
    elif operator == '!=':
      values = [value for value in self.r.sscan_iter('tags:' + tag + ':values') if value != spec]
    elif operator == '!=~':
      values = [value for value in self.r.sscan_iter('tags:' + tag + ':values') if spec.match(value) is None]

    # if this query matched no values, just short-circuit since the result of the final intersect will be empty
    if not values:
      return []

    results = []

    # apply filters
    operators = ['=','!=','=~','!=~']
    filters.sort(key=lambda a: operators.index(a[1]))

    for series in self.r.sunion(*['tags:' + tag + ':values:' + value for value in values]):
      parsed = self.parse(series)
      matched = True

      for (tag, operator, spec) in filters:
        value = parsed.tags.get(tag, '')
        if (
          (operator == '=' and value != spec) or
          (operator == '=~' and spec.match(value) is None) or
          (operator == '!=' and value == spec) or
          (operator == '!=~' and spec.match(value) is not None)
        ):
          matched = False
          break

      if matched:
        bisect.insort_left(results, series)

    return results
Ejemplo n.º 41
0
myset = {"apple", "banana", "cherry"}
myset = set([1,2,3])
set1 | set2 #union, set1.union(set2); intersection: &; difference: -;


from collections import defaultdict
d = defaultdict(list) #list: [], int: 0
d = defaultdict(lambda: "Not Present")
d["a"] = 1
d["b"]
########binary search###############
import bisect
a = [1,3,5]
bisect.bisect_left(a, 2) #1.  log(n) 
a.insert(1,2) #a = [1,2,3,5]
bisect.insort_left(a, 2) # for insert, 


####priority queue with heapq
import heapq
lo = []
hi = []
heapq.heapify(lo) #n*log(n), in place change, lo[0] is the smallest one, lo.pop(0)
heapq._heapify_max(hi)
heapq._heapreplace_max(a, 7)
heapq._heappop_max(a)
def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)

heapq.heappush(lo, value) #log(n) for push and pop
Ejemplo n.º 42
0
def simulate_population_msm(population, NRUNS, fixed_parameters, stylized_facts_real_life, weights):
    """
    Simulate a population of parameter spaces for the sim-fin model
    :param population: population of parameter spaces used to simulate model
    :param number_of_runs: number of times the simulation should be run
    :param simulation_time: amount of days which will be simulated for each run
    :param fixed_parameters: dictionary of parameters which will not be changed
    :return: simulated population, average population fitness
    """
    simulated_population = []
    for idx, individual in enumerate(population):
        # combine individual parameters with fixed parameters
        parameters = individual.parameters.copy()
        params = fixed_parameters.copy()
        params.update(parameters)

        # simulate the model
        obs = []
        for seed in range(NRUNS):
            traders, orderbook = init_objects.init_objects(params, seed)
            traders, orderbook = simfinmodel.sim_fin_model(traders, orderbook, params, seed)
            obs.append(orderbook)

        # store simulated stylized facts
        mc_prices, mc_returns, mc_autocorr_returns, mc_autocorr_abs_returns, mc_volatility, mc_volume, mc_fundamentals = organise_data(obs)

        first_order_autocors = []
        autocors1 = []
        autocors5 = []
        mean_abs_autocor = []
        kurtoses = []
        spy_abs_auto10 = []
        spy_abs_auto25 = []
        spy_abs_auto50 = []
        spy_abs_auto100 = []
        cointegrations = []
        for col in mc_returns:
            first_order_autocors.append(autocorrelation_returns(mc_returns[col][1:], 25))
            autocors1.append(mc_returns[col][1:].autocorr(lag=1))
            autocors5.append(mc_returns[col][1:].autocorr(lag=5))
            mean_abs_autocor.append(autocorrelation_abs_returns(mc_returns[col][1:], 25))
            kurtoses.append(mc_returns[col][2:].kurtosis())
            spy_abs_auto10.append(mc_returns[col][1:].abs().autocorr(lag=10))
            spy_abs_auto25.append(mc_returns[col][1:].abs().autocorr(lag=25))
            spy_abs_auto50.append(mc_returns[col][1:].abs().autocorr(lag=50))
            spy_abs_auto100.append(mc_returns[col][1:].abs().autocorr(lag=100))
            cointegrations.append(cointegr(mc_prices[col][1:], mc_fundamentals[col][1:])[0])

        stylized_facts_sim = np.array([
            np.mean(first_order_autocors),
            np.mean(autocors1),
            np.mean(autocors5),
            np.mean(mean_abs_autocor),
            np.mean(kurtoses),
            np.mean(spy_abs_auto10),
            np.mean(spy_abs_auto25),
            np.mean(spy_abs_auto50),
            np.mean(spy_abs_auto100),
            np.mean(cointegrations)
        ])

        # create next generation individual
        cost = quadratic_loss_function(stylized_facts_sim, stylized_facts_real_life, weights)
        next_gen_individual = Individual(parameters, stylized_facts_sim, cost)
        # insert into next generation population, lowest score (best) to the left
        bisect.insort_left(simulated_population, next_gen_individual)

    average_population_fitness = average_fitness(simulated_population)

    return simulated_population, average_population_fitness
Ejemplo n.º 43
0
 def track(self, x: int) -> None:
     bisect.insort_left(self.stream, x)
Ejemplo n.º 44
0
from bisect import bisect_left, bisect_right, insort_left,  insort_right
from string import ascii_lowercase
from heapq import heappush, heappop, heapify
from collections import Counter, defaultdict
from itertools import product


T = int(input())

for it in range(T):
    n = int(input())
    a = list(map(int, input().split()))
    ans = [1] * n
    qpp = []
    cnt = n
    for i in range(n):
      insort_left(qpp,a[i]) #qpp: [0..i] 共 i+1个 
      #target: ans[i-1] + 1 
      #i-target+1 = i-ans[i-1]
      if i and qpp[i - ans[i-1]] >= ans[i-1] + 1:
        ans[i] = ans[i-1] + 1
      else:
        ans[i] = ans[i-1]
        cnt -= 1

    print("Case #%d:"%it, end="")
    for num in ans:
      print(" %d"%num, end="")
    print()
 def add_movie(self, movie: Movie):
     if isinstance(movie, Movie):
         # self._movies.append(movie)
         insort_left(self._movies, movie)
         self._movie_index[movie.id] = movie
Ejemplo n.º 46
0
#maintain a sorted list in sorted order while mutating it
import bisect

#adding values by finding position then inserting. Or just shortcut insort
l = [x for x in range(10)]
l.insert(bisect.bisect_left(l, 2.5), 2.5)
bisect.insort_left(l, 3.5)
print(l)
Ejemplo n.º 47
0
 def add_bid(self, price, volume, agent):
     """Add a bid to the (price low-high, age young-old) sorted bids book"""
     bid = Order(order_type='b', owner=agent, price=price, volume=volume)
     bisect.insort_left(self.bids, bid)
     self.update_bid_ask_spread('bid')
     return bid
Ejemplo n.º 48
0
import bisect

N = int(input())
B = list(map(int, input().split()))
B_index = sorted([(b, _index) for _index, b in enumerate(B)])

complete = []
ans = 0
for i, (val, _index) in enumerate(B_index):
    left = _index - bisect.bisect_left(complete, _index)
    right = N - left - i - 1
    ans += min(left, right)
    bisect.insort_left(complete, _index)

print(ans)
Ejemplo n.º 49
0
 def add(self, e):
     # add the event at the proper (sorted) spot.
     bisect.insort_left(self.lst, e)
Ejemplo n.º 50
0
            path.append(v)
            q.extend([x for x in d[v][::-1] if x not in path])
    for j in path:
        print j,


def bfs(d, start, path=[]):
    q = deque()
    q.append(start)
    while q:
        v = q.popleft()
        if v not in path:
            path.append(v)
            q.extend([x for x in d[v] if x not in path])
    for j in path:
        print j,


r = lambda: sys.stdin.readline()
a, b, c = map(int, r().split())
d = {}
for i in xrange(1, a + 1):
    d[i] = []
for i in xrange(0, b):
    x, y = map(int, r().split())
    bisect.insort_left(d[x], y)
    bisect.insort_left(d[y], x)
dfs(d, c)
print ''
bfs(d, c)
Ejemplo n.º 51
0
    def neighboring(self, alignment_info, j_pegged=None):
        """
        Determine the neighbors of ``alignment_info``, obtained by
        moving or swapping one alignment point

        :param j_pegged: If specified, neighbors that have a different
            alignment point from j_pegged will not be considered
        :type j_pegged: int

        :return: A set neighboring alignments represented by their
            ``AlignmentInfo``
        :rtype: set(AlignmentInfo)
        """
        neighbors = set()

        l = len(alignment_info.src_sentence) - 1  # exclude NULL
        m = len(alignment_info.trg_sentence) - 1
        original_alignment = alignment_info.alignment
        original_cepts = alignment_info.cepts

        for j in range(1, m + 1):
            if j != j_pegged:
                # Add alignments that differ by one alignment point
                for i in range(0, l + 1):
                    new_alignment = list(original_alignment)
                    new_cepts = deepcopy(original_cepts)
                    old_i = original_alignment[j]

                    # update alignment
                    new_alignment[j] = i

                    # update cepts
                    insort_left(new_cepts[i], j)
                    new_cepts[old_i].remove(j)

                    new_alignment_info = AlignmentInfo(
                        tuple(new_alignment),
                        alignment_info.src_sentence,
                        alignment_info.trg_sentence,
                        new_cepts,
                    )
                    neighbors.add(new_alignment_info)

        for j in range(1, m + 1):
            if j != j_pegged:
                # Add alignments that have two alignment points swapped
                for other_j in range(1, m + 1):
                    if other_j != j_pegged and other_j != j:
                        new_alignment = list(original_alignment)
                        new_cepts = deepcopy(original_cepts)
                        other_i = original_alignment[other_j]
                        i = original_alignment[j]

                        # update alignments
                        new_alignment[j] = other_i
                        new_alignment[other_j] = i

                        # update cepts
                        new_cepts[other_i].remove(other_j)
                        insort_left(new_cepts[other_i], j)
                        new_cepts[i].remove(j)
                        insort_left(new_cepts[i], other_j)

                        new_alignment_info = AlignmentInfo(
                            tuple(new_alignment),
                            alignment_info.src_sentence,
                            alignment_info.trg_sentence,
                            new_cepts,
                        )
                        neighbors.add(new_alignment_info)

        return neighbors
Ejemplo n.º 52
0
 def add(self, num):
     bisect.insort_left(self._numbers, num)
Ejemplo n.º 53
0
    n = int(input())
    EP = []
    S = []
    for i in range(n):
        a, b, c, d = map(int, input().split())
        if a == c: # y軸と平行
            EP.append(EndPoint(Vector2(a, min(b, d)), i, 0)) # bottom
            EP.append(EndPoint(Vector2(a, max(b, d)), i, 3)) # top
            S.append([Vector2(a, max(b, d)), Vector2(a, min(b, d))])
        else: # x軸と平行
            EP.append(EndPoint(Vector2(min(a, c), b), i, 1)) # left
            EP.append(EndPoint(Vector2(max(a, c), b), i, 2)) # right
            S.append([Vector2(min(a, c), b), Vector2(max(a, c), b)])
    
    EP = sorted(EP, key=lambda x: (x.p.y, x.st))
    BT = []
    cnt = 0
    for e in EP:
        if e.st == 3: # top
            indx = bisect.bisect_left(BT, e.p.x)
            BT.pop(indx)
            # BT.remove(e.p.x)
        elif e.st == 0: # bottom
            bisect.insort_left(BT, e.p.x)
        elif e.st == 1: # left
            l = bisect.bisect_left(BT, S[e.seg][0].x)
            r = bisect.bisect_right(BT, S[e.seg][1].x)
            cnt += r-l

    print(cnt)
Ejemplo n.º 54
0
'USER_COUNTY_NAME','USER_COUNTRY_CODE','USER_CONTINENT_CODE',
'USER_TIME_ZONE','USER_REGION_CODE','USER_CITY_NAME',
'REVISION_TAGS', 'ip_prefix', 'revision_comment_category', 
'revision_comment_property'
]

label_encoders = {}
for attrib in attributes_to_encode:
    print 'loading %s encoder' % attrib
    with open('./models/'+attrib+'_label_encoder.bin', 'rb') as f:
        label_encoders[attrib] = cPickle.load(f)

for attrib in attributes_to_encode:
    le = label_encoders[attrib]
    classes = le.classes_.tolist()
    bisect.insort_left(classes, 'other')
    le.classes_ = classes

print 'mapping'
for attrib in attributes_to_encode:
    classes = label_encoders[attrib].classes_
    df[attrib] = df[attrib].map(lambda s: 'other' if s not in classes else s)

print 'encoding attributes'
for attrib in attributes_to_encode:
    print 'encoding %s' % attrib
    le = label_encoders[attrib]
    df[attrib] = le.transform(df[attrib])
    print 'done with %s' % attrib

print 'creating new df'
Ejemplo n.º 55
0
def find_k_best_moves(U, frame_array, index, i, count, M, start):

    # display update
    details = {
        "process_id": os.getpid(),
        "bacterium_index": i + 1,
        "bacteria_count": len(U),
        "universe_index": index + 1,
        "universe_count": count,
        "current_runtime": time.time() - start
    }

    safe_print("[PID: {process_id}] find_k_best_moves: "
               "Bacterium {bacterium_index} of {bacteria_count} in "
               "Universe {universe_index} of {universe_count} "
               "({current_runtime:.2f} sec)".format(**details))

    # Find best moves for each bacterium in U
    bacterium = U[i]
    bacterium.v = np.zeros(2)
    bacterium.w = np.zeros(2)
    k_best_moves = []
    U_copy = deepcopy_list(U)

    dx_space = np.linspace(-Config.MAX_X_MOTION, Config.MAX_X_MOTION,
                           Config.MAX_X_RESOLUTION)

    dy_space = np.linspace(-Config.MAX_Y_MOTION, Config.MAX_Y_MOTION,
                           Config.MAX_Y_RESOLUTION)

    dtheta_space = np.linspace(-Config.MAX_ROTATION, Config.MAX_ROTATION,
                               Config.MAX_ROTATION_RESOLUTION)

    dlength_space = np.linspace(Config.MIN_LENGTH_INCREASE,
                                Config.MAX_LENGTH_INCREASE,
                                Config.LENGTH_INCREASE_RESOLUTION)

    split_ratio_space = np.linspace(Config.SPLIT_RATIO_BEGINNING,
                                    Config.SPLIT_RATIO_END,
                                    Config.SPLIT_RATIO_RESOLUTION)

    for dx in dx_space:
        for dy in dy_space:
            for dtheta in dtheta_space:
                for dlength in dlength_space:

                    bacterium_copy = deepcopy(bacterium)

                    bacterium_copy.pos += np.array([dx, dy, 0])
                    bacterium_copy.theta += dtheta
                    bacterium_copy.length += dlength

                    bacterium_copy.update()
                    temp = U_copy[i]
                    U_copy[i] = bacterium_copy
                    collisionEventsHandler.run2(U_copy, i, M)
                    U_copy[i] = temp

                    c = cost(frame_array, [bacterium_copy])
                    bisect.insort_left(k_best_moves,
                                       (c, dx, dy, dtheta, dlength))

    # splitting
    to_insert = []
    for c, dx, dy, dtheta, dlength in k_best_moves:
        bacterium_copy = deepcopy(bacterium)

        bacterium_copy.pos += np.array([dx, dy, 0])
        bacterium_copy.theta += dtheta
        bacterium_copy.length += dlength
        bacterium_copy.update()

        if bacterium_copy.length > Config.MAX_LENGTH_BEFORE_SPLIT:
            for split_ratio in split_ratio_space:
                bacterium_copy_2 = deepcopy(bacterium_copy)

                new_bacterium = split(bacterium_copy_2, split_ratio)
                if (bacterium_copy_2.length < Config.MIN_LENGTH
                        or new_bacterium.length < Config.MIN_LENGTH):
                    continue

                c = cost(frame_array, [bacterium_copy_2, new_bacterium]) + 5
                to_insert.append((c, dx, dy, dtheta, dlength, split_ratio))

    for e in to_insert:
        bisect.insort_left(k_best_moves, e)

    # result extracted from k_best_moves
    results = [e[1:] for e in k_best_moves]

    return (index, i, results)
Ejemplo n.º 56
0
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x: lo = mid+1
        else: hi = mid
    return lo

def bisect_right(a, x, lo=0, hi=None):
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo
'''


import bisect

help(bisect)
a = [0,1,1,3,4]
print(len(a))
print(bisect.bisect_left(a, 1))
print(bisect.bisect_right(a, 1))
print(bisect.bisect_left(a, -1))
print(bisect.bisect_left(a, 10))
bisect.insort_left(a, 6)
print(a)
Ejemplo n.º 57
0
def _eliminationOrder_OLD(gm, orderMethod=None, nExtra=-1, cutoff=inf, priority=None, target=None):
  """Find an elimination order for a graphical model
  Args:
    gm (GraphModel): A graphical model object
    method (str): Heuristic method; one of {'minfill','wtminfill','minwidth','wtminwidth','random'}
    nExtra (int): Randomly select eliminated variable from among the best plus nExtra; this adds
        randomness to the order selection process.  0 => randomly from best; -1 => no randomness (default)
    cutoff (float): Quit early if ``score`` exceeds a user-supplied cutoff value (returning ``target, cutoff``)
    target (list): If the identified order is better than cutoff, write it directly into passed ``target`` list
    priority (list, optional): Optional list of variable priorities; lowest priority variables are 
        eliminated first.  Useful for mixed elimination models, such as marginal MAP inference tasks.
  Returns:
    list: The identified elimination order
    float: The "score" of this ordering
  Using ``target`` and ``cutoff`` one can easily search for better orderings by repeated calls:
  >>> ord, score = eliminationOrder(model, 'minfill', nExtra=2, cutoff=score, target=ord) 
  """
  import bisect
  orderMethod = 'minfill' if orderMethod is None else orderMethod.lower()
  priority = [1 for x in gm.X] if priority is None else priority

  if   orderMethod == 'minfill':    score = lambda adj,Xj: sum([0.5*len(adj[Xj]-adj[Xk]) for Xk in adj[Xj]])
  elif orderMethod == 'wtminfill':  score = lambda adj,Xj: sum([(adj[Xj]-adj[Xk]).nrStatesDouble() for Xk in adj[Xj]])
  elif orderMethod == 'minwidth':   score = lambda adj,Xj: len(adj[Xj])
  elif orderMethod == 'wtminwidth': score = lambda adj,Xj: adj[Xj].nrStatesDouble()
  elif orderMethod == 'random':     score = lambda adj,Xj: np.random.rand()
  else: raise ValueError('Unknown ordering method: {}'.format(orderMethod))

  adj = [ VarSet([Xi]) for Xi in gm.X ]
  for Xi in gm.X: 
    for f in gm.factorsWith(Xi, copy=False):
      adj[Xi] |= f.vars

  # initialize priority queue of scores using e.g. heapq or sort
  scores  = [ (priority[Xi],score(adj,Xi),Xi) for Xi in gm.X ]
  reverse = scores[:]
  scores.sort()
  totalSize = 0.0
  _order = [0 for Xi in gm.X]

  for idx in range(gm.nvar):
    pick = 0
    Pi,Si,Xi = scores[pick]
    if nExtra >= 0:
      mx = bisect.bisect_right(scores, (Pi,Si,gm.X[-1]))  # get one past last equal-priority & score vars
      pick = min(mx+nExtra, len(scores))                  # then pick a random "near-best" variable
      pick = np.random.randint(pick)
      Pi,Si,Xi = scores[pick]
    del scores[pick]
    _order[idx] = Xi.label        # write into order[idx] = Xi
    totalSize += adj[Xi].nrStatesDouble()
    if totalSize > cutoff: return target,cutoff  # if worse than cutoff, quit with no changes to "target"
    fix = VarSet()
    for Xj in adj[Xi]:
      adj[Xj] |= adj[Xi]
      adj[Xj] -= [Xi]
      fix |= adj[Xj]   # shouldn't need to fix as much for min-width?
    for Xj in fix:
      Pj,Sj,Xj = reverse[Xj]
      jPos = bisect.bisect_left(scores, (Pj,Sj,Xj))
      del scores[jPos]                        # erase (Pj,Sj,Xj) from heap 
      reverse[Xj] = (Pj,score(adj,Xj),Xj)     
      bisect.insort_left(scores, reverse[Xj]) # add (Pj,score(adj,Xj),Xj) to heap & update reverse lookup
  if not (target is None): 
    target.extend([None for i in range(len(target),len(_order))])  # make sure order is the right size
    for idx in range(gm.nvar): target[idx]=_order[idx]   # copy result if completed without quitting
  return _order,totalSize
Ejemplo n.º 58
0
 def add_child(self, child):
     bisect.insort_left(self.chields, child)
Ejemplo n.º 59
0
 def add(self, state: State) -> None:
     self.index_to_state[state.index] = state
     bisect.insort_left(self.ordered_index, state.index)
 def add_movie(self, movie: Movie):
     insort_left(self._movies, movie)
     self._movies_index[movie.rank] = movie