Skip to content

Commit

Permalink
Minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
TLCFEM committed Mar 20, 2024
1 parent 95b9e07 commit b05ef0e
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/mb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/mb/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/mb/app/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
12 changes: 5 additions & 7 deletions src/mb/record/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -156,19 +156,17 @@ 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]


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.")
Expand Down
4 changes: 1 addition & 3 deletions src/mb/record/response_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 5 additions & 9 deletions src/mb/record/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
7 changes: 4 additions & 3 deletions src/mb/utility/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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]
)

0 comments on commit b05ef0e

Please sign in to comment.