-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
337 lines (293 loc) · 13.9 KB
/
dataset.py
File metadata and controls
337 lines (293 loc) · 13.9 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import os
from datetime import datetime
import pandas as pd
class mm_dataset:
def __init__(self, filename):
self.filename = filename
if os.path.exists(filename):
self.df = pd.read_csv(filename)
else:
# create a new dataframe
self.create_new_df()
self.indexing_row_name = self.get_columns()[0] # The first column is the name of each row, this variable stores the name for the name of each row
if self.get_columns()[-1] == 'last_update':
self.add_timestamp = True
def create_new_df(self):
raise NotImplementedError("This method should be implemented in subclasses by calling self.create_new_df(column_names, rows).")
def create_new_df_from_labels(self, column_names, rows, add_timestamp=True):
"""
Create a new DataFrame with the specified column names.
Args:
column_names (list): List of column names for the DataFrame.
rows (list): List of dictionaries, where each dictionary represents a row of data.
"""
if add_timestamp:
column_names.append('last_update')
for row in rows:
row['last_update'] = datetime.now()
self.add_timestamp = True
else:
self.add_timestamp = False
self.df = pd.DataFrame(columns=column_names)
self.df = pd.concat([self.df, pd.DataFrame(rows)], ignore_index=True)
print("Creating or updating new csv at path:", self.filename)
self.df.to_csv(self.filename, index=False)
def get_value(self, row_name, column_name):
"""
Get the value from the DataFrame for a specific row and column.
Args:
row_name (str): The name of the row to look up.
column_name (str): The name of the column to look up.
Returns:
The value from the DataFrame at the specified row and column.
"""
return self.df.loc[self.df[self.indexing_row_name] == row_name, column_name].values[0]
def update_value(self, row_name, column_name, value, save_to_file=True):
"""
Update the value in the DataFrame for a specific row and column and saves to the csv file if requested.
Args:
row_name (str): The name of the row to update.
column_name (str): The name of the column to update.
value: The new value to set in the DataFrame.
"""
self.df.loc[self.df[self.indexing_row_name] == row_name, column_name] = value
if self.add_timestamp:
self.df.loc[self.df[self.indexing_row_name] == row_name, 'last_update'] = datetime.now()
if save_to_file:
print("Creating or updating new csv at path:", self.filename)
self.df.to_csv(self.filename, index=False)
def get_last_update(self, row_name):
if self.add_timestamp:
return self.get_value(row_name, 'last_update')
return None
def update_last_update(self, stor_name, save_to_file=True):
if not self.add_timestamp:
return
self.update_value(stor_name, 'last_update', datetime.now(), save_to_file=save_to_file)
def get_columns(self):
return self.df.columns.tolist()
def get_all(self, row_name):
return self.df[self.df[self.indexing_row_name] == row_name].values[0]
def update_all(self, *args, save_to_file=True):
"""
Update all values in the DataFrame for a specific row and always saves the file.
Args:
*args: A tuple containing the values to update in the order of the columns (excluding the timestamp).
The first value should be the row name, followed by values for each column.
save_to_file (bool): Whether to save the updated DataFrame to the CSV file.
"""
num_cols = len(self.get_columns())
if self.add_timestamp:
num_cols -= 1 # Exclude the timestamp column for check
if len(args) != num_cols:
raise ValueError(f"Number of arguments must match the columns in the DataFrame: {self.df.columns.tolist()}")
for i in range(1, num_cols):
column = self.get_columns()[i]
self.update_value(args[0], column, args[i], save_to_file=False)
self.update_last_update(args[0], save_to_file=save_to_file)
def append_dataset(self, *args, add_timestamp=True, save_to_file=True):
"""
Append a new row to the DataFrame with the provided values.
Args:
*args: A tuple containing the values to append in the order of the columns.
The first value should be the row_name, followed by values for each column, excluding the time stamp which will be added automatically.
"""
if len(args) != len(self.df.columns):
raise ValueError(f"Number of arguments must match the columns in the DataFrame: {self.df.columns.tolist()}")
new_row = dict()
for i, column in enumerate(self.df.columns):
if i == len(self.df.columns.tolist()) - 1 and add_timestamp: # last column is timestamp
new_row[column] = datetime.now()
else:
new_row[column] = args[i]
self.df = self.df.append(new_row, ignore_index=True)
if save_to_file:
print("Creating or updating new csv at path:", self.filename)
self.df.to_csv(self.filename, index=False)
# check whether the data is up-to-date
def is_up_to_date(self, row_name, max_time_diff = 7200):
last_update = self.get_last_update(row_name)
# Define the format of the date string
date_format = "%Y-%m-%d %H:%M:%S.%f"
# Convert the string to a datetime object
last_update_object= datetime.strptime(last_update, date_format)
time_diff = (datetime.now() - last_update_object).total_seconds()
return time_diff < max_time_diff
def create_copy(self, new_filename=None):
expts_path = ''
# print(f"expts_path: {expts_path}")
if new_filename is None:
name, ext = os.path.splitext(os.path.basename(self.filename))
new_filename = os.path.join(expts_path, f"{name}_copy{ext}")
else:
new_filename = os.path.join(expts_path, new_filename)
print(f"Creating a copy of the dataset at path: {new_filename}")
self.df.to_csv(new_filename, index=False)
return new_filename
def compare_with(self, other_dataset):
"""
Compare the current dataset with another dataset and identify differences.
This method iterates through the rows of the current dataset (`self.df`) and compares
each row with the corresponding row in the `other_dataset` based on the indexing_row_name column.
It identifies differences in column values between the two datasets and returns a list
of discrepancies.
Args:
other_dataset: An instance of the same dataset class containing a DataFrame (`df`)
to compare against.
Returns:
list: A list of dictionaries, where each dictionary represents a difference. Each
dictionary contains the following keys:
- whatever indexing_row_name is: The identifier of the row being compared.
- 'column': The column where the difference was found, or 'all' if the row
exists in `self` but is missing in `other_dataset`.
- 'self_value': The value in the current dataset.
- 'other_value': The value in the other dataset, or 'missing' if the row
does not exist in `other_dataset`.
Example:
differences = dataset1.compare_with(dataset2)
for diff in differences:
print(diff)
"""
differences = []
for _, row in self.df.iterrows():
row_name = row[self.indexing_row_name]
if row_name in other_dataset.df[self.indexing_row_name].values:
other_row = other_dataset.df[other_dataset.df[self.indexing_row_name] == row_name].iloc[0]
for column in self.df.columns:
if row[column] != other_row[column]:
differences.append({
self.indexing_row_name: row_name,
'column': column,
'self_value': row[column],
'other_value': other_row[column]
})
else:
differences.append({
self.indexing_row_name: row_name,
'column': 'all',
'self_value': 'exists',
'other_value': 'missing'
})
return differences
def save_to_file(self, filepath):
"""
Save the current dataset to a specified file.
Args:
filepath (str): The path to the file where the dataset should be saved.
"""
print("Creating or updating new csv at path:", filepath)
self.df.to_csv(filepath, index=False)
class storage_man_swap_dataset(mm_dataset):
def __init__(self, filename='man1_storage_swap_dataset.csv'):
super().__init__(filename=filename)
def create_new_df(self):
column_names = [
'stor_name',
'freq (MHz)',
'precision (MHz)',
'pi (mus)',
'h_pi (mus)',
'gain (DAC units)',
]
rows = []
for idx in range(1, 13, 1):
row = {
'stor_name': 'M1-S' + str(idx),
'freq (MHz)': -1,
'precision (MHz)': -1,
'pi (mus)': -1,
'h_pi (mus)': -1,
'gain (DAC units)': -1,
}
rows.append(row)
# also add for the manipulate
row = {
'stor_name': 'M1',
'freq (MHz)': -1,
'precision (MHz)': -1,
'pi (mus)': -1,
'h_pi (mus)': -1,
'gain (DAC units)': -1,
}
rows.append(row)
self.create_new_df_from_labels(column_names, rows, add_timestamp=True)
# fetch the data from the csv file
def get_freq(self, stor_name):
return self.get_value(stor_name, 'freq (MHz)')
def get_precision(self, stor_name):
return self.get_value(stor_name, 'precision (MHz)')
def get_pi(self, stor_name):
return self.get_value(stor_name, 'pi (mus)')
def get_h_pi(self, stor_name):
return self.get_value(stor_name, 'h_pi (mus)')
def get_gain(self, stor_name):
self.df['gain (DAC units)'] = self.df['gain (DAC units)'].astype(int)
return self.get_value(stor_name, 'gain (DAC units)')
# update the data in the csv file
def update_freq(self, stor_name, freq):
self.update_value(stor_name, 'freq (MHz)', freq)
def update_precision(self, stor_name, precision):
self.update_value(stor_name, 'precision (MHz)', precision)
def update_pi(self, stor_name, pi):
self.update_value(stor_name, 'pi (mus)', pi)
def update_h_pi(self, stor_name, h_pi):
self.update_value(stor_name, 'h_pi (mus)', h_pi)
def update_gain(self, stor_name, gain):
self.update_value(stor_name, 'gain (DAC units)', gain)
self.df['gain (DAC units)'] = self.df['gain (DAC units)'].astype(int)
class floquet_storage_swap_dataset(mm_dataset):
def __init__(self, filename='floquet_storage_swap_dataset.csv'):
super().__init__(filename=filename)
def create_new_df(self):
column_names = [
'stor_name',
'pi_frac', # this pulse implements a pi / pi_frac pulse
'freq (MHz)',
'gain (DAC units)',
'len (mus)',
'ramp_sigma (mus)',
]
for idx in range(1, 8, 1):
column_names.append('phase_from_M1-S' + str(idx) + ' (deg)') # phase of the pulse on M1-Sx
rows = []
for idx in range(1, 8, 1):
row = {
'stor_name': 'M1-S' + str(idx),
'pi_frac': -1,
'freq (MHz)': -1.0,
'gain (DAC units)': -1,
'len (mus)': -1.0,
'ramp_sigma (mus)': -1,
}
for i in range(1, 8, 1):
row['phase_from_M1-S' + str(i) + ' (deg)'] = 0.0
rows.append(row)
self.create_new_df_from_labels(column_names, rows, add_timestamp=True)
# fetch the data from the csv file
def get_freq(self, stor_name):
return self.get_value(stor_name, 'freq (MHz)')
def get_pi_frac(self, stor_name):
return self.get_value(stor_name, 'pi_frac')
def get_len(self, stor_name):
return self.get_value(stor_name, 'len (mus)')
def get_gain(self, stor_name):
self.df['gain (DAC units)'] = self.df['gain (DAC units)'].astype(int)
return self.get_value(stor_name, 'gain (DAC units)')
def get_ramp_sigma(self, stor_name):
return self.get_value(stor_name, 'ramp_sigma (mus)')
def get_phase_from(self, stor_name, from_stor_name):
return self.get_value(stor_name, f'phase_from_{from_stor_name} (deg)')
# update the data in the csv file
def update_freq(self, stor_name, freq):
self.update_value(stor_name, 'freq (MHz)', freq)
def update_pi_frac(self, stor_name, pi_frac):
self.update_value(stor_name, 'pi_frac', pi_frac)
def update_len(self, stor_name, length):
self.update_value(stor_name, 'len (mus)', length)
def update_gain(self, stor_name, gain):
self.update_value(stor_name, 'gain (DAC units)', gain)
self.df['gain (DAC units)'] = self.df['gain (DAC units)'].astype(int)
def update_ramp_sigma(self, stor_name, ramp_sigma):
self.update_value(stor_name, 'ramp_sigma (mus)', ramp_sigma)
def update_phase_from(self, stor_name, from_stor_name, phase):
self.update_value(stor_name, f'phase_from_{from_stor_name} (deg)', phase)