Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
5016509
1
Parent(s):
1bbd515
chore: cleaning up InputObservation
Browse filessee issue #27.
- added image_md5 as input (1)
- removed duplicate methods (3)
- removed unused hash func (5)
- checked by inspection the attribute coverage (4) - still to test
- src/input/input_observation.py +75 -25
src/input/input_observation.py
CHANGED
|
@@ -25,6 +25,8 @@ class InputObservation:
|
|
| 25 |
Additional time option for the observation.
|
| 26 |
uploaded_filename (Any):
|
| 27 |
The uploaded filename associated with the observation.
|
|
|
|
|
|
|
| 28 |
|
| 29 |
Methods:
|
| 30 |
__str__():
|
|
@@ -35,8 +37,6 @@ class InputObservation:
|
|
| 35 |
Checks if two observations are equal.
|
| 36 |
__ne__(other):
|
| 37 |
Checks if two observations are not equal.
|
| 38 |
-
__hash__():
|
| 39 |
-
Returns the hash of the observation.
|
| 40 |
to_dict():
|
| 41 |
Converts the observation to a dictionary.
|
| 42 |
from_dict(data):
|
|
@@ -49,7 +49,7 @@ class InputObservation:
|
|
| 49 |
|
| 50 |
def __init__(self, image=None, latitude=None, longitude=None,
|
| 51 |
author_email=None, date=None, time=None, date_option=None, time_option=None,
|
| 52 |
-
uploaded_filename=None):
|
| 53 |
self.image = image
|
| 54 |
self.latitude = latitude
|
| 55 |
self.longitude = longitude
|
|
@@ -59,12 +59,14 @@ class InputObservation:
|
|
| 59 |
self.date_option = date_option
|
| 60 |
self.time_option = time_option
|
| 61 |
self.uploaded_filename = uploaded_filename
|
| 62 |
-
self.
|
| 63 |
self._top_predictions = []
|
| 64 |
|
| 65 |
InputObservation._inst_count += 1
|
| 66 |
self._inst_id = InputObservation._inst_count
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def set_top_predictions(self, top_predictions:list):
|
| 70 |
self._top_predictions = top_predictions
|
|
@@ -76,32 +78,65 @@ class InputObservation:
|
|
| 76 |
|
| 77 |
# add a method to assign the image_md5 only once
|
| 78 |
def assign_image_md5(self):
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def __str__(self):
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def __repr__(self):
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
def __eq__(self, other):
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def __ne__(self, other):
|
| 95 |
return not self.__eq__(other)
|
| 96 |
|
| 97 |
-
def __hash__(self):
|
| 98 |
-
return hash((self.image, self.latitude, self.longitude, self.author_email, self.date, self.time, self.date_option, self.time_option, self.uploaded_filename))
|
| 99 |
-
|
| 100 |
def to_dict(self):
|
| 101 |
return {
|
| 102 |
#"image": self.image,
|
| 103 |
"image_filename": self.uploaded_filename.name if self.uploaded_filename else None,
|
| 104 |
-
"image_md5": self.
|
| 105 |
#"image_md5": hashlib.md5(self.uploaded_filename.read()).hexdigest() if self.uploaded_filename else generate_random_md5(),
|
| 106 |
"latitude": self.latitude,
|
| 107 |
"longitude": self.longitude,
|
|
@@ -115,19 +150,34 @@ class InputObservation:
|
|
| 115 |
|
| 116 |
@classmethod
|
| 117 |
def from_dict(cls, data):
|
| 118 |
-
return cls(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
@classmethod
|
| 121 |
def from_input(cls, input):
|
| 122 |
-
return cls(
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
@staticmethod
|
| 129 |
-
def from_dict(data):
|
| 130 |
-
return InputObservation(data["image"], data["latitude"], data["longitude"], data["author_email"], data["date"], data["time"], data["date_option"], data["time_option"], data["uploaded_filename"])
|
| 131 |
|
| 132 |
|
| 133 |
|
|
|
|
| 25 |
Additional time option for the observation.
|
| 26 |
uploaded_filename (Any):
|
| 27 |
The uploaded filename associated with the observation.
|
| 28 |
+
image_md5 (str):
|
| 29 |
+
The MD5 hash of the image associated with the observation.
|
| 30 |
|
| 31 |
Methods:
|
| 32 |
__str__():
|
|
|
|
| 37 |
Checks if two observations are equal.
|
| 38 |
__ne__(other):
|
| 39 |
Checks if two observations are not equal.
|
|
|
|
|
|
|
| 40 |
to_dict():
|
| 41 |
Converts the observation to a dictionary.
|
| 42 |
from_dict(data):
|
|
|
|
| 49 |
|
| 50 |
def __init__(self, image=None, latitude=None, longitude=None,
|
| 51 |
author_email=None, date=None, time=None, date_option=None, time_option=None,
|
| 52 |
+
uploaded_filename=None, image_md5=None):
|
| 53 |
self.image = image
|
| 54 |
self.latitude = latitude
|
| 55 |
self.longitude = longitude
|
|
|
|
| 59 |
self.date_option = date_option
|
| 60 |
self.time_option = time_option
|
| 61 |
self.uploaded_filename = uploaded_filename
|
| 62 |
+
self.image_md5 = image_md5
|
| 63 |
self._top_predictions = []
|
| 64 |
|
| 65 |
InputObservation._inst_count += 1
|
| 66 |
self._inst_id = InputObservation._inst_count
|
| 67 |
+
|
| 68 |
+
#self.assign_image_md5()
|
| 69 |
+
|
| 70 |
|
| 71 |
def set_top_predictions(self, top_predictions:list):
|
| 72 |
self._top_predictions = top_predictions
|
|
|
|
| 78 |
|
| 79 |
# add a method to assign the image_md5 only once
|
| 80 |
def assign_image_md5(self):
|
| 81 |
+
raise DeprecationWarning("This method is deprecated. hash is a required constructor argument.")
|
| 82 |
+
if not self.image_md5:
|
| 83 |
+
self.image_md5 = hashlib.md5(self.uploaded_filename.read()).hexdigest() if self.uploaded_filename else generate_random_md5()
|
| 84 |
+
|
| 85 |
+
# new comment / hybj hunk
|
| 86 |
+
self._cprint(f"[D] Assigned image md5: {self.image_md5} for {self.uploaded_filename}")
|
| 87 |
|
| 88 |
+
def _cprint(self, msg:str, color:str=OKGREEN):
|
| 89 |
+
"""Print colored message"""
|
| 90 |
+
print(f"{color}{msg}{ENDC}")
|
| 91 |
|
| 92 |
def __str__(self):
|
| 93 |
+
_im_str = "None" if self.image is None else f"image dims: {self.image.shape}"
|
| 94 |
+
return (
|
| 95 |
+
f"Observation: {_im_str}, {self.latitude}, {self.longitude}, "
|
| 96 |
+
f"{self.author_email}, {self.date}, {self.time}, {self.date_option}, "
|
| 97 |
+
f"{self.time_option}, {self.uploaded_filename}, {self.image_md5}"
|
| 98 |
+
)
|
| 99 |
|
| 100 |
def __repr__(self):
|
| 101 |
+
_im_str = "None" if self.image is None else f"image dims: {self.image.shape}"
|
| 102 |
+
return (
|
| 103 |
+
f"Observation: "
|
| 104 |
+
f"Image: {_im_str}, "
|
| 105 |
+
f"Latitude: {self.latitude}, "
|
| 106 |
+
f"Longitude: {self.longitude}, "
|
| 107 |
+
f"Author Email: {self.author_email}, "
|
| 108 |
+
f"Date: {self.date}, "
|
| 109 |
+
f"Time: {self.time}, "
|
| 110 |
+
f"Date Option: {self.date_option}, "
|
| 111 |
+
f"Time Option: {self.time_option}, "
|
| 112 |
+
f"Uploaded Filename: {self.uploaded_filename}"
|
| 113 |
+
f"Image MD5 hash: {self.image_md5}"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
|
| 117 |
def __eq__(self, other):
|
| 118 |
+
# TODO: ensure this covers all the attributes (some have been added?)
|
| 119 |
+
# - except inst_id which is unique
|
| 120 |
+
return (
|
| 121 |
+
self.image == other.image and self.latitude == other.latitude and
|
| 122 |
+
self.longitude == other.longitude and
|
| 123 |
+
self.author_email == other.author_email and
|
| 124 |
+
self.date == other.date and self.time == other.time and
|
| 125 |
+
self.date_option == other.date_option and
|
| 126 |
+
self.time_option == other.time_option and
|
| 127 |
+
self.uploaded_filename == other.uploaded_filename and
|
| 128 |
+
self.image_md5 == other._image_md5
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
|
| 132 |
def __ne__(self, other):
|
| 133 |
return not self.__eq__(other)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
| 135 |
def to_dict(self):
|
| 136 |
return {
|
| 137 |
#"image": self.image,
|
| 138 |
"image_filename": self.uploaded_filename.name if self.uploaded_filename else None,
|
| 139 |
+
"image_md5": self.image_md5,
|
| 140 |
#"image_md5": hashlib.md5(self.uploaded_filename.read()).hexdigest() if self.uploaded_filename else generate_random_md5(),
|
| 141 |
"latitude": self.latitude,
|
| 142 |
"longitude": self.longitude,
|
|
|
|
| 150 |
|
| 151 |
@classmethod
|
| 152 |
def from_dict(cls, data):
|
| 153 |
+
return cls(
|
| 154 |
+
image=data.get("image"),
|
| 155 |
+
latitude=data.get("latitude"),
|
| 156 |
+
longitude=data.get("longitude"),
|
| 157 |
+
author_email=data.get("author_email"),
|
| 158 |
+
date=data.get("date"),
|
| 159 |
+
time=data.get("time"),
|
| 160 |
+
date_option=data.get("date_option"),
|
| 161 |
+
time_option=data.get("time_option"),
|
| 162 |
+
uploaded_filename=data.get("uploaded_filename"),
|
| 163 |
+
image_hash=data.get("image_md5")
|
| 164 |
+
)
|
| 165 |
|
| 166 |
@classmethod
|
| 167 |
def from_input(cls, input):
|
| 168 |
+
return cls(
|
| 169 |
+
image=input.image,
|
| 170 |
+
latitude=input.latitude,
|
| 171 |
+
longitude=input.longitude,
|
| 172 |
+
author_email=input.author_email,
|
| 173 |
+
date=input.date,
|
| 174 |
+
time=input.time,
|
| 175 |
+
date_option=input.date_option,
|
| 176 |
+
time_option=input.time_option,
|
| 177 |
+
uploaded_filename=input.uploaded_filename,
|
| 178 |
+
image_hash=input.image_hash
|
| 179 |
+
)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
|