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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import datasets
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from PIL import ImageFile
|
| 6 |
+
|
| 7 |
+
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
| 8 |
+
|
| 9 |
+
_URLS = {
|
| 10 |
+
"F-45": "https://zenodo.org/records/7912264/files/embryo_dataset_F-45.tar.gz",
|
| 11 |
+
"F-30": "https://zenodo.org/records/7912264/files/embryo_dataset_F-30.tar.gz",
|
| 12 |
+
"F-15": "https://zenodo.org/records/7912264/files/embryo_dataset_F-15.tar.gz",
|
| 13 |
+
"F0": "https://zenodo.org/records/7912264/files/embryo_dataset.tar.gz",
|
| 14 |
+
"F+15": "https://zenodo.org/records/7912264/files/embryo_dataset_F15.tar.gz",
|
| 15 |
+
"F+30": "https://zenodo.org/records/7912264/files/embryo_dataset_F30.tar.gz",
|
| 16 |
+
"F+45": "https://zenodo.org/records/7912264/files/embryo_dataset_F45.tar.gz",
|
| 17 |
+
"grades": "https://zenodo.org/records/7912264/files/embryo_dataset_grades.csv",
|
| 18 |
+
"annotations": "https://zenodo.org/records/7912264/files/embryo_dataset_annotations.tar.gz",
|
| 19 |
+
"time_elapsed": "https://zenodo.org/records/7912264/files/embryo_dataset_time_elapsed.tar.gz",
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
_EVENT_NAMES = [
|
| 23 |
+
"tPB2", "tPNa", "tPNf", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9+", "tM", "tSB", "tB", "tEB", "tHB",
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
_GRADES = ["A", "B", "C", "NA"]
|
| 27 |
+
|
| 28 |
+
_DESCRIPTION = """
|
| 29 |
+
This dataset is composed of 704 videos, each recorded at 7 focal planes, accompanied by the annotations of 16 cellular events.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
_VERSION = datasets.Version("0.3.0")
|
| 33 |
+
|
| 34 |
+
_HOMEPAGE = "https://zenodo.org/record/7912264"
|
| 35 |
+
|
| 36 |
+
_LICENSE = "CC BY-NC-SA 4.0"
|
| 37 |
+
|
| 38 |
+
class HumanEmbryoTimelapse(datasets.GeneratorBasedBuilder):
|
| 39 |
+
|
| 40 |
+
def _info(self):
|
| 41 |
+
return datasets.DatasetInfo(
|
| 42 |
+
description=_DESCRIPTION,
|
| 43 |
+
version=_VERSION,
|
| 44 |
+
homepage=_HOMEPAGE,
|
| 45 |
+
license=_LICENSE,
|
| 46 |
+
features=datasets.Features(
|
| 47 |
+
{
|
| 48 |
+
"name": datasets.Value("string"),
|
| 49 |
+
"F-45": datasets.Sequence(datasets.Image()),
|
| 50 |
+
"F-30": datasets.Sequence(datasets.Image()),
|
| 51 |
+
"F-15": datasets.Sequence(datasets.Image()),
|
| 52 |
+
"F0": datasets.Sequence(datasets.Image()),
|
| 53 |
+
"F+45": datasets.Sequence(datasets.Image()),
|
| 54 |
+
"F+30": datasets.Sequence(datasets.Image()),
|
| 55 |
+
"F+15": datasets.Sequence(datasets.Image()),
|
| 56 |
+
"events": datasets.Sequence(
|
| 57 |
+
{
|
| 58 |
+
"name": datasets.ClassLabel(names=_EVENT_NAMES),
|
| 59 |
+
"frame_index_start": datasets.Value("uint16"),
|
| 60 |
+
"frame_index_stop": datasets.Value("uint16"),
|
| 61 |
+
},
|
| 62 |
+
),
|
| 63 |
+
"timeline": {
|
| 64 |
+
"frame_index": datasets.Sequence(datasets.Value("uint16")),
|
| 65 |
+
"time": datasets.Sequence(datasets.Value("float32")),
|
| 66 |
+
},
|
| 67 |
+
"grades": {
|
| 68 |
+
"TE": datasets.ClassLabel(names=_GRADES),
|
| 69 |
+
"ICM": datasets.ClassLabel(names=_GRADES),
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
),
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def _split_generators(self, dl_manager):
|
| 76 |
+
"""Generate splits."""
|
| 77 |
+
|
| 78 |
+
# download and extract all files
|
| 79 |
+
directories = {
|
| 80 |
+
name: Path(dl_manager.download_and_extract(url))
|
| 81 |
+
for name, url in _URLS.items()
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
# get all subfolders of embryo_names_dir
|
| 85 |
+
embryo_names_dir = directories["F0"] / "embryo_dataset"
|
| 86 |
+
embryo_names = [x.name for x in embryo_names_dir.iterdir() if x.is_dir()]
|
| 87 |
+
|
| 88 |
+
return [
|
| 89 |
+
datasets.SplitGenerator(
|
| 90 |
+
name=datasets.Split.TRAIN,
|
| 91 |
+
gen_kwargs={
|
| 92 |
+
"embryo_names": embryo_names,
|
| 93 |
+
"directories": directories,
|
| 94 |
+
},
|
| 95 |
+
)
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
def _generate_examples(self, embryo_names, directories):
|
| 99 |
+
"""Generate images and labels for splits."""
|
| 100 |
+
|
| 101 |
+
# get grades for each embryo (name, TE, ICM)
|
| 102 |
+
pd_grades = pd.read_csv(directories["grades"], delimiter=',')
|
| 103 |
+
grades = {
|
| 104 |
+
row["video_name"]: {
|
| 105 |
+
"TE": row["TE"],
|
| 106 |
+
"ICM": row["ICM"],
|
| 107 |
+
}
|
| 108 |
+
for _, row in pd_grades.iterrows()
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
for index, embryo_name in enumerate(embryo_names):
|
| 112 |
+
|
| 113 |
+
# get events of the embryo (name, frame_index_start, frame_index_stop)
|
| 114 |
+
pd_events = pd.read_csv(directories["annotations"] / "embryo_dataset_annotations" / f"{embryo_name}_phases.csv", header=None)
|
| 115 |
+
events = [
|
| 116 |
+
{
|
| 117 |
+
"name": row[0],
|
| 118 |
+
"frame_index_start": row[1],
|
| 119 |
+
"frame_index_stop": row[2],
|
| 120 |
+
}
|
| 121 |
+
for _, row in pd_events.iterrows()
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
# get frame index and time
|
| 125 |
+
pd_time = pd.read_csv(directories["time_elapsed"] / "embryo_dataset_time_elapsed" / f"{embryo_name}_timeElapsed.csv")
|
| 126 |
+
timeline = {
|
| 127 |
+
"frame_index": pd_time["frame_index"].tolist(),
|
| 128 |
+
"time": pd_time["time"].tolist(),
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
# get images of the embryo, with focal plane -45
|
| 132 |
+
F_m45 = list(map(
|
| 133 |
+
lambda x: str(x),
|
| 134 |
+
sorted(
|
| 135 |
+
(directories["F-45"] / "embryo_dataset_F-45" / embryo_name).glob("*.jpeg"),
|
| 136 |
+
key=lambda x: int(x.stem.split("RUN")[-1]),
|
| 137 |
+
),
|
| 138 |
+
))
|
| 139 |
+
|
| 140 |
+
# get images of the embryo, with focal plane -30
|
| 141 |
+
F_m30 = list(map(
|
| 142 |
+
lambda x: str(x),
|
| 143 |
+
sorted(
|
| 144 |
+
(directories["F-30"] / "embryo_dataset_F-30" / embryo_name).glob("*.jpeg"),
|
| 145 |
+
key=lambda x: int(x.stem.split("RUN")[-1]),
|
| 146 |
+
),
|
| 147 |
+
))
|
| 148 |
+
|
| 149 |
+
# get images of the embryo, with focal plane -15
|
| 150 |
+
F_m15 = list(map(
|
| 151 |
+
lambda x: str(x),
|
| 152 |
+
sorted(
|
| 153 |
+
(directories["F-15"] / "embryo_dataset_F-15" / embryo_name).glob("*.jpeg"),
|
| 154 |
+
key=lambda x: int(x.stem.split("RUN")[-1]),
|
| 155 |
+
),
|
| 156 |
+
))
|
| 157 |
+
|
| 158 |
+
# get images of the embryo, with focal plane 0
|
| 159 |
+
F_zero = list(map(
|
| 160 |
+
lambda x: str(x),
|
| 161 |
+
sorted(
|
| 162 |
+
(directories["F0"] / "embryo_dataset" / embryo_name).glob("*.jpeg"),
|
| 163 |
+
key=lambda x: int(x.stem.split("RUN")[-1]),
|
| 164 |
+
),
|
| 165 |
+
))
|
| 166 |
+
|
| 167 |
+
# get images of the embryo, with focal plane +15
|
| 168 |
+
F_p15 = list(map(
|
| 169 |
+
lambda x: str(x),
|
| 170 |
+
sorted(
|
| 171 |
+
(directories["F+15"] / "embryo_dataset_F15" / embryo_name).glob("*.jpeg"),
|
| 172 |
+
key=lambda x: int(x.stem.split("RUN")[-1]),
|
| 173 |
+
),
|
| 174 |
+
))
|
| 175 |
+
|
| 176 |
+
# get images of the embryo, with focal plane +30
|
| 177 |
+
F_p30 = list(map(
|
| 178 |
+
lambda x: str(x),
|
| 179 |
+
sorted(
|
| 180 |
+
(directories["F+30"] / "embryo_dataset_F30" / embryo_name).glob("*.jpeg"),
|
| 181 |
+
key=lambda x: int(x.stem.split("RUN")[-1]),
|
| 182 |
+
),
|
| 183 |
+
))
|
| 184 |
+
|
| 185 |
+
# get images of the embryo, with focal plane +45
|
| 186 |
+
F_p45 = list(map(
|
| 187 |
+
lambda x: str(x),
|
| 188 |
+
sorted(
|
| 189 |
+
(directories["F+45"] / "embryo_dataset_F45" / embryo_name).glob("*.jpeg"),
|
| 190 |
+
key=lambda x: int(x.stem.split("RUN")[-1]),
|
| 191 |
+
),
|
| 192 |
+
))
|
| 193 |
+
|
| 194 |
+
yield index, {
|
| 195 |
+
"name": embryo_name,
|
| 196 |
+
"F-45": F_m45,
|
| 197 |
+
"F-30": F_m30,
|
| 198 |
+
"F-15": F_m15,
|
| 199 |
+
"F0": F_zero,
|
| 200 |
+
"F+15": F_p15,
|
| 201 |
+
"F+30": F_p30,
|
| 202 |
+
"F+45": F_p45,
|
| 203 |
+
"events": events,
|
| 204 |
+
"grades": grades[embryo_name],
|
| 205 |
+
"timeline": timeline,
|
| 206 |
+
}
|