Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print to logger #448

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
dd2414b
Adding in `logger` commands instead of `print`
kallewesterling Jun 26, 2024
31eb638
Adding in clarifying comments
kallewesterling Jun 26, 2024
042c969
Adding in more clarifying comments
kallewesterling Jun 26, 2024
cc5a1ca
One last comment
kallewesterling Jun 26, 2024
75b810c
Dropping `cprint`
kallewesterling Jul 25, 2024
acc1a49
Replacing remaining relevant print statements with logger.info/logger…
kallewesterling Jul 25, 2024
8b4bfca
Fixing more print statements and testing side of things
kallewesterling Jul 26, 2024
2fbe079
merge rw changes
rwood-97 Jul 26, 2024
9642558
one extra fix
rwood-97 Jul 26, 2024
6a938cb
Merge branch 'main' into kallewesterling/issue27
kallewesterling Jul 26, 2024
4409edb
Adding a logger to base class Runner
kallewesterling Jul 26, 2024
638892a
Adding the correct URLs (see also #465)
kallewesterling Jul 26, 2024
a557f3f
Merge branch 'kallewesterling/issue27' of https://github.com/Living-w…
rwood-97 Aug 2, 2024
6722623
Merge branch 'main' into kallewesterling/issue27
rwood-97 Aug 2, 2024
181eebb
Merge branch 'main' into kallewesterling/issue27
rwood-97 Aug 2, 2024
bf79cff
delete test files
rwood-97 Aug 2, 2024
3a7d959
Resolving conflicts and fixing test errors
kallewesterling Aug 2, 2024
9a7e36a
adding temp checkpoints for tests to gitignore
kallewesterling Aug 2, 2024
7c6d1e6
Merge branch 'kallewesterling/issue27' into rw/issue27
kallewesterling Aug 2, 2024
b392189
fix missing "type" arguments
rwood-97 Aug 5, 2024
64c59ef
Update test_sheet_downloader.py
rwood-97 Aug 5, 2024
4f01220
Update test_sheet_downloader.py
rwood-97 Aug 8, 2024
bdb55d5
Update test_sheet_downloader.py
rwood-97 Aug 8, 2024
15e664c
Merge pull request #473 from Living-with-machines/rw/issue27
rwood-97 Aug 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ worked_examples/**/workshops_*/**/*.png
worked_examples/**/workshops_*/**/*.csv
worked_examples/**/workshops_*/**/*.geojson
worked_examples/**/workshops_*/**/*.xlsx

# test outputs
tmp_checkpoints
17 changes: 11 additions & 6 deletions mapreader/annotate/annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import functools
import hashlib
import json
import logging
import os
import random
import string
Expand All @@ -20,12 +21,16 @@

from ..load.loader import load_patches

# Ignore warnings
warnings.filterwarnings("ignore", category=UserWarning)

_CENTER_LAYOUT = widgets.Layout(
display="flex", flex_flow="column", align_items="center"
)

# Set up logging
logger = logging.getLogger(__name__)


class Annotator:
"""
Expand Down Expand Up @@ -218,7 +223,7 @@

# Test for existing patch annotation file
if os.path.exists(annotations_file):
print("[INFO] Loading existing patch annotations.")
logger.info("Loading existing patch annotations.")

Check warning on line 226 in mapreader/annotate/annotator.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/annotator.py#L226

Added line #L226 was not covered by tests
patch_df = self._load_annotations(
patch_df=patch_df,
annotations_file=annotations_file,
Expand Down Expand Up @@ -330,20 +335,20 @@
A tuple containing the parent dataframe and patch dataframe.
"""
if patch_paths:
print(f"[INFO] Loading patches from {patch_paths}.")
logger.info(f"Loading patches from {patch_paths}.")
if parent_paths:
print(f"[INFO] Loading parents from {parent_paths}.")
logger.info(f"Loading parents from {parent_paths}.")

maps = load_patches(patch_paths=patch_paths, parent_paths=parent_paths)
# Add pixel stats
maps.calc_pixel_stats()

try:
maps.add_metadata(metadata_path, delimiter=delimiter)
print(f"[INFO] Adding metadata from {metadata_path}.")
logger.info(f"Adding metadata from {metadata_path}.")
except ValueError:
raise FileNotFoundError(
f"[INFO] Metadata file at {metadata_path} not found. Please specify the correct file path using the ``metadata_path`` argument."
f"[ERROR] Metadata file at {metadata_path} not found. Please specify the correct file path using the ``metadata_path`` argument."
)

parent_df, patch_df = maps.convert_images()
Expand Down Expand Up @@ -745,7 +750,7 @@
self._queue = self.get_queue()

if self._filter_for is not None:
print(f"[INFO] Filtering for: {self._filter_for}")
logger.info(f"Filtering for: {self._filter_for}")

Check warning on line 753 in mapreader/annotate/annotator.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/annotator.py#L753

Added line #L753 was not covered by tests

self.out = widgets.Output(layout=_CENTER_LAYOUT)
display(self.box)
Expand Down
38 changes: 21 additions & 17 deletions mapreader/annotate/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
from __future__ import annotations

import logging

Check warning on line 4 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L4

Added line #L4 was not covered by tests
import os
import random
import sys
Expand All @@ -25,10 +26,14 @@

from mapreader import load_patches, loader

# Ignore warnings
warnings.filterwarnings("ignore")
# warnings.filterwarnings(
# (
# "ignore", message="Pandas doesn't allow columns to be created via a new attribute name")

# Set up logging
logger = logging.getLogger(__name__)

Check warning on line 35 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L35

Added line #L35 was not covered by tests


def prepare_data(
df: pd.DataFrame,
Expand Down Expand Up @@ -77,10 +82,10 @@
col_names = ["image_path", "parent_id"]
if (label_col_name in list(df.columns)) and (not redo):
already_annotated = len(df[~df[label_col_name].isnull()])
print(f"Number of already annotated images: {already_annotated}")
logger.info(f"Number of already annotated images: {already_annotated}")

Check warning on line 85 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L85

Added line #L85 was not covered by tests
# only annotate those patches that have not been already annotated
df = df[df[label_col_name].isnull()]
print(f"Number of images to be annotated (total): {len(df)}")
logger.info(f"Number of images to be annotated (total): {len(df)}")

Check warning on line 88 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L88

Added line #L88 was not covered by tests
else:
# if redo = True or "label" column does not exist
# annotate all patches in the pandas dataframe
Expand All @@ -100,10 +105,10 @@
else:
df = df.groupby("pixel_groups").sample(n=10, random_state=random_state)
except Exception:
print(f"[INFO] len(df) = {len(df)}, .sample method is deactivated.")
logger.info(f"len(df) = {len(df)}, .sample method is deactivated.")

Check warning on line 108 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L108

Added line #L108 was not covered by tests
df = df.iloc[:num_samples]
else:
print(f"[WARNING] could not find {tar_param} in columns.")
logger.warning(f"could not find {tar_param} in columns.")

Check warning on line 111 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L111

Added line #L111 was not covered by tests
df = df.iloc[:num_samples]

data = []
Expand All @@ -117,7 +122,7 @@
data.append(cols2add)
row_counter += 1

print(f"Number of images to annotate (current batch): {len(data)}")
logger.info(f"Number of images to annotate (current batch): {len(data)}")

Check warning on line 125 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L125

Added line #L125 was not covered by tests
return data


Expand Down Expand Up @@ -300,9 +305,9 @@
plt.tight_layout()
plt.show()

print(20 * "-")
print("Additional info:")
print(f"Counter: {record[-1]}")
logger.info(20 * "-")
logger.info("Additional info:")
logger.info(f"Counter: {record[-1]}")

Check warning on line 310 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L308-L310

Added lines #L308 - L310 were not covered by tests
if url_main:
try:
map_id = record[2].split("_")[-1].split(".")[0]
Expand All @@ -311,8 +316,7 @@
# the page exists
response = requests.get(url, stream=True)
assert response.status_code < 400
print()
print(f"URL: {url}")
logger.info(f"URL: {url}")

Check warning on line 319 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L319

Added line #L319 was not covered by tests
except:
url = False
pass
Expand Down Expand Up @@ -619,7 +623,7 @@
)

if len(data2annotate) == 0:
print("No image to annotate!")
logger.info("No image to annotate!")

Check warning on line 626 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L626

Added line #L626 was not covered by tests
else:
annotation = annotation_interface(
data2annotate,
Expand Down Expand Up @@ -665,7 +669,7 @@
annotation_tasks = yaml.load(f, Loader=yaml.FullLoader)

if annotation_set not in annotation_tasks["paths"].keys():
print(f"{annotation_set} could not be found in {annotation_tasks_file}")
logger.info(f"{annotation_set} could not be found in {annotation_tasks_file}")

Check warning on line 672 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L672

Added line #L672 was not covered by tests
else:
annot_file = os.path.join(
annotation_tasks["paths"][annotation_set]["annot_dir"],
Expand Down Expand Up @@ -704,8 +708,8 @@
if len(image_df) > 0:
# image_df = image_df.set_index("image_id")
image_df.to_csv(annot_file, mode="w")
print(f"[INFO] Save {newly_annotated} new annotations to {annot_file}")
print(f"[INFO] {new_labels} labels were not already stored")
print(f"[INFO] Total number of saved annotations: {len(image_df)}")
logger.info(f"Save {newly_annotated} new annotations to {annot_file}")
logger.info(f"{new_labels} labels were not already stored")
logger.info(f"Total number of saved annotations: {len(image_df)}")

Check warning on line 713 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L711-L713

Added lines #L711 - L713 were not covered by tests
else:
print("[INFO] No annotations to save!")
logger.info("No annotations to save!")

Check warning on line 715 in mapreader/annotate/utils.py

View check run for this annotation

Codecov / codecov/patch

mapreader/annotate/utils.py#L715

Added line #L715 was not covered by tests
Loading
Loading