def test_servo_values():
    p = MockPWMPin(1)
    with Servo(p) as device:
        device.min()
        assert device.is_active
        assert device.value == -1
        assert isclose(p.state, 0.05)
        device.max()
        assert device.is_active
        assert device.value == 1
        assert isclose(p.state, 0.1)
        device.mid()
        assert device.is_active
        assert device.value == 0.0
        assert isclose(p.state, 0.075)
        device.value = 0.5
        assert device.is_active
        assert device.value == 0.5
        assert isclose(p.state, 0.0875)
        device.detach()
        assert not device.is_active
        assert device.value is None
        device.value = 0
        assert device.value == 0
        device.value = None
        assert device.value is None
Example #2
0
 def on_line(self, p, q, tol=1.0e-6):
     if not math.isclose(p.x, q.x, tol):
         a = (q.y - p.y)/(q.x - p.x)
         b = p.y - a*p.x
         return math.isclose(self.y, a*self.x + b, tol)
     else:
         return math.isclose(self.x, p.x, tol)
Example #3
0
def test_convert_to_square_resolution(renderer, spoof_tesseract_cache,
                                      resources, outpdf):
    from math import isclose

    # Confirm input image is non-square resolution
    in_pageinfo = PdfInfo(resources / 'aspect.pdf')
    assert in_pageinfo[0].xres != in_pageinfo[0].yres

    # --force-ocr requires means forced conversion to square resolution
    check_ocrmypdf(
        resources / 'aspect.pdf', outpdf,
        '--force-ocr',
        '--pdf-renderer', renderer, env=spoof_tesseract_cache)

    out_pageinfo = PdfInfo(outpdf)

    in_p0, out_p0 = in_pageinfo[0], out_pageinfo[0]

    # Resolution show now be equal
    assert out_p0.xres == out_p0.yres

    # Page size should match input page size
    assert isclose(in_p0.width_inches,
                   out_p0.width_inches)
    assert isclose(in_p0.height_inches,
                   out_p0.height_inches)

    # Because we rasterized the page to produce a new image, it should occupy
    # the entire page
    out_im_w = out_p0.images[0]['width'] / out_p0.images[0]['dpi_w']
    out_im_h = out_p0.images[0]['height'] / out_p0.images[0]['dpi_h']
    assert isclose(out_p0.width_inches, out_im_w)
    assert isclose(out_p0.height_inches, out_im_h)
def test_output_pwm_pulse_background():
    pin = MockPWMPin(2)
    with PWMOutputDevice(pin) as device:
        start = time()
        device.pulse(0.2, 0.2, n=2)
        assert isclose(time() - start, 0, abs_tol=0.05)
        device._blink_thread.join()
        assert isclose(time() - start, 0.8, abs_tol=0.05)
        pin.assert_states_and_times([
            (0.0, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            ])
def test_rgbled_pulse_background():
    r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
    with RGBLED(r, g, b) as device:
        start = time()
        device.pulse(0.2, 0.2, n=2)
        assert isclose(time() - start, 0, abs_tol=0.05)
        device._blink_thread.join()
        assert isclose(time() - start, 0.8, abs_tol=0.05)
        expected = [
            (0.0, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            ]
        r.assert_states_and_times(expected)
        g.assert_states_and_times(expected)
        b.assert_states_and_times(expected)
Example #6
0
    def find_excessive_indexes(self):
        """
        Mark indexes of nodes-duplicates
        Return list of pairs of them and their originals
        """
        excessive_indexes = list()
        for i, (curr_x, curr_y, curr_index) in enumerate(self.nodes):
            # Seek duplicates of current nodes (by coordinates),
            #   mark down all pairs of duplicate-original.
            # Because nodes are sorted, possible duplicates are
            #   immediately after this in 'self.nodes' list.
            #   Also, because of sorting we need to adjust indexes
            #   via 'self.rearanged_nodes'
            for j in range(i+1, len(self.nodes)):
                next_x, next_y, next_index = self.nodes[j]
                if not math.isclose(curr_y, next_y):
                    break

                if math.isclose(curr_x, next_x):
                    c_i_sorted = self.rearanged_nodes[curr_index]
                    already_excessive = False
                    for (already_removed_index, _) in excessive_indexes:
                        if c_i_sorted == already_removed_index:
                            already_excessive = True
                            break

                    if not already_excessive:
                        n_i_sorted = self.rearanged_nodes[next_index]
                        excessive_indexes.append((n_i_sorted, c_i_sorted))

        return excessive_indexes, len(self.nodes) - len(excessive_indexes)
def test_servo_values():
    p = Device.pin_factory.pin(1)
    with Servo(1) as servo:
        servo.min()
        assert servo.is_active
        assert servo.value == -1
        assert isclose(p.state, 0.05)
        servo.max()
        assert servo.is_active
        assert servo.value == 1
        assert isclose(p.state, 0.1)
        servo.mid()
        assert servo.is_active
        assert servo.value == 0.0
        assert isclose(p.state, 0.075)
        servo.value = 0.5
        assert servo.is_active
        assert servo.value == 0.5
        assert isclose(p.state, 0.0875)
        servo.detach()
        assert not servo.is_active
        assert servo.value is None
        servo.value = 0
        assert servo.value == 0
        servo.value = None
        assert servo.value is None
Example #8
0
def test_ngram_models():
    flatland = DataFile("aima-data/EN-text/flatland.txt").read()
    wordseq = words(flatland)
    P1 = UnigramTextModel(wordseq)
    P2 = NgramTextModel(2, wordseq)
    P3 = NgramTextModel(3, wordseq)

    ## The most frequent entries in each model
    assert P1.top(10) == [(2081, 'the'), (1479, 'of'), (1021, 'and'), (1008, 'to'), (850, 'a'), 
                            (722, 'i'), (640, 'in'), (478, 'that'), (399, 'is'), (348, 'you')]

    assert P2.top(10) == [(368, ('of', 'the')), (152, ('to', 'the')), (152, ('in', 'the')), (86, ('of', 'a')), 
                            (80, ('it', 'is'   )), (71, ('by', 'the' )), (68, ('for', 'the'  )),
                            (68, ('and', 'the' )), (62, ('on', 'the' )), (60, ('to', 'be'))]

    assert P3.top(10) == [(30, ('a', 'straight', 'line')), (19, ('of', 'three', 'dimensions')), 
                            (16, ('the', 'sense', 'of'         )), (13, ('by', 'the', 'sense'   )),
                            (13, ('as', 'well', 'as'           )), (12, ('of', 'the', 'circles' )),
                            (12, ('of', 'sight', 'recognition' )), (11, ('the', 'number', 'of'  )),
                            (11, ('that', 'i', 'had'           )), (11, ('so', 'as', 'to'))]


    assert isclose(P1['the'], 0.0611)

    assert isclose(P2['of', 'the'], 0.0108)

    assert isclose(P3['', '', 'but'], 0.0)
    assert isclose(P3['', '', 'but'], 0.0)
    assert isclose(P3['so', 'as', 'to'], 0.000323)

    assert not P2.cond_prob['went',].dictionary

    assert P3.cond_prob['in','order'].dictionary == {'to': 6}
Example #9
0
def ramping_values(period=360):
    """
    Provides an infinite source of values representing a triangle wave (from 0
    to 1 and back again) which repeats every *period* values. For example, to
    pulse an LED once a second::

        from gpiozero import PWMLED
        from gpiozero.tools import ramping_values
        from signal import pause

        red = PWMLED(2)

        red.source_delay = 0.01
        red.source = ramping_values(100)

        pause()

    If you require a wider range than 0 to 1, see :func:`scaled`.
    """
    step = 2 / period
    value = 0
    while True:
        yield value
        value += step
        if isclose(value, 1, abs_tol=1e-9):
            value = 1
            step *= -1
        elif isclose(value, 0, abs_tol=1e-9):
            value = 0
            step *= -1
        elif value > 1 or value < 0:
            step *= -1
            value += step
Example #10
0
def test_output_pwm_fade_background():
    pin = Device.pin_factory.pin(4)
    with PWMOutputDevice(4) as device:
        start = time()
        device.blink(0, 0, 0.2, 0.2, n=2)
        assert isclose(time() - start, 0, abs_tol=0.05)
        device._blink_thread.join()
        assert isclose(time() - start, 0.8, abs_tol=0.05)
        pin.assert_states_and_times([
            (0.0, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            ])
    def test_jpg(self, directory, file_name, tolerance):
        source_file_path = os.path.join(BASE, 'files', file_name)
        output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
        format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
        exporter = ImageExporter(source_file_path=source_file_path, ext='.jpg',
                                 output_file_path=output_file_path, format=format,
                                 metadata={})

        assert not os.path.exists(output_file_path)

        exporter.export()

        assert os.path.exists(output_file_path)

        output_image = Image.open(output_file_path)
        source_image = Image.open(source_file_path)
        source_pixels = list(source_image.getdata())
        output_pixels = list(output_image.getdata())

        assert source_image.size == output_image.size
        assert output_image.mode == 'RGB'
        assert output_image.palette == source_image.palette
        assert output_image.format.lower() == settings.EXPORT_TYPE

        for i in range(100):
            # PIL conversions change some pixels, but first 100 are the same on this one
            assert isclose(source_pixels[i][0], output_pixels[i][0], abs_tol=tolerance)
            assert isclose(source_pixels[i][1], output_pixels[i][1], abs_tol=tolerance)
            assert isclose(source_pixels[i][2], output_pixels[i][2], abs_tol=tolerance)
Example #12
0
def test_dynamic_trapz():
    same_grid = dynamic_trapz(a_curve, 1, 6, 100)
    assert isclose(7.4512822710374, same_grid)
    big_grids = dynamic_trapz(a_curve, 1, 6, 5)
    assert isclose(big_grids, 7.2642983586919)
    small_grids = dynamic_trapz(a_curve, 1, 6, 500)
    assert isclose(small_grids, 7.4533539021642)
    def _determine_normal_force(element):
        dx = element.point_1.x - element.point_2.x
        if math.isclose(dx, 0):  # element is vertical
            if element.point_1.z < element.point_2.z:  # point 1 is bottom
                element.N = -element.node_1.Fz  # compression and tension in opposite direction
            else:
                element.N = element.node_1.Fz  # compression and tension in opposite direction

        elif math.isclose(element.alpha, 0):  # element is horizontal
            if element.point_1.x < element.point_2.x:  # point 1 is left
                element.N = -element.node_1.Fx  # compression and tension in good direction
            else:
                element.N = element.node_1.Fx  # compression and tension in opposite direction

        else:
            if math.cos(element.alpha) > 0:
                if element.point_1.x < element.point_2.x:  # point 1 is left
                    if element.node_1.Fx > 0:  # compression in element
                        element.N = -math.sqrt(element.node_1.Fx ** 2 + element.node_1.Fz ** 2)
                    else:
                        element.N = math.sqrt(element.node_1.Fx ** 2 + element.node_1.Fz ** 2)
                else:  # point 1 is right
                    if element.node_1.Fx < 0:  # compression in element
                        element.N = -math.sqrt(element.node_1.Fx ** 2 + element.node_1.Fz ** 2)
                    else:
                        element.N = math.sqrt(element.node_1.Fx ** 2 + element.node_1.Fz ** 2)
Example #14
0
 def test_query(self):
     l1, u1 = queryweightedmodel(self.q_access_alice_f1, self.ws, self.As,
             self.IC, wf=self.wf)
     l2, u2 = queryweightedmodel(self.q_access_alice_f2, self.ws, self.As,
             self.IC, wf=self.wf)
     l3, u3 = queryweightedmodel(self.q_access_bob_f1, self.ws, self.As,
             self.IC, wf=self.wf)
     l4, u4 = queryweightedmodel(self.q_access_bob_f2, self.ws, self.As,
             self.IC, wf=self.wf)
     l5, u5 = queryweightedmodel(self.q_blacklisted_alice, self.ws, self.As,
             self.IC, wf=self.wf)
     l6, u6 = queryweightedmodel(self.q_blacklisted_bob, self.ws, self.As,
             self.IC, wf=self.wf)
     self.assertTrue(math.isclose(l1, 0.44, abs_tol=1e-2))
     self.assertTrue(math.isclose(u1, 0.44, abs_tol=1e-2))
     self.assertTrue(math.isclose(l2, 0.6, abs_tol=1e-1))
     self.assertTrue(math.isclose(u2, 0.6, abs_tol=1e-1))
     # too much deviation: 0.14 to 0.04
     # self.assertTrue(math.isclose(l3, 0.14, abs_tol=1e-2))
     # self.assertTrue(math.isclose(u3, 0.14, abs_tol=1e-2))
     self.assertTrue(math.isclose(l4, 0.4, abs_tol=1e-1))
     self.assertTrue(math.isclose(u4, 0.4, abs_tol=1e-1))
     self.assertTrue(math.isclose(l5, 0.005, abs_tol=5e-3))
     self.assertTrue(math.isclose(u5, 0.005, abs_tol=5e-3))
     self.assertTrue(math.isclose(l6, 0.017, abs_tol=1e-2))
     self.assertTrue(math.isclose(u6, 0.017, abs_tol=1e-2))
def compare(to_compare, path, message, processes):
    the_path = (path + '/' + to_compare)
    if platform.system() == 'Windows':  # windows compatibility
        the_path = the_path[the_path.find('/') + 1:]
    with open(to_compare, 'r') as generatedf:
        generated = {}
        for row in csv.DictReader(generatedf):
            try:
                generated[(row['round'], row['name'])] = row
            except KeyError:
                generated[row['round']] = row
    with open(the_path, 'r') as orginialf:
        orginial = {}
        for row in csv.DictReader(orginialf):
            try:
                orginial[(row['round'], row['name'])] = row
            except KeyError:
                orginial[row['round']] = row
    for row in generated:
        for key in generated[row]:
            if key != 'index':
                if generated[row][key] != orginial[row][key]:
                    assert isclose(float(generated[row][key]), float(orginial[row][key])), (
                        to_compare, key, generated[row][key], orginial[row][key])
    for row in orginial:
        for key in orginial[row]:
            if key != 'index':
                if generated[row][key] != orginial[row][key]:
                    assert isclose(float(generated[row][key]), float(orginial[row][key])), (
                        to_compare, key, generated[row][key], orginial[row][key])
Example #16
0
def test_single_page_image(outdir):
    filename = outdir / 'image-mono.pdf'

    im_tmp = outdir / 'tmp.png'
    im = Image.new('1', (8, 8), 0)
    for n in range(8):
        im.putpixel((n, n), 1)
    im.save(str(im_tmp), format='PNG')

    imgsize = ((img2pdf.ImgSize.dpi, 8), (img2pdf.ImgSize.dpi, 8))
    layout_fun = img2pdf.get_layout_fun(None, imgsize, None, None, None)

    im_bytes = im_tmp.read_bytes()
    pdf_bytes = img2pdf.convert(
            im_bytes, producer="img2pdf", with_pdfrw=False,
            layout_fun=layout_fun)
    filename.write_bytes(pdf_bytes)

    info = pdfinfo.PdfInfo(filename)

    assert len(info) == 1
    page = info[0]

    assert not page.has_text
    assert len(page.images) == 1

    pdfimage = page.images[0]
    assert pdfimage.width == 8
    assert pdfimage.color == Colorspace.gray

    # DPI in a 1"x1" is the image width
    assert isclose(pdfimage.xres, 8)
    assert isclose(pdfimage.yres, 8)
Example #17
0
 def set_fp_gpio_voltage(self, value):
     """ Set Front Panel GPIO voltage (in volts)
     3V3 2V5 | Voltage
     -----------------
      0   0  | 1.8 V
      0   1  | 2.5 V
      1   0  | 3.3 V
     Arguments:
         value : 3.3
     """
     assert any([math.isclose(value, nn, abs_tol=0.1) for nn in (3.3,)]),\
         "FP GPIO currently only supports 3.3V"
     if math.isclose(value, 1.8, abs_tol=0.1):
         voltage_reg = 0
     elif math.isclose(value, 2.5, abs_tol=0.1):
         voltage_reg = 1
     elif math.isclose(value, 3.3, abs_tol=0.1):
         voltage_reg = 2
     mask = 0xFFFFFFFF ^ ((0b1 << self.MB_GPIO_CTRL_EN_3V3) | \
                          (0b1 << self.MB_GPIO_CTRL_EN_2V5))
     with self.regs:
         reg_val = self.peek32(self.MB_GPIO_CTRL) & mask
         reg_val = reg_val | (voltage_reg << self.MB_GPIO_CTRL_EN_2V5)
         self.log.trace("Writing MB_GPIO_CTRL to 0x{:08X}".format(reg_val))
         return self.poke32(self.MB_GPIO_CTRL, reg_val)
Example #18
0
def test_cos_values():
    for e, v in zip([1, -1], cos_values(2)):
        assert -1 <= v <= 1
        assert isclose(e, v, abs_tol=1e-9)
    for e, v in zip([1, 0, -1, 0], cos_values(4)):
        assert -1 <= v <= 1
        assert isclose(e, v, abs_tol=1e-9)
    for e, v in zip([1, 2**0.5/2, 0, -2**0.5/2, -1, -2**0.5/2, 0, 2**0.5/2], cos_values(8)):
        assert -1 <= v <= 1
        assert isclose(e, v, abs_tol=1e-9)
    firstval = None
    for i, v in zip(range(1000), cos_values()):
        assert -1 <= v <= 1
        assert isclose(v, cos(radians(i)), abs_tol=1e-9)
        if i == 0:
            firstval = v
        else:
            if i % 360 == 0:
                assert v == firstval
    for period in (360, 100):
        firstval = None
        for i, v in zip(range(1000), cos_values(period)):
            assert -1 <= v <= 1
            if i == 0:
                firstval = v
            else:
                if i % period == 0:
                    assert v == firstval
Example #19
0
    def request(self, layer, crs, style, dotiming=False, checkimg=False,
                saveimg=False, profiler=None):
        """Requests WMS image from server and returns image"""
        env = self.base_env.copy()
        query = self.base_query_map.copy()
        bounds = list(self.wms[layer].boundingBoxWGS84)
        if crs != 'EPSG:4326':
            delta = 1.0 # We move 1 degrees away from the poles
            if math.isclose(bounds[1], -90.0):
                bounds[1] += delta
            if math.isclose(bounds[3], 90.0):
                bounds[3] -= delta
        bbox = wms_utils.get_bounding_box(bounds, crs)
        if bbox != '':
            query['BBOX'] = bbox
        query['CRS'] = crs
    
        t = self.wms[layer].timepositions
        if t is not None:
            t = [tv.strip() for tv in t]
            query['TIME'] = t[int(len(t)/2)]

        elevations = self.wms[layer].elevations
        if elevations is not None:
            elevations = [ev.strip() for ev in elevations]
            query['ELEVATION'] = elevations[int(len(elevations)/2)]

        query['LAYERS'] = layer
        query['STYLES'] = style

        t = None
        if dotiming:
            t1 = time.clock()
        if profiler is not None:
            profiler.enable()
        response = self.get(params=query,
                            extra_environ=self.base_env, status=200)
        result = response.body[:]

        if profiler is not None:
            profiler.disable()
        if dotiming:
            t2 = time.clock()
            t = (t2-t1)*1000
        if saveimg:
            with open('tmp/png_%s_%s.png' % (layer, crs), 'wb') as f:
                f.write(result)
        if checkimg:
            is_blank = wms_utils.check_blank(result)
            if is_blank:
                # Construct URL for easy testing in browser
                url = self.path_info + '?' + urllib.parse.urlencode(query)
                msg = "Error: Blank image returned for layer=%s; crs=%s" % (layer, crs)
                msg += "\nQuery: " + url
                raise SystemExit(msg)
                """
            else:
                print('Image data seems OK')
                """
        return result, t
Example #20
0
def get_alpha_sim(bug_count, t):

    alpha_min = 1.2
    alpha_max = 4
    alpha = alpha_min
    op_alpha = None
    step = 0.01
    min_diff = 1000000

    while alpha < alpha_max:
        results = []
        for i in range(0, 10):
            bugs, bugs_accu, seq = expected_find_sim(None, t, alpha)
            results.append(len(bugs))
        _diff = bug_count - np.mean(results)
        diff = abs(_diff)
        if diff < min_diff:
            min_diff = diff
            op_alpha = alpha

        alpha += step

    print(op_alpha)
    if math.isclose(op_alpha, alpha_min, rel_tol=1e-6) or math.isclose(op_alpha, alpha_max, rel_tol=1e-6):
        print("WARNING: the range of alpha might not be enough for the fitting.")
    return op_alpha
def test_model_load_mem_leak():
    "testing memory leak on load"
    pytest.xfail("memory leak in learn.load()")

    path = untar_data(URLs.MNIST_TINY)
    data = ImageDataBunch.from_folder(path, ds_tfms=([], []), bs=2)
    learn = cnn_learner(data, models.resnet18, metrics=accuracy)
    this_tests(learn.load)
    gpu_mem_reclaim() # baseline
    used_before = gpu_mem_get_used()

    name = 'mnist-tiny-test-load-mem-leak'
    model_path = learn.save(name, return_path=True)
    _ = learn.load(name)
    if os.path.exists(model_path): os.remove(model_path)
    used_after = gpu_mem_get_used()

    # models.resnet18 loaded in GPU RAM is about 50MB
    # calling learn.load() of a saved and then instantly re-loaded model shouldn't require more GPU RAM
    # XXX: currently w/o running gc.collect() this temporarily leaks memory and causes fragmentation - the fragmentation can't be tested from here, but it'll get automatically fixed once load is fixed. load() must unload first the previous model, gc.collect() and only then load the new one onto cuda.
    assert isclose(used_before, used_after, abs_tol=6), f"load() and used GPU RAM: before load(): {used_before}, after: {used_after}"

    # this shows how it should have been
    gc.collect()
    gpu_cache_clear()
    used_after_reclaimed = gpu_mem_get_used()
    # XXX: not sure where 6MB get lost still but for now it's a small leak - need to test with a bigger model
    assert isclose(used_before, used_after_reclaimed, abs_tol=6),f"load() and used GPU RAM: before load(): {used_before}, after: {used_after}, after gc.collect() {used_after_reclaimed} used"
Example #22
0
def plot_reference_data(ax, atomic_number, ion_stage, estimators_celltimestep, dfpopthision, args, annotatelines):
    nne, Te, TR, W = [estimators_celltimestep[s] for s in ['nne', 'Te', 'TR', 'W']]
    # comparison to Chianti file
    elsym = at.elsymbols[atomic_number]
    elsymlower = elsym.lower()
    if Path('data', f'{elsymlower}_{ion_stage}-levelmap.txt').exists():
        # ax.set_ylim(bottom=2e-3)
        # ax.set_ylim(top=4)
        levelmapfile = Path('data', f'{elsymlower}_{ion_stage}-levelmap.txt').open('r')
        levelnumofconfigterm = {}
        for line in levelmapfile:
            row = line.split()
            levelnumofconfigterm[(row[0], row[1])] = int(row[2]) - 1

        # ax.set_ylim(bottom=5e-4)
        for depfilepath in sorted(Path('data').rglob(f'chianti_{elsym}_{ion_stage}_*.txt')):
            with depfilepath.open('r') as depfile:
                firstline = depfile.readline()
                file_nne = float(firstline[firstline.find('ne = ') + 5:].split(',')[0])
                file_Te = float(firstline[firstline.find('Te = ') + 5:].split(',')[0])
                file_TR = float(firstline[firstline.find('TR = ') + 5:].split(',')[0])
                file_W = float(firstline[firstline.find('W = ') + 5:].split(',')[0])
                # print(depfilepath, file_nne, nne, file_Te, Te, file_TR, TR, file_W, W)
                if (math.isclose(file_nne, nne, rel_tol=0.01) and
                        math.isclose(file_Te, Te, abs_tol=10)):
                    if file_W > 0:
                        continue
                        bbstr = ' with dilute blackbody'
                        color = 'C2'
                        marker = '+'
                    else:
                        bbstr = ''
                        color = 'C1'
                        marker = '^'

                    print(f'Plotting reference data from {depfilepath},')
                    print(f'nne = {file_nne} (ARTIS {nne}) cm^-3, Te = {file_Te} (ARTIS {Te}) K, '
                          f'TR = {file_TR} (ARTIS {TR}) K, W = {file_W} (ARTIS {W})')
                    levelnums = []
                    depcoeffs = []
                    firstdep = -1
                    for line in depfile:
                        row = line.split()
                        try:
                            levelnum = levelnumofconfigterm[(row[1], row[2])]
                            if levelnum in dfpopthision['level'].values:
                                levelnums.append(levelnum)
                                if firstdep < 0:
                                    firstdep = float(row[0])
                                depcoeffs.append(float(row[0]) / firstdep)
                        except (KeyError, IndexError, ValueError):
                            pass
                    ionstr = at.get_ionstring(atomic_number, ion_stage, spectral=False)
                    ax.plot(levelnums, depcoeffs, linewidth=1.5, color=color,
                            label=f'{ionstr} CHIANTI NLTE{bbstr}', linestyle='None', marker=marker, zorder=-1)

        if annotatelines and atomic_number == 28 and ion_stage == 2:
            annotate_emission_line(ax=ax, y=0.04, upperlevel=6, lowerlevel=0, label=r'$\lambda$7378')
            annotate_emission_line(ax=ax, y=0.15, upperlevel=6, lowerlevel=2, label=r'1.939 $\mu$m')
            annotate_emission_line(ax=ax, y=0.26, upperlevel=7, lowerlevel=1, label=r'$\lambda$7412')
Example #23
0
def perspective_matrix(field_of_view, aspect_ratio, near_plane, far_plane):
    """Return a perspective matrix.

    :param field_of_view: The field_of_view to use.
    :type field_of_view: float

    :param aspect_ratio: The aspect ratio of the projection.
    :type aspect_ratio: float

    :param near_plane: Location of the near plane of the projection.
    :type near_plane: float

    :param far_plane: Location of the far plane of the projection.
    :type far_plane: float

    :returns: A new perspective matrix.
    :rtype: np.ndarray

    :raises AssertionError: When the near plane is zero or the near
         plane is the same as the far plane.

    """
    assert not math.isclose(near_plane, 0), \
        "Near plane cannot be at zero."
    assert not math.isclose(near_plane, far_plane), \
        "Near plane and far plane can't be same"
    f = 1 / math.tan(field_of_view / 2)
    mat = np.identity(4)
    mat[0, 0] = f / aspect_ratio
    mat[1, 1] =  f
    mat[2, 2] = (far_plane + near_plane) / (near_plane - far_plane)
    mat[2, 3] = 2 * far_plane * near_plane / (near_plane - far_plane)
    mat[3, 2] = -1
    mat[3, 3] = 0
    return mat
def calc_coverage_threshold(cov_dict):
    '''
    calculate minimum coverage threshold for each key in cov_dict.
    see end of 'alternative parameterization' section of Negative binomial page
    and scipy negative binomial documentation for details of calculation.
    '''
    threshold_dict = {}
    for g in cov_dict:
        mean = float(cov_dict[g]['mean'])
        var = float(cov_dict[g]['variance'])
        q = (var-mean)/var
        n = mean**2/(var-mean)
        p = 1 - q

        ## assert that I did the math correctly.
        assert(isclose(nbinom.mean(n,p), mean))
        assert(isclose(nbinom.var(n,p), var))

        ## find the integer threshold that includes ~95% of REL606 distribution,
        ## excluding 5% on the left hand side.
        my_threshold = nbinom.ppf(0.05,n,p)
        my_threshold_p = nbinom.cdf(my_threshold,n,p)
        threshold_dict[g] = {'threshold':str(my_threshold),
                             'threshold_p':str(my_threshold_p)}
    return threshold_dict
Example #25
0
def list_to_monolists_concat(lis, key=lambda x: x):
    """
    list_to_monolists and concatenates at stationary key.
    >>> pairs = [(0, 0), (1, 1),
    ...          (1, 2), (0, 3), (0, 4), (-1, 5),
    ...          (-1, 6), (0, 7),
    ...          (0, 8), (10, 9)]
    >>> list_to_monolists(pairs, key=lambda x: x[0])
    ... # doctest: +NORMALIZE_WHITESPACE
    ([[(0, 0), (1, 1)],
      [(1, 2), (0, 3)],
      [(0, 4), (-1, 5)],
      [(-1, 6), (0, 7)],
      [(0, 8), (10, 9)]],
     [1, -1, -1, 1, 10])

    >>> list_to_monolists_concat(pairs, key=lambda x: x[0])
    ... # doctest: +NORMALIZE_WHITESPACE
    ([[(0, 0), (1, 1)],
      [(1, 2), (0, 3), (-1, 5)],
      [(-1, 6), (0, 7)],
      [(0, 8), (10, 9)]],
      [1, -1, 1, 10])
    """
    lists, diffs = list_to_monolists(lis, key)
    for i in range(len(diffs) - 2, -1, -1):  # Second from last --> 0
        key1 = key(lists[i][-1])
        key2 = key(lists[i + 1][0])
        if math.isclose(diffs[i], diffs[i + 1]) and math.isclose(key1, key2):
            del diffs[i + 1]
            del lists[i + 1][0]
            lists[i] += lists[i + 1]
            del lists[i + 1]
    return lists, diffs
Example #26
0
    def get_hkl_indices_from_streamfile(self, crystal_index, peak_x, peak_y):
        """
        This method returns the hkl indices of the predicted bragg peaks of
        the crystal with the given index at the position (peak_x, peak_y)

        Args:
            crystal_index (int): Index of the crystal from which the unit cell
                is returned.
            peak_x (float): The x coordinate of the peak position
            peak_y (float): The y coordinate of the peak position

        Returns:
            list: The hkl indices
        """

        try:
            crystal = self.crystals[crystal_index]
            self.stream_file.seek(crystal.begin_predicted_peaks_pointer)
            while(self.stream_file.tell() != 
                crystal.end_predicted_peaks_pointer):
                line = self.stream_file.readline()
                matches = re.findall(self._float_matching_regex, line)
               
                if(math.isclose(peak_x, float(matches[7]))
                    and math.isclose(peak_y, float(matches[8]))):
                    return [int(matches[0]), int(matches[1]), int(matches[2])]
        except IOError:
            print("Cannot read the peak information from streamfile: ", 
                self.filename)
        return []
Example #27
0
def test_rgbled_pulse_background():
    r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
    with RGBLED(1, 2, 3) as led:
        start = time()
        led.pulse(0.2, 0.2, n=2)
        assert isclose(time() - start, 0, abs_tol=0.05)
        led._blink_thread.join()
        assert isclose(time() - start, 0.8, abs_tol=0.05)
        expected = [
            (0.0, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            (0.04, 0.2),
            (0.04, 0.4),
            (0.04, 0.6),
            (0.04, 0.8),
            (0.04, 1),
            (0.04, 0.8),
            (0.04, 0.6),
            (0.04, 0.4),
            (0.04, 0.2),
            (0.04, 0),
            ]
        r.assert_states_and_times(expected)
        g.assert_states_and_times(expected)
        b.assert_states_and_times(expected)
Example #28
0
    def shear_force(self, factor=None, figsize=None, verbosity=0, scale=1, offset=(0, 0), show=True, gridplot=False):
        self.plot_structure(figsize, 1, scale=scale, offset=offset, gridplot=gridplot)
        if factor is None:
            max_force = max(map(lambda el: np.max(np.abs(el.shear_force)), self.system.element_map.values()))
            factor = det_scaling_factor(max_force, self.max_val_structure)

        for el in self.system.element_map.values():
            if math.isclose(el.node_1.Ty, 0, rel_tol=1e-5, abs_tol=1e-9) and \
                    math.isclose(el.node_2.Ty, 0, rel_tol=1e-5, abs_tol=1e-9) and el.q_load is None:
                # If True there is no bending moment and no shear, thus no shear force, so no need for plotting.
                continue
            axis_values = plot_values_shear_force(el, factor)
            shear_1 = el.shear_force[0]
            shear_2 = el.shear_force[-1]

            if verbosity == 0:
                node_results = True
            else:
                node_results = False

            self.plot_result(axis_values, shear_1, shear_2, node_results=node_results)
        if show:
            self.plot()
        else:
            return self.fig
def test_output_pwm_active_high_read():
    pin = MockPWMPin(2)
    with PWMOutputDevice(pin, active_high=False) as device:
        device.value = 0.1
        assert isclose(device.value, 0.1)
        assert isclose(pin.state, 0.9)
        device.frequency = None
        assert device.value
Example #30
0
 def assert_states_and_times(self, expected_states):
     # Tests that the pin went through the expected states at the expected
     # times (times are compared with a tolerance of tens-of-milliseconds as
     # that's about all we can reasonably expect in a non-realtime
     # environment on a Pi 1)
     for actual, expected in zip(self.states, expected_states):
         assert isclose(actual.timestamp, expected[0], rel_tol=0.01, abs_tol=0.01)
         assert isclose(actual.state, expected[1])
Example #31
0
data_dir = 'dataset'
sampling_size = 20
clustering_env = env.Env(data_dir, sampling_size, reward='local_purity')
state, _ = clustering_env.reset(seed=0, steps=15)
print(state.cluster_assignments)
# labels                            [ 8, 3, 6, 7, 8, 1, 3, 1, 7, 6 ]
assert state.cluster_assignments == [[0], [1], [2], [3], [4], [5], [6], [7],
                                     [8], [9]]

action = env.Action(0, 3)
reward, state, purity = clustering_env.step(action)
assert purity == 1
assert state.cluster_assignments == [[0, 3], [1], [2], [4], [5], [6], [7], [8],
                                     [9]]
assert reward == 0

action = env.Action(0, 1)
reward, state, purity = clustering_env.step(action)
assert purity == 0.9
assert state.cluster_assignments == [[0, 1, 3], [2], [4], [5], [6], [7], [8],
                                     [9]]
assert math.isclose(reward, -1 / 3)

action = env.Action(1, 7)
reward, state, purity = clustering_env.step(action)
assert purity == 0.9
assert state.cluster_assignments == [[0, 1, 3], [2, 9], [4], [5], [6], [7],
                                     [8]]
assert math.isclose(reward, 0)
Example #32
0
    def image_location(self,
                       needle,
                       haystack,
                       tolerance=0.95,
                       draw=1,
                       template_res_w=1440,
                       device_res_w=1080):
        """Locate an image (needle) within an bigger image (haystack). Tolarance
        is pixel tolerance, i.e. 1.0 = all pixels are correct, 0.5 = 50% of the pixels
        are correct. If we know the original resolution, from which the template
        image is coming, we can supply it as template_res_w.
        Return value is the central (x,y) of the first image found.
        Draw function will plot red lines where needle image is found.
        """

        print("*INFO* _image_location Starts")

        image = cv2.imread(haystack)
        image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        _hay_h, hay_w = image_gray.shape[:2]

        needle_path = Path(needle)
        if not needle_path.exists():
            raise Exception("Needle file does not exist. Tried: {}".format(needle_path))
        template = cv2.imread(str(needle_path.resolve()), 0)

        if template is None:
            raise Exception("Cannot read template image. Tried: {}".format(needle))
        height, width = template.shape[:2]

        scale_ratios = self._get_scale_ratios(template_res_w, device_res_w)
        print("*DEBUG* Scale ratios to be used in order: {}".format(scale_ratios))

        best_highest_max_val = 0.0
        best_highest_max_val_loc = (-1, -1)
        best_scale_ratio = None
        best_matched_image = None

        print("*DEBUG* Resampling loop Starts")
        for scale_ratio in scale_ratios:
            interpolation_method = cv2.INTER_LINEAR if scale_ratio > 1.0 else cv2.INTER_AREA

            print(("*DEBUG* resize starts: for scale {}".format(scale_ratio)))

            if math.isclose(scale_ratio, 1.0, rel_tol=0.03):
                scaled_img_template = template
            else:
                scaled_img_template = cv2.resize(template, None, fx=scale_ratio,
                                                 fy=scale_ratio, interpolation=interpolation_method)
            print("*DEBUG* matchTemplate Starts:")

            res = cv2.matchTemplate(image_gray, scaled_img_template, cv2.TM_CCOEFF_NORMED)

            ratio = device_res_w / hay_w

            if CONFIG.get_value("RetinaDisplay"):
                ratio = ratio * 2
            elif ratio < 1.1:
                ratio = 1.0

            print("*DEBUG* _extract_points Starts:")
            _current_points, highest_max_val, highest_max_val_loc = \
                self._extract_points(height * scale_ratio,
                                     res,
                                     tolerance,
                                     width * scale_ratio,
                                     ratio)

            if highest_max_val > best_highest_max_val:
                best_highest_max_val = highest_max_val
                best_highest_max_val_loc = highest_max_val_loc

                best_scale_ratio = scale_ratio
                best_matched_image = scaled_img_template
                print("*DEBUG* Current best match location: {},\n"
                      "max_value: {},\n"
                      "scale_ratio: {}".format(best_highest_max_val_loc,
                                               best_highest_max_val,
                                               best_scale_ratio))

                if best_highest_max_val > tolerance:
                    if draw == 1:
                        loc = np.where(res >= tolerance)

                        for pt in zip(*loc[::-1]):
                            cv2.rectangle(image, pt,
                                          (pt[0] + width, pt[1] + height),
                                          (0, 0, 255), 2)
                        cv2.imwrite('temp_matched_area.png', image)
                    break

        print("*DEBUG* Ready to return points:")
        print("*DEBUG* Best match location: {}, best correlation value: {}, "
              "best scale ratio: {}".format(best_highest_max_val_loc,
                                            best_highest_max_val, best_scale_ratio))
        self._log_matched_image(image, template, best_matched_image,
                                best_highest_max_val_loc, best_scale_ratio)

        if best_highest_max_val >= tolerance:
            return best_highest_max_val_loc

        return -1, -1
def test_area():
    c = Circle(2)
    assert math.isclose(c.area,12.566370, rel_tol = 1e-7)
Example #34
0
def test_coordinates_to_decimal_edgecases(arcminutes, expected_easting,
                                          expected_northing):
    easting, northing = coordinate_string_to_decimal(arcminutes)
    assert math.isclose(easting, expected_easting, abs_tol=0.00001)
    assert math.isclose(northing, expected_northing, abs_tol=0.00001)
Example #35
0
def _is_unit_square(shorthand):
    values = map(float, shorthand)
    pairwise = zip(values, UNIT_SQUARE)
    return all(isclose(a, b, rel_tol=1e-3) for a, b in pairwise)
Example #36
0
def liveData():
	response = requests.get("https://api.zebpay.com/api/v1/ticker?currencyCode=INR")
	data = response.json()
	cur = LiveData.objects.get(siteId = 1, currency = 'INR')
	# print (data['buy'],data['sell'])
	if not math.isclose(data['buy'],cur.buy,rel_tol=1e-11) or not math.isclose(data['sell'],cur.sell,rel_tol=1e-11):
		cur.buy = data['buy']
		cur.sell = data['sell']
		cur.save()
		history = ZebpayHistory();
		history.buy = data['buy']
		history.sell = data['sell']
		history.currency = 'INR'
		history.timestamp = datetime.datetime.now()
		history.save()
	response = requests.get("https://api.zebpay.com/api/v1/ticker?currencyCode=SGD")
	data = response.json()
	cur = LiveData.objects.get(siteId = 1, currency = 'SGD')
	# print (data['buy'],data['sell'])
	if not math.isclose(data['buy'],cur.buy,rel_tol=1e-11) or not math.isclose(data['sell'],cur.sell,rel_tol=1e-11):
		cur.buy = data['buy']
		cur.sell = data['sell']
		cur.save()
		history = ZebpayHistory();
		history.buy = data['buy']
		history.sell = data['sell']
		history.currency = 'SGD'
		history.timestamp = datetime.datetime.now()
		history.save()
	response = requests.get("https://api.zebpay.com/api/v1/ticker?currencyCode=USD")
	data = response.json()
	cur = LiveData.objects.get(siteId = 1, currency = 'USD')
	# print (data['buy'],data['sell'])
	if not math.isclose(data['buy'],cur.buy,rel_tol=1e-11) or not math.isclose(data['sell'],cur.sell,rel_tol=1e-11):
		cur.buy = data['buy']
		cur.sell = data['sell']
		cur.save()
		history = ZebpayHistory();
		history.buy = data['buy']
		history.sell = data['sell']
		history.currency = 'USD'
		history.timestamp = datetime.datetime.now()
		history.save()
	response = requests.get("https://www.coinhako.com/api/v1/price/currency/BTCSGD")
	data = response.json()
	cur = LiveData.objects.get(siteId = 3, currency = 'SGD')
	# print (data['buy'],data['sell'])
	if not math.isclose(float(data['data']['buy_price']),cur.buy,rel_tol=1e-11) or not math.isclose(float(data['data']['sell_price']),cur.sell,rel_tol=1e-11):
		cur.buy = float(data['data']['buy_price'])
		cur.sell = float(data['data']['sell_price'])
		cur.save()
		history = CoinhakoHistory();
		history.buy = float(data['data']['buy_price'])
		history.sell = float(data['data']['sell_price'])
		history.currency = 'SGD'
		history.timestamp = datetime.datetime.now()
		history.save()
	response = requests.get("https://www.coinhako.com/api/v1/price/currency/BTCUSD")
	data = response.json()
	cur = LiveData.objects.get(siteId = 3, currency = 'USD')
	# print (data['buy'],data['sell'])
	if not math.isclose(float(data['data']['buy_price']),cur.buy,rel_tol=1e-11) or not math.isclose(float(data['data']['sell_price']),cur.sell,rel_tol=1e-11):
		cur.buy = data['data']['buy_price']
		cur.sell = data['data']['sell_price']
		cur.save()
		history = CoinhakoHistory();
		history.buy = data['data']['buy_price']
		history.sell = data['data']['sell_price']
		history.currency = 'USD'
		history.timestamp = datetime.datetime.now()
		history.save()
	response = requests.get("https://www.coinhako.com/api/v1/price/currency/BTCSGD")
	data = response.json()
	cur = LiveData.objects.get(siteId = 3, currency = 'SGD')
	# print (data['buy'],data['sell'])
	if not math.isclose(float(data['data']['buy_price']),cur.buy,rel_tol=1e-11) or not math.isclose(float(data['data']['sell_price']),cur.sell,rel_tol=1e-11):
		cur.buy = data['data']['buy_price']
		cur.sell = data['data']['sell_price']
		cur.save()
		history = CoinhakoHistory();
		history.buy = data['data']['buy_price']
		history.sell = data['data']['sell_price']
		history.currency = 'SGD'
		history.timestamp = datetime.datetime.now()
		history.save()	
	response = requests.get("https://api.coinbase.com/v2/prices/buy?currency=USD")
	data = response.json()
	response = requests.get("https://api.coinbase.com/v2/prices/sell?currency=USD")
	data2 = response.json()
	cur = LiveData.objects.get(siteId = 2, currency = 'USD')
	if not math.isclose(float(data['data']['amount']),cur.buy,rel_tol=1e-11) or not math.isclose(float(data2['data']['amount']),cur.sell,rel_tol=1e-11):
		cur.buy = data['data']['amount']
		cur.sell = data2['data']['amount']
		cur.save()
		history = CoinbaseHistory();
		history.buy = data['data']['amount']
		history.sell = data2['data']['amount']
		history.currency = 'USD'
		history.timestamp = datetime.datetime.now()
		history.save()
	response = requests.get("https://api.coinbase.com/v2/prices/buy?currency=SGD")
	data = response.json()
	response = requests.get("https://api.coinbase.com/v2/prices/sell?currency=SGD")
	data2 = response.json()
	cur = LiveData.objects.get(siteId = 2, currency = 'SGD')
	if not math.isclose(float(data['data']['amount']),cur.buy,rel_tol=1e-11) or not math.isclose(float(data2['data']['amount']),cur.sell,rel_tol=1e-11):
		cur.buy = data['data']['amount']
		cur.sell = data2['data']['amount']
		cur.save()
		history = CoinbaseHistory();
		history.buy = data['data']['amount']
		history.sell = data2['data']['amount']
		history.currency = 'SGD'
		history.timestamp = datetime.datetime.now()
		history.save()
Example #37
0
def is_unit_vector(vector):
    return math.isclose(LA.norm(vector), 1.0, rel_tol=1e-2)
	def trust_region_algorithm(self):
		# eta value in Book's trust-region algorithm 6.2 
		eta = 0.9 * 0.001 # eta \in (0,0.001)
		tolerance = 1E-5

		g = self.model.eval_gradient_vec()
		self.g = g
		norm_g = norm(g)	
		print('norm of g = {0:.4f}' .format(norm_g))
		self.norm_g = norm_g

		if norm_g < tolerance:
			print('-'*60)
			print('gradient is smaller than tolerance')

		if self.first_iteration is True:
			p = self.backtracking_line_search()			
			# we should call this function everytime before 
			# evaluation of aux gradient
			new_loss = self.model.eval_aux_loss(p_vec=p) 
			new_y = self.model.eval_y(use_overlap=self.use_overlap)
			new_s = p
			if new_s.T @ new_y <= 0 and self.quasi_Newton_matrix == 'L_BFGS':
				print('curvature condition did not satisfy for L_BFGS ==> danger zone') 
				# alpha = self.satisfy_curvature_condition(p)
				# alpha = self.satisfy_wolfe_condition(p)
				# new_s = alpha * p
				# new_loss = self.model.eval_aux_loss(p_vec=alpha * p) 
				# new_y = self.model.eval_y(use_overlap=self.use_overlap)

			self.update_S_Y(new_s,new_y)
			self.update_M()

			self.iter += 1
			self.first_iteration = False
			self.model.update_weights(p_vec=p)
			return
		
		gamma = self.find_gamma()
		p = self.trust_region_subproblem_solver()		
		rho = self.eval_reduction_ratio(p)

		new_loss = self.model.eval_aux_loss(p_vec=p) 
		new_y = self.model.eval_y(use_overlap=self.use_overlap)
		new_s = p
		if new_s.T @ new_y <= 0 and self.quasi_Newton_matrix == 'L_BFGS':
			print('curvature condition did not satisfy for L_BFGS ==> danger zone') 
			#alpha = self.satisfy_curvature_condition(p)
			# alpha = self.satisfy_wolfe_condition(p)
			# new_s = alpha * p
			#new_loss = self.model.eval_aux_loss(p_vec=alpha * p) 
			#new_y = self.model.eval_y(use_overlap=self.use_overlap)

		self.iter += 1

		# if new_s.T @ new_y > 0:
		self.update_S_Y(new_s,new_y)
		self.update_M()
		
		if rho > eta:
			self.model.update_weights(p_vec=p)
			print('weights updated')
		else:
			print('-'*30)
			print('No update in this iteration')

		# strategy_1
		if rho < 1/4:
			self.delta = 1/2 * self.delta
			print('shrinking trust region radius')
		else:
			if rho > 3/4 and isclose( norm(p), self.delta ):
				self.delta = min(2*self.delta, self.delta_hat)
				print('expanding trust region radius')
			else:
				self.delta = self.delta


		# strategy_2
		# if rho > 3/4:
		# 	if norm(new_s) < 0.8 * self.delta:
		# 		self.delta = self.delta
		# 	else:
		# 		print('expanding trust region radius')
		# 		self.delta = 2 * self.delta
		# elif rho > 0.1 and rho < 3/4:
		# 	self.delta = self.delta
		# else:
		# 	print('shrinking trust region radius')
		# 	self.delta = 0.5 * self.delta
		return
	def trust_region_subproblem_solver(self):
		# size of w = g.size
		delta = self.delta
		g = self.g
		n = g.size
		S = self.S
		Y = self.Y		
		self.form_Psi()
		Psi = self.Psi
		gamma = self.gamma
		M = self.M

		Q, R = qr(Psi, mode='reduced')

		# check if Psi is full rank or not
		if np.isclose(np.diag(R),0).any():
			rank_deficieny = True
			# find zeros of diagonal of R
			rank_deficient_idx = np.where( np.isclose(np.diag(R),0))[0]
			# deleting the rows of R with a 0 diagonal entry (r * k+1)
			R_cross = np.delete( R, obj=rank_deficient_idx, axis=0 )
			# deleting the columsn of Psi with a 0 diagonal entry on R (n * r)
			Psi_cross = np.delete( Psi, obj=rank_deficient_idx, axis=1 )
			# deleting the rows and columns of R with a 0 diagonal entry (r * r)
			R_cross_cross = np.delete( R_cross, obj=rank_deficient_idx, axis=1 )
			# (n * r)
			Q_hat = Psi_cross @ inv(R_cross_cross)
			# (r * r)
			R_M_R_T = R_cross @ M @ R_cross.T
		else:
			rank_deficieny = False
			R_M_R_T = R @ M @ R.T

		eigen_values, eigen_vectors = eig( R_M_R_T )
		# make sure eigen values are real
		eigen_values = eigen_values.real
		eigen_vectors = eigen_vectors.real

		# sorted eigen values
		idx = eigen_values.argsort()
		eigen_values_sorted = eigen_values[idx]
		eigen_vectors_sorted = eigen_vectors[:,idx]

		Lambda_hat = eigen_values_sorted
		V = eigen_vectors_sorted

		Lambda_1 = gamma + Lambda_hat

		lambda_min = min( Lambda_1.min(), gamma )

		if rank_deficieny:
			P_ll = Psi_cross @ inv(R_cross_cross) @ V
		else:
			P_ll = Psi @ inv(R) @ V # P_parallel 
		g_ll = P_ll.T @ g	# g_Parallel
		g_NL_norm = sqrt ( abs( norm(g) ** 2 - norm(g_ll) ** 2 ) )

		self.P_ll = P_ll
		self.g_ll = g_ll
		self.g_NL_norm = g_NL_norm
		self.Lambda_1 = Lambda_1
		self.lambda_min = lambda_min

		sigma = 0

		if lambda_min > 0 and self.phi_bar_func(0,delta) >= 0:
			sigma_star = 0
			tau_star = gamma
			# Equation (11) of SR1 paper
			p_star = - 1 / tau_star * \
				( g - Psi @ inv( tau_star * inv(M) + Psi.T @ Psi ) @ (Psi.T @ g) )
		elif lambda_min <= 0 and self.phi_bar_func(-lambda_min, delta) >= 0:
			sigma_star = -lambda_min
			if rank_deficieny:
				if ~isclose(sigma_star, -gamma):
					p_star = - Psi_cross @ inv(R_cross_cross) @ U * pinv( np.diag(Lambda_1 + sigma_star) ) @ g_ll - 1 / ( gamma + sigma_star) * ( g - ( Psi_cross @ inv(R_cross_cross) ) @ inv(R_cross_cross).T @ ( Psi_cross.T @ g ) )
				else:
					p_star = - Psi_cross @ inv(R_cross_cross) @ U * inv( np.diag(Lambda_1 + sigma_star) ) @ g_ll

			else:
				# Equation (13) of SR1 paper
				if ~isclose(sigma_star, -gamma):
					p_star = - Psi @ inv(R) @ U * pinv( np.diag(Lambda_1 + sigma_star) ) @ g_ll - 1 / ( gamma + sigma_star) * ( g - ( Psi @ inv(R) ) @ inv(R).T @ ( Psi.T @ g ) )
				else:
					p_star = - Psi @ inv(R) @ U * inv( np.diag(Lambda_1 + sigma_star) ) @ g_ll
			###############???????????????????#########################
			# so-called hard-case
			if lambda_min < 0:
				p_star_hat = p_star.copy()
				# Equation (14) of SR1 paper
				# check if lambda_min is Lambda_1[0]
				if isclose( lambda_min, Lambda_1.min()):
					u_min = P_ll[:,0].reshape(-1,1)
				else:
					for j in range(Lambda_1.size+2):
						e = np.zeros((n,1))
						e[j,0] = 1.0
						u_min = e - P_ll @ P_ll.T @ e
						if ~isclose( norm(u_min), 0.0):
							break
				# find alpha in Eq (14)
				# solve a * alpha^2 + b * alpha + c = 0  
				a = norm(u_min) ** 2
				b = 2 * norm(u_min) * norm(p_star_hat)
				c = norm(p_star_hat) - delta ** 2
				
				alpha_1 = -b + sqrt(b ** 2 - 4 * a * c) / (2 * a)
				alpha_2 = -b - sqrt(b ** 2 - 4 * a * c) / (2 * a)
				alpha = alpha_1
				
				p_star = p_star_hat + alpha * u_min 
		else:
			sigma_star = self.solve_newton_equation_to_find_sigma(delta)
			tau_star = sigma_star + gamma
			# Equation (11) of SR1 paper
			p_star = - 1 / tau_star * \
				( g - Psi @ inv(tau_star * inv(M) + Psi.T @ Psi ) @ (Psi.T @ g))

		return p_star
Example #40
0
def test_sin_values():
    for i, v in zip(range(1000), sin_values()):
        assert isclose(v, sin(radians(i)), abs_tol=1e-9)
Example #41
0
def test_kl_divergence_computation_is_correct():
    p = Counter({"a": 0.5, "b": 0.25})
    q = Counter({"a": 0.25, "b": 0.50, "c": 0.15, "d": 0.10})
    result = _get_kl_divergence(p, q)
    expected = 0.1733
    assert math.isclose(result, expected, rel_tol=1e-3)
Example #42
0
def test_cos_values():
    for i, v in zip(range(1000), cos_values()):
        assert isclose(v, cos(radians(i)), abs_tol=1e-9)
Example #43
0
def test_is_close():
    ''' just to make sure '''
    assert isclose(4.5, 4.5)
    assert isclose(4.5, 4.499999999999999999)

    assert not isclose(4.5, 4.6)
Example #44
0
def test_quadratic_3():
    # quadratic with partial
    partial_quadratic_1_3_2 = partial(quadratic, A=1, B=3, C=2)
    partial_quad = trapz(partial_quadratic_1_3_2, 5, 10)
    assert isclose(partial_quad, 414.16875)
Example #45
0
def comp(expected_output, output):
    if len(output) != len(expected_output):
        return False
    return all(
        math.isclose(s.distance, d)
        for s, d in zip(sorted(output), expected_output))
Example #46
0
 def __eq__(self, other) -> bool:
     if not type(other) is Angle:
         return NotImplemented
     return isclose(self.radians % (2 * pi),
                    other.radians % (2 * pi),
                    abs_tol=1e-9)
Example #47
0
 def __eq__(self, rhs):
     return math.isclose(self.distance, rhs.distance)
Example #48
0
def test_to_ocs_angle_deg():
    ucs = UCS.from_x_axis_and_point_in_xy(origin=(1, 2, 3), axis=(2, 3, 4), point=(3, 2, 5))
    expected = 120.077450607124
    assert isclose(ucs.to_ocs_angle_deg(45), expected)
Example #49
0
    def intersection(AC, BD):
        if not isinstance(AC, Segment):
            raise TypeError("Expected 'a' to be an instance of Segment!")

        if not isinstance(BD, Segment):
            raise TypeError("Expected 'b' to be an instance of Segment!")

        # Goal: Determine the intersection of line segments AC and BD, if it exists.
        I, delta = None, None

        try:
            # Objective: Find the angle of intersection from A to I to B, or angle AIB.
            # ================================
            # The method used will be to find the angles of ABD and BAC, and subtract
            # their summation from the sum of angles in any triangle, 180* or π. We'll
            # be using radians, so as to find a "slice of π". ^_^

            # Task: Calculate angle ABD.
            # --------------------------------
            vAB = AC.debut - BD.debut
            vBD = BD.arret - BD.debut
            ABD = Vertex.angleBetween(vAB, vBD)

            # Task: Calculate angle BAC.
            # --------------------------------
            vBA = BD.debut - AC.debut
            vAC = AC.arret - AC.debut
            BAC = Vertex.angleBetween(vBA, vAC)

            # Task: Derive the angle AIB.
            # --------------------------------
            AIB = pi - (ABD + BAC)

            # Objective: Find the lengths of line segments AB, AI, and BI.
            # ================================
            # We'll be using the rule of sines, as we already know the length of
            # line segment AB, which also happens to correspond to angle AIB, so
            # we'll call it.. i.
            ai = vAB.magnitude()
            bi = vBA.magnitude()

            # Task: Calculate the lengths of line segments AI and BI.
            # --------------------------------
            af = abs(
                ai * sin(ABD) / sin(AIB)
            )  # The length of line segment AI along line segment AC. It can't be negative.
            bf = abs(
                bi * sin(BAC) / sin(AIB)
            )  # The length of line segment BI along line segment BD. It can't be negative.

            # Objective: Find the intersection vertex I.
            # ================================
            # If an intersection exists, the lengths of AI or BI must be less than or equal to
            # AC or BD, respectively.
            if af <= vAC.magnitude() and bf <= vBD.magnitude():

                # Task: Calculate the intersection vertices Ia and Ib.
                # --------------------------------
                # Since we know the lengths of AI and BI, we can scale the unit vectors of the
                # respective line segments to derive "their I"s.
                Ia = AC.debut + (vAC.unit() * af)
                Ib = BD.debut + (vBD.unit() * bf)

                # print(f"Ia: {Ia}; Ib: {Ib}")

                # Task: Derive the intersection vertex I.
                # --------------------------------
                # But only if the values are close enough; otherwise, no deal!

                if isclose(Ia[0], Ib[0]) and isclose(Ia[1], Ib[1]):
                    I = Ia

                    # Due to potential rounding issues, we'll provide the delta of the calculated
                    # intersection vectors.
                    delta = Ia - Ib

        except ZeroDivisionError:
            # If we get a division by zero error... either there was a line length of 0, or
            # something... interesting happened.
            pass
            # TODO: What to do in this case? As one test outputs:
            # No intersection between [130, 351] --- [521, 154] and [521, 154] --- [521, 154].
            # Technically, the intersection point is [521, 154], but because the second line
            # has a length of zero, the algorithm above cannot find one of the angles, given
            # that the vector has zero length (in this case, vBD).
        except ValueError:
            # This can happen when exceeding the domain of math.acos(numerator/divisor).
            # I didn't know .. math had a domain that could error out.
            pass

        return (I, delta)
Example #50
0
 def _do_assertions_about_params(self, rot, center, angle, radius):
     assert math.isclose(rot.arc.angle, angle)
     assert rot.arc.center == center
     assert rot.arc.radius == radius
Example #51
0
            # Hgnc response
            response = hgnc_http.request(
                '/search/' + quote(processed['P_NAME']), 'GET', '',
                '')['response']
        except:
            print('HGNC request failed, so retry after 5 seconds')
            time.sleep(5)
            continue
        else:
            print('HGNC response time:',
                  get_elapsed_seconds(get_current_millis(), elapsed_millis))
            break

    # Ignore empty docs, not equal to response's max score.
    if not response['docs'] or \
      not math.isclose(original['MAX_SCORE'], response['maxScore'], abs_tol=0.01):
        continue

    max_doc = get_max_score_doc(response['docs'])

    elapsed_millis = get_current_millis()
    gene_family_info = None
    with db.cursor(pymysql.cursors.DictCursor) as cursor:
        cursor.execute('SELECT * FROM GENES_FAMILY where APPROVED_SYMBOL=%s',
                       (max_doc['symbol'], ))
        gene_family_info = cursor.fetchone()
    print('Find gene family time:',
          get_elapsed_seconds(get_current_millis(), elapsed_millis))

    if not gene_family_info:
        continue
Example #52
0
      return "this is a string"

assert math.ceil(StrangeCeil()) == "this is a string"

# tests for math.nan and math.inf (some tests could be shared with py_float.js)
assert math.nan != math.nan
assert math.inf > 10
assert math.inf > -10
assert -math.inf < 10
assert -math.inf < -10

# tests for math.isclose
cases = [
    {'a': 1e10, 'b': 1.00001e10, 'rel_tol': 1e-09, 'abs_tol': 0.0},
    {'a': 1e-7, 'b': 1e-8, 'rel_tol': 1e-09, 'abs_tol': 0.0},
    {'a': 1e-8, 'b': 1e-9, 'rel_tol': 1e-09, 'abs_tol': 0.0},
    {'a': 1e10, 'b': 1.0001e10, 'rel_tol': 1e-09, 'abs_tol': 0.0},
    {'a': 1.0, 'b': 1.0, 'rel_tol': 1e-09, 'abs_tol': 0.0},
    {'a': 1.0, 'b': 1.01, 'rel_tol': 1, 'abs_tol': 0.0},
    {'a': 1.0, 'b': 1.01, 'rel_tol': 0.001, 'abs_tol': 0.0},
    {'a': math.nan, 'b': math.nan, 'rel_tol': 1e-09, 'abs_tol': 0.0},
    {'a': math.inf, 'b': 10, 'rel_tol': 1e-09, 'abs_tol': 0.0}
]
expected = [False, False, False, False, True, True, False, False, False]
for case, result in zip(cases, expected):
    assert math.isclose(**case) == result

# issue 924
assert math.gcd(234, 78) == 78
print("passed all tests..")
Example #53
0
import math
a = 4.75
print("Variabel a : ", a)
print("Quadratwurzel von a :", math.sqrt(a))
print("Natürliche Logaritmus von a :", math.log(a))
print("e hoch a : ", math.exp(a))
print("10er-logaritmus von a :", math.log10(a))

print()
print("Kreiszahl pi :", math.pi)
print("Eulersche Zahl e :", math.e)

print()
print("Fakultät von 5 :", math.factorial(5))
print("grösster gem.Teiler von 60 und 135 ist: ", math.gcd(60, 135))

if (math.isclose(3, 2.96, rel_tol=0.01)):
    print("Nahe dran")
else:
    print("Nicht nahe dran")
if (math.isclose(3, 2.97, rel_tol=0.01)):
    print("Nahe dran")

else:
    print("Nicht nahe dran")
Example #54
0
def test_sine():
    # python sin function with arbitrary start and end points
    under_sin = trapz(sin, 24, 346)
    assert isclose(under_sin, 0.030750318486179)
Example #55
0
def test_backward_forward():
    results = []
    headers = []

    print("#############################################\nTesting backward "
          "forward\n#############################################\n\n")
    # Basic
    alg = "forward"
    configure_sys(alg, SEQ_DICT["tata_seq_long"], TSV_LIST[0], PROB_DICT["more_likely_zero"])
    output = motif_find.main()
    print_result("forward runs and finish", PASSED if output else FAILED)

    alg = "backward"
    configure_sys(alg, SEQ_DICT["tata_seq_long"], TSV_LIST[0], PROB_DICT["more_likely_zero"])
    output = motif_find.main()
    print_result("backward runs and finish", PASSED if output else FAILED)

    for seq in SEQ_DICT:
        for tsv in TSV_LIST:
            for prob in PROB_DICT:
                alg = "forward"
                configure_sys(alg, SEQ_DICT[seq], tsv, PROB_DICT[prob])
                output_forward = motif_find.main()

                alg = "backward"
                configure_sys(alg, SEQ_DICT[seq], tsv, PROB_DICT[prob])
                output_backward = motif_find.main()

                print_result("compare backward and forward results for seq: %s tsv: %s prob: %s" %
                             (seq, tsv, prob),
                             PASSED if math.isclose(output_backward, output_forward, rel_tol=1e-5)
                             else FAILED)

                results.append(output_forward)
                headers.append(format_things(seq, tsv, prob))

    # Testing very long seq
    alg = "forward"
    configure_sys(alg, TO_MUCH_TATA, TSV_LIST[0], PROB_DICT["one"])
    output_forward = motif_find.main()

    alg = "backward"
    configure_sys(alg, TO_MUCH_TATA, TSV_LIST[0], PROB_DICT["one"])
    output_backward = motif_find.main()
    print_result("Testing very long seq", PASSED if math.isclose(output_backward, output_forward,
                                                                 rel_tol=1e-5) else FAILED)

    results.append(output_forward)
    headers.append(format_things("TO_MUCH_TATA", TSV_LIST[0], "one"))

    # Testing very long motif
    alg = "forward"
    configure_sys(alg, TO_MUCH_TATA, LONG_MOTIF_TSV, PROB_DICT["one"])
    output_forward = motif_find.main()

    alg = "backward"
    configure_sys(alg, TO_MUCH_TATA, LONG_MOTIF_TSV, PROB_DICT["one"])
    output_backward = motif_find.main()
    print_result("Testing very long motif", PASSED if math.isclose(output_backward, output_forward,
                                                                   rel_tol=1e-5) else FAILED)

    results.append(output_forward)
    headers.append(format_things("TO_MUCH_TATA", "LONG_MOTIF_TSV", "one"))

    with open("forward_backward.csv", "w") as output_file:
        output_file.write(",".join(headers))
        output_file.write("\n")
        output_file.write(",".join([str(i) for i in results]))
Example #56
0
    def _process_trade_event_message(self, order_msg: Dict[str, Any]):
        """
        Updates in-flight order and trigger order filled event for trade message received. Triggers order completed
        event if the total executed amount equals to the specified order amount.
        :param order_msg: The order event message payload
        """

        client_order_id = str(order_msg["ClientOrderId"])
        if client_order_id in self.in_flight_orders:
            tracked_order = self.in_flight_orders[client_order_id]
            updated = tracked_order.update_with_trade_update(order_msg)

            if updated:
                trade_amount = Decimal(str(order_msg["Quantity"]))
                trade_price = Decimal(str(order_msg["Price"]))
                trade_fee = self.get_fee(
                    base_currency=tracked_order.base_asset,
                    quote_currency=tracked_order.quote_asset,
                    order_type=tracked_order.order_type,
                    order_side=tracked_order.trade_type,
                    amount=trade_amount,
                    price=trade_price)
                amount_for_fee = (trade_amount
                                  if tracked_order.trade_type is TradeType.BUY
                                  else trade_amount * trade_price)
                tracked_order.fee_paid += amount_for_fee * trade_fee.percent

                self.trigger_event(
                    MarketEvent.OrderFilled,
                    OrderFilledEvent(self.current_timestamp,
                                     tracked_order.client_order_id,
                                     tracked_order.trading_pair,
                                     tracked_order.trade_type,
                                     tracked_order.order_type,
                                     trade_price,
                                     trade_amount,
                                     trade_fee,
                                     exchange_trade_id=str(
                                         order_msg["TradeId"])))
                if (math.isclose(tracked_order.executed_amount_base,
                                 tracked_order.amount)
                        or tracked_order.executed_amount_base >=
                        tracked_order.amount):
                    tracked_order.mark_as_filled()
                    self.logger().info(
                        f"The {tracked_order.trade_type.name} order "
                        f"{tracked_order.client_order_id} has completed "
                        f"according to order status API")
                    event_tag = (MarketEvent.BuyOrderCompleted
                                 if tracked_order.trade_type is TradeType.BUY
                                 else MarketEvent.SellOrderCompleted)
                    event_class = (BuyOrderCompletedEvent
                                   if tracked_order.trade_type is TradeType.BUY
                                   else SellOrderCompletedEvent)
                    self.trigger_event(
                        event_tag,
                        event_class(self.current_timestamp,
                                    tracked_order.client_order_id,
                                    tracked_order.base_asset,
                                    tracked_order.quote_asset,
                                    tracked_order.fee_asset,
                                    tracked_order.executed_amount_base,
                                    tracked_order.executed_amount_quote,
                                    tracked_order.fee_paid,
                                    tracked_order.order_type,
                                    tracked_order.exchange_order_id))
                    self.stop_tracking_order(tracked_order.client_order_id)
Example #57
0
def test_quadratic_2():
    # quadratic with closure
    closure_quadratic_1_3_2 = quadratic_2(1, 3, 2)
    curried_quad = trapz(closure_quadratic_1_3_2, 5, 10)
    assert isclose(curried_quad, 414.16875)
Example #58
0
# 通过跟踪多个中间部分款项, 避免了精度损失
print(sum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
           0.1]))  # 10 * 0.1  # >>>  0.9999999999999999
print(math.fsum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
                 0.1]))  # 10 * 0.1  # >>>  1.10

# math.gcd(a, b)
# 返回整数a和b的最大公约数, gcd(0, 0)返回0
print(math.gcd(12, 16))  # >>> 4

# math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
# 如果a和b的值彼此接近, 则返回True, 反之则False
# rel_tol is the relative tolerance – it is the maximum allowed difference between a and b
# To set a tolerance of 5%, pass rel_tol=0.05
# abs_tol is the minimum absolute tolerance – useful for comparisons near zero. abs_tol must be at least zero
print(math.isclose(100, 90, rel_tol=0.05))  # >>> False  # 设置为百分比

# math.isfinite(x)
# 如果x既不是无穷大也不是NaN, 则返回True, 否则返回False. 注意0.0 是被认为是有限的
print(math.isfinite(5))  # >>> True
print(math.isfinite(1 / 3))  # >>> True  # 无限循环小数也是有限的
print(math.isfinite(math.pi))  # True  # 无限不循环小数也是有限
# print(math.isfinite(1/0))    # raise ZeroDivisionError

# math.isinf(x)
# 如果x是正或负无穷大, 则返回True, 否则返回False
print(9e2)  # >>> 900.0
print(math.isinf(9e999))  # >>> True

# math.isnan(x)
# 如果x是NaN(不是数字), 则返回True, 否则返回False
Example #59
0
def test_quadratic_1():
    # quadratic with kwargs
    coef = {'A': 1, 'B': 3, 'C': 2}
    quad = trapz(quadratic, 5, 10, **coef)
    assert isclose(quad, 414.16875)
def test_distance():
    spiral = EulerSpiral(2.0)
    assert isclose(spiral.distance(10), 0.4)