Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update viz.py docstrings and Inputs #193

Merged
merged 8 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phys2bids/phys2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
phys_in.print_info(filename)
# #!# Here the function viz.plot_channel should be called
if chplot != '' or info:
viz.plot_all(phys_in, infile, chplot)
viz.plot_all(phys_in.ch_name, phys_in.timeseries, phys_in.units,
phys_in.freq, infile, chplot)
# If only info were asked, end here.
if info:
return
Expand Down
3 changes: 2 additions & 1 deletion phys2bids/tests/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def test_plot_all(samefreq_full_acq_file):
test_path, test_filename = os.path.split(samefreq_full_acq_file)
phys_obj = acq.populate_phys_input(samefreq_full_acq_file, chtrig)
out = os.path.join(test_path, 'Test_belt_pulse_samefreq.png')
viz.plot_all(phys_obj, test_filename, outfile=out)
viz.plot_all(phys_obj.ch_name, phys_obj.timeseries, phys_obj.units,
phys_obj.freq, test_filename, outfile=out)
assert os.path.isfile(out)


Expand Down
73 changes: 59 additions & 14 deletions phys2bids/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
def plot_trigger(time, trigger, fileprefix, tr, thr, num_timepoints_expected,
filename, figsize=FIGSIZE, dpi=SET_DPI):
"""
Produces a textfile of the specified extension `ext`,
containing the given content `text`.

Produces a figure with three plots:
1. Plots the triggers in blue, a block in orange that indicates
the time from the first trigger to the last, and a red line showing
the threshold used for trigger detection
2. Same plot but showing only the intial trigger
3. Same plot but showing only the intial trigger
Parameters
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
----------
time: numpy ndarray
Expand All @@ -26,12 +29,16 @@ def plot_trigger(time, trigger, fileprefix, tr, thr, num_timepoints_expected,
fileprefix: str or path
A string representing a file name or a fullpath
to a file, WITHOUT extension
tr: float
Repetition time
thr: float
Threshold used to detect the number of triggers
num_timepoints_expected: int
Number of timepoints expected by the user
filename: string
name of the original file
options: argparse object
The object produced by `get_parser` in `cli.run.py`
figsize: tuple
Desired size of the figure (see `matplotlib`),
figsize: tuple or list of int
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Size of the figure X*Y
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Default is {FIGSIZE}
dpi: int
Desired DPI of the figure (see `matplotlib`),
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -42,6 +49,10 @@ def plot_trigger(time, trigger, fileprefix, tr, thr, num_timepoints_expected,
Outcome:
fileprefix + _trigger_time.png:
Creates new plot `fileprefix_trigger_time.png`.
See Also
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
--------
https://phys2bids.readthedocs.io/en/latest/howto.html
https://matplotlib.org/
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
"""

def time2ntr(x):
Expand Down Expand Up @@ -114,19 +125,53 @@ def ntr2time(x):
plt.close()


def plot_all(phys_in, infile, outfile='', dpi=SET_DPI, size=FIGSIZE):
ch_num = len(phys_in.ch_name) # get number of channels:
def plot_all(ch_name, timeseries, units, freq, infile, outfile='', dpi=SET_DPI, size=FIGSIZE):
"""
Plots all the channels for visualizations and saves them in outfile
Parameters
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
----------
ch_name: (ch) list of strings
List of names of the channels - can be the header of the columns
in the output files.
timeseries: (ch, [tps]) list
List of numpy 1d arrays - one for channel, plus one for time.
Time channel has to be the first, trigger the second.
Contains all the timeseries recorded.
units: (ch) list of strings
List of the units of the channels.
freq: (ch) list of floats
List of floats - one per channel.
Contains all the frequencies of the recorded channel.
infile: string
name of the input file to phys2bids
outfile: string
path of the output plot
dpi: int
Desired DPI of the figure (see `matplotlib`),
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Default is {SET_DPI}
figsize: integer tuple or list
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Size of the figure X*Y
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Default is {FIGSIZE}
-----
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
outfile:
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Creates new plot `fileprefix_trigger_time.png`.
See Also
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
--------
https://phys2bids.readthedocs.io/en/latest/howto.html
https://matplotlib.org/
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
"""
ch_num = len(ch_name) # get number of channels:
fig, ax = plt.subplots(ch_num - 1, 1, figsize=size, sharex=True)
time = phys_in.timeseries[0] # assume time is first channel
time = timeseries[0] # assume time is first channel
fig.suptitle(os.path.basename(infile))
for row, timeser in enumerate(phys_in.timeseries[1:]):
for row, timeser in enumerate(timeseries[1:]):
if timeser.shape != time.shape:
time_old = np.linspace(0, time[-1], num=timeser.shape[0])
timeser = np.interp(time, time_old, timeser)
ax[row].plot(time, timeser)
ax[row].set_title(f' Channel {row + 1}: {phys_in.ch_name[row + 1]}')
ax[row].set_ylabel(phys_in.units[row + 1])
ax[row].xlim = 30 * 60 * phys_in.freq[0] # maximum display of half an hour
ax[row].set_title(f' Channel {row + 1}: {ch_name[row + 1]}')
ax[row].set_ylabel(units[row + 1])
ax[row].xlim = 30 * 60 * freq[0] # maximum display of half an hour
ax[row].grid()
ax[row].set_xlabel("seconds")
if outfile == '':
Expand Down