andreped commited on
Commit
ea1a7dc
·
1 Parent(s): e9ddd19

Create IBDColEpi.py

Browse files
Files changed (1) hide show
  1. IBDColEpi.py +184 -0
IBDColEpi.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+ # TODO: Add BibTeX citation
26
+ # Find for instance the citation on arxiv or on the dataset repo/website
27
+ _CITATION = """\
28
+ @ARTICLE{10.3389/fmed.2021.816281,
29
+ AUTHOR={Pettersen, Henrik Sahlin and Belevich, Ilya and Røyset, Elin Synnøve and Smistad, Erik and Simpson, Melanie Rae and Jokitalo, Eija and Reinertsen, Ingerid and Bakke, Ingunn and Pedersen, André},
30
+ TITLE={Code-Free Development and Deployment of Deep Segmentation Models for Digital Pathology},
31
+ JOURNAL={Frontiers in Medicine},
32
+ VOLUME={8},
33
+ YEAR={2022},
34
+ URL={https://www.frontiersin.org/articles/10.3389/fmed.2021.816281},
35
+ DOI={10.3389/fmed.2021.816281},
36
+ ISSN={2296-858X},
37
+ ABSTRACT={Application of deep learning on histopathological whole slide images (WSIs) holds promise of improving diagnostic efficiency and reproducibility but is largely dependent on the ability to write computer code or purchase commercial solutions. We present a code-free pipeline utilizing free-to-use, open-source software (QuPath, DeepMIB, and FastPathology) for creating and deploying deep learning-based segmentation models for computational pathology. We demonstrate the pipeline on a use case of separating epithelium from stroma in colonic mucosa. A dataset of 251 annotated WSIs, comprising 140 hematoxylin-eosin (HE)-stained and 111 CD3 immunostained colon biopsy WSIs, were developed through active learning using the pipeline. On a hold-out test set of 36 HE and 21 CD3-stained WSIs a mean intersection over union score of 95.5 and 95.3% was achieved on epithelium segmentation. We demonstrate pathologist-level segmentation accuracy and clinical acceptable runtime performance and show that pathologists without programming experience can create near state-of-the-art segmentation solutions for histopathological WSIs using only free-to-use software. The study further demonstrates the strength of open-source solutions in its ability to create generalizable, open pipelines, of which trained models and predictions can seamlessly be exported in open formats and thereby used in external solutions. All scripts, trained models, a video tutorial, and the full dataset of 251 WSIs with ~31 k epithelium annotations are made openly available at <ext-link ext-link-type="uri" xlink:href="https://github.com/andreped/NoCodeSeg" xmlns:xlink="http://www.w3.org/1999/xlink">https://github.com/andreped/NoCodeSeg</ext-link> to accelerate research in the field.}
38
+ }
39
+ """
40
+
41
+ # TODO: Add description of the dataset here
42
+ # You can copy an official description
43
+ _DESCRIPTION = """\
44
+ IBDColEpi: 140 HE and 111 CD3-stained colon biopsies of active and inactivate inflammatory bowel disease with epithelium annotated.
45
+ """
46
+
47
+ # TODO: Add a link to an official homepage for the dataset here
48
+ _HOMEPAGE = "https://github.com/andreped/NoCodeSeg"
49
+
50
+ # TODO: Add the licence for the dataset here if you can find it
51
+ _LICENSE = "MIT"
52
+
53
+ # TODO: Add link to the official dataset URLs here
54
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
55
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
56
+ _URLS = {
57
+ "part1": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/QA7YDZ",
58
+ "part2": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/ICNVMQ",
59
+ "part3": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/PPG2QO",
60
+ "part4": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/AZNXDW",
61
+ "part5": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/5QEDTH",
62
+ "part6": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/APRUCP",
63
+ "part7": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/YWJXJ8",
64
+ "part8": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/G3KIOU",
65
+ "part9": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/SWALD9",
66
+ "part10": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/6ZJ4KD",
67
+ "wsi-annotations": "https://dataverse.no/api/access/datafile/:persistentId?persistentId=doi:10.18710/TLA01U/PMZJPQ",
68
+ }
69
+
70
+
71
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
72
+ class IBDColEpi(datasets.GeneratorBasedBuilder):
73
+ """140 HE and 111 CD3-stained colon biopsies of active and inactivate inflammatory bowel disease with epithelium annotated."""
74
+
75
+ VERSION = datasets.Version("1.0.0")
76
+
77
+ # This is an example of a dataset with multiple configurations.
78
+ # If you don't want/need to define several sub-sets in your dataset,
79
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
80
+
81
+ # If you need to make complex sub-parts in the datasets with configurable options
82
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
83
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
84
+
85
+ # You will be able to load one or the other configurations in the following list with
86
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
87
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
88
+ BUILDER_CONFIGS = [
89
+ datasets.BuilderConfig(name="part" + str(x), version=VERSION, description="This include part " + str(x) + "/10 of the WSIs of the dataset.") \
90
+ for x in range(1, 11):
91
+ ] + [
92
+ datasets.BuilderConfig(name="wsi-annotations", version=VERSION, description="This includes all 27 CTs stored as a single zip on Zenodo")
93
+ ]
94
+
95
+ DEFAULT_CONFIG_NAME = "part1" # It's not mandatory to have a default configuration. Just use one if it make sense.
96
+
97
+ def __init__(self, **kwargs):
98
+ super().__init__(**kwargs)
99
+ self.DATA_DIR = None
100
+
101
+ def _info(self):
102
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
103
+ if self.config.name.startswith("part"): # This is the name of the configuration selected in BUILDER_CONFIGS above
104
+ features = datasets.Features(
105
+ {
106
+ "wsi": datasets.Value("string"),
107
+ # These are the features of your dataset like images, labels ...
108
+ }
109
+ )
110
+ elif self.config.name == "wsi-annotations":
111
+ features = datasets.Features(
112
+ {
113
+ "wsi": datasets.Value("string"),
114
+ # These are the features of your dataset like images, labels ...
115
+ }
116
+ )
117
+ else:
118
+ # This is an example to show how to have different features for "first_domain" and "second_domain"
119
+ raise ValueError("An unsupported dataset object was provided. Please, choose either 'partX' for 'X' in range [1, 10] or 'wsi-annotations'.")
120
+
121
+ return datasets.DatasetInfo(
122
+ # This is the description that will appear on the datasets page.
123
+ description=_DESCRIPTION,
124
+ # This defines the different columns of the dataset and their types
125
+ features=features, # Here we define them above because they are different between the two configurations
126
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
127
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
128
+ # supervised_keys=("sentence", "label"),
129
+ # Homepage of the dataset for documentation
130
+ homepage=_HOMEPAGE,
131
+ # License for the dataset if available
132
+ license=_LICENSE,
133
+ # Citation for the dataset
134
+ citation=_CITATION,
135
+ )
136
+
137
+ def get_data_dir(self):
138
+ return self.DATA_DIR
139
+
140
+ def _split_generators(self, dl_manager):
141
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
142
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
143
+
144
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
145
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
146
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
147
+ urls = _URLS[self.config.name]
148
+ self.DATA_DIR = dl_manager.download_and_extract(urls)
149
+
150
+ # append AeroPath
151
+ self.DATA_DIR = os.path.join(self.DATA_DIR, "IBDColEpi")
152
+
153
+ print("data is downloaded to:", self.DATA_DIR)
154
+
155
+ generators = [
156
+ datasets.SplitGenerator(
157
+ name=datasets.Split.WSI,
158
+ # These kwargs will be passed to _generate_examples
159
+ gen_kwargs={
160
+ "dataset_name": "part" + str(x),
161
+ },
162
+ ),
163
+ for x in range(1, 11)
164
+ ] + [
165
+ datasets.SplitGenerator(
166
+ name=datasets.Split.ANNOTATIONS,
167
+ # These kwargs will be passed to _generate_examples
168
+ gen_kwargs={
169
+ "dataset_name": "wsi-annotations",
170
+ },
171
+ ),
172
+ ]
173
+
174
+ return generators
175
+
176
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
177
+ def _generate_examples(self, split):
178
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
179
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
180
+ for image_id in os.listdir(self.DATA_DIR):
181
+ curr_path = os.path.join(self.DATA_DIR, image_id)
182
+ yield image_id, {
183
+ "filename": os.path.join(curr_path, image_id),
184
+ }