Skip to content

Commit 85b96e6

Browse files
authored
global action headers in log + remove microseconds + log number of red flags after each after (#88)
1 parent 6a9ce4c commit 85b96e6

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

trackedit/DatabaseHandler.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def __init__(
190190
self.toannotate = self.find_all_toannotate()
191191
self.divisions = self.find_all_divisions()
192192
self.log(
193-
f"Start annotation session - TrackEdit v{__version__} ({datetime.now()})"
193+
f"Start annotation session - TrackEdit v{__version__} ({datetime.now().replace(microsecond=0)})"
194194
)
195195
self.log(
196196
f"Parameters: Tmax: {self.Tmax}, working_directory: {self.working_directory}, "
@@ -242,11 +242,14 @@ def initialize_logfile(self, log_filename_new, work_in_existing_db):
242242

243243
return log_file_path
244244

245-
def log(self, message):
245+
def log(self, message, is_header=True):
246246
"""Append a message to the log file."""
247247
with open(self.log_file, "a") as log:
248-
time_stap = f"[{datetime.now()}]"
249-
log.write(time_stap + " " + message + "\n")
248+
time_stamp = f"[{datetime.now().replace(microsecond=0)}]"
249+
if is_header:
250+
log.write(message + "\n")
251+
else:
252+
log.write("\t\t" + time_stamp + " " + message + "\n")
250253

251254
def initialize_config(self):
252255
# import db filename properly into an Ultrack config, neccesary for chaning values in database
@@ -429,7 +432,7 @@ def get_same_db_filename(self, old_filename):
429432
log_filename_new = f"{name}_changelog.txt"
430433
return old_filename, db_filename_new, log_filename_new
431434

432-
def change_values(self, indices, field, values):
435+
def change_values(self, indices, field, values, log_header=None):
433436
"""Change values in the database for one or multiple indices.
434437
435438
Parameters
@@ -441,13 +444,18 @@ def change_values(self, indices, field, values):
441444
value : int or list
442445
Value(s) to set. If a single value is provided, it will be applied to all indices.
443446
If a list is provided, it must match the length of indices.
447+
log_header : str, optional
448+
Optional header to prepend to log messages.
444449
"""
445450
# Convert single index to list
446451
if isinstance(indices, (int, np.integer)):
447452
indices = [int(indices)]
448453
else:
449454
indices = [int(i) for i in indices]
450455

456+
if log_header is not None:
457+
self.log(log_header)
458+
451459
# Handle value input
452460
if isinstance(values, (list, np.ndarray)):
453461
if len(values) != len(indices):
@@ -483,7 +491,7 @@ def change_values(self, indices, field, values):
483491
else old_vals
484492
)
485493
message = f"db: setting {field.name}[id={indices[i]}] = {values[i]} (was {old_val})"
486-
self.log(message)
494+
self.log(message, is_header=False)
487495

488496
def calc_time_window(self):
489497
time_chunk_starts = np.arange(
@@ -1146,7 +1154,12 @@ def annotate_track(
11461154
indices = self.df_full[track_mask].index.tolist()
11471155

11481156
if indices:
1149-
self.change_values(indices, NodeDB.generic, label)
1157+
self.change_values(
1158+
indices,
1159+
NodeDB.generic,
1160+
label,
1161+
log_header="annotate_track:" + str(track_id),
1162+
)
11501163

11511164
def clear_nodes_annotations(self, nodes):
11521165
"""Clear the annotations for the entire track of a list of nodes. Called when a node is deleted."""

trackedit/motile_overwrites.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ def db_add_nodes(self):
6262
)
6363

6464
# Change the selected/annotation status of the nodes
65-
DB_handler.change_values(indices=self.nodes, field=NodeDB.selected, values=1)
65+
DB_handler.change_values(
66+
indices=self.nodes,
67+
field=NodeDB.selected,
68+
values=1,
69+
log_header="AddNodes:" + str(self.nodes),
70+
)
6671
DB_handler.change_values(
6772
indices=self.nodes, field=NodeDB.generic, values=NodeDB.generic.default.arg
6873
)
@@ -85,18 +90,25 @@ def db_delete_nodes(self):
8590
DB_handler.df_full["parent_id"].isin(self.nodes)
8691
].index.tolist()
8792
print("orphaned_children", orphaned_children)
93+
log_header = "DeleteNodes:" + str(self.nodes)
8894
if orphaned_children:
8995
DB_handler.change_values(
90-
indices=orphaned_children, field=NodeDB.parent_id, values=-1
96+
indices=orphaned_children,
97+
field=NodeDB.parent_id,
98+
values=-1,
99+
log_header=log_header,
91100
)
101+
log_header = None # prevent printing twice
92102
show_warning(
93103
"An edge in the next time window is removed, so 'UNDO' will not work."
94104
)
95105
# ToDo: potentially only remove orphan edges into the next time window,
96106
# because normal edges are already properly removed
97107

98108
# Set nodes as unselected
99-
DB_handler.change_values(indices=self.nodes, field=NodeDB.selected, values=0)
109+
DB_handler.change_values(
110+
indices=self.nodes, field=NodeDB.selected, values=0, log_header=log_header
111+
)
100112

101113
return db_delete_nodes
102114

@@ -120,7 +132,10 @@ def db_add_edges(self):
120132

121133
# Batch the changes into a single call
122134
DB_handler.change_values(
123-
indices=child_nodes, field=NodeDB.parent_id, values=parent_nodes
135+
indices=child_nodes,
136+
field=NodeDB.parent_id,
137+
values=parent_nodes,
138+
log_header="AddEdges:" + str(self.edges),
124139
)
125140

126141
return db_add_edges
@@ -141,7 +156,12 @@ def db_delete_edges(self):
141156
child_nodes = [e[1] for e in self.edges]
142157

143158
# Batch the changes into a single call
144-
DB_handler.change_values(indices=child_nodes, field=NodeDB.parent_id, values=-1)
159+
DB_handler.change_values(
160+
indices=child_nodes,
161+
field=NodeDB.parent_id,
162+
values=-1,
163+
log_header="DeleteEdges:" + str(self.edges),
164+
)
145165

146166
return db_delete_edges
147167

trackedit/widgets/navigation/red_flag_box.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def update_red_flags(self, *args):
6969
)
7070

7171
self.update_red_flag_counter_and_info()
72+
self.databasehandler.log(
73+
f"Number of remaining red flags: {len(self.databasehandler.red_flags)}",
74+
is_header=False,
75+
)
7276

7377
def _update_button_states(self):
7478
"""Update the enabled state of navigation buttons based on red flag count."""

0 commit comments

Comments
 (0)