def test_write(file_format_examples): t1 = read_topography(f'{file_format_examples}/di1.di') t2 = read_topography(f'{file_format_examples}/example.opd') t3 = read_topography(f'{file_format_examples}/example2.txt') c = SurfaceContainer([t1, t2, t3]) with tempfile.TemporaryFile() as fobj: c.to_zip(fobj) c2, = read_container(fobj) assert len(c2) == 3 assert c2[0].nb_grid_pts == t1.nb_grid_pts assert c2[1].nb_grid_pts == t2.nb_grid_pts assert c2[2].nb_grid_pts == t3.nb_grid_pts assert_allclose(c2[0].physical_sizes, t1.physical_sizes) assert_allclose(c2[1].physical_sizes, t2.physical_sizes) assert_allclose(c2[2].physical_sizes, t3.physical_sizes) assert c2[0].info['unit'] == t1.info['unit'] assert c2[1].info['unit'] == t2.info['unit'] assert c2[2].info['unit'] == t3.info['unit']
def test_opdx_txt_consistency(): t_opdx = read_topography(os.path.join(DATADIR, 'opdx2.OPDx')) t_txt = read_topography(os.path.join(DATADIR, 'opdx2.txt')) print(t_opdx.pixel_size) assert abs(t_opdx.pixel_size[0] / t_opdx.pixel_size[1] - 1) < 1e-3 assert abs(t_txt.pixel_size[0] / t_txt.pixel_size[1] - 1) < 1e-3 ratio_ref = t_opdx.physical_sizes[1] / t_opdx.physical_sizes[0] assert (t_txt.physical_sizes[1] / t_txt.physical_sizes[0] - ratio_ref) / ratio_ref < 1e-3 assert t_opdx.nb_grid_pts == t_txt.nb_grid_pts # opd file's heights are in µm, txt file's heights in m assert t_opdx.info['unit'] == 'µm' assert t_txt.info['unit'] == 'm' npt.assert_allclose(t_opdx.detrend().heights(), t_txt.detrend().scale(1e6).heights(), rtol=1e-6, atol=1e-3) if False: import matplotlib.pyplot as plt fig, ax = plt.subplots() plt.colorbar(ax.imshow(t_txt.scale(1e9).heights())) fig2, ax2 = plt.subplots() plt.colorbar(ax2.imshow(t_opdx.heights())) plt.show(block=True)
def test_opdx_txt_absolute_consistency(): t_opdx = read_topography(os.path.join(DATADIR, 'opdx2.OPDx')) t_txt = read_topography(os.path.join(DATADIR, 'opdx2.txt')) print(t_opdx.pixel_size) assert ((abs(t_opdx.pixel_size - t_txt.pixel_size) / t_opdx.pixel_size) < 1e-3).all() assert ((abs(t_opdx.physical_sizes - t_txt.physical_sizes) / t_opdx.physical_sizes) < 1e-3).all() assert t_opdx.nb_grid_pts == t_txt.nb_grid_pts npt.assert_all_close(t_opdx.heights, t_txt.heights)
def test_to_netcdf(fn): """Test that files can be stored as NetCDF and that reading then gives an identical topography object""" if fn in explicit_physical_sizes: t = read_topography(fn, physical_sizes=(1, 1)) else: t = read_topography(fn) with tempfile.TemporaryDirectory() as d: tmpfn = f'{d}/netcdf_representation.nc' t.to_netcdf(tmpfn) t2 = read_topography(tmpfn) assert t == t2
def test_brute_force_vs_fft(): t = read_topography(os.path.join(DATADIR, 'example.xyz')) r, A = t.detrend().autocorrelation_from_profile() r2, A2 = t.detrend().autocorrelation_from_profile(algorithm='brute-force', distances=r, nb_interpolate=5) x = A[1:] / A2[1:] assert np.alltrue(np.logical_and(x > 0.98, x < 1.01))
def test_reader_arguments(self): """Check whether all readers have channel, physical_sizes and height_scale_factor arguments. Also check whether we can execute `topography` multiple times for all readers""" physical_sizes0 = (1.2, 1.3) for fn in self.text_example_file_list + self.binary_example_file_list: # Test open -> topography r = open_topography(fn) physical_sizes = None if r.channels[0].dim == 1 \ else physical_sizes0 t = r.topography(channel_index=0, physical_sizes=physical_sizes, height_scale_factor=None) if physical_sizes is not None: self.assertEqual(t.physical_sizes, physical_sizes) # Second call to topography t2 = r.topography(channel_index=0, physical_sizes=physical_sizes, height_scale_factor=None) if physical_sizes is not None: self.assertEqual(t2.physical_sizes, physical_sizes) assert_array_equal(t.heights(), t2.heights()) # Test read_topography t = read_topography(fn, channel_index=0, physical_sizes=physical_sizes, height_scale_factor=None) if physical_sizes is not None: self.assertEqual(t.physical_sizes, physical_sizes)
def test_di_orientation(): # # topography.heights() should return an array where # - first index corresponds to x with lowest x in top rows and largest x # in bottom rows # - second index corresponds to y with lowest y in left column and largest # x in right column # # This is given when reading di.txt, see test elsewhere. # # The heights should be equal to those from txt reader di_fn = os.path.join(DATADIR, "di1.di") di_t = read_topography(di_fn) with pytest.deprecated_call(): assert di_t.info['unit'] == 'nm' # # Check values in 4 corners, this should fix orientation # di_heights = di_t.heights() assert pytest.approx(di_heights[0, 0], abs=1e-3) == 6.060 assert pytest.approx(di_heights[0, -1], abs=1e-3) == 2.843 assert pytest.approx(di_heights[-1, 0], abs=1e-3) == -9.740 assert pytest.approx(di_heights[-1, -1], abs=1e-3) == -30.306
def test_reader_arguments(fn): """Check whether all readers have channel, physical_sizes and height_scale_factor arguments. Also check whether we can execute `topography` multiple times for all readers""" physical_sizes0 = (1.2, 1.3) # Test open -> topography r = open_topography(fn) physical_sizes = None if r.channels[ 0].physical_sizes is not None else physical_sizes0 t = r.topography(channel_index=0, physical_sizes=physical_sizes, height_scale_factor=None) if physical_sizes is not None: assert t.physical_sizes == physical_sizes # Second call to topography t2 = r.topography(channel_index=0, physical_sizes=physical_sizes, height_scale_factor=None) if physical_sizes is not None: assert t2.physical_sizes == physical_sizes assert_array_equal(t.heights(), t2.heights()) # Test read_topography t = read_topography(fn, channel_index=0, physical_sizes=physical_sizes, height_scale_factor=None) if physical_sizes is not None: assert t.physical_sizes == physical_sizes
def test_brute_force_vs_fft(): t = read_topography(os.path.join(DATADIR, 'example.xyz')) q, A = t.detrend().power_spectrum_from_profile(window="None") q2, A2 = t.detrend().power_spectrum_from_profile(algorithm='brute-force', wavevectors=q, nb_interpolate=5, window="None") length = len(A2) x = A[1:length // 16] / A2[1:length // 16] assert np.alltrue(np.logical_and(x > 0.90, x < 1.35))
def test_detrended(self): t1 = read_topography(os.path.join(DATADIR, 'di1.di')) dt1 = t1.detrend(detrend_mode="center") t1_pickled = pickle.dumps(t1) t1_unpickled = pickle.loads(t1_pickled) dt2 = t1_unpickled.detrend(detrend_mode="center") self.assertAlmostEqual(dt1.coeffs[0], dt2.coeffs[0])
def test_txt_example(): t = read_topography(os.path.join(DATADIR, 'txt_example.txt')) assert t.physical_sizes == (1e-6, 0.5e-6) assert t.nb_grid_pts == (6, 3) expected_heights = np.array([ [1.0e-007, 0.0e-007, 0.0e-007, 0.0e-007, 0.0e-007, 0.0e-007], [0.5e-007, 0.5e-007, 0.0e-008, 0.0e-008, 0.0e-008, 0.0e-008], [0.0e-007, 0.0e-007, 0.0e-007, 0.0e-007, 0.0e-007, 0.0e-007], ]).T np.testing.assert_allclose(t.heights(), expected_heights)
def test_opdx_txt_heights_lateral_consistency(): t_txt = read_topography(os.path.join(DATADIR, 'opdx2.txt')) assert t_txt.info["unit"] == "m" # the radius of the sphere should be 250 µm R = 250 * 1e-6 rhoxx, rhoyy, rhoxy = t_txt.detrend(detrend_mode="curvature").curvatures assert (1 / rhoxx - R) / R < 0.01 assert (1 / rhoyy - R) / R < 0.01
def test_read_filestream(self): """ The reader has to work when the file was already opened as binary for it to work in topobank. """ file_path = os.path.join(DATADIR, 'opdx2.OPDx') try: read_topography(file_path) except Exception as e: self.fail("read_topography() raised an exception (not passing " "a file stream)!" + str(e)) try: with open(file_path, 'r') as f: read_topography(f) except Exception as e: self.fail("read_topography() raised an exception (passing a " "non-binary file stream)!" + str(e)) finally: f.close() try: f = open(file_path, 'rb') read_topography(f) except Exception as e: self.fail("read_topography() raised an exception (passing a " "binary file stream)!" + str(e)) finally: f.close()
def test_read_filestream(self): """ The reader has to work when the file was already opened as binary for it to work in TopoBank. """ try: read_topography(self.file_path) except Exception as e: self.fail("read_topography() raised an exception (not passing a " "file stream)!" + str(e)) try: f = open(self.file_path, 'r') read_topography(f) except Exception as e: self.fail("read_topography() raised an exception (passing a " "non-binary file stream)!" + str(e)) finally: f.close() try: f = open(self.file_path, 'rb') read_topography(f) except Exception as e: self.fail("read_topography() raised an exception (passing a " "binary file stream)!" + str(e)) finally: f.close()
def test_short_cutoff(): t = read_topography(os.path.join(DATADIR, 'example.xyz')) q1, C1 = t.detrend().power_spectrum_from_profile() q2, C2 = t.detrend().power_spectrum_from_profile(short_cutoff=np.min) q3, C3 = t.detrend().power_spectrum_from_profile(short_cutoff=np.max) assert len(q3) < len(q1) assert len(q1) < len(q2) assert np.max(q3) < np.max(q1) assert np.max(q1) < np.max(q2) x, y = t.positions_and_heights() assert_almost_equal(np.max(q1), 2 * np.pi / np.mean(np.diff(x))) assert abs(np.max(q2) - 2 * np.pi / np.min(np.diff(x))) < 0.03 assert abs(np.max(q3) - 2 * np.pi / np.max(np.diff(x))) < 0.02
### import matplotlib.pyplot as plt ### # This is the elastic contact modulus, E*. E_s = 2 ### # This is the physical physical_sizes of the surfaces. sx, sy = 1, 1 # Read the two contacting surfacs. surface1 = read_topography('surface1.out', physical_sizes=(sx, sy)) surface2 = read_topography('surface2.out', physical_sizes=(sx, sy)) print('RMS heights of surfaces = {} {}'.format( surface1.rms_height_from_area(), surface2.rms_height_from_area())) # This is the grid nb_grid_pts of the two surfaces. nx, ny = surface1.nb_grid_pts # TranslatedSurface knows how to translate a surface into some direction. translated_surface1 = TranslatedTopography(surface1) # This is the compound of the two surfaces, effectively creating a surface # that is the difference between the two profiles. compound_surface = CompoundTopography(translated_surface1, surface2)
def test_scanning_probe_reliability_cutoff(file_format_examples): surf = read_topography(os.path.join(file_format_examples, 'di1.di')) np.testing.assert_allclose(surf.scanning_probe_reliability_cutoff(40), 91.79698634551458)
logger.pr('size_unit = {}'.format(arguments.size_unit)) logger.pr('height_fac = {}'.format(arguments.height_fac)) logger.pr('height_unit = {}'.format(arguments.height_unit)) logger.pr('pentol = {}'.format(arguments.pentol)) logger.pr('contact-fn = {}'.format(arguments.contact_fn)) logger.pr('pressure-fn = {}'.format(arguments.pressure_fn)) logger.pr('displ-fn = {}'.format(arguments.displ_fn)) logger.pr('gap-fn = {}'.format(arguments.gap_fn)) logger.pr('log-fn = {}'.format(arguments.log_fn)) logger.pr('netcdf-fn = {}'.format(arguments.netcdf_fn)) ### # Read a surface topography from a text file. Returns a SurfaceTopography.SurfaceTopography # object. surface = read_topography(arguments.filename, physical_sizes=arguments.physical_sizes) # Set the *physical* physical_sizes of the surface. We here set it to equal the shape, # i.e. the nb_grid_pts of the surface just open_topography. Size is returned by surface.physical_sizes # and can be unknown, i.e. *None*. if arguments.size_unit is not None: surface.info['unit'] = arguments.size_unit if 'unit' not in surface.info: surface.info['unit'] = 'N/A' if arguments.height_fac is not None or arguments.height_unit is not None: fac = 1.0 if arguments.height_fac is not None: fac *= arguments.height_fac if arguments.height_unit is not None: fac *= unit_to_meters[arguments.height_unit]/unit_to_meters[surface.info['unit']] logger.pr('Rescaling surface heights by {}.'.format(fac)) surface = surface.scale(fac)
def test_di_date(): t = read_topography(os.path.join(DATADIR, 'di1.di')) assert t.info['acquisition_time'] == datetime.datetime( 2016, 1, 12, 9, 57, 48)
def test_di_date(file_format_examples): t = read_topography(os.path.join(file_format_examples, 'di1.di')) assert t.info['acquisition_time'] == str( datetime.datetime(2016, 1, 12, 9, 57, 48))
def test_cannot_detect_file_format_on_txt(): with pytest.raises(CannotDetectFileFormat): read_topography(os.path.join(DATADIR, 'nonsense_txt_file.txt'))
def test_uniform_stylus(): t = read_topography(os.path.join(DATADIR, 'example7.txt')) assert t.is_uniform