from pathlib import Path
import pandas as pd
import json
import nibabel as nib
import numpy as np
from src.database import *
from src.nist import get_reference_NIST_values, get_NIST_ids
from src.tools import calc_error
import matplotlib.pyplot as plt
plt.style.use('custom_matplotlibrc')
plt.rcParams["figure.figsize"] = (10,10)
fig_id = 0
database_path = Path('databases/3T_NIST_T1maps_database.pkl')
output_folder = Path("plots/03_singledataset_scatter_NIST/")
estimate_type = 'mean' # median or mean
def plot_single_scatter(x, y, y_std,
title, x_label, y_label,
file_prefix, folder_path, fig_id,
y_type='linear'):
if y_type is 'linear':
plt.errorbar(x,y, y_std, fmt='o', solid_capstyle='projecting')
ax = plt.gca()
ax.axline((1, 1), slope=1, linestyle='dashed')
ax.set_ylim(ymin=0, ymax=2500)
ax.set_xlim(xmin=0, xmax=2500)
if y_type is 'log':
plt.loglog(x,y,'o')
ax = plt.gca()
ax.set_ylim(ymin=20, ymax=2500)
ax.set_xlim(xmin=20, xmax=2500)
if y_type is 'error_t1':
plt.errorbar(x,calc_error(x,y), fmt='o')
ax = plt.gca()
ax.axline((1, 0), slope=0, color='k')
ax.axline((1, -10), slope=0, linestyle='dashed', color='k')
ax.axline((1, 10), slope=0, linestyle='dashed', color='k')
ax.set_ylim(ymin=-100, ymax=100)
ax.set_xlim(xmin=0, xmax=2500)
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
fig = plt.gcf()
folder_path.mkdir(parents=True, exist_ok=True)
if fig_id<10:
filename = "0" + str(fig_id) + "_" + file_prefix
else:
filename = str(fig_id) + "_" + file_prefix
fig.savefig(folder_path / (str(filename) + '.svg'), facecolor='white')
fig.savefig(folder_path / (str(filename) + '.png'), facecolor='white')
fig_id = fig_id + 1
plt.show()
return fig_id
<>:5: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:11: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:16: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:5: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:11: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:16: SyntaxWarning: "is" with a literal. Did you mean "=="? /var/folders/l1/1xswjffd73l8yp7dd7pq9lyw0000gn/T/ipykernel_1546/368739589.py:5: SyntaxWarning: "is" with a literal. Did you mean "=="? if y_type is 'linear': /var/folders/l1/1xswjffd73l8yp7dd7pq9lyw0000gn/T/ipykernel_1546/368739589.py:11: SyntaxWarning: "is" with a literal. Did you mean "=="? if y_type is 'log': /var/folders/l1/1xswjffd73l8yp7dd7pq9lyw0000gn/T/ipykernel_1546/368739589.py:16: SyntaxWarning: "is" with a literal. Did you mean "=="? if y_type is 'error_t1':
df = pd.read_pickle(database_path)
dataset_estimate = np.array([])
dataset_std = np.array([])
index = 6.001
serial_number = df.loc[index]['phantom serial number']
for key in get_NIST_ids():
if estimate_type is 'mean':
dataset_estimate = np.append(dataset_estimate, np.mean(df.loc[index][key]))
elif estimate_type is 'median':
dataset_estimate = np.append(dataset_estimate, np.median(df.loc[index][key]))
else:
Exception('Unsupported dataset estimate type.')
dataset_std = np.append(dataset_std, np.std(df.loc[index][key]))
ref_values = get_reference_NIST_values(serial_number)
<>:9: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:11: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:9: SyntaxWarning: "is" with a literal. Did you mean "=="? <>:11: SyntaxWarning: "is" with a literal. Did you mean "=="? /var/folders/l1/1xswjffd73l8yp7dd7pq9lyw0000gn/T/ipykernel_1546/3451975214.py:9: SyntaxWarning: "is" with a literal. Did you mean "=="? if estimate_type is 'mean': /var/folders/l1/1xswjffd73l8yp7dd7pq9lyw0000gn/T/ipykernel_1546/3451975214.py:11: SyntaxWarning: "is" with a literal. Did you mean "=="? elif estimate_type is 'median':
fig_id = plot_single_scatter(x=ref_values,
y=dataset_estimate,
y_std=dataset_std,
title='Linear axes',
x_label='Reference T$_1$ (ms)',
y_label="T$_1$ estimate (ms)",
file_prefix=str(index),
folder_path=output_folder,
fig_id=fig_id,
y_type='linear')
fig_id = plot_single_scatter(x=ref_values,
y=dataset_estimate,
y_std=dataset_std,
title='Log-log axes',
x_label='Reference T$_1$ (ms)',
y_label="T$_1$ estimate (ms)",
file_prefix=str(index),
folder_path=output_folder,
fig_id=fig_id,
y_type='log')
fig_id = plot_single_scatter(x=ref_values,
y=dataset_estimate,
y_std=dataset_std,
title='Error relative to reference',
x_label='Reference T$_1$ (ms)',
y_label="Error (%)",
file_prefix=str(index),
folder_path=output_folder,
fig_id=fig_id,
y_type='error_t1')