-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New kinetic models, bug fixes and refactoring (#176)
* Fix `path` wrongly converted to lower case (Fix #175) * Update the calculation of propagators This is to match the way stacked matrices are dealt with in SciPy. When there is only one delay to compute, the function `scipy.linalg.expm` is used instead of the diagonalization approach. * Use `NonNegativeFloat` instead of `PositiveFloat` This corrects a bug where `0.0` was not an acceptable value for `h_larmor_frq`, `p_total`, and `l_total`. * Add new kinetic models for various oligomerization and binding reactions. * Bump packages versions to the latest * Remove pre-commit * Add docstrings and refactor for code maintenance * Forbid unknown options in method files * Refactor "spin_system.py" into a package * Refactor "pick_cest.py" into a package * Add 1H EXSY experiment
- Loading branch information
Guillaume Bouvignies
authored
Nov 28, 2023
1 parent
55d1f7d
commit fddcb7e
Showing
108 changed files
with
4,571 additions
and
2,088 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,76 @@ | ||
"""Utility module for list and string manipulation in Pydantic models. | ||
This module provides utility functions for ensuring a variable is in list format and | ||
for converting strings to lowercase. It also includes a Pydantic BaseModel subclass | ||
which applies these utilities to convert all keys in a model to lowercase. | ||
Typical usage example: | ||
variable = "Sample" | ||
variable_list = ensure_list(variable) | ||
lower_variable = to_lower("HELLO") | ||
model_instance = BaseModelLowerCase.parse_obj({"Name": "Alice"}) | ||
""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
from typing import TypeVar, cast | ||
|
||
from pydantic import BaseModel, model_validator | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def ensure_list(variable: T | list[T] | None) -> T | list[T]: | ||
"""Ensures that the input variable is returned as a list. | ||
from pydantic import BaseModel, ConfigDict, model_validator | ||
If the input variable is already a list, it is returned as-is. | ||
If the variable is None, an empty list is returned. | ||
Otherwise, the variable is wrapped in a list and returned. | ||
Parameters: | ||
variable (T | list[T] | None): The variable to be ensured as a list. | ||
def ensure_list(variable: Any | list[Any] | None) -> list[Any]: | ||
Returns: | ||
T | list[T]: The input variable as a list. | ||
""" | ||
if isinstance(variable, list): | ||
return variable | ||
return cast(list[T], variable) | ||
if variable is None: | ||
return [] | ||
return [variable] | ||
|
||
|
||
def to_lower(string: Any) -> Any: | ||
def to_lower(string: T) -> T: | ||
"""Converts a string to lowercase if it is of type str. | ||
If the input is not a string, it is returned unchanged. | ||
Parameters: | ||
string (T): The string to be converted to lowercase. | ||
Returns: | ||
T: The lowercase string if input is a string; otherwise, the original input. | ||
""" | ||
if isinstance(string, str): | ||
return string.lower() | ||
return string | ||
|
||
|
||
class BaseModelLowerCase(BaseModel): | ||
model_config = ConfigDict(str_to_lower=True) | ||
"""A Pydantic BaseModel class that converts all keys in the model to lowercase. | ||
This is achieved using a model validator that operates before model initialization. | ||
The validator applies the `to_lower` function to each key of the model. | ||
""" | ||
|
||
@model_validator(mode="before") | ||
def key_to_lower(cls, model: dict[str, Any]) -> dict[str, Any]: | ||
def key_to_lower(cls, model: dict[str, T]) -> dict[str, T]: | ||
"""Model validator to convert all dictionary keys to lowercase. | ||
Parameters: | ||
model (dict[str, T]): The dictionary model with string keys. | ||
Returns: | ||
dict[str, T]: The modified dictionary with all keys in lowercase. | ||
""" | ||
return {to_lower(k): v for k, v in model.items()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.