Commit 6a94694b authored by William Clarke's avatar William Clarke
Browse files

Added uncertaintites and mapped parameters to dynamic results save.

parent 9bf07fd8
......@@ -116,6 +116,12 @@ def test_dynRes_newton(fixed_ratio_mrs):
assert np.allclose(res_obj.std_dyn, np.sqrt(np.diagonal(res_obj.cov_dyn)))
assert isinstance(res_obj.std, pd.DataFrame)
assert res_obj.std.shape == (2, 8)
assert isinstance(res_obj.mapped_params, pd.DataFrame)
assert res_obj.mapped_params.shape == (2, 8)
def test_dynRes_mcmc(fixed_ratio_mrs):
"""Test mcmc optimiser specific components"""
......@@ -133,16 +139,22 @@ def test_dynRes_mcmc(fixed_ratio_mrs):
res = dyn_obj.fit(method='MH', mh_jumps=20)
res_obj = res['result']
assert isinstance(res_obj.cov, pd.DataFrame)
assert res_obj.cov.shape == (10, 10)
assert isinstance(res_obj.cov_dyn, pd.DataFrame)
assert res_obj.cov_dyn.shape == (10, 10)
assert isinstance(res_obj.corr_dyn, pd.DataFrame)
assert res_obj.corr_dyn.shape == (10, 10)
assert isinstance(res_obj.std_dyn, pd.Series)
assert res_obj.std_dyn.shape == (10,)
assert isinstance(res_obj.corr, pd.DataFrame)
assert res_obj.corr.shape == (10, 10)
assert np.allclose(res_obj.std_dyn, np.sqrt(np.diagonal(res_obj.cov_dyn)))
assert isinstance(res_obj.std, pd.Series)
assert res_obj.std.shape == (10,)
assert isinstance(res_obj.std, pd.DataFrame)
assert res_obj.std.shape == (2, 8)
assert np.allclose(res_obj.std, np.sqrt(np.diagonal(res_obj.cov)))
assert isinstance(res_obj.mapped_params, pd.DataFrame)
assert res_obj.mapped_params.shape == (2, 8)
def test_load_save(fixed_ratio_mrs, tmp_path):
......@@ -166,8 +178,12 @@ def test_load_save(fixed_ratio_mrs, tmp_path):
assert_frame_equal(res._data, res_loaded._data)
assert_frame_equal(res._init_x, res_loaded._init_x)
assert (tmp_path / 'res_save_test' / 'dyn_std.csv').is_file()
assert (tmp_path / 'res_save_test' / 'mapped_mean.csv').is_file()
assert (tmp_path / 'res_save_test' / 'mapped_std.csv').is_file()
res.save(tmp_path / 'res_save_test2', save_dyn_obj=True)
res_loaded2 = dyn.load_dyn_result(tmp_path / 'res_save_test2')
assert_frame_equal(res._data, res_loaded2._data)
assert_frame_equal(res._init_x, res_loaded2._init_x)
assert_frame_equal(res._init_x, res_loaded2._init_x)
......@@ -129,6 +129,11 @@ class dynRes:
self._data.to_csv(save_dir / 'dyn_results.csv')
self._init_x.to_csv(save_dir / 'init_results.csv')
# Save the uncertainties and mapped parameters as well - for ease of results writing
self.std_dyn.to_csv(save_dir / 'dyn_std.csv')
self.mapped_params.to_csv(save_dir / 'mapped_mean.csv')
self.std.to_csv(save_dir / 'mapped_std.csv')
# If selected save the dynamic object to a nested directory
if save_dyn_obj:
self._dyn.save(save_dir / 'dynmrs_obj', save_mrs_list=True)
......@@ -237,6 +242,48 @@ class dynRes:
"""Free names from stored dynamic object"""
return self._dyn.free_names
# Methods implemented in child classes
@property
def cov_dyn(self):
"""Implemented in child class
Returns the covariance matrix of free parameters
"""
pass
@property
def corr_dyn(self):
"""Implemented in child class
Returns the correlation matrix of free parameters
"""
pass
@property
def std_dyn(self):
"""Implemented in child class
Returns the standard deviations of the free parameters
"""
pass
@property
def std(self):
"""Implemented in child class
Returns the standard deviations of the mapped parameters
"""
pass
# TODO: Do we want to keep this and the similar method in the parent class?
@property
def mapped_params(self):
"""Implemented in child class
Returns mapped parameters as pandas data series
"""
pass
def collected_results(self, to_file=None):
"""Collect the results of dynamic MRS fitting
......@@ -461,7 +508,7 @@ class dynRes_mcmc(dynRes):
super().__init__(samples, dyn, init)
@property
def cov(self):
def cov_dyn(self):
"""Returns the covariance matrix of free parameters
:return: Covariance matrix as a DataFrame
......@@ -470,7 +517,7 @@ class dynRes_mcmc(dynRes):
return self.data_frame.cov()
@property
def corr(self):
def corr_dyn(self):
"""Returns the correlation matrix of free parameters
:return: Covariance matrix as a DataFrame
......@@ -479,7 +526,7 @@ class dynRes_mcmc(dynRes):
return self.data_frame.corr()
@property
def std(self):
def std_dyn(self):
"""Returns the standard deviations of the free parameters
:return: Std as data Series
......@@ -487,6 +534,20 @@ class dynRes_mcmc(dynRes):
"""
return self.data_frame.std()
@property
def std(self):
"""Returns the standard deviations of the mapped parameters
:return: Std as data Series
:rtype: pandas.Series
"""
return pd.DataFrame(self.mapped_parameters.std(axis=0), columns=self.mapped_names)
# TODO: Do we want to keep this and the similar method in the parent class?
@property
def mapped_params(self):
return pd.DataFrame(self.mapped_parameters.mean(axis=0), columns=self.mapped_names)
class dynRes_newton(dynRes):
......@@ -568,9 +629,9 @@ class dynRes_newton(dynRes):
:return: Std as data Series
:rtype: pandas.Series
"""
return pd.Series(self._std, self._dyn.vm.mapped_names)
return pd.DataFrame(np.hstack(list(self._std.values())), columns=self.mapped_names)
# TODO: Do we want to keep this and the similar method in the parent class?
@property
def mapped_params(self):
return pd.Series(self._mapped_params, self._dyn.vm.mapped_names)
return pd.DataFrame(self.mapped_parameters.mean(axis=0), columns=self.mapped_names)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment