File size: 7,987 Bytes
38f4bf8
 
 
 
 
 
 
 
5bc980b
 
 
 
 
 
 
 
 
 
38f4bf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bc980b
38f4bf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
201
202
203
204
205
206
import datasets
import pandas as pd
from pathlib import Path
from PIL import ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

_URLS = {
    "F-45": "https://zenodo.org/records/7912264/files/embryo_dataset_F-45.tar.gz?download=1",
    "F-30": "https://zenodo.org/records/7912264/files/embryo_dataset_F-30.tar.gz?download=1",
    "F-15": "https://zenodo.org/records/7912264/files/embryo_dataset_F-15.tar.gz?download=1",
    "F0": "https://zenodo.org/records/7912264/files/embryo_dataset.tar.gz?download=1",
    "F+15": "https://zenodo.org/records/7912264/files/embryo_dataset_F15.tar.gz?download=1",
    "F+30": "https://zenodo.org/records/7912264/files/embryo_dataset_F30.tar.gz?download=1",
    "F+45": "https://zenodo.org/records/7912264/files/embryo_dataset_F45.tar.gz?download=1",
    "grades": "https://zenodo.org/records/7912264/files/embryo_dataset_grades.csv?download=1",
    "annotations": "https://zenodo.org/records/7912264/files/embryo_dataset_annotations.tar.gz?download=1",
    "time_elapsed": "https://zenodo.org/records/7912264/files/embryo_dataset_time_elapsed.tar.gz?download=1",
}

_EVENT_NAMES = [
    "tPB2", "tPNa", "tPNf", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9+", "tM", "tSB", "tB", "tEB", "tHB",
]

_GRADES = ["A", "B", "C", "NA"]

_DESCRIPTION = """
This dataset is composed of 704 videos, each recorded at 7 focal planes, accompanied by the annotations of 16 cellular events.
"""

_VERSION = datasets.Version("0.3.0")

_HOMEPAGE = "https://zenodo.org/record/7912264"

_LICENSE = "CC BY-NC-SA 4.0"

class HumanEmbryoTimelapse(datasets.GeneratorBasedBuilder):

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            version=_VERSION,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            features=datasets.Features(
                {
                    "name": datasets.Value("string"),
                    "F-45": datasets.Sequence(datasets.Image()),
                    "F-30": datasets.Sequence(datasets.Image()),
                    "F-15": datasets.Sequence(datasets.Image()),
                    "F0": datasets.Sequence(datasets.Image()),
                    "F+45": datasets.Sequence(datasets.Image()),
                    "F+30": datasets.Sequence(datasets.Image()),
                    "F+15": datasets.Sequence(datasets.Image()),
                    "events": datasets.Sequence(
                        {
                            "name": datasets.ClassLabel(names=_EVENT_NAMES),
                            "frame_index_start": datasets.Value("uint16"),
                            "frame_index_stop": datasets.Value("uint16"),
                        },
                    ),
                    "timeline": {
                        "frame_index": datasets.Sequence(datasets.Value("uint16")),
                        "time": datasets.Sequence(datasets.Value("float32")),
                    },
                    "grades": {
                        "TE": datasets.ClassLabel(names=_GRADES),
                        "ICM": datasets.ClassLabel(names=_GRADES),
                    }
                }
            ),
        )

    def _split_generators(self, dl_manager):
        """Generate splits."""

        # download and extract all files
        directories = {
            name: Path(dl_manager.download_and_extract(url))
            for name, url in _URLS.items()
        }

        # get all subfolders of embryo_names_dir
        embryo_names_dir = directories["F0"] / "embryo_dataset"
        embryo_names = [x.name for x in embryo_names_dir.iterdir() if x.is_dir()]

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "embryo_names": embryo_names,
                    "directories": directories,
                },
            )
        ]

    def _generate_examples(self, embryo_names, directories):
        """Generate images and labels for splits."""
        
        # get grades for each embryo (name, TE, ICM)
        pd_grades = pd.read_csv(directories["grades"], keep_default_na=False)
        grades = {
            row["video_name"]: {
                "TE": row["TE"],
                "ICM": row["ICM"],
            }
            for _, row in pd_grades.iterrows()
        }

        for index, embryo_name in enumerate(embryo_names):

            # get events of the embryo (name, frame_index_start, frame_index_stop)
            pd_events = pd.read_csv(directories["annotations"] / "embryo_dataset_annotations" / f"{embryo_name}_phases.csv", header=None)
            events = [
                {
                    "name": row[0],
                    "frame_index_start": row[1],
                    "frame_index_stop": row[2],
                }
                for _, row in pd_events.iterrows()
            ]

            # get frame index and time
            pd_time = pd.read_csv(directories["time_elapsed"] / "embryo_dataset_time_elapsed" / f"{embryo_name}_timeElapsed.csv")
            timeline = {
                "frame_index": pd_time["frame_index"].tolist(),
                "time": pd_time["time"].tolist(),
            }

            # get images of the embryo, with focal plane -45
            F_m45 = list(map(
                lambda x: str(x),
                sorted(
                    (directories["F-45"] / "embryo_dataset_F-45" / embryo_name).glob("*.jpeg"),
                    key=lambda x: int(x.stem.split("RUN")[-1]),
                ),
            ))

            # get images of the embryo, with focal plane -30
            F_m30 = list(map(
                lambda x: str(x),
                sorted(
                    (directories["F-30"] / "embryo_dataset_F-30" / embryo_name).glob("*.jpeg"),
                    key=lambda x: int(x.stem.split("RUN")[-1]),
                ),
            ))

            # get images of the embryo, with focal plane -15
            F_m15 = list(map(
                lambda x: str(x),
                sorted(
                    (directories["F-15"] / "embryo_dataset_F-15" / embryo_name).glob("*.jpeg"),
                    key=lambda x: int(x.stem.split("RUN")[-1]),
                ),
            ))

            # get images of the embryo, with focal plane 0
            F_zero = list(map(
                lambda x: str(x),
                sorted(
                    (directories["F0"] / "embryo_dataset" / embryo_name).glob("*.jpeg"),
                    key=lambda x: int(x.stem.split("RUN")[-1]),
                ),
            ))

            # get images of the embryo, with focal plane +15
            F_p15 = list(map(
                lambda x: str(x),
                sorted(
                    (directories["F+15"] / "embryo_dataset_F15" / embryo_name).glob("*.jpeg"),
                    key=lambda x: int(x.stem.split("RUN")[-1]),
                ),
            ))

            # get images of the embryo, with focal plane +30
            F_p30 = list(map(
                lambda x: str(x),
                sorted(
                    (directories["F+30"] / "embryo_dataset_F30" / embryo_name).glob("*.jpeg"),
                    key=lambda x: int(x.stem.split("RUN")[-1]),
                ),
            ))

            # get images of the embryo, with focal plane +45
            F_p45 = list(map(
                lambda x: str(x),
                sorted(
                    (directories["F+45"] / "embryo_dataset_F45" / embryo_name).glob("*.jpeg"),
                    key=lambda x: int(x.stem.split("RUN")[-1]),
                ),
            ))

            yield index, {
                "name": embryo_name,
                "F-45": F_m45,
                "F-30": F_m30,
                "F-15": F_m15,
                "F0": F_zero,
                "F+15": F_p15,
                "F+30": F_p30,
                "F+45": F_p45,
                "events": events,
                "grades": grades[embryo_name],
                "timeline": timeline,
            }