Beispiel #1
0
def test_remove_valueerror2():
    slt = SortedListWithKey(range(100), key=negate)
    slt._reset(10)
    slt.remove(100)
Beispiel #2
0
    def sample_until_n_accepted(
        self,
        n,
        simulate_one,
        t,
        *,
        max_eval=np.inf,
        all_accepted=False,
        ana_vars=None,
    ):
        # For default pickling
        if self.default_pickle:
            self.simulate_accept_one = pickle.dumps(simulate_one)
            full_submit_function = self.full_submit_function_pickle
        else:
            # For advanced pickling, e.g. cloudpickle
            def full_submit_function(job_id):
                result_batch = []
                for j in range(self.batch_size):
                    eval_result = simulate_one()
                    eval_accept = eval_result.accepted
                    result_batch.append((eval_result, eval_accept, job_id[j]))
                return result_batch

        num_accepted_total = 0
        num_accepted_sequential = 0
        next_job_id = 0
        running_jobs = []
        unprocessed_results = SortedListWithKey(key=lambda x: x[0])
        all_results = SortedListWithKey(key=lambda x: x[0])
        next_valid_index = -1

        # Main Loop, leave once we have enough material
        while True:
            # Gather finished jobs
            # make sure to track and update both
            # total accepted and sequentially
            # accepted jobs
            for curJob in running_jobs:
                if curJob.done():
                    remote_batch = curJob.result()
                    running_jobs.remove(curJob)
                    for i in range(self.batch_size):
                        remote_evaluated = remote_batch[i]
                        remote_result = remote_evaluated[0]
                        remote_accept = remote_evaluated[1]
                        remote_jobid = remote_evaluated[2]
                        # print("Received result on job ", remote_jobid)
                        unprocessed_results.add(
                            (remote_jobid, remote_accept, remote_result)
                        )
                        if remote_accept:
                            num_accepted_total += 1

            if len(unprocessed_results) > 0:
                next_index = unprocessed_results[0][0]
            else:
                next_index = np.nan
            while next_index == next_valid_index + 1:
                seq_evaluated = unprocessed_results.pop(0)
                # add to all_results
                all_results.add((seq_evaluated[0], seq_evaluated[2]))
                # update accepted counter
                if seq_evaluated[1]:
                    num_accepted_sequential += 1
                next_valid_index += 1
                if len(unprocessed_results) > 0:
                    next_index = unprocessed_results[0][0]
                else:
                    next_index = np.nan

            # If num_accepted >= n
            # return the first n accepted results
            if num_accepted_sequential >= n:
                break

            # Update information on scheduler state
            # Only submit more jobs if:
            # Number of jobs open < max_jobs
            # Number of jobs open < self.scheduler_workers_running *
            # worker_load_factor
            # num_accepted_total < jobs required
            if (
                (len(running_jobs) < self.client_max_jobs)
                and (len(running_jobs) < self.client_cores())
                and (num_accepted_total < n)
            ):
                for _ in range(
                    0,
                    np.minimum(
                        self.client_max_jobs, self.client_cores()
                    ).astype(int)
                    - len(running_jobs),
                ):
                    job_id_batch = []
                    for _ in range(self.batch_size):
                        job_id_batch.append(next_job_id)
                        next_job_id += 1

                    running_jobs.append(
                        self.my_client.submit(
                            full_submit_function, job_id_batch
                        )
                    )

        # cancel all unfinished jobs
        for curJob in running_jobs:
            curJob.cancel()

        # create 1 to-be-returned sample from all results
        sample = self._create_empty_sample()
        counter_accepted = 0
        self.nr_evaluations_ = 0
        while counter_accepted < n:
            cur_res = all_results.pop(0)
            particle = cur_res[1]
            sample.append(particle)
            if particle.accepted:
                counter_accepted += 1
            # n_eval is latest job_id + 1
            self.nr_evaluations_ = max(self.nr_evaluations_, cur_res[0] + 1)

        return sample
def test_remove_valueerror1():
    slt = SortedListWithKey(key=modulo)
    slt.remove(0)
Beispiel #4
0
def test_index_valueerror6():
    slt = SortedListWithKey(range(10), key=negate)
    slt._reset(4)
    slt.index(6, 5)
Beispiel #5
0
def test_repr():
    this = SortedListWithKey(range(10), key=negate)
    this._reset(4)
    assert repr(this).startswith(
        'SortedListWithKey([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], key=<function negate at '
    )
Beispiel #6
0
def test_insert_valueerror4():
    slt = SortedListWithKey(range(10), key=negate)
    slt._reset(4)
    slt.insert(5, 7)
Beispiel #7
0
def test_index_valueerror4():
    slt = SortedListWithKey([0] * 10, key=negate)
    slt._reset(4)
    slt.index(1)
Beispiel #8
0
def test_setitem_slice_bad():
    slt = SortedListWithKey(range(100), key=negate)
    slt._reset(17)
    slt[:10] = list(reversed(range(10)))
Beispiel #9
0
def test_setitem_slice_bad1():
    slt = SortedListWithKey(range(100), key=negate)
    slt._reset(17)
    slt[10:20] = range(20, 30)
Beispiel #10
0
def test_getitem_indexerror1():
    slt = SortedListWithKey(key=negate)
    slt[5]
Beispiel #11
0
def test_getitem_indexerror3():
    slt = SortedListWithKey(range(100), key=negate)
    slt[-101]
Beispiel #12
0
def test_getitem_slicezero():
    slt = SortedListWithKey(range(100), key=negate)
    slt._reset(17)
    slt[::0]
Beispiel #13
0
def test_identity():
    slt = SortedListWithKey(range(100))
    slt._reset(7)
    slt._check()
Beispiel #14
0
def test_remove_valueerror3():
    slt = SortedListWithKey([1, 2, 2, 2, 3, 3, 5], key=negate)
    slt.remove(4)
Beispiel #15
0
def test_extend_valueerror1():
    slt = SortedListWithKey(key=negate)
    slt.extend([1, 2, 3, 5, 4, 6])
Beispiel #16
0
def test_setitem_slice_bad2():
    slt = SortedListWithKey(range(100), key=negate)
    slt._reset(17)
    slt[20:30] = range(10, 20)
Beispiel #17
0
def test_extend_valueerror2():
    slt = SortedListWithKey(range(20), key=negate)
    slt._reset(4)
    slt.extend([5, 4, 3, 2, 1])
Beispiel #18
0
def test_setitem_extended_slice_bad2():
    slt = SortedListWithKey(range(100), key=negate)
    slt._reset(17)
    slt[40:90:5] = list(range(10))
Beispiel #19
0
def test_pop_indexerror2():
    slt = SortedListWithKey(range(10), key=negate)
    slt._reset(4)
    slt.pop(10)
Beispiel #20
0
def test_setitem_valueerror2():
    slt = SortedListWithKey(range(10), key=negate)
    slt[0] = 0
Beispiel #21
0
def test_index_valueerror5():
    slt = SortedListWithKey(key=negate)
    slt.index(1)
Beispiel #22
0
def test_iter():
    slt = SortedListWithKey(range(10000), key=negate)
    itr = iter(slt)
    assert all(tup[0] == tup[1] for tup in zip(range(9999, -1, -1), itr))
Beispiel #23
0
def test_imul():
    this = SortedListWithKey(range(10), key=negate)
    this._reset(4)
    this *= 5
    this._check()
    assert this == sorted(list(range(10)) * 5, reverse=True)
Beispiel #24
0
def test_reversed():
    slt = SortedListWithKey(range(10000), key=negate)
    rev = reversed(slt)
    assert all(tup[0] == tup[1] for tup in zip(range(10000), rev))
Beispiel #25
0
def test_check():
    slt = SortedListWithKey(range(10), key=negate)
    slt._reset(4)
    slt._len = 5
    slt._check()
Beispiel #26
0
def test_len():
    slt = SortedListWithKey(key=negate)

    for val in range(10000):
        slt.add(val)
        assert len(slt) == (val + 1)
Beispiel #27
0
def find_intersections(lines):
    def poi_sort_x(poi, other):
        if poi.x == other.x:
            if poi.point_type == _PointOfInterest.PointType.VERTICAL and \
                    other.point_type == _PointOfInterest.PointType.HORIZONTAL_LEFT:
                return 1
            elif poi.point_type == _PointOfInterest.PointType.HORIZONTAL_LEFT and \
                    other.point_type == _PointOfInterest.PointType.VERTICAL:
                return -1
            elif poi.point_type == _PointOfInterest.PointType.VERTICAL and \
                    other.point_type == _PointOfInterest.PointType.HORIZONTAL_RIGHT:
                return -1
            elif poi.point_type == _PointOfInterest.PointType.HORIZONTAL_RIGHT and \
                    other.point_type == _PointOfInterest.PointType.VERTICAL:
                return 1
            elif poi.point_type == _PointOfInterest.PointType.HORIZONTAL_RIGHT and \
                    other.point_type == _PointOfInterest.PointType.HORIZONTAL_LEFT:
                return 1
            elif poi.point_type == _PointOfInterest.PointType.HORIZONTAL_LEFT and \
                    other.point_type == _PointOfInterest.PointType.HORIZONTAL_RIGHT:
                return -1
            else:
                return 0
        else:
            if poi.x > other.x:
                return 1
            else:
                return -1

    vertical_lines = []
    horizontal_lines = []

    for line in lines:
        if line.is_vertical():
            vertical_lines.append(line)
        elif line.is_horizontal():
            if line.start.x > line.end.x:
                horizontal_lines.append(Line(start=line.end, end=line.start))
            else:
                horizontal_lines.append(line)
        else:
            raise ValueError(
                "Find intersections supports horizontal and vertical lines")

    points_of_interest = []

    for h_line in horizontal_lines:
        points_of_interest.append(
            _PointOfInterest(_PointOfInterest.PointType.HORIZONTAL_LEFT,
                             h_line.start.x, h_line))
        points_of_interest.append(
            _PointOfInterest(_PointOfInterest.PointType.HORIZONTAL_RIGHT,
                             h_line.end.x, h_line))

    for v_line in vertical_lines:
        points_of_interest.append(
            _PointOfInterest(_PointOfInterest.PointType.VERTICAL,
                             v_line.start.x, v_line))

    points_of_interest.sort(key=functools.cmp_to_key(poi_sort_x))

    line_sweep_hor_list = SortedListWithKey(key=lambda l: l.start.y)
    intersections = []
    for poi in points_of_interest:
        if poi.point_type == _PointOfInterest.PointType.HORIZONTAL_LEFT:
            line_sweep_hor_list.add(poi.line)
        elif poi.point_type == _PointOfInterest.PointType.HORIZONTAL_RIGHT:
            line_sweep_hor_list.remove(poi.line)
        else:
            for hor_line in line_sweep_hor_list.irange_key(
                    min_key=poi.line.start.y, max_key=poi.line.end.y):
                intersections.append(
                    Intersection(Point(poi.line.start.x, hor_line.start.y),
                                 hor_line, poi.line))

    return intersections
Beispiel #28
0
def test_append_valueerror():
    slt = SortedListWithKey(range(100), key=negate)
    slt.append(5)
def test_remove_valueerror2():
    slt = SortedListWithKey(range(100), load=10, key=modulo)
    slt.remove(100)
Beispiel #30
0
def test_remove_valueerror1():
    slt = SortedListWithKey(key=negate)
    slt.remove(0)