def update_stats(path_dir, data, ref, tests, metrics, clip, eps=1e-2): """Update some entries of data.js; assumes it was already created.""" find_idx = lambda t, d: list(d['stats'][0]['labels']).index(t['name']) for test in tests: # Check if entry exists is_new = test['name'] not in data['stats'][0]['labels'] # Update dictionary if is_new: data['imageBoxes'][0]['elements'].append(hdr_to_ldr( path_dir, test)) data['stats'][0]['labels'].append(test['name']) else: t = find_idx(test, data) hdr_to_ldr(path_dir, test) # Compute desired metrics for m, metric in enumerate(metrics): # Recompute error err_img = compute_metric(ref, test['data'], metric.lower(), eps) err_mean = '{:.6f}'.format(np.mean(err_img)) if is_new: data['stats'][0]['series'][m]['data'].append(err_mean) else: data['stats'][0]['series'][m]['data'][t] = err_mean # Recompute false color heatmap and save to files fc = falsecolor(err_img, clip, eps) fc_fname = '{}-{}.png'.format(test['name'], metric.upper()) plt.imsave(os.path.join(path_dir, fc_fname), fc) if is_new: fc_entry = { 'title': test['name'], 'version': '-', 'image': fc_fname } data['imageBoxes'][m + 1]['elements'].append(fc_entry) # TODO: Update stats.json return data
def compute_stats(path_dir, ref, tests, metrics, clip, negpos, eps=1e-2): """Generate all false color LDR maps and dictionary for JS. Assumes tests = {'name': 'my_alg', 'data': ...} """ data = {} data['imageBoxes'] = [{'title': 'Images', 'elements': []}] data['stats'] = [{'title': 'Stats', 'labels': [], 'series': []}] ref_entry = hdr_to_ldr(path_dir, {'name': 'Reference', 'data': ref}) data['imageBoxes'][0]['elements'].append(ref_entry) # Generate images and compute stats # Couldn't find a way to do it all in only two loops stats = [] for t, test in enumerate(tests): # Update dictionary data['imageBoxes'][0]['elements'].append(hdr_to_ldr(path_dir, test)) data['stats'][0]['labels'].append(test['name']) # Compute all metrics stat_entry = {test['name']: {}} stats.append(stat_entry) for metric in metrics: # Compute error err_img = compute_metric(ref, test['data'], metric, eps) err_mean = '{:.6f}'.format(np.mean(err_img)) # Compute false color heatmap and save to files fc = falsecolor(err_img, clip, eps) fc_fname = '{}-{}.png'.format(test['name'], metric.upper()) plt.imsave(os.path.join(path_dir, fc_fname), fc) # Save stats, if necessary stats[t][test['name']][metric.upper()] = { 'val': err_mean, 'fc': fc_fname } # Write dictionary for metric in metrics: fc_entry = {'title': metric.upper(), 'elements': []} metric_entry = { 'label': metric.upper(), 'data': [], 'track': { 'x': [], 'y': [] } } for t, test in enumerate(tests): # Add false color filenames to dict fc_fname = stats[t][test['name']][metric.upper()]['fc'] entry = {'title': test['name'], 'version': '-', 'image': fc_fname} fc_entry['elements'].append(entry) # Add metric value to dict err_mean = stats[t][test['name']][metric.upper()]['val'] metric_entry['data'].append(err_mean) # Update dictionary with false color filenames and metrics data['imageBoxes'].append(fc_entry) data['stats'][0]['series'].append(metric_entry) # Write negative/positive image if requested if negpos: fc_entry = {'title': 'NP SMAPE', 'elements': []} for t, test in enumerate(tests): # Compute the N/P false color image fc = falsecolor_np(ref, test['data'], eps) fc_fname = '{}-NP.png'.format(test['name']) plt.imsave(os.path.join(path_dir, fc_fname), fc) # Save the fcname inside JSON entry = {'title': test['name'], 'version': '-', 'image': fc_fname} fc_entry['elements'].append(entry) # Update dictionary with false color filenames data['imageBoxes'].append(fc_entry) generate_thumbnail(path_dir, ref) return data