Skip to content

Commit

Permalink
changed the check of the start, end to utils, and added the conversio…
Browse files Browse the repository at this point in the history
…n to datetime in case it's a datetime.date object
  • Loading branch information
cparcerisas committed Jan 20, 2025
1 parent a618809 commit 654d012
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pbp/meta_gen/gen_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(
:return:
"""
try:
start, end = utils.check_start_end_args(start, end)
self.audio_loc = audio_loc
self.json_base_dir = json_base_dir
self.df = pd.DataFrame()
Expand Down Expand Up @@ -106,6 +107,7 @@ def __init__(
:return:
"""
try:
start, end = utils.check_start_end_args(start, end)
self.audio_loc = audio_loc
self.json_base_dir = json_base_dir
self.df = pd.DataFrame()
Expand Down
37 changes: 33 additions & 4 deletions pbp/meta_gen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
from typing import Tuple, List
from urllib.parse import urlparse
from datetime import datetime
import datetime
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -90,7 +90,7 @@ def get_datetime(time_str: str, prefixes: List[str]):
]
for fmt in possible_dt_formats:
try:
return datetime.strptime(time_str, fmt)
return datetime.datetime.strptime(time_str, fmt)
except ValueError:
continue

Expand All @@ -101,8 +101,8 @@ def plot_daily_coverage(
instrument_type: InstrumentType,
df: pd.DataFrame,
base_dir: str,
start: datetime,
end: datetime,
start: datetime.datetime,
end: datetime.datetime,
) -> str:
"""
Plot the daily coverage of the recordings
Expand Down Expand Up @@ -175,3 +175,32 @@ def plot_daily_coverage(
fig.savefig(plot_file.as_posix(), dpi=DEFAULT_DPI, bbox_inches="tight")
plt.close(fig)
return plot_file.as_posix()


def check_start_end_args(start, end):
"""
Check if start and end dates are at 00:00:00. If it's a datetime.date object, convert to datetime.datetime object.
:param start The start date of the recordings
:param end The end date of the recordings
:return The corrected start and end date of the recordings as datetime.datetime objects
"""
if type(start) is datetime.date:
start = datetime.datetime(start.year, start.month, start.day)
if type(end) is datetime.date:
end = datetime.datetime(end.year, end.month, end.day)

if type(start) is datetime.datetime:
if start.hour == 0 and start.minute == 0 and start.second == 0:
pass
else:
raise ValueError("Start must be of type datetime.date or a datetime.datetime object at 00:00:00. "
"Otherwise that would be the start of the HMD computation.")

if type(end) is datetime.datetime:
if end.hour == 0 and end.minute == 0 and end.second == 0:
pass
else:
raise ValueError("End must be of type datetime.date or a datetime.datetime object at 00:00:00. "
"Otherwise that would be the start of the HMD computation.")

return start, end

0 comments on commit 654d012

Please sign in to comment.