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 6 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
62 changes: 52 additions & 10 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 trigger in blue, a block in orange that indicates the start and end of the time,
red line for the threshold used for trigger detection
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
2. Same plot but this showing only the intial trigger
3. Same plot but this showing only the intial trigger
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
More info in the howto.rst and doc page
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -33,6 +36,15 @@ def plot_trigger(time, trigger, fileprefix, tr, thr, num_timepoints_expected,
figsize: tuple
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Desired size of the figure (see `matplotlib`),
Default is {FIGSIZE}
tr: float
Repetition time
thr:float
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Threshold used to detect the number of triggers
num_timepoints_expected: int
Number of timepoints expected by the user
figsize: integer tuple or list
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
Default is {SET_DPI}
Expand Down Expand Up @@ -114,19 +126,49 @@ 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
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
Parameters
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
----------
ch_name : (ch) list of strings
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
List of names of the channels - can be the header of the columns
in the output files.
timeseries : (ch, [tps]) list
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
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
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
List of the units of the channels.
freq : (ch) list of floats
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
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`.
"""
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