def test_non_tiff(): # test loading a few other image formats. We have some in the docs # director, so just use them import holopy root = os.path.split(os.path.split(holopy.__file__)[0])[0] doc_images = os.path.join(root, 'docs', 'source', 'images') load(os.path.join(doc_images, 'image_5Particle_Hologram.jpg')) load(os.path.join(doc_images, 'ReconVolume_mlab_5Particle_Hologram.png'))
def test_serialization(): par_s = Sphere(center=(Uniform(0, 1e-5, guess=.567e-5), Uniform(0, 1e-5, .567e-5), Uniform(1e-5, 2e-5)), r=Uniform(1e-8, 1e-5, 8.5e-7), n=Uniform(1, 2, 1.59)) alpha = Uniform(.1, 1, .6, 'alpha') schema = update_metadata(detector_grid(shape=100, spacing=.1151e-6), illum_wavelen=.66e-6, medium_index=1.33, illum_polarization=(1, 0)) model = AlphaModel(par_s, medium_index=schema.medium_index, illum_wavelen=schema.illum_wavelen, alpha=alpha) holo = calc_holo(schema, model.scatterer.guess, scaling=model.alpha.guess) result = fix_flat(NmpfitStrategy().fit(model, holo)) temp = tempfile.NamedTemporaryFile(suffix='.h5', delete=False) with warnings.catch_warnings(): warnings.simplefilter('ignore') save(temp.name, result) loaded = load(temp.name) assert_obj_close(result, loaded, context='serialized_result')
def test_serialization(): par_s = Sphere(center=(par(.567e-5, [0, 1e-5]), par(.576e-6, [0, 1e-5]), par(15e-6, [1e-5, 2e-5])), r=par(8.5e-7, [1e-8, 1e-5]), n=par(1.59, [1, 2])) alpha = par(.6, [.1, 1], 'alpha') schema = ImageSchema(shape=100, spacing=.1151e-6, optics=Optics(.66e-6, 1.33)) model = Model(par_s, Mie.calc_holo, alpha=alpha) holo = Mie.calc_holo(model.scatterer.guess, schema, model.alpha.guess) result = fit(model, holo) temp = tempfile.NamedTemporaryFile() save(temp, result) temp.flush() temp.seek(0) loaded = load(temp) assert_obj_close(result, loaded, context='serialized_result')
def test_image_io(): holo = get_example_data('image0001.yaml') t = tempfile.mkdtemp() filename = os.path.join(t, 'image0001.tif') save(filename, holo) l = load(filename) assert_obj_close(l, holo) # check that it defaults to saving as tif filename = os.path.join(t, 'image0002') save_image(filename, holo) l = load(filename + '.tif') assert_obj_close(l, holo) # check saving 16 bit filename = os.path.join(t, 'image0003') save_image(filename, holo, scaling=None, depth=16) l = load(filename + '.tif') assert_obj_close(l, holo) # test that yaml save works corretly with a string instead of a file filename = os.path.join(t, 'image0001.yaml') save(filename, holo) loaded = load(filename) assert_obj_close(loaded, holo) f = get_example_data_path('image0001.yaml') spacing = .1 optics = Optics(.66, 1.33, (1, 0)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') h = load(f, spacing=spacing, optics=optics) assert_obj_close(h.optics, optics) assert_equal(h.spacing, spacing) assert_equal(len(w), 1) assert "Overriding spacing and optics of loaded yaml" in w[-1].message with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') h = load(f, optics=optics) assert_obj_close(h.optics, optics) assert_equal(h.spacing, holo.spacing) assert_equal(len(w), 1) assert ("WARNING: overriding optics of loaded yaml without " "overriding spacing, this is probably incorrect." in w[-1].message) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') h = load(f, spacing=spacing) assert_obj_close(h.optics, holo.optics) assert_equal(h.spacing, spacing) assert_equal(len(w), 1) assert ("WARNING: overriding spacing of loaded yaml without " "overriding optics, this is probably incorrect." in w[-1].message) shutil.rmtree(t)
def test_save_images(self): filenames = [ os.path.join(self.tempdir, f) for f in ['dummy_filename_1.tif', 'dummy_filename_2.tif'] ] save_images(filenames, self.holograms, scaling=None) for ground_truth, filename in zip(self.holograms, filenames): loaded = load(filename) self.assertTrue(np.all(loaded.values == ground_truth.values))
def test_load_optics(): optics_yaml = """wavelen: 785e-9 polarization: [1.0, 0] divergence: 0 pixel_size: [6.8e-6, 6.8e-6] pixel_scale: [3.3e-7, 3.3e-7]""" t = tempfile.TemporaryFile() t.write(optics_yaml) t.seek(0) o = Optics(**load(t)) assert_obj_close(o, Optics(wavelen=7.85e-07, polarization=[1.0, 0.0], divergence=0, pixel_size=[6.8e-06, 6.8e-06], pixel_scale=[3.3e-07, 3.3e-07]))
def test_image_io(): holo = get_example_data('image0001.yaml') t = tempfile.mkdtemp() filename = os.path.join(t, 'image0001.tif') save(filename, holo) l = load(filename) assert_obj_close(l, holo) # check that it defaults to saving as tif filename = os.path.join(t, 'image0002') save_image(filename, holo) l = load(filename+'.tif') assert_obj_close(l, holo) # test that yaml save works corretly with a string instead of a file filename = os.path.join(t, 'image0001.yaml') save(filename, holo) loaded = load(filename) assert_obj_close(loaded, holo) f = get_example_data_path('image0001.yaml') spacing = .1 optics = Optics(.66, 1.33, (1,0)) with warnings.catch_warnings(record =True) as w: warnings.simplefilter('always') h = load(f, spacing = spacing, optics = optics) assert_obj_close(h.optics, optics) assert_equal(h.spacing, spacing) assert_equal(len(w), 1) assert "Overriding spacing and optics of loaded yaml" in w[-1].message with warnings.catch_warnings(record =True) as w: warnings.simplefilter('always') h = load(f, optics = optics) assert_obj_close(h.optics, optics) assert_equal(h.spacing, holo.spacing) assert_equal(len(w), 1) assert ("WARNING: overriding optics of loaded yaml without " "overriding spacing, this is probably incorrect." in w[-1].message) with warnings.catch_warnings(record =True) as w: warnings.simplefilter('always') h = load(f, spacing = spacing) assert_obj_close(h.optics, holo.optics) assert_equal(h.spacing, spacing) assert_equal(len(w), 1) assert ("WARNING: overriding spacing of loaded yaml without " "overriding optics, this is probably incorrect." in w[-1].message) shutil.rmtree(t)
def test_load_optics(): optics_yaml = """wavelen: 785e-9 polarization: [1.0, 0] divergence: 0 pixel_size: [6.8e-6, 6.8e-6] pixel_scale: [3.3e-7, 3.3e-7]""" t = tempfile.TemporaryFile() t.write(optics_yaml) t.seek(0) o = Optics(**load(t)) assert_obj_close( o, Optics(wavelen=7.85e-07, polarization=[1.0, 0.0], divergence=0, pixel_size=[6.8e-06, 6.8e-06], pixel_scale=[3.3e-07, 3.3e-07]))
def test_serialization(): par_s = Sphere(center = (par(.567e-5, [0, 1e-5]), par(.576e-6, [0, 1e-5]), par(15e-6, [1e-5, 2e-5])), r = par(8.5e-7, [1e-8, 1e-5]), n = par(1.59, [1,2])) alpha = par(.6, [.1, 1], 'alpha') schema = ImageSchema(shape = 100, spacing = .1151e-6, optics = Optics(.66e-6, 1.33)) model = Model(par_s, Mie.calc_holo, alpha=alpha) holo = Mie.calc_holo(model.scatterer.guess, schema, model.alpha.guess) result = fit(model, holo) temp = tempfile.NamedTemporaryFile() save(temp, result) temp.flush() temp.seek(0) loaded = load(temp) assert_obj_close(result, loaded, context = 'serialized_result')
def test_load_func_from_save_image_func_with_scaling(self): filename = os.path.join(self.tempdir, 'image0006') save_image(filename, self.holo, scaling='auto') loaded = load(filename + '.tif') assert_obj_close(np.around(loaded), self.holo)
def test_load_func_from_save_image_func(self): filename = os.path.join(self.tempdir, 'image0006') save_image(filename, self.holo, scaling=None) loaded = load(filename + '.tif') assert_obj_close(loaded, self.holo)
def test_save_load_h5(self): filename = os.path.join(self.tempdir, 'image0001') save(filename, self.holo) loaded = load(filename) assert_obj_close(loaded, self.holo)
from __future__ import print_function import numpy as np import holopy as hp from holopy.propagation import propagate from holopy.core import load import matplotlib.pyplot as plt import time optics = hp.core.Optics(wavelen=0.405, index=1.33, polarization=[1.0, 0.0]) holo = load("jerichoObject.bmp", spacing=6e-6, optics=optics) bg = load("jerichoRef.bmp", spacing=6e-6, optics=optics) holo = holo - bg rec_vol = propagate(holo, np.linspace(245e-6, 250e-6, 5)) plt.ion() fig = plt.figure() ax = fig.gca() for i in xrange(rec_vol.shape[2]): print(i) # name='figure%d.png'%(i,) # plt.savefig(name,bbox_inches=0) plt.clf() plt.imshow(rec_vol[..., i].real, cmap=plt.cm.Greys_r) fig.canvas.draw() time.sleep(1)