Repository navigation
Expand file tree
/
Copy pathprocess.py
More file actions
427 lines (394 loc) · 17.4 KB
/
Copy pathprocess.py
File metadata and controls
427 lines (394 loc) · 17.4 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import numpy as np
import xarray as xr
import re
from pathlib import Path
import collections
def distance(val, ref):
return abs(ref - val)
vectDistance = np.vectorize(distance)
def cmap_xmap(function, cmap):
""" Applies function, on the indices of colormap cmap. Beware, function
should map the [0, 1] segment to itself, or you are in for surprises.
See also cmap_xmap.
"""
cdict = cmap._segmentdata
function_to_map = lambda x : (function(x[0]), x[1], x[2])
for key in ('red','green','blue'):
cdict[key] = map(function_to_map, cdict[key])
# cdict[key].sort()
# assert (cdict[key][0]<0 or cdict[key][-1]>1), "Resulting indices extend out of the [0, 1] segment."
return matplotlib.colors.LinearSegmentedColormap('colormap',cdict,1024)
def getClosest(sortedMatrix, column, val):
while len(sortedMatrix) > 3:
half = int(len(sortedMatrix) / 2)
sortedMatrix = sortedMatrix[-half - 1:] if sortedMatrix[half, column] < val else sortedMatrix[: half + 1]
if len(sortedMatrix) == 1:
result = sortedMatrix[0].copy()
result[column] = val
return result
else:
safecopy = sortedMatrix.copy()
safecopy[:, column] = vectDistance(safecopy[:, column], val)
minidx = np.argmin(safecopy[:, column])
safecopy = safecopy[minidx, :].A1
safecopy[column] = val
return safecopy
def convert(column, samples, matrix):
return np.matrix([getClosest(matrix, column, t) for t in samples])
def valueOrEmptySet(k, d):
return (d[k] if isinstance(d[k], set) else {d[k]}) if k in d else set()
def mergeDicts(d1, d2):
"""
Creates a new dictionary whose keys are the union of the keys of two
dictionaries, and whose values are the union of values.
Parameters
----------
d1: dict
dictionary whose values are sets
d2: dict
dictionary whose values are sets
Returns
-------
dict
A dict whose keys are the union of the keys of two dictionaries,
and whose values are the union of values
"""
res = {}
for k in d1.keys() | d2.keys():
res[k] = valueOrEmptySet(k, d1) | valueOrEmptySet(k, d2)
return res
def extractCoordinates(filename):
"""
Scans the header of an Alchemist file in search of the variables.
Parameters
----------
filename : str
path to the target file
mergewith : dict
a dictionary whose dimensions will be merged with the returned one
Returns
-------
dict
A dictionary whose keys are strings (coordinate name) and values are
lists (set of variable values)
"""
with open(filename, 'r') as file:
# regex = re.compile(' (?P<varName>[a-zA-Z._-]+) = (?P<varValue>[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?),?')
regex = r"(?P<varName>[a-zA-Z._-]+) = (?P<varValue>[^,]*),?"
dataBegin = r"\d"
is_float = r"[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?"
for line in file:
match = re.findall(regex, line.replace('Infinity', '1e30000'))
if match:
return {
var : float(value) if re.match(is_float, value)
else bool(re.match(r".*?true.*?", value.lower())) if re.match(r".*?(true|false).*?", value.lower())
else value
for var, value in match
}
elif re.match(dataBegin, line[0]):
return {}
def extractVariableNames(filename):
"""
Gets the variable names from the Alchemist data files header.
Parameters
----------
filename : str
path to the target file
Returns
-------
list of list
A matrix with the values of the csv file
"""
with open(filename, 'r') as file:
dataBegin = re.compile('\d')
lastHeaderLine = ''
for line in file:
if dataBegin.match(line[0]):
break
else:
lastHeaderLine = line
if lastHeaderLine:
regex = re.compile(' (?P<varName>\S+)')
return regex.findall(lastHeaderLine)
return []
def openCsv(path):
"""
Converts an Alchemist export file into a list of lists representing the matrix of values.
Parameters
----------
path : str
path to the target file
Returns
-------
list of list
A matrix with the values of the csv file
"""
regex = re.compile('\d')
with open(path, 'r') as file:
lines = filter(lambda x: regex.match(x[0]), file.readlines())
return [[float(x) for x in line.split()] for line in lines]
def beautifyValue(v):
"""
Converts an object to a better version for printing, in particular:
- if the object converts to float, then its float value is used
- if the object can be rounded to int, then the int value is preferred
Parameters
----------
v : object
the object to try to beautify
Returns
-------
object or float or int
the beautified value
"""
try:
v = float(v)
if v.is_integer():
return int(v)
return v
except:
return v
if __name__ == '__main__':
# CONFIGURE SCRIPT
# Where to find Alchemist data files
directory = 'data'
# Where to save charts
output_directory = 'charts'
# How to name the summary of the processed data
pickleOutput = 'data_summary'
# Experiment prefixes: one per experiment (root of the file name)
experiments = ['simulation']
floatPrecision = '{: 0.3f}'
# Number of time samples
timeSamples = 100
# time management
minTime = 0
maxTime = 50
timeColumnName = 'time'
logarithmicTime = False
# One or more variables are considered random and "flattened"
seedVars = ['seed', 'longseed']
# Label mapping
class Measure:
def __init__(self, description, unit = None):
self.__description = description
self.__unit = unit
def description(self):
return self.__description
def unit(self):
return '' if self.__unit is None else f'({self.__unit})'
def derivative(self, new_description = None, new_unit = None):
def cleanMathMode(s):
return s[1:-1] if s[0] == '$' and s[-1] == '$' else s
def deriveString(s):
return r'$d ' + cleanMathMode(s) + r'/{dt}$'
def deriveUnit(s):
return f'${cleanMathMode(s)}' + '/{s}$' if s else None
result = Measure(
new_description if new_description else deriveString(self.__description),
new_unit if new_unit else deriveUnit(self.__unit),
)
return result
def __str__(self):
return f'{self.description()} {self.unit()}'
centrality_label = 'H_a(x)'
def expected(x):
return r'\mathbf{E}[' + x + ']'
def stdev_of(x):
return r'\sigma{}[' + x + ']'
def mse(x):
return 'MSE[' + x + ']'
def cardinality(x):
return r'\|' + x + r'\|'
labels = {
'nodeCount': Measure(r'$n$', 'nodes'),
'harmonicCentrality[Mean]': Measure(f'${expected("H(x)")}$'),
'meanNeighbors': Measure(f'${expected(cardinality("N"))}$', 'nodes'),
'speed': Measure(r'$\|\vec{v}\|$', r'$m/s$'),
'msqer@harmonicCentrality[Max]': Measure(r'$\max{(' + mse(centrality_label) + ')}$'),
'msqer@harmonicCentrality[Min]': Measure(r'$\min{(' + mse(centrality_label) + ')}$'),
'msqer@harmonicCentrality[Mean]': Measure(f'${expected(mse(centrality_label))}$'),
'msqer@harmonicCentrality[StandardDeviation]': Measure(f'${stdev_of(mse(centrality_label))}$'),
'org:protelis:tutorial:distanceTo[max]': Measure(r'$m$', 'max distance'),
'org:protelis:tutorial:distanceTo[mean]': Measure(r'$m$', 'mean distance'),
'org:protelis:tutorial:distanceTo[min]': Measure(r'$m$', ',min distance'),
}
def derivativeOrMeasure(variable_name):
if variable_name.endswith('dt'):
return labels.get(variable_name[:-2], Measure(variable_name)).derivative()
return Measure(variable_name)
def label_for(variable_name):
return labels.get(variable_name, derivativeOrMeasure(variable_name)).description()
def unit_for(variable_name):
return str(labels.get(variable_name, derivativeOrMeasure(variable_name)))
# Setup libraries
np.set_printoptions(formatter={'float': floatPrecision.format})
# Read the last time the data was processed, reprocess only if new data exists, otherwise just load
import pickle
import os
if os.path.exists(directory):
newestFileTime = max([os.path.getmtime(directory + '/' + file) for file in os.listdir(directory)], default=0.0)
try:
lastTimeProcessed = pickle.load(open('timeprocessed', 'rb'))
except:
lastTimeProcessed = -1
shouldRecompute = not os.path.exists(".skip_data_process") and newestFileTime != lastTimeProcessed
if not shouldRecompute:
try:
means = pickle.load(open(pickleOutput + '_mean', 'rb'))
stdevs = pickle.load(open(pickleOutput + '_std', 'rb'))
except:
shouldRecompute = True
if shouldRecompute:
timefun = np.logspace if logarithmicTime else np.linspace
means = {}
stdevs = {}
for experiment in experiments:
# Collect all files for the experiment of interest
import fnmatch
allfiles = filter(lambda file: fnmatch.fnmatch(file, experiment + '_*.csv'), os.listdir(directory))
allfiles = [directory + '/' + name for name in allfiles]
allfiles.sort()
# From the file name, extract the independent variables
dimensions = {}
for file in allfiles:
dimensions = mergeDicts(dimensions, extractCoordinates(file))
dimensions = {k: sorted(v) for k, v in dimensions.items()}
# Add time to the independent variables
dimensions[timeColumnName] = range(0, timeSamples)
# Compute the matrix shape
shape = tuple(len(v) for k, v in dimensions.items())
# Prepare the Dataset
dataset = xr.Dataset()
for k, v in dimensions.items():
dataset.coords[k] = v
if len(allfiles) == 0:
print("WARNING: No data for experiment " + experiment)
means[experiment] = dataset
stdevs[experiment] = xr.Dataset()
else:
varNames = extractVariableNames(allfiles[0])
for v in varNames:
if v != timeColumnName:
novals = np.ndarray(shape)
novals.fill(float('nan'))
dataset[v] = (dimensions.keys(), novals)
# Compute maximum and minimum time, create the resample
timeColumn = varNames.index(timeColumnName)
allData = { file: np.matrix(openCsv(file)) for file in allfiles }
computeMin = minTime is None
computeMax = maxTime is None
if computeMax:
maxTime = float('-inf')
for data in allData.values():
maxTime = max(maxTime, data[-1, timeColumn])
if computeMin:
minTime = float('inf')
for data in allData.values():
minTime = min(minTime, data[0, timeColumn])
timeline = timefun(minTime, maxTime, timeSamples)
# Resample
for file in allData:
# print(file)
allData[file] = convert(timeColumn, timeline, allData[file])
# Populate the dataset
for file, data in allData.items():
dataset[timeColumnName] = timeline
for idx, v in enumerate(varNames):
if v != timeColumnName:
darray = dataset[v]
experimentVars = extractCoordinates(file)
darray.loc[experimentVars] = data[:, idx].A1
# Fold the dataset along the seed variables, producing the mean and stdev datasets
mergingVariables = [seed for seed in seedVars if seed in dataset.coords]
means[experiment] = dataset.mean(dim = mergingVariables, skipna=True)
stdevs[experiment] = dataset.std(dim = mergingVariables, skipna=True)
# Save the datasets
pickle.dump(means, open(pickleOutput + '_mean', 'wb'), protocol=-1)
pickle.dump(stdevs, open(pickleOutput + '_std', 'wb'), protocol=-1)
pickle.dump(newestFileTime, open('timeprocessed', 'wb'))
else:
means = { experiment: xr.Dataset() for experiment in experiments }
stdevs = { experiment: xr.Dataset() for experiment in experiments }
# QUICK CHARTING
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
matplotlib.rcParams.update({'axes.titlesize': 12})
matplotlib.rcParams.update({'axes.labelsize': 10})
def make_line_chart(
xdata,
ydata,
title=None,
ylabel=None,
xlabel=None,
colors=None,
linewidth=1,
error_alpha=0.2,
figure_size=(6, 4)
):
fig = plt.figure(figsize = figure_size)
ax = fig.add_subplot(1, 1, 1)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
# ax.set_ylim(0)
# ax.set_xlim(min(xdata), max(xdata))
index = 0
for (label, (data, error)) in ydata.items():
# print(f'plotting {data}\nagainst {xdata}')
lines = ax.plot(xdata, data, label=label, color=colors(index / (len(ydata) - 1)) if colors else None, linewidth=linewidth)
index += 1
if error is not None:
last_color = lines[-1].get_color()
ax.fill_between(
xdata,
data+error,
data-error,
facecolor=last_color,
alpha=error_alpha,
)
return (fig, ax)
def generate_all_charts(means, errors = None, basedir=''):
viable_coords = { coord for coord in means.coords if means[coord].size > 1 }
for comparison_variable in viable_coords - {timeColumnName}:
mergeable_variables = viable_coords - {timeColumnName, comparison_variable}
for current_coordinate in mergeable_variables:
merge_variables = mergeable_variables - { current_coordinate }
merge_data_view = means.mean(dim = merge_variables, skipna = True)
merge_error_view = errors.mean(dim = merge_variables, skipna = True)
for current_coordinate_value in merge_data_view[current_coordinate].values:
beautified_value = beautifyValue(current_coordinate_value)
for current_metric in merge_data_view.data_vars:
title = f'{label_for(current_metric)} for diverse {label_for(comparison_variable)} when {label_for(current_coordinate)}={beautified_value}'
for withErrors in [True, False]:
fig, ax = make_line_chart(
title = title,
xdata = merge_data_view[timeColumnName],
xlabel = unit_for(timeColumnName),
ylabel = unit_for(current_metric),
ydata = {
beautifyValue(label): (
merge_data_view.sel(selector)[current_metric],
merge_error_view.sel(selector)[current_metric] if withErrors else 0
)
for label in merge_data_view[comparison_variable].values
for selector in [{comparison_variable: label, current_coordinate: current_coordinate_value}]
},
)
ax.set_xlim(minTime, maxTime)
ax.legend()
fig.tight_layout()
by_time_output_directory = f'{output_directory}/{basedir}/{comparison_variable}'
Path(by_time_output_directory).mkdir(parents=True, exist_ok=True)
figname = f'{comparison_variable}_{current_metric}_{current_coordinate}_{beautified_value}{"_err" if withErrors else ""}'
for symbol in r".[]\/@:":
figname = figname.replace(symbol, '_')
fig.savefig(f'{by_time_output_directory}/{figname}.pdf')
plt.close(fig)
for experiment in experiments:
current_experiment_means = means[experiment]
current_experiment_errors = stdevs[experiment]
generate_all_charts(current_experiment_means, current_experiment_errors, basedir = f'{experiment}/all')
# Custom charting