def run_around_tests(): # Code that will run before your test, for example: session.clear_session() # A test function will be run at this point yield # Code that will run after your test, for example: session.clear_session()
def test_wrong_hash(): tempdir = pathlib.Path(tempfile.mkdtemp(prefix="test_shapeout2_session_")) spath = tempdir / "session.so2" # custom path to measurement p0 = pathlib.Path(__file__).parent / "data" / "calibration_beads_47.rtdc" pp = pathlib.Path(tempdir) / "calibration_beads_47.rtdc" shutil.copy(p0, pp) pl = make_pipeline(paths=[pp]) session.save_session(spath, pl) session.clear_session(pl) # clear session before opening h5 file rw session.hash_file_partially.cache_clear() # force recomputation of hashes # modify the file with h5py.File(pp, mode="a") as h5: h5.attrs["setup:medium"] = "unknown" # opening modified file should just work if the path matches pl2 = session.open_session(spath) session.clear_session(pl2) # but when the directory changes, the hash is checked other = tempdir / "other" other.mkdir() pp.rename(other / pp.name) try: session.open_session(spath, search_paths=[other]) except session.DataFileNotFoundError as e: assert pp in e.missing_paths else: assert False, "should have raised an error!"
def test_relative_paths(): tempdir = pathlib.Path(tempfile.mkdtemp(prefix="test_shapeout2_session_")) # custom path for data p0 = pathlib.Path(__file__).parent / "data" / "calibration_beads_47.rtdc" datadir = tempdir / "data" datadir.mkdir() pp = datadir / "calibration_beads_47.rtdc" shutil.copy(p0, pp) # custom path for session sessiondir = tempdir / "session" sessiondir.mkdir() spath = sessiondir / "session.so2" # pipeline pl = make_pipeline(paths=[pp]) session.save_session(spath, pl) session.clear_session(pl) # new session directory new_sessiondir = tempdir / "new" / "path" / "abracadabra" new_sessiondir.mkdir(parents=True) new_spath = new_sessiondir / spath.name spath.rename(new_spath) # new path directory (same relative path) new_datadir = tempdir / "new" / "path" / "data" new_datadir.mkdir(parents=True) new_pp = new_datadir / pp.name pp.rename(new_pp) # and load it (without search_paths as arguments) session.open_session(new_spath)
def test_save_all_polygon_filters_issue_101(): pl = make_pipeline() # add a polygon filter ds = pl.get_dataset(0) pf1 = dclab.PolygonFilter( axes=("deform", "area_um"), points=[ [ds["deform"].min(), ds["area_um"].min()], [ds["deform"].min(), ds["area_um"].mean()], [ds["deform"].mean(), ds["area_um"].mean()], ], name="Triangle of Minimum", ) pf2_state = dclab.PolygonFilter( axes=("deform", "area_um"), points=[ [ds["deform"].max(), ds["area_um"].max()], [ds["deform"].max(), ds["area_um"].mean()], [ds["deform"].mean(), ds["area_um"].mean()], ], name="Triangle of Maximum", ).__getstate__() pl.filters[0].polylist.append(pf1.unique_id) old_state = pl.__getstate__() tempdir = pathlib.Path(tempfile.mkdtemp(prefix="test_shapeout2_session_")) spath = tempdir / "session.so2" session.save_session(spath, pl) assert len(dclab.PolygonFilter.instances) == 2 session.clear_session(pl) assert len(dclab.PolygonFilter.instances) == 0 # currently, there may only be one pipeline session.open_session(spath, pl) new_state = pl.__getstate__() # This is the actual test for issue #101 assert len(dclab.PolygonFilter.instances) == 2 # This is a sanity check assert equal_state(old_state, new_state) # This is another sanity check pf2_id = pf2_state["identifier"] assert equal_state( pf2_state, dclab.PolygonFilter.get_instance_from_id(pf2_id).__getstate__())
def test_missing_path_in_session(): tempdir = pathlib.Path(tempfile.mkdtemp(prefix="test_shapeout2_session_")) spath = tempdir / "session.so2" # custom path to measurement p0 = pathlib.Path(__file__).parent / "data" / "calibration_beads_47.rtdc" pp = tempdir / "calibration_beads_47.rtdc" shutil.copy(p0, pp) pl = make_pipeline(paths=[pp]) session.save_session(spath, pl) session.clear_session(pl) # rename the file pc = pp.with_name("calibration_beads_47_moved.rtdc") pp.rename(pc) # load bad session try: session.open_session(spath) except session.DataFileNotFoundError as e: assert pp in e.missing_paths else: assert False, "should have raised an error!" # try again with proper search path pl3 = session.open_session(spath, search_paths=[pc]) session.clear_session(pl3) # try again with a directory as search path other = tempdir / "other" other.mkdir() pc.rename(other / pp.name) # must have same name as `pp` pl4 = session.open_session(spath, search_paths=[other]) session.clear_session(pl4)