Skip to content

Commit

Permalink
Merge pull request #13 from canlab/unittests
Browse files Browse the repository at this point in the history
Final project changes
  • Loading branch information
claytonjschneider authored Dec 10, 2020
2 parents b5f2eaa + d47a52f commit dade862
Show file tree
Hide file tree
Showing 5,302 changed files with 7,238,842 additions and 51 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion Ex_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def main():
raise FileNotFoundError
sys.exit(3)

if group_num is not range(0, 9):
if group_num not in range(0, 9):
print("group_num must be an int, between 0 and 9.")
raise ValueError
sys.exit(3)
Expand Down
8 changes: 4 additions & 4 deletions Ex_contigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def main():
raise ValueError
sys.exit(3)

if spectra is not bool:
print("Spectra must be boolean, True or False.")
if spectra not in [True, False]:
print("spectra must be boolean, True or False.")
raise ValueError
sys.exit(3)

Expand Down Expand Up @@ -202,8 +202,8 @@ def main():
raise ValueError
sys.exit(3)

if erp is not bool:
print("Spectra must be boolean, True or False.")
if erp not in [True, False]:
print("erp must be boolean, True or False.")
raise ValueError
sys.exit(3)

Expand Down
3 changes: 2 additions & 1 deletion ML.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def Balance(self, parentPath, filter_band="nofilter"):
fnames = os.listdir(parentPath+"/"+folder+"/"+dataFolder)

subjects = list(set(
[fname[:config.participantNumLen] for fname in fnames]))
[fname[:config.participantNumLen] for fname in fnames if
fname[0] == "1"]))

random.shuffle(subjects)

Expand Down
7 changes: 4 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
}

refGroupFolders = [
"ref 24-30",
"ref 31-40",
"ref 41-50",
"CANlab",
# "ref 24-30",
# "ref 31-40",
# "ref 41-50",
# "ref 51-60",
# "ref 61-70",
# "ref 71-80",
Expand Down
101 changes: 59 additions & 42 deletions test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@

class TestPreprocessing(unittest.TestCase):

def test_x(self):
pass
def test_taskdata_generation(self):

def test_raw_generation(self):
task = [
task for task in config.tasks][
random.randint(0, len(config.tasks)-1)]
for sub in taskObj.subjects:
task_length = random.randint(1000, 10000)
task_length = random.randint(2000, 5000)
eeg_file = []
art_file = []
evt_file = []
Expand All @@ -44,90 +39,112 @@ def test_raw_generation(self):

np.savetxt(
taskObj.studyFolder\
+"/"\
+str(sub)\
+"/"+taskObj.task+"/"\
+sub\
+"_"\
+task\
+taskObj.task\
+"_nofilter.eeg",
eeg_file,
delimiter=" ",
fmt="%2.1f")

np.savetxt(
taskObj.studyFolder\
+"/"\
+str(sub)\
+"/"+taskObj.task+"/"\
+sub\
+"_"\
+task\
+taskObj.task\
+".art",
art_file,
delimiter=" ",
fmt="%2.1f")

np.savetxt(
taskObj.studyFolder\
+"/"\
+str(sub)\
+"/"+taskObj.task+"/"\
+sub\
+"_"\
+task\
+taskObj.task\
+".evt",
evt_file,
delimiter=" ",
fmt="%2.1f")

del eeg_file
del art_file
del evt_file

self.assertEqual(
len(os.listdir(taskObj.studyFolder)),
len(os.listdir(taskObj.studyFolder+"/"+taskObj.task)),
len(taskObj.subjects)*3)

def test_contig_generation(self):
length = random.randint(100, 20000)
art_degrees = np.linspace(0, 2, 1, endpoint=True)
erps = [True, False]
# def test_contig_generation(self):

taskObj.task_fnames = taskObj.get_task_fnames(taskObj.task)

length = random.randint(100, 2000)
# art_degrees = [0, 1, 2]
art_degrees = [1]
# erps = [True, False]
erps = [False]
channels = ""
for val in list(Signals.rand_bin_string(
19,
sample_rate=1)):
channels += str(val)

for art_degree in art_degrees:
for erp in erps:
taskObj.gen_contigs(
length,
network_channels=Signals.rand_bin_string(
19,
sample_rate=1),
network_channels=channels,
art_degree=art_degree,
erp=erp)
taskObj.write_contigs()
taskObj.gen_spectra(
length,
network_channels=channels,
art_degree=art_degree,
erp=erp)

assertEqual(
self.assertEqual(
len(os.listdir(taskObj.studyFolder+"/contigs")),
len(art_degrees))

assertEqual(
len(os.listdir(taskObj.studyFolder+"/erps")),
len(art_degrees))
# self.assertEqual(
# len(os.listdir(taskObj.studyFolder+"/erps")),
# len(art_degrees))

assertEqual(
self.assertEqual(
len(os.listdir(taskObj.studyFolder+"/spectra")),
len(art_degrees)*len(erps))


if __name__ == '__main__':
# unittest.TestCase.__init__(self,x)
taskObj = Prep.TaskData('.')
taskObj.studyFolder = 'testdata/raw'
taskObj.task = None
taskObj.task_fnames = None

if not os.path.isdir(taskObj.studyFolder):
if not os.path.isdir('testdata/P300'):
try:
os.mkdir('testdata')
os.mkdir('testdata/P300')
except FileExistsError:
try:
os.mkdir('testdata/raw')
os.mkdir('testdata/P300')
except FileExistsError:
print(
"Something went wrong creating test folders.",
"Try manually deleting ''/testdata/raw'")
"Try manually deleting ''/testdata/'")
sys.exit(1)
else:
shutil.rmtree('testdata/raw')
os.mkdir('testdata/raw')

taskObj = Prep.TaskData(
'testdata/'
# + str([
# task for task in config.tasks][
# random.randint(0, len(config.tasks)-1)]))
+ "P300")
taskObj.task_fnames = None
# else:
# shutil.rmtree('testdata/')
# os.mkdir('testdata/'+taskObj.task)

# make random list of fake subjects
# adhering to subj-number lengths defined in config.py
Expand All @@ -142,5 +159,5 @@ def test_contig_generation(self):
while num in taskObj.subjects:
num = random.randint(1000, 3999)

taskObj.subjects.append(num)
taskObj.subjects.append(str(num))
unittest.main()
Loading

0 comments on commit dade862

Please sign in to comment.