autoencodix 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of autoencodix might be problematic. Click here for more details.

Files changed (91) hide show
  1. autoencodix/__init__.py +27 -0
  2. autoencodix/base/.ipynb_checkpoints/_base_pipeline-checkpoint.py +1274 -0
  3. autoencodix/base/.ipynb_checkpoints/_base_trainer-checkpoint.py +247 -0
  4. autoencodix/base/__init__.py +21 -0
  5. autoencodix/base/_base_autoencoder.py +138 -0
  6. autoencodix/base/_base_dataset.py +103 -0
  7. autoencodix/base/_base_evaluator.py +34 -0
  8. autoencodix/base/_base_loss.py +311 -0
  9. autoencodix/base/_base_pipeline.py +1355 -0
  10. autoencodix/base/_base_predictor.py +6 -0
  11. autoencodix/base/_base_preprocessor.py +1033 -0
  12. autoencodix/base/_base_trainer.py +259 -0
  13. autoencodix/base/_base_visualizer.py +578 -0
  14. autoencodix/base/sampleconfig.yaml +132 -0
  15. autoencodix/configs/__init__.py +21 -0
  16. autoencodix/configs/default_config.py +652 -0
  17. autoencodix/configs/disentanglix_config.py +15 -0
  18. autoencodix/configs/maskix_config.py +39 -0
  19. autoencodix/configs/ontix_config.py +35 -0
  20. autoencodix/configs/stackix_config.py +33 -0
  21. autoencodix/configs/vanillix_config.py +18 -0
  22. autoencodix/configs/varix_config.py +15 -0
  23. autoencodix/configs/xmodalix_config.py +43 -0
  24. autoencodix/disentanglix.py +72 -0
  25. autoencodix/evaluate/.ipynb_checkpoints/evaluate-checkpoint.py +532 -0
  26. autoencodix/evaluate/__init__.py +4 -0
  27. autoencodix/evaluate/_general_evaluator.py +730 -0
  28. autoencodix/evaluate/_xmodalix_evaluator.py +376 -0
  29. autoencodix/imagix.py +76 -0
  30. autoencodix/losses/disentanglix_loss.py +215 -0
  31. autoencodix/losses/maskix_loss.py +94 -0
  32. autoencodix/losses/vanillix_loss.py +28 -0
  33. autoencodix/losses/varix_loss.py +102 -0
  34. autoencodix/losses/xmodal_loss.py +304 -0
  35. autoencodix/maskix.py +108 -0
  36. autoencodix/modeling/.ipynb_checkpoints/__init__-checkpoint.py +16 -0
  37. autoencodix/modeling/.ipynb_checkpoints/_captum_forward-checkpoint.py +16 -0
  38. autoencodix/modeling/__init__.py +20 -0
  39. autoencodix/modeling/_captum_forward.py +20 -0
  40. autoencodix/modeling/_classifier.py +45 -0
  41. autoencodix/modeling/_imagevae_architecture.py +296 -0
  42. autoencodix/modeling/_imgfast_architecture.py +288 -0
  43. autoencodix/modeling/_layer_factory.py +71 -0
  44. autoencodix/modeling/_maskix_architecture.py +190 -0
  45. autoencodix/modeling/_ontix_architecture.py +305 -0
  46. autoencodix/modeling/_vanillix_architecture.py +136 -0
  47. autoencodix/modeling/_varix_architecture.py +192 -0
  48. autoencodix/notebooks/__init__.py +0 -0
  49. autoencodix/ontix.py +136 -0
  50. autoencodix/py.typed +0 -0
  51. autoencodix/stackix.py +122 -0
  52. autoencodix/trainers/__init__.py +6 -0
  53. autoencodix/trainers/_general_trainer.py +525 -0
  54. autoencodix/trainers/_maskix_trainer.py +116 -0
  55. autoencodix/trainers/_ontix_trainer.py +77 -0
  56. autoencodix/trainers/_stackix_orchestrator.py +522 -0
  57. autoencodix/trainers/_stackix_trainer.py +209 -0
  58. autoencodix/trainers/_xmodal_trainer.py +1006 -0
  59. autoencodix/utils/.ipynb_checkpoints/_explainer-checkpoint.py +118 -0
  60. autoencodix/utils/.ipynb_checkpoints/_model_output-checkpoint.py +30 -0
  61. autoencodix/utils/.ipynb_checkpoints/_utils-checkpoint.py +626 -0
  62. autoencodix/utils/.ipynb_checkpoints/adata_converter-checkpoint.py +99 -0
  63. autoencodix/utils/.ipynb_checkpoints/feature_importance-checkpoint.py +823 -0
  64. autoencodix/utils/__init__.py +22 -0
  65. autoencodix/utils/_annealer.py +193 -0
  66. autoencodix/utils/_bulkreader.py +232 -0
  67. autoencodix/utils/_explainer.py +246 -0
  68. autoencodix/utils/_imgreader.py +404 -0
  69. autoencodix/utils/_internals.py +23 -0
  70. autoencodix/utils/_llm_explainer.py +318 -0
  71. autoencodix/utils/_losses.py +20 -0
  72. autoencodix/utils/_model_output.py +30 -0
  73. autoencodix/utils/_result.py +393 -0
  74. autoencodix/utils/_screader.py +76 -0
  75. autoencodix/utils/_traindynamics.py +187 -0
  76. autoencodix/utils/_utils.py +690 -0
  77. autoencodix/utils/adata_converter.py +111 -0
  78. autoencodix/utils/example_data.py +398 -0
  79. autoencodix/utils/feature_importance.py +829 -0
  80. autoencodix/utils/prompts.py +21 -0
  81. autoencodix/vanillix.py +141 -0
  82. autoencodix/varix.py +80 -0
  83. autoencodix/visualize/__init__.py +5 -0
  84. autoencodix/visualize/_general_visualizer.py +913 -0
  85. autoencodix/visualize/_imagix_visualizer.py +111 -0
  86. autoencodix/visualize/_xmodal_visualizer.py +841 -0
  87. autoencodix/visualize/visualize.py +1214 -0
  88. autoencodix/xmodalix.py +318 -0
  89. autoencodix-0.2.0.dist-info/METADATA +143 -0
  90. autoencodix-0.2.0.dist-info/RECORD +91 -0
  91. autoencodix-0.2.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,626 @@
1
+ """
2
+ Stores utility functions for the autoencodix package.
3
+ Use of OOP would be overkill for the simple functions in this module.
4
+ """
5
+
6
+ import zipfile
7
+ import inspect
8
+ import os
9
+ from collections import defaultdict
10
+ from dataclasses import MISSING, fields, is_dataclass
11
+ from functools import wraps
12
+ from typing import Any, Callable, Dict, List, Optional, no_type_check
13
+ from autoencodix.data._datasetcontainer import DatasetContainer
14
+ from autoencodix.utils._result import Result
15
+
16
+ import dill as pickle # type: ignore
17
+ import torch
18
+ from matplotlib import pyplot as plt
19
+
20
+ from autoencodix.configs.default_config import DefaultConfig
21
+
22
+
23
+ # only for type hints, to avoid circual import
24
+ class BasePipeline:
25
+ """Only for type hints in utils, not real BasePipeline class"""
26
+
27
+ pass
28
+
29
+
30
+ def get_dataset(result: Result) -> Optional[DatasetContainer]:
31
+ """Retrieve the dataset from the Result object, depending on if new_datasets is filled.
32
+
33
+ Args:
34
+ result: The Result object containing the dataset.
35
+ Returns:
36
+ The appropriate DatasetContainer object.
37
+
38
+ """
39
+ splits = ["train", "valid", "test"]
40
+ if not result.new_datasets:
41
+ return result.datasets
42
+ new_values: List[Any] = [result.new_datasets[split] for split in splits]
43
+ # check if all new_datasets are None
44
+ if all(v is None for v in new_values):
45
+ return result.datasets
46
+ else:
47
+ return result.new_datasets
48
+
49
+
50
+ def nested_dict():
51
+ """Creates a nested defaultdict.
52
+
53
+ This function returns a defaultdict where each value is another defaultdict
54
+ of the same type. This allows for the creation of arbitrarily deep nested
55
+ dictionaries without having to explicitly define each level.
56
+
57
+ Returns:
58
+ :A nested defaultdict where each value is another nested defaultdict.
59
+ """
60
+ return defaultdict(nested_dict)
61
+
62
+
63
+ def nested_to_tuple(d, base=()):
64
+ """Recursively converts a nested dictionary into tuples.
65
+
66
+ Args:
67
+ d: The dictionary to convert.
68
+ base: The base tuple to start with. Defaults to ().
69
+
70
+ Yields:
71
+ tuple: Tuples representing the nested dictionary structure, where each tuple
72
+ contains the keys leading to a value and the value itself.
73
+ """
74
+ for k, v in d.items():
75
+ if isinstance(v, dict):
76
+ yield from nested_to_tuple(v, base + (k,))
77
+ else:
78
+ yield base + (k, v)
79
+
80
+
81
+ @no_type_check
82
+ def show_figure(fig):
83
+ """Display a given Matplotlib figure in a new window.
84
+
85
+ Args:
86
+ fig: The figure to be displayed.
87
+
88
+ """
89
+ dummy = plt.figure()
90
+ new_manager = dummy.canvas.manager
91
+ new_manager.canvas.figure = fig
92
+ fig.set_canvas(new_manager.canvas)
93
+
94
+
95
+ def config_method(valid_params: Optional[set[str]] = None):
96
+ """Decorator for methods that accept configuration parameters via kwargs or an explicit 'config' object.
97
+
98
+ It separates kwargs intended for the function's signature from those
99
+ intended as configuration overrides, validates the latter against
100
+ `valid_params`, applies valid overrides to a copy of `self.config`,
101
+ and passes the appropriate arguments to the decorated function.
102
+
103
+ Args:
104
+ valid_params: Set of valid configuration parameter names that can be overridden
105
+ via kwargs for this method. If None, all kwargs not matching the
106
+ function signature are considered potentially valid config overrides.
107
+ """
108
+
109
+ def decorator(func: Callable) -> Callable:
110
+ sig = inspect.signature(func)
111
+
112
+ param_docs = "\n\nValid configuration parameters (passed via **kwargs):\n"
113
+ if valid_params:
114
+ param_docs += "\n".join(f"- `{param}`" for param in sorted(valid_params))
115
+ else:
116
+ param_docs += (
117
+ "All keyword arguments not matching the function's "
118
+ "signature are treated as potential configuration overrides."
119
+ )
120
+
121
+ if func.__doc__ is None:
122
+ func.__doc__ = ""
123
+ # Avoid duplicating if decorator is applied multiple times (though unlikely)
124
+ if "Valid configuration parameters" not in func.__doc__:
125
+ func.__doc__ += param_docs
126
+ # --- End Docstring Modification ---
127
+
128
+ @wraps(func)
129
+ def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
130
+ # Pop the explicit config object if provided
131
+ user_config = kwargs.pop("config", None)
132
+
133
+ # Get names of parameters in the decorated function's signature
134
+ # that can accept keyword arguments (excluding self and config)
135
+ func_sig_kwarg_names = {
136
+ name
137
+ for name, param in sig.parameters.items()
138
+ if param.kind
139
+ in (
140
+ inspect.Parameter.POSITIONAL_OR_KEYWORD,
141
+ inspect.Parameter.KEYWORD_ONLY,
142
+ )
143
+ and name not in ("self", "config")
144
+ }
145
+
146
+ # Separate kwargs into those matching the signature and potential config overrides
147
+ func_specific_kwargs = {}
148
+ potential_config_kwargs = {}
149
+ for k, v in kwargs.items():
150
+ if k in func_sig_kwarg_names:
151
+ func_specific_kwargs[k] = v
152
+ else:
153
+ potential_config_kwargs[k] = v
154
+
155
+ # Determine the configuration object to use
156
+ if user_config is None:
157
+ # No explicit config object, use self.config and apply overrides
158
+ if not hasattr(self, "config"):
159
+ raise AttributeError(
160
+ f"{type(self).__name__} instance is missing 'config' attribute."
161
+ )
162
+ # Ensure self.config is the right type (or duck-types model_copy/model_dump)
163
+ if not hasattr(self.config, "model_copy") or not hasattr(
164
+ self.config, "model_dump"
165
+ ):
166
+ raise TypeError(
167
+ f"'self.config' on {type(self).__name__} must have 'model_copy' and 'model_dump' methods."
168
+ )
169
+
170
+ final_config = self.config.model_copy(deep=True) # Start with a copy
171
+
172
+ # Validate potential_config_kwargs against valid_params
173
+ if valid_params:
174
+ # Check for invalid *config* parameters among the potential overrides
175
+ invalid_config_params = (
176
+ set(potential_config_kwargs.keys()) - valid_params
177
+ )
178
+ if invalid_config_params:
179
+ print(
180
+ f"\nWarning: The following parameters are not valid "
181
+ f"configuration overrides for {func.__name__}:" # type: ignore
182
+ )
183
+ print(
184
+ f"Invalid config parameters: {', '.join(invalid_config_params)}"
185
+ )
186
+ print(
187
+ f"Valid config parameters are: {', '.join(sorted(valid_params))}"
188
+ )
189
+ print("These parameters will be ignored.")
190
+
191
+ # Filter potential overrides to only include those listed in valid_params
192
+ # and that actually exist as fields in the config object
193
+ # (prevents adding arbitrary attributes if DefaultConfig uses Pydantic's extra='forbid')
194
+ valid_config_fields = {
195
+ p
196
+ for p in valid_params
197
+ if hasattr(final_config, p) # Safer check
198
+ }
199
+ config_overrides = {
200
+ k: v
201
+ for k, v in potential_config_kwargs.items()
202
+ if k in valid_config_fields
203
+ }
204
+
205
+ else: # valid_params is None: Allow all potential config kwargs as overrides
206
+ # Optional: Add a check here if you want to ensure they exist in the config model
207
+ config_overrides = potential_config_kwargs
208
+
209
+ # Apply the valid overrides
210
+ if config_overrides:
211
+ final_config = final_config.model_copy(update=config_overrides)
212
+
213
+ else:
214
+ # User provided an explicit config object
215
+ # Add type check if DefaultConfig class is available
216
+ # Note: Replace 'object' with 'DefaultConfig' if it's imported/defined
217
+ if not isinstance(user_config, DefaultConfig):
218
+ # Trying to be robust if DefaultConfig is not strictly enforced type
219
+ if hasattr(user_config, "model_copy") and hasattr(
220
+ user_config, "model_dump"
221
+ ):
222
+ pass # Looks like a valid config object duck-typing Pydantic
223
+ else:
224
+ raise TypeError(
225
+ "The 'config' parameter must be a valid configuration object "
226
+ "(e.g., an instance of DefaultConfig or similar)."
227
+ )
228
+ final_config = user_config
229
+ # Decide what to do with potential_config_kwargs when user_config is provided.
230
+ # Option 1: Ignore them (current implementation below)
231
+ # Option 2: Raise an error if any exist
232
+ # Option 3: Apply them even to the user_config (might be unexpected)
233
+ if potential_config_kwargs:
234
+ print(
235
+ f"\nWarning: Additional keyword arguments provided "
236
+ f"({', '.join(potential_config_kwargs.keys())}) "
237
+ f"while an explicit 'config' object was also passed to {func.__name__}. " # type: ignore
238
+ f"These additional arguments will be ignored as configuration overrides."
239
+ )
240
+
241
+ # Call the original function with the correct arguments
242
+ # Pass: self, original *args, the determined config object,
243
+ # and only the **kwargs that matched the function's signature.
244
+ return func(self, *args, config=final_config, **func_specific_kwargs)
245
+
246
+ # Preserve information about valid_params on the wrapper if needed elsewhere
247
+ setattr(wrapper, "valid_params", valid_params)
248
+ return wrapper
249
+
250
+ return decorator
251
+
252
+
253
+ class Saver:
254
+ """Handles the saving of BasePipeline objects.
255
+
256
+ Atrributes:
257
+ file_path: path to save file.
258
+ preprocessor_path: path where pickle object of preprocesser should be saved.
259
+ model_state_path: path where model state dict should be saved.
260
+ save_all: indicator if all results should be save, or only core pipeline functionalty
261
+
262
+ """
263
+
264
+ def __init__(self, file_path: str, save_all: bool):
265
+ """Initializes the Saver with the base file path.
266
+
267
+ Args:
268
+ file_path: The base file path (without extensions).
269
+ """
270
+
271
+ self.save_all = save_all
272
+ self.preprocessor_path = f"{file_path}_preprocessor.pkl"
273
+ self.model_state_path = f"{file_path}_model.pth"
274
+
275
+ self.file_path = f"{file_path}.pkl"
276
+ folder = os.path.dirname(self.file_path)
277
+ if folder:
278
+ os.makedirs(folder, exist_ok=True)
279
+
280
+ @no_type_check
281
+ def save(self, pipeline: "BasePipeline"):
282
+ """Saves the BasePipeline object.
283
+
284
+ Args:
285
+ pipeline: The BasePipeline object to save.
286
+ """
287
+
288
+ self._save_preprocessor(pipeline._preprocessor) # type: ignore
289
+ self._save_model_state(pipeline)
290
+ self.pipeline = pipeline
291
+
292
+ self.pipeline._trainer.purge()
293
+
294
+ if not self.save_all:
295
+ print("saving memory efficient")
296
+ self.reset_to_defaults(pipeline.result) # ty: ignore
297
+
298
+ self.pipeline.preprocessed_data = None # ty: ignore
299
+ self.pipeline._datasets = None # ty: ignore
300
+ self.pipeline.raw_user_data = None # ty: ignore
301
+ self.pipeline._datasets = None
302
+ self.pipeline._preprocessor = type(
303
+ self.pipeline._preprocessor
304
+ )( # ty: ignore
305
+ config=pipeline.config # ty: ignore
306
+ ) # ty: ignore
307
+ self.pipeline.visualizer = type(self.pipeline.visualizer)() # ty: ignore
308
+
309
+ self._save_pipeline_object(self.pipeline)
310
+
311
+ with zipfile.ZipFile(f"{self.file_path}.zip", "w") as archive:
312
+ archive.write(self.file_path)
313
+ archive.write(self.preprocessor_path)
314
+ for model_state_path in self.model_state_paths:
315
+ archive.write(model_state_path)
316
+ os.remove(self.file_path)
317
+ os.remove(self.preprocessor_path)
318
+ for model_state_path in self.model_state_paths:
319
+ os.remove(model_state_path)
320
+
321
+ def _save_pipeline_object(self, pipeline: "BasePipeline"):
322
+ try:
323
+ with open(self.file_path, "wb") as f:
324
+ pickle.dump(pipeline, f)
325
+ print("Pipeline object saved successfully.")
326
+ except (pickle.PicklingError, OSError) as e:
327
+ print(f"Error saving pipeline object: {e}")
328
+ raise e
329
+
330
+ def _save_preprocessor(self, preprocessor):
331
+ if preprocessor is not None:
332
+ try:
333
+ with open(self.preprocessor_path, "wb") as f:
334
+ pickle.dump(preprocessor, f)
335
+ print("Preprocessor saved successfully.")
336
+ except (pickle.PickleError, OSError) as e:
337
+ print(f"Error saving preprocessor: {e}")
338
+ raise e
339
+
340
+ @no_type_check
341
+ def _save_model_state(self, pipeline: "BasePipeline"):
342
+ self.model_state_paths: List[str] = []
343
+ if pipeline.result is not None and pipeline.result.model is not None: # type: ignore
344
+ if isinstance(pipeline.result.model, torch.nn.Module):
345
+ try:
346
+ pipeline.result.model.to("cpu")
347
+ torch.save(
348
+ pipeline.result.model.state_dict(), self.model_state_path
349
+ ) # type: ignore
350
+ self.model_state_paths.append(self.model_state_path)
351
+
352
+ except (TypeError, OSError) as e:
353
+ print(f"Error saving model state: {e}")
354
+ raise e
355
+ elif isinstance(pipeline.result.model, dict):
356
+ for model_name, model in pipeline.result.model.items():
357
+ if hasattr(model, "module"):
358
+ model = model.module
359
+ model.to("cpu")
360
+ torch.save(
361
+ model.state_dict(), f"{model_name}_{self.model_state_path}.pth"
362
+ ) # type: ignore
363
+ self.model_state_paths.append(
364
+ f"{model_name}_{self.model_state_path}.pth"
365
+ )
366
+ else:
367
+ raise TypeError(
368
+ f"pipeline.result.model is neither a torch.nn.Module nor a dict, got {type(pipeline.result.model)}"
369
+ )
370
+
371
+ @no_type_check
372
+ def reset_to_defaults(self, obj):
373
+ if not is_dataclass(obj):
374
+ raise ValueError("Object must be a dataclass")
375
+
376
+ for f in fields(obj):
377
+ # we keep the adata_latent space as a "core result"
378
+ if f.name == "adata_latent":
379
+ continue
380
+ if f.name == "model":
381
+ # we need to keep the instantiated class, so we can load the state dict
382
+ # but we don't want to save the model twice (once via the pipeline object, once as model.pth)
383
+ # thus, we first save the state_dict, then reiniate the model to free memory
384
+ if isinstance(obj.model, dict):
385
+ empty_models = {}
386
+ for model_name, model in obj.model.items():
387
+ if hasattr(model, "module"):
388
+ model = model.module
389
+ empty_models[model_name] = type(model)(**model.init_args)
390
+ elif isinstance(obj.model, torch.nn.Module):
391
+ obj.model.init_args["ontologies"] = self.pipeline.ontologies
392
+ obj.model.init_args["feature_order"] = (
393
+ self.pipeline._trainer.feature_order
394
+ )
395
+ obj.model = type(obj.model)(**obj.model.init_args)
396
+ continue
397
+ if f.default_factory is not MISSING:
398
+ setattr(obj, f.name, f.default_factory())
399
+ elif f.default is not MISSING:
400
+ setattr(obj, f.name, f.default)
401
+ else:
402
+ setattr(obj, f.name, None)
403
+
404
+
405
+ class Loader:
406
+ """Handles the loading of BasePipeline objects.
407
+
408
+ Atrributes:
409
+ file_path: path of saved pipeline object.
410
+ preprocessor_path: path where pickle object of preprocesser should was saved.
411
+ model_state_path: path where model state dict was saved.
412
+ """
413
+
414
+ def __init__(self, file_path: str):
415
+ """Initializes the Loader with the base file path.
416
+
417
+ Args:
418
+ file_path: The base file path (without extensions).
419
+ """
420
+ self.preprocessor_path = f"{file_path}_preprocessor.pkl"
421
+ self.model_state_path = f"{file_path}_model.pth"
422
+ self.file_path = f"{file_path}.pkl"
423
+
424
+ def load(self) -> Any:
425
+ """Loads the BasePipeline object.
426
+
427
+ Returns:
428
+ The loaded BasePipeline object, or None on error.
429
+ """
430
+ with zipfile.ZipFile(f"{self.file_path}.zip", "r") as archive:
431
+ archive.extractall()
432
+
433
+ loaded_obj = self._load_pipeline_object()
434
+ if loaded_obj is None:
435
+ raise ValueError("Error while loading pipeline object")
436
+
437
+ loaded_obj._preprocessor = self._load_preprocessor()
438
+ loaded_obj.result.model = self._load_model_state(loaded_obj)
439
+
440
+ return loaded_obj
441
+
442
+ def _load_pipeline_object(self) -> Any:
443
+ print(f"Attempting to load a pipeline from {self.file_path}...")
444
+ try:
445
+ if not os.path.exists(self.file_path):
446
+ print(f"Error: File not found at {self.file_path}")
447
+ return None
448
+ with open(self.file_path, "rb") as f:
449
+ loaded_obj = pickle.load(f)
450
+ print(
451
+ f"Pipeline object loaded successfully. Actual type: {type(loaded_obj).__name__}"
452
+ )
453
+ return loaded_obj
454
+ except (pickle.UnpicklingError, EOFError, OSError, FileNotFoundError) as e:
455
+ print(f"Error loading pipeline object: {e}")
456
+ return None
457
+
458
+ def _load_preprocessor(self):
459
+ if os.path.exists(self.preprocessor_path):
460
+ try:
461
+ with open(self.preprocessor_path, "rb") as f:
462
+ preprocessor = pickle.load(f)
463
+ print("Preprocessor loaded successfully.")
464
+ return preprocessor
465
+ except (pickle.UnpicklingError, EOFError, OSError, FileNotFoundError) as e:
466
+ print(f"Error loading preprocessor: {e}")
467
+ return None
468
+ else:
469
+ print("Preprocessor file not found. Skipping preprocessor load.")
470
+ return None
471
+
472
+ @no_type_check
473
+ def _load_model_state(self, loaded_obj: "BasePipeline"):
474
+ if loaded_obj.result is None: # type: ignore:
475
+ raise ValueError("Loaded pipeline has no result attribute")
476
+ if loaded_obj.result.model is None:
477
+ raise ValueError("Loaded pipeline result has no model attribute")
478
+ if isinstance(loaded_obj.result.model, dict):
479
+ for model_name, model in loaded_obj.result.model.items():
480
+ if hasattr(model, "module"):
481
+ model = model.module
482
+ if not os.path.exists(f"{model_name}_{self.model_state_path}.pth"):
483
+ raise FileNotFoundError(
484
+ f"Model state file not found at {model_name}_{self.model_state_path}.pth"
485
+ )
486
+ model_state = torch.load(
487
+ f"{model_name}_{self.model_state_path}.pth", map_location="cpu"
488
+ )
489
+ loaded_obj.result.model[model_name].to("cpu")
490
+ loaded_obj.result.model[model_name].load_state_dict( # type: ignore
491
+ model_state
492
+ )
493
+ return loaded_obj.result.model # type: ignore
494
+ elif isinstance(loaded_obj.result.model, torch.nn.Module):
495
+ if not os.path.exists(self.model_state_path):
496
+ raise FileNotFoundError(
497
+ f"Model state file not found at {self.model_state_path}"
498
+ )
499
+ model_state = torch.load(self.model_state_path, map_location="cpu")
500
+ loaded_obj.result.model.to("cpu")
501
+ loaded_obj.result.model.load_state_dict(model_state) # type: ignore
502
+ return loaded_obj.result.model # type: ignore
503
+ else:
504
+ raise TypeError(
505
+ f"Loaded model is neither a dict nor a torch.nn.Module, got {type(loaded_obj.result.model)}"
506
+ )
507
+
508
+
509
+ def flip_labels(labels: torch.Tensor) -> torch.Tensor:
510
+ """Randomly flip modality labels with probability (1 - 1/n_modalities), vectorized.
511
+
512
+ This is mainly used for advers training in multi modal xmodalix.
513
+
514
+ Args:
515
+ labels: tensor of labels
516
+ Returns:
517
+ flipped tensor
518
+
519
+ """
520
+ device = labels.device
521
+ n_modalities = labels.unique().numel()
522
+ batch_size = labels.size(0)
523
+ flip_prob = 1.0 - 1.0 / n_modalities
524
+
525
+ # Decide which labels to flip
526
+ flip_mask = torch.rand(batch_size, device=device) < flip_prob
527
+
528
+ # Sample random labels for flipping
529
+ rand_labels = torch.randint(0, n_modalities, size=(batch_size,), device=device)
530
+
531
+ # Ensure sampled labels are different from original labels
532
+ needs_resample = (rand_labels == labels) & flip_mask
533
+ while needs_resample.any():
534
+ rand_labels[needs_resample] = torch.randint(
535
+ 0, n_modalities, size=(needs_resample.sum(),), device=device
536
+ )
537
+ needs_resample = (rand_labels == labels) & flip_mask
538
+
539
+ # Apply flipped labels where needed
540
+ flipped = labels.clone()
541
+ flipped[flip_mask] = rand_labels[flip_mask]
542
+
543
+ return flipped
544
+
545
+
546
+ def find_translation_keys(
547
+ config: DefaultConfig,
548
+ trained_modalities: List[str],
549
+ from_key: Optional[str] = None,
550
+ to_key: Optional[str] = None,
551
+ ) -> Dict[str, str]: # type: ignore
552
+ """Find translation source and target modalities.
553
+
554
+ Determines which modalities serve as the "from" and "to" directions for
555
+ cross-modal prediction, either from explicit arguments or from the
556
+ configuration.
557
+
558
+ Args:
559
+ config: Experiment configuration containing data information.
560
+ trained_modalities: List of trained modality names.
561
+ from_key: Optional name of the source modality.
562
+ to_key: Optional name of the target modality.
563
+
564
+ Returns:
565
+ A dictionary with two entries:
566
+ - "from": Name of the source modality.
567
+ - "to": Name of the target modality.
568
+
569
+ Raises:
570
+ ValueError: If no valid "from" or "to" modality is found, or if
571
+ multiple conflicting directions are specified.
572
+ """
573
+ from_key_final: Optional[str] = None
574
+ to_key_final: Optional[str] = None
575
+ simple_names: List[str] = [
576
+ tm.split(".", 1)[1] if "." in tm else tm for tm in trained_modalities
577
+ ]
578
+
579
+ if from_key and to_key:
580
+ for name in trained_modalities:
581
+ simple_name = name.split(".", 1)[1] if "." in name else name
582
+
583
+ if from_key == simple_name or from_key == name:
584
+ from_key_final = name
585
+ # use if instead of elif to allow for reference prediciton where from_key == to_key
586
+ if to_key == simple_name or from_key == name:
587
+ to_key_final = name
588
+ # if the users passes from_key and to_key and we don't find them, we raise an error
589
+ if not (from_key_final and to_key_final):
590
+ raise ValueError(
591
+ f"Invalid translation keys: {from_key} => {to_key}, valid keys are: {simple_names}"
592
+ )
593
+ return {"from": from_key_final, "to": to_key_final}
594
+
595
+ data_info = config.data_config.data_info
596
+ for name in trained_modalities:
597
+ simple_name = name.split(".", 1)[1] if "." in name else name
598
+ cur_info = data_info.get(simple_name)
599
+ if not cur_info or not hasattr(cur_info, "translate_direction"):
600
+ continue
601
+
602
+ direction = cur_info.translate_direction
603
+ if direction == "to":
604
+ if to_key_final is not None:
605
+ raise ValueError(
606
+ f"Multiple 'to' directions found: '{to_key_final}' and '{name}'"
607
+ )
608
+ to_key_final = name
609
+ elif direction == "from":
610
+ if from_key_final is not None:
611
+ raise ValueError(
612
+ f"Multiple 'from' directions found: '{from_key_final}' and '{name}'"
613
+ )
614
+ from_key_final = name
615
+
616
+ if from_key_final is None:
617
+ raise ValueError(
618
+ "No modality with a 'from' direction was specified in the config."
619
+ )
620
+ if to_key_final is None:
621
+ raise ValueError(
622
+ "No modality with a 'to' direction was specified in the config."
623
+ )
624
+ assert from_key_final is not None and to_key_final is not None
625
+
626
+ return {"from": from_key_final, "to": to_key_final}