コード例 #1
0
    def test_send_list(self):
        listing = [
            np.random.rand(7, 2),
            'Hello World',
            1,
            2.654,
        ]

        server = SimpleServer(
            ('127.0.0.1', 0),
            handle_fun=lambda x: Socket(x).send_list(listing))
        server.start()
        client = Socket(
            socket.create_connection(('127.0.0.1', server.server_port)))

        sleep(0.1)

        data = []
        list_len = client.get_int()

        for ind in range(list_len):
            data_type = client.get_string()
            if data_type == 'scalar':
                data.append(client.get_scalar())
            elif data_type == 'string':
                data.append(client.get_string())
            elif data_type == 'array':
                data.append(client.get_array())
        utils.check_vals_in_iterable(data, listing)

        client.close()
        server.stop()
コード例 #2
0
 def test_init_file_addhoc(self, get_h5saver, tmp_path):
     h5saver = get_h5saver
     addhoc_file_path = tmp_path.joinpath('h5file.h5')
     h5saver.init_file(update_h5=True,
                       addhoc_file_path=addhoc_file_path,
                       metadata=dict(attr1='attr1', attr2=(10, 2)))
     assert h5saver.h5_file_path == addhoc_file_path.parent
     assert h5saver.h5_file_name == addhoc_file_path.name
     assert h5saver.settings.child(
         ('current_h5_file')).value() == str(addhoc_file_path)
     assert h5saver.current_scan_group is None
     assert h5saver.get_node_path(h5saver.raw_group) == '/Raw_datas'
     assert h5saver.get_node_path(
         h5saver.logger_array) == '/Raw_datas/Logger'
     assert h5saver.get_attr(h5saver.logger_array, 'data_dimension') == '0D'
     assert h5saver.get_attr(h5saver.logger_array, 'scan_type') == 'scan1D'
     assert h5saver.get_attr(h5saver.logger_array,
                             'dtype') == np.dtype(np.uint8).name
     assert h5saver.get_attr(h5saver.logger_array, 'subdtype') == 'string'
     assert h5saver.get_last_scan() is None
     assert h5saver.get_attr(h5saver.raw_group,
                             'type') == h5saver.settings.child(
                                 ('save_type')).value()
     assert h5saver.get_attr(h5saver.root(),
                             'date') == datetime.now().date().isoformat()
     assert h5saver.get_attr(h5saver.raw_group, 'attr1') == 'attr1'
     utils.check_vals_in_iterable(
         h5saver.get_attr(h5saver.raw_group, 'attr2'), (10, 2))
コード例 #3
0
def test_check_vals_in_iterable():
    with pytest.raises(Exception):
        utils.check_vals_in_iterable([
            1,
        ], [])

    assert not utils.check_vals_in_iterable([1, 2.0, 4], (1, 2, 4))
    assert not utils.check_vals_in_iterable([1, 2.0, 4], (1, 2.0, 4))
    assert not utils.check_vals_in_iterable(np.array([1, 2.0, 4]),
                                            np.array((1, 2, 4)))
コード例 #4
0
    def test_earray(self, get_backend):
        bck = get_backend
        g1 = bck.get_set_group(bck.root(), 'g1')
        title = 'this is a earray'
        dtype = np.uint32
        array = bck.create_earray(g1, 'array', dtype=dtype, title=title)
        assert array.attrs['TITLE'] == title
        assert array.attrs['CLASS'] == 'EARRAY'
        assert array.attrs['dtype'] == np.dtype(dtype).name
        utils.check_vals_in_iterable(array.attrs['shape'], [0])
        array.append(np.array([10]))
        assert g1.children()['array'] == array
        assert isinstance(g1.children()['array'], EARRAY)
        utils.check_vals_in_iterable(array.attrs['shape'], [1])

        array_shape = (10, 3)
        array1 = bck.create_earray(g1,
                                   'array1',
                                   dtype=dtype,
                                   data_shape=array_shape,
                                   title=title)
        array1.append(generate_random_data(array_shape, dtype))
        expected_shape = [1]
        expected_shape.extend(array_shape)
        utils.check_vals_in_iterable(array1.attrs['shape'], expected_shape)
        array1.append(generate_random_data(array_shape, dtype))
        expected_shape[0] += 1
        utils.check_vals_in_iterable(array1.attrs['shape'], expected_shape)
        bck.close_file()
コード例 #5
0
    def test_group_creation(self, get_backend):
        bck = get_backend
        title = 'this is a group'
        assert bck.get_node(bck.root()) == bck.root()
        assert bck.get_node('/') == bck.root()

        g1 = bck.get_set_group(bck.root(), 'g1', title)
        g2 = bck.get_set_group('/', 'g2')
        assert g1.attrs['TITLE'] == title
        assert g1.attrs['CLASS'] == 'GROUP'

        assert bck.get_node_name(g2) == 'g2'

        g21 = bck.get_set_group(g2, 'g21')
        assert bck.get_node_path(g21) == '/g2/g21'
        assert bck.is_node_in_group(g2, 'g21')

        g22 = bck.get_set_group('/g2', 'g22', title='group g22')
        assert bck.get_group_by_title('/g2', 'group g22') == g22

        assert list(bck.get_children(g2)) == ['g21', 'g22']

        assert bck.get_parent_node(g22) == g2
        assert bck.get_parent_node(bck.get_parent_node(g2)) is None
        assert g22.parent_node == g2
        assert g2.parent_node.parent_node is None

        assert isinstance(g2, Node)
        assert isinstance(g22, Node)

        #test node methods
        assert g2 != g1

        assert g22.parent_node == g2
        assert g22.parent_node == g21.parent_node
        utils.check_vals_in_iterable(g2.children_name(), ['g21', 'g22'])
        for child in g2.children():
            assert g2.children()[child] in [g21, g22]
        g22.set_attr('test', 12.5)
        assert g22.get_attr('test') == 12.5
        assert g22.attrs['test'] == 12.5
        g22.attrs['test1'] = 'attr'
        assert g22.attrs['test1'] == 'attr'
        assert isinstance(g22.attrs, Attributes)
        assert g21.name == 'g21'
        assert bck.root().name == '/'
        assert g22.path == '/g2/g22'

        bck.close_file()
コード例 #6
0
    def test_attrs(self, get_backend):
        bck = get_backend
        attrs = dict(attr1='one attr', attr2=(10, 15), attr3=12.4)
        for attr in attrs:
            bck.root().attrs[attr] = attrs[attr]
        for attr in attrs:
            attr_back = bck.root().attrs[attr]
            if hasattr(attr_back, '__iter__'):
                utils.check_vals_in_iterable(attr_back, attrs[attr])
            else:
                assert attr_back == attrs[attr]

        attrs_back = bck.root().attrs.attrs_name
        for attr in attrs:
            assert attr in attrs_back
            if hasattr(attrs[attr], '__iter__'):
                utils.check_vals_in_iterable(attrs[attr], bck.root().attrs[attr])
            else:
                assert attrs[attr] == bck.root().attrs[attr]

        bck.close_file()
コード例 #7
0
    def test_init_file(self, get_h5saver_scan, tmp_path):
        h5saver = get_h5saver_scan
        datetime_now = datetime.now()
        date = datetime_now.date()
        today = f'{date.year}{date.month:02d}{date.day:02d}'
        base_path = tmp_path
        h5saver.settings.child(('base_path')).setValue(str(base_path))
        update_h5 = True
        scan_path, current_scan_name, save_path = h5saver.update_file_paths(
            update_h5)
        assert scan_path == tmp_path.joinpath(str(
            date.year)).joinpath(today).joinpath(f'Dataset_{today}_000')
        assert current_scan_name == 'Scan000'

        assert save_path == tmp_path.joinpath(str(
            date.year)).joinpath(today).joinpath(f'Dataset_{today}_000')

        h5saver.init_file(update_h5=update_h5)
        assert h5saver.h5_file is h5saver._h5file
        assert h5saver.h5_file_path.joinpath(
            f'Dataset_{today}_001.h5').is_file()
        assert h5saver.get_scan_index() == 0

        h5saver.init_file(update_h5=update_h5)
        assert h5saver.h5_file_path.joinpath(
            f'Dataset_{today}_002.h5').is_file()
        scan_group = h5saver.add_scan_group()
        assert h5saver.get_node_name(h5saver.get_last_scan()) == 'Scan000'
        assert h5saver.get_scan_index() == 0
        scan_group1 = h5saver.add_scan_group()
        assert scan_group == scan_group1  #no increment as no scan_done attribute
        utils.check_vals_in_iterable(
            sorted(list(h5saver.get_children(h5saver.raw_group))),
            sorted(['Logger', 'Scan000']))
        h5saver.init_file(update_h5=False)
        utils.check_vals_in_iterable(
            sorted(list(h5saver.get_children(h5saver.raw_group))),
            sorted(['Logger', 'Scan000']))
コード例 #8
0
    def test_carray(self, get_backend):
        bck = get_backend
        g1 = bck.get_set_group(bck.root(), 'g1')
        carray1_data = np.random.rand(5, 10)
        title = 'this is a carray'
        with pytest.raises(ValueError):
            bck.create_carray(g1, 'carray0', title='')
        carray1 = bck.create_carray(g1,
                                    'carray1',
                                    obj=carray1_data,
                                    title=title)
        assert isinstance(carray1, CARRAY)
        assert carray1.attrs['dtype'] == carray1_data.dtype.name
        assert carray1.attrs['TITLE'] == title
        assert carray1.attrs['CLASS'] == 'CARRAY'
        utils.check_vals_in_iterable(carray1.attrs['shape'],
                                     carray1_data.shape)

        assert np.all(carray1.read() == pytest.approx(carray1_data))
        assert np.all(carray1[1, 2] == pytest.approx(carray1_data[1, 2]))
        with pytest.raises(AttributeError):
            bck.append(carray1, carray1_data)

        bck.close_file()
コード例 #9
0
    def test_get_h5_data(self, load_test_file):
        """Load files (created with all backends) and manipulate them using
         all backends for cross compatibility checks
         """
        h5utils = load_test_file
        node_path = '/Raw_datas/Logger'
        node = h5utils.get_node(node_path)
        assert isinstance(node, StringARRAY)
        data, axes, nav_axes = h5utils.get_h5_data(node_path)
        assert isinstance(data, list)
        assert len(data) == 2
        assert data[0] == 'log1 to check'
        assert data[1] == 'log2 to check'
        assert axes == []
        assert nav_axes == []

        Nx = 12
        Nnavx = 5
        Nnavy = 10
        x_axis_in_file = dict(label='this is data axis',
                              units='no units',
                              data=np.arange(Nx))
        nav_x_axis_in_file = dict(label='this is nav x axis',
                                  units='x units',
                                  data=np.arange(Nnavx))
        nav_y_axis_in_file = dict(label='this is nav y axis',
                                  units='y units',
                                  data=np.arange(Nnavy))
        data_in_file = np.arange(Nx * Nnavx * Nnavy)
        data_in_file = data_in_file.reshape(Nnavx, Nnavy, Nx)

        node_path = '/Agroup'
        node = h5utils.get_node(node_path)
        assert len(node.children()) == 2
        assert node.children_name() == ['Data', 'X_axis']
        node_path = '/Agroup/Data'
        data, axes, nav_axes = h5utils.get_h5_data(node_path)
        assert np.all(data == pytest.approx(np.arange(Nx) * 1.0 + 7))

        node_path = '/Raw_datas/Scan000/Detector000/Data1D/Ch000/Data'
        node = h5utils.get_node(node_path)
        data, axes, nav_axes = h5utils.get_h5_data(node_path)
        assert isinstance(data, np.ndarray)
        assert data.shape == (5, 10, 12)
        assert node.attrs['shape'] == (5, 10, 12)
        assert np.all(data == data_in_file)

        assert len(axes) == 4
        assert len(nav_axes) == 2
        utils.check_vals_in_iterable(nav_axes, [0, 1])
        assert axes['x_axis']['label'] == x_axis_in_file['label']
        assert axes['x_axis']['units'] == x_axis_in_file['units']
        assert np.all(axes['x_axis']['data'] == x_axis_in_file['data'])
        assert isinstance(axes['x_axis'], utils.Axis)
        assert isinstance(axes['x_axis'], dict)

        assert axes['nav_x_axis']['label'] == nav_x_axis_in_file['label']
        assert axes['nav_x_axis']['units'] == nav_x_axis_in_file['units']
        assert np.all(axes['nav_x_axis']['data'] == nav_x_axis_in_file['data'])

        assert axes['nav_y_axis']['label'] == nav_y_axis_in_file['label']
        assert axes['nav_y_axis']['units'] == nav_y_axis_in_file['units']
        assert np.all(axes['nav_y_axis']['data'] == nav_y_axis_in_file['data'])
        h5utils.close_file()
コード例 #10
0
    def test_methods(self, qtbot):
        server = SimpleServer(('127.0.0.1', 6341), )
        server.start()

        params = [{
            'title': 'Device index:',
            'name': 'device',
            'type': 'int',
            'value': 0,
            'max': 3,
            'min': 0
        }, {
            'title': 'Infos:',
            'name': 'infos',
            'type': 'str',
            'value': "one_info",
            'readonly': True
        }, {
            'title':
            'Line Settings:',
            'name':
            'line_settings',
            'type':
            'group',
            'expanded':
            False,
            'children': [
                {
                    'name': 'someparam',
                    'type': 'float',
                    'value': 15.54,
                    'readonly': True
                },
            ]
        }]
        param = Parameter.create(name='settings',
                                 type='group',
                                 children=params)
        client = TCPClient(ipaddress="127.0.0.1",
                           port=6341,
                           params_state=param.saveState(),
                           client_type="sometype")
        client.cmd_signal.connect(self.get_cmd_signal)

        #check init method
        assert client.ipaddress == "127.0.0.1"
        assert client.port == 6341
        assert client.client_type == 'sometype'

        client.socket = Socket(
            native_socket.socket(socket.AF_INET, socket.SOCK_STREAM))
        client.socket.connect((client.ipaddress, client.port))
        sleep(0.5)
        server_socket = Socket(server.do_read()[0])

        client.send_data([np.array([0, 1, 2, 3]), 'item', 5.1])
        assert server_socket.get_string() == 'Done'
        utils.check_vals_in_iterable(server_socket.get_list(),
                                     [np.array([0, 1, 2, 3]), 'item', 5.1])

        client.send_infos_xml(custom_tree.parameter_to_xml_string(param))
        assert server_socket.get_string() == 'Infos'
        assert server_socket.get_string(
        ) == custom_tree.parameter_to_xml_string(param).decode()

        client.send_info_string('someinfo', 'this is an info')
        assert server_socket.get_string() == 'Info'
        assert server_socket.get_string() == 'someinfo'
        assert server_socket.get_string() == 'this is an info'

        #test queue_command
        client.cmd_signal.connect(self.get_cmd_signal)
        client_manager = ClientObjectManager()
        client_manager.cmd_signal.connect(client.queue_command)

        with pytest.raises(Exception):
            client.queue_command(utils.ThreadCommand('Weird command'))

        #test get_data
        server_socket.send_string('set_info')
        server_socket.send_list(['line_settings', 'someparam'])
        server_socket.send_string(
            custom_tree.parameter_to_xml_string(
                param.child('line_settings', 'someparam')))

        msg = client.socket.get_string()
        client.get_data(msg)
        assert self.command == 'set_info'
        utils.check_vals_in_iterable(
            self.attributes,
            [['line_settings', 'someparam'],
             custom_tree.parameter_to_xml_string(
                 param.child('line_settings', 'someparam')).decode()])

        server_socket.send_string('move_abs')
        server_socket.send_scalar(12.546)

        msg = client.socket.get_string()
        client.get_data(msg)
        assert self.command == 'move_abs'
        utils.check_vals_in_iterable(self.attributes, [12.546])

        server_socket.send_string('move_rel')
        server_socket.send_scalar(3.2)

        msg = client.socket.get_string()
        client.get_data(msg)
        assert self.command == 'move_rel'
        utils.check_vals_in_iterable(self.attributes, [3.2])

        server.stop()