Skip to content

Commit

Permalink
Merge pull request #256 from vinferrer/file_restruct
Browse files Browse the repository at this point in the history
Create folder code/conversion to store trigger and channel plots, yaml and heuristic files and the call.sh file
  • Loading branch information
eurunuela authored Jun 18, 2020
2 parents 83bbbcc + 3bd02ff commit 8d42c28
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 37 deletions.
27 changes: 21 additions & 6 deletions phys2bids/phys2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
"""

import os
import datetime
import logging
import os
import sys
from copy import deepcopy
from shutil import copy as cp

from numpy import savetxt

Expand Down Expand Up @@ -145,14 +147,17 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
# #!# This can probably be done while parsing?
outdir = utils.check_input_dir(outdir)
utils.path_exists_or_make_it(outdir)
utils.path_exists_or_make_it(os.path.join(outdir, 'code'))
conversion_path = os.path.join(outdir, 'code/conversion')
utils.path_exists_or_make_it(conversion_path)
# generate extra path
extra_dir = os.path.join(outdir, 'bids_ignore')
utils.path_exists_or_make_it(extra_dir)
# Create logfile name
basename = 'phys2bids_'
extension = 'tsv'
isotime = datetime.datetime.now().strftime('%Y-%m-%dT%H%M%S')
logname = os.path.join(extra_dir, (basename + isotime + '.' + extension))
logname = os.path.join(conversion_path, (basename + isotime + '.' + extension))

# Set logging format
log_formatter = logging.Formatter(
Expand All @@ -178,6 +183,13 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
LGR.info(f'Currently running phys2bids version {version_number}')
LGR.info(f'Input file is {filename}')

# Save call.sh
arg_str = ' '.join(sys.argv[1:])
call_str = f'phys2bids {arg_str}'
f = open(os.path.join(conversion_path, 'call.sh'), "a")
f.write(f'#!bin/bash \n{call_str}')
f.close()

# Check options to make them internally coherent pt. II
# #!# This can probably be done while parsing?
indir = utils.check_input_dir(indir)
Expand Down Expand Up @@ -208,7 +220,7 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
phys_in.print_info(filename)
# #!# Here the function viz.plot_channel should be called
viz.plot_all(phys_in.ch_name, phys_in.timeseries, phys_in.units,
phys_in.freq, infile, extra_dir)
phys_in.freq, infile, conversion_path)
# If only info were asked, end here.
if info:
return
Expand All @@ -220,7 +232,7 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
# #!# Get option of no trigger! (which is wrong practice or Respiract)
phys_in.check_trigger_amount(thr, num_timepoints_expected, tr)
LGR.info('Plot trigger')
plot_path = os.path.join(extra_dir,
plot_path = os.path.join(conversion_path,
os.path.splitext(os.path.basename(filename))[0])
if sub:
plot_path += f'_sub-{sub}'
Expand Down Expand Up @@ -286,6 +298,8 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
bids.dataset_description_file(outdir)
# Generate README file if it doesn't exist already.
bids.readme_file(outdir)
cp(heur_file, os.path.join(conversion_path,
os.path.splitext(os.path.basename(heur_file))[0] + '.py'))
elif heur_file and not sub:
LGR.warning('While "-heur" was specified, option "-sub" was not.\n'
'Skipping BIDS formatting.')
Expand Down Expand Up @@ -317,7 +331,8 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
print_summary(filename, num_timepoints_expected,
phys_in.num_timepoints_found, uniq_freq,
phys_out[uniq_freq].start_time,
os.path.join(extra_dir, os.path.splitext(os.path.basename(outfile))[0]))
os.path.join(conversion_path,
os.path.splitext(os.path.basename(outfile))[0]))


def _main(argv=None):
Expand All @@ -326,7 +341,7 @@ def _main(argv=None):


if __name__ == '__main__':
_main()
_main(sys.argv[1:])

"""
Copyright 2019, The Phys2BIDS community.
Expand Down
67 changes: 36 additions & 31 deletions phys2bids/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ def test_logger(multifreq_lab_file):
test_chtrig = 3
test_ntp = 1
test_outdir = test_path
extra_dir = os.path.join(test_path, 'bids_ignore')
conversion_path = os.path.join(test_path, 'code/conversion')
# Phys2bids call through terminal
subprocess.run(f'phys2bids -in {test_filename} -indir {test_path} '
f'-chtrig {test_chtrig} -ntp {test_ntp} -outdir {test_outdir}',
shell=True, check=True)

assert os.path.isfile(os.path.join(conversion_path, 'call.sh'))

# Read logger file
logger_file = glob.glob(os.path.join(extra_dir, '*phys2bids*'))[0]
logger_file = glob.glob(os.path.join(conversion_path, '*phys2bids*'))[0]
with open(logger_file) as logger_info:
logger_info = logger_info.readlines()

Expand All @@ -51,6 +53,7 @@ def test_logger(multifreq_lab_file):

# Removes generated files
os.remove(os.path.join(test_path, logger_file))
shutil.rmtree(conversion_path)


def test_integration_txt(samefreq_short_txt_file):
Expand All @@ -60,7 +63,7 @@ def test_integration_txt(samefreq_short_txt_file):

test_path, test_filename = os.path.split(samefreq_short_txt_file)
test_chtrig = 2
extra_dir = os.path.join(test_path, 'bids_ignore')
conversion_path = os.path.join(test_path, 'code/conversion')

phys2bids(filename=test_filename, indir=test_path, outdir=test_path,
chtrig=test_chtrig, num_timepoints_expected=1)
Expand All @@ -71,10 +74,11 @@ def test_integration_txt(samefreq_short_txt_file):

# Check files in extra are generated
for suffix in ['.log', '_trigger_time.png']:
assert os.path.isfile(os.path.join(extra_dir, 'Test_belt_pulse_samefreq_short' + suffix))
assert os.path.isfile(os.path.join(conversion_path,
'Test_belt_pulse_samefreq_short' + suffix))

# Read log file (note that this file is not the logger file)
with open(os.path.join(extra_dir, 'Test_belt_pulse_samefreq_short.log')) as log_info:
with open(os.path.join(conversion_path, 'Test_belt_pulse_samefreq_short.log')) as log_info:
log_info = log_info.readlines()

# Check timepoints expected
Expand Down Expand Up @@ -102,7 +106,7 @@ def test_integration_txt(samefreq_short_txt_file):
os.remove(filename)
for filename in glob.glob(os.path.join(test_path, 'Test_belt_pulse_samefreq_short*')):
os.remove(filename)
shutil.rmtree(extra_dir)
shutil.rmtree(conversion_path)


def test_integration_acq(samefreq_full_acq_file):
Expand All @@ -112,7 +116,7 @@ def test_integration_acq(samefreq_full_acq_file):

test_path, test_filename = os.path.split(samefreq_full_acq_file)
test_chtrig = 3
extra_dir = os.path.join(test_path, 'bids_ignore')
conversion_path = os.path.join(test_path, 'code/conversion')

phys2bids(filename=test_filename, indir=test_path, outdir=test_path,
chtrig=test_chtrig, num_timepoints_expected=1)
Expand All @@ -123,10 +127,10 @@ def test_integration_acq(samefreq_full_acq_file):

# Check files in extra are generated
for suffix in ['.log', '_trigger_time.png']:
assert os.path.isfile(os.path.join(extra_dir, 'Test_belt_pulse_samefreq' + suffix))
assert os.path.isfile(os.path.join(conversion_path, 'Test_belt_pulse_samefreq' + suffix))

# Read log file (note that this file is not the logger file)
with open(os.path.join(extra_dir, 'Test_belt_pulse_samefreq.log')) as log_info:
with open(os.path.join(conversion_path, 'Test_belt_pulse_samefreq.log')) as log_info:
log_info = log_info.readlines()

# Check timepoints expected
Expand All @@ -151,11 +155,11 @@ def test_integration_acq(samefreq_full_acq_file):
'MR TRIGGER - Custom, HLT100C - A 5', 'PPG100C', 'CO2', 'O2']

# Remove generated files
for filename in glob.glob(os.path.join(extra_dir, 'phys2bids*')):
for filename in glob.glob(os.path.join(conversion_path, 'phys2bids*')):
os.remove(filename)
for filename in glob.glob(os.path.join(test_path, 'Test_belt_pulse_samefreq*')):
os.remove(filename)
shutil.rmtree(extra_dir)
shutil.rmtree(conversion_path)


def test_integration_multifreq(multifreq_lab_file):
Expand All @@ -165,7 +169,7 @@ def test_integration_multifreq(multifreq_lab_file):

test_path, test_filename = os.path.split(multifreq_lab_file)
test_chtrig = 3
extra_dir = os.path.join(test_path, 'bids_ignore')
conversion_path = os.path.join(test_path, 'code/conversion')

phys2bids(filename=test_filename, indir=test_path, outdir=test_path,
chtrig=test_chtrig, num_timepoints_expected=1)
Expand All @@ -184,15 +188,16 @@ def test_integration_multifreq(multifreq_lab_file):
assert os.path.isfile(os.path.join(test_path,
'Test1_multifreq_onescan_1000.0' + suffix))
for freq in ['40', '100', '500', '1000']:
assert os.path.isfile(os.path.join(extra_dir,
assert os.path.isfile(os.path.join(conversion_path,
'Test1_multifreq_onescan_' + freq + '.log'))
assert os.path.isfile(os.path.join(extra_dir, 'Test1_multifreq_onescan_trigger_time.png'))
assert os.path.isfile(os.path.join(conversion_path,
'Test1_multifreq_onescan_trigger_time.png'))

"""
Checks 40 Hz output
"""
# Read log file of frequency 625 (note that this file is not the logger file)
with open(os.path.join(extra_dir, 'Test1_multifreq_onescan_40.log')) as log_info:
with open(os.path.join(conversion_path, 'Test1_multifreq_onescan_40.log')) as log_info:
log_info = log_info.readlines()

# Check timepoints expected
Expand All @@ -219,7 +224,7 @@ def test_integration_multifreq(multifreq_lab_file):
Checks 100 Hz output
"""
# Read log file of frequency 625 (note that this file is not the logger file)
with open(os.path.join(extra_dir, 'Test1_multifreq_onescan_100.log')) as log_info:
with open(os.path.join(conversion_path, 'Test1_multifreq_onescan_100.log')) as log_info:
log_info = log_info.readlines()

# Check timepoints expected
Expand All @@ -246,7 +251,7 @@ def test_integration_multifreq(multifreq_lab_file):
Checks 500 Hz output
"""
# Read log file of frequency 625 (note that this file is not the logger file)
with open(os.path.join(extra_dir, 'Test1_multifreq_onescan_500.log')) as log_info:
with open(os.path.join(conversion_path, 'Test1_multifreq_onescan_500.log')) as log_info:
log_info = log_info.readlines()

# Check timepoints expected
Expand All @@ -273,7 +278,7 @@ def test_integration_multifreq(multifreq_lab_file):
Checks 1000 Hz output
"""
# Read log file of frequency 625 (note that this file is not the logger file)
with open(os.path.join(extra_dir, 'Test1_multifreq_onescan_1000.log')) as log_info:
with open(os.path.join(conversion_path, 'Test1_multifreq_onescan_1000.log')) as log_info:
log_info = log_info.readlines()

# Check timepoints expected
Expand All @@ -297,11 +302,11 @@ def test_integration_multifreq(multifreq_lab_file):
assert json_data['Columns'] == ['time', 'Trigger']

# Remove generated files
for filename in glob.glob(os.path.join(extra_dir, 'phys2bids*')):
for filename in glob.glob(os.path.join(conversion_path, 'phys2bids*')):
os.remove(filename)
for filename in glob.glob(os.path.join(test_path, 'Test_belt_pulse_multifreq*')):
os.remove(filename)
shutil.rmtree(extra_dir)
shutil.rmtree(conversion_path)


def test_integration_heuristic(samefreq_short_txt_file):
Expand All @@ -313,7 +318,7 @@ def test_integration_heuristic(samefreq_short_txt_file):
test_full_path = os.path.join(test_path, test_filename)
test_chtrig = 1
test_outdir = test_path
extra_dir = os.path.join(test_path, 'bids_ignore')
conversion_path = os.path.join(test_path, 'code/conversion')
test_ntp = 158
test_tr = 1.2
test_thr = 0.735
Expand All @@ -329,10 +334,10 @@ def test_integration_heuristic(samefreq_short_txt_file):
base_filename = 'sub-006_ses-01_task-test_rec-biopac_run-01_physio'
for suffix in ['.json', '.tsv.gz']:
assert os.path.isfile(os.path.join(test_path_output, base_filename + suffix))
assert os.path.isfile(os.path.join(extra_dir, base_filename + '.log'))
assert os.path.isfile(os.path.join(conversion_path, base_filename + '.log'))
# Read log file (note that this file is not the logger file)
log_filename = 'sub-006_ses-01_task-test_rec-biopac_run-01_physio.log'
with open(os.path.join(extra_dir, log_filename)) as log_info:
with open(os.path.join(conversion_path, log_filename)) as log_info:
log_info = log_info.readlines()

# Check timepoints expected
Expand Down Expand Up @@ -370,13 +375,13 @@ def test_integration_heuristic(samefreq_short_txt_file):
counter += 1

# Remove generated files
for filename in glob.glob(os.path.join(extra_dir, 'phys2bids*')):
for filename in glob.glob(os.path.join(conversion_path, 'phys2bids*')):
os.remove(filename)
for filename in glob.glob(os.path.join(test_path, 'Test_belt_pulse_samefreq*')):
os.remove(filename)
for filename in glob.glob(os.path.join(test_path_output, '*')):
os.remove(filename)
shutil.rmtree(extra_dir)
shutil.rmtree(conversion_path)


def test_integration_info(samefreq_short_txt_file):
Expand All @@ -390,7 +395,7 @@ def test_integration_info(samefreq_short_txt_file):
test_ntp = 158
test_tr = 1.2
test_thr = 0.735
extra_dir = os.path.join(test_path, 'bids_ignore')
conversion_path = os.path.join(test_path, 'code/conversion')
# Move into folder
subprocess.run(f'cd {test_path}', shell=True, check=True)
# Phys2bids call through terminal
Expand All @@ -402,11 +407,11 @@ def test_integration_info(samefreq_short_txt_file):
subprocess.run(command_str, shell=True, check=True)

# Check that plot all file is generated
assert os.path.isfile(os.path.join(test_outdir,
'bids_ignore/Test_belt_pulse_samefreq_short.png'))
assert os.path.isfile(os.path.join(conversion_path,
'Test_belt_pulse_samefreq_short.png'))

# Read logger file
logger_file = glob.glob(os.path.join(extra_dir, '*phys2bids*'))[0]
logger_file = glob.glob(os.path.join(conversion_path, '*phys2bids*'))[0]
with open(logger_file) as logger_info:
logger_info = logger_info.readlines()

Expand All @@ -415,6 +420,6 @@ def test_integration_info(samefreq_short_txt_file):
'02. MR TRIGGER - Custom, HLT100C - A 5; sampled at', '10000.0')

# Remove generated files
for filename in glob.glob(os.path.join(extra_dir, 'phys2bids*')):
for filename in glob.glob(os.path.join(conversion_path, 'phys2bids*')):
os.remove(filename)
shutil.rmtree(extra_dir)
shutil.rmtree(conversion_path)

0 comments on commit 8d42c28

Please sign in to comment.