Skip to content

Commit 0651a2a

Browse files
authored
Merge pull request #448 from courtois-neuromod/fix/huge_memleaks_perfs_issues
Improve performance by using numpy for trigger detection and closing matplotlib figures to avoid memleaks.
2 parents 6bc0acb + 6b186af commit 0651a2a

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

phys2bids/physio_obj.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,7 @@ def check_trigger_amount(self, thr=None, num_timepoints_expected=0, tr=0):
524524
thr = np.mean(trigger)
525525
flag = 1
526526
timepoints = trigger > thr
527-
num_timepoints_found = len(
528-
[is_true for is_true, _ in groupby(timepoints, lambda x: x != 0) if is_true]
529-
)
527+
num_timepoints_found = np.count_nonzero(np.ediff1d(timepoints.astype(np.int8)) > 0)
530528
if flag == 1:
531529
LGR.info(
532530
f"The number of timepoints according to the std_thr method "

phys2bids/tests/test_viz.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_plot_all(samefreq_full_acq_file):
2020
assert os.path.isfile(
2121
os.path.join(test_path, os.path.splitext(os.path.basename(test_filename))[0] + ".png")
2222
)
23+
assert len(viz.plt.get_fignums()) == 0, "plots are not closed, this can cause memory leaks"
2324

2425

2526
# Expected to fail due to trigger plot issue
@@ -33,3 +34,4 @@ def test_plot_trigger(samefreq_full_acq_file):
3334
phys_obj.timeseries[0], phys_obj.timeseries[chtrig], out, 1.5, 1.6, 60, test_filename
3435
)
3536
assert os.path.isfile(out + "_trigger_time.png")
37+
assert len(viz.plt.get_fignums()) == 0, "plots are not closed, this can cause memory leaks"

phys2bids/viz.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,4 @@ def plot_all(ch_name, timeseries, units, freq, infile, outfile, dpi=SET_DPI, siz
267267
outfile = os.path.join(outfile, os.path.splitext(os.path.basename(infile))[0] + ".png")
268268
LGR.info(f"saving channel plot to {outfile}")
269269
fig.savefig(outfile, dpi=dpi, bbox_inches="tight")
270+
plt.close()

0 commit comments

Comments
 (0)