-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharging-curve.py
281 lines (262 loc) · 15.4 KB
/
charging-curve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import os
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.dates import (MINUTELY, RRuleLocator, rrulewrapper, DateFormatter)
from matplotlib.lines import Line2D
import pandas as pd # to read csv, more convenient than csv module
import datetime
# define colors
default_blue = '#1f77b4'
default_orange = '#ff7f0e'
default_purple = '#9467bd'
# Optional (but use consistent styles for all plots)
# plt.style.use('seaborn')
# According to the IEEE format
plt.rcParams.update({
"text.usetex": True,
"font.family": "serif",
"font.serif": ["Times"],
"font.size": 10,
"legend.fontsize": 8, # verify
"xtick.labelsize": 8, # verify
"ytick.labelsize": 8, # verify
"axes.labelsize": 10})
def set_size(width, fraction=1, subplots=(1, 1)):
"""Set figure dimensions to avoid scaling in LaTeX.
Parameters
----------
width: float or string
Document width in points, or string of predined document type
fraction: float, optional
Fraction of the width which you wish the figure to occupy
subplots: array-like, optional
The number of rows and columns of subplots.
Returns
-------
fig_dim: tuple
Dimensions of figure in inches
"""
if width == 'ieee-textwidth':
width_pt = 516
elif width == 'ieee-columnwidth':
width_pt = 252
else:
width_pt = width
# Width of figure (in pts)
fig_width_pt = width_pt * fraction
# Convert from pt to inches
inches_per_pt = 1 / 72.27
# Golden ratio to set aesthetic figure height
# https://disq.us/p/2940ij3
golden_ratio = (5**.5 - 1) / 2
# Figure width in inches
fig_width_in = fig_width_pt * inches_per_pt
# Figure height in inches
fig_height_in = fig_width_in * golden_ratio * (subplots[0] / subplots[1])
return (fig_width_in, fig_height_in-2) # the "-2" is added manually to reduce the height for MPT4.8-75(2-panels) case
# data
datadir = '../../data/charging/'
# panel = 'MPT4.8-75(4-panels)' # possible values: MPT4.8-75(4-panels), MPT4.8-75(4-panels), MPT6-75(4-panels)
panel = 'MPT4.8-75(2-panels)' # possible values: MPT4.8-75(4-panels), MPT4.8-75(4-panels), MPT6-75(4-panels)
# panel = 'MPT6-75(4-panels)' # possible values: MPT4.8-75(4-panels), MPT4.8-75(4-panels), MPT6-75(4-panels)
datafile = datadir + panel + '.csv'
data = pd.read_csv(datafile)
data = data.astype({'Lux': 'Int64', 'Iin (mA)': 'Float64', 'Vin': 'Float64', 'Iout (mA)': 'Float64', 'Vout': 'Float64', 'Notes': 'string', 'Date': 'string', 'Time': 'string', 'Efficiency': 'float'})
datetime_arr = np.asarray([datetime.datetime.strptime(d, '%Y/%m/%d-%H:%M %Z') for d in np.asarray('2021/' + data.iloc[:,0] + '-' + data.iloc[:,1] + ' PST')])
lux_arr = np.asarray(data.get('Lux'))
Vin_arr = np.asarray(data.get('Vin'))
Vout_arr = np.asarray(data.get('Vout'))
# Remove the open circuit readings (for plotting current, efficiency)
trimmed_data = data.loc[data.get('Notes') != 'Open circuit']
trimmed_datetime_arr = np.asarray([datetime.datetime.strptime(d, '%Y/%m/%d-%H:%M %Z') for d in np.asarray('2021/' + trimmed_data.iloc[:,0] + '-' + trimmed_data.iloc[:,1] + ' PST')])
trimmed_lux_arr = np.asarray(trimmed_data.get('Lux'))
Iin_arr = np.asarray(trimmed_data.get('Iin (mA)'))
Iout_arr = np.asarray(trimmed_data.get('Iout (mA)'))
efficiency_arr = np.asarray(trimmed_data.get('Efficiency'))
print('Plotting data for {}'.format(panel))
## Plot Vout
if panel == 'MPT6-75(4-panels)':
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=set_size('ieee-textwidth', subplots=(1,2)))
# limits
ax1.set_ylim(3.25, 4.3)
ax2.set_ylim(3.25, 4.3)
# plot
ax1.plot(datetime_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-14')], Vout_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-14')], color=default_blue, marker='o', linewidth=2)
ax2.plot(datetime_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-15')], Vout_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-15')], color=default_blue, marker='o', linewidth=2)
# show days on the top
ax1.set_title('Day 1')
ax2.set_title('Day 2')
# ticks and grid
ax1.xaxis.set_major_locator(RRuleLocator(rrulewrapper(MINUTELY, interval=5, dtstart=datetime_arr[0]+datetime.timedelta(minutes=5))))
ax2.xaxis.set_major_locator(RRuleLocator(rrulewrapper(MINUTELY, interval=5, dtstart=datetime_arr[np.asarray([d.date() for d in datetime_arr]) == datetime.datetime.strptime('2021-01-15', '%Y-%m-%d').date()][0])))
ax1.grid(b=True, which='major', axis='both')
ax2.grid(b=True, which='major', axis='both')
# tick formatting
ax1.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax2.xaxis.set_major_formatter(DateFormatter('%H:%M'))
# remove spines between the two plots
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
# axis labels
ax1.set_ylabel('Volts', color=default_blue)
ax1.tick_params(axis='y', labelcolor=default_blue)
ax2.yaxis.set_ticklabels([])
fig.subplots_adjust(wspace=0.02)
ax1.yaxis.set_ticks_position('left')
# Kinks in the time axis
d = .015 # how big to make the diagonal lines in axes coordinates
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((1-d, 1+d), (-d, +d), **kwargs) # top-left diagonal
ax1.plot((1-d, 1+d), (1-d, 1+d), **kwargs) # top-right diagonal
kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d, +d), (1-d, 1+d), **kwargs) # bottom-left diagonal
ax2.plot((-d, +d), (-d, +d), **kwargs) # bottom-right diagonal
else:
fig, ax = plt.subplots(1, 1, figsize=set_size('ieee-textwidth'))
ax.set_ylim(3.10, 4.3)
if panel == 'MPT4.8-75(4-panels)':
ax.plot(datetime_arr, Vout_arr, color=default_blue, marker='o', linewidth=2)
elif panel == 'MPT4.8-75(2-panels)':
ax.plot(datetime_arr[::2], Vout_arr[::2], color=default_blue, marker='o', linewidth=2)
ax.xaxis.set_major_locator(RRuleLocator(rrulewrapper(MINUTELY, interval=5, dtstart=datetime_arr[0]+datetime.timedelta(minutes=5))))
ax.grid(b=True, which='major', axis='both')
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.set_ylabel('Volts', color=default_blue)
ax.tick_params(axis='y', labelcolor=default_blue)
ax.yaxis.set_ticks_position('left')
## Plot sunlight intensity
if panel == 'MPT6-75(4-panels)':
ax1 = ax1.twinx()
ax2 = ax2.twinx()
# limits
ax1.set_ylim(min(lux_arr) - 15000, max(lux_arr) + 5000)
ax2.set_ylim(min(lux_arr) - 15000, max(lux_arr) + 5000)
# plot
ax1.plot(datetime_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-14')], lux_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-14')], color=default_orange, marker='o', linestyle='--', markersize=4, linewidth=1)
ax2.plot(datetime_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-15')], lux_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-15')], color=default_orange, marker='o', linestyle='--', markersize=4, linewidth=1)
# tick formatting
ax1.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax2.xaxis.set_major_formatter(DateFormatter('%H:%M'))
# remove spines between the two plots
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
# axis labels
ax1.yaxis.set_ticklabels([])
ax1.yaxis.set_ticks([])
ax2.yaxis.set_ticks_position('right')
ax2.yaxis.set_ticklabels([])
ax2.yaxis.set_ticks([])
# show intensity as a text annotation near the point
xoffset = datetime.timedelta(minutes=2.5)
yoffset = 3000
for datetimestamp, intensity in zip(datetime_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-14')], lux_arr[np.array([d.date() for d in datetime_arr]) == datetime.date.fromisoformat('2021-01-14')]):
ax1.text(datetimestamp + xoffset, intensity + yoffset, intensity/1000, horizontalalignment='center', verticalalignment='center', color=default_orange, fontsize='small')
for datetimestamp, intensity in zip(trimmed_datetime_arr[np.array([d.date() for d in trimmed_datetime_arr]) == datetime.date.fromisoformat('2021-01-15')], trimmed_lux_arr[np.array([d.date() for d in trimmed_datetime_arr]) == datetime.date.fromisoformat('2021-01-15')]):
ax2.text(datetimestamp + xoffset, intensity + yoffset, intensity/1000, horizontalalignment='center', verticalalignment='center', color=default_orange, fontsize='small')
else:
ax = ax.twinx()
ax.set_ylim(min(lux_arr) - 5000, max(lux_arr) + 15000)
if panel == 'MPT4.8-75(4-panels)':
ax.plot(datetime_arr, lux_arr, color=default_orange, marker='o', linestyle='--', markersize=4, linewidth=1)
if panel == 'MPT4.8-75(2-panels)':
ax.plot(datetime_arr[2::2], lux_arr[2::2], color=default_orange, marker='o', linestyle='--', markersize=4, linewidth=1)
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.yaxis.set_ticklabels([])
ax.yaxis.set_ticks([])
ax.yaxis.set_ticks_position('right')
# show intensity as a text annotation near the point
if panel == 'MPT4.8-75(4-panels)':
xoffset = datetime.timedelta(minutes=0)
yoffset = 5000
for datetimestamp, intensity in zip(datetime_arr, lux_arr):
ax.text(datetimestamp + xoffset, intensity + yoffset, intensity/1000, horizontalalignment='center', verticalalignment='center', color=default_orange, fontsize='small')
elif panel == 'MPT4.8-75(2-panels)':
xoffset = datetime.timedelta(minutes=2.5)
yoffsets = [9000, 9000, 6000, 6000, 6000, 5000, 4000, 6000, 6000, 6000, 6000, 6000, 7000, 6000, 8000, 8000, 8000, 8000, 6000, -3000]
for datetimestamp, intensity in zip(datetime_arr[2::2], lux_arr[2::2]):
ax.text(datetimestamp + xoffset, intensity - yoffsets.pop(0), intensity/1000, horizontalalignment='center', verticalalignment='center', color=default_orange, fontsize='x-small', fontweight='heavy', zorder=10)
## Plot current output from the charger (show that it follows light intensity trend)
if panel == 'MPT6-75(4-panels)':
ax1 = ax1.twinx()
ax2 = ax2.twinx()
# limits
ax1.set_ylim(min(Iout_arr) - 30, max(Iout_arr) + 50)
ax2.set_ylim(min(Iout_arr) - 30, max(Iout_arr) + 50)
# plot
ax1.plot(trimmed_datetime_arr[np.array([d.date() for d in trimmed_datetime_arr]) == datetime.date.fromisoformat('2021-01-14')], Iout_arr[np.array([d.date() for d in trimmed_datetime_arr]) == datetime.date.fromisoformat('2021-01-14')], color=default_purple, marker='o', linestyle=':', linewidth=1, markersize=4, zorder=2)
ax2.plot(trimmed_datetime_arr[np.array([d.date() for d in trimmed_datetime_arr]) == datetime.date.fromisoformat('2021-01-15')], Iout_arr[np.array([d.date() for d in trimmed_datetime_arr]) == datetime.date.fromisoformat('2021-01-15')], color=default_purple, marker='o', linestyle=':', linewidth=1, markersize=4, zorder=2)
# tick formatting
ax1.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax2.xaxis.set_major_formatter(DateFormatter('%H:%M'))
# remove spines between the two plots
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
# axis labels
ax2.set_ylabel('mA', color=default_purple)
ax2.tick_params(axis='y', labelcolor=default_purple)
ax1.yaxis.set_ticklabels([])
ax1.yaxis.set_ticks([])
ax2.yaxis.set_ticks_position('right')
else:
ax = ax.twinx()
ax.set_ylim(min(Iout_arr) - 5, max(Iout_arr) + 5)
if panel == 'MPT4.8-75(4-panels)':
ax.plot(trimmed_datetime_arr, Iout_arr, color=default_purple, marker='o', linestyle=':', linewidth=1, markersize=4, zorder=2)
elif panel == 'MPT4.8-75(2-panels)':
ax.plot(trimmed_datetime_arr[1::2], Iout_arr[1::2], color=default_purple, marker='o', linestyle=':', linewidth=1, markersize=4, zorder=2)
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.set_ylabel('mA', color=default_purple)
ax.tick_params(axis='y', labelcolor=default_purple)
ax.yaxis.set_ticks_position('right')
# legend
if panel == 'MPT6-75(4-panels)':
ax1.legend([Line2D([0], [0], color=default_blue, marker='o', linewidth=2), Line2D([0], [0], color=default_orange, marker='o', linestyle='--', linewidth=1, markersize=4), Line2D([0], [0], color=default_purple, marker='o', linestyle=':', linewidth=1, markersize=4)], ['Battery voltage (V)', r'Light intensity ($\times10^3$ lux)', 'Charging current (mA)'], loc='lower left', bbox_to_anchor=(0.105, -0.01))
else:
ax.legend([Line2D([0], [0], color=default_blue, marker='o', linewidth=2), Line2D([0], [0], color=default_orange, marker='o', linestyle='--', linewidth=1, markersize=4), Line2D([0], [0], color=default_purple, marker='o', linestyle=':', linewidth=1, markersize=4)], ['Battery voltage (V)', r'Light intensity ($\times10^3$ lux)', 'Charging current (mA)'], loc='upper left')
print('Average light intensity is: {}'.format(np.average(lux_arr)))
print('Maximum light intensity is: {}'.format(max(lux_arr)))
print('Average efficiency is: {}%'.format(100*np.average(efficiency_arr)))
# save plot
plt.savefig('../../img/charging-curve.eps', bbox_inches='tight')
print('Saved ../../img/charging-curve.eps')
plt.savefig('../../img/charging-curve.pdf', bbox_inches='tight')
print('Saved ../../img/charging-curve.pdf')
plt.savefig('../../img/charging-curve.png')
print('Saved ../../img/charging-curve.png')
# Efficiency plot
# Read efficiency data from all files
fig2 = plt.figure(2, figsize=set_size('ieee-columnwidth'))
ax2 = fig2.add_subplot(1,1,1)
efficiency_arr = []
lux_arr = []
for datafile in os.listdir(datadir):
if os.path.isfile(datadir + datafile):
data = pd.read_csv(datadir + datafile)
data = data.astype({'Lux': 'Int64', 'Iin (mA)': 'Float64', 'Vin': 'Float64', 'Iout (mA)': 'Float64', 'Vout': 'Float64', 'Notes': 'string', 'Date': 'string', 'Time': 'string', 'Efficiency': 'float'})
# Remove the open circuit readings (for plotting current, efficiency)
trimmed_data = data.loc[data.get('Notes') != 'Open circuit']
lux_arr.extend(list(trimmed_data.get('Lux')))
efficiency_arr.extend(list(trimmed_data.get('Efficiency')))
efficiency_arr = np.asarray(efficiency_arr)
lux_arr = np.asarray(lux_arr)
ax2.plot(lux_arr, efficiency_arr, 'o', markersize=3)
ax2.yaxis.set_major_formatter(matplotlib.ticker.PercentFormatter(1.0))
ax2.set_xlabel('Light intensity (lux)')
ax2.set_ylabel('Efficiency')
ax2.grid()
ax2.set_ylim(0, 1)
#plt.subplots_adjust(bottom=0.18, left=0.18)
plt.savefig('../../img/efficiency.eps', bbox_inches='tight')
print('Saved ../../img/efficiency.eps')
plt.savefig('../../img/efficiency.pdf', bbox_inches='tight')
print('Saved ../../img/efficiency.pdf')
plt.savefig('../../img/efficiency.png')
print('Saved ../../img/efficiency.png')
# Average efficiency for indoor charge
for datafile in os.listdir(datadir + 'inside'):
data = pd.read_csv(datadir + 'inside/' + datafile)
data = data.astype({'Lux': 'Int64', 'Iin (mA)': 'Float64', 'Vin': 'Float64', 'Iout (mA)': 'Float64', 'Vout': 'Float64', 'Notes': 'string', 'Date': 'string', 'Time': 'string', 'Efficiency': 'float'})
# Remove the open circuit readings (for plotting current, efficiency)
trimmed_data = data.loc[data.get('Notes') != 'Open circuit']
print("{}: avg efficiency is {}%".format(datadir+'inside/'+datafile, np.average(np.asarray(trimmed_data.get('Efficiency')))*100))