<jupyter_start><jupyter_text>Plot tractometry results<jupyter_code>%matplotlib inline

import numpy as np 
import matplotlib.pyplot as plt
import seaborn as sns
from tractseg.libs.ExpUtils import ExpUtils

values = {}
#Load tractometry results subject 1
values["s0"] = np.loadtxt("Tractometry_subject1.csv", delimiter=";", skiprows=1).transpose()
#Load tractometry results subject 2
values["s1"] = np.loadtxt("Tractometry_subject2.csv", delimiter=";", skiprows=1).transpose()
#todo: load more subjects

subjects_controls = ["s0"]
subjects_patients = ["s1"]

#Define bundles you want to plot
selected_bundles = ["CST_right", "SCP_left", "UF_right", "CG_right", "CC_4", "AF_right"]

NR_POINTS = values[subjects_controls[0]].shape[1]
bundles = ExpUtils.get_bundle_names("All")[1:]
selected_bun_indices = [bundles.index(b) for b in selected_bundles]

a4_dims = (15, 10)
#adapt nr of subplots if you want to show more bundles
f, axes = plt.subplots(2, 3, figsize=a4_dims)

axes = axes.flatten()
sns.set(font_scale=1.2)
sns.set_style("whitegrid")

for i, b_idx in enumerate(selected_bun_indices):
    data = { "position": [],
             "fa": [],
             "group": [],
             "subject": []}
    for j, subject in enumerate(subjects_controls + subjects_patients):
        for position in range(NR_POINTS):
            data["position"].append(position)
            data["subject"].append(subject)
            data["fa"].append(values[subject][b_idx][position])
            if subject in subjects_controls:
                data["group"].append("Group A")
            else:
                data["group"].append("Group B")
    
    ax = sns.lineplot(x="position", y="fa", data=data, ax=axes[i], hue="group")
    
    ax.set(xlabel='position', ylabel='FA')
    ax.set_title(bundles[b_idx])
    if i > 0:
        ax.legend_.remove()   #only show legend on first subplot

plt.tight_layout()
        
#save as image
#plt.savefig("tractometry_plot.png", dpi=200)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14336016096579476, 'fraction_numerical': 0.014084507042253521, 'mean_word_length': 3.9232673267326734, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '257232', 'license': 'Apache-2.0', 'n_tokens_mistral': 699, 'n_tokens_neox': 671, 'n_words': 150, 'path': 'examples/plot_tractometry_results.ipynb', 'repo_name': 'soichih/TractSeg', 'size': '1988'}