calc_duration.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from types import SimpleNamespace
  2. import numpy as np
  3. from seqgen.pypulseq.block_to_events import block_to_events
  4. def calc_duration(*args: SimpleNamespace) -> float:
  5. """
  6. Calculate the duration of an event or block.
  7. Parameters
  8. ----------
  9. args : SimpleNamespace
  10. Block or events.
  11. Returns
  12. -------
  13. duration : float
  14. Cumulative duration of `args`.
  15. """
  16. events = block_to_events(*args)
  17. duration = 0
  18. for event in events:
  19. if isinstance(event, (float, int)): # block_duration field
  20. assert duration <= event
  21. duration = event
  22. continue
  23. if not isinstance(event, (dict, SimpleNamespace)):
  24. raise TypeError(
  25. "input(s) should be of type SimpleNamespace or a dict() in case of LABELINC or LABELSET"
  26. )
  27. if event.type == "delay":
  28. duration = np.max([duration, event.delay])
  29. elif event.type == "rf":
  30. duration = np.max(
  31. [duration, event.delay + event.shape_dur + event.ringdown_time]
  32. )
  33. elif event.type == "grad":
  34. duration = np.max([duration, event.delay + event.shape_dur])
  35. elif event.type == "adc":
  36. duration = np.max(
  37. [
  38. duration,
  39. event.delay + event.num_samples * event.dwell + event.dead_time,
  40. ]
  41. )
  42. elif event.type == "trap":
  43. duration = np.max(
  44. [
  45. duration,
  46. event.delay + event.rise_time + event.flat_time + event.fall_time,
  47. ]
  48. )
  49. elif event.type == "output" or event.type == "trigger":
  50. duration = np.max([duration, event.delay + event.duration])
  51. return duration