Pipelines
This file establishes the dynamic pipelines used to produce training, test, and validation sets from the dataset.
A dynamic pipeline accepts processed data, and transforms it into: training data, test data, validation data with the specified labels. The pipelines account for the variable taxonomic levels and the encoding of the location feature, to produce the above transformations.
Note, the encoding of the location feature occurs within the pipeline processes. Please review the Silhouette score documentation for further information on the process.
Attributes:
Name | Type | Description |
---|---|---|
root_path |
str
|
The path to the project root. |
data_path |
str
|
The path to where the data is stored within the project |
save_path |
str
|
The path to where models and validation data (if created) is saved. To train the models used in ensemble use |
validation_set_flag |
bool
|
A boolean flag indicating whether a validation set should be created and saved. The validation set is saved to save_path. Each file will have suffixx |
aggregate_data(observation_file, meta_file)
This method aggregates the original observations with the collected metadata to form a single cohesive dataframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
observation_file |
str
|
The file name, that points to the file containing the processed iNaturalist observations |
required |
meta_file |
str
|
The file name, that points to the file containing the metadata for the processed iNaturalist observations |
required |
Source code in src/models/meta/pipelines.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
dark_light_calc(x)
This method performs the dark/ light feature creation based on the time of observation and the sunrise & sunset times
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
DataFrame row
|
This variable represents a dataframe row. |
required |
Returns:
Type | Description |
---|---|
DataFrame row
|
The method returns the dataframe row with a new binary 'light' column |
Source code in src/models/meta/pipelines.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
day_night_calculation(df)
This method provides the overall process to create the light/ dark feature.
This method converts the time of observation, sunrise, and sunset into local times. Local times are compared to determine light or dark. The sunrise and sunset columns are removed as they are no longer required.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the additional light column, and the sunrise & sunset columns removed. |
Source code in src/models/meta/pipelines.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
decision_tree_data(df, taxon_target, validation_file)
Method to create the train/set/validation data to be used by the decision tree/ random forest/ Adaboost models
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all data for each observation. |
required |
taxon_target |
str
|
The taxonomic target level, to extract the correct labels (taxon_family_name, taxon_genus_name, taxon_species_name, subspecies) |
required |
validation_file |
str
|
The name of the file where the validation data will be stored. Also informs the name of the saved models. |
required |
Returns:
Name | Type | Description |
---|---|---|
X |
DataFrame
|
The features in a form suitable for direct use within the models. |
y |
Series
|
The labels for the corresponding observations as the correct taxonomic level. |
Source code in src/models/meta/pipelines.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
elevation_clean(x)
This method performs a logical check on each observation's elevation based on the land feature value.
The Open-Meteo API sets elevation to be 0m if the elevation is unknown. If the elevation is 0m and the land value is 1 (indicating a terrestrial sighting), then the elevation is set to NaN value. This NaN value will be modified within the pipeline with the species average elevation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
DataFrame row
|
This variable represents a dataframe row containing the 'land' column. |
required |
Returns:
Type | Description |
---|---|
DataFrame row
|
The method returns the dataframe row with the 'elevation' feature adjusted. |
Source code in src/models/meta/pipelines.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
general_pipeline(df, k_means, taxon_target)
Method performs general pipeline functions for all model types (Neural network, XGBoost, AdaBoost, Decision tree, Random Forest)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
k_means |
KMeans
|
The trained K-means model that performs the location encoding |
required |
taxon_target |
str
|
The taxonomic level at which to extract the taxon labels (taxon_family_name, taxon_genus_name, taxon_species_name, sub_species) |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
A dataframe containing cleaned, transformed, and new data features for further specified processing depending on the model. |
Source code in src/models/meta/pipelines.py
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 |
|
land_mask(x)
This method determines if the observation coordinates are terrestrial or aquatic in nature.
This method uses the Globe library to evaluate the location against a land mask. If the observation is terrestrial a value of 1 is given. If not 0 is given.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
DataFrame row
|
This variable represents a dataframe row containing the 'latitude' and 'longitude' columns. |
required |
Returns:
Type | Description |
---|---|
DataFrame row
|
The method returns the dataframe row with an additional binary column value 'land' |
Source code in src/models/meta/pipelines.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
|
localize_sunrise_sunset(x)
This method localizes the sunrise and sunset times based on the time zone to aid in the light/ dark feature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
DataFrame row
|
This variable represents a dataframe row containing the 'sunrise' and 'sunset' columns. |
required |
Returns:
Type | Description |
---|---|
DataFrame row
|
The method returns the dataframe row with the 'sunrise' and 'sunset' features adjusted. |
Source code in src/models/meta/pipelines.py
415 416 417 418 419 420 421 422 423 424 425 426 |
|
neural_network_data(df, taxon_target, validation_file)
Method to create the train/set/validation data to be used by the neural network model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all data for each observation. |
required |
taxon_target |
str
|
The taxonomic target level, to extract the correct labels (taxon_family_name, taxon_genus_name, taxon_species_name, subspecies) |
required |
validation_file |
str
|
The name of the file where the validation data will be stored. Also informs the name of the saved models. |
required |
Returns:
Name | Type | Description |
---|---|---|
X |
DataFrame
|
The features in a form suitable for direct use within the models. |
y |
Series
|
The labels for the corresponding observations as the correct taxonomic level. |
classes |
int
|
The number of classes data labels |
Source code in src/models/meta/pipelines.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
nn_binary_label_handling(y)
Method handles the OHE of a binary case to ensure that OHE values returned are of the form [1, 0] or [0, 1].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y |
Series
|
The labels in the form of either 1 or 0 to be transformed into a binary OHE |
required |
Returns:
Type | Description |
---|---|
Series
|
Returns a Series containing OHE labels of the form [1, 0] or [0, 1] |
Source code in src/models/meta/pipelines.py
332 333 334 335 336 337 338 339 340 341 |
|
nn_pipeline(df, k_means, taxon_target, validation_file)
This method performs further data processing to structure and format it for use in the Neural Network model
This method performs similar processing steps to both the decision tree and XGBoost pipelines. However, categorical variables are required to be OHE and the resulting features are normalized for use in the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
k_means |
KMeans
|
The trained K-means model that performs the location encoding |
required |
taxon_target |
str
|
The taxonomic level at which to extract the taxon labels (taxon_family_name, taxon_genus_name, taxon_species_name, sub_species) |
required |
validation_file |
str
|
The name of file to store validation data. Informs model naming as well. |
required |
Returns:
Name | Type | Description |
---|---|---|
X |
DataFrame
|
A dataframe containing features in rows and observations in column ready for use as input features to the models for training and evaluation. These features are normalized. |
y |
Series
|
The OHE encoding of the observation labels at the correct taxonomic level specified. |
Source code in src/models/meta/pipelines.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
north_south_calc(month, seasons)
This method determined the current season when provided with a month and a list of seasonal months.
Note, this method is used within the season_calc()
method.
Args: month (int): The integer value of the month of sighting [1-12] seasons (list): The list of months seperated by season, starting with winter. Example of northern hemisphere [[12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
Returns:
Type | Description |
---|---|
str
|
The season categorical variable |
Source code in src/models/meta/pipelines.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
ohe_labels(y)
This method encodes the taxonomic labels in a One-hot-encoded format.
Special consideration is enforced for binary labels such that the resulting ohe labels are of the form [0, 1] or [1, 0]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y |
Series
|
The categorical taxonomic labels |
required |
Returns:
Type | Description |
---|---|
Series
|
OHE taxonomic labels |
Source code in src/models/meta/pipelines.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
ohe_month(df)
Method performs OHE on the month feature of each observation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
Source code in src/models/meta/pipelines.py
361 362 363 364 365 366 367 368 369 370 371 372 373 |
|
ohe_season(df)
This method OHE the season categorical feature
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the season feature OHE (this results in additional columns within the dataframe) |
Source code in src/models/meta/pipelines.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
|
over_sample(X, y)
This method performs oversampling on the dataset in order to provide a more balanced data distribution, to combat the tail-end distribution (characteristic of wildlife data).
Note, the oversampling aimed to increase the quantity of observations in minority classes to achieve a more even distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
DataFrame
|
The dataset's observation features to be used in model training and evaluation. |
required |
y |
Series
|
The label for each observation (still categorical) |
required |
Returns:
Name | Type | Description |
---|---|---|
X_res |
DataFrame
|
The features dataset with additional observations due to the oversampling |
y_res |
Series
|
An associated dataframe containing the observation labels, including for the additional observations created. |
Source code in src/models/meta/pipelines.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
season_calc(x)
This method determines the season in which an observation occurred based on the month of observation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
DataFrame row
|
This variable represents a dataframe row. |
required |
Returns:
Type | Description |
---|---|
DataFrame row
|
The method returns the dataframe row with a new season feature |
Source code in src/models/meta/pipelines.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
|
sub_species_detection(x)
Method uses the scientific name of observations to extract the subspecies name when there are more than three words present (3 names describe a subspecies)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
DataFrame row
|
This variable represents a dataframe row containing the 'scientific_name' column. |
required |
Returns:
Type | Description |
---|---|
DataFrame row
|
The method returns the dataframe row with an additional column value 'sub_species' if it could be extracted from the scientific name. |
Source code in src/models/meta/pipelines.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
tree_pipeline(df, k_means, taxon_target, validation_file)
This method performs further data processing to structure and format it for use in a decision tree, random forest and adaboost models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
k_means |
KMeans
|
The trained K-means model that performs the location encoding |
required |
taxon_target |
str
|
The taxonomic level at which to extract the taxon labels (taxon_family_name, taxon_genus_name, taxon_species_name, sub_species) |
required |
validation_file |
str
|
The name of file to store validation data. Informs model naming as well. |
required |
Returns:
Name | Type | Description |
---|---|---|
X |
DataFrame
|
A dataframe containing features in rows and observations in column ready for use as input features to the models for training and evaluation. |
y |
Series
|
The categorical labels of the associated observations at the correct taxonomic level specified. |
Source code in src/models/meta/pipelines.py
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 |
|
validation_set(df, taxon_target, file_name)
This method creates a validation set from the provided dataframe for further model evaluation
The validation set comprises 20% of each class's composition from the dataframe. The observations included in the validation set are removed from the dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
taxon_target |
str
|
The taxonomic level at which to extract the taxon labels (taxon_family_name, taxon_genus_name, taxon_species_name, sub_species) |
required |
file_name |
str
|
The name of the file in which the validation data will be stored. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the validation observations removed |
Source code in src/models/meta/pipelines.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
xgb_data(df, taxon_target, validation_file)
Method to create the train/set/validation data to be used by the XGBoost model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all data for each observation. |
required |
taxon_target |
str
|
The taxonomic target level, to extract the correct labels (taxon_family_name, taxon_genus_name, taxon_species_name, subspecies) |
required |
validation_file |
str
|
The name of the file where the validation data will be stored. Also informs the name of the saved models. |
required |
Returns:
Name | Type | Description |
---|---|---|
X |
DataFrame
|
The features in a form suitable for direct use within the models. |
y |
Series
|
The labels for the corresponding observations as the correct taxonomic level. |
Source code in src/models/meta/pipelines.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
xgb_pipeline(df, k_means, taxon_target, validation_file)
This method performs further data processing to structure and format it for use in the XGBoost model
This method makes use of the decison_tree_pipeline, simply encoding the labels in a One-Hot-Encoded (OHE) format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe containing all observation data from the processed data directory. |
required |
k_means |
KMeans
|
The trained K-means model that performs the location encoding |
required |
taxon_target |
str
|
The taxonomic level at which to extract the taxon labels (taxon_family_name, taxon_genus_name, taxon_species_name, sub_species) |
required |
validation_file |
str
|
The name of file to store validation data. Informs model naming as well. |
required |
Returns:
Name | Type | Description |
---|---|---|
X |
DataFrame
|
A dataframe containing features in rows and observations in column ready for use as input features to the models for training and evaluation. |
y |
Series
|
The OHE encoding of the observation labels at the correct taxonomic level specified. |
Source code in src/models/meta/pipelines.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|