Beispiel #1
0
    def from_seconds(cls, events: List[BPMAtSecond]) -> TimeMap:
        """Create a time map from a list of BPM changes with time positions
        given in seconds. The first BPM implicitely happens at beat zero"""
        if not events:
            raise ValueError("No BPM defined")

        grouped_by_time = group_by(events, key=lambda e: e.seconds)
        for time, events_at_time in grouped_by_time.items():
            if len(events_at_time) > 1:
                raise ValueError(f"Multiple BPMs defined at {time} seconds : {events}")

        # take the first BPM change then compute from there
        sorted_events = sorted(events, key=lambda e: e.seconds)
        first_event = sorted_events[0]
        current_beat = Fraction(0)
        bpm_changes = [BPMChange(current_beat, first_event.seconds, first_event.BPM)]
        for previous, current in windowed(sorted_events, 2):
            if previous is None or current is None:
                continue

            seconds_since_last_event = current.seconds - previous.seconds
            beats_since_last_event = (
                previous.BPM * seconds_since_last_event
            ) / Fraction(60)
            current_beat += beats_since_last_event
            bpm_change = BPMChange(current_beat, current.seconds, current.BPM)
            bpm_changes.append(bpm_change)

        return cls(
            events_by_beats=SortedKeyList(bpm_changes, key=lambda b: b.beats),
            events_by_seconds=SortedKeyList(bpm_changes, key=lambda b: b.seconds),
        )
Beispiel #2
0
 def __init__(self, bids: bool):
     if bids:
         self.data = SortedKeyList(key=lambda val: -val[0])
     else:
         self.data = SortedKeyList(key=lambda val: val[0])
     self.is_bids = bids
     self.time = None
 def __init__(self):
     self.bids = SortedKeyList(key=lambda x: -x.price)
     self.asks = SortedKeyList(key=lambda x: x.price)
     self.best_bid: Optional[BaseOrder] = None
     self.best_ask: Optional[BaseOrder] = None
     self.attempt_match = False
     self.trades: deque = deque()
     self.complete_orders: deque = deque()
def test_eq():
    this = SortedKeyList(range(10), key=negate)
    this._reset(4)
    that = SortedKeyList(range(20), key=negate)
    that._reset(4)
    assert not (this == that)
    that.clear()
    that.update(range(10))
    assert this == that
Beispiel #5
0
def test_update_order_consistency():
    setup = [10, 20, 30]
    slt1 = SortedKeyList(setup, key=modulo)
    slt2 = SortedKeyList(setup, key=modulo)
    addition = [40, 50, 60]
    for value in addition:
        slt1.add(value)
    slt2.update(addition)
    assert slt1 == slt2
Beispiel #6
0
def test_bisect_right():
    slt = SortedKeyList(key=modulo)
    assert slt.bisect_right(10) == 0
    slt = SortedKeyList(range(100), key=modulo)
    slt._reset(17)
    slt.update(range(100))
    slt._check()
    assert slt.bisect_right(10) == 20
    assert slt.bisect_right(0) == 20
def test_bisect():
    slt = SortedKeyList(key=negate)
    assert slt.bisect(10) == 0
    slt = SortedKeyList(range(100), key=negate)
    slt._reset(17)
    slt.update(range(100))
    slt._check()
    assert slt.bisect(10) == 180
    assert slt.bisect(0) == 200
def test_gte():
    this = SortedKeyList(range(10), key=negate)
    this._reset(4)
    that = SortedKeyList(range(10), key=negate)
    that._reset(5)
    assert this >= that
    assert that >= this
    del this[-1]
    assert that >= this
    assert not (this >= that)
Beispiel #9
0
def test_op_add():
    this = SortedKeyList(range(10), key=modulo)
    this._reset(4)
    assert (this + this + this) == (this * 3)

    that = SortedKeyList(range(10), key=modulo)
    that._reset(4)
    that += that
    that += that
    assert that == (this * 4)
Beispiel #10
0
    def representative_trajectory(self, cluster):
        # TODO: Fix this :/
        rep_trajectory = []
        #Average direction vector:
        av_vector = np.array([0.0, 0.0])
        for line in cluster:
            av_vector += line.vector
        av_vector /= len(cluster)
        print(av_vector)
        unit_av = av_vector/np.linalg.norm(av_vector)
        print(unit_av)
        x = np.array([1.0, 0.0])
        theta = np.arccos(x.dot(unit_av))
        if unit_av[1] > 0.0:
            theta = -theta
        rotation_mat = np.array([[np.cos(theta), -np.sin(theta)],
                                 [np.sin(theta), np.cos(theta)]])
        back_rotation_mat = np.array([[np.cos(-theta), np.sin(-theta)],
                              [-np.sin(-theta), np.cos(-theta)]])
        rotated_points = []
        rotated_lines = []
        for line in cluster:
            rot_v = rotation_mat.dot(line.vector)
            rot_b = line.a + line.length*rot_v
            rotated_points.append({"end": False, "point":line.a})
            rotated_points.append({"end": True, "point": rot_b})
            rotated_lines.append(LineSegment(line.a, rot_b))

        rotated_points = sorted(rotated_points, key=lambda x: x["point"][0])
        #Sort lines by starting x value
        line_start_lookup = SortedKeyList(rotated_lines, key=lambda x: x.a[0])
        #Sort lines the sweep line crosses by ending x value
        intersecting_lines = SortedKeyList([], key=lambda x:x.b[0])
        last_x = 0.0
        for point_dict in rotated_points:
            if point_dict["end"]:
                try:
                    intersecting_lines.pop(0)
                except Exception as e:
                    print("Could not generate a representative trajectory. Examine your clustering parameters")
                    break;
            else:
                intersecting_lines.add(line_start_lookup.pop(0))
            if len(intersecting_lines) >= self.min_lns:
                # diff = point_dict["point"][0] - last_x
                # if diff >= self.gamma:
                average_y = 0.0
                for line in intersecting_lines:
                    slope = line.vector[1]/line.vector[0]
                    average_y += (point_dict["point"][0]-line.a[0])*slope
                average_y /= len(intersecting_lines)
                rep_trajectory.append(np.array([point_dict["point"][0], average_y]))
        return rep_trajectory
def test_gt():
    this = SortedKeyList(range(10), key=negate)
    this._reset(4)
    that = SortedKeyList(range(10, 20), key=negate)
    that._reset(5)
    assert that > this
    assert not (this > that)
    that = SortedKeyList(range(1, 20), key=negate)
    that._reset(6)
    assert that > this
    that = SortedKeyList(range(1, 10), key=negate)
    that._reset(4)
    assert not (that > this)
Beispiel #12
0
def test_remove():
    slt = SortedKeyList(key=modulo)

    assert slt.discard(0) == None
    assert len(slt) == 0
    slt._check()

    slt = SortedKeyList([1, 2, 2, 2, 3, 3, 5], key=modulo)
    slt._reset(4)

    slt.remove(2)
    slt._check()

    assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
Beispiel #13
0
def test_delitem():
    random.seed(0)

    slt = SortedKeyList(range(100), key=modulo)
    slt._reset(17)
    while len(slt) > 0:
        del slt[random.randrange(len(slt))]
        slt._check()

    slt = SortedKeyList(range(100), key=modulo)
    slt._reset(17)
    del slt[:]
    assert len(slt) == 0
    slt._check()
Beispiel #14
0
def test_count():
    slt = SortedKeyList(key=modulo)
    slt._reset(7)

    assert slt.count(0) == 0

    for iii in range(100):
        for jjj in range(iii):
            slt.add(iii)
    slt._check()

    for iii in range(100):
        assert slt.count(iii) == iii

    slt = SortedKeyList(range(8), key=modulo)
    assert slt.count(9) == 0
Beispiel #15
0
def test_copy():
    slt = SortedKeyList(range(100), key=modulo)
    slt._reset(7)
    two = slt.copy()
    slt.add(100)
    assert len(slt) == 101
    assert len(two) == 100
Beispiel #16
0
 async def fetch_block(
     self,
     request_params: DataRequestSchema,
 ) -> Tuple[SortedKeyList, datetime]:
     from_datetime = request_params.from_datetime
     to_datetime = from_datetime + self.block_range
     async with self.pool.acquire() as conn:
         query = f"""
         SELECT
             time_bucket_gapfill($1, timestamp) AS time,
             locf(avg(price)) as price,
             sum(volume) as volume
         FROM {self.dataset_name}_ticks
         WHERE session_id = $2
         and label = $3
         and data_type = $4
         and timestamp BETWEEN $5 and $6
         GROUP BY time
         ORDER BY time ASC;
         """
         params = (
             timedelta(minutes=request_params.period),
             self.session_id,
             request_params.label,
             request_params.data_type,
             from_datetime,
             to_datetime,
         )
         result = list(await conn.fetch(query, *params))
         print(f'Fetched list: {len(result)}')
         return SortedKeyList(
             result, key=lambda x: x['time'].timestamp()), to_datetime
Beispiel #17
0
def test_islice():
    sl = SortedKeyList(key=modulo)
    sl._reset(7)

    assert [] == list(sl.islice())

    values = sorted(range(100), key=modulo)
    sl.update(values)

    for start in range(53):
        for stop in range(53):
            assert list(sl.islice(start, stop)) == values[start:stop]

    for start in range(53):
        for stop in range(53):
            assert list(sl.islice(start, stop,
                                  reverse=True)) == values[start:stop][::-1]

    for start in range(53):
        assert list(sl.islice(start=start)) == values[start:]
        assert list(sl.islice(start=start,
                              reverse=True)) == values[start:][::-1]

    for stop in range(53):
        assert list(sl.islice(stop=stop)) == values[:stop]
        assert list(sl.islice(stop=stop, reverse=True)) == values[:stop][::-1]
Beispiel #18
0
def test_delitem_slice():
    slt = SortedKeyList(range(100), key=modulo)
    slt._reset(17)
    del slt[10:40:1]
    del slt[10:40:-1]
    del slt[10:40:2]
    del slt[10:40:-2]
def test_delitem():
    random.seed(0)
    slt = SortedKeyList(range(100), key=negate)
    slt._reset(17)
    while len(slt) > 0:
        del slt[random.randrange(len(slt))]
        slt._check()
Beispiel #20
0
def test_ge():
    this = SortedKeyList(range(10, 15), key=modulo)
    this._reset(4)
    assert this >= [10, 11, 12, 13, 14]
    assert this >= [10, 11, 12, 13]
    assert this >= [10, 11, 11, 13, 14]
    assert this >= [9]
Beispiel #21
0
def test_stress(repeat=1000):
    slt = SortedKeyList((random.random() for rpt in range(1000)))

    for rpt in range(repeat):
        action = random.choice(actions)
        action(slt)

        slt._check()

        while len(slt) > 2000:
            # Shorten the sortedlist. This maintains the "jaggedness"
            # of the sublists which helps coverage.
            pos = random.randrange(len(slt._maxes))
            del slt._maxes[pos]
            del slt._keys[pos]
            del slt._lists[pos]
            slt._len = sum(len(sublist) for sublist in slt._lists)
            slt._index = []
            slt._check()

    slt._check()

    stress_update(slt)

    while len(slt) > 0:
        pos = random.randrange(len(slt))
        del slt[pos]

    slt._check()
Beispiel #22
0
Datei: day15.py Projekt: oabm/aoc
def setup(cavern, is_part2=False, elves_power=3):
	global units
	global elves
	global goblins
	global elves_atk
	global part2
	global G

	units     = SortedKeyList(key=lambda u: u[:2])
	elves     = {}
	goblins   = {}
	elves_atk = elves_power
	part2     = is_part2
	G         = nx.Graph()

	for i, row in enumerate(cavern):
		for j, cell in enumerate(row):
			if cell == FREE:
				G.add_node((i, j))
			elif cell != WALL:
				u = [i, j, 200, False, cell]
				units.add(u)

				if cell == ELF:
					elves[i, j] = u
				elif cell == GOBLIN:
					goblins[i, j] = u

	for u in units:
		u[3] = is_near_enemies(u)

	for i, j in G.nodes:
		for ni, nj in adjacent(i, j):
			if (ni, nj) in G: # assumes borders are all walls
				G.add_edge((i, j), (ni, nj))
Beispiel #23
0
    def __init__(self,
                 annotations: List[Annotation] = None,
                 namespaces: Dict[str, str] = None,
                 sofas: List[Sofa] = None,
                 views: List[View] = None):
        self.namespaces = namespaces or {}
        self._sofas = {}
        self.views = views or []
        # Annotations are sorted by begin index first (smaller first). If begin
        #  is equal, sort by end index, smaller first. This is the same as
        # comparing a Python tuple of (begin, end)
        self._annotations = defaultdict(
            lambda: SortedKeyList(key=lambda a: (a.begin, a.end)))
        _annotations = annotations or []
        for annotation in _annotations:
            self._annotations[annotation.type].add(annotation)

        # Handle sofas
        if sofas == None or len(sofas) == 0:
            _sofas = [Sofa(sofaNum=1)]
        else:
            _sofas = sofas

        for sofa in _sofas:
            self._sofas[sofa.sofaNum] = sofa

        # Find maximum id
        maximum_xmiID = 1
        for obj in chain(_sofas, _annotations):
            if obj.xmiID and obj.xmiID > maximum_xmiID:
                maximum_xmiID = obj.xmiID

        self.maximum_xmiID = maximum_xmiID
Beispiel #24
0
    def __attrs_post_init__(self):
        if not self.game:
            if not isinstance(self.parent, SegmentReference):
                raise ValueError('`game` is required when referencing Segment')
            self.game = self.parent.game

        self.subrefs = SortedKeyList(key=lambda x: x.start)

        if len(self._subrefs) > 0:
            if len(self._blacklist) > 0:
                raise ValueError('`blacklist` can not be used with `subrefs`')

            for data in self._subrefs:
                if isinstance(data, dict):
                    SubReference(**data, parent=self)
                elif isinstance(data, SubReference):
                    if data.parent is not self:
                        data.parent = self
                else:
                    raise TypeError(f'Unsupported subref type: {type(data)}')

        delattr(self, '_subrefs')

        if len(self.subrefs) == 0:
            if not self._name:
                raise ValueError('`name` is required without `subrefs`')

            SubReference(name=self._name, start=self._start, parent=self,
                         blacklist=self._blacklist)

        delattr(self, '_name')
        delattr(self, '_start')

        self.parent = self._parent
        delattr(self, '_parent')
Beispiel #25
0
 def __init__(self, n, cols, rows):
     self.n = n
     self.cols = cols
     self.rows = rows
     self.LFBs = SortedKeyList([], key=lambda x: (x[1], x[0]))
     self.LFBs.add((0, 0))
     self.grid = self.initialize_grid()
Beispiel #26
0
def test_key():
    slt = SortedKeyList(range(10000), key=lambda val: val % 10)
    slt._check()

    values = sorted(range(10000), key=lambda val: (val % 10, val))
    assert slt == values
    assert all(val in slt for val in range(10000))
Beispiel #27
0
    def solve(self, initial_state: State, heuristic: callable) -> State:

        def smart_heuristic(state: State):
            value = len(state.board_history) / 10 + heuristic(state)
            return value

        state_list = SortedKeyList(key=smart_heuristic)

        state_list.add(initial_state)
        visited = dict()

        while len(state_list) > 0:
            curr: State = state_list.pop(0)
            key = list_to_string(curr.current_board.content)

            visited[key] = True

            if curr.current_board.content == sorted(curr.current_board.content):
                return curr

            for child in curr.next_states():
                new_key = list_to_string(child.current_board.content)

                if not visited.get(new_key, False):
                    while len(state_list) > 100:
                        state_list.pop(-1)

                    state_list.add(child)
        return None
Beispiel #28
0
def test_getitem_slice():
    random.seed(0)
    slt = SortedKeyList(key=modulo)
    slt._reset(17)

    lst = list()

    for rpt in range(100):
        val = random.random()
        slt.add(val)
        lst.append(val)

    lst.sort(key=modulo)

    assert all(slt[start:] == lst[start:] for start in [-75, -25, 0, 25, 75])

    assert all(slt[:stop] == lst[:stop] for stop in [-75, -25, 0, 25, 75])

    assert all(slt[::step] == lst[::step] for step in [-5, -1, 1, 5])

    assert all(slt[start:stop] == lst[start:stop]
               for start in [-75, -25, 0, 25, 75]
               for stop in [-75, -25, 0, 25, 75])

    assert all(slt[:stop:step] == lst[:stop:step]
               for stop in [-75, -25, 0, 25, 75] for step in [-5, -1, 1, 5])

    assert all(slt[start::step] == lst[start::step]
               for start in [-75, -25, 0, 25, 75] for step in [-5, -1, 1, 5])

    assert all(slt[start:stop:step] == lst[start:stop:step]
               for start in [-75, -25, 0, 25, 75]
               for stop in [-75, -25, 0, 25, 75] for step in [-5, -1, 1, 5])
Beispiel #29
0
def test_add():
    random.seed(0)
    slt = SortedKeyList(key=modulo)
    for val in range(1000):
        slt.add(val)
    slt._check()

    slt = SortedKeyList(key=modulo)
    for val in range(1000, 0, -1):
        slt.add(val)
    slt._check()

    slt = SortedKeyList(key=modulo)
    for val in range(1000):
        slt.add(random.random())
    slt._check()
def test_islice():
    return
    slt = SortedKeyList(key=negate)
    slt._reset(7)

    assert [] == list(slt.islice())

    values = sorted(range(53), key=negate)
    slt.update(values)

    for start in range(53):
        for stop in range(53):
            assert list(slt.islice(start, stop)) == values[start:stop]

    for start in range(53):
        for stop in range(53):
            assert list(slt.islice(start, stop,
                                   reverse=True)) == values[start:stop][::-1]

    for start in range(53):
        assert list(slt.islice(start=start)) == values[start:]
        assert list(slt.islice(start=start,
                               reverse=True)) == values[start:][::-1]

    for stop in range(53):
        assert list(slt.islice(stop=stop)) == values[:stop]
        assert list(slt.islice(stop=stop, reverse=True)) == values[:stop][::-1]