Skip to content

Commit c5b5ef6

Browse files
authored
Report table name in accept_divergence (#1350)
* Report table name in `accept_divergence` * Update changelog * Add type hint specificity
1 parent 9a5ef79 commit c5b5ef6

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ ImportedLFP().drop()
109109
- Correct name parsing in Session.Experimenter insertion #1306
110110
- Allow insert with dio events but no e-series data #1318
111111
- Prompt user to verify compatibility between new insert and existing table
112-
entries # 1318
112+
entries # 1318, #1350
113113
- Skip empty timeseries ingestion (`PositionSource`, `DioEvents`) #1347
114114
- Position
115115
- Allow population of missing `PositionIntervalMap` entries during population

src/spyglass/common/common_device.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,19 @@ def _add_device(cls, new_device_dict, test_mode=None):
171171
DataAcquisitionDevice & {"data_acquisition_device_name": name}
172172
).fetch1()
173173
for k, existing_val in db_dict.items():
174-
if not (new_val := new_device_dict.get(k, None)) == existing_val:
175-
# if the values do not match, check whether the user wants to
176-
# accept the entry in the database, or raise an exception
177-
if not accept_divergence(k, new_val, existing_val, test_mode):
178-
raise PopulateException(
179-
"Data acquisition device properties of PyNWB Device object "
180-
+ f"with name '{name}': {new_device_dict} do not match "
181-
f"properties of the corresponding database entry: {db_dict}."
182-
)
174+
new_val = new_device_dict.get(k, None)
175+
if new_val == existing_val:
176+
continue # values match, no need to check further
177+
# if the values do not match, check whether the user wants to
178+
# accept the entry in the database, or raise an exception
179+
if not accept_divergence(
180+
k, new_val, existing_val, test_mode, cls.camel_name
181+
):
182+
raise PopulateException(
183+
"Data acquisition device properties of PyNWB Device object "
184+
+ f"with name '{name}': {new_device_dict} do not match "
185+
f"properties of the corresponding database entry: {db_dict}"
186+
)
183187

184188
@classmethod
185189
def _add_system(cls, system):

src/spyglass/common/common_task.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,22 @@ def unequal_vals(key, a, b):
7373
continue
7474
existing = query.fetch1()
7575
for key in set(task_dict).union(existing):
76-
if unequal_vals(key, task_dict, existing):
77-
if not accept_divergence(
78-
key,
79-
task_dict.get(key),
80-
existing.get(key),
81-
self._test_mode,
82-
):
83-
# If the user does not accept the divergence,
84-
# raise an error to prevent data inconsistency
85-
raise ValueError(
86-
f"Task {task_dict['task_name']} already exists "
87-
+ f"with different values for {key}: "
88-
+ f"{task_dict.get(key)} != {existing.get(key)}"
89-
)
76+
if not unequal_vals(key, task_dict, existing):
77+
continue # skip if values are equal
78+
if not accept_divergence(
79+
key,
80+
task_dict.get(key),
81+
existing.get(key),
82+
self._test_mode,
83+
self.camel_name,
84+
):
85+
# If the user does not accept the divergence,
86+
# raise an error to prevent data inconsistency
87+
raise ValueError(
88+
f"Task {task_dict['task_name']} already exists "
89+
+ f"with different values for {key}: "
90+
+ f"{task_dict.get(key)} != {existing.get(key)}"
91+
)
9092
# Insert the tasks into the table
9193
self.insert(inserts)
9294

src/spyglass/utils/dj_helper_fn.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import re
88
from pathlib import Path
9-
from typing import Iterable, List, Type, Union
9+
from typing import Any, Iterable, List, Optional, Type, Union
1010
from uuid import uuid4
1111

1212
import datajoint as dj
@@ -106,9 +106,9 @@ def declare_all_merge_tables():
106106
from spyglass.decoding.decoding_merge import DecodingOutput # noqa: F401
107107
from spyglass.lfp.lfp_merge import LFPOutput # noqa: F401
108108
from spyglass.position.position_merge import PositionOutput # noqa: F401
109-
from spyglass.spikesorting.spikesorting_merge import ( # noqa: F401
109+
from spyglass.spikesorting.spikesorting_merge import (
110110
SpikeSortingOutput,
111-
)
111+
) # noqa: F401
112112

113113

114114
def fuzzy_get(index: Union[int, str], names: List[str], sources: List[str]):
@@ -686,16 +686,39 @@ def bytes_to_human_readable(size: int) -> str:
686686
return msg_template.format(size=size, unit="PB")
687687

688688

689-
def accept_divergence(key, new_value, existing_value, test_mode=False):
690-
"""prompt to accept divergence in values between existing and new entries"""
689+
def accept_divergence(
690+
key: str,
691+
new_value: Any,
692+
existing_value: Any,
693+
test_mode: bool = False,
694+
table_name: Optional[str] = None,
695+
):
696+
"""Prompt to accept divergence in values between existing and new entries
697+
698+
Parameters
699+
----------
700+
key : str
701+
Name of the column where the divergence is found
702+
new_value : Any
703+
New value to be inserted into the table
704+
existing_value : Any
705+
Existing value in the table that is different from the new value
706+
test_mode : bool, optional
707+
If True, will not prompt and return False, by default False
708+
table_name : str, optional
709+
Name of the table where the divergence is found, by default None
710+
"""
691711
if test_mode:
692712
# If get here in test mode, is because want to test failure
693713
logger.warning(
694-
"accept_divergence called in test mode, returning False without prompt"
714+
"accept_divergence called in test mode, returning False w/o prompt"
695715
)
696716
return False
717+
tbl_msg = ""
718+
if table_name: # optional message with table name
719+
tbl_msg = f" of '{table_name}'"
697720
response = dj.utils.user_choice(
698-
f"Existing entry differs in '{key}' column.\n"
721+
f"Existing entry differs in '{key}' column{tbl_msg}.\n"
699722
+ "Accept the existing value of: \n"
700723
+ f"'{existing_value}' \n"
701724
+ "in place of the new value: \n"

0 commit comments

Comments
 (0)