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 convert_image_yaml(yaml): h = serialize.load(yaml) d = Image(h, spacing=h.spacing, medium_index=h.optics.index, illum_wavelen=h.optics.wavelen, illum_polarization=h.optics.polarization) name, ext = os.path.splitext(yaml) save(name, d)
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_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_save_load_h5(self): filename = os.path.join(self.tempdir, 'image0001') save(filename, self.holo) loaded = load(filename) assert_obj_close(loaded, self.holo)