def get_header_from_signal(signal, endianess="<"): header = get_default_header(endianess) if "blockfile_header" in signal.original_metadata: header = dict2sarray(signal.original_metadata["blockfile_header"], sarray=header) note = signal.original_metadata["blockfile_header"]["Note"] else: note = "" # The navigation and signal units are 'nm' and 'cm', respectively, so we # convert the units accordingly before saving the signal axes_manager = signal.axes_manager.deepcopy() try: axes_manager.convert_units("navigation", "nm") axes_manager.convert_units("signal", "cm") except Exception: warnings.warn( "BLO file expects cm units in signal dimensions and nm in navigation dimensions. " "Existing units could not be converted; saving " "axes scales as is. Beware that scales " "will likely be incorrect in the file.", UserWarning, ) if axes_manager.navigation_dimension == 2: NX, NY = axes_manager.navigation_shape SX = axes_manager.navigation_axes[0].scale SY = axes_manager.navigation_axes[1].scale elif axes_manager.navigation_dimension == 1: NX = axes_manager.navigation_shape[0] NY = 1 SX = axes_manager.navigation_axes[0].scale SY = SX elif axes_manager.navigation_dimension == 0: NX = NY = SX = SY = 1 DP_SZ = axes_manager.signal_shape if DP_SZ[0] != DP_SZ[1]: raise ValueError("Blockfiles require signal shape to be square!") DP_SZ = DP_SZ[0] SDP = 100.0 / axes_manager.signal_axes[0].scale offset2 = NX * NY + header["Data_offset_1"] # Based on inspected files, the DPs are stored at 16-bit boundary... # Normally, you'd expect word alignment (32-bits) ¯\_(°_o)_/¯ offset2 += offset2 % 16 header = dict2sarray( { "NX": NX, "NY": NY, "DP_SZ": DP_SZ, "SX": SX, "SY": SY, "SDP": SDP, "Data_offset_2": offset2, }, sarray=header, ) return header, note
def get_header_from_signal(signal, endianess='<'): header = get_default_header(endianess) if 'blockfile_header' in signal.original_metadata: header = dict2sarray(signal.original_metadata['blockfile_header'], sarray=header) note = signal.original_metadata['blockfile_header']['Note'] else: note = '' # The navigation and signal units are 'nm' and 'cm', respectively, so we # convert the units accordingly before saving the signal axes_manager = signal.axes_manager.deepcopy() axes_manager.convert_units('navigation', 'nm') axes_manager.convert_units('signal', 'cm') if axes_manager.navigation_dimension == 2: NX, NY = axes_manager.navigation_shape SX = axes_manager.navigation_axes[0].scale SY = axes_manager.navigation_axes[1].scale elif axes_manager.navigation_dimension == 1: NX = axes_manager.navigation_shape[0] NY = 1 SX = axes_manager.navigation_axes[0].scale SY = SX elif axes_manager.navigation_dimension == 0: NX = NY = SX = SY = 1 DP_SZ = axes_manager.signal_shape if DP_SZ[0] != DP_SZ[1]: raise ValueError('Blockfiles require signal shape to be square!') DP_SZ = DP_SZ[0] SDP = 100. / axes_manager.signal_axes[0].scale offset2 = NX * NY + header['Data_offset_1'] # Based on inspected files, the DPs are stored at 16-bit boundary... # Normally, you'd expect word alignment (32-bits) ¯\_(°_o)_/¯ offset2 += offset2 % 16 header = dict2sarray( { 'NX': NX, 'NY': NY, 'DP_SZ': DP_SZ, 'SX': SX, 'SY': SY, 'SDP': SDP, 'Data_offset_2': offset2, }, sarray=header) return header, note
def test_d2s_arrayX(): dt2 = dt + [('z', (np.uint8, 4)), ('u', (np.uint16, 4))] d = dict(z=2, u=[1, 2, 3, 4]) sa = np.zeros((4,), dtype=dt2) sa = dict2sarray(d, sarray=sa) np.testing.assert_array_equal(sa['z'], [[2, 2, 2, 2], ] * 4) np.testing.assert_array_equal(sa['u'], [[1, 2, 3, 4], ] * 4)
def test_d2s_arrayX(): dt2 = dt + [("z", (np.uint8, 4)), ("u", (np.uint16, 4))] d = dict(z=2, u=[1, 2, 3, 4]) sa = np.zeros((4,), dtype=dt2) sa = dict2sarray(d, sarray=sa) np.testing.assert_array_equal(sa["z"], [[2, 2, 2, 2]] * 4) np.testing.assert_array_equal(sa["u"], [[1, 2, 3, 4]] * 4)
def test_d2s_extra_dict_ok(): d = dict(x=5, y=10, text='abcdef', other=55) ref = np.zeros((1,), dtype=dt) ref['x'] = 5 ref['y'] = 10 ref['text'] = 'abcdef' nt.assert_equal(ref, dict2sarray(d, dtype=dt))
def test_d2s_dtype(): d = dict(x=5, y=10, text='abcdef') ref = np.zeros((1, ), dtype=dt) ref['x'] = 5 ref['y'] = 10 ref['text'] = 'abcdef' assert ref == dict2sarray(d, dtype=dt)
def test_d2s_extra_dict_ok(): d = dict(x=5, y=10, text="abcdef", other=55) ref = np.zeros((1,), dtype=dt) ref["x"] = 5 ref["y"] = 10 ref["text"] = "abcdef" nt.assert_equal(ref, dict2sarray(d, dtype=dt))
def test_d2s_extra_dict_ok(): d = dict(x=5, y=10, text='abcdef', other=55) ref = np.zeros((1, ), dtype=dt) ref['x'] = 5 ref['y'] = 10 ref['text'] = 'abcdef' assert ref == dict2sarray(d, dtype=dt)
def test_d2s_dtype(): d = dict(x=5, y=10, text='abcdef') ref = np.zeros((1,), dtype=dt) ref['x'] = 5 ref['y'] = 10 ref['text'] = 'abcdef' assert ref == dict2sarray(d, dtype=dt)
def get_header_from_signal(signal, endianess='<'): header = get_default_header(endianess) if 'blockfile_header' in signal.original_metadata: header = dict2sarray(signal.original_metadata['blockfile_header'], sarray=header) note = signal.original_metadata['blockfile_header']['Note'] else: note = '' # The navigation and signal units are 'nm' and 'cm', respectively, so we # convert the units accordingly before saving the signal axes_manager = signal.axes_manager.deepcopy() axes_manager.convert_units('navigation', 'nm') axes_manager.convert_units('signal', 'cm') if axes_manager.navigation_dimension == 2: NX, NY = axes_manager.navigation_shape SX = axes_manager.navigation_axes[0].scale SY = axes_manager.navigation_axes[1].scale elif axes_manager.navigation_dimension == 1: NX = axes_manager.navigation_shape[0] NY = 1 SX = axes_manager.navigation_axes[0].scale SY = SX elif axes_manager.navigation_dimension == 0: NX = NY = SX = SY = 1 DP_SZ = axes_manager.signal_shape if DP_SZ[0] != DP_SZ[1]: raise ValueError('Blockfiles require signal shape to be square!') DP_SZ = DP_SZ[0] SDP = 100. / axes_manager.signal_axes[0].scale offset2 = NX * NY + header['Data_offset_1'] # Based on inspected files, the DPs are stored at 16-bit boundary... # Normally, you'd expect word alignment (32-bits) ¯\_(°_o)_/¯ offset2 += offset2 % 16 header = dict2sarray({ 'NX': NX, 'NY': NY, 'DP_SZ': DP_SZ, 'SX': SX, 'SY': SY, 'SDP': SDP, 'Data_offset_2': offset2, }, sarray=header) return header, note
def test_d2s_type_cast_ok(): d = dict(x="34", text=55) ref = np.zeros((1,), dtype=dt) ref["x"] = 34 ref["y"] = 0 ref["text"] = "55" nt.assert_equal(ref, dict2sarray(d, dtype=dt))
def test_d2s_type_cast_ok(): d = dict(x='34', text=55) ref = np.zeros((1, ), dtype=dt) ref['x'] = 34 ref['y'] = 0 ref['text'] = '55' assert ref == dict2sarray(d, dtype=dt)
def test_d2s_type_cast_ok(): d = dict(x='34', text=55) ref = np.zeros((1,), dtype=dt) ref['x'] = 34 ref['y'] = 0 ref['text'] = '55' nt.assert_equal(ref, dict2sarray(d, dtype=dt))
def test_d2s_sarray(): d = dict(x=5, y=10, text='abcdef') base = np.zeros((1, ), dtype=dt) base['x'] = 65 base['text'] = 'gg' ref = np.zeros((1, ), dtype=dt) ref['x'] = 5 ref['y'] = 10 ref['text'] = 'abcdef' assert ref == dict2sarray(d, sarray=base)
def test_d2s_partial_sarray(): d = dict(text='abcdef') base = np.zeros((1, ), dtype=dt) base['x'] = 65 base['text'] = 'gg' ref = np.zeros((1, ), dtype=dt) ref['x'] = 65 ref['y'] = 0 ref['text'] = 'abcdef' assert ref == dict2sarray(d, sarray=base)
def test_d2s_partial_sarray(): d = dict(text="abcdef") base = np.zeros((1,), dtype=dt) base["x"] = 65 base["text"] = "gg" ref = np.zeros((1,), dtype=dt) ref["x"] = 65 ref["y"] = 0 ref["text"] = "abcdef" nt.assert_equal(ref, dict2sarray(d, sarray=base))
def test_d2s_sarray(): d = dict(x=5, y=10, text='abcdef') base = np.zeros((1,), dtype=dt) base['x'] = 65 base['text'] = 'gg' ref = np.zeros((1,), dtype=dt) ref['x'] = 5 ref['y'] = 10 ref['text'] = 'abcdef' assert ref == dict2sarray(d, sarray=base)
def test_d2s_partial_sarray(): d = dict(text='abcdef') base = np.zeros((1,), dtype=dt) base['x'] = 65 base['text'] = 'gg' ref = np.zeros((1,), dtype=dt) ref['x'] = 65 ref['y'] = 0 ref['text'] = 'abcdef' nt.assert_equal(ref, dict2sarray(d, sarray=base))
def test_d2s_fail(): d = dict(x=5, y=10, text='abcdef') with pytest.raises(ValueError): dict2sarray(d)
def test_d2s_array2(): d = dict(x=2, y=[1, 2, 3, 4]) sa = np.zeros((4, ), dtype=dt) sa = dict2sarray(d, sarray=sa) np.testing.assert_array_equal(sa['x'], [2, 2, 2, 2]) np.testing.assert_array_equal(sa['y'], [1, 2, 3, 4])
def test_d2s_array1(): dt2 = dt + [('z', (np.uint8, 4)), ('u', (np.uint16, 4))] d = dict(z=2, u=[1, 2, 3, 4]) sa = dict2sarray(d, dtype=dt2) np.testing.assert_array_equal(sa['z'][0], [2, 2, 2, 2]) np.testing.assert_array_equal(sa['u'][0], [1, 2, 3, 4])
def test_d2s_string_cut(): d = dict(text="Testerstring") sa = dict2sarray(d, dtype=dt) nt.assert_equal(sa["text"][0], "Tester")
def test_d2s_string_cut(): d = dict(text='Testerstring') sa = dict2sarray(d, dtype=dt) nt.assert_equal(sa['text'][0], 'Tester')
def test_d2s_string_cut(): d = dict(text='Testerstring') sa = dict2sarray(d, dtype=dt) assert sa['text'][0] == b'Tester'
def test_d2s_type_cast_invalid(): d = dict(x='Test') dict2sarray(d, dtype=dt)
def test_d2s_type_cast_invalid(): d = dict(x='Test') with pytest.raises(ValueError): dict2sarray(d, dtype=dt)
def test_d2s_array2(): d = dict(x=2, y=[1, 2, 3, 4]) sa = np.zeros((4,), dtype=dt) sa = dict2sarray(d, sarray=sa) np.testing.assert_array_equal(sa['x'], [2, 2, 2, 2]) np.testing.assert_array_equal(sa['y'], [1, 2, 3, 4])
def test_d2s_fail(): d = dict(x=5, y=10, text='abcdef') dict2sarray(d)