Skip to content

Commit 0600a7f

Browse files
committed
add unittests, fix absolute links
1 parent 3488048 commit 0600a7f

File tree

7 files changed

+88
-29
lines changed

7 files changed

+88
-29
lines changed

analysis/BehavioralBrain.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import numpy
22
import pandas as pd
33
import matplotlib.pyplot as plt
4-
5-
import BrainActivationAnalysis
6-
from analysis import BehavioralSubjective
7-
8-
plt.rcParams.update({'font.size': 26})
94
import seaborn as sns
105
import scipy
116

12-
# TODO this file is supposed to analyze complexity metrics versus behavioral data
7+
from analysis import BehavioralSubjective, BrainActivationAnalysis
8+
from config import ROOT_DIR
9+
10+
plt.rcParams.update({'font.size': 26})
1311
graph_label = dict(color='#202020', alpha=0.9)
1412

13+
1514
def plot_correlation(df, computeResponseTime, ba, activation=True):
1615
if computeResponseTime:
1716
variable = 'ResponseTime'
@@ -53,11 +52,10 @@ def plot_correlation(df, computeResponseTime, ba, activation=True):
5352
else:
5453
pre = 'Deactivation_'
5554

56-
plt.savefig('output/' + pre + ba + '_' + variable + '.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
55+
plt.savefig(ROOT_DIR + '/analysis/output/' + pre + ba + '_' + variable + '.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
5756

5857

5958
def compute_behavioral_brain(df_ba_cond, behavioral_data, activation):
60-
6159
behavioral_ba = pd.merge(df_ba_cond, behavioral_data, how='left', left_on=['participant', 'Snippet'], right_on=['Participant', 'Snippet'])
6260

6361
# check whether there is response times < 5s and exclude them since they are accidental clicks
@@ -76,8 +74,8 @@ def compute_behavioral_brain(df_ba_cond, behavioral_data, activation):
7674

7775

7876
def main():
79-
df_ba_part_cond_act = pd.read_csv('../data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Activation.csv')
80-
df_ba_part_cond_deact = pd.read_csv('../data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Deactivation.csv')
77+
df_ba_part_cond_act = pd.read_csv(ROOT_DIR + '/data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Activation.csv')
78+
df_ba_part_cond_deact = pd.read_csv(ROOT_DIR + '/data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Deactivation.csv')
8179

8280
behavioral_data = BehavioralSubjective.load_behavioral_data()
8381

analysis/BehavioralSubjective.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
import seaborn as sns
88
import scipy
99

10+
from config import ROOT_DIR
11+
1012
plt.rcParams.update({'font.size': 26})
1113

1214
graph_label = dict(color='#202020', alpha=0.9)
1315

1416

1517
def load_snippet_metrics():
1618
print('\n##### \nread complexity metric data')
17-
snippet_metrics = pd.read_csv("../data/metrics/SnippetComplexityMetricsValues.csv", delimiter=",")
19+
snippet_metrics = pd.read_csv(ROOT_DIR + "/data/metrics/SnippetComplexityMetricsValues.csv", delimiter=",")
1820
snippet_metrics = snippet_metrics.astype('float', errors='ignore')
1921
print(snippet_metrics.head(5))
2022
return snippet_metrics
@@ -23,7 +25,7 @@ def load_snippet_metrics():
2325
def load_behavioral_data():
2426
print('\n##### \nread behavioral data')
2527

26-
behavioral_data = pd.read_csv("../data/behavioral/ParticipantBehavior.csv", delimiter=",")
28+
behavioral_data = pd.read_csv(ROOT_DIR + "/data/behavioral/ParticipantBehavior.csv", delimiter=",")
2729
behavioral_data = behavioral_data.astype('float', errors='ignore')
2830

2931
behavioral_data = behavioral_data[behavioral_data['Condition'] == "Comprehension"]
@@ -64,7 +66,7 @@ def plot_correlation_correctness(df, metric):
6466
sns.despine()
6567
plt.tight_layout()
6668

67-
plt.savefig('output/' + metric + '_Correctness.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
69+
plt.savefig(ROOT_DIR + '/analysis/output/' + metric + '_Correctness.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
6870

6971

7072
def plot_correlation_responsetime(df, metric):
@@ -97,7 +99,7 @@ def plot_correlation_responsetime(df, metric):
9799
sns.despine()
98100
plt.tight_layout()
99101

100-
plt.savefig('output/' + metric + '_ResponseTime.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
102+
plt.savefig(ROOT_DIR + '/analysis/output/' + metric + '_ResponseTime.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
101103

102104

103105
def plot_correlation_subjcomplexity_metrics(df, metric):
@@ -130,7 +132,7 @@ def plot_correlation_subjcomplexity_metrics(df, metric):
130132
sns.despine()
131133
plt.tight_layout()
132134

133-
plt.savefig('output/SubjComplexity_' + metric + '.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
135+
plt.savefig(ROOT_DIR + '/analysis/output/SubjComplexity_' + metric + '.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
134136

135137

136138
def plot_correlation_subjcomplexity_responsetime(df):
@@ -158,7 +160,7 @@ def plot_correlation_subjcomplexity_responsetime(df):
158160
sns.despine()
159161
plt.tight_layout()
160162

161-
plt.savefig('output/SubjComplexity_ResponseTime.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
163+
plt.savefig(ROOT_DIR + '/analysis/output/SubjComplexity_ResponseTime.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
162164

163165

164166
def plot_correlation_subjcomplexity_correctness(df):
@@ -186,7 +188,7 @@ def plot_correlation_subjcomplexity_correctness(df):
186188
sns.despine()
187189
plt.tight_layout()
188190

189-
plt.savefig('output/SubjComplexity_Correctness.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
191+
plt.savefig(ROOT_DIR + '/analysis/output/SubjComplexity_Correctness.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
190192

191193

192194
def select_color_for_metric(metric):
@@ -242,7 +244,7 @@ def main():
242244

243245
# correlate with behavioral data
244246
print('\n##### \n correlating subjective complexity with behavioral data')
245-
snippet_subjective_complexity = pd.read_csv('../data/subjective/SnippetSubjectiveComplexityRatings.csv')
247+
snippet_subjective_complexity = pd.read_csv(ROOT_DIR + '/data/subjective/SnippetSubjectiveComplexityRatings.csv')
246248

247249
print('mean:' + str(snippet_subjective_complexity['subj_complexity'].mean()))
248250
print('std:' + str(snippet_subjective_complexity['subj_complexity'].std(ddof=1)))

analysis/BrainActivationAnalysis.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
import statsmodels.api as sm
99
from statsmodels.formula.api import ols
1010

11+
from analysis import BehavioralSubjective
12+
from config import ROOT_DIR
13+
1114
plt.rcParams.update({'font.size': 24})
1215
plt.rcParams['font.family'] = 'Calibri'
1316

14-
import BehavioralSubjective
15-
1617
graph_label = dict(color='#101010', alpha=0.95)
1718

1819

@@ -47,9 +48,9 @@ def plot_ba_subj_rating(df, ba, activation=True, participant=None):
4748
plt.tight_layout()
4849

4950
if activation:
50-
prefix = 'output/act_subj_'
51+
prefix = ROOT_DIR + '/analysis/output/act_subj_'
5152
else:
52-
prefix = 'output/deact_subj_'
53+
prefix = ROOT_DIR + '/analysis/output/deact_subj_'
5354

5455
if participant:
5556
prefix += participant + '_'
@@ -112,9 +113,9 @@ def plot_ba_for_metric(df, metric, ba, activation=True):
112113
plt.tight_layout()
113114

114115
if activation:
115-
prefix = 'output/activation_'
116+
prefix = ROOT_DIR + '/analysis/output/activation_'
116117
else:
117-
prefix = 'output/deactivation_'
118+
prefix = ROOT_DIR + '/analysis/output/deactivation_'
118119

119120
plt.savefig(prefix + metric + '_' + ba + '.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
120121

@@ -154,11 +155,11 @@ def compute_statistics(df, activation=True):
154155

155156

156157
def compute_ba_subj_rating(activation=True):
157-
df_subj_complexity = pd.read_csv('../data/subjective/SnippetSubjectiveComplexityRatings.csv')
158+
df_subj_complexity = pd.read_csv(ROOT_DIR + '/data/subjective/SnippetSubjectiveComplexityRatings.csv')
158159
if activation:
159-
df_ba_part_cond = pd.read_csv('../data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Activation.csv')
160+
df_ba_part_cond = pd.read_csv(ROOT_DIR + '/data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Activation.csv')
160161
else:
161-
df_ba_part_cond = pd.read_csv('../data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Deactivation.csv')
162+
df_ba_part_cond = pd.read_csv(ROOT_DIR + '/data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Deactivation.csv')
162163

163164
df_ba_part_cond_subj = pd.merge(df_ba_part_cond, df_subj_complexity, left_on=['participant', 'Snippet'], right_on=['participant', 'snippet'])
164165
df_ba_part_cond_subj.sort_values(by='participant', inplace=True)
@@ -174,8 +175,8 @@ def compute_ba_subj_rating(activation=True):
174175

175176
def main():
176177
print('\n# Analyzing fMRI and complexity metrics data')
177-
df_ba_cond_act = pd.read_csv('../data/fMRI/fMRI_Analyzed_BA_Snippet_Activation.csv')
178-
df_ba_cond_deact = pd.read_csv('../data/fMRI/fMRI_Analyzed_BA_Snippet_Deactivation.csv')
178+
df_ba_cond_act = pd.read_csv(ROOT_DIR + '/data/fMRI/fMRI_Analyzed_BA_Snippet_Activation.csv')
179+
df_ba_cond_deact = pd.read_csv(ROOT_DIR + '/data/fMRI/fMRI_Analyzed_BA_Snippet_Deactivation.csv')
179180

180181
snippet_metrics = BehavioralSubjective.load_snippet_metrics()
181182

analysis/__init__.py

Whitespace-only changes.

config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
3+
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

tests/PipelineTests.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import unittest
2+
import os
3+
4+
from analysis import BehavioralSubjective, BehavioralBrain, BrainActivationAnalysis
5+
from config import ROOT_DIR
6+
7+
OUTPUT_DIR = ROOT_DIR + '/analysis/output'
8+
9+
10+
def empty_output_dir():
11+
files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".pdf")]
12+
for file in files:
13+
os.remove(os.path.join(OUTPUT_DIR, file))
14+
15+
16+
def get_number_of_files_in_output():
17+
path, dirs, files = next(os.walk(OUTPUT_DIR))
18+
file_count = len(files)
19+
return file_count
20+
21+
22+
class PipelineTest(unittest.TestCase):
23+
def test_behavioral_subjective(self):
24+
empty_output_dir()
25+
26+
file_count_initial = get_number_of_files_in_output()
27+
self.assertEqual(1, file_count_initial) # only .gitkeep file
28+
29+
BehavioralSubjective.main()
30+
file_count_end = get_number_of_files_in_output()
31+
self.assertEqual(15, file_count_end) # pipeline should create 14 files + .gitkeep
32+
33+
def test_behavioral_brain(self):
34+
empty_output_dir()
35+
36+
file_count_initial = get_number_of_files_in_output()
37+
self.assertEqual(1, file_count_initial) # only .gitkeep file
38+
39+
BehavioralBrain.main()
40+
file_count_end = get_number_of_files_in_output()
41+
self.assertEqual(13, file_count_end) # pipeline should create 12 files + .gitkeep
42+
43+
def test_brain_activation(self):
44+
empty_output_dir()
45+
46+
file_count_initial = get_number_of_files_in_output()
47+
self.assertEqual(1, file_count_initial) # only .gitkeep file
48+
49+
BrainActivationAnalysis.main()
50+
file_count_end = get_number_of_files_in_output()
51+
self.assertEqual(31, file_count_end) # pipeline should create 30 files + .gitkeep
52+
53+
54+
if __name__ == '__main__':
55+
unittest.main()

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)