Skip to content

prairie_view_loader.py

get_prairieview_metadata(ome_tif_filepath)

Extract metadata for scans generated by Prairie View acquisition software.

The Prairie View software generates one .ome.tif imaging file per frame acquired. The metadata for all frames is contained in one .xml file. This function locates the .xml file and generates a dictionary necessary to populate the DataJoint ScanInfo and Field tables. Prairie View works with resonance scanners with a single field. Prairie View does not support bidirectional x and y scanning. ROI information is not contained in the .xml file. All images generated using Prairie View have square dimensions(e.g. 512x512).

Parameters:

Name Type Description Default
ome_tif_filepath str

An absolute path to the .ome.tif image file.

required

Raises:

Type Description
FileNotFoundError

No .xml file containing information about the acquired scan was found at path in parent directory at ome_tif_filepath.

Returns:

Name Type Description
metainfo dict

A dict mapping keys to corresponding metadata values fetched from the .xml file.

Source code in element_interface/prairie_view_loader.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def get_prairieview_metadata(ome_tif_filepath: str) -> dict:
    """Extract metadata for scans generated by Prairie View acquisition software.

    The Prairie View software generates one `.ome.tif` imaging file per frame
    acquired. The metadata for all frames is contained in one .xml file. This
    function locates the .xml file and generates a dictionary necessary to
    populate the DataJoint `ScanInfo` and `Field` tables. Prairie View works
    with resonance scanners with a single field. Prairie View does not support
    bidirectional x and y scanning. ROI information is not contained in the
    `.xml` file. All images generated using Prairie View have square dimensions(e.g. 512x512).

    Args:
        ome_tif_filepath: An absolute path to the .ome.tif image file.

    Raises:
        FileNotFoundError: No .xml file containing information about the acquired scan
            was found at path in parent directory at `ome_tif_filepath`.

    Returns:
        metainfo: A dict mapping keys to corresponding metadata values fetched from the
            .xml file.
    """

    # May return multiple xml files. Only need one that contains scan metadata.
    xml_files_list = pathlib.Path(ome_tif_filepath).parent.glob("*.xml")

    for file in xml_files_list:
        xml_tree = ET.parse(file)
        xml_file = xml_tree.getroot()
        if xml_file.find(".//Sequence"):
            break
    else:
        raise FileNotFoundError(
            f"No PrarieView metadata .xml file found at {pathlib.Path(ome_tif_filepath).parent}"
        )

    bidirectional_scan = False  # Does not support bidirectional
    roi = 0
    n_fields = 1  # Always contains 1 field
    recording_start_time = xml_file.find(".//Sequence/[@cycle='1']").attrib.get("time")

    # Get all channels and find unique values
    channel_list = [
        int(channel.attrib.get("channel"))
        for channel in xml_file.iterfind(".//Sequence/Frame/File/[@channel]")
    ]
    n_channels = len(set(channel_list))
    n_frames = len(xml_file.findall(".//Sequence/Frame"))
    framerate = 1 / float(
        xml_file.findall('.//PVStateValue/[@key="framePeriod"]')[0].attrib.get("value")
    )  # rate = 1/framePeriod

    usec_per_line = (
        float(
            xml_file.findall(".//PVStateValue/[@key='scanLinePeriod']")[0].attrib.get(
                "value"
            )
        )
        * 1e6
    )  # Convert from seconds to microseconds

    scan_datetime = datetime.strptime(
        xml_file.attrib.get("date"), "%m/%d/%Y %I:%M:%S %p"
    )

    total_scan_duration = float(
        xml_file.findall(".//Sequence/Frame")[-1].attrib.get("relativeTime")
    )

    pixel_height = int(
        xml_file.findall(".//PVStateValue/[@key='pixelsPerLine']")[0].attrib.get(
            "value"
        )
    )
    # All PrairieView-acquired images have square dimensions (512 x 512; 1024 x 1024)
    pixel_width = pixel_height

    um_per_pixel = float(
        xml_file.find(
            ".//PVStateValue/[@key='micronsPerPixel']/IndexedValue/[@index='XAxis']"
        ).attrib.get("value")
    )

    um_height = um_width = float(pixel_height) * um_per_pixel

    # x and y coordinate values for the center of the field
    x_field = float(
        xml_file.find(
            ".//PVStateValue/[@key='currentScanCenter']/IndexedValue/[@index='XAxis']"
        ).attrib.get("value")
    )
    y_field = float(
        xml_file.find(
            ".//PVStateValue/[@key='currentScanCenter']/IndexedValue/[@index='YAxis']"
        ).attrib.get("value")
    )
    if (
        xml_file.find(
            ".//Sequence/[@cycle='1']/Frame/PVStateShard/PVStateValue/[@key='positionCurrent']/SubindexedValues/[@index='ZAxis']"
        )
        is None
    ):
        z_fields = np.float64(
            xml_file.find(
                ".//PVStateValue/[@key='positionCurrent']/SubindexedValues/[@index='ZAxis']/SubindexedValue"
            ).attrib.get("value")
        )
        n_depths = 1
        assert z_fields.size == n_depths
        bidirection_z = False

    else:
        bidirection_z = (
            xml_file.find(".//Sequence").attrib.get("bidirectionalZ") == "True"
        )

        # One "Frame" per depth in the .xml file. Gets number of frames in first sequence
        planes = [
            int(plane.attrib.get("index"))
            for plane in xml_file.findall(".//Sequence/[@cycle='1']/Frame")
        ]
        n_depths = len(set(planes))

        z_controllers = xml_file.findall(
            ".//Sequence/[@cycle='1']/Frame/[@index='1']/PVStateShard/PVStateValue/[@key='positionCurrent']/SubindexedValues/[@index='ZAxis']/SubindexedValue"
        )

        # If more than one Z-axis controllers are found, 
        # check which controller is changing z_field depth. Only 1 controller
        # must change depths.
        if len(z_controllers) > 1:
            z_repeats = []
            for controller in xml_file.findall(
                ".//Sequence/[@cycle='1']/Frame/[@index='1']/PVStateShard/PVStateValue/[@key='positionCurrent']/SubindexedValues/[@index='ZAxis']/"
            ):
                z_repeats.append(
                    [
                        float(z.attrib.get("value"))
                        for z in xml_file.findall(
                            ".//Sequence/[@cycle='1']/Frame/PVStateShard/PVStateValue/[@key='positionCurrent']/SubindexedValues/[@index='ZAxis']/SubindexedValue/[@subindex='{0}']".format(
                                controller.attrib.get("subindex")
                            )
                        )
                    ]
                )
            controller_assert = [
                not all(z == z_controller[0] for z in z_controller)
                for z_controller in z_repeats
            ]
            assert (
                sum(controller_assert) == 1
            ), "Multiple controllers changing z depth is not supported"

            z_fields = z_repeats[controller_assert.index(True)]

        else:
            z_fields = [
                z.attrib.get("value")
                for z in xml_file.findall(
                    ".//Sequence/[@cycle='1']/Frame/PVStateShard/PVStateValue/[@key='positionCurrent']/SubindexedValues/[@index='ZAxis']/SubindexedValue/[@subindex='0']"
                )
            ]

        assert (
            len(z_fields) == n_depths
        ), "Number of z fields does not match number of depths."

    metainfo = dict(
        num_fields=n_fields,
        num_channels=n_channels,
        num_planes=n_depths,
        num_frames=n_frames,
        num_rois=roi,
        x_pos=None,
        y_pos=None,
        z_pos=None,
        frame_rate=framerate,
        bidirectional=bidirectional_scan,
        bidirectional_z=bidirection_z,
        scan_datetime=scan_datetime,
        usecs_per_line=usec_per_line,
        scan_duration=total_scan_duration,
        height_in_pixels=pixel_height,
        width_in_pixels=pixel_width,
        height_in_um=um_height,
        width_in_um=um_width,
        fieldX=x_field,
        fieldY=y_field,
        fieldZ=z_fields,
        recording_time=recording_start_time,
    )

    return metainfo