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.
You can download the Jupyter Notebook of the study here.
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 VerticaPy:
import verticapy as vp
vp.__version__
Out[2]: '1.1.0'
Connect to Vertica. This example uses an existing connection called VerticaDSN.
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.
vp.connect("VerticaDSN")
Before we import the data, we’ll drop any existing schemas of the same name.
vp.drop("battery_data", method="schema")
Out[3]: True
vp.create_schema("battery_data", True)
Out[4]: True
Let us now ingest the data.
battery5 = vp.read_csv("data/data.csv")
Warning
This example uses a sample dataset. For the full analysis, you should consider using the complete dataset.
Understanding the Data¶
Let’s examine our data. Here, we use head() to retrieve the first five rows of the dataset.
123 Voltage_measured100% | ... | 123 Current_measured100% | 123 Capacity8% | |
| 1 | 2.66684542897547 | ... | -2.00971847365549 | 1.76231507040931 |
| 2 | 2.68499881852498 | ... | -2.01263017531067 | 1.81403112750839 |
| 3 | 2.69951692300468 | ... | -2.01819088625634 | 1.49084440504002 |
| 4 | 2.70742704244784 | ... | -2.01141689693154 | 1.3389145230207 |
| 5 | 2.80290487900751 | ... | -2.01245713079778 | 1.32902865651207 |
| 6 | 2.81015874386668 | ... | -2.01301807875465 | 1.3389145230207 |
| 7 | 2.86534003859311 | ... | -2.01112277845238 | 1.30779599469444 |
| 8 | 2.87532762204241 | ... | -2.01373897721455 | 1.43867093714806 |
| 9 | 2.87544665543895 | ... | -2.01381259402161 | 1.83464550821204 |
| 10 | 2.88060038984582 | ... | -2.0137387886831 | 1.76231507040931 |
| 11 | 2.88091852838522 | ... | -2.01120565508369 | 1.52852526297564 |
| 12 | 2.8898440837763 | ... | -2.01373483656512 | 1.30335735590382 |
| 13 | 2.89319852614925 | ... | -2.01145478051058 | 1.29788708064957 |
| 14 | 2.8997991086539 | ... | -2.01285501364142 | 1.3236741274999 |
| 15 | 2.90158446050215 | ... | -2.01164854386623 | 1.78844323353705 |
| 16 | 2.90322576197612 | ... | -2.01357019097282 | 1.52264732528438 |
| 17 | 2.90406473654365 | ... | -2.01117875087499 | 1.30341004386556 |
| 18 | 2.92177442536308 | ... | -2.01270005947885 | 1.30901536423074 |
| 19 | 2.93939223121491 | ... | -2.01072443794785 | 1.71053335113519 |
| 20 | 2.94061511365568 | ... | -2.01367148573802 | 1.31863389675329 |
Let’s perform a few aggregations with describe() to get a high-level overview of the dataset.
battery5.describe()
| ... | approx_75% | max | |
| "Voltage_measured" | ... | 4.20589540329895 | 4.98472045064886 |
| "Current_measured" | ... | 1.22946886392836 | 1.5215190253818 |
| "Temperature_measured" | ... | 27.3344916075796 | 41.2725661150757 |
| "Current_charge" | ... | 1.498 | 1.9984 |
| "Voltage_charge" | ... | 4.65 | 5.002 |
| "Time" | ... | 7494.96635294118 | 10807.219 |
| "ambient_temp" | ... | 24.0 | 24.0 |
| "Capacity" | ... | 1.74175033925828 | 1.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" |
| dtype | timestamp |
| count | 35288 |
| min | 2008-04-02 13:08:17.000920 |
| max | 2008-05-28 11:09:42.000045 |
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.head(100)
📅 start_time100% | ... | 123 cycle_duration100% | 123 cycle_id100% | |
| 1 | 2008-04-02 13:08:17.000920 | ... | 7561.75 | 1 |
| 2 | 2008-04-02 15:25:41.000593 | ... | 3072.859 | 2 |
| 3 | 2008-04-02 16:37:51.000984 | ... | 10516.0 | 3 |
| 4 | 2008-04-02 19:43:48.000405 | ... | 3509.61 | 4 |
| 5 | 2008-04-02 20:55:40.000811 | ... | 10401.937 | 5 |
| 6 | 2008-04-03 00:01:06.000687 | ... | 3651.641 | 6 |
| 7 | 2008-04-03 01:12:38.000670 | ... | 10397.89 | 7 |
| 8 | 2008-04-03 04:16:37.000375 | ... | 3309.719 | 8 |
| 9 | 2008-04-03 05:27:49.000125 | ... | 10166.718 | 9 |
| 10 | 2008-04-03 08:33:25.000702 | ... | 3507.391 | 10 |
| 11 | 2008-04-03 09:44:35.000078 | ... | 10352.141 | 11 |
| 12 | 2008-04-03 12:55:10.000686 | ... | 3530.0 | 12 |
| 13 | 2008-04-03 14:06:43.000234 | ... | 10683.61 | 13 |
| 14 | 2008-04-03 17:17:16.000015 | ... | 2917.359 | 14 |
| 15 | 2008-04-03 18:28:47.000125 | ... | 9667.515 | 15 |
| 16 | 2008-04-03 21:28:14.000718 | ... | 3531.75 | 16 |
| 17 | 2008-04-03 22:38:27.000452 | ... | 9901.391 | 17 |
| 18 | 2008-04-04 01:38:15.000217 | ... | 3429.047 | 18 |
| 19 | 2008-04-04 02:48:06.000155 | ... | 9370.672 | 19 |
| 20 | 2008-04-04 05:48:08.000609 | ... | 3231.25 | 20 |
| 21 | 2008-04-04 06:58:00.000296 | ... | 9491.344 | 21 |
| 22 | 2008-04-04 09:57:19.000765 | ... | 3231.25 | 22 |
| 23 | 2008-04-04 11:06:50.000375 | ... | 9969.688 | 23 |
| 24 | 2008-04-04 15:05:59.000905 | ... | 9525.25 | 24 |
| 25 | 2008-04-04 17:56:27.000609 | ... | 3491.016 | 25 |
| 26 | 2008-04-04 19:05:19.000234 | ... | 9738.64 | 26 |
| 27 | 2008-04-04 22:01:54.000670 | ... | 3310.344 | 27 |
| 28 | 2008-04-04 23:10:25.000420 | ... | 10629.89 | 28 |
| 29 | 2008-04-05 02:20:26.000702 | ... | 2744.891 | 29 |
| 30 | 2008-04-05 03:28:57.000234 | ... | 9606.453 | 30 |
| 31 | 2008-04-05 06:25:01.000890 | ... | 3114.0 | 31 |
| 32 | 2008-04-05 07:33:12.000734 | ... | 9978.516 | 32 |
| 33 | 2008-04-05 10:30:32.000311 | ... | 2002.875 | 33 |
| 34 | 2008-04-05 11:38:22.000140 | ... | 9293.968 | 34 |
| 35 | 2008-04-05 14:34:41.000468 | ... | 3171.938 | 35 |
| 36 | 2008-04-05 15:42:11.000531 | ... | 9649.344 | 36 |
| 37 | 2008-04-05 18:39:25.000217 | ... | 3192.39 | 37 |
| 38 | 2008-04-05 19:46:36.000125 | ... | 10138.625 | 38 |
| 39 | 2008-04-05 22:46:35.000484 | ... | 3035.625 | 39 |
| 40 | 2008-04-18 17:34:22.000890 | ... | 10049.844 | 40 |
| 41 | 2008-04-18 21:10:19.000795 | ... | 3055.828 | 41 |
| 42 | 2008-04-18 22:53:58.000343 | ... | 10181.281 | 42 |
| 43 | 2008-04-19 02:29:09 | ... | 3430.813 | 43 |
| 44 | 2008-04-19 04:12:06.000343 | ... | 10159.375 | 44 |
| 45 | 2008-04-19 07:47:15.000702 | ... | 3351.016 | 45 |
| 46 | 2008-04-19 09:29:52.000703 | ... | 9911.547 | 46 |
| 47 | 2008-04-19 13:01:42.000561 | ... | 3154.875 | 47 |
| 48 | 2008-04-19 14:43:41.000265 | ... | 10250.282 | 48 |
| 49 | 2008-04-19 18:27:29.000827 | ... | 2687.719 | 49 |
| 50 | 2008-04-19 20:09:47.000750 | ... | 9695.265 | 50 |
| 51 | 2008-04-19 23:41:55.000686 | ... | 3233.07799999999 | 51 |
| 52 | 2008-04-20 01:22:53.000953 | ... | 10018.812 | 52 |
| 53 | 2008-04-20 04:54:41.000734 | ... | 3390.922 | 53 |
| 54 | 2008-04-20 06:35:39.000765 | ... | 10265.531 | 54 |
| 55 | 2008-04-20 10:16:28.000859 | ... | 3392.25 | 55 |
| 56 | 2008-04-20 11:57:08.000765 | ... | 10192.688 | 56 |
| 57 | 2008-04-20 15:37:05.000280 | ... | 3410.688 | 57 |
| 58 | 2008-04-20 17:17:44.000030 | ... | 9938.359 | 58 |
| 59 | 2008-04-20 20:50:38.000920 | ... | 2938.156 | 59 |
| 60 | 2008-04-20 22:31:18.000045 | ... | 10660.578 | 60 |
| 61 | 2008-04-21 02:15:02.000921 | ... | 3076.766 | 61 |
| 62 | 2008-04-21 17:51:26.000312 | ... | 10361.016 | 62 |
| 63 | 2008-04-22 14:15:41.000186 | ... | 1671.75 | 63 |
| 64 | 2008-04-22 15:33:49.000875 | ... | 3422.125 | 64 |
| 65 | 2008-04-22 17:04:53.000218 | ... | 10039.766 | 65 |
| 66 | 2008-04-22 20:26:18.000920 | ... | 3214.187 | 66 |
| 67 | 2008-04-22 21:56:21.000405 | ... | 10129.656 | 67 |
| 68 | 2008-04-23 01:18:11.000795 | ... | 3110.969 | 68 |
| 69 | 2008-04-23 02:47:55.000453 | ... | 10618.844 | 69 |
| 70 | 2008-04-23 06:18:19.000920 | ... | 3337.219 | 70 |
| 71 | 2008-04-23 07:47:54.000718 | ... | 9917.282 | 71 |
| 72 | 2008-04-23 11:08:00.000312 | ... | 3329.14 | 72 |
| 73 | 2008-04-23 12:37:17.000515 | ... | 10456.562 | 73 |
| 74 | 2008-04-23 16:06:25.000170 | ... | 3338.922 | 74 |
| 75 | 2008-04-23 17:35:42.000828 | ... | 10344.032 | 75 |
| 76 | 2008-04-23 21:02:58.000295 | ... | 3185.875 | 76 |
| 77 | 2008-04-23 22:32:06.000671 | ... | 9949.17200000001 | 77 |
| 78 | 2008-04-24 01:51:33.000890 | ... | 3329.735 | 78 |
| 79 | 2008-04-24 03:20:23.000359 | ... | 10539.406 | 79 |
| 80 | 2008-04-24 06:50:00.000031 | ... | 2998.875 | 80 |
| 81 | 2008-04-24 08:18:41.000703 | ... | 10629.657 | 81 |
| 82 | 2008-04-24 11:49:24.000280 | ... | 3224.375 | 82 |
| 83 | 2008-04-24 13:17:56.000250 | ... | 10482.89 | 83 |
| 84 | 2008-04-24 16:45:56.000015 | ... | 3312.032 | 84 |
| 85 | 2008-04-24 18:14:28.000937 | ... | 10233.109 | 85 |
| 86 | 2008-04-24 21:38:11.000077 | ... | 3263.375 | 86 |
| 87 | 2008-04-24 23:06:34.000155 | ... | 10111.484 | 87 |
| 88 | 2008-04-25 12:03:44.000093 | ... | 3148.859 | 88 |
| 89 | 2008-04-25 17:02:35.000703 | ... | 10804.922 | 89 |
| 90 | 2008-04-25 20:03:55.000920 | ... | 3273.343 | 90 |
| 91 | 2008-04-25 21:32:20.000468 | ... | 10758.985 | 91 |
| 92 | 2008-04-26 01:04:35.000718 | ... | 2998.234 | 92 |
| 93 | 2008-04-26 02:32:41.000093 | ... | 10257.328 | 93 |
| 94 | 2008-04-26 05:57:35.000140 | ... | 3227.031 | 94 |
| 95 | 2008-04-26 07:25:23.000030 | ... | 10751.062 | 95 |
| 96 | 2008-04-26 10:58:25.000843 | ... | 3168.437 | 96 |
| 97 | 2008-04-28 17:20:47.000859 | ... | 9940.907 | 97 |
| 98 | 2008-04-29 12:15:56.000953 | ... | 3328.937 | 98 |
| 99 | 2008-04-29 13:44:46.000545 | ... | 10060.813 | 99 |
| 100 | 2008-04-29 17:05:27.000780 | ... | 3045.407 | 100 |
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.head(100)
📅 start_time100% | ... | 123 cycle_duration100% | 123 cycle_id100% | |
| 1 | 2008-04-02 13:08:17.000920 | ... | 7561.75 | 1 |
| 2 | 2008-04-02 15:25:41.000593 | ... | 3072.859 | 2 |
| 3 | 2008-04-02 16:37:51.000984 | ... | 10516.0 | 3 |
| 4 | 2008-04-02 19:43:48.000405 | ... | 3509.61 | 4 |
| 5 | 2008-04-02 20:55:40.000811 | ... | 10401.937 | 5 |
| 6 | 2008-04-03 00:01:06.000687 | ... | 3651.641 | 6 |
| 7 | 2008-04-03 01:12:38.000670 | ... | 10397.89 | 7 |
| 8 | 2008-04-03 04:16:37.000375 | ... | 3309.719 | 8 |
| 9 | 2008-04-03 05:27:49.000125 | ... | 10166.718 | 9 |
| 10 | 2008-04-03 08:33:25.000702 | ... | 3507.391 | 10 |
| 11 | 2008-04-03 09:44:35.000078 | ... | 10352.141 | 11 |
| 12 | 2008-04-03 12:55:10.000686 | ... | 3530.0 | 12 |
| 13 | 2008-04-03 14:06:43.000234 | ... | 10683.61 | 13 |
| 14 | 2008-04-03 17:17:16.000015 | ... | 2917.359 | 14 |
| 15 | 2008-04-03 18:28:47.000125 | ... | 9667.515 | 15 |
| 16 | 2008-04-03 21:28:14.000718 | ... | 3531.75 | 16 |
| 17 | 2008-04-03 22:38:27.000452 | ... | 9901.391 | 17 |
| 18 | 2008-04-04 01:38:15.000217 | ... | 3429.047 | 18 |
| 19 | 2008-04-04 02:48:06.000155 | ... | 9370.672 | 19 |
| 20 | 2008-04-04 05:48:08.000609 | ... | 3231.25 | 20 |
| 21 | 2008-04-04 06:58:00.000296 | ... | 9491.344 | 21 |
| 22 | 2008-04-04 09:57:19.000765 | ... | 3231.25 | 22 |
| 23 | 2008-04-04 11:06:50.000375 | ... | 9969.688 | 23 |
| 24 | 2008-04-04 15:05:59.000905 | ... | 9525.25 | 24 |
| 25 | 2008-04-04 17:56:27.000609 | ... | 3491.016 | 25 |
| 26 | 2008-04-04 19:05:19.000234 | ... | 9738.64 | 26 |
| 27 | 2008-04-04 22:01:54.000670 | ... | 3310.344 | 27 |
| 28 | 2008-04-04 23:10:25.000420 | ... | 10629.89 | 28 |
| 29 | 2008-04-05 02:20:26.000702 | ... | 2744.891 | 29 |
| 30 | 2008-04-05 03:28:57.000234 | ... | 9606.453 | 30 |
| 31 | 2008-04-05 06:25:01.000890 | ... | 3114.0 | 31 |
| 32 | 2008-04-05 07:33:12.000734 | ... | 9978.516 | 32 |
| 33 | 2008-04-05 10:30:32.000311 | ... | 2002.875 | 33 |
| 34 | 2008-04-05 11:38:22.000140 | ... | 9293.968 | 34 |
| 35 | 2008-04-05 14:34:41.000468 | ... | 3171.938 | 35 |
| 36 | 2008-04-05 15:42:11.000531 | ... | 9649.344 | 36 |
| 37 | 2008-04-05 18:39:25.000217 | ... | 3192.39 | 37 |
| 38 | 2008-04-05 19:46:36.000125 | ... | 10138.625 | 38 |
| 39 | 2008-04-05 22:46:35.000484 | ... | 3035.625 | 39 |
| 40 | 2008-04-18 17:34:22.000890 | ... | 10049.844 | 40 |
| 41 | 2008-04-18 21:10:19.000795 | ... | 3055.828 | 41 |
| 42 | 2008-04-18 22:53:58.000343 | ... | 10181.281 | 42 |
| 43 | 2008-04-19 02:29:09 | ... | 3430.813 | 43 |
| 44 | 2008-04-19 04:12:06.000343 | ... | 10159.375 | 44 |
| 45 | 2008-04-19 07:47:15.000702 | ... | 3351.016 | 45 |
| 46 | 2008-04-19 09:29:52.000703 | ... | 9911.547 | 46 |
| 47 | 2008-04-19 13:01:42.000561 | ... | 3154.875 | 47 |
| 48 | 2008-04-19 14:43:41.000265 | ... | 10250.282 | 48 |
| 49 | 2008-04-19 18:27:29.000827 | ... | 2687.719 | 49 |
| 50 | 2008-04-19 20:09:47.000750 | ... | 9695.265 | 50 |
| 51 | 2008-04-19 23:41:55.000686 | ... | 3233.07799999999 | 51 |
| 52 | 2008-04-20 01:22:53.000953 | ... | 10018.812 | 52 |
| 53 | 2008-04-20 04:54:41.000734 | ... | 3390.922 | 53 |
| 54 | 2008-04-20 06:35:39.000765 | ... | 10265.531 | 54 |
| 55 | 2008-04-20 10:16:28.000859 | ... | 3392.25 | 55 |
| 56 | 2008-04-20 11:57:08.000765 | ... | 10192.688 | 56 |
| 57 | 2008-04-20 15:37:05.000280 | ... | 3410.688 | 57 |
| 58 | 2008-04-20 17:17:44.000030 | ... | 9938.359 | 58 |
| 59 | 2008-04-20 20:50:38.000920 | ... | 2938.156 | 59 |
| 60 | 2008-04-20 22:31:18.000045 | ... | 10660.578 | 60 |
| 61 | 2008-04-21 02:15:02.000921 | ... | 3076.766 | 61 |
| 62 | 2008-04-21 17:51:26.000312 | ... | 10361.016 | 62 |
| 63 | 2008-04-22 14:15:41.000186 | ... | 1671.75 | 63 |
| 64 | 2008-04-22 15:33:49.000875 | ... | 3422.125 | 64 |
| 65 | 2008-04-22 17:04:53.000218 | ... | 10039.766 | 65 |
| 66 | 2008-04-22 20:26:18.000920 | ... | 3214.187 | 66 |
| 67 | 2008-04-22 21:56:21.000405 | ... | 10129.656 | 67 |
| 68 | 2008-04-23 01:18:11.000795 | ... | 3110.969 | 68 |
| 69 | 2008-04-23 02:47:55.000453 | ... | 10618.844 | 69 |
| 70 | 2008-04-23 06:18:19.000920 | ... | 3337.219 | 70 |
| 71 | 2008-04-23 07:47:54.000718 | ... | 9917.282 | 71 |
| 72 | 2008-04-23 11:08:00.000312 | ... | 3329.14 | 72 |
| 73 | 2008-04-23 12:37:17.000515 | ... | 10456.562 | 73 |
| 74 | 2008-04-23 16:06:25.000170 | ... | 3338.922 | 74 |
| 75 | 2008-04-23 17:35:42.000828 | ... | 10344.032 | 75 |
| 76 | 2008-04-23 21:02:58.000295 | ... | 3185.875 | 76 |
| 77 | 2008-04-23 22:32:06.000671 | ... | 9949.17200000001 | 77 |
| 78 | 2008-04-24 01:51:33.000890 | ... | 3329.735 | 78 |
| 79 | 2008-04-24 03:20:23.000359 | ... | 10539.406 | 79 |
| 80 | 2008-04-24 06:50:00.000031 | ... | 2998.875 | 80 |
| 81 | 2008-04-24 08:18:41.000703 | ... | 10629.657 | 81 |
| 82 | 2008-04-24 11:49:24.000280 | ... | 3224.375 | 82 |
| 83 | 2008-04-24 13:17:56.000250 | ... | 10482.89 | 83 |
| 84 | 2008-04-24 16:45:56.000015 | ... | 3312.032 | 84 |
| 85 | 2008-04-24 18:14:28.000937 | ... | 10233.109 | 85 |
| 86 | 2008-04-24 21:38:11.000077 | ... | 3263.375 | 86 |
| 87 | 2008-04-24 23:06:34.000155 | ... | 10111.484 | 87 |
| 88 | 2008-04-25 12:03:44.000093 | ... | 3148.859 | 88 |
| 89 | 2008-04-25 17:02:35.000703 | ... | 10804.922 | 89 |
| 90 | 2008-04-25 20:03:55.000920 | ... | 3273.343 | 90 |
| 91 | 2008-04-25 21:32:20.000468 | ... | 10758.985 | 91 |
| 92 | 2008-04-26 01:04:35.000718 | ... | 2998.234 | 92 |
| 93 | 2008-04-26 02:32:41.000093 | ... | 10257.328 | 93 |
| 94 | 2008-04-26 05:57:35.000140 | ... | 3227.031 | 94 |
| 95 | 2008-04-26 07:25:23.000030 | ... | 10751.062 | 95 |
| 96 | 2008-04-26 10:58:25.000843 | ... | 3168.437 | 96 |
| 97 | 2008-04-28 17:20:47.000859 | ... | 9940.907 | 97 |
| 98 | 2008-04-29 12:15:56.000953 | ... | 3328.937 | 98 |
| 99 | 2008-04-29 13:44:46.000545 | ... | 10060.813 | 99 |
| 100 | 2008-04-29 17:05:27.000780 | ... | 3045.407 | 100 |
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_time100% | ... | 123 Capacity100% | 123 smooth_capacity99% | |
| 1 | 2008-05-26 20:21:04.000921 | ... | 1.28745252213794 | [null] |
| 2 | 2008-05-26 15:30:43.000968 | ... | 1.28800339261912 | 1.28745252213794 |
| 3 | 2008-05-26 10:44:38.000468 | ... | 1.29346361384424 | 1.28772795737853 |
| 4 | 2008-05-26 01:13:02.000796 | ... | 1.29788708064957 | 1.2896398428671 |
| 5 | 2008-05-26 06:01:08.000170 | ... | 1.29807350569189 | 1.29170165231272 |
| 6 | 2008-05-25 10:51:37.000015 | ... | 1.30303291864059 | 1.29297602298855 |
| 7 | 2008-05-25 15:37:08.000890 | ... | 1.30335735590382 | 1.29465217226389 |
| 8 | 2008-05-25 20:23:04.000453 | ... | 1.30341004386556 | 1.29589576992674 |
| 9 | 2008-05-25 05:57:17.000890 | ... | 1.30779599469444 | 1.29683505416909 |
| 10 | 2008-05-27 15:52:41.000359 | ... | 1.30901536423074 | 1.29805293644969 |
| 11 | 2008-05-25 01:06:49.000828 | ... | 1.31320206344422 | 1.29914917922779 |
| 12 | 2008-05-24 20:21:13.000436 | ... | 1.31347513213243 | 1.30042671415656 |
| 13 | 2008-05-22 11:23:22.000187 | ... | 1.31816915884452 | 1.30151408232121 |
| 14 | 2008-05-22 20:59:50.000203 | ... | 1.3182930401027 | 1.30279524205378 |
| 15 | 2008-05-22 16:17:58.000890 | ... | 1.31846644366403 | 1.3039022276287 |
| 16 | 2008-05-24 15:31:10.000593 | ... | 1.31863389675329 | 1.30487317536439 |
| 17 | 2008-05-22 06:32:40.000920 | ... | 1.32317089922762 | 1.30573322045119 |
| 18 | 2008-05-24 10:42:50.000484 | ... | 1.3236741274999 | 1.30675896626157 |
| 19 | 2008-05-23 12:13:16.000359 | ... | 1.32387242224427 | 1.30769869744148 |
| 20 | 2008-05-27 20:45:42.000125 | ... | 1.32507932864294 | 1.30854994611531 |
Now we can plot the graphs. In VerticaPy 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
vp.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()
import matplotlib.pyplot as plt
from matplotlib.pyplot import axhline
# Switch the plotting library to Matplotlib
vp.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")
Out[10]: <matplotlib.lines.Line2D at 0x7fd387638b80>
ax.set_title("Capacity degradation curve of the battery, its smoothed version and its end-of-life threshold")
Out[11]: Text(0.5, 1.0, 'Capacity degradation curve of the battery, its smoothed version and its end-of-life threshold')
ax.legend()
Out[12]: <matplotlib.legend.Legend at 0x7fd3876e8670>
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
vp.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:
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 = ["*"],
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
final_df.to_db(name = "battery_data.finaldata_battery_5")
Machine Learning¶
AutoML tests several models and returns input scores for each. We can use this to find the best model for our dataset.
Note
We are only using the three algorithms, but you can change the estimator parameter to try all the native algorithms: ``estimator = ‘native’ ``
from verticapy.machine_learning.vertica.automl import AutoML
from verticapy.machine_learning.vertica import LinearRegression, RandomForestRegressor, Ridge
model = AutoML(
"battery_data.battery_autoML",
estimator = [
RandomForestRegressor(),
LinearRegression(),
Ridge(),
],
estimator_type = "regressor"
)
model.fit(
"battery_data.finaldata_battery_5",
X = [
"min_temp",
"max_temp",
"min_volt",
"time_to_reach_minvolt",
"time_to_reach_maxtemp",
],
y = "SOH",
)
We can visualize the performance and efficency differences of each model with a plot.
model.plot()
# take the best model and its parameters
best_model = model.best_model_
params = best_model.get_params()
print(best_model._model_type)
LinearRegression
We can now define the model using those hyperparameters and train it.
# define a regression model based on the selected parameters
model_rf = LinearRegression(name = "btr_lr1", **params)
model_rf.fit(
final_df,
X = [
"min_temp",
"max_temp",
"min_volt",
"time_to_reach_minvolt",
"time_to_reach_maxtemp",
],
y = "SOH",
)
model_rf.regression_report()
| value | |
| explained_variance | 0.96333884685611 |
| max_error | 0.0826358574982834 |
| median_absolute_error | 0.00811763379847186 |
| mean_absolute_error | 0.0125867357193153 |
| mean_squared_error | 0.000330328059204944 |
| root_mean_squared_error | 0.0181749294140292 |
| r2 | 0.96333884685611 |
| r2_adj | 0.962207329783768 |
| aic | -1333.62233469026 |
| bic | -1315.84749491322 |
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 the plot them along the true ones
result = model_rf.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.