How to detect MRI file being loaded?

Hello,

I need to log the header of the MRI file. How can I detect if an MRI file is loaded in slicer?

Thanks for your help.

If you need to do something whenever a node is added, you can listen for the slicer.vtkMRMLScene.NodeAddedEvent. Example

What kind of information is in the header depends on the file format, so you would need to figure out how your file format indicates whether the modality is MRI. Not all formats indicate this information in any standard way.

It is also possible to add a callback for DICOM image having been loaded. Let me know if that’s what you want instead.

The user will be working only with .nii files. (Most of the times atleast)

Can you please elaborate on that?

You can add this in .slicerrc.py or if you have a custom module then to its initialization:

    dicomWidget = slicer.modules.dicom.widgetRepresentation()
    dicomWidgetSelf = dicomWidget.self()
    # Override onLoadingFinished to use customized function
    dicomWidgetSelf.browserWidget.onLoadingFinished = self.onDicomLoadingFinished

Then you can implement onDicomLoadingFinished as you desire. You can check the modality of the DICOM, and it it’s an MRI, then you can do what you need with that (you provided very little information on what you want to do).

1 Like

Thanks for your help.
I have only one more question.
Is there any way to find out if the vtkMRMLScalarVolumeNode came from a nifty file or a dicom file?

Yes. If loaded from DICOM, the volume node and its subject hierarchy item has all kinds of attributes

If loaded from file, the volume node’s storage node has the file name.

3 Likes

Thank you so much for your help.

1 Like