示例#1
0
    def test_load_already_open_netcdf_error(self, patched, tmp_path):
        """
        Test that a checkpoint can be loaded when the load expects there to be
        a netcdf file. This will create a new netcdf file and raise a
        warning.
        """
        # define a yaml with an output, and checkpoint
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml', {
            'save_checkpoint': True,
            'save_eta_grids': True
        })
        _delta = DeltaModel(input_file=p)

        # make mocks
        _delta.log_info = mock.MagicMock()
        _delta.logger = mock.MagicMock()
        _delta.init_output_file = mock.MagicMock()

        # close the file so can be safely opened in load
        _delta.output_netcdf.close()

        # check that files exist, and then open the nc back up
        assert os.path.isfile(
            os.path.join(_delta.prefix, 'pyDeltaRCM_output.nc'))
        assert os.path.isfile(os.path.join(_delta.prefix, 'checkpoint.npz'))
        _ = Dataset(os.path.join(_delta.prefix, 'pyDeltaRCM_output.nc'))

        # now try to resume a model and should throw error
        with pytest.raises(RuntimeError):
            _delta.load_checkpoint()
示例#2
0
    def test_load_wo_netcdf_expected(self, patched, tmp_path):
        """
        Test that a checkpoint can be loaded when the load expects there to be
        a netcdf file. This will create a new netcdf file and raise a
        warning.
        """
        # define a yaml with NO outputs, but checkpoint
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml', {
            'save_checkpoint': True,
            'save_eta_grids': True
        })
        _delta = DeltaModel(input_file=p)

        # make mocks
        _delta.log_info = mock.MagicMock()
        _delta.logger = mock.MagicMock()
        _delta.init_output_file = mock.MagicMock()

        # close the file so can be safely opened in load
        _delta.output_netcdf.close()

        # check that files exist, and then delete nc
        assert os.path.isfile(
            os.path.join(_delta.prefix, 'pyDeltaRCM_output.nc'))
        assert os.path.isfile(os.path.join(_delta.prefix, 'checkpoint.npz'))
        os.remove(os.path.join(_delta.prefix, 'pyDeltaRCM_output.nc'))

        # now mess up a field
        _eta0 = np.copy(_delta.eta)
        _rand_field = np.random.uniform(0, 1, size=_delta.eta.shape)
        _delta.eta = _rand_field
        assert np.all(_delta.eta == _rand_field)

        # now resume from the checkpoint to restore the field
        with pytest.warns(UserWarning, match=r'NetCDF4 output *.'):
            _delta.load_checkpoint()

        # check that fields match
        assert np.all(_delta.eta == _eta0)
        assert _delta._save_iter == 0

        # assertions on function calls
        _delta.log_info.assert_called()
        _delta.logger.warning.assert_called()
        _delta.init_output_file.assert_called()
        patched.assert_called()
示例#3
0
    def test_load_standard_grid(self, patched, tmp_path):
        """Test that a run can be resumed when there are outputs.
        """
        # create one delta, just to have a checkpoint file
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml', {
            'save_checkpoint': True,
            'save_eta_grids': True
        })
        _delta = DeltaModel(input_file=p)

        # make mocks
        _delta.log_info = mock.MagicMock()
        _delta.logger = mock.MagicMock()
        _delta.init_output_file = mock.MagicMock()

        # close the file so can be safely opened in load
        _delta.output_netcdf.close()

        # check checkpoint exists
        assert os.path.isfile(os.path.join(_delta.prefix, 'checkpoint.npz'))
        assert os.path.isfile(
            os.path.join(_delta.prefix, 'pyDeltaRCM_output.nc'))

        # now mess up a field
        _eta0 = np.copy(_delta.eta)
        _rand_field = np.random.uniform(0, 1, size=_delta.eta.shape)
        _delta.eta = _rand_field
        assert np.all(_delta.eta == _rand_field)

        # now resume from the checkpoint to restore the field
        _delta.load_checkpoint()

        # check that fields match
        assert np.all(_delta.eta == _eta0)

        # assertions on function calls
        _call = [mock.call('Renaming old NetCDF4 output file', verbosity=2)]
        _delta.log_info.assert_has_calls(_call, any_order=True)
        _delta.logger.assert_not_called()
        _delta.init_output_file.assert_not_called()
        patched.assert_called()
示例#4
0
    def test_load_wo_netcdf_not_expected(self, patched, tmp_path):
        """
        Test that a checkpoint can be loaded when the load does not expect
        there to be any netcdf file.
        """
        # create one delta, just to have a checkpoint file
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml',
                                     {'save_checkpoint': True})
        _delta = DeltaModel(input_file=p)

        # make mocks
        _delta.log_info = mock.MagicMock()
        _delta.logger = mock.MagicMock()
        _delta.init_output_file = mock.MagicMock()

        assert os.path.isfile(os.path.join(_delta.prefix, 'checkpoint.npz'))
        assert not os.path.isfile(
            os.path.join(_delta.prefix, 'pyDeltaRCM_output.nc'))

        # now mess up a field
        _eta0 = np.copy(_delta.eta)
        _rand_field = np.random.uniform(0, 1, size=_delta.eta.shape)
        _delta.eta = _rand_field
        assert np.all(_delta.eta == _rand_field)

        # now resume from the checkpoint to restore the field
        _delta.load_checkpoint()

        # check that fields match
        assert np.all(_delta.eta == _eta0)

        # assertions on function calls
        _delta.log_info.assert_called()
        _delta.logger.assert_not_called()
        _delta.init_output_file.assert_not_called()
        patched.assert_called()