<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 1. Settings
Step1:  4. Looking at the data
Summaries
Step2:  Cycles
Step3:  Selecting specific cells and investigating them
Step4:  Let's see how the smoothing (interpolation) method works
Step5:  Using hvplot for plotting summaries
You can for example use hvplot for looking more at your summary data
Step6:  Looking more in-depth and utilising advanced features
OCV relaxation points
Picking out 5 points on each OCV relaxation curve (distributed by last, last/2, last/2/2, ..., first).
Step7:  Looking closer at some summary-plots
Step8:  5. Checking for more details per cycle
A. pick the CellpyData object for one of the cells
Step9:  B. Get some voltage curves for some cycles and plot them
The method get_cap can be used to extract voltage curves.
Step10:  Looking at some dqdv data
Get capacity cycles and make dqdv using the ica module
Step11:  Put it in a for-loop for plotting many ica plots
Step12:  Get all the dqdv data in one go
<ASSISTANT_TASK:>
Python Code:
%load_ext autoreload
%autoreload 2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cellpy
from cellpy import log
from cellpy import cellreader
from cellpy import prms
from cellpy import prmreader
from cellpy.utils import batch
# import holoviews as hv
%matplotlib inline
# hv.extension('bokeh')
log.setup_logging(default_level="DEBUG")
## Uncomment this and run for checking your cellpy parameters.
# prmreader.info()
filepath = r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\2019_types.res"
filepath = [
    r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\20181126_cen41_02_cc_01.res",
    r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\20181126_cen41_02_cc_02.res",
    r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\20181126_cen41_02_cc_03.res",
    r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\20181126_cen41_02_cc_04.res",
    r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\20181126_cen41_02_cc_05.res",
    r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\20181126_cen41_02_cc_06.res",
]
filepath2 = filepath[0]
m = 0.374433
outfilepath = r"C:\Scripting\MyFiles\development_cellpy\dev_data\arbin\2019_types.h5"
prms.Paths.rawdatadir = r"C:\ExperimentalData\BatteryTestData\Arbin\RAW"
cell = cellreader.CellpyData()
cell.from_raw(filepath)
cell2 = cellreader.CellpyData()
cell2.from_raw(filepath2)
cell.set_mass(m)
cell2.set_mass(m)
cell.make_step_table()
cell2.make_step_table()
cell.make_summary()
cell2.make_summary()
dataset = cell.dataset
dataset2 = cell2.dataset
dataset.summary
dataset.steps
dataset.raw
dataset.raw.describe()
dataset.raw.dtypes
dataset2.raw.dtypes
dataset.raw.Step_Index.unique()
dataset2.raw.Step_Index.unique()
dataset.summary.dtypes
dataset.steps.dtypes
cell.save(outfilepath)
Explanation: 1. Settings
End of explanation
# Plot the charge capacity and the C.E. (and resistance) vs. cycle number (standard plot)
b.plot_summaries()
# Show the journal pages
# b.experiment.journal.pages.head()
# Show the most important part of the journal pages
b.view
# b.experiment.status()
# b.summaries.head()
Explanation: 4. Looking at the data
Summaries
End of explanation
%%opts Curve (color=hv.Palette('Magma'))
voltage_curves = dict()
for label in b.experiment.cell_names:
    d = b.experiment.data[label]
    curves = d.get_cap(label_cycle_number=True, interpolated=True, number_of_points=100)
    curve = hv.Curve(curves, kdims=["capacity", "cycle"], vdims="voltage").groupby("cycle").overlay().opts(show_legend=False)
    voltage_curves[label] = curve
NdLayout = hv.NdLayout(voltage_curves, kdims='label').cols(3)
NdLayout
%%opts Curve (color=hv.Palette('Magma'))
ocv_curves = dict()
for label in b.experiment.cell_names:
    d = b.experiment.data[label]
    ocv_data = d.get_ocv(direction="up", number_of_points=40)
    ocv_curve = hv.Curve(ocv_data, kdims=["Step_Time", "Cycle_Index"], vdims="Voltage").groupby("Cycle_Index").overlay().opts(show_legend=False)
    ocv_curves[label] = ocv_curve
NdLayout = hv.NdLayout(ocv_curves, kdims='label').cols(3)
NdLayout
Explanation: Cycles
End of explanation
# This will show you all your cell names
cell_labels = b.experiment.cell_names
cell_labels
# This is how to select the data (CellpyData-objects)
data1 = b.experiment.data["20160805_test001_45_cc"]
data2 = b.experiment.data["20160805_test001_47_cc"]
Explanation: Selecting specific cells and investigating them
End of explanation
# get voltage curves
df_cycles1 = data1.get_cap(
    method="back-and-forth",
    categorical_column=True,
    label_cycle_number=True,
    interpolated=False,
)
# get interpolated voltage curves
df_cycles2 = data1.get_cap(
    method="back-and-forth",
    categorical_column=True,
    label_cycle_number=True,
    interpolated=True,
    dx=0.1,
    number_of_points=100,
)
%%opts Scatter [width=600] (color="red", alpha=0.9, size=12)
single_curve = hv.Curve(df_cycles1, kdims=["capacity", "cycle"], vdims="voltage", label="not-smoothed").groupby("cycle")
single_scatter = hv.Scatter(df_cycles2, kdims=["capacity", "cycle"], vdims="voltage", label="smoothed").groupby("cycle")
single_scatter * single_curve
Explanation: Let's see how the smoothing (interpolation) method works
End of explanation
import hvplot.pandas
# hvplot does not like infinities
s = b.summaries.replace([np.inf, -np.inf], np.nan)
layout = (
    s["coulombic_efficiency"].hvplot()
    + s["discharge_capacity"].hvplot() * s["charge_capacity"].hvplot()
)
layout.cols(1)
s["cumulated_coulombic_efficiency"].hvplot()
Explanation: Using hvplot for plotting summaries
You can for example use hvplot for looking more at your summary data
End of explanation
from cellpy.utils.batch_tools.batch_analyzers import OCVRelaxationAnalyzer
print(" analyzing ocv relaxation data ".center(80, "-"))
analyzer = OCVRelaxationAnalyzer()
analyzer.assign(b.experiment)
analyzer.direction = "down"
analyzer.do()
dfs = analyzer.last
df_file_one, _df_file_two = dfs
# keeping only the columns with voltages
ycols = [col for col in df_file_one.columns if col.find("point") >= 0]
# removing the first ocv rlx (relaxation before starting cycling)
df = df_file_one.iloc[1:, :]
# tidy format
df = df.melt(id_vars="cycle", var_name="point", value_vars=ycols, value_name="voltage")
curve = (
    hv.Curve(df, kdims=["cycle", "point"], vdims="voltage")
    .groupby("point")
    .overlay()
    .opts(xlim=(1, 10), width=800)
)
scatter = (
    hv.Scatter(df, kdims=["cycle", "point"], vdims="voltage")
    .groupby("point")
    .overlay()
    .opts(xlim=(1, 10), ylim=(0.7, 1))
)
layout = hv.Layout(curve * scatter)
layout.cols(1)
Explanation: Looking more in-depth and utilising advanced features
OCV relaxation points
Picking out 5 points on each OCV relaxation curve (distributed by last, last/2, last/2/2, ..., first).
End of explanation
b.summary_columns
discharge_capacity = b.summaries.discharge_capacity
charge_capacity = b.summaries.charge_capacity
coulombic_efficiency = b.summaries.coulombic_efficiency
ir_charge = b.summaries.ir_charge
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(discharge_capacity)
ax1.set_ylabel("capacity ")
ax2.plot(ir_charge)
ax2.set_xlabel("cycle")
ax2.set_ylabel("resistance")
Explanation: Looking closer at some summary-plots
End of explanation
# Lets check what cells we have
cell_labels = b.experiment.cell_names
cell_labels
# OK, then I choose one of them
data = b.experiment.data["20160805_test001_45_cc"]
Explanation: 5. Checking for more details per cycle
A. pick the CellpyData object for one of the cells
End of explanation
cap = data.get_cap(categorical_column=True)
cap.head()
fig, ax = plt.subplots()
ax.plot(cap.capacity, cap.voltage)
ax.set_xlabel("capacity")
ax.set_ylabel("voltage")
cv = data.get_cap(method="forth")
fig, ax = plt.subplots()
ax.set_xlabel("capacity")
ax.set_ylabel("voltage")
ax.plot(cv.capacity, cv.voltage)
c4 = data.get_cap(cycle=4, method="forth-and-forth")
c10 = data.get_cap(cycle=10, method="forth-and-forth")
fig, ax = plt.subplots()
ax.set_xlabel("capacity")
ax.set_ylabel("voltage")
ax.plot(c4.capacity, c4.voltage, "ro", label="cycle 4")
ax.plot(c10.capacity, c10.voltage, "bs", label="cycle 22")
ax.legend();
Explanation: B. Get some voltage curves for some cycles and plot them
The method get_cap can be used to extract voltage curves.
End of explanation
from cellpy.utils import ica
v4, dqdv4 = ica.dqdv_cycle(
    data.get_cap(4, categorical_column=True, method="forth-and-forth")
)
v10, dqdv10 = ica.dqdv_cycle(
    data.get_cap(10, categorical_column=True, method="forth-and-forth")
)
plt.plot(v4, dqdv4, label="cycle 4")
plt.plot(v10, dqdv10, label="cycle 10")
plt.legend();
Explanation: Looking at some dqdv data
Get capacity cycles and make dqdv using the ica module
End of explanation
fig, ax = plt.subplots()
for cycle in data.get_cycle_numbers():
    d = data.get_cap(cycle, categorical_column=True, method="forth-and-forth")
    if not d.empty:
        v, dqdv = ica.dqdv_cycle(d)
        ax.plot(v, dqdv)
    else:
        print(f"cycle {cycle} seems to be missing or corrupted")
Explanation: Put it in a for-loop for plotting many ica plots
End of explanation
hv.extension("bokeh")
tidy_ica = ica.dqdv_frames(data)
cycles = list(range(1, 3)) + [10, 11, 12, 15]
tidy_ica = tidy_ica.loc[tidy_ica.cycle.isin(cycles), :]
%%opts Curve [xlim=(0,1)] (color=hv.Palette('Magma'), alpha=0.9) NdOverlay [legend_position='right', width=800, height=500]
curve4 = (hv.Curve(tidy_ica, kdims=['voltage'], vdims=['dq', 'cycle'], label="Incremental capacity plot")
          .groupby("cycle")
          .overlay()
         )
curve4
Explanation: Get all the dqdv data in one go
End of explanation
<END_TASK>