Skip to content

Commit

Permalink
Refactoring to composition
Browse files Browse the repository at this point in the history
  • Loading branch information
Femme Phile committed Nov 12, 2023
1 parent 43584bc commit 4fa1ca7
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/cleanvision/videolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def sample(self, video_file: Path, output_dir: Path) -> None:
frame_pil.save(sample_sub_dir / image_file_name)

Check warning on line 52 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L52

Added line #L52 was not covered by tests


class Videolab(Imagelab):
class Videolab:

Check warning on line 55 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L55

Added line #L55 was not covered by tests
"""A single class to find all types of issues in video datasets."""

def __init__(

Check warning on line 58 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L58

Added line #L58 was not covered by tests
Expand Down Expand Up @@ -86,7 +86,7 @@ def _parent_dir_frame_samples_dict(self) -> Dict[str, List[str]]:
cluster_frame_samples: Dict[str, List[str]] = {}

Check warning on line 86 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L86

Added line #L86 was not covered by tests

# looper over index
for img_path in self.issues.index:
for img_path in self.imagelab.issues.index:
# get frame sample parent
sample_dir = Path(img_path).parents[0]

Check warning on line 91 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L91

Added line #L91 was not covered by tests

Expand All @@ -108,7 +108,7 @@ def _parent_dir_frame_samples_dict(self) -> Dict[str, List[str]]:
def _aggregate_issues(self) -> pd.DataFrame:

Check warning on line 108 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L108

Added line #L108 was not covered by tests
"""Aggregate Imagelab issues into a single frame for each video."""
# convert booleans to floats
pure_float_issues = self.issues * 1
pure_float_issues = self.imagelab.issues * 1

Check warning on line 111 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L111

Added line #L111 was not covered by tests

# store new aggregate_issues
aggregate_issues = []

Check warning on line 114 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L114

Added line #L114 was not covered by tests
Expand All @@ -130,7 +130,9 @@ def _aggregate_issues(self) -> pd.DataFrame:
agg_df = pd.concat(aggregate_issues)

Check warning on line 130 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L130

Added line #L130 was not covered by tests

# create lists of columns
issue_columns = [get_is_issue_colname(issue) for issue in self._issue_types]
issue_columns = [
get_is_issue_colname(issue) for issue in self.imagelab._issue_types
]

# convert float represent average booleans back to booleans
agg_df[issue_columns] = agg_df[issue_columns].astype(bool)

Check warning on line 138 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L138

Added line #L138 was not covered by tests
Expand All @@ -144,7 +146,7 @@ def _aggregate_summary(self) -> pd.DataFrame:
summary_dict = {}

Check warning on line 146 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L146

Added line #L146 was not covered by tests

# loop over issue type
for issue_type in self._issue_types:
for issue_type in self.imagelab._issue_types:
# add individual type summaries
summary_dict[issue_type] = {

Check warning on line 151 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L151

Added line #L151 was not covered by tests
"num_images": self.agg_issues[get_is_issue_colname(issue_type)].sum()
Expand All @@ -167,15 +169,15 @@ def find_issues(
n_jobs: Optional[int] = None,
verbose: bool = True,
) -> None:
"""Sampe frames before call Imagelab.find_issues."""
"""Sample frames before calling find_issues and aggregating."""
# create sample frames
self._sample_frames(Path(frame_samples_dir), frame_samples_interval)

Check warning on line 174 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L174

Added line #L174 was not covered by tests

# call parent constructor
super().__init__(frame_samples_dir)
# get imagelab instance
self.imagelab = Imagelab(frame_samples_dir)

Check warning on line 177 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L177

Added line #L177 was not covered by tests

# call parent find_issues on sampled frames
super().find_issues(issue_types, n_jobs, verbose)
# use imagelab to find issues in frames
self.imagelab.find_issues(issue_types, n_jobs, verbose)

Check warning on line 180 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L180

Added line #L180 was not covered by tests

# update aggregate issues/summary
self.agg_issues = self._aggregate_issues()
Expand All @@ -194,14 +196,14 @@ def _aggregate_report(
assert isinstance(verbosity, int) and 0 <= verbosity < 5

user_supplied_args = locals()
report_args = self._get_report_args(user_supplied_args)
report_args = self.imagelab._get_report_args(user_supplied_args)

Check warning on line 199 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L198-L199

Added lines #L198 - L199 were not covered by tests

issue_types_to_report = (

Check warning on line 201 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L201

Added line #L201 was not covered by tests
issue_types if issue_types else self.agg_summary["issue_type"].tolist()
)

# filter issues based on max_prevalence in the dataset
filtered_issue_types = self._filter_report(
filtered_issue_types = self.imagelab._filter_report(

Check warning on line 206 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L206

Added line #L206 was not covered by tests
issue_types_to_report, report_args["max_prevalence"]
)

Expand All @@ -212,7 +214,7 @@ def _aggregate_report(
if verbosity:

Check warning on line 214 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L214

Added line #L214 was not covered by tests
print("Issues found in videos in order of severity in the dataset\n")
if print_summary:
self._pprint_issue_summary(issue_summary)
self.imagelab._pprint_issue_summary(issue_summary)

Check warning on line 217 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L217

Added line #L217 was not covered by tests
for issue_type in filtered_issue_types:
if (
self.agg_summary.query(f"issue_type == {issue_type!r}")[
Expand All @@ -227,7 +229,7 @@ def _aggregate_report(
f"{self.agg_issues[get_is_issue_colname(issue_type)].sum()}\n"
f"Examples representing most severe instances of this issue:\n"
)
self._visualize(
self.imagelab._visualize(

Check warning on line 232 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L232

Added line #L232 was not covered by tests
issue_type,
report_args["num_images"],
report_args["cell_size"],
Expand All @@ -248,7 +250,7 @@ def report(
print_summary: bool = True,
show_id: bool = False,
) -> None:
"""Prints summary of the issues found in your dataset."""
"""Prints summary of the aggregate issues found in your dataset."""
# report on video frame samples
self._aggregate_report(

Check warning on line 255 in src/cleanvision/videolab.py

View check run for this annotation

Codecov / codecov/patch

src/cleanvision/videolab.py#L255

Added line #L255 was not covered by tests
issue_types,
Expand Down

0 comments on commit 4fa1ca7

Please sign in to comment.