def test_save_to_json(tmp_path, ecoli_obj): fname = str(tmp_path / "ecoli_obj.json") save_to_json(ecoli_obj, fname) assert isfile(fname) new_icd = load_json_model(fname) # Data assert np.allclose(new_icd.M, ecoli_obj.M) assert np.allclose(new_icd.A, ecoli_obj.A) assert np.allclose(new_icd.X, ecoli_obj.X) # Names assert new_icd.imodulon_names == ecoli_obj.imodulon_names assert new_icd.sample_names == ecoli_obj.sample_names assert new_icd.gene_names == ecoli_obj.gene_names # Tables assert new_icd.gene_table.equals(ecoli_obj.gene_table.replace("", np.nan)) assert new_icd.sample_table.equals( ecoli_obj.sample_table.replace("", np.nan)) assert new_icd.imodulon_table.equals( ecoli_obj.imodulon_table.replace("", np.nan)) # Thresholds assert np.allclose( [new_icd.thresholds[x] for x in new_icd.imodulon_names], [ecoli_obj.thresholds[x] for x in ecoli_obj.imodulon_names], ) assert new_icd.cutoff_optimized == ecoli_obj.cutoff_optimized assert new_icd.dagostino_cutoff == ecoli_obj.dagostino_cutoff
def load_bsub_data(): """ Load *Bacillus subtilis* :class:`~pymodulon.core.IcaData` object from :cite:`Rychel2020a` Returns ------- bsub_data: ~pymodulon.core.IcaData *B. subtilis* :class:`~pymodulon.core.IcaData` object """ return load_json_model(path.join(_data_dir, "objects", "bsub_data.json.gz"))
def load_staph_data(): """ Load *Staphylococcus aureus* :class:`~pymodulon.core.IcaData` object from :cite:`Poudel2020` Returns ------- staph_data: ~pymodulon.core.IcaData *S. aureus* :class:`~pymodulon.core.IcaData` object """ return load_json_model(path.join(_data_dir, "objects", "staph_data.json.gz"))
def load_ecoli_data(): """ Load *Escherichia coli* :class:`~pymodulon.core.IcaData` object from :cite:`Sastry2019` Returns ------- ecoli_data: ~pymodulon.core.IcaData *E. coli* :class:`~pymodulon.core.IcaData` object """ return load_json_model(path.join(_data_dir, "objects", "ecoli_data.json.gz"))
def test_init_thresholds(ecoli_obj, mini_obj_opt, caplog, tmp_path): # Test all threshold initialization options M = mini_obj_opt.M A = mini_obj_opt.A # Reduce TRN size trn = ecoli_obj.trn.iloc[:10].copy() # Define custom thresholds thresh_list = list(np.arange(1, 2, 0.1)) thresh_dict = dict(zip(M.columns, thresh_list)) thresholds_opts = [None, thresh_dict] threshold_method_opts = ["kmeans", "dagostino"] trn_opts = [None, trn] optimize_cutoff_opts = [True, False] dagostino_cutoff_opts = [None, 600] for opt1 in thresholds_opts: for opt2 in threshold_method_opts: for opt3 in trn_opts: for opt4 in optimize_cutoff_opts: for opt5 in dagostino_cutoff_opts: caplog.clear() obj1 = IcaData( M, A, thresholds=opt1, threshold_method=opt2, trn=opt3, optimize_cutoff=opt4, dagostino_cutoff=opt5, ) fname = str(tmp_path / "test_data.json") save_to_json(obj1, fname) obj2 = load_json_model(fname) for obj in [obj1, obj2]: if opt1 is not None: assert obj.thresholds == thresh_dict assert obj.dagostino_cutoff is None assert not obj.cutoff_optimized if opt4: # optimize_cutoff == True assert caplog.messages == [ "Using manually input thresholds. " "D'agostino optimization will not be " "performed." ] else: assert caplog.text == "" # opt1 == None elif opt2 == "kmeans" or opt3 is None: assert obj.dagostino_cutoff is None assert not obj.cutoff_optimized if opt4: # optimize_cutoff == True assert caplog.messages == [ "Using Kmeans threshold " "method. D'agostino optimization will not " "be performed." ] else: assert caplog.text == "" # opt2 == 'dagostino' # opt3 == trn elif opt4: assert obj.cutoff_optimized assert obj.dagostino_cutoff is not None assert caplog.messages == [ "Optimizing iModulon thresholds, may take " "2-3 minutes..." ] # opt4 == False elif opt5 is None: assert not obj.cutoff_optimized assert obj.dagostino_cutoff == 550 assert caplog.messages == [ "Using the default " "dagostino_cutoff of 550. This may not be " "optimal for your dataset. Use " "ica_data.reoptimize_thresholds() to find the " "optimal threshold." ] elif opt5 == 600: assert not obj.cutoff_optimized assert obj.dagostino_cutoff == 600 else: raise ValueError("Missing test case!")