rmm commited on
Commit
756c23e
·
1 Parent(s): 3b302a7

fix: added timezone to InputObservation, updated tests

Browse files
src/input/input_handling.py CHANGED
@@ -257,7 +257,7 @@ def metadata_inputs_one_file(file:UploadedFile, image_hash:str, dbg_ix:int=0) ->
257
 
258
  observation = InputObservation(image=image, latitude=latitude, longitude=longitude,
259
  author_email=author_email, image_datetime_raw=image_datetime_raw,
260
- date=date, time=time,
261
  uploaded_file=file, image_md5=image_hash
262
  )
263
 
 
257
 
258
  observation = InputObservation(image=image, latitude=latitude, longitude=longitude,
259
  author_email=author_email, image_datetime_raw=image_datetime_raw,
260
+ date=date, time=time, timezone=tz_str,
261
  uploaded_file=file, image_md5=image_hash
262
  )
263
 
src/input/input_observation.py CHANGED
@@ -26,6 +26,8 @@ class InputObservation:
26
  Date of the observation
27
  time (datetime.time):
28
  Time of the observation
 
 
29
  uploaded_file (UploadedFile):
30
  The uploaded file associated with the observation.
31
  image_md5 (str):
@@ -57,6 +59,7 @@ class InputObservation:
57
  author_email:str=None, image_datetime_raw:str=None,
58
  date:datetime.date=None,
59
  time:datetime.time=None,
 
60
  uploaded_file:UploadedFile=None, image_md5:str=None):
61
 
62
  self.image = image
@@ -66,6 +69,7 @@ class InputObservation:
66
  self.image_datetime_raw = image_datetime_raw
67
  self.date = date
68
  self.time = time
 
69
  self.uploaded_file = uploaded_file
70
  self.image_md5 = image_md5
71
  # attributes that get set after predictions/processing
@@ -121,7 +125,7 @@ class InputObservation:
121
  return (
122
  f"Observation: {_im_str}, {self.latitude}, {self.longitude}, "
123
  f"{self.author_email}, {self.image_datetime_raw}, {self.date}, "
124
- f"{self.time}, {self.uploaded_file}, {self.image_md5}"
125
  )
126
 
127
  def __repr__(self):
@@ -135,6 +139,7 @@ class InputObservation:
135
  f"raw timestamp: {self.image_datetime_raw}, "
136
  f"Date: {self.date}, "
137
  f"Time: {self.time}, "
 
138
  f"Uploaded Filename: {self.uploaded_file}"
139
  f"Image MD5 hash: {self.image_md5}"
140
  )
@@ -158,6 +163,7 @@ class InputObservation:
158
  self.date == other.date and
159
  # temporarily skip time, it is followed by the clock and that is always differnt
160
  #self.time == other.time and
 
161
  self.uploaded_file == other.uploaded_file and
162
  self.image_md5 == other.image_md5
163
  )
@@ -167,7 +173,7 @@ class InputObservation:
167
  # only highlight the differences, if element is the same don't show it
168
  # have a summary at the top that shows if the observations are the same or not
169
 
170
- def show_diff(self, other):
171
  """Show the differences between two observations"""
172
  differences = []
173
  if self.image is None or other.image is None:
@@ -189,6 +195,8 @@ class InputObservation:
189
  differences.append(f" Date is different. (self: {self.date}, other: {other.date})")
190
  if self.time != other.time:
191
  differences.append(f" Time is different. (self: {self.time}, other: {other.time})")
 
 
192
  if self.uploaded_file != other.uploaded_file:
193
  differences.append(" Uploaded filename is different.")
194
  if self.image_md5 != other.image_md5:
@@ -216,6 +224,7 @@ class InputObservation:
216
  "image_datetime_raw": self.image_datetime_raw,
217
  "date": str(self.date),
218
  "time": str(self.time),
 
219
  "selected_class": self._selected_class,
220
  "top_prediction": self._top_predictions[0] if len(self._top_predictions) else None,
221
  "class_overriden": self._class_overriden,
@@ -233,12 +242,13 @@ class InputObservation:
233
  image_datetime_raw=data.get("image_datetime_raw"),
234
  date=data.get("date"),
235
  time=data.get("time"),
 
236
  uploaded_file=data.get("uploaded_file"),
237
  image_hash=data.get("image_md5")
238
  )
239
 
240
  @classmethod
241
- def from_input(cls, input):
242
  return cls(
243
  image=input.image,
244
  latitude=input.latitude,
@@ -247,8 +257,9 @@ class InputObservation:
247
  image_datetime_raw=input.image_datetime_raw,
248
  date=input.date,
249
  time=input.time,
 
250
  uploaded_file=input.uploaded_file,
251
- image_hash=input.image_hash
252
  )
253
 
254
 
 
26
  Date of the observation
27
  time (datetime.time):
28
  Time of the observation
29
+ timezone (str):
30
+ Timezone of the observation (e.g. +0300)
31
  uploaded_file (UploadedFile):
32
  The uploaded file associated with the observation.
33
  image_md5 (str):
 
59
  author_email:str=None, image_datetime_raw:str=None,
60
  date:datetime.date=None,
61
  time:datetime.time=None,
62
+ timezone:str=None,
63
  uploaded_file:UploadedFile=None, image_md5:str=None):
64
 
65
  self.image = image
 
69
  self.image_datetime_raw = image_datetime_raw
70
  self.date = date
71
  self.time = time
72
+ self.timezone = timezone
73
  self.uploaded_file = uploaded_file
74
  self.image_md5 = image_md5
75
  # attributes that get set after predictions/processing
 
125
  return (
126
  f"Observation: {_im_str}, {self.latitude}, {self.longitude}, "
127
  f"{self.author_email}, {self.image_datetime_raw}, {self.date}, "
128
+ f"{self.time}, {self.timezone}, {self.uploaded_file}, {self.image_md5}"
129
  )
130
 
131
  def __repr__(self):
 
139
  f"raw timestamp: {self.image_datetime_raw}, "
140
  f"Date: {self.date}, "
141
  f"Time: {self.time}, "
142
+ f"Timezone: {self.timezone}, "
143
  f"Uploaded Filename: {self.uploaded_file}"
144
  f"Image MD5 hash: {self.image_md5}"
145
  )
 
163
  self.date == other.date and
164
  # temporarily skip time, it is followed by the clock and that is always differnt
165
  #self.time == other.time and
166
+ self.timezone == other.timezone and
167
  self.uploaded_file == other.uploaded_file and
168
  self.image_md5 == other.image_md5
169
  )
 
173
  # only highlight the differences, if element is the same don't show it
174
  # have a summary at the top that shows if the observations are the same or not
175
 
176
+ def show_diff(self, other: 'InputObservation'):
177
  """Show the differences between two observations"""
178
  differences = []
179
  if self.image is None or other.image is None:
 
195
  differences.append(f" Date is different. (self: {self.date}, other: {other.date})")
196
  if self.time != other.time:
197
  differences.append(f" Time is different. (self: {self.time}, other: {other.time})")
198
+ if self.timezone != other.timezone:
199
+ differences.append(f" Timezone is different. (self: {self.timezone}, other: {other.timezone})")
200
  if self.uploaded_file != other.uploaded_file:
201
  differences.append(" Uploaded filename is different.")
202
  if self.image_md5 != other.image_md5:
 
224
  "image_datetime_raw": self.image_datetime_raw,
225
  "date": str(self.date),
226
  "time": str(self.time),
227
+ "timezone": str(self.timezone),
228
  "selected_class": self._selected_class,
229
  "top_prediction": self._top_predictions[0] if len(self._top_predictions) else None,
230
  "class_overriden": self._class_overriden,
 
242
  image_datetime_raw=data.get("image_datetime_raw"),
243
  date=data.get("date"),
244
  time=data.get("time"),
245
+ timezone=data.get("timezone"),
246
  uploaded_file=data.get("uploaded_file"),
247
  image_hash=data.get("image_md5")
248
  )
249
 
250
  @classmethod
251
+ def from_input(cls, input: 'InputObservation'):
252
  return cls(
253
  image=input.image,
254
  latitude=input.latitude,
 
257
  image_datetime_raw=input.image_datetime_raw,
258
  date=input.date,
259
  time=input.time,
260
+ timezone=input.timezone,
261
  uploaded_file=input.uploaded_file,
262
+ image_md5=input.image_md5
263
  )
264
 
265
 
tests/test_input_observation.py CHANGED
@@ -89,11 +89,12 @@ def test_input_observation_valid(mock_uploadedFile):
89
 
90
  _date="2023-10-10"
91
  _time="10:10:10"
92
- image_datetime_raw = _date + " " + _time
93
- dt = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S")
 
94
  date = dt.date()
95
  time = dt.time()
96
-
97
  ## make a random image with dtype uint8 using np.random.randint
98
  image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
99
  image_md5 = 'd1d2515e6f6ac4c5ca6dd739d5143cd4' # 32 hex chars.
@@ -101,7 +102,7 @@ def test_input_observation_valid(mock_uploadedFile):
101
  obs = InputObservation(
102
  image=image,
103
  latitude=12.34, longitude=56.78, author_email=author_email,
104
- time=time, date=date,
105
  uploaded_file=mock_file,
106
  image_md5=image_md5,
107
  )
@@ -116,6 +117,7 @@ def test_input_observation_valid(mock_uploadedFile):
116
  assert isinstance(obs.time, datetime.time)
117
  assert str(obs.date) == "2023-10-10"
118
  assert str(obs.time) == "10:10:10"
 
119
 
120
  assert obs.uploaded_file.name == image_name
121
  assert obs.uploaded_file.size == 123456
@@ -274,16 +276,20 @@ def good_datadict_for_input_observation(mock_uploadedFile) -> dict:
274
  # set up the good and bad inputs
275
  _date="2023-10-10"
276
  _time="10:10:10"
277
- image_datetime_raw = _date + " " + _time
 
 
278
  fname = "test_image.jpg"
279
  image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
280
 
281
- dt_ok = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S")
 
282
  valid_inputs = {
283
  "author_email": "[email protected]",
284
  "uploaded_file": mock_uploadedFile(name=fname).get_data(),
285
  "date": dt_ok.date(),
286
  "time": dt_ok.time(),
 
287
  "image": image,
288
  "image_md5": 'd1d2515e6f6ac4c5ca6dd739d5143cd4', # 32 hex chars.
289
  "image_datetime_raw": image_datetime_raw,
 
89
 
90
  _date="2023-10-10"
91
  _time="10:10:10"
92
+ _timezone = "+04:00"
93
+ image_datetime_raw = _date + " " + _time + " " + _timezone
94
+ dt = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S %z")
95
  date = dt.date()
96
  time = dt.time()
97
+ tz_str = dt.strftime('%z')
98
  ## make a random image with dtype uint8 using np.random.randint
99
  image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
100
  image_md5 = 'd1d2515e6f6ac4c5ca6dd739d5143cd4' # 32 hex chars.
 
102
  obs = InputObservation(
103
  image=image,
104
  latitude=12.34, longitude=56.78, author_email=author_email,
105
+ time=time, date=date, timezone=tz_str,
106
  uploaded_file=mock_file,
107
  image_md5=image_md5,
108
  )
 
117
  assert isinstance(obs.time, datetime.time)
118
  assert str(obs.date) == "2023-10-10"
119
  assert str(obs.time) == "10:10:10"
120
+ assert obs.timezone == tz_str
121
 
122
  assert obs.uploaded_file.name == image_name
123
  assert obs.uploaded_file.size == 123456
 
276
  # set up the good and bad inputs
277
  _date="2023-10-10"
278
  _time="10:10:10"
279
+ _timezone = "+04:00"
280
+ image_datetime_raw = _date + " " + _time + " " + _timezone
281
+ #dt = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S %z")
282
  fname = "test_image.jpg"
283
  image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
284
 
285
+ dt_ok = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S %z")
286
+ tz_str = dt_ok.strftime('%z')
287
  valid_inputs = {
288
  "author_email": "[email protected]",
289
  "uploaded_file": mock_uploadedFile(name=fname).get_data(),
290
  "date": dt_ok.date(),
291
  "time": dt_ok.time(),
292
+ "timezone": tz_str,
293
  "image": image,
294
  "image_md5": 'd1d2515e6f6ac4c5ca6dd739d5143cd4', # 32 hex chars.
295
  "image_datetime_raw": image_datetime_raw,