def requirements(): def it_returns_required_elems(): userdata = dict(some_key=1) test_s = s( s.is_dict( all_required=True, elems=dict( a=s.is_int(), b=s.is_float(help="A float"), c=s.is_number(), d=s.is_str(userdata=userdata), e=s.is_list(), f=s.is_dict(all_required=True, elems=dict(d=s.is_int(), e=s.is_int())), ), )) reqs = test_s.requirements() assert reqs == [ ("a", int, None, None), ("b", float, "A float", None), ("c", float, None, None), ("d", str, None, userdata), ("e", list, None, None), ("f", dict, None, None), ] def it_returns_none_on_a_non_dict_schema(): test_s = s(s.is_str()) reqs = test_s.requirements() assert reqs == [] zest()
def zest_cache(): folder = local.path(local.env["ERISYON_TMP"]) / "cache" called = 0 @cache() def __test_cacheme(a, b): nonlocal called called += 1 return f"a={a} b={b}" def it_caches_to_correct_place(): nonlocal called cache_glob = "cache_wrapper___test_cacheme*" for f in folder // cache_glob: f.delete() assert len(list(folder // cache_glob)) == 0 called = 0 __test_cacheme(1, 2) assert len(list(folder // cache_glob)) == 1 assert called == 1 called = 0 __test_cacheme(1, 2) assert len(list(folder // cache_glob)) == 1 assert called == 0 zest()
def it_permutes_labels_and_proteases(): def it_defaults_arguments_to_self(): gen = gen_klass(n_edmans=1, label_set=["A,B,C:2", "C"], protease=["p0", "p1"]) perms = list(gen.run_parameter_permutator()) # assert perms == [ # ("p0", ("A", "B")), # ("p0", ("A", "C")), # ("p0", ("B", "C")), # ("p0", ("C",)), # ("p1", ("A", "B")), # ("p1", ("A", "C")), # ("p1", ("B", "C")), # ("p1", ("C",)), # ] # def it_accepts_arguments(): # gen = gen_klass(n_edmans=1, protease=[], label_set=[]) # perms = list( # gen.protease_labels_permutator( # label_set=["A", "B"], protease=["p0", "p1"] # ) # ) # assert perms == [("p0", ("A",)), ("p0", ("B",)), ("p1", ("A",)), ("p1", ("B",))] # # def it_handles_protease_none(): # gen = gen_klass(n_edmans=1, protease=[], label_set=[]) # perms = list( # gen.protease_labels_permutator(label_set=["A", "B"], protease=None) # ) # assert perms == [(None, ("A",)), (None, ("B",))] zest()
def it_cleaves_with_multiple_proteases(): df, seqstrs = (None, ) * 2 def _before(): nonlocal df, seqstrs df = _proteolyze(_pro_seq_df("TBKCDPEF"), protease_list) seqstrs = seqstrs_from_proteolyze(df) def it_picks_up_first(): assert seqstrs == ["TBK", "CDP", "EF"] def it_assigns_offsets(): assert df.pep_offset_in_pro.values.tolist() == list(range(8)) def it_handles_cut_at_end(): # end cut is 2nd protease df = _proteolyze(_pro_seq_df("TBKCDP"), protease_list) seqstrs = seqstrs_from_proteolyze(df) assert seqstrs == ["TBK", "CDP"] # end cut is 1st protease df = _proteolyze(_pro_seq_df("TBPCDK"), protease_list) seqstrs = seqstrs_from_proteolyze(df) assert seqstrs == ["TBP", "CDK"] def it_handles_no_cut(): df = _proteolyze(_pro_seq_df("TBCD"), protease_list) seqstrs = seqstrs_from_proteolyze(df) assert seqstrs == ["TBCD"] zest()
def zest_raises(): def it_catches_raises(): with zest.raises(ValueError) as e: raise ValueError("test") assert isinstance(e, TrappedException) and isinstance( e.exception, ValueError) def it_checks_properties_of_exception(): class MyException(Exception): def __init__(self, foo): self.foo = foo def it_passes_if_property_found(): with zest.raises(MyException, in_foo="bar") as e: raise MyException(foo="bar") def it_fails_if_property_not_found(): # Tricky test -- using "with zest.raises()" to catch the # AssertionError that is raised when the inner MyException # does not contain the expected property with zest.raises(AssertionError) as outer_e: with zest.raises(MyException, in_foo="blah") as e: raise MyException(foo="bar") assert isinstance(e, TrappedException) and isinstance( e.exception, MyException) assert "exception to have" in str(outer_e.exception) zest() def it_checks_args_of_exception(): with zest.raises(ValueError, in_args="bar"): raise ValueError("not", "bar") zest()
def zest_input_request(): zest.stack_mock(log._interactive_emit_line) def it_calls_input_when_not_headless(): with zest.mock(log.is_headless, returns=False): with zest.mock(log._input, returns="ret test 1") as m_input: ret = log.input_request("test1", "when headless") assert m_input.called_once() assert ret == "ret test 1" def it_handles_headless_mode(): m_input = zest.stack_mock(log._input) zest.stack_mock(log.is_headless, returns=True) def it_does_not_call_input_when_headless(): ret = log.input_request("test2", "when headless") assert m_input.not_called() assert ret == "when headless" def it_raises_when_headless_and_theres_an_exception_passed(): ret = log.input_request("test2", "when headless") assert m_input.not_called() assert ret == "when headless" zest() zest()
def it_cleaves_with_protease(): df, seqstrs = (None, ) * 2 def _before(): nonlocal df, seqstrs df = _proteolyze(_pro_seq_df("ABKCD"), protease) seqstrs = seqstrs_from_proteolyze(df) def it_picks_up_first(): assert seqstrs == ["ABK", "CD"] def it_assigns_offsets(): assert df.pep_offset_in_pro.values.tolist() == list(range(5)) def it_handles_cut_at_end(): df = _proteolyze(_pro_seq_df("ABKCDK"), protease) seqstrs = seqstrs_from_proteolyze(df) assert seqstrs == ["ABK", "CDK"] def it_handles_no_cut(): df = _proteolyze(_pro_seq_df("ABCD"), protease) seqstrs = seqstrs_from_proteolyze(df) assert seqstrs == ["ABCD"] zest()
def zest_survey_integration(): """ Show that a survey gen and run can execute """ csv_file = "/tmp/__zest_survey_integration.csv" utils.save( csv_file, utils.smart_wrap( """ Name,Seq,Abundance,POI pep0,ALNCLVMQL,1,1 pep1,APHGVVFL,1,1 pep2,KIADYNYML,1,1 pep3,MLPDDFTGC,4,1 pep4,CCQSLQTYV,1,1 pep5,TLMSKTQSL,1,1 pep6,VLCMNQKLI,1,1 pep7,ACCDFTAKV,1,0 """, assert_if_exceeds_width=True, ), ) local["p"]["gen", "survey", "--sample=zest_survey_integration", f"--protein_csv={csv_file}", "--label_set=C,M", "--n_pres=1", "--n_mocks=0", "--n_edmans=15", "--force", "--job=./jobs_folder/__zest_survey_integration", ] & FG local["p"]["run", "./jobs_folder/__zest_survey_integration"] & FG zest()
def zest_ConfMat(): def it_creates_from_arrays(): mat = np.array([[1, 2], [3, 4]]) cm = ConfMat.from_array(mat) assert np.all(cm == mat) assert mat is not cm def it_creates_from_true_pred(): true = np.array([1, 2, 3]) pred = np.array([1, 2, 3]) cm = ConfMat.from_true_pred(true, pred, true_dim=4, pred_dim=4) assert np.all(cm == [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) def it_asserts_that_no_elements_are_outside_of_dim(): with zest.raises(AssertionError): ConfMat.from_true_pred(true, pred, true_dim=2, pred_dim=2) zest() def it_extracts_precision_without_div_0(): cm = ConfMat.from_array(np.array([[5, 0], [0, 0]])) prec = cm.precision() assert np.all(prec == [1.0, 0]) def it_extracts_recall_without_div_0(): cm = ConfMat.from_array(np.array([[5, 0], [5, 0]])) rec = cm.recall() assert np.all(rec == [0.5, 0]) zest()
def it_generates_flu_info(): with tmp.tmp_folder(chdir=True): prep_result = PrepResult.prep_result_fixture( pros=[".", "XAXCD", "XAXCDXX", "XCCXX"], pro_is_decoys=[False, False, False, False], peps=[".", "XAXCD", "XAXCDXX", "XCCXX"], pep_pro_iz=[0, 1, 2, 3], ) sim_params = _stub_sim_params(some_error_model, n_samples) sim_result = sim_v1_worker.sim_v1(sim_params, prep_result) sim_result._generate_flu_info(prep_result) def it_computes_head_and_tail(): _flus = sim_result._flus assert np.all(_flus[_flus.pep_i.isin([1, 2])].flu_count == 2) assert np.all(_flus[_flus.pep_i.isin([1, 2])].n_head_ch_0 == 1) assert np.all(_flus[_flus.pep_i.isin([1, 2])].n_head_ch_1 == 0) assert np.all(_flus[_flus.pep_i.isin([1, 2])].n_tail_ch_0 == 0) assert np.all(_flus[_flus.pep_i.isin([1, 2])].n_tail_ch_1 == 1) assert np.all(_flus[_flus.pep_i == 3].flu_count == 1) def it_peps__flus(): df = sim_result.peps__flus(prep_result) assert "flustr" in df assert len(df) == 4 def it_peps__flus__unique_flus(): df = sim_result.peps__flus__unique_flus(prep_result) assert np.all(df.pep_i.values == [0, 3]) zest()
def zest_smart_wrap(): def it_keeps_blank_lines(): l = utils.smart_wrap(""" ABC DEF """) assert l == "\nABC\n\nDEF\n" def it_keeps_indents(): l = utils.smart_wrap(""" ABC DEF GHI JKL """) assert l == "\nABC\n\nDEF\n GHI\nJKL\n" def it_keeps_indents_but_not_wraps_when_width_is_none(): l = utils.smart_wrap( """ ABC This is a very very long line, much longer than the 80 chracters that are the default length of a line that woudl normally wrap but here it should not wrap. GHI JKL """, width=None, ) assert l.startswith( "\nABC\n\nThis is a very very long line, much longer than the 80 chracters that are the default length of a line that woudl normally wrap but here it should not wrap.\n GHI\nJKL\n" ) zest()
def zest_step_5_make_radmat(): # fmt: off dyemat = npf([ [ [2, 1], [2, 1], ], [ [0, 0], [0, 0], ], ]) sim_params = SimV1Params.from_aa_list_fixture( ["DE", "C"], error_model=ErrorModel.no_errors(n_channels=2, beta=7500.0, sigma=0.16), n_pres=1, n_mocks=0, n_edmans=1, ) radmat = sim_v1_worker._step_5_make_radmat(dyemat, sim_params) def it_makes_radmat(): assert np.all(radmat[0] > 4000.0) def it_deals_with_zeros(): assert np.all(radmat[1] == 0.0) # fmt: on zest()
def zest_step_3_evolve_cycles(): # fmt: off samples = npf([ [[1, 1, 0], [0, 1, 0]], [[0, 1, 1], [0, 0, 1]], ]) # fmt: on sim_params = None def _before(): nonlocal sim_params sim_params = SimV1Params.from_aa_list_fixture( ["DE", "C"], error_model=ErrorModel.no_errors(n_channels=2, beta=7500.0), n_pres=0, n_mocks=1, n_edmans=1, ) def it_proceeds_with_no_error(): evolution = sim_v1_worker._step_3_evolve_cycles(samples, sim_params) # fmt: off n = np.nan expected = npf([ [ [[1, 1, 0], [0, 1, 0]], [[0, 1, 1], [0, 0, 1]], ], [ [[n, 1, 0], [n, 1, 0]], [[n, 1, 1], [n, 0, 1]], ], ]) # fmt: on if not np_array_same(evolution, expected): debug(evolution) debug(sim_params) assert np_array_same(evolution, expected) def it_proceeds_with_errors(): # Example error: detach 100% sim_params.error_model.p_detach = 1.0 sim_params._build_join_dfs() evolution = sim_v1_worker._step_3_evolve_cycles(samples, sim_params) expected = np.full_like(evolution, np.nan) assert np_array_same(evolution, expected) def it_makes_a_copy(): assert samples[0, 0, 0] == 1.0 zest() zest()
def zest_step_3b_photobleach(): n = np.nan samples = npf([ [[1, 1, 0], [0, 1, 0]], [[n, 1, 1], [n, 0, 1]], ]) def it_bleaches_all(): result = sim_v1_worker._step_3b_photobleach( samples, p_bleach_by_channel=[1.0, 1.0]) expected = npf([ [[0, 0, 0], [0, 0, 0]], [[n, 0, 0], [n, 0, 0]], ]) assert np_array_same(expected, result) def it_makes_a_copy(): # The original should not have changed assert samples[0, 0, 0] == 1.0 zest() def it_bleaches_channels_independently(): result = sim_v1_worker._step_3b_photobleach( samples, p_bleach_by_channel=[1.0, 0.0]) expected = npf([ [[0, 0, 0], [0, 1, 0]], [[n, 0, 0], [n, 0, 1]], ]) assert np_array_same(expected, result) zest()
def zest_survey_v2_pyx(): # TODO: This prep_result = prep_fixtures.result_simple_fixture(True) sim_v2_result = SimV2Result.from_prep_fixture(prep_result, labels="A,B") # pep 0: # pep 1: 10000 11000 # pep 2: 00000 21100 # pep 3: 00000 21000 pep_i_to_mic_pep_i, pep_i_to_isolation_metric = survey_v2_fast.survey( prep_result.n_peps, sim_v2_result.train_dyemat, sim_v2_result.train_dyepeps, n_threads=1, progress=None, ) assert pep_i_to_mic_pep_i.tolist() == [0, 3, 3, 2] # In the current verion they are all close # The first peptide should be a long way away and the other two should collide assert pep_i_to_isolation_metric[1] < 2 assert pep_i_to_isolation_metric[2] < 2 assert pep_i_to_isolation_metric[3] < 2 # TODO: I really want to do a better job where I compare some contrived # peptides and make sure that the outliers are outliers # I also need to do the sampling to figure out that magic number of the "nothing close" # TODO: Test for unlabelled peptides. I'm sure it is broken zest()
def shuffling(): def _all_identical_ordering(disable_shuffle): first_found_tests = [] for _ in range(5): ret_code, output = _call_zest_cli( "--verbose=2", "--disable_shuffle" if disable_shuffle else "", "zest_basics", ) assert ret_code == 0 found_tests = _get_run_tests(output) if len(first_found_tests) == 0: first_found_tests = list(found_tests) else: if found_tests != first_found_tests: return False else: return True def it_shuffles_by_default(): assert not _all_identical_ordering(False) def it_can_disable_shuffle(): assert n_workers != 1 or _all_identical_ordering(True) zest()
def zest_survey_v2_integration(): """ This needs a lot of work on figuring out what the metric of success of the survey is exactly. Also need some brain-dead simpler cases. Cases where the peptides are super clearly separated and make sure that we get sensible results. """ with tmp.tmp_folder(chdir=True): prep_result = prep_fixtures.result_random_fixture(20) sim_v2_result = SimV2Result.from_prep_fixture(prep_result, labels="DE,C,Y") sim_v2_result.save() survey_v2_result = survey_v2_worker.survey_v2(SurveyV2Params(), prep_result, sim_v2_result) # survey_v2_result._survey.to_csv("/erisyon/internal/test.csv") # I will need to set the RNG on this to test. # There's a weird effect # https://docs.google.com/spreadsheets/d/1SrOjdNTpw7uLWU1iS7PFm4kbfNLTnW6Am2t85b-GKww/edit#gid=1462476311 # Why are 3 peptides with the same flu not all showing each other as the nn? zest()
def out_of_date(): m_parent_timestamps = zest.stack_mock(PipelineTask._parent_timestamps) m_child_timestamps = zest.stack_mock(PipelineTask._child_timestamps) def it_returns_false_on_no_parent_files(): m_parent_timestamps.returns([]) assert PipelineTask._out_of_date("parent", "child")[0] is False def it_returns_true_on_no_child_files(): m_parent_timestamps.returns([("a0", 1)]) m_child_timestamps.returns([]) assert PipelineTask._out_of_date("parent", "child")[0] is True def it_returns_true_if_any_parent_file_is_younger_than_youngest_child( ): m_parent_timestamps.returns([("p0", 3)]) m_child_timestamps.returns([("c0", 1), ("c1", 2)]) assert PipelineTask._out_of_date("parent", "child")[0] is True def it_returns_false_if_all_parent_files_are_older_than_all_child_files( ): m_parent_timestamps.returns([("p0", 1)]) m_child_timestamps.returns([("c0", 2), ("c1", 3)]) assert PipelineTask._out_of_date("parent", "child")[0] is False zest()
def zest_step_4_proteolysis_one_protease(): pro_seqs_df = pd.DataFrame( [ (0, "."), (1, "B"), (1, "K"), (1, "C"), (2, "D"), (2, "E"), ], columns=["pro_i", "aa"], ) peps_df, pep_seqs_df = _step_4_proteolysis(pro_seqs_df, "lysc") # Cut after K def it_renumbers_consecutively(): assert peps_df.pro_i.tolist() == [0, 1, 1, 2] assert peps_df.pep_i.tolist() == [0, 1, 2, 3] def it_sets_seqs(): assert pep_seqs_df.pep_i.tolist() == [0, 1, 1, 2, 3, 3] assert pep_seqs_df.pep_offset_in_pro.tolist() == [0, 0, 1, 2, 0, 1] assert pep_seqs_df.aa.tolist() == [".", "B", "K", "C", "D", "E"] zest()
def corrects_for_background_shifts(): n_cycles = 1 w, h = 128, 128 x, y = 16.5, 20.5 def it_bg_corrects_with_no_collisions(): cy_ims = np.zeros((n_cycles, h, w)) _peak(cy_ims[0], x, y) # A uniform drop is a common thing that happens as a result of a filter # so simulate that here with a constant bg_shift = -2.0 _cy_ims = cy_ims + bg_shift radrow = radiometry_cy_ims( _cy_ims, locs=np.array([[y, x]]), reg_psf_samples=reg_psf_samples, peak_mea=reg_psf.hyper_peak_mea, ) sig = radrow[0, 0, 0] bg_med = radrow[0, 0, 2] bg_std = radrow[0, 0, 3] assert np.abs(sig - 999.0) < 1.0 assert np.abs(bg_med - bg_shift) < 0.5 assert np.abs(bg_std - 3.0) < 0.5 def it_catches_collisions_in_bg(): cy_ims = np.zeros((n_cycles, h, w)) _peak(cy_ims[0], x, y) # Add a second peak nearby offset_x = 5.0 _peak(cy_ims[0], x + offset_x, y) # A uniform drop is a common thing that happens as a result of a filter # so simulate that here with a constant bg_shift = -2.0 _cy_ims = cy_ims + bg_shift radrow = radiometry_cy_ims( _cy_ims, locs=np.array([[y, x], [y, x + offset_x]]), reg_psf_samples=reg_psf_samples, peak_mea=reg_psf.hyper_peak_mea, ) sig = radrow[0, 0, 0] noi = radrow[0, 0, 1] bg_med = radrow[0, 0, 2] bg_std = radrow[0, 0, 3] # It will be brighter because of the contention assert np.abs(sig - 1134.0) < 20.0 # 1134.0 is empirical assert np.abs(bg_med - bg_shift) < 0.5 assert np.abs(bg_std) > 50 # empirical zest()
def zest_step_5_create_ptm_peptides(): peps_df = pd.DataFrame( [ (0, 0, 1, 0), (1, 0, 5, 1), ], columns=["pep_i", "pep_start", "pep_stop", "pro_i"], ) pep_seqs_df = pd.DataFrame( dict( pep_i=[1] * 6, aa=list("ABCDEF"), pep_offset_in_pro=list(range(6)), )) pros_df = pd.DataFrame( [ ("nul", False, 0, ""), ("id1", False, 1, "1;3"), ], columns=["pro_id", "pro_is_decoy", "pro_i", "pro_ptm_locs"], ) def it_adds_correct_pep_iz(): ptm_peps_df, ptm_pep_seqs_df = _step_5_create_ptm_peptides( peps_df, pep_seqs_df, pros_df, n_ptms_limit=5) assert len(ptm_peps_df) == 3 assert ptm_peps_df.iloc[0].pep_i == 2 assert ptm_peps_df.iloc[1].pep_i == 3 assert ptm_peps_df.iloc[2].pep_i == 4 assert len(ptm_pep_seqs_df) == 18 assert list(ptm_pep_seqs_df.pep_i.unique()) == [2, 3, 4] zest()
def zest_nn_step_2_create_inverse_variances(): def it_scales_by_the_std(): dt_mat = np.array( [[[0, 0, 0], [0, 0, 0]], [[2, 1, 0], [1, 1, 0]], [[1, 1, 0], [1, 0, 0]],] ) channel_i_to_vpd = np.array([1.0, 4.0]) # 4 because it is a perfect square inv_var = nn._step_2_create_inverse_variances(dt_mat, channel_i_to_vpd) assert np.all( # 0 becomes 0.5 * sqrt(1) == 0.5. 1.0 / 0.5**2 # Channel 0: # 0.5 * sqrt(1) == 0.5... 1/0.5**2 == 4 # 1.0 * sqrt(1) == 1.0... 1/1.0**2 == 1 # 2.0 * sqrt(1) == 2.0... 1/2.0**2 == 0.25 # Channel 1: # 0.5 * sqrt(4) == 1.0... 1/1.0**2 == 1 # 1.0 * sqrt(4) == 2.0... 1/2.0**2 == 0.25 inv_var == [ [[4.0, 4.0, 4.0], [1.0, 1.0, 1.0],], [[0.25, 1.0, 4.0], [0.25, 0.25, 1.0],], [[1.0, 1.0, 4.0], [0.25, 1.0, 1.0],], ] ) zest()
def zest_sigproc_v2_analyze_field(): n_channels = 2 im_mea = 512 def _synth(): ch_scale = (1.0, 1.0) ch_aln = np.array([ [0.0, 0.0], [-5.0, -5.0], ]) with synth.Synth(n_channels=n_channels, n_cycles=3, dim=(im_mea, im_mea)) as s: s.channel_aln_offsets(ch_aln) peaks = (synth.PeaksModelGaussianCircular( n_peaks=500).uniform_width_and_heights().amps_constant( 5000).locs_randomize().channel_scale_factor(ch_scale)) return s.render_chcy(), peaks def it_runs(): reg_illum = PriorsMLEFixtures.reg_illum_uniform(im_mea) reg_psf = PriorsMLEFixtures.reg_psf_uniform(hyper_im_mea=im_mea) priors = Priors(hyper_n_channels=n_channels) priors.add("reg_illum", reg_illum, "test") priors.add("reg_psf", reg_psf, "test") sigproc_v2_params = SigprocV2Params(mode="analyze", priors=priors) raw_chcy_ims, peaks = _synth() _analyze_field(raw_chcy_ims, sigproc_v2_params) # No asserts, just checking that it doesn't exception zest()
def zest_gauss2_synth(): """ Test the high speed gaussian synth """ def it_generates_one_peak_correctly(): im = np.zeros((25, 25)) peak_mea = 11 locs = np.array([[5.5, 5.5]]) n_locs = len(locs) amps = np.full((n_locs, ), 1000.0) std_xs = np.full((n_locs, ), 1.5) std_ys = np.full((n_locs, ), 1.5) gauss2_fitter.synth_image(im, peak_mea, locs, amps, std_xs, std_ys) m = np.max(im) # When peak is at 5.5 it should be cengtered and only 1 pixel will the be brightest assert (im == m).sum() == 1 def it_generates_synth(): im = np.zeros((128, 128)) peak_mea = 11 x, y = np.meshgrid(np.linspace(0, 128, 20), np.linspace(0, 128, 20)) locs = np.vstack((y.flatten(), x.flatten())).T n_locs = len(locs) amps = np.full((n_locs, ), 1000.0) amps[0:50] = 2000.0 std_xs = np.full((n_locs, ), 1.2) std_ys = np.full((n_locs, ), 1.5) gauss2_fitter.synth_image(im, peak_mea, locs, amps, std_xs, std_ys) # No assert here: Just checking that it doesn't crash # The values are tested by synth in every test. zest()
def it_calls_start_and_stop_callbacks(): start_was_called = 0 stop_was_called = 0 # Note the following two callbacks are ignored because they are underscored def _test_start_callback(zest_result): nonlocal start_was_called start_was_called += 1 def _test_stop_callback(zest_result): nonlocal stop_was_called stop_was_called += 1 def test1(): pass def test2(): pass zest( test_start_callback=_test_start_callback, test_stop_callback=_test_stop_callback, ) assert start_was_called == 2 and stop_was_called == 2
def zest_align(): def _ims(mea=512, std=1.5): bg_mean = 145 with synth.Synth(n_cycles=3, overwrite=True, dim=(mea, mea)) as s: ( synth.PeaksModelGaussianCircular(n_peaks=1000) .amps_constant(val=4_000) .locs_randomize() .widths_uniform(std) ) synth.CameraModel(bias=bg_mean, std=14) cy_ims = s.render_chcy()[0] return cy_ims, s.aln_offsets def it_removes_the_noise_floor(): cy_ims, true_aln_offsets = _ims() pred_aln_offsets, aln_scores = worker._align(cy_ims) assert np.all(true_aln_offsets == pred_aln_offsets) def it_is_robust_to_different_image_sizes(): cy_ims, true_aln_offsets = _ims(mea=128) pred_aln_offsets, aln_scores = worker._align(cy_ims) assert np.all(true_aln_offsets == pred_aln_offsets) def it_is_robust_to_different_peak_sizes(): cy_ims, true_aln_offsets = _ims(std=3.0) pred_aln_offsets, aln_scores = worker._align(cy_ims) assert np.all(true_aln_offsets == pred_aln_offsets) zest()
def zest_validate_job_folder(): job_name = "__test_job1" job1 = local.path("./jobs_folder") / job_name run1 = local.path("./jobs_folder") / job_name / "run1" def _before(): job1.mkdir() run1.mkdir() def _after(): job1.delete() run1.mkdir() def it_accepts_folder_in_jobs_folder_by_absolute_str(): job = str(local.path(local.env["HOST_JOBS_FOLDER"]) / job_name) assert assets.validate_job_folder(job) == job_name def it_accepts_folder_in_jobs_folder_by_absolute_plumbum(): job = local.path(local.env["HOST_JOBS_FOLDER"]) / job_name assert assets.validate_job_folder(job) == job_name def it_accepts_folder_in_jobs_folder_by_relative_string(): assert assets.validate_job_folder( f"./jobs_folder/{job_name}") == job_name def it_accepts_folder_in_jobs_folder_by_relative_string_with_trailing_slash( ): assert assets.validate_job_folder( f"./jobs_folder/{job_name}/") == job_name def it_accepts_named_job_folder_if_it_exists(): assert assets.validate_job_folder(job_name) == job_name def it_accepts_slash_slash_jobs_folder(): assert assets.validate_job_folder( f"//jobs_folder/{job_name}") == job_name def it_accepts_run_folder_if_specified(): assert (assets.validate_job_folder( f"./jobs_folder/{job_name}/run1", allow_run_folders=True) == f"{job_name}/run1") def it_raises_on_run_folder_if_not_specified(): with zest.raises(ValueError): assets.validate_job_folder(f"./jobs_folder/{job_name}/run1", allow_run_folders=False) def it_raises_on_non_existing_file_in_jobs_folder(): with zest.raises(FileNotFoundError): assets.validate_job_folder("./jobs_folder/__does_not_exist") def it_raises_on_file_outside_jobs_folder(): with zest.raises(ValueError): assets.validate_job_folder("/tmp/foo/bar") def it_raises_on_non_str(): with zest.raises(check.CheckError): assets.validate_job_folder(123) zest()
def zest_composite_with_alignment_offsets_chcy_ims(): def _ims(): bg_mean = 145 with synth.Synth(n_channels=2, n_cycles=3, overwrite=True, dim=(256, 256)) as s: ( synth.PeaksModelGaussianCircular(n_peaks=1000) .amps_constant(val=4_000) .locs_randomize() .widths_uniform(1.5) ) synth.CameraModel(bias=bg_mean, std=14) chcy_ims = s.render_chcy() return chcy_ims, s.aln_offsets def it_creates_a_single_composite_image(): chcy_ims, true_aln_offsets = _ims() comp_im = worker._composite_with_alignment_offsets_chcy_ims( chcy_ims, true_aln_offsets ) assert comp_im.ndim == 4 assert ( comp_im.shape[0] == chcy_ims.shape[0] and comp_im.shape[1] == chcy_ims.shape[1] ) assert ( comp_im.shape[2] < chcy_ims.shape[2] or comp_im.shape[3] < chcy_ims.shape[3] ) for cy in range(2): for ch in range(2): diff = np.sum(comp_im[ch, cy + 1, :, :] - comp_im[ch, cy, :, :]) assert utils.np_within(diff, 0.0, 12_000) zest()
def zest_align_cy_ims(): def _synth(n_peaks=500): with synth.Synth(n_channels=1, n_cycles=3, dim=(512, 512)) as s: reg_psf = PriorsMLEFixtures.reg_psf_uniform() ( synth.PeaksModelPSF(reg_psf, n_peaks=n_peaks) .amps_constant(5000) .locs_randomize() ) synth.CameraModel(100, 30) synth.HaloModel() return s.aln_offsets, s.render_chcy() def it_handles_clean(): true_aln_offsets, chcy_ims = _synth() pred_aln_offsets = align_ims(chcy_ims[0]) assert np.all(np.abs(true_aln_offsets[None, :, :] - pred_aln_offsets) < 0.1) @zest.retry(3) def it_handles_smudged(): true_aln_offsets, chcy_ims = _synth() smudge_im = np.full((400, 400), 1000) imops.accum_inplace(chcy_ims[0, 0], smudge_im, loc=XY(100, 100)) pred_cy_offsets = align_ims(chcy_ims[0]) assert np.all(np.abs(true_aln_offsets - pred_cy_offsets) < 0.16) def it_handles_unalignable(): cy_ims = np.zeros((3, 512, 512)) pred_cy_offsets = align_ims(cy_ims) assert np.all(pred_cy_offsets[0, :] == 0.0) assert np.all(pred_cy_offsets[1:, :] >= 512.0) zest()
def check_bounds(): def it_validates_min_val(): s._check_bounds(5, bounds=(5, None)) with zest.raises(SchemaValidationFailed): s._check_bounds(3, bounds=(5, None)) def it_validates_max_val(): s._check_bounds(5, bounds=(None, 5)) with zest.raises(SchemaValidationFailed): s._check_bounds(6, bounds=(None, 5)) def it_validates_bounds(): s._check_bounds(4, bounds=(4, 6)) s._check_bounds(6, bounds=(4, 6)) with zest.raises(SchemaValidationFailed): s._check_bounds(3, bounds=(4, 6)) s._check_bounds(7, bounds=(4, 6)) def it_raises_if_bounds_not_valid(): with zest.raises(SchemaInvalid): s._check_bounds_arg(bounds=4) with zest.raises(SchemaInvalid): s._check_bounds_arg(bounds=("a", "b")) with zest.raises(SchemaInvalid): s._check_bounds_arg(bounds=()) zest()