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

TimeBlock types #34

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 11 additions & 6 deletions cpp/petsird_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using petsird::binary::PETSIRDReader;
#include <xtensor/xview.hpp>
#include <xtensor/xio.hpp>
#include <iostream>
#include <variant>

int
main(int argc, char* argv[])
Expand Down Expand Up @@ -64,13 +65,17 @@ main(int argc, char* argv[])
float last_time = 0.F;
while (reader.ReadTimeBlocks(time_block))
{
last_time = time_block.id * header.scanner.listmode_time_block_duration;
num_prompts += time_block.prompt_events.size();

for (auto& event : time_block.prompt_events)
if (std::holds_alternative<petsird::EventTimeBlock>(time_block))
{
energy_1 += energy_mid_points[event.energy_indices[0]];
energy_2 += energy_mid_points[event.energy_indices[1]];
auto& event_time_block = std::get<petsird::EventTimeBlock>(time_block);
last_time = event_time_block.start;
num_prompts += event_time_block.prompt_events.size();

for (auto& event : event_time_block.prompt_events)
{
energy_1 += energy_mid_points[event.energy_indices[0]];
energy_2 += energy_mid_points[event.energy_indices[1]];
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions cpp/petsird_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ get_scanner_info()
scanner_info.tof_bin_edges = tof_bin_edges;
scanner_info.tof_resolution = 9.4F; // in mm
scanner_info.energy_bin_edges = energy_bin_edges;
scanner_info.energy_resolution_at_511 = .11F; // as fraction of 511
scanner_info.listmode_time_block_duration = 1.F; // ms
scanner_info.energy_resolution_at_511 = .11F; // as fraction of 511
scanner_info.event_time_block_duration = 1.F; // ms
}

return scanner_info;
Expand Down Expand Up @@ -217,8 +217,8 @@ main(int argc, char* argv[])
std::poisson_distribution<> poisson(COUNT_RATE);
const auto num_prompts_this_block = poisson(gen);
const auto prompts_this_block = get_events(header, num_prompts_this_block);
petsird::TimeBlock time_block;
time_block.id = t;
petsird::EventTimeBlock time_block;
time_block.start = t * header.scanner.event_time_block_duration;
time_block.prompt_events = prompts_this_block;
writer.WriteTimeBlocks(time_block);
}
Expand Down
53 changes: 50 additions & 3 deletions model/Protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,68 @@ PETSIRD: !protocol
sequence:
header: Header
timeBlocks: !stream
# block with information (e.g. events, signals, etc.) occurring at a certain time or in a time interval
# Note: multiple time blocks can occur at the same start time
items: TimeBlock

Header: !record
fields:
scanner: ScannerInformation
exam: ExamInformation?

TimeBlock: !record
# types of timeBlocks
# TODO more types could be needed
TimeBlock: [EventTimeBlock, ExternalSignalTimeBlock, BedMovementTimeBlock, GantryMovementTimeBlock]

EventTimeBlock: !record
fields:
# number of the block. Multiply with listmodeTimeBlockDuration to get time since startOfAcquisition
id: uint
# start time since ExamInformation.startOfAcquisition in ms
# Note: duration is given by ScannerInformation.eventTimeBlockDuration
start: uint
# TODO encode end time?
# list of prompts in this time block
# TODO might be better to use !array
promptEvents: CoincidenceEvent*
# optional list of delayed coincidences in this time block
delayedEvents: CoincidenceEvent*?
# optional list of triple coincidences in this time block
tripleEvents: TripleEvent*?

ExternalSignalTypeEnum: !enum
values:
- ecgTrace
- ecgTrigger
- respTrace
- respTrigger
- otherMotionSignal
- otherMotionTrigger
- externalSync
# other options, to be listed in the future
- other

ExternalSignalType: !record
fields:
type: ExternalSignalTypeEnum
description: string
id: uint

ExternalSignalTimeBlock: !record
fields:
# start time since ExamInformation.startOfAcquisition in ms
start: uint
# refer to ExternalSignalType.id
signalID: uint
# Note for triggers, this field is to be ignored
signalValues: float*

BedMovementTimeBlock: !record
fields:
# start time since ExamInformation.startOfAcquisition in ms
start: uint
transform: RigidTransformation

GantryMovementTimeBlock: !record
fields:
# start time since ExamInformation.startOfAcquisition in ms
start: uint
transform: RigidTransformation
4 changes: 2 additions & 2 deletions model/ScannerInformation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ ScannerInformation: !record
# FWHM of photopeak for incoming gamma of 511 keV, expressed as a ratio w.r.t. 511
energyResolutionAt511: float

# duration of each time block in ms
listmodeTimeBlockDuration: uint
# duration of each event time block in ms
eventTimeBlockDuration: uint

# Encode how the scanner handles multiple coincidences
coincidencePolicy: CoincidencePolicy
Expand Down
11 changes: 6 additions & 5 deletions python/petsird_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
num_prompts = 0
last_time = 0
for time_block in reader.read_time_blocks():
last_time = time_block.id * header.scanner.listmode_time_block_duration
num_prompts += len(time_block.prompt_events)
for event in time_block.prompt_events:
energy_1 += energy_mid_points[event.energy_indices[0]]
energy_2 += energy_mid_points[event.energy_indices[1]]
if isinstance(time_block, petsird.TimeBlock.EventTimeBlock):
last_time = time_block.value.start # all TimeBlock types have this field
num_prompts += len(time_block.value.prompt_events)
for event in time_block.value.prompt_events:
energy_1 += energy_mid_points[event.energy_indices[0]]
energy_2 += energy_mid_points[event.energy_indices[1]]

print(f"Last time block at {last_time} ms")
print(f"Number of prompt events: {num_prompts}")
Expand Down
11 changes: 9 additions & 2 deletions python/petsird_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def get_scanner_info() -> petsird.ScannerInformation:
tof_resolution=9.4, # in mm
energy_bin_edges=energyBinEdges,
energy_resolution_at_511=0.11, # as fraction of 511
listmode_time_block_duration=1, # ms
event_time_block_duration=1, # ms
)


Expand Down Expand Up @@ -162,9 +162,16 @@ def get_events(
header = get_header()
writer.write_header(header)
for t in range(NUMBER_OF_TIME_BLOCKS):
start = t * header.scanner.event_time_block_duration
num_prompts_this_block = rng.poisson(COUNT_RATE)
prompts_this_block = list(get_events(header, num_prompts_this_block))
# Normally we'd write multiple blocks, but here we have just one, so let's write a tuple with just one element
writer.write_time_blocks(
(petsird.TimeBlock(id=t, prompt_events=prompts_this_block),)
(
petsird.TimeBlock.EventTimeBlock(
petsird.EventTimeBlock(
start=start, prompt_events=prompts_this_block
)
),
)
Comment on lines +170 to +176
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax works, but is a bit awkward. @johnstairs can this be simplified? (I didn't find any doc on creating unions at https://microsoft.github.io/yardl/python/language.html#generated-union-types)

)
Loading