def test_simplevsfull_plot(self): config = 'tests/small_simple.yaml' simple = niche_vlaanderen.Niche() simple.run_config_file(config) config_full = "tests/small.yaml" full = niche_vlaanderen.Niche() full.run_config_file(config_full) delta = niche_vlaanderen.NicheDelta(simple, full) # as the full model always contains less than the simple model, # we can use this in a test df = delta.table self.assertEqual(0, df[df.presence == "only in model 2"].area_ha.sum()) import matplotlib as mpl mpl.use('agg') import matplotlib.pyplot as plt plt.show = lambda: None delta.plot(5) delta.name = "vergelijking" delta.plot(5)
def testinvalidDelta(self): small = niche_vlaanderen.Niche() with pytest.raises(NicheException): # should fail as there is no extent yet niche_vlaanderen.NicheDelta(small, small) small.read_config_file("tests/small_simple.yaml") with pytest.raises(NicheException): # should fail as the model has not yet been run niche_vlaanderen.NicheDelta(small, small) zwb = TestNiche.create_zwarte_beek_niche() zwb.run() small.run(full_model=False) # should fail due to different extent with pytest.raises(NicheException): niche_vlaanderen.NicheDelta(zwb, small)
def test_overwrite_file(self): myniche = TestNiche.create_small() myniche.run(full_model=False) delta = niche_vlaanderen.NicheDelta(myniche, myniche) tmpdir = tempfile.mkdtemp() delta.write(tmpdir) # should raise: file already exists with pytest.raises(NicheException): delta.write(tmpdir) # should just warn delta.write(tmpdir, overwrite_files=True) shutil.rmtree(tmpdir)
def test_differentvegsize(self): myniche = niche_vlaanderen.Niche( ct_vegetation="tests/data/bad_ct/one_vegetation.csv") myniche.set_input("mhw", "tests/data/small/mhw.asc") myniche.set_input("mlw", "tests/data/small/mlw.asc") myniche.set_input("soil_code", "tests/data/small/soil_code.asc") myniche.run(full_model=False) small = niche_vlaanderen.Niche() small.run_config_file("tests/small.yaml") # we try to compare but both elements have different vegetations with pytest.raises(NicheException): niche_vlaanderen.NicheDelta(small, myniche)
def test_simplevsfull_write(self): config = 'tests/small_simple.yaml' simple = niche_vlaanderen.Niche() simple.run_config_file(config) config_full = "tests/small.yaml" full = niche_vlaanderen.Niche() full.run_config_file(config_full) delta = niche_vlaanderen.NicheDelta(simple, full) tmpdir = tempfile.mkdtemp() tmpsubdir = tmpdir + "/new" delta.write(tmpsubdir) # check tempdir contains the vegetation and the abiotic files expected_files = [ 'D1.tif', 'D2.tif', 'D3.tif', 'D4.tif', 'D5.tif', 'D6.tif', 'D7.tif', 'D8.tif', 'D9.tif', 'D10.tif', 'D11.tif', 'D12.tif', 'D13.tif', 'D14.tif', 'D15.tif', 'D16.tif', 'D17.tif', 'D18.tif', 'D19.tif', 'D20.tif', 'D21.tif', 'D22.tif', 'D23.tif', 'D24.tif', 'D25.tif', 'D26.tif', 'D27.tif', 'D28.tif', 'legend_delta.csv', 'delta_summary.csv' ] dir = os.listdir(tmpsubdir) if sys.version_info < (3, 2): self.assertItemsEqual(expected_files, dir) else: self.assertCountEqual(expected_files, dir) delta.name = "vgl" delta.write(tmpsubdir) dir = os.listdir(tmpsubdir) assert 60 == len(dir) assert 30 == sum(f.startswith("vgl_") for f in dir) shutil.rmtree(tmpdir)