Download Human-Embryo-Timelapse.py from 1aurent/Human-Embryo-Timelapse: direct link, hf CLI and curl.
- Browser
- Download file 7.99 kB
-
https://huggingface.co/datasets/1aurent/Human-Embryo-Timelapse/resolve/main/Human-Embryo-Timelapse.py
- Command line
-
hf download hf://datasets/1aurent/Human-Embryo-Timelapse/Human-Embryo-Timelapse.py
-
curl -L -o Human-Embryo-Timelapse.py https://huggingface.co/datasets/1aurent/Human-Embryo-Timelapse/resolve/main/Human-Embryo-Timelapse.py
7.99 kB
| 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, | |
| } | |