Skip to content

Commit

Permalink
fix path to seq examples and avoid use of exec
Browse files Browse the repository at this point in the history
  • Loading branch information
schuenke committed Jan 10, 2025
1 parent cfe7a6f commit b1a66d2
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions tests/test_sequence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib
import math
import os
import sys
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
Expand All @@ -11,9 +11,6 @@
from _pytest.python_api import ApproxBase
from pypulseq import Sequence

# Add parent directory to path, so sequences from ../examples can be imported
sys.path.insert(0, Path(__file__).parent.parent)

expected_output_path = Path(__file__).parent / 'expected_output'


Expand Down Expand Up @@ -129,11 +126,11 @@ def seq_make_sinc_pulses():
# Dummy sequence which contains only block pulses with different parameters
def seq_make_block_pulses():
seq = Sequence()
seq.add_block(pp.make_block_pulse(flip_angle=1))
seq.add_block(pp.make_block_pulse(flip_angle=1, duration=4e-3))
seq.add_block(pp.make_delay(1))
seq.add_block(pp.make_block_pulse(flip_angle=1, delay=1e-3))
seq.add_block(pp.make_block_pulse(flip_angle=1, delay=1e-3, duration=4e-3))
seq.add_block(pp.make_delay(1))
seq.add_block(pp.make_block_pulse(flip_angle=math.pi / 2))
seq.add_block(pp.make_block_pulse(flip_angle=math.pi / 2, duration=4e-3))
seq.add_block(pp.make_delay(1))
seq.add_block(pp.make_block_pulse(flip_angle=math.pi / 2, duration=1e-3))
seq.add_block(pp.make_delay(1))
Expand Down Expand Up @@ -232,14 +229,19 @@ def seq4():
# Create a seq_func for each example script and add it to the list of sequences.
# Defining a new function ensures that pytest understands the name is
# e.g. `write_gre` instead of `main`.
# Derive the relative path from the test file to the examples folder
examples_dir = Path(__file__).resolve().parents[1] / 'examples' / 'scripts'

for example in seq_examples:
exec(f"""
def {example}():
from examples.scripts.{example} import main
return main()
""") # noqa: S102

sequence_zoo.append(eval(f'{example}'))
def test_func(module_name=f'examples.{example}'):
spec = importlib.util.spec_from_file_location(module_name, examples_dir / f'{example}.py') # noqa: B023
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module.main()

test_func.__name__ = example
sequence_zoo.append(test_func)


# Main Sequence test class
Expand Down

0 comments on commit b1a66d2

Please sign in to comment.