Skip to content

Commit

Permalink
Fixed saving of data in CV (#40)
Browse files Browse the repository at this point in the history
* Added a recursive funciton to get rid of internal pd in the saving dict
* Fixed the cli
* Make the logo smaller
  • Loading branch information
fuzhanrahmanian authored Dec 11, 2023
1 parent ea29079 commit 2ef9db4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
.. image:: logo.png
:align: center


MADAP
~~~~~

.. image:: logo.png
:align: center
:width: 240px

Modular and Autonomous Data Analysis Platform (MADAP) is a
well-documented python package which can be used for electrochmeical
data analysis.
Expand Down
15 changes: 7 additions & 8 deletions madap/echem/voltammetry/voltammetry_CV.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def __init__(self, current, voltage, time_params, cycle_list, args, scan_rate=No
self.cathodic_peak_params = {}
self.E_half_params = {}
self.smoothing_window_size = 1
self.fitting_window_size = int(args.fitting_window_size) if args.fitting_window_size is not None else 30
self.data = None
self.temperature = float(args.temperature) if args.temperature is not None else 298.15 # Unit: K
self.cycle_list = cycle_list
Expand Down Expand Up @@ -245,10 +244,10 @@ def _calculate_diffusion_coefficient(self, i_peak, scan_rate):
def _find_peak_and_tafel_params(self):
""" Find the peak parameters for each anodic and cathodic peak.
"""
for cycle, tafel_data in self.E_half_params.items():
tafel_data = {}
for cycle, _ in self.E_half_params.items():
self.tafel_data[cycle] = {}
for pair in self.E_half_params[cycle].keys():
tafel_data[pair] = {}
self.tafel_data[cycle][pair] = {}
peak_anodic_number = self.E_half_params[cycle][pair]['anodic_peak']
peak_cathodic_number = self.E_half_params[cycle][pair]['cathodic_peak']

Expand Down Expand Up @@ -323,7 +322,7 @@ def _find_height_of_cathodic_peak_current(self, cycle, pair, peak_cathodic_numbe
# find the index of the voltage where the current is half of the peak current
index_of_I_half_voltage = max(filter_cathodic_data.index[filter_cathodic_data['voltage'] >= self.E_half_params[cycle][pair]['E_half']])
# find the fitting window size
fitting_window = int(len(filter_cathodic_data[:index_of_I_half_voltage]) / 6) #self.fitting_window_size)
fitting_window = int(len(filter_cathodic_data[:index_of_I_half_voltage]) / 6)

for i in range(len(smoothened_data.loc[:index_of_I_half_voltage])):
# select two points and fit a line
Expand Down Expand Up @@ -374,7 +373,7 @@ def _find_height_of_anodic_peak_current(self, cycle, pair, peak_anodic_number, f
# Smoothen the filtered data
filter_anodic_data['current'] = filter_anodic_data['current'].rolling(window=self.smoothing_window_size).mean()
index_of_I_half_voltage = max(filter_anodic_data.index[filter_anodic_data['voltage'] <= self.E_half_params[cycle][pair]['E_half']])
fitting_window = int(len(filter_anodic_data[:index_of_I_half_voltage]) / 6) #self.fitting_window_size)
fitting_window = int(len(filter_anodic_data[:index_of_I_half_voltage]) / 6)

for i in range(len(filter_anodic_data.loc[:index_of_I_half_voltage-fitting_window])):
# select two points and fit a line
Expand Down Expand Up @@ -733,10 +732,10 @@ def save_data(self, save_dir:str, optional_name:str = None):

added_data = {**self.E_half_params, **self.anodic_peak_params, **self.cathodic_peak_params,
"Smoothing window size": self.smoothing_window_size,
"Fitting window size": self.fitting_window_size,
"Temperature [K]": self.temperature}

processed_data = utils.convert_numpy_to_python(added_data)
processed_data = utils.convert_from_pd(processed_data)
utils.save_data_as_json(save_dir, processed_data, name)

self._save_data_with_name(optional_name, self.__class__.__name__, save_dir, self.data)
Expand All @@ -760,7 +759,7 @@ def perform_all_actions(self, save_dir:str, plots:list, optional_name:str = None
self.plot(save_dir, plots, optional_name=optional_name)
except ValueError as e:
raise e
#self.save_data(save_dir=save_dir, optional_name=optional_name)
self.save_data(save_dir=save_dir, optional_name=optional_name)


@property
Expand Down
19 changes: 19 additions & 0 deletions madap/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ def append_to_save_data(directory, added_data, name):
save_data_as_json(directory, data, name)


def convert_from_pd(obj):
"""Helper to convert pandas data to python data
Args:
obj (pandas.DataFrame): The data that should be converted
Returns:
dict: The converted data
"""

if isinstance(obj, pd.Series):
return obj.to_dict()
if isinstance(obj, dict):
return {k: convert_from_pd(v) for k, v in obj.items()}
if isinstance(obj, list):
return [convert_from_pd(x) for x in obj]
else:
return obj


def convert_numpy_to_python(data):
"""Convert numpy data to python data
Expand Down
3 changes: 1 addition & 2 deletions madap_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def _analyze_parser_args():
if proc.voltammetry_procedure == "cv":
cv = first_parser.add_argument_group("Options for the CV procedure")
cv.add_argument("-plcy", "--cycle_list", required=False, default=None,
help="list of cycles to be plotted. \n format: [1, 2, 3] \n if it is not specified, all cycles will be plotted",
required=False, default=None)
help="list of cycles to be plotted. \n format: [1, 2, 3] \n if it is not specified, all cycles will be plotted")
cv.add_argument("-pl", "--plots", required=True, choices=["E-t", "I-t", "Peak Scan", "CV", "Tafel"],
nargs="+", help="plots to be generated")
cv.add_argument("-temp", "--temperature", type=float, required=False, default=None,
Expand Down

0 comments on commit 2ef9db4

Please sign in to comment.