def test_import(self, tree, tmpdir): """Test import of frequencies JSON that was exported from a frequencies instance. """ start_date = 2015.5 end_date = 2018.5 kde_frequencies = TreeKdeFrequencies(start_date=start_date, end_date=end_date) frequencies = kde_frequencies.estimate(tree) frequencies_json = kde_frequencies.to_json() # Try to dump exported JSON to disk. tmp_fh = tmpdir.mkdir("json").join("frequencies.json") fh = tmp_fh.open(mode="w") json.dump(frequencies_json, fh) fh.close() assert tmp_fh.check() # Import frequencies from existing tree and JSON. fh = tmp_fh.open() new_frequencies_json = json.load(fh) fh.close() new_kde_frequencies = TreeKdeFrequencies.from_json( new_frequencies_json) assert np.array_equal(kde_frequencies.pivots, new_kde_frequencies.pivots) # Get the first non-root key (root clade is number 0) and should be first in the sorted list of keys. key = sorted(kde_frequencies.frequencies.keys())[1] assert np.array_equal(kde_frequencies.frequencies[key], new_kde_frequencies.frequencies[key])
def test_export_without_frequencies(self): """Test frequencies export to JSON when frequencies have *not* been estimated. """ kde_frequencies = TreeKdeFrequencies() frequencies_json = kde_frequencies.to_json() assert "params" in frequencies_json assert kde_frequencies.pivot_frequency == frequencies_json["params"]["pivot_frequency"] assert "node_filters" in frequencies_json["params"] assert "data" not in frequencies_json
def test_import_without_frequencies(self): """Test import of frequencies JSON that was exported from a frequencies instance without frequency values. """ kde_frequencies = TreeKdeFrequencies() frequencies_json = kde_frequencies.to_json() # Import frequencies from existing tree and JSON. new_kde_frequencies = TreeKdeFrequencies.from_json(frequencies_json) assert kde_frequencies.pivot_frequency == new_kde_frequencies.pivot_frequency assert not hasattr(new_kde_frequencies, "frequencies")
def test_export_without_frequencies(self): """Test frequencies export to JSON when frequencies have *not* been estimated. """ kde_frequencies = TreeKdeFrequencies() frequencies_json = kde_frequencies.to_json() assert "params" in frequencies_json assert kde_frequencies.pivot_frequency == frequencies_json["params"][ "pivot_frequency"] assert "node_filters" in frequencies_json["params"] assert "data" not in frequencies_json
def test_import_without_frequencies(self): """Test import of frequencies JSON that was exported from a frequencies instance without frequency values. """ kde_frequencies = TreeKdeFrequencies() frequencies_json = kde_frequencies.to_json() # Import frequencies from existing tree and JSON. new_kde_frequencies = TreeKdeFrequencies.from_json(frequencies_json) assert kde_frequencies.pivot_frequency == new_kde_frequencies.pivot_frequency assert not hasattr(new_kde_frequencies, "frequencies")
def test_export_with_frequencies(self, tree): """Test frequencies export to JSON when frequencies have been estimated. """ kde_frequencies = TreeKdeFrequencies() frequencies = kde_frequencies.estimate(tree) frequencies_json = kde_frequencies.to_json() assert "params" in frequencies_json assert kde_frequencies.pivot_frequency == frequencies_json["params"]["pivot_frequency"] assert kde_frequencies.start_date == frequencies_json["params"]["start_date"] assert kde_frequencies.end_date == frequencies_json["params"]["end_date"] assert "data" in frequencies_json assert "pivots" in frequencies_json["data"] assert "frequencies" in frequencies_json["data"]
def test_export_with_frequencies(self, tree): """Test frequencies export to JSON when frequencies have been estimated. """ kde_frequencies = TreeKdeFrequencies() frequencies = kde_frequencies.estimate(tree) frequencies_json = kde_frequencies.to_json() assert "params" in frequencies_json assert kde_frequencies.pivot_frequency == frequencies_json["params"][ "pivot_frequency"] assert kde_frequencies.start_date == frequencies_json["params"][ "start_date"] assert kde_frequencies.end_date == frequencies_json["params"][ "end_date"] assert "data" in frequencies_json assert "pivots" in frequencies_json["data"] assert "frequencies" in frequencies_json["data"]
def test_import(self, tree, tmpdir): """Test import of frequencies JSON that was exported from a frequencies instance. """ start_date = 2015.5 end_date = 2018.5 kde_frequencies = TreeKdeFrequencies( start_date=start_date, end_date=end_date ) frequencies = kde_frequencies.estimate(tree) frequencies_json = kde_frequencies.to_json() # Try to dump exported JSON to disk. tmp_fh = tmpdir.mkdir("json").join("frequencies.json") fh = tmp_fh.open(mode="w") json.dump(frequencies_json, fh) fh.close() assert tmp_fh.check() # Import frequencies from existing tree and JSON. fh = tmp_fh.open() new_frequencies_json = json.load(fh) fh.close() new_kde_frequencies = TreeKdeFrequencies.from_json(new_frequencies_json) assert np.array_equal( kde_frequencies.pivots, new_kde_frequencies.pivots ) # Get the first non-root key (root clade is number 0) and should be first in the sorted list of keys. key = sorted(kde_frequencies.frequencies.keys())[1] assert np.array_equal( kde_frequencies.frequencies[key], new_kde_frequencies.frequencies[key] )