-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbase.py
More file actions
1388 lines (1161 loc) · 52.4 KB
/
base.py
File metadata and controls
1388 lines (1161 loc) · 52.4 KB
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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import random
from abc import ABC, abstractmethod
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Tuple, Union
import lightning as pl
import numpy as np
import pandas as pd
import torch
import tqdm
from lightning.pytorch.core.datamodule import LightningDataModule
from lightning_utilities.core.rank_zero import rank_zero_info
from torch.utils.data import DataLoader
from chebai.preprocessing import reader as dr
if TYPE_CHECKING:
import networkx as nx
class XYBaseDataModule(LightningDataModule):
"""
Base class for data modules.
This class provides a base implementation for loading and preprocessing datasets.
It inherits from `LightningDataModule` and defines common properties and methods for data loading and processing.
Args:
batch_size (int): The batch size for data loading. Default is 1.
test_split (float): The ratio of test data to total data. Default is 0.1.
validation_split (float): The ratio of validation data to total data. Default is 0.05.
reader_kwargs (dict): Additional keyword arguments to be passed to the data reader. Default is None.
prediction_kind (str): The kind of prediction to be performed (only relevant for the predict_dataloader). Default is "test".
data_limit (Optional[int]): The maximum number of data samples to load. If set to None, the complete dataset will be used. Default is None.
label_filter (Optional[int]): The index of the label to filter. Default is None.
balance_after_filter (Optional[float]): The ratio of negative samples to positive samples after filtering. Default is None.
num_workers (int): The number of worker processes for data loading. Default is 1.
chebi_version (int): The version of ChEBI to use. Default is 200.
inner_k_folds (int): The number of folds for inner cross-validation. Use -1 to disable inner cross-validation. Default is -1.
fold_index (Optional[int]): The index of the fold to use for training and validation. Default is None.
base_dir (Optional[str]): The base directory for storing processed and raw data. Default is None.
**kwargs: Additional keyword arguments.
Attributes:
READER (DataReader): The data reader class to use.
reader (DataReader): An instance of the data reader class.
test_split (float): The ratio of test data to total data.
validation_split (float): The ratio of validation data to total data.
batch_size (int): The batch size for data loading.
prediction_kind (str): The kind of prediction to be performed.
data_limit (Optional[int]): The maximum number of data samples to load.
label_filter (Optional[int]): The index of the label to filter.
balance_after_filter (Optional[float]): The ratio of negative samples to positive samples after filtering.
num_workers (int): The number of worker processes for data loading.
chebi_version (int): The version of ChEBI to use.
inner_k_folds (int): The number of folds for inner cross-validation. If it is less than to, no cross-validation will be performed.
fold_index (Optional[int]): The index of the fold to use for training and validation (only relevant for cross-validation).
_base_dir (Optional[str]): The base directory for storing processed and raw data.
raw_dir (str): The directory for storing raw data.
processed_dir (str): The directory for storing processed data.
fold_dir (str): The name of the directory where the folds from inner cross-validation are stored.
_name (str): The name of the data module.
"""
READER = dr.DataReader
def __init__(
self,
batch_size: int = 1,
test_split: Optional[float] = 0.1,
validation_split: Optional[float] = 0.05,
reader_kwargs: Optional[dict] = None,
prediction_kind: str = "test",
data_limit: Optional[int] = None,
label_filter: Optional[int] = None,
balance_after_filter: Optional[float] = None,
num_workers: int = 1,
persistent_workers: bool = True,
chebi_version: int = 200,
inner_k_folds: int = -1, # use inner cross-validation if > 1
fold_index: Optional[int] = None,
base_dir: Optional[str] = None,
n_token_limit: Optional[int] = None,
**kwargs,
):
super().__init__()
if reader_kwargs is None:
reader_kwargs = dict()
self.reader = self.READER(**reader_kwargs)
self.test_split = test_split
self.validation_split = validation_split
self.batch_size = batch_size
self.prediction_kind = prediction_kind
self.data_limit = data_limit
self.label_filter = label_filter
assert (balance_after_filter is not None) or (
self.label_filter is None
), "Filter balancing requires a filter"
self.balance_after_filter = balance_after_filter
self.num_workers = num_workers
self.persistent_workers: bool = bool(persistent_workers)
self.chebi_version = chebi_version
assert type(inner_k_folds) is int
self.inner_k_folds = inner_k_folds
self.use_inner_cross_validation = (
inner_k_folds > 1
) # only use cv if there are at least 2 folds
assert (
fold_index is None or self.use_inner_cross_validation is not None
), "fold_index can only be set if cross validation is used"
if fold_index is not None and self.inner_k_folds is not None:
assert (
fold_index < self.inner_k_folds
), "fold_index can't be larger than the total number of folds"
self.fold_index = fold_index
self._base_dir = base_dir
self.n_token_limit = n_token_limit
os.makedirs(self.raw_dir, exist_ok=True)
os.makedirs(self.processed_dir, exist_ok=True)
if self.use_inner_cross_validation:
os.makedirs(os.path.join(self.raw_dir, self.fold_dir), exist_ok=True)
os.makedirs(os.path.join(self.processed_dir, self.fold_dir), exist_ok=True)
self.save_hyperparameters()
self._num_of_labels = None
self._feature_vector_size = None
self._prepare_data_flag = 1
self._setup_data_flag = 1
@property
def num_of_labels(self):
assert self._num_of_labels is not None, "num of labels must be set"
return self._num_of_labels
@property
def feature_vector_size(self):
assert (
self._feature_vector_size is not None
), "size of feature vector must be set"
return self._feature_vector_size
@property
def identifier(self) -> tuple:
"""Identifier for the dataset."""
return (self.reader.name(),)
@property
def full_identifier(self) -> tuple:
"""Full identifier for the dataset."""
return (self._name, *self.identifier)
@property
def base_dir(self) -> str:
"""Common base directory for processed and raw directories."""
if self._base_dir is not None:
return self._base_dir
return os.path.join("data", self._name)
@property
def processed_dir_main(self) -> str:
"""Name of the directory where processed (but not tokenized) data is stored."""
return os.path.join(self.base_dir, "processed")
@property
def processed_dir(self) -> str:
"""Name of the directory where the processed and tokenized data is stored."""
return os.path.join(self.processed_dir_main, *self.identifier)
@property
def raw_dir(self) -> str:
"""Name of the directory where the raw data is stored."""
return os.path.join(self.base_dir, "raw")
@property
def fold_dir(self) -> str:
"""Name of the directory where the folds from inner cross-validation (i.e., the train and val sets) are stored."""
return f"cv_{self.inner_k_folds}_fold"
@property
@abstractmethod
def _name(self) -> str:
"""
Abstract property representing the name of the data module.
This property should be implemented in subclasses to provide a unique name for the data module.
The name is used to create subdirectories within the base directory or `processed_dir_main`
for storing relevant data associated with this module.
Returns:
str: The name of the data module.
"""
pass
def _filter_labels(self, row: dict) -> dict:
"""
Filter labels based on `label_filter`.
This method selects specific labels from the `labels` list within the row dictionary
according to the index or indices provided by the `label_filter` attribute of the class.
Args:
row (dict): A dictionary containing the row data.
Returns:
dict: The filtered row data.
"""
row["labels"] = [row["labels"][self.label_filter]]
return row
def load_processed_data(
self, kind: Optional[str] = None, filename: Optional[str] = None
) -> List:
"""
Load processed data from a file. Either the kind or the filename has to be provided. If both are provided, the
filename is used.
Args:
kind (str, optional): The kind of dataset to load such as "train", "val" or "test". Defaults to None.
filename (str, optional): The name of the file to load the dataset from. Defaults to None.
Returns:
List: The loaded processed data.
Raises:
ValueError: If both kind and filename are None.
"""
if kind is None and filename is None:
raise ValueError(
"Either kind or filename is required to load the correct dataset, both are None"
)
# if both kind and filename are given, use filename
if kind is not None and filename is None:
try:
# processed_file_names_dict is only implemented for _ChEBIDataExtractor
if self.use_inner_cross_validation and kind != "test":
filename = self.processed_file_names_dict[
f"fold_{self.fold_index}_{kind}"
]
else:
filename = self.processed_file_names_dict[kind]
except NotImplementedError:
filename = f"{kind}.pt"
return torch.load(
os.path.join(self.processed_dir, filename), weights_only=False
)
def dataloader(self, kind: str, **kwargs) -> DataLoader:
"""
Returns a DataLoader object for the specified kind (train, val or test) of data.
Args:
kind (str): The kind indicates whether it is a train, val or test data to load.
**kwargs: Additional keyword arguments.
Returns:
DataLoader: A DataLoader object.
"""
rank_zero_info(
f"Loading {kind} data... (datamodule.current_epoch={self.curr_epoch if hasattr(self, 'curr_epoch') else 'N/A'})"
)
dataset = self.load_processed_data(kind)
if "ids" in kwargs:
ids = kwargs.pop("ids")
_dataset = []
for i in range(len(dataset)):
if i in ids:
_dataset.append(dataset[i])
dataset = _dataset
if self.label_filter is not None:
original_len = len(dataset)
dataset = [self._filter_labels(r) for r in dataset]
positives = [r for r in dataset if r["labels"][0]]
negatives = [r for r in dataset if not r["labels"][0]]
if self.balance_after_filter is not None:
negative_length = min(
original_len, int(len(positives) * self.balance_after_filter)
)
dataset = positives + negatives[:negative_length]
else:
dataset = positives + negatives
random.shuffle(dataset)
if self.data_limit is not None:
dataset = dataset[: self.data_limit]
return DataLoader(
dataset,
collate_fn=self.reader.collator,
batch_size=self.batch_size,
**kwargs,
)
@staticmethod
def _load_dict(
input_file_path: str,
) -> Generator[Dict[str, Any], None, None]:
"""
Load data from a file and return a dictionary.
Args:
input_file_path (str): The path to the input file.
Yields:
dict: A dictionary containing the features and labels.
"""
with open(input_file_path, "r") as input_file:
for row in input_file:
smiles, labels = row.split("\t")
yield dict(features=smiles, labels=labels)
@staticmethod
def _get_data_size(input_file_path: str) -> int:
"""
Get the number of lines in a file.
Args:
input_file_path (str): The path to the input file.
Returns:
int: The number of lines in the file.
"""
with open(input_file_path, "r") as f:
return sum(1 for _ in f)
def _load_data_from_file(self, path: str) -> List[Dict[str, Any]]:
"""
Load data from a file and return a list of dictionaries.
Args:
path (str): The path to the input file.
Returns:
List: A list of dictionaries containing the features and labels.
"""
lines = self._get_data_size(path)
print(f"Processing {lines} lines...")
data = [
self.reader.to_data(d)
for d in tqdm.tqdm(self._load_dict(path), total=lines)
if d["features"] is not None
]
data = [val for val in data if self._filter_to_token_limit(val)]
return data
def _filter_to_token_limit(self, data_instance: dict) -> bool:
# filter for missing features in resulting data, keep features length below token limit
if data_instance["features"] is not None and (
self.n_token_limit is None
or len(data_instance["features"]) <= self.n_token_limit
):
return True
return False
def train_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
"""
Returns the train DataLoader.
Args:
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.
Returns:
DataLoader: A DataLoader object for training data.
"""
return self.dataloader(
"train",
shuffle=True,
num_workers=self.num_workers,
persistent_workers=self.persistent_workers,
**kwargs,
)
def val_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
"""
Returns the validation DataLoader.
Args:
*args: Additional positional arguments (unused).
**kwargs: Additional keyword arguments, passed to dataloader().
Returns:
Union[DataLoader, List[DataLoader]]: A DataLoader object for validation data.
"""
return self.dataloader(
"validation",
shuffle=False,
num_workers=self.num_workers,
persistent_workers=self.persistent_workers,
**kwargs,
)
def test_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
"""
Returns the test DataLoader.
Args:
*args: Additional positional arguments (unused).
**kwargs: Additional keyword arguments, passed to dataloader().
Returns:
Union[DataLoader, List[DataLoader]]: A DataLoader object for test data.
"""
return self.dataloader("test", shuffle=False, **kwargs)
def predict_dataloader(
self,
smiles_list: List[str],
model_hparams: Optional[dict] = None,
**kwargs,
) -> tuple[DataLoader, list[int]]:
"""
Returns the predict DataLoader.
Args:
smiles_list (List[str]): List of SMILES strings to predict.
model_hparams (Optional[dict]): Model hyperparameters.
Some prediction pre-processing pipelines may require these.
**kwargs: Additional keyword arguments, passed to dataloader().
Returns:
tuple[DataLoader, list[int]]: A DataLoader object for prediction data and a list of valid indices.
"""
data, valid_indices = self._process_input_for_prediction(
smiles_list, model_hparams
)
return (
DataLoader(
data,
collate_fn=self.reader.collator,
batch_size=self.batch_size,
**kwargs,
),
valid_indices,
)
def _process_input_for_prediction(
self, smiles_list: list[str], model_hparams: Optional[dict] = None
) -> tuple[list, list]:
"""
Process input data for prediction.
Args:
smiles_list (List[str]): List of SMILES strings.
model_hparams (Optional[dict]): Model hyperparameters.
Some prediction pre-processing pipelines may require these.
Returns:
tuple[list, list]: Processed input data and valid indices.
"""
data, valid_indices = [], []
for idx, smiles in enumerate(smiles_list):
result = self._preprocess_smiles_for_pred(idx, smiles, model_hparams)
if result is None or result["features"] is None:
continue
if not self._filter_to_token_limit(result):
continue
data.append(result)
valid_indices.append(idx)
return data, valid_indices
def _preprocess_smiles_for_pred(
self, idx, smiles: str, model_hparams: Optional[dict] = None
) -> dict:
"""Preprocess prediction data."""
# Add dummy labels because the collate function requires them.
# Note: If labels are set to `None`, the collator will insert a `non_null_labels` entry into `loss_kwargs`,
# which later causes `_get_prediction_and_labels` method in the prediction pipeline to treat the data as empty.
return self.reader.to_data(
{"id": f"smiles_{idx}", "features": smiles, "labels": [1, 2]}
)
def prepare_data(self, *args, **kwargs) -> None:
if self._prepare_data_flag != 1:
return
self._prepare_data_flag += 1
self._perform_data_preparation(*args, **kwargs)
self._after_prepare_data(*args, **kwargs)
def _perform_data_preparation(self, *args, **kwargs) -> None:
raise NotImplementedError
def _after_prepare_data(self, *args, **kwargs) -> None:
"""
Hook to perform additional pre-processing after pre-processed data is available.
"""
...
def setup(self, *args, **kwargs) -> None:
"""
Setup the data module.
This method checks for the processed data and sets up the data module for training, validation, and testing.
Args:
**kwargs: Additional keyword arguments.
"""
if self._setup_data_flag != 1:
return
self._setup_data_flag += 1
rank_zero_info(f"Check for processed data in {self.processed_dir}")
rank_zero_info(f"Cross-validation enabled: {self.use_inner_cross_validation}")
rank_zero_info(f"Looking for files: {self.processed_file_names}")
if any(
not os.path.isfile(os.path.join(self.processed_dir, f))
for f in self.processed_file_names
):
self.setup_processed()
self._after_setup(**kwargs)
def _after_setup(self, **kwargs):
"""
Finalize the setup process after ensuring the processed data is available.
This method performs post-setup tasks like finalizing the reader and setting internal properties.
"""
self.reader.on_finish()
self._set_processed_data_props()
def _set_processed_data_props(self):
"""
Load processed data and extract metadata.
Sets:
- self._num_of_labels: Number of target labels in the dataset.
- self._feature_vector_size: Maximum feature vector length across all data points.
"""
pt_file_path = os.path.join(
self.processed_dir, self.processed_file_names_dict["data"]
)
data_pt = torch.load(pt_file_path, weights_only=False)
self._num_of_labels = len(data_pt[0]["labels"])
self._feature_vector_size = max(len(d["features"]) for d in data_pt)
print(
f"Number of samples in encoded data ({pt_file_path}): {len(data_pt)} samples"
)
print(f"Number of labels for loaded data: {self._num_of_labels}")
print(f"Feature vector size: {self._feature_vector_size}")
def setup_processed(self):
"""
Setup the processed data.
This method should be implemented by subclasses to handle the specific setup of processed data.
"""
raise NotImplementedError
@property
def processed_main_file_names_dict(self) -> dict:
"""
Returns a dictionary mapping processed data file names.
Returns:
dict: A dictionary mapping dataset key to their respective file names.
For example, {"data": "data.pkl"}.
"""
raise NotImplementedError
@property
def processed_main_file_names(self) -> List[str]:
"""
Returns a list of file names for processed data (before tokenization).
Returns:
List[str]: A list of file names corresponding to the processed data.
"""
return list(self.processed_main_file_names_dict.values())
@property
def processed_file_names_dict(self) -> dict:
"""
Returns a dictionary for the processed and tokenized data files.
Returns:
dict: A dictionary mapping dataset keys to their respective file names.
For example, {"data": "data.pt"}.
"""
raise NotImplementedError
@property
def processed_file_names(self) -> List[str]:
"""
Returns a list of file names for processed data.
Returns:
List[str]: A list of file names corresponding to the processed data.
"""
return list(self.processed_file_names_dict.values())
@property
def raw_file_names(self) -> List[str]:
"""
Returns the list of raw file names.
Returns:
List[str]: The list of raw file names.
"""
return list(self.raw_file_names_dict.values())
@property
def raw_file_names_dict(self) -> dict:
"""
Returns the dictionary of raw file names (i.e., files that are directly obtained from an external source).
This property should be implemented by subclasses to provide the dictionary of raw file names.
Returns:
dict: The dictionary of raw file names.
"""
raise NotImplementedError
@property
def classes_txt_file_path(self) -> str:
"""
Returns the filename for the classes text file.
Returns:
str: The filename for the classes text file.
"""
# This property also used in following places:
# - results/prediction.py: to load class names for csv columns names
# - chebai/cli.py: to link this property to `model.init_args.classes_txt_file_path`
return os.path.join(self.processed_dir_main, "classes.txt")
class MergedDataset(XYBaseDataModule):
MERGED = []
@property
def _name(self) -> str:
"""
Returns a concatenated name of all subset names.
"""
return "+".join(s._name for s in self.subsets)
def __init__(
self,
batch_size: int = 1,
train_split: float = 0.85,
reader_kwargs: Union[None, List[dict]] = None,
**kwargs,
):
"""
Args:
batch_size (int): Batch size for data loaders.
train_split (float): Fraction of data to use for training.
reader_kwargs (Union[None, List[dict]]): Optional arguments for subset readers.
**kwargs: Additional arguments to pass to LightningDataModule.
"""
if reader_kwargs is None:
reader_kwargs = [None for _ in self.MERGED]
self.train_split = train_split
self.batch_size = batch_size
self.subsets = [
s(train_split=train_split, reader_kwargs=kws)
for s, kws in zip(self.MERGED, reader_kwargs)
]
self.reader = self.subsets[0].reader
os.makedirs(self.processed_dir, exist_ok=True)
super(pl.LightningDataModule, self).__init__(**kwargs)
def _perform_data_preparation(self):
"""
Placeholder for data preparation logic.
"""
for s in self.subsets:
s.prepare_data()
def setup(self, **kwargs):
"""
Setup the data module.
This method checks for the processed data and sets up the data module for training, validation, and testing.
Args:
**kwargs: Additional keyword arguments.
"""
if self._setup_data_flag != 1:
return
self._setup_data_flag += 1
for s in self.subsets:
s.setup(**kwargs)
self._set_processed_data_props()
def dataloader(self, kind: str, **kwargs) -> DataLoader:
"""
Creates a DataLoader for a specific subset.
Args:
kind (str): Kind of data loader ('train', 'validation', or 'test').
**kwargs: Additional arguments passed to DataLoader.
Returns:
DataLoader: DataLoader object for the specified subset.
"""
subdatasets = [
torch.load(os.path.join(s.processed_dir, f"{kind}.pt"), weights_only=False)
for s in self.subsets
]
dataset = [
self._process_data(i, d)
for i, (s, lim) in enumerate(zip(subdatasets, self.limits))
for d in (s if lim is None else s[:lim])
]
return DataLoader(
dataset,
collate_fn=self.reader.collator,
batch_size=self.batch_size,
**kwargs,
)
def train_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
"""
Returns the training DataLoader.
"""
return self.dataloader("train", shuffle=True, **kwargs)
def val_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
"""
Returns the validation DataLoader.
"""
return self.dataloader("validation", shuffle=False, **kwargs)
def test_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
"""
Returns the test DataLoader.
"""
return self.dataloader("test", shuffle=False, **kwargs)
def _process_data(self, subset_id: int, data: dict) -> dict:
"""
Processes data from a subset.
Args:
subset_id (int): Index of the subset.
data (dict): Data from the subset.
Returns:
dict: Processed data with 'features', 'labels', and 'ident' keys.
"""
return dict(
features=data["features"], labels=data["labels"], ident=data["ident"]
)
def setup_processed(self):
"""
Placeholder for setup logic after data processing.
"""
pass
@property
def processed_file_names(self) -> List[str]:
"""
Returns the list of processed file names.
"""
return ["test.pt", "train.pt", "validation.pt"]
@property
def limits(self):
"""
Returns None, assuming no limits on data slicing.
"""
return None
class _DynamicDataset(XYBaseDataModule, ABC):
"""
A class for extracting and processing data from the given dataset.
The processed and transformed data is stored in `data.pkl` and `data.pt` format as a whole respectively,
rather than as separate train, validation, and test splits, with dynamic splitting of data.pt occurring at runtime.
The `_DynamicDataset` class manages data splits by either generating them during execution or retrieving them from
a CSV file.
If no split file path is provided, `_generate_dynamic_splits` creates the training, validation, and test splits
from the encoded/transformed data, storing them in `_dynamic_df_train`, `_dynamic_df_val`, and `_dynamic_df_test`.
When a split file path is provided, `_retrieve_splits_from_csv` loads splits from the CSV file, which must
include 'id' and 'split' columns.
The `dynamic_split_dfs` property ensures that the necessary splits are loaded as required.
Args:
dynamic_data_split_seed (int, optional): The seed for random data splitting. Defaults to 42.
splits_file_path (str, optional): Path to the splits CSV file. Defaults to None.
apply_label_filter (Optional[str]): Path to a classes.txt file - only labels that are in the labels filter
file will be used (in that order). All labels in the label filter have to be present in the dataset. This filter
is only active when loading splits from a CSV file. Defaults to None.
apply_id_filter (Optional[str]): Path to a data.pt file from a different dataset - only IDs that are in the
id filter file will be used. Defaults to None. This filter is only active when loading splits from a CSV file.
**kwargs: Additional keyword arguments passed to XYBaseDataModule.
Attributes:
dynamic_data_split_seed (int): The seed for random data splitting, default is 42.
splits_file_path (Optional[str]): Path to the CSV file containing split assignments.
apply_label_filter (Optional[str]): Path to a classes.txt file for label filtering.
apply_id_filter (Optional[str]): Path to a data.pt file for ID filtering.
"""
# ---- Index for columns of processed `data.pkl` (should be derived from `_graph_to_raw_dataset` method) ------
_ID_IDX: int = None
_DATA_REPRESENTATION_IDX: int = None
_LABELS_START_IDX: int = None
def __init__(
self,
apply_label_filter: Optional[str] = None,
apply_id_filter: Optional[str] = None,
**kwargs,
):
super(_DynamicDataset, self).__init__(**kwargs)
self.dynamic_data_split_seed = int(kwargs.get("seed", 42)) # default is 42
# Class variables to store the dynamics splits
self._dynamic_df_train = None
self._dynamic_df_test = None
self._dynamic_df_val = None
# Path of csv file which contains a list of ids & their assignment to a dataset (either train,
# validation or test).
self.splits_file_path = self._validate_splits_file_path(
kwargs.get("splits_file_path", None)
)
self.apply_label_filter = apply_label_filter
self.apply_id_filter = apply_id_filter
self._data_pkl_filename: str = "data.pkl"
@staticmethod
def _validate_splits_file_path(splits_file_path: Optional[str]) -> Optional[str]:
"""
Validates the file in provided splits file path.
Args:
splits_file_path (Optional[str]): Path to the splits CSV file.
Returns:
Optional[str]: Validated splits file path if checks pass, None if splits_file_path is None.
Raises:
FileNotFoundError: If the splits file does not exist.
ValueError: If splits file is empty or missing required columns ('id' and/or 'split'), or not a CSV file.
"""
if splits_file_path is None:
return None
if not os.path.isfile(splits_file_path):
raise FileNotFoundError(f"File {splits_file_path} does not exist")
file_size = os.path.getsize(splits_file_path)
if file_size == 0:
raise ValueError(f"File {splits_file_path} is empty")
# Check if the file has a CSV extension
if not splits_file_path.lower().endswith(".csv"):
raise ValueError(f"File {splits_file_path} is not a CSV file")
# Read the first row of CSV file into a DataFrame
splits_df = pd.read_csv(splits_file_path, nrows=1)
# Check if 'id' and 'split' columns are in the DataFrame
required_columns = {"id", "split"}
if not required_columns.issubset(splits_df.columns):
raise ValueError(
f"CSV file {splits_file_path} is missing required columns ('id' and/or 'split')."
)
return splits_file_path
# ------------------------------ Phase: Prepare data -----------------------------------
def _perform_data_preparation(self, *args: Any, **kwargs: Any) -> None:
"""
Prepares the data for the dataset.
This method checks for the presence of raw data in the specified directory.
If the raw data is missing, it fetches the ontology and creates a dataframe and saves it to a data.pkl file.
The resulting dataframe/pickle file is expected to contain columns with the following structure:
- Column at index `self._ID_IDX`: ID of data instance
- Column at index `self._DATA_REPRESENTATION_IDX`: Sequence representation of the protein
- Column from index `self._LABELS_START_IDX` onwards: Labels
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
None
"""
print("Checking for processed data in", self.processed_dir_main)
processed_name = self.processed_main_file_names_dict["data"]
if not os.path.isfile(os.path.join(self.processed_dir_main, processed_name)):
print(f"Missing processed data file (`{processed_name}` file)")
os.makedirs(self.processed_dir_main, exist_ok=True)
data_path = self._download_required_data()
g = self._extract_class_hierarchy(data_path)
data_df = self._graph_to_raw_dataset(g)
self.save_processed(data_df, processed_name)
@abstractmethod
def _download_required_data(self) -> str:
"""
Downloads the required raw data.
Returns:
str: Path to the downloaded data.
"""
pass
@abstractmethod
def _extract_class_hierarchy(self, data_path: str) -> "nx.DiGraph":
"""
Extracts the class hierarchy from the data.
Constructs a directed graph (DiGraph) using NetworkX, where nodes are annotated with fields/terms from
the term documents.
Args:
data_path (str): Path to the data.
Returns:
nx.DiGraph: The class hierarchy graph.
"""
pass
@abstractmethod
def _graph_to_raw_dataset(self, graph: "nx.DiGraph") -> pd.DataFrame:
"""
Converts the graph to a raw dataset.
Uses the graph created by `_extract_class_hierarchy` method to extract the
raw data in Dataframe format with additional columns corresponding to each multi-label class.
Args:
graph (nx.DiGraph): The class hierarchy graph.
Returns:
pd.DataFrame: The raw dataset.
"""
pass
@abstractmethod
def select_classes(self, g: "nx.DiGraph", *args, **kwargs) -> List:
"""
Selects classes from the dataset based on a specified criteria.
Args:
g (nx.Graph): The graph representing the dataset.
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.
Returns:
List: A sorted list of node IDs that meet the specified criteria.
"""
pass
def save_processed(self, data: pd.DataFrame, filename: str) -> None:
"""
Save the processed dataset to a pickle file.
Args:
data (pd.DataFrame): The processed dataset to be saved.
filename (str): The filename for the pickle file.
"""
pd.to_pickle(data, open(os.path.join(self.processed_dir_main, filename), "wb"))
def get_processed_pickled_df_file(self, filename: str) -> Optional[pd.DataFrame]:
"""
Gets the processed dataset pickle file.
Args:
filename (str): The filename for the pickle file.
Returns:
pd.DataFrame: The processed dataset as a DataFrame.
"""
file_path = Path(self.processed_dir_main) / filename
if file_path.exists():
return pd.read_pickle(file_path)
return None
# ------------------------------ Phase: Setup data -----------------------------------
def setup_processed(self) -> None:
"""
Transforms `data.pkl` into a model input data format (`data.pt`), ensuring that the data is in a format
compatible for input to the model.
The transformed data contains the following keys: `ident`, `features`, `labels`, and `group`.
This method uses a subclass of Data Reader to perform the transformation.
Returns:
None
"""
os.makedirs(self.processed_dir, exist_ok=True)
transformed_file_name = self.processed_file_names_dict["data"]
print(