DOSMA icon indicating copy to clipboard operation
DOSMA copied to clipboard

Allow `group_by` and `sort_by` to take a list of tags

Open sluijs opened this issue 2 years ago • 3 comments

For some DICOM datasets I would like to order the slices based on multiple tags. For example, in a dataset with a TemporalPositionIdentifier present, I would like to sort by ImagePositionPatient first, and then using the position identifier. The same applies to group_by too.

Preferably, it would be nice to add support for ascending/descending sorts for each tag too:

# single tag
dm.read(path, sort_by="InstanceNumber")

# multiple tags
dm.read(path, sort_by=["ImagePositionPatient", "TemporalPositionIdentifier"])

# multiple tags + order
dm.read(path, sort_by=[{ "tag": "ImagePositionPatient", "asc": True }, { "tag": "TemporalPositionIdentifier", "asc": False }])

NB: maybe we should consider to set the default sort_by to ImagePositionPatient.

sluijs avatar Sep 08 '22 16:09 sluijs

DOSMA supports multi-tag sorting/group_by - https://github.com/ad12/DOSMA/blob/bd5efecbb944263c9a5d7853f154d9071c72ba62/dosma/core/io/dicom_io.py#L83-L84

For arguments to sort_by, it may be easier to specify a sort_key argument that allows the user to customize their sorting algorithm

ad12 avatar Sep 09 '22 16:09 ad12

Default for sort_by is InstanceNumber because 1) it is the default value to sort by when writing dicoms and 2) it is scalar value that is easier to sort by. It is unclear what sorting by position would mean in cases where all coordinates in the position are changing (e.g. oblique scans)

ad12 avatar Sep 09 '22 16:09 ad12

DOSMA supports multi-tag sorting/group_by -

Ha. Maybe an option for a callback would be nice for users to define their own sorting algo. Is that what you meant with sort_key?

Default for sort_by is InstanceNumber because 1) it is the default value to sort by when writing dicoms and 2) it is scalar value that is easier to sort by. It is unclear what sorting by position would mean in cases where all coordinates in the position are changing (e.g. oblique scans)

The problem with InstanceNumber is, that doesn't have to correlate with slice position at all. DICOM does not enforce that in the standard IIRC. I'll look into other implementations, but I'm pretty sure most of them use ImagePositionPatient + ImageOrientationPatient.

sluijs avatar Sep 09 '22 16:09 sluijs