rmm commited on
Commit
5cb2d26
·
1 Parent(s): 80c0010

feat: InputObservations are compared and only updated if new

Browse files
Files changed (1) hide show
  1. src/input/input_handling.py +17 -8
src/input/input_handling.py CHANGED
@@ -248,7 +248,7 @@ def process_files():
248
  st.session_state.image_filenames = filenames
249
 
250
 
251
- def metadata_inputs_one_file(file:UploadedFile, ukey:str, dbg_ix:int=0) -> InputObservation:
252
  # dbg_ix is a hack to have different data in each input group, checking persistence
253
 
254
  if st.session_state.container_metadata_inputs is not None:
@@ -262,6 +262,7 @@ def metadata_inputs_one_file(file:UploadedFile, ukey:str, dbg_ix:int=0) -> Input
262
  author_email = st.session_state["input_author_email"]
263
  filename = file.name
264
  image_datetime = get_image_datetime(file)
 
265
  # add the UI elements
266
  #viewcontainer.title(f"Metadata for {filename}")
267
  viewcontainer = _viewcontainer.expander(f"Metadata for {file.name}", expanded=True)
@@ -276,7 +277,7 @@ def metadata_inputs_one_file(file:UploadedFile, ukey:str, dbg_ix:int=0) -> Input
276
  latitude = viewcontainer.text_input(
277
  "Latitude for " + filename,
278
  spoof_metadata.get('latitude', 0) + dbg_ix,
279
- key=f"input_latitude_{ukey}")
280
  if latitude and not is_valid_number(latitude):
281
  viewcontainer.error("Please enter a valid latitude (numerical only).")
282
  m_logger.error(f"Invalid latitude entered: {latitude}.")
@@ -284,7 +285,7 @@ def metadata_inputs_one_file(file:UploadedFile, ukey:str, dbg_ix:int=0) -> Input
284
  longitude = viewcontainer.text_input(
285
  "Longitude for " + filename,
286
  spoof_metadata.get('longitude', ""),
287
- key=f"input_longitude_{ukey}")
288
  if longitude and not is_valid_number(longitude):
289
  viewcontainer.error("Please enter a valid longitude (numerical only).")
290
  m_logger.error(f"Invalid latitude entered: {latitude}.")
@@ -299,13 +300,13 @@ def metadata_inputs_one_file(file:UploadedFile, ukey:str, dbg_ix:int=0) -> Input
299
  date_value = datetime.datetime.now().date()
300
 
301
  ## if not, give user the option to enter manually
302
- date_option = viewcontainer.date_input("Date for "+filename, value=date_value, key=f"input_date_{ukey}")
303
- time_option = viewcontainer.time_input("Time for "+filename, time_value, key=f"input_time_{ukey}")
304
 
305
- observation = InputObservation(image=file, latitude=latitude, longitude=longitude,
306
  author_email=author_email, date=image_datetime, time=None,
307
  date_option=date_option, time_option=time_option,
308
- uploaded_filename=file,
309
  )
310
 
311
  # TODO: pass in the hash to InputObservation, so it is done once only. (need to refactor the class a bit)
@@ -330,7 +331,15 @@ def _setup_dynamic_inputs() -> None:
330
  for ix, file in enumerate(uploaded_files):
331
  hash = hashes[ix]
332
  observation = metadata_inputs_one_file(file, hash, ix)
333
- observations[hash] = observation
 
 
 
 
 
 
 
 
334
 
335
  st.session_state.observations = observations
336
 
 
248
  st.session_state.image_filenames = filenames
249
 
250
 
251
+ def metadata_inputs_one_file(file:UploadedFile, image_hash:str, dbg_ix:int=0) -> InputObservation:
252
  # dbg_ix is a hack to have different data in each input group, checking persistence
253
 
254
  if st.session_state.container_metadata_inputs is not None:
 
262
  author_email = st.session_state["input_author_email"]
263
  filename = file.name
264
  image_datetime = get_image_datetime(file)
265
+ image = st.session_state.images.get(image_hash, None)
266
  # add the UI elements
267
  #viewcontainer.title(f"Metadata for {filename}")
268
  viewcontainer = _viewcontainer.expander(f"Metadata for {file.name}", expanded=True)
 
277
  latitude = viewcontainer.text_input(
278
  "Latitude for " + filename,
279
  spoof_metadata.get('latitude', 0) + dbg_ix,
280
+ key=f"input_latitude_{image_hash}")
281
  if latitude and not is_valid_number(latitude):
282
  viewcontainer.error("Please enter a valid latitude (numerical only).")
283
  m_logger.error(f"Invalid latitude entered: {latitude}.")
 
285
  longitude = viewcontainer.text_input(
286
  "Longitude for " + filename,
287
  spoof_metadata.get('longitude', ""),
288
+ key=f"input_longitude_{image_hash}")
289
  if longitude and not is_valid_number(longitude):
290
  viewcontainer.error("Please enter a valid longitude (numerical only).")
291
  m_logger.error(f"Invalid latitude entered: {latitude}.")
 
300
  date_value = datetime.datetime.now().date()
301
 
302
  ## if not, give user the option to enter manually
303
+ date_option = viewcontainer.date_input("Date for "+filename, value=date_value, key=f"input_date_{image_hash}")
304
+ time_option = viewcontainer.time_input("Time for "+filename, time_value, key=f"input_time_{image_hash}")
305
 
306
+ observation = InputObservation(image=image, latitude=latitude, longitude=longitude,
307
  author_email=author_email, date=image_datetime, time=None,
308
  date_option=date_option, time_option=time_option,
309
+ uploaded_filename=file, image_md5=image_hash
310
  )
311
 
312
  # TODO: pass in the hash to InputObservation, so it is done once only. (need to refactor the class a bit)
 
331
  for ix, file in enumerate(uploaded_files):
332
  hash = hashes[ix]
333
  observation = metadata_inputs_one_file(file, hash, ix)
334
+ old_obs = st.session_state.observations.get(hash, None)
335
+ if old_obs is not None:
336
+ if old_obs == observation:
337
+ observations[hash] = old_obs
338
+ else:
339
+ observations[hash] = observation
340
+ observation.show_diff(old_obs)
341
+ else:
342
+ observations[hash] = observation
343
 
344
  st.session_state.observations = observations
345