File size: 10,413 Bytes
4b57888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
207
208
209
210
211
212
213
214
215
216
217
import pandas as pd
import datasets
import numpy as np
import ast
from PIL import Image

class NeuripsConfig(datasets.BuilderConfig):

    def __init__(self, **kwargs):
        super(NeuripsConfig, self).__init__(**kwargs)
        
class NeuripsDataset(datasets.GeneratorBasedBuilder):

    BUILDER_CONFIGS = [
        NeuripsConfig(
            name="Kenya",
            version=datasets.Version("1.0.0"),
            description="Full dataset, combining both systematically and opportunistically sampled leaves"
        ),
        NeuripsConfig(
            name="South_Africa",
            version=datasets.Version("1.0.0"),
            description="Subset containing only systematically sampled leaves"
        ),
        NeuripsConfig(
            name="USA_Summer",
            version=datasets.Version("1.0.0"),
            description="Subset containing only opportunistically sampled leaves"
        ),
        NeuripsConfig(
            name="USA_Winter",
            version=datasets.Version("1.0.0"),
            description="Subset containing only opportunistically sampled leaves"
        ),
        NeuripsConfig(
            name="Species_ID",
            version=datasets.Version("1.0.0"),
            description="Subset containing the DataFrames allowing to link the target encounter rates to a list of species for each country"
        )
    ]

    DEFAULT_CONFIG_NAME = "Kenya"

    def _info(self):
        #state,state_code,split,num_complete_checklists,target,geometry

        
        if self.config.name == "Kenya":
            features = datasets.Features({
                "hotspot_id": datasets.Value("string"),
                "hotspot_name": datasets.Value("string"),
                "lon": datasets.Value("float32"),
                "lat": datasets.Value("float32"),
                "county": datasets.Value("string"),
                "county_code": datasets.Value("string"),
                "state": datasets.Value("string"),
                "state_code": datasets.Value("string"),
                'sat_imagery_path': datasets.Value("string"),
                'environmental_path': datasets.Value("string"),
                "split": datasets.Value("string"),
                "num_complete_checklists" : datasets.Value("int32"),
                "target": datasets.Sequence(feature=datasets.Value("float32"), length=1054),
                "geometry": datasets.Value("string")

            })
        elif self.config.name == "South_Africa":
            features = datasets.Features({
                "hotspot_id": datasets.Value("string"),
                "hotspot_name": datasets.Value("string"),
                "lon": datasets.Value("float32"),
                "lat": datasets.Value("float32"),
                "county": datasets.Value("string"),
                "county_code": datasets.Value("string"),
                "state": datasets.Value("string"),
                "state_code": datasets.Value("string"),
                'sat_imagery_path': datasets.Value("string"),
                'environmental_path': datasets.Value("string"),
                "split": datasets.Value("string"),
                "num_complete_checklists" : datasets.Value("int32"),
                "target": datasets.Sequence(feature=datasets.Value("float32"), length=1054),
                "geometry": datasets.Value("string")
            })
        elif self.config.name == "USA_Summer":
            features = datasets.Features({
                "hotspot_id": datasets.Value("string"),
                "hotspot_name": datasets.Value("string"),
                "lon": datasets.Value("float32"),
                "lat": datasets.Value("float32"),
                "county": datasets.Value("string"),
                "county_code": datasets.Value("string"),
                "state": datasets.Value("string"),
                "state_code": datasets.Value("string"),
                'sat_imagery_path': datasets.Value("string"),
                'environmental_path': datasets.Value("string"),
                "split": datasets.Value("string"),
                "num_complete_checklists" : datasets.Value("int32"),
                "target": datasets.Sequence(feature=datasets.Value("float32"), length=1054),
                "geometry": datasets.Value("string")
            })
        elif self.config.name == "USA_Winter":
            features = datasets.Features({
                "hotspot_id": datasets.Value("string"),
                "hotspot_name": datasets.Value("string"),
                "lon": datasets.Value("float32"),
                "lat": datasets.Value("float32"),
                "county": datasets.Value("string"),
                "county_code": datasets.Value("string"),
                "state": datasets.Value("string"),
                "state_code": datasets.Value("string"),
                'sat_imagery_path': datasets.Value("string"),
                'environmental_path': datasets.Value("string"),
                "split": datasets.Value("string"),
                "num_complete_checklists" : datasets.Value("int32"),
                "target": datasets.Sequence(feature=datasets.Value("float32"), length=1054),
                "geometry": datasets.Value("string")
            })
        elif self.config.name == "Species_ID":
            features = datasets.Features({
                "scientific_name": datasets.Value("string"),
                "ebird_code": datasets.Value("string"),
                "inat_preview": datasets.Image(),
                "target_value_index" : datasets.Value("int32"),
            })
        else:
            raise ValueError(f"Unsupported config: {self.config.name}")
        
        return datasets.DatasetInfo(
            description="The SITTELLE Benchmark Dataset",
            features=features,
            supervised_keys=None,
            homepage="https://huggingface.co/datasets/imageomics/invasive_plants_hawaii",
            license="MIT",
        )
    
    def _split_generators(self, dl_manager):
        if self.config.name == "Kenya":
            train_csv = "Kenya/train_split.csv"
            test_csv = "Kenya/test_split.csv" 
            val_csv = "Kenya/valid_split.csv"
            return [
                datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}),
                datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}),
                datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}),
            ]
        elif self.config.name == "South_Africa":
            train_csv = "Kenya/train_split.csv"
            test_csv = "Kenya/test_split.csv" 
            val_csv = "Kenya/valid_split.csv"
            return [
                datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}),
                datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}),
                datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}),
            ]
        elif self.config.name == "USA_Summer":
            train_csv = "Kenya/train_split.csv"
            test_csv = "Kenya/test_split.csv" 
            val_csv = "Kenya/valid_split.csv"
            return [
                datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}),
                datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}),
                datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}),
            ]
        elif self.config.name == "USA_Winter":
            train_csv = "Kenya/train_split.csv"
            test_csv = "Kenya/test_split.csv" 
            val_csv = "Kenya/valid_split.csv"
            return [
                datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}),
                datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}),
                datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}),
            ]
        elif self.config.name == "Species_ID":
            kenya_csv = "Kenya/species_id_kenya.csv"
            southafrica_csv = "Kenya/species_id_kenya.csv"
            usasummer_csv = "Kenya/species_id_kenya.csv"
            usawinter_csv = "Kenya/species_id_kenya.csv"
            return [
                datasets.SplitGenerator(name="Kenya", gen_kwargs={"filepath": kenya_csv}),
                datasets.SplitGenerator(name="South_Africa", gen_kwargs={"filepath": southafrica_csv}),
                datasets.SplitGenerator(name="USA_Summer", gen_kwargs={"filepath": usasummer_csv}),
                datasets.SplitGenerator(name="USA_Winter", gen_kwargs={"filepath": usawinter_csv}),                             
            ]
        else:
            raise ValueError(f"Unknown config: {self.config.name}")
    
    def _generate_examples(self, filepath):
        if self.config.name in ["Kenya", "South_Africa", "USA_Summer", "USA_Winter"]:
            df_metadata = pd.read_csv(filepath)
            df_metadata["target"] = df_metadata["target"].apply(ast.literal_eval).apply(lambda x: list(map(float, x)))
            for idx in range(len(df_metadata)):
                row = df_metadata.iloc[idx]
                yield idx, {
                    "hotspot_id": row['hotspot_id'],
                    "hotspot_name": row['hotspot_name'],
                    "lon": row['lon'],
                    "lat": row['lat'],
                    "county": row['county'],
                    "county_code": row['county_code'],
                    "state": row['state'],
                    "state_code": row['state_code'],
                    'sat_imagery_path': row['sat_imagery_path'],
                    'environmental_path': row['environmental_path'],
                    "split": row['split'],
                    "num_complete_checklists" : row['num_complete_checklists'],
                    "target": row['target'],
                    "geometry": row['geometry']
                }
        if self.config.name == "Species_ID":
            df_metadata = pd.read_csv(filepath)
            for idx in range(len(df_metadata)):
                row = df_metadata.iloc[idx]
                yield idx, {
                        "scientific_name": row['scientific_name'],
                        "ebird_code": row['ebird_code'],
                        "inat_preview": row['inat_preview'],
                        "target_value_index" : row['target_value_index'],
                    }