Skip to content

Commit

Permalink
#958 added export_csv to parameter values and relevant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottmar93 committed Apr 15, 2020
1 parent 9b72b1e commit 48da682
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 22 additions & 0 deletions tests/unit/test_parameters/test_parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 48da682

Please sign in to comment.