Loading...

Estimating Lithium-ion Battery Health

Introduction

Lithium-based batteries - their cycles characteristics and aging

Lithium-ion (or Li-ion) batteries are rechargeable batteries used for a variety of electronic devices, which range from eletric vehicles, smartphones, and even satellites.

However, despite their wide adoption, research isn’t mature enough to avoid problems with battery health and safety, and given the ubiquity of consumer electronics using the technology, this has led to some poor outcomes that range from poor user-experience to public safety concerns (see, for example, the Samsung Galaxy Note 7 explosions from 2016).

Dataset

In this example of predictive maintenance, we propose a data-driven method to estimate the health of a battery using the Li-ion battery dataset released by NASA.

This dataset includes information on Li-ion batteries over several charge and discharge cycles at room temperature. Charging was at a constant current (CC) at 1.5A until the battery voltage reached 4.2V and then continued in a constant voltage (CV) mode until the charge current dropped to 20mA.

Discharge was at a constant current (CC) level of 2A until the battery voltage fell to 2.7V.

The dataset includes the following:

  • Voltage_measured: Battery’s terminal voltage (Volts) for charging and discharging cycles.

  • Current_measured: Battery’s output current (Amps) for charging and discharging cycles.

  • Temperature_measured: Battery temperature (degree Celsius).

  • Current_charge: Current measured at charger for charging cycles and at load for discharging cycles (Amps).

  • Voltage_charge: Voltage measured at charger for charging cycles and at load for discharging ones (Volts).

  • Start_time: Starting time of the cycle.

  • Time: Time in seconds after the starting time for the cycle (seconds).

  • Capacity: Battery capacity (Ahr) for discharging until 2.7V. Battery capacity is the product of the current drawn from the battery (while the battery is able to supply the load) until its voltage drops lower than a certain value for each cell.

We will follow the data science cycle (Data Exploration - Data Preparation - Data Modeling - Model Evaluation - Model Deployment) to solve this problem.

Initialization

This example uses the following version of vastorbit:

import vastorbit as vo
vo.__version__

Connect to VAST. This example uses an existing connection called VASTDSN. For details on how to create a connection, see the Connection tutorial. You can skip the below cell if you already have an established connection.

vo.connect("VASTDSN")

Let us now ingest the data.

# load datasets as VastFrame objects
battery5  = vo.read_csv("battery.csv")

# Display
battery5
123
voltage_measured
Double
100%
123
current_measured
Double
100%
123
temperature_measured
Double
100%
123
current_charge
Double
100%
123
voltage_charge
Double
100%
123
time
Double
100%
Abc
type
Varchar(50)
100%
📅
start_time
Timestamp(3)
99%
123
ambient_temp
Integer
100%
123
capacity
Double
8%
13.998594675287321.508657813039225.38240872599841.54.748807.016charge2008-05-09 17:26:5124[null]
23.998602879175111.5110786063455326.2336120064021.4984.748871.812charge2008-05-06 22:46:4024[null]
33.998614121404641.5094637721751426.48768625004511.4984.748639.906charge2008-05-14 06:18:5724[null]
43.998709768541921.5103506346796327.76246951290621.54.7531631.829charge2008-04-29 13:44:4624[null]
53.998746243143871.5113142410364326.87131133642321.54.748597.594charge2008-05-15 16:33:2424[null]
63.999042119056891.5094236198549125.56376161788281.4984.7531289.64charge2008-05-01 09:43:1824[null]
73.999082122835091.512276790196525.99154277879261.4984.742379.453charge2008-05-25 21:43:4024[null]
83.999140450911421.5078224171797925.62413445800371.4984.7371428.766charge2008-04-18 17:34:2224[null]
93.999561043347661.514782728337825.80606743450691.54.7421628.203charge2008-04-22 21:56:2124[null]
103.999697528636651.5110905280507326.97962166854141.4984.748773.64charge2008-05-12 20:23:5124[null]
113.999698679001271.5073050542172826.67522998399611.54.748715.985charge2008-05-13 05:58:5024[null]
123.999764275109531.5103062128982126.24782287917381.54.7531490.594charge2008-04-29 23:28:2024[null]
133.999914961661861.5122351048512525.97503225287271.4984.7531086.0charge2008-05-03 20:24:2124[null]
143.999927351769791.5112415460862325.85782522324941.54.7481445.922charge2008-04-24 23:06:3424[null]
154.000130189751261.5093426949430825.20396949915941.54.7421319.813charge2008-04-28 17:20:4724[null]
164.00018864142721.5109321341520326.67471474174331.54.7311622.719charge2008-04-02 16:37:5124[null]
174.000203184814641.5081034665580426.87497858426111.4984.753607.687charge2008-05-15 16:33:2424[null]
184.000445927782361.5090551332646726.30749421119781.54.748781.563charge2008-05-11 08:19:2724[null]
194.000518129751121.5107006506075124.97610501204831.54.7481371.015charge2008-04-26 07:25:2324[null]
204.000554805060391.5123289139349825.63765032992391.54.748887.219charge2008-05-06 12:50:2024[null]

Warning

This example uses a sample dataset. For the full analysis, you should consider using the complete dataset.

Understanding the Data

Let’s perform a few aggregations with describe() to get a high-level overview of the dataset.

battery5.describe()
countmeanstdminapprox_25%approx_50%approx_75%max
"voltage_measured"35288.04.1023764128958080.215692815748569552.592947964776554.0801227938555564.2050985902816564.2059206323027994.98472045064886
"current_measured"35288.00.363517998709487530.9102328508247932-3.793335274646680.042270323788895770.17043991515769741.1576240041664591.5215190253818
"temperature_measured"35288.026.3819139088263452.80145774098126823.214801785728124.49242279124126825.49020723122078327.36795367019744841.2725661150757
"current_charge"35288.00.63314098843799830.7385171402476751-3.7850.0585238389811174940.2659733408155631.49652349263745471.9984
"voltage_charge"35288.04.0258642598050341.20172353622969070.04.2426445140162014.302659117065124.6511249580156655.002
"time"35288.04756.1143373101443138.1008788933270.01918.87850404064174486.4364311448127417.26329627387410807.219
"ambient_temp"35288.024.00.024.024.024.024.024.0
"capacity"3076.01.5568253655271490.18203261297510371.287452522137941.38930113213561461.53863755158210141.74002316265781181.85648742081816

To get a better idea of the changes between each cycle, we look at an aggregation at their start time, duration, and voltage at the beginning and the end of each cycle.

battery5["start_time"].describe()
value
name"start_time"
dtypetimestamp(3)
count35215
min2008-04-02 13:08:17
max2008-05-28 11:09:42

To see how the voltage changes during the cycle, we extract the initial and final voltage measurements for each cycle.

battery5.analytic(
    func = "first_value",
    columns = "Voltage_measured",
    by = "start_time",
    order_by = {"Time": "asc"},
    name = "first_voltage_measured",
)
battery5.analytic(
    func = "first_value",
    columns = "Voltage_measured",
    by = "start_time",
    order_by = {"Time": "desc"},
    name = "last_voltage_measured",
)
cycling_info = battery5.groupby(
        columns = [
            "start_time",
            "type",
            "first_voltage_measured",
            "last_voltage_measured",
        ],
        expr = [
            "COUNT(*) AS nr_of_measurements",
            "MAX(Time) AS cycle_duration",
        ],
).sort("start_time")
cycling_info["cycle_id"] = "ROW_NUMBER() OVER(ORDER BY start_time)"
cycling_info
📅
start_time
Timestamp(3)
99%
Abc
type
Varchar(50)
100%
123
first_voltage_measured
Double
100%
123
last_voltage_measured
Double
100%
123
nr_of_measurements
Bigint
100%
123
cycle_duration
Double
100%
123
cycle_id
Bigint
100%
12008-04-02 13:08:17charge4.077986508414314.19138815949231427561.751
22008-04-02 15:25:41discharge3.80112163346333.30494144277741113072.8592
32008-04-02 16:37:51charge3.468788159573124.189061841085516810516.03
42008-04-02 19:43:48discharge4.18888138027913.2442521889979193509.614
52008-04-02 20:55:40charge3.679072675746974.187391928452136210401.9375
62008-04-03 00:01:06discharge4.18818673599133.32745100986863163651.6416
72008-04-03 01:12:38charge3.516029386707174.188054956074235310397.897
82008-04-03 04:16:37discharge3.868564978210422.59294796477655103309.7198
92008-04-03 05:27:49charge3.610433126295894.187315332765376210166.7189
102008-04-03 08:33:25discharge4.188298524761053.26107394236745133507.39110
112008-04-03 09:44:35charge3.550316509149834.188178509438035310352.14111
122008-04-03 12:55:10discharge3.878532193763423.26168497902227173530.012
132008-04-03 14:06:43charge3.54438598551274.188600685497775510683.6113
142008-04-03 17:17:16discharge3.84289520955383.38445289185396132917.35914
152008-04-03 18:28:47charge3.707744610493464.20739149806757499667.51515
162008-04-03 21:28:14discharge3.862650708331283.3025817288386893531.7516
172008-04-03 22:38:27charge3.539937641028064.18757378400996569901.39117
182008-04-04 01:38:15discharge4.187448221825883.232605870187963429.04718
192008-04-04 02:48:06charge3.696392761002114.20692182956778519370.67219
202008-04-04 05:48:08discharge3.936675690361052.98209271205363113231.2520

We can see from the “duration” column that charging seems to take a longer time than discharging.

Let’s visualize this trend with an animated graph.

cycling_info.animated_bar(
    ts = "start_time",
    columns = ["type", "cycle_duration"],
)

The animated graph below shows how the cycles change throughout time. Another way we can verify that charging cycles are longer than discharging cycles is by looking at the average duration of each type of cycle.

cycling_info.bar(
    ["type"],
    method = "avg",
    of = "cycle_duration",
)

In general, charging cycles are longer than discharging cycles.

Let’s examine how voltage changes between cycles and their transitions.

cycling_info = cycling_info.groupby(
    "type",
    [
        "MIN(first_voltage_measured) AS min_first_voltage",
        "AVG(first_voltage_measured) AS avg_first_voltage",
        "MAX(first_voltage_measured) AS max_first_voltage",
        "MIN(last_voltage_measured)  AS min_last_voltage",
        "AVG(last_voltage_measured)  AS avg_last_voltage",
        "MAX(last_voltage_measured)  AS max_last_voltage",
    ],
)
cycling_info
📅
start_time
Timestamp(3)
99%
Abc
type
Varchar(50)
100%
123
first_voltage_measured
Double
100%
123
last_voltage_measured
Double
100%
123
nr_of_measurements
Bigint
100%
123
cycle_duration
Double
100%
123
cycle_id
Bigint
100%
12008-04-02 13:08:17charge4.077986508414314.19138815949231427561.751
22008-04-02 15:25:41discharge3.80112163346333.30494144277741113072.8592
32008-04-02 16:37:51charge3.468788159573124.189061841085516810516.03
42008-04-02 19:43:48discharge4.18888138027913.2442521889979193509.614
52008-04-02 20:55:40charge3.679072675746974.187391928452136210401.9375
62008-04-03 00:01:06discharge4.18818673599133.32745100986863163651.6416
72008-04-03 01:12:38charge3.516029386707174.188054956074235310397.897
82008-04-03 04:16:37discharge3.868564978210422.59294796477655103309.7198
92008-04-03 05:27:49charge3.610433126295894.187315332765376210166.7189
102008-04-03 08:33:25discharge4.188298524761053.26107394236745133507.39110
112008-04-03 09:44:35charge3.550316509149834.188178509438035310352.14111
122008-04-03 12:55:10discharge3.878532193763423.26168497902227173530.012
132008-04-03 14:06:43charge3.54438598551274.188600685497775510683.6113
142008-04-03 17:17:16discharge3.84289520955383.38445289185396132917.35914
152008-04-03 18:28:47charge3.707744610493464.20739149806757499667.51515
162008-04-03 21:28:14discharge3.862650708331283.3025817288386893531.7516
172008-04-03 22:38:27charge3.539937641028064.18757378400996569901.39117
182008-04-04 01:38:15discharge4.187448221825883.232605870187963429.04718
192008-04-04 02:48:06charge3.696392761002114.20692182956778519370.67219
202008-04-04 05:48:08discharge3.936675690361052.98209271205363113231.2520

From this table, it looks like batteries are charged until they are almost full (4.2V) and discharging doesn’t begin until they are fully charged.

End-of-life (EOL) criteria for batteries is usually defined as when the battery capacity is lower than 70%-80% of its rated capacity. Since the rated capacity by the manufacturer for this battery is 2Ah, this battery is considered EOL when its capacity reaches 2Ah x 70% = 1.4Ah.

Let’s plot the capacity curve of the battery with its smoothed version and observe when it reaches the degradation criteria.

But first we need to perform some preprocessing.

discharging_data = battery5[battery5["type"] == "discharge"]
d_cap = discharging_data[["start_time", "Capacity"]].groupby(["start_time", "Capacity"])
d_cap["discharge_id"] = "ROW_NUMBER() OVER(ORDER BY start_time, Capacity)"
d_cap.rolling(
    func = "mean",
    columns = "capacity",
    window = (-100, -1),
    name = "smooth_capacity",
)
📅
start_time
Timestamp(3)
97%
123
capacity
Double
100%
123
discharge_id
Bigint
100%
123
smooth_capacity
Double
99%
12008-04-02 15:25:411.8564874208181611.7037028048511649
22008-04-02 19:43:481.8463272497199321.6889999962164293
32008-04-03 00:01:061.8353491942234131.6777173563148102
42008-04-03 04:16:371.8352625275821141.673903743313835
52008-04-03 08:33:251.8346455082120451.666082957811581
62008-04-03 12:55:101.8356616600675561.6815293861898084
72008-04-03 17:17:161.8351461429226671.6700427035222207
82008-04-03 21:28:141.8257567905665581.6543115073726005
92008-04-04 01:38:151.8247738529891391.6424005897668672
102008-04-04 05:48:081.82461326849694101.6342560990568322
112008-04-04 09:57:191.82461955268645111.638328684567316
122008-04-04 17:56:271.81420193576739121.6220932106173143
132008-04-04 22:01:541.81375215775491131.609724546530668
142008-04-05 02:20:261.81344049147358141.6055029895044595
152008-04-05 06:25:011.80259800363065151.5756552118443041
162008-04-05 10:30:321.80210690024615161.5669546758186803
172008-04-05 14:34:411.80257950082621171.571327916871881
182008-04-05 18:39:251.80306831428341181.5885740470380634
192008-04-05 22:46:351.8027776247196191.5842986349411237
202008-04-18 21:10:191.84702599493292201.6927111728445972

Now we can plot the graphs. In vastorbit we have multiple options to plot the graphs with different syntax of customization. For a complete list of all the graphs and their options check out the Chart Gallery.

Now let’s first try to plot this using Matplotlib:

import matplotlib.pyplot as plt
from matplotlib.pyplot import axhline

# Switch the plotting library to Matplotlib
vo.set_option("plotting_lib", "matplotlib")

fig = plt.figure()
ax = d_cap.plot(ts = "discharge_id", columns = ["Capacity", "smooth_capacity"])
ax.axhline(y = 1.4, label = "End-of-life criteria")
ax.set_title("Capacity degradation curve of the battery, its smoothed version and its end-of-life threshold")
ax.legend()
plt.show()

We can now try to plot it using Plotly. We can conveniently switch between the plotting libraries using:

# Switch the plotting library to Plotly
vo.set_option("plotting_lib", "plotly")
import plotly.graph_objects as go

plot = d_cap.plot(ts = "discharge_id", columns = ["Capacity", "smooth_capacity"], title = "Capacity degradation curve of the battery, its smoothed version and its end-of-life threshold")

# Add horizontal line
plot.add_hline(y = 1.4, line_width = 3, line_dash = "dash", line_color = "green")

# Add legend for the horizontal line
plot.add_trace(go.Scatter(x = [None], y = [None], mode = "lines", line = dict(color="green", width=3, dash="dash"), name = "End-of-life criteria"))

The sudden increases in battery capacity come from the self-charging property of Li-ion batteries. The smoothed graph makes the downward trend in the battery’s capacity very clear.

An important observation here is that the battery meets the EOL criteria around the 125th cycle.

Goal and Problem Modeling

Understanding battery health is important, but at the time of writing, there’s no direct way to measure it. In our case, we’ll create a degredation model to find the relationship between a battery’s overall health and the other properties in the dataset, which includes charge and discharge cycle duration, average voltage and current, etc.

One possible definition of the battery’s overall health (“state of health” or “SoH”) is the following:

Let \(Cap_{rate}\) be the rated capacity of the battery when it’s new (2Ah in our case), and \(Cap_{actual}\) be the actual capacity of the battery at a specific time. The state of health of the battery is defined as:

\[SoH = \frac{Cap_{actual}}{Cap_{rate}} \times 100\% = \frac{1}{2}Cap_{actual}\]

Data preparation

Outliet detection

Let’s start by finding and removing the global outliers from our dataset.

battery5.outliers(
    columns = [
        "Voltage_measured",
        "Current_measured",
        "Temperature_measured","Capacity",
    ],
    name = "global_outlier",
    threshold = 4.0,
)
battery5.filter("global_outlier = 0").drop("global_outlier")

Feature engineering

Since measurements like voltage and temperature tend to differ within the different cycles, we’ll create some features that can describe those cycles.

sample_cycle = battery5[battery5["Capacity"] == 1.83514614292266]
sample_cycle["Voltage_measured"].plot(ts = "Time")
sample_cycle["Temperature_measured"].plot(ts = "Time")

We’ll define new features that describe the minimum and maximum temperature during one cycle; the minimal voltage; and the time needed to reach minimum voltage and maximum temperature.

# filter for discharge cycles
discharging_data = battery5[battery5["type"] == "discharge"]

# define new features
discharge_cycle_metrics = discharging_data.groupby(
        columns = ["start_time"],
        expr = [
            "MIN(Temperature_measured) AS min_temp",
            "MAX(Temperature_measured) AS max_temp",
            "MIN(Voltage_measured) AS min_volt",
        ]
).join(
        discharging_data,
        how = "left",
        on = {"min_volt": "voltage_measured"},
        expr1 = ["*"],
        expr2 = ["Time AS time_to_reach_minvolt"],
).join(
        discharging_data,
        how = "left",
        on = {"max_temp": "temperature_measured"},
        expr1 = ["*"],
        expr2 = ["Time AS time_to_reach_maxtemp"],
)

# calculate values of SOH
discharging_data = discharging_data.groupby(["start_time", "Capacity"])
discharging_data["SOH"] = discharging_data["Capacity"] * 0.5

# define the final dataset and save it to db
final_df = discharge_cycle_metrics.join(
    discharging_data,
    on_interpolate = {"start_time": "start_time"},
    how = "left",
    expr1 = discharge_cycle_metrics.get_columns(),
    expr2 = ["SOH AS SOH"],
)

# normalize the features
final_df.normalize(
    method = "minmax",
    columns = [
        "min_temp",
        "max_temp",
        "min_volt",
        "time_to_reach_minvolt",
        "time_to_reach_maxtemp",
    ],
)

# save it to db (materialized as a table so downstream queries stay small)
final_df.to_db(name = "finaldata_battery_5", relation_type = "table")

Machine Learning

We can now build a regression model to estimate the battery’s State of Health (SOH) from the engineered features. LinearRegression offers a good balance of accuracy and interpretability for this dataset.

from vastorbit.machine_learning.vast import LinearRegression

final_model = LinearRegression(name = "btr_lr1")
final_model.fit(
    final_df,
    X = [
        "min_temp",
        "max_temp",
        "min_volt",
        "time_to_reach_minvolt",
        "time_to_reach_maxtemp",
    ],
    y = "SOH",
)

We can evaluate the model with a regression report.

final_model.regression_report()
value
explained_variance0.9612009734097492
max_error0.08195958721894148
median_absolute_error0.007948597664127859
mean_absolute_error0.012847025290883152
mean_squared_error0.15074810178462253
root_mean_squared_error0.01874546909239797
r20.9611928045786778
r2_adj0.9599724525214035
aic-299.21658904644966
bic-281.5682579751981

The predictive power of our model looks pretty good. Let’s use our model to predict the SoH of the battery. We can visualize our prediction with a plot against the true values.

# take the predicted values and then plot them along the true ones
result = final_model.predict(
    final_df,
    name = "SOH_estimates",
)
result.plot(
    ts = "start_time",
    columns = ["SOH", "SOH_estimates"],
)

Conclusion

We successfully defined a battery degradation model that can make accurate predictions about the health of a Li-ion battery. This model could be used to, for example, accurately send warnings to users when their batteries meet the EOL criteria.