diff --git a/pybamm/parameters/parameter_values.py b/pybamm/parameters/parameter_values.py index 7e7668d654..161e54815f 100644 --- a/pybamm/parameters/parameter_values.py +++ b/pybamm/parameters/parameter_values.py @@ -569,3 +569,19 @@ def evaluate(self, symbol): def _ipython_key_completions_(self): return list(self._dict_items.keys()) + + def export_csv(self, filename): + + # process functions and data to output + # like they appear in inputs csv files + parameter_output = {} + for key, val in self.items(): + if callable(val): + val = "[function]" + val.__name__ + elif isinstance(val, tuple): + val = "[data]" + val[0] + parameter_output[key] = [val] + + df = pd.DataFrame(parameter_output) + df = df.transpose() + df.to_csv(filename, header=None) diff --git a/tests/unit/test_parameters/test_parameter_values.py b/tests/unit/test_parameters/test_parameter_values.py index 39156f62a6..7c00f7a0b1 100644 --- a/tests/unit/test_parameters/test_parameter_values.py +++ b/tests/unit/test_parameters/test_parameter_values.py @@ -8,6 +8,8 @@ import unittest import tests.shared as shared +import pandas as pd + class TestParameterValues(unittest.TestCase): def test_read_parameters_csv(self): @@ -523,6 +525,26 @@ def test_evaluate(self): with self.assertRaises(ValueError): parameter_values.evaluate(array) + def test_export_csv(self): + def some_function(self): + return None + + example_data = ("some_data", [0, 1, 2]) + + parameter_values = pybamm.ParameterValues( + {"a": 0.1, "b": some_function, "c": example_data} + ) + + filename = "parameter_values_test.csv" + + parameter_values.export_csv(filename) + + df = pd.read_csv(filename, index_col=0, header=None) + + self.assertEqual(df[1]["a"], "0.1") + self.assertEqual(df[1]["b"], "[function]some_function") + self.assertEqual(df[1]["c"], "[data]some_data") + if __name__ == "__main__": print("Add -v for more debug output")