From b05ef0e54d73272419c153127b27c23d108b88a8 Mon Sep 17 00:00:00 2001 From: Theodore Chang Date: Wed, 20 Mar 2024 01:16:51 +0100 Subject: [PATCH] Minor update --- src/mb/__init__.py | 2 +- src/mb/app/main.py | 2 +- src/mb/app/utility.py | 2 +- src/mb/record/parser.py | 12 +++++------- src/mb/record/response_spectrum.py | 4 +--- src/mb/record/utility.py | 14 +++++--------- src/mb/utility/config.py | 7 ++++--- 7 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/mb/__init__.py b/src/mb/__init__.py index 9eaa4d3..8a53ec2 100644 --- a/src/mb/__init__.py +++ b/src/mb/__init__.py @@ -32,7 +32,7 @@ def run_app(**kwargs): config["host"] = kwargs["host"] if not load_dotenv(os.path.join(os.path.dirname(__file__), ".env")): - raise RuntimeError("No .env file found") + raise RuntimeError("No .env file found.") uvicorn.run("mb.app.main:app", **config) diff --git a/src/mb/app/main.py b/src/mb/app/main.py index e68ce9c..d14e1bd 100644 --- a/src/mb/app/main.py +++ b/src/mb/app/main.py @@ -130,7 +130,7 @@ async def get_random_record() -> Record: if result: return result[0] - raise HTTPException(HTTPStatus.NO_CONTENT, detail="Record not found") + raise HTTPException(HTTPStatus.NO_CONTENT, detail="Record not found.") @app.get("/raw/jackpot", response_model=Record) diff --git a/src/mb/app/utility.py b/src/mb/app/utility.py index 95ee9c0..d99b130 100644 --- a/src/mb/app/utility.py +++ b/src/mb/app/utility.py @@ -125,7 +125,7 @@ async def current_user(token: str = Depends(OAUTH2)): async def is_active(user: User = Depends(current_user)): if user.disabled: - raise HTTPException(HTTPStatus.BAD_REQUEST, detail="Inactive user") + raise HTTPException(HTTPStatus.BAD_REQUEST, detail="Inactive user.") return user diff --git a/src/mb/record/parser.py b/src/mb/record/parser.py index 6e02421..7c0fbd2 100644 --- a/src/mb/record/parser.py +++ b/src/mb/record/parser.py @@ -125,9 +125,9 @@ async def parse_file(file_path: str | IO[bytes]) -> NIED: def _parse_unit(line: str) -> str: matches = re.findall(r"\(([^)]+)\)", line) if len(matches) == 0: - raise ValueError(f"No unit found in line: {line}") + raise ValueError(f"No unit found in line: {line}.") if len(matches) > 1: - raise ValueError(f"Multiple units found in line: {line}") + raise ValueError(f"Multiple units found in line: {line}.") return matches[0] @@ -156,9 +156,9 @@ def _parse_direction(line: str) -> str: def _parse_value(line: str) -> str: matches = re.findall(r"([0-9.]+)", line) if len(matches) == 0: - raise ValueError(f"No value found in line: {line}") + raise ValueError(f"No value found in line: {line}.") if len(matches) > 1: - raise ValueError(f"Multiple values found in line: {line}") + raise ValueError(f"Multiple values found in line: {line}.") return matches[0] @@ -166,9 +166,7 @@ class ParserNZSM: @staticmethod def validate_file(file_path: str): lower_path = file_path.lower() - if lower_path.endswith(".v2a"): - return - if lower_path.endswith(".v1a"): + if lower_path.endswith((".v2a", ".v1a")): return raise ValueError("NZSM archive file should be a V2A/V1A file.") diff --git a/src/mb/record/response_spectrum.py b/src/mb/record/response_spectrum.py index 4877741..8312946 100644 --- a/src/mb/record/response_spectrum.py +++ b/src/mb/record/response_spectrum.py @@ -107,9 +107,7 @@ def compute_task(p): oscillator = Oscillator(2 * np.pi / p, damping_ratio) return oscillator.compute_maximum_response(interval, motion) - spectrum = np.array(Parallel(n_jobs=os.cpu_count(), prefer="threads")(delayed(compute_task)(p) for p in period)) - - return spectrum + return np.array(Parallel(n_jobs=os.cpu_count(), prefer="threads")(delayed(compute_task)(p) for p in period)) def sdof_response(damping_ratio: float, interval: float, freq: float, motion: np.ndarray) -> np.ndarray: diff --git a/src/mb/record/utility.py b/src/mb/record/utility.py index 10a28dc..e2240c6 100644 --- a/src/mb/record/utility.py +++ b/src/mb/record/utility.py @@ -33,7 +33,7 @@ def normalise(magnitude: np.ndarray) -> np.ndarray: return magnitude -def convert_to(quantity: pint.Quantity, unit: pint.Unit): +def convert_to(quantity: pint.Quantity, unit: pint.Unit | None): return quantity.to(unit).magnitude if unit else quantity.magnitude @@ -66,14 +66,10 @@ def get_window(filter_type: str, window_type: str, length: int, cutoff: float | elif window_type == "hamming": window = ("hamming",) elif window_type == "kaiser": - beta = kwargs.get("beta", 9) - window = ("kaiser", beta) + window = ("kaiser", kwargs.get("beta", 9)) elif window_type == "chebwin": - at = kwargs.get("at", 80) - window = ("chebwin", at) + window = ("chebwin", kwargs.get("at", 80)) else: - raise ValueError(f"Unknown window type: {window_type}") + raise ValueError(f"Unknown window type: {window_type}.") - bin_num = 2 * length + 1 - - return signal.firwin(bin_num, cutoff, window=window, pass_zero=filter_type) * kwargs.get("ratio", 1) + return signal.firwin(2 * length + 1, cutoff, window=window, pass_zero=filter_type) * kwargs.get("ratio", 1) diff --git a/src/mb/utility/config.py b/src/mb/utility/config.py index 3f1726d..1c5933b 100644 --- a/src/mb/utility/config.py +++ b/src/mb/utility/config.py @@ -25,7 +25,7 @@ def mongo_uri(): if not load_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), ".env")): - raise RuntimeError("No .env file found") + raise RuntimeError("No .env file found.") username: str = os.getenv("MONGO_USERNAME") password: str = os.getenv("MONGO_PASSWORD") host: str = os.getenv("MONGO_HOST") @@ -34,5 +34,6 @@ def mongo_uri(): async def init_mongo(): - client = AsyncIOMotorClient(mongo_uri()) - await init_beanie(database=client["StrongMotion"], document_models=[Record, User, UploadTask]) + await init_beanie( + database=AsyncIOMotorClient(mongo_uri())["StrongMotion"], document_models=[Record, User, UploadTask] + )