예제 #1
0
    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)
예제 #2
0
    def test_mxw_validation_yml(self):
        myniche = niche_vlaanderen.Niche()
        # should not raise
        myniche.run_config_file("tests/small_nostrict.yml")

        n2 = niche_vlaanderen.Niche()
        n2.read_config_file("tests/small_nostrict.yml")
        with pytest.raises(NicheException):
            n2.run()
예제 #3
0
    def test_combine(self):
        fp = nv.Flooding()

        myniche = nv.Niche()
        input = "testcase/dijle/"
        myniche.set_input("soil_code", input + "bodemv.asc")
        myniche.set_input("msw", input + "gvg_0_cm.asc")
        myniche.set_input("mlw", input + "glg_0_cm.asc")
        myniche.set_input("mhw", input + "ghg_0_cm.asc")
        myniche.set_input("seepage", input + "kwel_mm_dag.asc")

        myniche.set_input("management", input + "beheer_int.asc")

        myniche.set_input("nitrogen_atmospheric", input + "depositie_def.asc")
        myniche.set_input("nitrogen_animal", input + "bemest_dier.asc")

        myniche.set_input("nitrogen_fertilizer", input + "bemest_kunst.asc")

        myniche.set_input("inundation_vegetation", input + "overstr_veg.asc")
        myniche.set_input("inundation_acidity", input + "ovrstr_t10_50.asc")
        myniche.set_input("inundation_nutrient", input + "ovrstr_t10_50.asc")

        myniche.set_input("minerality", input + "minerality.asc")

        myniche.set_input("rainwater", input + "nulgrid.asc")

        with pytest.raises(FloodingException):
            # niche model not yet run
            fp.combine(myniche)

        myniche.run()
        with pytest.raises(FloodingException):
            # floodplain model not yet run
            fp.combine(myniche)

        fp.calculate("testcase/flooding/ff_bt_t10_h.asc",
                     "T10",
                     period="winter",
                     duration=1)

        small = nv.Niche()
        small.run_config_file("tests/small.yaml")

        with pytest.raises(FloodingException):
            # floodplain has different spatial extent than niche
            fp.combine(small)

        result = fp.combine(myniche)

        # get unique values for every vegtype
        unique = []
        for i in result._veg:
            unique.append(np.unique(result._veg[i]))
        unique = np.unique(np.hstack(unique))
        expected = np.array([-99, -1, 1, 2, 3])
        np.testing.assert_equal(expected, unique)
예제 #4
0
    def test_run_configuration_abiotic(self):
        config = 'tests/small_abiotic.yaml'
        myniche = niche_vlaanderen.Niche()
        myniche.run_config_file(config)

        config = 'tests/small_abiotic_extra.yaml'
        myniche = niche_vlaanderen.Niche()
        myniche.run_config_file(config)

        # rerun with a file with missing abiotic values
        config = 'tests/small_abiotic_invalid.yaml'
        myniche = niche_vlaanderen.Niche()
        with pytest.raises(NicheException):
            myniche.run_config_file(config)
예제 #5
0
    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)
예제 #6
0
    def test_rereadoutput(self):
        """
        This tests checks if the output written by the model is a valid input
        for a new run
        """
        config = 'tests/small_simple.yaml'
        myniche = niche_vlaanderen.Niche()
        myniche.run_config_file(config)
        myniche = niche_vlaanderen.Niche()

        shutil.copy('_output/log.txt', 'log.txt')

        config = 'log.txt'
        myniche2 = niche_vlaanderen.Niche()
        myniche2.run_config_file(config)
예제 #7
0
 def test_overwrite_codetable_nojoin(self):
     # test should generate a warning
     myniche = niche_vlaanderen.Niche(
         ct_vegetation="tests/data/bad_ct/vegetation_noinnerjoin.csv")
     myniche.read_config_file("tests/small.yaml")
     with pytest.warns(UserWarning):
         myniche.run()
예제 #8
0
 def test_incomplete_model(self):
     myniche = niche_vlaanderen.Niche()
     myniche.set_input("mhw", "tests/data/small/msw.asc")
     with pytest.raises(NicheException):
         myniche.run()  # incomplete, keys are missing
     with pytest.raises(NicheException):
         myniche.write("_temp")  # should raise, no calculation done
예제 #9
0
 def test_overwrite_codetable(self):
     myniche = niche_vlaanderen.Niche(
         ct_vegetation="tests/data/bad_ct/one_vegetation_limited.csv",
         ct_acidity="tests/data/bad_ct/acidity_limited.csv",
         lnk_acidity="tests/data/bad_ct/lnk_acidity_limited.csv",
         ct_nutrient_level="tests/data/bad_ct/nutrient_level.csv")
     myniche.read_config_file("tests/small.yaml")
     myniche.run()
예제 #10
0
    def test_uint(self):
        myniche = niche_vlaanderen.Niche()

        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/tif/soil_code.tif")
        myniche.run(full_model=False)

        # this dataset should contain one nodata cell
        df = myniche.table
        assert np.all(df[df.presence == "no data"]["area_ha"] == 0.0625)
예제 #11
0
    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)
예제 #12
0
    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)
예제 #13
0
 def create_zwarte_beek_niche():
     myniche = niche_vlaanderen.Niche()
     input_dir = "testcase/zwarte_beek/input/"
     myniche.set_input("soil_code", input_dir + "soil_code.asc")
     myniche.set_input("mhw", input_dir + "mhw.asc")
     myniche.set_input("mlw", input_dir + "mlw.asc")
     myniche.set_input("msw", input_dir + "msw.asc")
     myniche.set_input("minerality", input_dir + "minerality.asc")
     myniche.set_input("nitrogen_atmospheric",
                       input_dir + "nitrogen_atmospheric.asc")
     myniche.set_input("nitrogen_animal", 0)
     myniche.set_input("nitrogen_fertilizer", 0)
     myniche.set_input("management", input_dir + "management.asc")
     myniche.set_input("inundation_nutrient", input_dir + "inundation.asc")
     myniche.set_input("inundation_acidity", input_dir + "inundation.asc")
     myniche.set_input("seepage", input_dir + "seepage.asc")
     myniche.set_input("rainwater", input_dir + "rainwater.asc")
     return myniche
예제 #14
0
def cli(ctx, config, example, version):
    """Command line interface to the NICHE vegetation model
    """
    if example:
        ex = resource_filename(
                "niche_vlaanderen",
                "system_tables/example.yaml")
        with open(ex) as f:
            print(f.read())

    if config is not None:
        n = niche_vlaanderen.Niche()
        n.run_config_file(config, overwrite_ct=True)
        click.echo(n)
    if config is None and not example:
        # we should really find a neater way to show --help here by default.
        print("No config file added. Use --help for more info")

    if version:
        print("niche_vlaanderen version: " + niche_vlaanderen.__version__)
예제 #15
0
    def test_overwrite_code_table(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)

        # we expect only one vegetation type, as the codetable has only one
        self.assertEqual(1, len(myniche.occurrence))

        # we try to overwrite using a non existing key
        with pytest.raises(NicheException):
            myniche._set_ct("bla", "tests/data/bad_ct/one_vegetation.csv")

        # we try to overwrite using a non existing codetable
        with pytest.raises(NicheException):
            myniche._set_ct("ct_vegetation", "nonexisting")

        assert myniche._vegcode2name(1) == "Sphagno-Betuletum"
예제 #16
0
    def test_testcase_simple(self):
        """
        This tests runs the data from the testcase/zwarte_beek.
        Note: validation is done in the vegetation tests - not here.

        """

        myniche = niche_vlaanderen.Niche()
        input_dir = "testcase/zwarte_beek/input/"

        myniche.set_input("soil_code", input_dir + "soil_code.asc")
        myniche.set_input("mhw", input_dir + "mhw.asc")
        myniche.set_input("mlw", input_dir + "mlw.asc")
        myniche.name = "simple"
        myniche.run(full_model=False)
        tmpdir = tempfile.mkdtemp()
        myniche.write(tmpdir)
        # check tempdir contains the vegetation files
        expected_files = [
            'V01.tif', 'V02.tif', 'V03.tif', 'V04.tif', 'V05.tif', 'V06.tif',
            'V07.tif', 'V08.tif', 'V09.tif', 'V10.tif', 'V11.tif', 'V12.tif',
            'V13.tif', 'V14.tif', 'V15.tif', 'V16.tif', 'V17.tif', 'V18.tif',
            'V19.tif', 'V20.tif', 'V21.tif', 'V22.tif', 'V23.tif', 'V24.tif',
            'V25.tif', 'V26.tif', 'V27.tif', 'V28.tif', 'log.txt',
            'summary.csv'
        ]

        expected_files = ["simple_" + i for i in expected_files]

        dir = os.listdir(tmpdir)

        if sys.version_info < (3, 2):
            self.assertItemsEqual(expected_files, dir)
        else:
            self.assertCountEqual(expected_files, dir)

        shutil.rmtree(tmpdir)
예제 #17
0
 def test_invalidfile(self):
     n = niche_vlaanderen.Niche()
     with pytest.raises(RasterioIOError):
         n.set_input("msw", "nonexistingfile")
예제 #18
0
 def test_run_configuration(self):
     config = 'tests/small_simple.yaml'
     myniche = niche_vlaanderen.Niche()
     myniche.run_config_file(config)
예제 #19
0
 def test_run_configuration_numeric(self):
     config = 'tests/small_ct.yaml'
     myniche = niche_vlaanderen.Niche()
     myniche.run_config_file(config)
예제 #20
0
 def test_invalid_input_type(self):
     n = niche_vlaanderen.Niche()
     with pytest.raises(NicheException):
         n.set_input("bla", "testcase/zwarte_beek/input/soil_code.asc")
예제 #21
0
 def create_small():
     myniche = niche_vlaanderen.Niche()
     myniche.read_config_file("tests/small.yaml")
     return myniche
예제 #22
0
 def test_overwrite_codetable_nonexisting(self):
     # assume error - file does not exist
     with pytest.raises(NicheException):
         niche_vlaanderen.Niche(ct_vegetation="nonexisting file")
예제 #23
0
 def test_read_configuration(self):
     config = 'tests/small_simple.yaml'
     myniche = niche_vlaanderen.Niche()
     myniche.read_config_file(config)
     myniche.run(full_model=False)