Core API
culicidaelab.core
Core components of the CulicidaeLab library.
This module provides the base classes, configuration management, and resource handling functionalities that form the foundation of the library. It exports key classes and functions for convenient access from other parts of the application.
Attributes:
Name | Type | Description |
---|---|---|
__all__ |
list[str]
|
A list of the public objects of this module. |
__all__ = ['BasePredictor', 'BaseProvider', 'WeightsManagerProtocol', 'ConfigManager', 'CulicidaeLabConfig', 'PredictorConfig', 'DatasetConfig', 'ProviderConfig', 'SpeciesModel', 'SpeciesConfig', 'ProviderService', 'ResourceManager', 'ResourceManagerError', 'Settings', 'get_settings', 'download_file', 'str_to_bgr']
module-attribute
BasePredictor
Abstract base class for all predictors.
This class defines the common interface for all predictors (e.g., detector, segmenter, classifier). It relies on the main Settings object for configuration and a WeightsManager for model file management.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings
|
The main Settings object for the library. |
required |
predictor_type
|
str
|
The key for this predictor in the configuration (e.g., 'classifier'). |
required |
weights_manager
|
WeightsManagerProtocol
|
An object conforming to the WeightsManagerProtocol. |
required |
load_model
|
bool
|
If True, loads the model immediately upon initialization. |
False
|
Attributes:
Name | Type | Description |
---|---|---|
settings |
Settings
|
The main settings object. |
predictor_type |
str
|
The type of the predictor. |
weights_manager |
WeightsManagerProtocol
|
The manager responsible for providing model weights. |
Source code in culicidaelab/core/base_predictor.py
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 |
|
settings = settings
instance-attribute
predictor_type = predictor_type
instance-attribute
config: PredictorConfig
property
Get the predictor configuration Pydantic model.
model_loaded: bool
property
Check if the model is loaded.
model_path: Path
property
Gets the path to the model weights file.
__init__(settings: Settings, predictor_type: str, weights_manager: WeightsManagerProtocol, load_model: bool = False)
Initializes the predictor.
Raises:
Type | Description |
---|---|
ValueError
|
If the configuration for the specified |
Source code in culicidaelab/core/base_predictor.py
__call__(input_data: np.ndarray, **kwargs: Any) -> Any
Convenience method that calls predict()
.
__enter__()
__exit__(exc_type, exc_val, exc_tb)
model_context()
A context manager for temporary model loading.
Ensures the model is loaded upon entering the context and unloaded upon exiting. This is useful for managing memory in pipelines.
Yields:
Name | Type | Description |
---|---|---|
BasePredictor |
The predictor instance itself. |
Example
with predictor.model_context(): ... predictions = predictor.predict(data)
Source code in culicidaelab/core/base_predictor.py
evaluate(ground_truth: GroundTruthType, prediction: PredictionType | None = None, input_data: np.ndarray | None = None, **predict_kwargs: Any) -> dict[str, float]
Evaluate a prediction against a ground truth.
Either prediction
or input_data
must be provided. If prediction
is provided, it is used directly. If prediction
is None, input_data
is used to generate a new prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth
|
GroundTruthType
|
The ground truth annotation. |
required |
prediction
|
PredictionType
|
A pre-computed prediction. |
None
|
input_data
|
ndarray
|
Input data to generate a prediction from, if one isn't provided. |
None
|
**predict_kwargs
|
Any
|
Additional arguments passed to the |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing evaluation metrics for a |
dict[str, float]
|
single item. |
Raises:
Type | Description |
---|---|
ValueError
|
If neither |
Source code in culicidaelab/core/base_predictor.py
evaluate_batch(ground_truth_batch: list[GroundTruthType], predictions_batch: list[PredictionType] | None = None, input_data_batch: list[np.ndarray] | None = None, num_workers: int = 4, show_progress: bool = True, **predict_kwargs: Any) -> dict[str, float]
Evaluate on a batch of items using parallel processing.
Either predictions_batch
or input_data_batch
must be provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth_batch
|
list[GroundTruthType]
|
List of corresponding ground truth annotations. |
required |
predictions_batch
|
list[PredictionType]
|
A pre-computed list of predictions. |
None
|
input_data_batch
|
list[ndarray]
|
List of input data to generate predictions from. |
None
|
num_workers
|
int
|
Number of parallel workers for calculating metrics. |
4
|
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
**predict_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing aggregated evaluation metrics. |
Raises:
Type | Description |
---|---|
ValueError
|
If the number of predictions does not match the number of ground truths. |
Source code in culicidaelab/core/base_predictor.py
get_model_info() -> dict[str, Any]
Gets information about the loaded model.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary containing details about the model, such |
dict[str, Any]
|
as architecture, path, etc. |
Source code in culicidaelab/core/base_predictor.py
load_model() -> None
Loads the model if it is not already loaded.
This is a convenience wrapper around _load_model
that prevents
reloading.
Raises:
Type | Description |
---|---|
RuntimeError
|
If model loading fails. |
Source code in culicidaelab/core/base_predictor.py
predict_batch(input_data_batch: list[np.ndarray], show_progress: bool = True, **kwargs: Any) -> list[PredictionType]
Makes predictions on a batch of inputs.
This base implementation processes items serially. Subclasses with native batching capabilities SHOULD override this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data_batch
|
list[ndarray]
|
List of input data to make predictions on. |
required |
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
**kwargs
|
Any
|
Additional arguments passed to each |
{}
|
Returns:
Type | Description |
---|---|
list[PredictionType]
|
list[PredictionType]: List of predictions. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If model fails to load or predict. |
Source code in culicidaelab/core/base_predictor.py
unload_model() -> None
Unloads the model to free memory.
predict(input_data: np.ndarray, **kwargs: Any) -> PredictionType
abstractmethod
Makes a prediction on a single input data sample.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
The input data (e.g., an image as a NumPy array) to make a prediction on. |
required |
**kwargs
|
Any
|
Additional predictor-specific arguments. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
PredictionType |
PredictionType
|
The prediction result, with a format specific to the |
PredictionType
|
predictor type. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model is not loaded before calling this method. |
Source code in culicidaelab/core/base_predictor.py
visualize(input_data: np.ndarray, predictions: PredictionType, save_path: str | Path | None = None) -> np.ndarray
abstractmethod
Visualizes the predictions on the input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
The original input data (e.g., an image). |
required |
predictions
|
PredictionType
|
The prediction result obtained from
the |
required |
save_path
|
str | Path
|
An optional path to save the visualization to a file. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A NumPy array representing the visualized image. |
Source code in culicidaelab/core/base_predictor.py
BaseProvider
Abstract base class for all data and model providers.
Source code in culicidaelab/core/base_provider.py
download_dataset(dataset_name: str, save_dir: str | None = None, *args: Any, **kwargs: Any) -> Path
abstractmethod
Downloads a dataset from a source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name
|
str
|
The name of the dataset to download. |
required |
save_dir
|
str
|
The directory to save the dataset. Defaults to None. |
None
|
*args
|
Any
|
Additional positional arguments. |
()
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the download method. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The path to the downloaded dataset. |
Source code in culicidaelab/core/base_provider.py
download_model_weights(model_type: str, *args: Any, **kwargs: Any) -> Path
abstractmethod
Downloads model weights and returns the path to them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_type
|
str
|
The type of model (e.g., 'detection', 'classification'). |
required |
*args
|
Any
|
Additional positional arguments. |
()
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The path to the model weights file. |
Source code in culicidaelab/core/base_provider.py
get_provider_name() -> str
abstractmethod
Gets the unique name of the provider.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string representing the provider's name (e.g., 'huggingface'). |
load_dataset(dataset_path: str | Path, **kwargs: Any) -> Any
abstractmethod
Loads a dataset from a local path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_path
|
str | Path
|
The local path to the dataset, typically
returned by |
required |
**kwargs
|
Any
|
Additional keyword arguments for loading. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The loaded dataset object (e.g., a Hugging Face Dataset, a |
Any
|
PyTorch Dataset, or a Pandas DataFrame). |
Source code in culicidaelab/core/base_provider.py
WeightsManagerProtocol
Defines the interface for any class that manages model weights.
This protocol ensures that core components can work with any weights manager without depending on its concrete implementation.
Source code in culicidaelab/core/weights_manager_protocol.py
ensure_weights(predictor_type: str) -> Path
Ensures weights for a given predictor type are available locally.
This method might download the weights if they are missing or simply return the path if they already exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictor_type
|
str
|
The key for the predictor (e.g., 'classifier'). |
required |
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The local path to the model weights file. |
Source code in culicidaelab/core/weights_manager_protocol.py
ConfigManager
Handles loading, merging, and validating configurations for the library.
This manager implements a robust loading strategy: 1. Loads default YAML configurations bundled with the library. 2. Loads user-provided YAML configurations from a specified directory. 3. Merges the user's configuration on top of the defaults. 4. Validates the final merged configuration against Pydantic models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_config_dir
|
str | Path
|
Path to a directory containing user-defined YAML configuration files. These will override the defaults. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
user_config_dir |
Path | None
|
The user configuration directory. |
default_config_path |
Path
|
The path to the default config directory. |
config |
CulicidaeLabConfig
|
The validated configuration object. |
Source code in culicidaelab/core/config_manager.py
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 |
|
user_config_dir = Path(user_config_dir) if user_config_dir else None
instance-attribute
default_config_path = self._get_default_config_path()
instance-attribute
config: CulicidaeLabConfig = self._load()
instance-attribute
__init__(user_config_dir: str | Path | None = None)
Initializes the ConfigManager.
Source code in culicidaelab/core/config_manager.py
get_config() -> CulicidaeLabConfig
Returns the fully validated Pydantic configuration object.
Returns:
Name | Type | Description |
---|---|---|
CulicidaeLabConfig |
CulicidaeLabConfig
|
The |
instantiate_from_config(config_obj: Any, **kwargs: Any) -> Any
Instantiates a Python object from its Pydantic config model.
The config model must have a _target_
field specifying the fully
qualified class path (e.g., 'my_module.my_class.MyClass').
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_obj
|
Any
|
A Pydantic model instance (e.g., a predictor config). |
required |
**kwargs
|
Any
|
Additional keyword arguments to pass to the object's constructor, overriding any existing parameters in the config. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
An instantiated Python object. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
ImportError
|
If the class could not be imported and instantiated. |
Source code in culicidaelab/core/config_manager.py
save_config(file_path: str | Path) -> None
Saves the current configuration state to a YAML file.
This is useful for exporting the fully merged and validated config.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str | Path
|
The path where the YAML config will be saved. |
required |
Source code in culicidaelab/core/config_manager.py
CulicidaeLabConfig
The root Pydantic model for all CulicidaeLab configurations.
It validates the entire configuration structure after loading from YAML files.
Source code in culicidaelab/core/config_models.py
model_config = ConfigDict(extra='allow')
class-attribute
instance-attribute
app_settings: AppSettings = Field(default_factory=AppSettings)
class-attribute
instance-attribute
processing: ProcessingConfig = Field(default_factory=ProcessingConfig)
class-attribute
instance-attribute
datasets: dict[str, DatasetConfig] = {}
class-attribute
instance-attribute
predictors: dict[str, PredictorConfig] = {}
class-attribute
instance-attribute
providers: dict[str, ProviderConfig] = {}
class-attribute
instance-attribute
species: SpeciesModel = Field(default_factory=SpeciesModel)
class-attribute
instance-attribute
DatasetConfig
Configuration for a single dataset.
Source code in culicidaelab/core/config_models.py
model_config = ConfigDict(extra='allow')
class-attribute
instance-attribute
name: str
instance-attribute
path: str
instance-attribute
format: str
instance-attribute
classes: list[str]
instance-attribute
provider_name: str
instance-attribute
PredictorConfig
Configuration for a single predictor.
Source code in culicidaelab/core/config_models.py
model_config = ConfigDict(extra='allow', protected_namespaces=())
class-attribute
instance-attribute
target_: str = Field(..., alias='_target_')
class-attribute
instance-attribute
model_path: str
instance-attribute
confidence: float = 0.5
class-attribute
instance-attribute
device: str = 'cpu'
class-attribute
instance-attribute
params: dict[str, Any] = {}
class-attribute
instance-attribute
repository_id: str | None = None
class-attribute
instance-attribute
filename: str | None = None
class-attribute
instance-attribute
provider_name: str | None = None
class-attribute
instance-attribute
model_arch: str | None = None
class-attribute
instance-attribute
model_config_path: str | None
instance-attribute
model_config_filename: str | None
instance-attribute
visualization: VisualizationConfig = Field(default_factory=VisualizationConfig)
class-attribute
instance-attribute
ProviderConfig
Configuration for a data provider.
Source code in culicidaelab/core/config_models.py
model_config = ConfigDict(extra='allow')
class-attribute
instance-attribute
target_: str = Field(..., alias='_target_')
class-attribute
instance-attribute
dataset_url: str
instance-attribute
api_key: str | None = None
class-attribute
instance-attribute
SpeciesModel
Configuration for the entire 'species' section.
Source code in culicidaelab/core/config_models.py
model_config = ConfigDict(extra='allow')
class-attribute
instance-attribute
species_classes: dict[int, str] = Field(default_factory=dict)
class-attribute
instance-attribute
species_metadata: SpeciesFiles = Field(default_factory=SpeciesFiles)
class-attribute
instance-attribute
SpeciesConfig
A user-friendly facade for accessing species configuration.
This class acts as an adapter, taking the complex, validated SpeciesModel
object and providing simple methods and properties for accessing species data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
SpeciesModel
|
A validated |
required |
Attributes:
Name | Type | Description |
---|---|---|
_config |
SpeciesModel
|
The source configuration model. |
_species_map |
dict[int, str]
|
A mapping of class indices to full species names. |
_reverse_species_map |
dict[str, int]
|
A reverse mapping of species names to indices. |
_metadata_store |
dict
|
A store for species metadata. |
Source code in culicidaelab/core/species_config.py
species_map: dict[int, str]
property
Gets the mapping of class indices to full, human-readable species names.
Example
{0: "Aedes aegypti", 1: "Aedes albopictus"}
__init__(config: SpeciesModel)
Initializes the species configuration helper.
Source code in culicidaelab/core/species_config.py
get_index_by_species(species_name: str) -> int | None
Gets the class index by its full species name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
species_name
|
str
|
The full name of the species. |
required |
Returns:
Type | Description |
---|---|
int | None
|
int | None: The integer class index, or None if not found. |
Source code in culicidaelab/core/species_config.py
get_species_by_index(index: int) -> str | None
Gets the full species name by its class index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
int
|
The integer class index. |
required |
Returns:
Type | Description |
---|---|
str | None
|
str | None: The full species name as a string, or None if not found. |
Source code in culicidaelab/core/species_config.py
get_species_metadata(species_name: str) -> dict[str, Any] | None
Gets the detailed metadata for a specific species as a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
species_name
|
str
|
The full name of the species (e.g., "Aedes aegypti"). |
required |
Returns:
Type | Description |
---|---|
dict[str, Any] | None
|
dict[str, Any] | None: A dictionary representing the species metadata, |
dict[str, Any] | None
|
or None if not found. |
Source code in culicidaelab/core/species_config.py
list_species_names() -> list[str]
Returns a list of all configured full species names.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of strings, where each string is a species name. |
ProviderService
Manages the instantiation and lifecycle of data providers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings
|
The main |
required |
Attributes:
Name | Type | Description |
---|---|---|
_settings |
Settings
|
The settings instance. |
_providers |
dict[str, BaseProvider]
|
A cache of instantiated providers. |
Source code in culicidaelab/core/provider_service.py
__init__(settings: Settings)
get_provider(provider_name: str) -> BaseProvider
Retrieves an instantiated provider by its name.
It looks up the provider's configuration, instantiates it if it hasn't been already, and caches it for future calls.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
provider_name
|
str
|
The name of the provider (e.g., 'huggingface'). |
required |
Returns:
Name | Type | Description |
---|---|---|
BaseProvider |
BaseProvider
|
An instance of a class that inherits from |
Raises:
Type | Description |
---|---|
ValueError
|
If the provider is not found in the configuration. |
Source code in culicidaelab/core/provider_service.py
ResourceManager
Centralized resource management for models, datasets, and temporary files.
This class provides thread-safe operations for managing application resources, including models, datasets, cache files, and temporary workspaces.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app_name
|
str
|
Application name used for directory naming.
If None, the name is loaded from |
None
|
custom_base_dir
|
str | Path
|
Custom base directory for all resources. If None, system-default paths are used. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
app_name |
str
|
The application name. |
user_data_dir |
Path
|
User-specific data directory for persistent storage. |
user_cache_dir |
Path
|
User-specific cache directory for temporary files. |
temp_dir |
Path
|
Temporary directory for runtime operations. |
model_dir |
Path
|
Directory for storing model files. |
dataset_dir |
Path
|
Directory for storing dataset files. |
downloads_dir |
Path
|
Directory for downloaded files. |
Raises:
Type | Description |
---|---|
ResourceManagerError
|
If initialization fails. |
Source code in culicidaelab/core/resource_manager.py
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 |
|
app_name = self._determine_app_name(app_name)
instance-attribute
__init__(app_name: str | None = None, custom_base_dir: str | Path | None = None)
Initializes resource paths with cross-platform compatibility.
Source code in culicidaelab/core/resource_manager.py
__repr__() -> str
temp_workspace(prefix: str = 'workspace', suffix: str = '')
A context manager for temporary workspaces that auto-cleans on exit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
A prefix for the temporary directory name. |
'workspace'
|
suffix
|
str
|
A suffix for the temporary directory name. |
''
|
Yields:
Name | Type | Description |
---|---|---|
Path |
The path to the temporary workspace. |
Example
with resource_manager.temp_workspace("processing") as ws: ... # Use ws for temporary operations ... (ws / "temp.txt").write_text("data")
The workspace is automatically removed here.
Source code in culicidaelab/core/resource_manager.py
clean_old_files(days: int = 5, include_cache: bool = True) -> dict[str, int]
Cleans up old download and temporary files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
days
|
int
|
The number of days after which files are considered old. |
5
|
include_cache
|
bool
|
Whether to include the cache directory in cleanup. |
True
|
Returns:
Type | Description |
---|---|
dict[str, int]
|
dict[str, int]: A dictionary with cleanup statistics. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in culicidaelab/core/resource_manager.py
clean_temp_workspace(workspace_path: Path, force: bool = False) -> None
Cleans up a temporary workspace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workspace_path
|
Path
|
The path to the workspace to clean. |
required |
force
|
bool
|
If True, force remove even if not in a temp directory. |
False
|
Raises:
Type | Description |
---|---|
ResourceManagerError
|
If cleanup fails. |
ValueError
|
If the workspace is outside the temp dir and |
Source code in culicidaelab/core/resource_manager.py
create_checksum(file_path: str | Path, algorithm: str = 'md5') -> str
Creates a checksum for a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str | Path
|
The path to the file. |
required |
algorithm
|
str
|
The hashing algorithm to use ('md5', 'sha1', 'sha256'). |
'md5'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The hexadecimal checksum string. |
Raises:
Type | Description |
---|---|
ResourceManagerError
|
If the file does not exist or creation fails. |
Source code in culicidaelab/core/resource_manager.py
create_temp_workspace(prefix: str = 'workspace', suffix: str = '') -> Path
Creates a temporary workspace for runtime operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
A prefix for the temporary directory name. |
'workspace'
|
suffix
|
str
|
A suffix for the temporary directory name. |
''
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The path to the created temporary workspace. |
Raises:
Type | Description |
---|---|
ResourceManagerError
|
If workspace creation fails. |
Source code in culicidaelab/core/resource_manager.py
get_all_directories() -> dict[str, Path]
Gets all managed directories.
Returns:
Type | Description |
---|---|
dict[str, Path]
|
dict[str, Path]: A dictionary mapping directory names to their paths. |
Source code in culicidaelab/core/resource_manager.py
get_cache_path(cache_name: str, create_if_missing: bool = True) -> Path
Gets a path for cache files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_name
|
str
|
The name of the cache. |
required |
create_if_missing
|
bool
|
Whether to create the directory if it doesn't exist. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The path to the cache directory. |
Source code in culicidaelab/core/resource_manager.py
get_dataset_path(dataset_name: str, create_if_missing: bool = True) -> Path
Gets a standardized path for a specific dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name
|
str
|
The name of the dataset. |
required |
create_if_missing
|
bool
|
Whether to create the directory if it doesn't exist. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The absolute path to the dataset directory. |
Source code in culicidaelab/core/resource_manager.py
get_disk_usage() -> dict[str, dict[str, int | str]]
Gets disk usage statistics for all managed directories.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict[str, dict[str, int | str]]
|
A dictionary with disk usage information for each directory. |
Source code in culicidaelab/core/resource_manager.py
get_model_path(model_name: str, create_if_missing: bool = True) -> Path
Gets a standardized path for a specific model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The name of the model. |
required |
create_if_missing
|
bool
|
Whether to create the directory if it doesn't exist. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The absolute path to the model directory. |
Source code in culicidaelab/core/resource_manager.py
verify_checksum(file_path: str | Path, expected_checksum: str, algorithm: str = 'md5') -> bool
Verifies a file's checksum.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str | Path
|
The path to the file. |
required |
expected_checksum
|
str
|
The expected checksum value. |
required |
algorithm
|
str
|
The hashing algorithm used. |
'md5'
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the checksum matches, False otherwise. |
Source code in culicidaelab/core/resource_manager.py
ResourceManagerError
Settings
User-friendly facade for CulicidaeLab configuration management.
This class provides a simple, stable interface to access configuration values, resource directories, and application settings. All actual operations are delegated to a validated configuration object managed by ConfigManager and a ResourceManager.
Source code in culicidaelab/core/settings.py
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 |
|
config: CulicidaeLabConfig = self._config_manager.get_config()
instance-attribute
model_dir: Path
property
Model weights directory.
weights_dir: Path
property
Alias for model_dir.
dataset_dir: Path
property
Datasets directory.
cache_dir: Path
property
Cache directory.
config_dir: Path
property
The active user configuration directory.
species_config: SpeciesConfig
property
Species configuration (lazily loaded).
__init__(config_dir: str | Path | None = None) -> None
Initializes the Settings facade.
This loads the configuration using a ConfigManager and sets up a ResourceManager for file paths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_dir
|
str | Path | None
|
Optional path to a user-provided configuration directory. |
None
|
Source code in culicidaelab/core/settings.py
get_config(path: str | None = None, default: Any = None) -> Any
Gets a configuration value using a dot-separated path.
Example
settings.get_config("predictors.classifier.confidence")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | None
|
A dot-separated string path to the configuration value. If None, returns the entire configuration object. |
None
|
default
|
Any
|
A default value to return if the path is not found. |
None
|
Returns:
Type | Description |
---|---|
Any
|
The configuration value, or the default value if not found. |
Source code in culicidaelab/core/settings.py
set_config(path: str, value: Any) -> None
Sets a configuration value at a specified dot-separated path. This method can traverse both objects (Pydantic models) and dictionaries.
Note: This modifies the configuration in memory. To make it persistent,
call save_config()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
A dot-separated string path to the configuration value. |
required |
value
|
Any
|
The new value to set. |
required |
Source code in culicidaelab/core/settings.py
save_config(file_path: str | Path | None = None) -> None
Save current configuration to a user config file. Args: file_path: Optional path to save the configuration file. If None, defaults to "culicidaelab_saved.yaml" in the user config directory.
Source code in culicidaelab/core/settings.py
get_dataset_path(dataset_type: str) -> Path
Gets the standardized path for a specific dataset directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_type
|
str
|
The name of the dataset type (e.g., 'classification'). |
required |
Returns:
Type | Description |
---|---|
Path
|
An absolute path to the dataset directory. |
Source code in culicidaelab/core/settings.py
list_datasets() -> list[str]
get_model_weights_path(model_type: str) -> Path
Gets the configured path to a model's weights file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_type
|
str
|
The name of the model type (e.g., 'classifier'). |
required |
Returns:
Type | Description |
---|---|
Path
|
The path to the model weights file. |
Source code in culicidaelab/core/settings.py
list_model_types() -> list[str]
set_model_weights_path(model_type: str, weights_path: str | Path) -> None
Set custom weights path for model type. Args: model_type: The name of the model type (e.g., 'classifier'). weights_path: The new path to the model weights file.
Source code in culicidaelab/core/settings.py
get_api_key(provider: str) -> str | None
Get API key for external provider from environment variables. Args: provider: The name of the provider (e.g., 'kaggle', 'huggingface', 'roboflow').
Source code in culicidaelab/core/settings.py
temp_workspace(prefix: str = 'workspace')
instantiate_from_config(config_path: str, **kwargs: Any) -> Any
Instantiates an object from a configuration path.
This is a convenience method that finds a config object by its path and uses the underlying ConfigManager to instantiate it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path
|
str
|
A dot-separated path to the configuration object (e.g., "predictors.classifier"). |
required |
**kwargs
|
Any
|
Additional keyword arguments to pass to the constructor. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
The instantiated object. |
Source code in culicidaelab/core/settings.py
get_settings(config_dir: str | Path | None = None) -> Settings
Get the Settings singleton instance.
This is the primary way to access Settings throughout the application.
If a config_dir
is provided that differs from the existing instance,
a new instance will be created and returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_dir
|
str | Path | None
|
Optional path to a user-provided configuration directory. |
None
|
Returns:
Type | Description |
---|---|
Settings
|
The Settings instance. |
Source code in culicidaelab/core/settings.py
download_file(url: str, destination: str | Path | None = None, downloads_dir: str | Path | None = None, progress_callback: Callable | None = None, chunk_size: int = 8192, timeout: int = 30, desc: str | None = None) -> Path
Downloads a file from a URL with progress tracking.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL of the file to download. |
required |
destination
|
str | Path
|
The specific destination path for the file. |
None
|
downloads_dir
|
str | Path
|
Default directory for downloads. |
None
|
progress_callback
|
Callable
|
A custom progress callback. |
None
|
chunk_size
|
int
|
The size of chunks to download in bytes. |
8192
|
timeout
|
int
|
The timeout for the download request in seconds. |
30
|
desc
|
str
|
A description for the progress bar. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The path to the downloaded file. |
Raises:
Type | Description |
---|---|
ValueError
|
If the URL is invalid. |
RuntimeError
|
If the download or file write fails. |
Source code in culicidaelab/core/utils.py
str_to_bgr(str_color: str) -> tuple[int, int, int]
Converts a hexadecimal color string to a BGR tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
str_color
|
str
|
A hex color string in '#RRGGBB' or 'RRGGBB' format. |
required |
Returns:
Type | Description |
---|---|
tuple[int, int, int]
|
tuple[int, int, int]: A (B, G, R) tuple of integers. |
Raises:
Type | Description |
---|---|
ValueError
|
If the string has an invalid format or invalid characters. |