Skip to content

Commit

Permalink
Merge pull request #376 from LAAC-LSCP/rework_metrics
Browse files Browse the repository at this point in the history
Rework metrics
  • Loading branch information
LoannPeurey authored Jul 25, 2022
2 parents afcd554 + d7592cf commit 4cc4f43
Show file tree
Hide file tree
Showing 24 changed files with 24,480 additions and 755 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ docs/PROJECTS.md

# vim
*~

# sphinx created csvs
docs/source/*.csv
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- "3.6"
- "3.7"
- "3.8"
- "3.9"
Expand Down
38 changes: 18 additions & 20 deletions ChildProject/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .projects import ChildProject
from .converters import *
from .tables import IndexTable, IndexColumn, assert_dataframe, assert_columns_presence
from .utils import Segment, intersect_ranges, path_is_parent
from .utils import Segment, intersect_ranges, path_is_parent, TimeInterval


class AnnotationManager:
Expand Down Expand Up @@ -1196,7 +1196,7 @@ def get_within_ranges(
return pd.concat(stack) if len(stack) else pd.DataFrame()

def get_within_time_range(
self, annotations: pd.DataFrame, start_time: str, end_time: str, errors="raise"
self, annotations: pd.DataFrame, interval : TimeInterval, errors="raise"
):
"""Clip all input annotations within a given HH:MM clock-time range.
Those that do not intersect the input time range at all are filtered out.
Expand Down Expand Up @@ -1227,24 +1227,12 @@ def get_within_time_range(
def get_ms_since_midight(dt):
return (dt - dt.replace(hour=0, minute=0, second=0)).total_seconds() * 1000

try:
start_dt = datetime.datetime.strptime(start_time, "%H:%M")
except:
raise ValueError(
f"invalid value for start_time ('{start_time}'); should have HH:MM format instead"
)

try:
end_dt = datetime.datetime.strptime(end_time, "%H:%M")
except:
raise ValueError(
f"invalid value for end_time ('{end_time}'); should have HH:MM format instead"
)

assert end_dt > start_dt, "end_time must follow start_time"
#assert end_dt > start_dt, "end_time must follow start_time"
# no reason to keep this condition, 23:00 to 03:00 is completely acceptable

start_ts = get_ms_since_midight(start_dt)
end_ts = get_ms_since_midight(end_dt)
if not isinstance(interval, TimeInterval): raise ValueError("interval must be a TimeInterval object")
start_ts = get_ms_since_midight(interval.start)
end_ts = get_ms_since_midight(interval.stop)

annotations = annotations.merge(
self.project.recordings[["recording_filename", "start_time"]], how="left"
Expand Down Expand Up @@ -1272,8 +1260,18 @@ def get_ms_since_midight(dt):

matches = []
for annotation in annotations.to_dict(orient="records"):
#onsets = np.arange(start_ts, annotation["range_offset_ts"], 86400 * 1000)
#offsets = onsets + (end_ts - start_ts)

onsets = np.arange(start_ts, annotation["range_offset_ts"], 86400 * 1000)
offsets = onsets + (end_ts - start_ts)
offsets = np.arange(end_ts, annotation["range_offset_ts"], 86400 * 1000)
#treat edge cases when the offset is after the end of annotation, onset before start etc
if len(onsets) > 0 and onsets[0] < annotation["range_onset_ts"] :
if len(offsets) > 0 and offsets[0] < annotation["range_onset_ts"]: onsets = onsets[1:]
else : onsets[0] = annotation["range_onset_ts"]
if len(offsets) > 0 and offsets[0] < annotation["range_onset_ts"] : offsets = offsets[1:]
if len(onsets) > 0 and len(offsets) > 0 and onsets[0] > offsets[0] : onsets = np.append(annotation["range_onset_ts"], onsets)
if (len(onsets) > 0 and len(offsets) > 0 and onsets[-1] > offsets[-1]) or len(onsets) > len(offsets) : offsets = np.append(offsets,annotation["range_offset_ts"])

xs = (Segment(onset, offset) for onset, offset in zip(onsets, offsets))
ys = iter(
Expand Down
1 change: 1 addition & 0 deletions ChildProject/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ def main():
register_pipeline("eaf-builder", EafBuilderPipeline)
register_pipeline("anonymize", AnonymizationPipeline)
register_pipeline("metrics", MetricsPipeline)
register_pipeline("metrics-specification", MetricsSpecificationPipeline)

args = parser.parse_args()
args.func(args)
1 change: 1 addition & 0 deletions ChildProject/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from .eafbuilder import EafBuilderPipeline
from .zooniverse import ZooniversePipeline
from .metrics import MetricsPipeline
from .metrics import MetricsSpecificationPipeline
from .processors import AudioProcessingPipeline
from .anonymize import AnonymizationPipeline
Loading

0 comments on commit 4cc4f43

Please sign in to comment.