batoolset 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. batoolset/GUI/__init__.py +0 -0
  2. batoolset/GUI/list_gui.py +513 -0
  3. batoolset/GUI/multi_image_display.py +989 -0
  4. batoolset/GUI/open.py +325 -0
  5. batoolset/GUI/overlay_hints.py +45 -0
  6. batoolset/GUI/paint2.py +1345 -0
  7. batoolset/GUI/scrollablepaint.py +1152 -0
  8. batoolset/IJ_ROIs/__init__.py +0 -0
  9. batoolset/IJ_ROIs/tools.py +40 -0
  10. batoolset/PATH/__init__.py +0 -0
  11. batoolset/PATH/tools.py +79 -0
  12. batoolset/SQLite_tools/__init__.py +0 -0
  13. batoolset/SQLite_tools/tools.py +840 -0
  14. batoolset/__init__.py +0 -0
  15. batoolset/binary/__init__.py +0 -0
  16. batoolset/binary/tools.py +123 -0
  17. batoolset/binarytools/__init__.py +0 -0
  18. batoolset/binarytools/cell_center_detector.py +290 -0
  19. batoolset/clipboard_tools/__init__.py +0 -0
  20. batoolset/clipboard_tools/tools.py +7 -0
  21. batoolset/colors/__init__.py +0 -0
  22. batoolset/colors/colorgen.py +193 -0
  23. batoolset/colors/tools.py +22 -0
  24. batoolset/command_line/__init__.py +0 -0
  25. batoolset/command_line/tools.py +18 -0
  26. batoolset/dialogs/__init__.py +0 -0
  27. batoolset/dialogs/opensave.py +228 -0
  28. batoolset/dicts/__init__.py +0 -0
  29. batoolset/dicts/tools.py +299 -0
  30. batoolset/dims/__init__.py +0 -0
  31. batoolset/dims/tools.py +83 -0
  32. batoolset/draw/__init__.py +0 -0
  33. batoolset/draw/shapes/Position.py +183 -0
  34. batoolset/draw/shapes/__init__.py +0 -0
  35. batoolset/draw/shapes/automated_alignment_drawing_refactored.py +346 -0
  36. batoolset/draw/shapes/circle2d.py +99 -0
  37. batoolset/draw/shapes/compute_optimal_size_all_merged.py +317 -0
  38. batoolset/draw/shapes/ellipse2d.py +336 -0
  39. batoolset/draw/shapes/freehand2d.py +235 -0
  40. batoolset/draw/shapes/group.py +1830 -0
  41. batoolset/draw/shapes/image2d.py +2469 -0
  42. batoolset/draw/shapes/line2d.py +575 -0
  43. batoolset/draw/shapes/point2d.py +257 -0
  44. batoolset/draw/shapes/polygon2d.py +357 -0
  45. batoolset/draw/shapes/polyline2d.py +235 -0
  46. batoolset/draw/shapes/rect2d.py +388 -0
  47. batoolset/draw/shapes/rectangle2d.py +601 -0
  48. batoolset/draw/shapes/scalebar.py +304 -0
  49. batoolset/draw/shapes/serializablefont.py +126 -0
  50. batoolset/draw/shapes/serializableguiparams.py +10 -0
  51. batoolset/draw/shapes/square2d.py +103 -0
  52. batoolset/draw/shapes/textorientation.py +45 -0
  53. batoolset/draw/shapes/tools.py +467 -0
  54. batoolset/draw/shapes/transform2D.py +350 -0
  55. batoolset/draw/shapes/txt2d.py +550 -0
  56. batoolset/draw/widgets/__init__.py +0 -0
  57. batoolset/draw/widgets/col.py +343 -0
  58. batoolset/draw/widgets/image_group.py +248 -0
  59. batoolset/draw/widgets/image_organizer.py +96 -0
  60. batoolset/draw/widgets/paint.py +184 -0
  61. batoolset/draw/widgets/vectorial.py +257 -0
  62. batoolset/figure/__init__.py +0 -0
  63. batoolset/figure/alignment.py +814 -0
  64. batoolset/figure/column.py +1117 -0
  65. batoolset/figure/fig_tools.py +285 -0
  66. batoolset/figure/minimal_debugger_scale_etc.py +139 -0
  67. batoolset/figure/panel.py +697 -0
  68. batoolset/figure/row.py +852 -0
  69. batoolset/figure/user_selection.py +287 -0
  70. batoolset/files/__init__.py +0 -0
  71. batoolset/files/tools.py +1321 -0
  72. batoolset/images/__init__.py +0 -0
  73. batoolset/images/tools.py +81 -0
  74. batoolset/img.py +5216 -0
  75. batoolset/linux/__init__.py +0 -0
  76. batoolset/linux/tools.py +72 -0
  77. batoolset/lists/__init__.py +0 -0
  78. batoolset/lists/tools.py +436 -0
  79. batoolset/luts/__init__.py +0 -0
  80. batoolset/luts/lut_minimal_test.py +1207 -0
  81. batoolset/matplolib/__init__.py +0 -0
  82. batoolset/matplolib/tools.py +44 -0
  83. batoolset/np_tools/__init__.py +0 -0
  84. batoolset/np_tools/tools.py +252 -0
  85. batoolset/np_tools/trash.py +15 -0
  86. batoolset/pandas_tools/__init__.py +0 -0
  87. batoolset/pandas_tools/tools.py +880 -0
  88. batoolset/partials/__init__.py +0 -0
  89. batoolset/partials/tools.py +83 -0
  90. batoolset/plot_tools/__init__.py +0 -0
  91. batoolset/plot_tools/tools.py +70 -0
  92. batoolset/pyqt/__init__.py +0 -0
  93. batoolset/pyqt/tools.py +691 -0
  94. batoolset/re_tools/__init__.py +0 -0
  95. batoolset/re_tools/tools.py +35 -0
  96. batoolset/serialization/__init__.py +0 -0
  97. batoolset/serialization/tools.py +1192 -0
  98. batoolset/sets/__init__.py +0 -0
  99. batoolset/sets/tools.py +109 -0
  100. batoolset/settings/__init__.py +0 -0
  101. batoolset/settings/global_settings.py +134 -0
  102. batoolset/strings/__init__.py +0 -0
  103. batoolset/strings/tools.py +225 -0
  104. batoolset/ta/__init__.py +0 -0
  105. batoolset/ta/database/__init__.py +0 -0
  106. batoolset/ta/database/advanced_sql_plotter.py +1170 -0
  107. batoolset/ta/database/my_qt_table.py +79 -0
  108. batoolset/ta/database/preview_and_edit_sqlite_db.py +405 -0
  109. batoolset/ta/database/sql.py +2141 -0
  110. batoolset/ta/database/sql_column_name_dialog.py +122 -0
  111. batoolset/ta/measurements/TAmeasures.py +2226 -0
  112. batoolset/ta/measurements/__init__.py +0 -0
  113. batoolset/ta/measurements/measurements3D/__init__.py +0 -0
  114. batoolset/ta/measurements/measurements3D/get_point_on_surface_if_centroid_is_bad.py +175 -0
  115. batoolset/ta/measurements/measurements3D/height_map_to_projection.py +82 -0
  116. batoolset/ta/measurements/measurements3D/measures3D.py +208 -0
  117. batoolset/ta/measurements/measurements3D/triangulate.py +331 -0
  118. batoolset/ta/measurements/nematic.py +562 -0
  119. batoolset/ta/measurements/nematic_measurements.py +107 -0
  120. batoolset/ta/segmentation/__init__.py +0 -0
  121. batoolset/ta/segmentation/neo_wshed.py +314 -0
  122. batoolset/ta/selections/__init__.py +0 -0
  123. batoolset/ta/selections/selection.py +329 -0
  124. batoolset/ta/tracking/__init__.py +0 -0
  125. batoolset/ta/tracking/last_tracking_based_on_matching_or_on_translation_from_mermaid_warp.py +653 -0
  126. batoolset/ta/tracking/local_to_track_correspondance.py +138 -0
  127. batoolset/ta/tracking/rapid_detection_of_vertices.py +517 -0
  128. batoolset/ta/tracking/registration.py +108 -0
  129. batoolset/ta/tracking/swap_detector_and_fixer.py +296 -0
  130. batoolset/ta/tracking/track_correction.py +238 -0
  131. batoolset/ta/tracking/tracking_error_detector_and_fixer.py +3099 -0
  132. batoolset/ta/tracking/tracking_yet_another_approach_pyramidal_registration_n_neo_swapping_correction.py +725 -0
  133. batoolset/ta/utils/TA_tools.py +43 -0
  134. batoolset/ta/utils/__init__.py +0 -0
  135. batoolset/ta/utils/rps_tools.py +1783 -0
  136. batoolset/tests/__init__.py +0 -0
  137. batoolset/tests/rps_tools.py +51 -0
  138. batoolset/tests/tools.py +10 -0
  139. batoolset/tools/__init__.py +0 -0
  140. batoolset/tools/early_stopper_class.py +9 -0
  141. batoolset/tools/guided_data_recoverer.py +110 -0
  142. batoolset/tools/logger.py +100 -0
  143. batoolset/tools/mytimer.py +31 -0
  144. batoolset/tools/prompt_color.py +160 -0
  145. batoolset/tools/qthandler.py +118 -0
  146. batoolset/uitools/__init__.py +0 -0
  147. batoolset/uitools/blinker.py +56 -0
  148. batoolset/unsafe/__init__.py +0 -0
  149. batoolset/unsafe/tools.py +115 -0
  150. batoolset/utils/__init__.py +0 -0
  151. batoolset/utils/commontools.py +158 -0
  152. batoolset/utils/loadlist.py +728 -0
  153. batoolset/version.py +10 -0
  154. batoolset/xml/__init__.py +0 -0
  155. batoolset/xml/tools.py +65 -0
  156. batoolset/zip_tools/__init__.py +0 -0
  157. batoolset/zip_tools/tools.py +76 -0
  158. batoolset-0.1.0.dist-info/LICENSE +24 -0
  159. batoolset-0.1.0.dist-info/METADATA +45 -0
  160. batoolset-0.1.0.dist-info/RECORD +162 -0
  161. batoolset-0.1.0.dist-info/WHEEL +5 -0
  162. batoolset-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,840 @@
1
+ # see also pandas_tools/tools as there is some overlap between the two
2
+ import os.path
3
+ import sqlite3
4
+
5
+ def set_voxel_size(db_path, voxel_size):
6
+ """
7
+ Inserts a list or tuple of three floats into an SQLite database table named 'voxel_size'
8
+ with columns 'vz', 'vy', and 'vx'.
9
+
10
+ Args:
11
+ db_path (str): The path to the SQLite database file.
12
+ voxel_size (list or tuple): A list or tuple containing three floats.
13
+
14
+ Returns:
15
+ None.
16
+ """
17
+
18
+ # delete table if exists
19
+ drop_table_if_exists(db_path,'voxel_size', verbose=False)
20
+
21
+ if voxel_size is None:
22
+ return
23
+
24
+ # Connect to the SQLite database
25
+ conn = sqlite3.connect(db_path)
26
+
27
+ # Create a cursor object
28
+ c = conn.cursor()
29
+
30
+ # Insert the values into the 'voxel_size' table
31
+ c.execute('CREATE TABLE voxel_size ("vz", "vy", "vx")')
32
+ c.execute('INSERT INTO voxel_size (vz, vy, vx) VALUES (?, ?, ?)', voxel_size)
33
+
34
+ # Commit the transaction
35
+ conn.commit()
36
+
37
+ # Close the cursor and database connection
38
+ c.close()
39
+ conn.close()
40
+
41
+ def get_voxel_size(db_path):
42
+ """
43
+ Retrieves the voxel size values from an SQLite database file.
44
+
45
+ Args:
46
+ db_path (str): The path to the SQLite database file.
47
+
48
+ Returns:
49
+ list: A list containing the voxel size values as floats.
50
+
51
+ # Examples:
52
+ # If the 'voxel_size' table in the 'example.db' database file contains the values (1.0, 2.0, 3.0), then:
53
+ #
54
+ # >>> get_voxel_size('/E/Sample_images/FISH/Sample_transcription_dot_detection/manue_manually_segmented_images/training_set/tests/full_real_analysis_tst1/Coloc X2 2048/230314 Coloc X2 ZH-2A m 2048_2023_03_14__17_43_51(1)/FISH.db')
55
+ # [1.0, 2.0, 3.0]
56
+ #
57
+ # If the 'voxel_size' table does not exist in the database file:
58
+ #
59
+ # >>> get_voxel_size('/E/Sample_images/FISH/Sample_transcription_dot_detection/manue_manually_segmented_images/training_set/tests/full_real_analysis_tst1/Coloc X2 2048/230314 Coloc X2 ZH-2A m 2048_2023_03_14__17_43_51(1)/FISH.db')
60
+ # None
61
+ #
62
+ # If the database file does not exist:
63
+ #
64
+ # >>> get_voxel_size('nonexistent.db')
65
+ # None
66
+ """
67
+ if not os.path.exists(db_path):
68
+ return None
69
+
70
+ # Connect to the SQLite database
71
+ conn = sqlite3.connect(db_path)
72
+
73
+ # Create a cursor object
74
+ c = conn.cursor()
75
+
76
+ # Select the row from the 'voxel_size' table
77
+ try:
78
+ c.execute('SELECT * FROM voxel_size')
79
+ row = c.fetchone()
80
+
81
+ # Convert the row to a list
82
+ voxel_size_list = list(row)
83
+ except:
84
+ # most likely table does not exist --> return None
85
+ voxel_size_list = None
86
+
87
+ # Close the cursor and database connection
88
+ c.close()
89
+ conn.close()
90
+
91
+ return voxel_size_list
92
+
93
+
94
+ def drop_table_if_exists(db_path, table_name, verbose=True):
95
+ """
96
+ Drops an SQLite table if it exists in the specified database file.
97
+
98
+ Args:
99
+ db_path (str): The path to the SQLite database file.
100
+ table_name (str): The name of the table to drop.
101
+
102
+ Returns:
103
+ None.
104
+ """
105
+
106
+ if not os.path.exists(db_path):
107
+ if verbose:
108
+ print('sqlite file does not exist -−> skipping')
109
+ return
110
+
111
+ # Connect to the SQLite database
112
+ conn = sqlite3.connect(db_path)
113
+
114
+ # Create a cursor object
115
+ c = conn.cursor()
116
+
117
+ # Check if the table exists
118
+ c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,))
119
+ result = c.fetchone()
120
+
121
+ if result:
122
+ # Drop the table if it exists
123
+ c.execute(f"DROP TABLE {table_name}")
124
+
125
+ # Commit the transaction
126
+ conn.commit()
127
+
128
+ # Close the cursor and database connection
129
+ c.close()
130
+ conn.close()
131
+
132
+ def get_column_from_sqlite_table(db_path, table_name, column_identifier):
133
+ """
134
+ Retrieves a column from an SQLite table by name or position.
135
+
136
+ Args:
137
+ db_path (str): The path to the SQLite database file.
138
+ table_name (str): The name of the table.
139
+ column_identifier (str or int): The column name or position (starting from 0).
140
+
141
+ Returns:
142
+ list: The column data as a list.
143
+
144
+ Raises:
145
+ ValueError: If the column_identifier is invalid (neither a string nor an integer).
146
+
147
+ """
148
+ # NOT TESTED WITH INDEX −−> TODO especially with negative indices
149
+
150
+ conn = sqlite3.connect(db_path)
151
+ cursor = conn.cursor()
152
+
153
+ # Fetch the column data by name or position
154
+ if isinstance(column_identifier, str):
155
+ cursor.execute(f"SELECT {column_identifier} FROM {table_name}")
156
+ elif isinstance(column_identifier, int):
157
+ cursor.execute(f"PRAGMA table_info({table_name})")
158
+ columns = cursor.fetchall()
159
+ column_name = columns[column_identifier][1]
160
+ cursor.execute(f"SELECT {column_name} FROM {table_name}")
161
+ else:
162
+ conn.close()
163
+ raise ValueError("Invalid column_identifier. Must be either a string (column name) or an integer (position).")
164
+
165
+ column_data = cursor.fetchall()
166
+ column_data = [row[0] for row in column_data]
167
+
168
+ conn.close()
169
+
170
+ return column_data
171
+
172
+
173
+ def get_tables(path_to_db):
174
+ """
175
+ Retrieves the names of all tables in a SQLite database.
176
+
177
+ Args:
178
+ path_to_db (str): The path to the SQLite database file.
179
+
180
+ Returns:
181
+ list: A list of table names in the database.
182
+
183
+ # Examples:
184
+ # >>> get_tables('/media/teamPrudhomme/EqpPrudhomme2/Benoit_pr_Benjamin/coccinelles/latest images_20230614/raw images/N4N4_29_M_1a/ladybug_seg.db')[:2]
185
+ # ['elytras_shape', 'elytras_without_spots_gray']
186
+ """
187
+ # Establish a connection to the SQLite database
188
+ conn = sqlite3.connect(path_to_db)
189
+
190
+ # Create a cursor object to execute SQL queries
191
+ cursor = conn.cursor()
192
+
193
+ # Execute a SQL query to retrieve the names of all tables in the database
194
+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
195
+
196
+ # Fetch all the rows returned by the query and store them in the 'tables' variable
197
+ tables = cursor.fetchall()
198
+
199
+ # Close the database connection
200
+ conn.close()
201
+
202
+ # Extract the table names from the fetched rows and return them as a list
203
+ return [table[0] for table in tables]
204
+
205
+
206
+ def get_table_columns(path_to_db, table_name, force_lower_case=False):
207
+ """
208
+ Given a table name, prints the names of all the columns in the table.
209
+
210
+ Args:
211
+ path_to_db (str): The path to the SQLite database file.
212
+ table_name (str): The name of the table.
213
+
214
+ Returns:
215
+ None
216
+
217
+ Examples:
218
+ >>> get_table_columns('/media/teamPrudhomme/EqpPrudhomme2/Benoit_pr_Benjamin/coccinelles/latest images_20230614/raw images/N4N4_29_M_1a/ladybug_seg.db', 'elytras_shape')[:2]
219
+ ['areas_without_holes', 'areas_with_holes']
220
+ """
221
+
222
+ # connect to the database
223
+ conn = sqlite3.connect(path_to_db)
224
+
225
+ # create a cursor object
226
+ cur = conn.cursor()
227
+
228
+ # execute a query to get the column names
229
+ cur.execute("PRAGMA table_info({})".format(table_name))
230
+
231
+ # fetch the results and print the column names
232
+ cols = []
233
+ results = cur.fetchall()
234
+ for row in results:
235
+ # print(row)
236
+ # print('TADA', row[1].strip("'"))
237
+ cols.append(row[1].strip("'"))
238
+
239
+ if force_lower_case:
240
+ if cols:
241
+ cols = [col.lower() for col in cols]
242
+
243
+ # close the cursor and connection
244
+ cur.close()
245
+ conn.close()
246
+ return cols
247
+
248
+
249
+ '''
250
+ to be added
251
+ def set_synchronous(sync):
252
+ """
253
+ Sets the synchronous setting for a SQLite database connection.
254
+ """
255
+ # connect to the database
256
+ conn = sqlite3.connect('my_database.db')
257
+
258
+ # create a cursor object
259
+ cur = conn.cursor()
260
+
261
+ # execute the PRAGMA statement to set the synchronous setting
262
+ cur.execute("PRAGMA synchronous = {};".format("NORMAL" if sync else "OFF"))
263
+
264
+ # commit the changes and close the cursor and connection
265
+ conn.commit()
266
+ cur.close()
267
+ conn.close()
268
+
269
+ def set_memory(sync):
270
+ """
271
+ Sets the journal mode of a SQLite database connection to either MEMORY or OFF.
272
+ """
273
+ # connect to the database
274
+ conn = sqlite3.connect('my_database.db')
275
+
276
+ # create a cursor object
277
+ cur = conn.cursor()
278
+
279
+ # execute the PRAGMA statement to set the journal mode
280
+ cur.execute("PRAGMA journal_mode = {};".format("MEMORY" if sync else "OFF"))
281
+
282
+ # commit the changes and close the cursor and connection
283
+ conn.commit()
284
+ cur.close()
285
+ conn.close()
286
+
287
+ def update_table_by_hidden_index(table_name, row_index, data):
288
+ """
289
+ Updates a row in a SQLite database table using a hidden index.
290
+ """
291
+ try:
292
+ # connect to the database
293
+ conn = sqlite3.connect('my_database.db')
294
+
295
+ # create a cursor object
296
+ cur = conn.cursor()
297
+
298
+ # check if table exists
299
+ if not contains_table(table_name):
300
+ print(f"Table {table_name} does not exist.")
301
+ return False
302
+
303
+ # get the columns of the table
304
+ cols = get_columns(table_name)
305
+
306
+ # check if the row index is valid
307
+ if row_index < 1:
308
+ print("HIDDEN_TABLE_INDEX < 1 --> error")
309
+ return False
310
+
311
+ # build the SQL command to update the table
312
+ sql_command = f"UPDATE \"{table_name}\" SET "
313
+ first = True
314
+ for col_name, val in data.items():
315
+ if col_name.lower() not in cols:
316
+ print(f"Column {col_name} does not exist --> skipping")
317
+ continue
318
+ val_str = "NULL" if val is None else str(val)
319
+ if first:
320
+ sql_command += f"\"{col_name}\" = \"{val_str}\""
321
+ first = False
322
+ else:
323
+ sql_command += f", \"{col_name}\" = \"{val_str}\""
324
+ sql_command += f" WHERE HIDDEN_TABLE_INDEX = {row_index};"
325
+
326
+ # execute the SQL command and commit the changes
327
+ cur.execute(sql_command)
328
+ conn.commit()
329
+
330
+ # close the cursor and connection
331
+ cur.close()
332
+ conn.close()
333
+ except Exception as e:
334
+ print(f"Error updating table: {e}")
335
+ return False
336
+
337
+ return True
338
+
339
+
340
+
341
+ def update_table_by_row_index(table_name, row_index, data):
342
+ """
343
+ Updates a row in a SQLite database table using a row index.
344
+ """
345
+ try:
346
+ # connect to the database
347
+ conn = sqlite3.connect('my_database.db')
348
+
349
+ # create a cursor object
350
+ cur = conn.cursor()
351
+
352
+ # check if table exists
353
+ if not contains_table(table_name):
354
+ print(f"Table {table_name} does not exist.")
355
+ return False
356
+
357
+ # get the columns of the table
358
+ cols = get_columns(table_name)
359
+
360
+ # check if the row index is valid
361
+ if row_index < 1:
362
+ print("row idx < 1 --> error")
363
+ return False
364
+
365
+ # build the SQL command to update the table
366
+ sql_command = f"UPDATE \"{table_name}\" SET "
367
+ first = True
368
+ for col_name, val in data.items():
369
+ if col_name.lower() not in cols:
370
+ print(f"Column {col_name} does not exist --> skipping")
371
+ continue
372
+ val_str = "NULL" if val is None else str(val)
373
+ if first:
374
+ sql_command += f"\"{col_name}\" = \"{val_str}\""
375
+ first = False
376
+ else:
377
+ sql_command += f", \"{col_name}\" = \"{val_str}\""
378
+ sql_command += f" WHERE ROWID = {row_index};"
379
+
380
+ # execute the SQL command and commit the changes
381
+ cur.execute(sql_command)
382
+ conn.commit()
383
+
384
+ # close the cursor and connection
385
+ cur.close()
386
+ conn.close()
387
+ except Exception as e:
388
+ print(f"Error updating table: {e}")
389
+ return False
390
+
391
+ return True
392
+
393
+ def update_column(table_name, column_name, col_data):
394
+ """
395
+ Updates a column in a SQLite database table.
396
+ """
397
+ try:
398
+ # connect to the database
399
+ conn = sqlite3.connect('my_database.db')
400
+
401
+ # create a cursor object
402
+ cur = conn.cursor()
403
+
404
+ # check if table exists
405
+ if not contains_table(table_name):
406
+ print(f"Table {table_name} does not exist.")
407
+ return
408
+
409
+ # build the SQL command to update the column
410
+ count = 1
411
+ for val in col_data:
412
+ val_str = "NULL" if val is None else str(val)
413
+ sql_command = f"UPDATE \"{table_name}\" SET \"{column_name}\" = \"{val_str}\" WHERE ROWID = {count};"
414
+ cur.execute(sql_command)
415
+ count += 1
416
+
417
+ # commit the changes and close the cursor and connection
418
+ conn.commit()
419
+ cur.close()
420
+ conn.close()
421
+ except Exception as e:
422
+ print(f"Error updating column: {e}")
423
+ return
424
+
425
+ def append_to_existing_table_or_create_it_if_it_does_not_exist(table_name, data):
426
+ """
427
+ Appends data to an existing table in a SQLite database or creates the table if it does not exist.
428
+ """
429
+ try:
430
+ # connect to the database
431
+ conn = sqlite3.connect('my_database.db')
432
+
433
+ # create a cursor object
434
+ cur = conn.cursor()
435
+
436
+ # check if table exists
437
+ if not contains_table(table_name):
438
+ # create the table with columns based on the keys of the data dictionary
439
+ headers = generate_headers_with_type_from_key_set(data, ",")
440
+ create_table(table_name, headers)
441
+ else:
442
+ # check if all columns exist, create them if necessary
443
+ for key, val in data.items():
444
+ if not col_exists(table_name, key):
445
+ data_type = get_data_type(val)
446
+ add_column(table_name, key, data_type)
447
+
448
+ # add the data to the table
449
+ add_to_table_fast_object(table_name, data)
450
+
451
+ # commit the changes and close the cursor and connection
452
+ conn.commit()
453
+ cur.close()
454
+ conn.close()
455
+ except Exception as e:
456
+ print(f"Error appending to table: {e}")
457
+ return
458
+
459
+ def rename_table(old_table_name, new_table_name):
460
+ """
461
+ Renames a table in a SQLite database.
462
+ """
463
+ try:
464
+ # connect to the database
465
+ conn = sqlite3.connect('my_database.db')
466
+
467
+ # create a cursor object
468
+ cur = conn.cursor()
469
+
470
+ # check if new table name already exists
471
+ if contains_table(new_table_name):
472
+ print(f"Table {new_table_name} already exists.")
473
+ return
474
+
475
+ # rename the table
476
+ sql_query = f"ALTER TABLE \"{old_table_name}\" RENAME TO \"{new_table_name}\";"
477
+ cur.execute(sql_query)
478
+
479
+ # commit the changes and close the cursor and connection
480
+ conn.commit()
481
+ cur.close()
482
+ conn.close()
483
+ except Exception as e:
484
+ print(f"Error renaming table: {e}")
485
+ return
486
+
487
+ def add_row(table_name):
488
+ """
489
+ Adds an empty row to a table in a SQLite database.
490
+ """
491
+ try:
492
+ # connect to the database
493
+ conn = sqlite3.connect('my_database.db')
494
+
495
+ # create a cursor object
496
+ cur = conn.cursor()
497
+
498
+ # add the empty row to the table
499
+ sql_query = f"INSERT INTO \"{table_name}\" DEFAULT VALUES;"
500
+ cur.execute(sql_query)
501
+
502
+ # commit the changes and close the cursor and connection
503
+ conn.commit()
504
+ cur.close()
505
+ conn.close()
506
+ except Exception as e:
507
+ print(f"Error adding row: {e}")
508
+ return
509
+
510
+
511
+ def add_column(table_name, new_col_name, data_type=None):
512
+ """
513
+ Adds a new column to a table in a SQLite database.
514
+ """
515
+ try:
516
+ # connect to the database
517
+ conn = sqlite3.connect('my_database.db')
518
+
519
+ # create a cursor object
520
+ cur = conn.cursor()
521
+
522
+ # check if column already exists
523
+ if col_exists(table_name, new_col_name):
524
+ print(f"Column {new_col_name} already exists in table {table_name}.")
525
+ return
526
+
527
+ # add the new column to the table
528
+ if data_type is not None:
529
+ sql_query = f"ALTER TABLE \"{table_name}\" ADD COLUMN \"{new_col_name}\" {data_type};"
530
+ else:
531
+ sql_query = f"ALTER TABLE \"{table_name}\" ADD COLUMN \"{new_col_name}\";"
532
+ cur.execute(sql_query)
533
+
534
+ # commit the changes and close the cursor and connection
535
+ conn.commit()
536
+ cur.close()
537
+ conn.close()
538
+ except Exception as e:
539
+ print(f"Error adding column: {e}")
540
+ return
541
+
542
+
543
+ def swap_2_rows(table_name, row1, row2):
544
+ """
545
+ Swaps the positions of two rows in a table in a SQLite database.
546
+ """
547
+ try:
548
+ # connect to the database
549
+ conn = sqlite3.connect('my_database.db')
550
+
551
+ # create a cursor object
552
+ cur = conn.cursor()
553
+
554
+ # swap the positions of the rows
555
+ sql_query = f"UPDATE \"{table_name}\" SET HIDDEN_TABLE_INDEX = \"tmp\" WHERE HIDDEN_TABLE_INDEX = {row1};"
556
+ sql_query += f"UPDATE \"{table_name}\" SET HIDDEN_TABLE_INDEX = {row1} WHERE HIDDEN_TABLE_INDEX = {row2};"
557
+ sql_query += f"UPDATE \"{table_name}\" SET HIDDEN_TABLE_INDEX = {row2} WHERE HIDDEN_TABLE_INDEX = \"tmp\";"
558
+ cur.execute(sql_query)
559
+
560
+ # commit the changes and close the cursor and connection
561
+ conn.commit()
562
+ cur.close()
563
+ conn.close()
564
+ except Exception as e:
565
+ print(f"Error swapping rows: {e}")
566
+ return
567
+
568
+
569
+ def drop_table(table_name):
570
+ """
571
+ Drops a table from a SQLite database.
572
+ """
573
+ try:
574
+ # connect to the database
575
+ conn = sqlite3.connect('my_database.db')
576
+
577
+ # create a cursor object
578
+ cur = conn.cursor()
579
+
580
+ # drop the table
581
+ sql_query = f"DROP TABLE IF EXISTS \"{table_name}\";"
582
+ cur.execute(sql_query)
583
+
584
+ # commit the changes and close the cursor and connection
585
+ conn.commit()
586
+ cur.close()
587
+ conn.close()
588
+ except Exception as e:
589
+ print(f"Error dropping table: {e}")
590
+ return
591
+
592
+
593
+ def replace_nulls_with_SQL_NULL():
594
+ """
595
+ Replaces all occurrences of the string 'null' with the SQL NULL value in all columns of all tables in a SQLite database.
596
+ """
597
+ try:
598
+ # connect to the database
599
+ conn = sqlite3.connect('my_database.db')
600
+
601
+ # create a cursor object
602
+ cur = conn.cursor()
603
+
604
+ # replace nulls with SQL NULL in all columns of all tables
605
+ tables = get_tables()
606
+ for table in tables:
607
+ columns = get_columns(table)
608
+ for column in columns:
609
+ sql_query = f"UPDATE \"{table}\" SET \"{column}\" = REPLACE(\"{column}\", 'null', NULL) WHERE \"{column}\" = 'null';"
610
+ cur.execute(sql_query)
611
+
612
+ # commit the changes and close the cursor and connection
613
+ conn.commit()
614
+ cur.close()
615
+ conn.close()
616
+ except Exception as e:
617
+ print(f"Error replacing nulls: {e}")
618
+ return
619
+
620
+
621
+ def replace_true_with_SQL_TRUE():
622
+ """
623
+ Replaces all occurrences of the string 'true' with the SQL TRUE value in all columns of all tables in a SQLite database.
624
+ """
625
+ try:
626
+ # connect to the database
627
+ conn = sqlite3.connect('my_database.db')
628
+
629
+ # create a cursor object
630
+ cur = conn.cursor()
631
+
632
+ # replace trues with SQL TRUE in all columns of all tables
633
+ tables = get_tables()
634
+ for table in tables:
635
+ columns = get_columns(table)
636
+ for column in columns:
637
+ sql_query = f"UPDATE \"{table}\" SET \"{column}\" = REPLACE(\"{column}\", 'true', TRUE) WHERE \"{column}\" = 'true';"
638
+ cur.execute(sql_query)
639
+
640
+ # commit the changes and close the cursor and connection
641
+ conn.commit()
642
+ cur.close()
643
+ conn.close()
644
+
645
+ except Exception as e:
646
+ print(f"Error replacing trues: {e}")
647
+ return
648
+
649
+
650
+ def replace_NaN_with_SQL_NULL():
651
+ """
652
+ Replaces all occurrences of the string 'NaN' with the SQL NULL value in all columns of all tables in a SQLite database.
653
+ """
654
+ try:
655
+ # connect to the database
656
+ conn = sqlite3.connect('my_database.db')
657
+
658
+ # create a cursor object
659
+ cur = conn.cursor()
660
+
661
+ # replace NaNs with SQL NULL in all columns of all tables
662
+ tables = get_tables()
663
+ for table in tables:
664
+ columns = get_columns(table)
665
+ for column in columns:
666
+ sql_query = f"UPDATE \"{table}\" SET \"{column}\" = REPLACE(\"{column}\", 'NaN', NULL) WHERE \"{column}\" = 'NaN';"
667
+ cur.execute(sql_query)
668
+
669
+ # commit the changes and close the cursor and connection
670
+ conn.commit()
671
+ cur.close()
672
+ conn.close()
673
+ except Exception as e:
674
+ print(f"Error replacing NaNs: {e}")
675
+ return
676
+
677
+
678
+ def replace_string_in_column(column_name, string_to_replace, replacement_val):
679
+ """
680
+ Replaces all occurrences of a string in a column of all tables in a SQLite database.
681
+ """
682
+ try:
683
+ # connect to the database
684
+ conn = sqlite3.connect('my_database.db')
685
+
686
+ # create a cursor object
687
+ cur = conn.cursor()
688
+
689
+ # replace the string in the column of all tables
690
+ tables = get_tables()
691
+ for table in tables:
692
+ columns = get_columns(table)
693
+ if column_name not in columns:
694
+ continue
695
+ sql_query = f"UPDATE \"{table}\" SET \"{column_name}\" = REPLACE(\"{column_name}\", '{string_to_replace}', '{replacement_val}') WHERE \"{column_name}\" LIKE '%{string_to_replace}%';"
696
+ cur.execute(sql_query)
697
+
698
+ # commit the changes and close the cursor and connection
699
+ conn.commit()
700
+ cur.close()
701
+ conn.close()
702
+ except Exception as e:
703
+ print(f"Error replacing string: {e}")
704
+ return
705
+
706
+ def mergeSeveralDatabases(list_of_databases, force_matching_of_partially_overlapping_databases, *tables2merge):
707
+ for string in list_of_databases:
708
+ attach_db = f"ATTACH DATABASE {string} AS tmp"
709
+ executeCommand(attach_db)
710
+ for string1 in tables2merge:
711
+ if containsTable(string1):
712
+ insert_all = f"INSERT INTO {string1} SELECT * FROM tmp.{string1}"
713
+ executeCommand(insert_all)
714
+ else:
715
+ insert_all = f"CREATE TABLE {string1} AS SELECT * FROM tmp.{string1}"
716
+ executeCommand(insert_all)
717
+ detach_db = "DETACH DATABASE tmp"
718
+ executeCommand(detach_db)
719
+
720
+ def SELECT_ALL(table_name):
721
+ return f"SELECT * FROM TABLE {table_name}"
722
+
723
+ def SELECT_COLUMNS(table_name, *columns_to_select):
724
+ columns = createSelection(*columns_to_select)
725
+ return f"SELECT {columns} FROM TABLE {table_name}"
726
+
727
+ def SELECT_COLUMN(table_name, columns_to_select):
728
+ return f"SELECT {columns_to_select} FROM TABLE {table_name}"
729
+
730
+ def CREATE_TABLE_AS_SELECT(newTableName, SQLCommandUsedForCreation):
731
+ command = f"CREATE TABLE '{newTableName}' AS {SQLCommandUsedForCreation}"
732
+ if not SQLCommandUsedForCreation.strip().endswith(";"):
733
+ command += ";"
734
+ return command
735
+
736
+ def RENAME_TABLE(orig_table_name, new_table_name):
737
+ return f"ALTER TABLE '{orig_table_name}' RENAME TO '{new_table_name}';"
738
+
739
+ def createSelection(*cols):
740
+ out = ""
741
+ for string in cols:
742
+ out += string + ","
743
+ if out.endswith(","):
744
+ out = out[:-1]
745
+ return out
746
+
747
+ def createSelection(table_name, *cols):
748
+ out = ""
749
+ for string in cols:
750
+ out += f"{table_name}.{string},"
751
+ if out.endswith(","):
752
+ out = out[:-1]
753
+ return out
754
+
755
+
756
+ def SELECT_ALL_EXCEPT(table_name, *columns_to_exclude):
757
+ names = [str(column).strip() for column in columns_to_exclude]
758
+ cols = EXCEPT(table_name, names)
759
+ return SELECT_COLUMN(table_name, cols)
760
+
761
+
762
+ def EXCEPT(table_name, *columns_to_exclude):
763
+ return EXCEPT(table_name, True, columns_to_exclude)
764
+
765
+
766
+ def getAllColumnsAsASingleString(table_name):
767
+ col_name = getColumns(table_name)
768
+ out = ""
769
+ for col_name1 in col_name:
770
+ string = col_name1.strip()
771
+ out += f" {table_name}.{string},"
772
+ if out.endswith(","):
773
+ out = out[:-1]
774
+ return out
775
+
776
+
777
+ def SELECT_ALL_EXCEPT(table_name, columns_to_exclude):
778
+ return SELECT_ALL_EXCEPT(table_name, *columns_to_exclude)
779
+
780
+
781
+ def NODUPES(table_name1, table_name2):
782
+ col_name1 = getColumns(table_name1)
783
+ col_name2 = getColumns(table_name2)
784
+ col_name2 = list(set(col_name2) - set(col_name1))
785
+ columns = ""
786
+ for cur_name in col_name1:
787
+ columns += f" {table_name1}.{cur_name},"
788
+ for cur_name in col_name2:
789
+ columns += f" {table_name2}.{cur_name},"
790
+ if columns.endswith(","):
791
+ columns = columns[:-1]
792
+ columns = f"SELECT {columns} FROM TABLE {table_name1},{table_name2}"
793
+ return columns
794
+
795
+
796
+ def palette2DB(table_name, palette):
797
+ if len(palette) > 260:
798
+ palette_fused = PaletteCreator().fused_RGB_palette(palette)
799
+ else:
800
+ palette_fused = palette
801
+ values = []
802
+ header = ["gray_value", "corresponding_RGB_color", "R_only", "G_only", "B_only"]
803
+ values.append(header)
804
+ for i in range(len(palette_fused)):
805
+ row = []
806
+ RGB = palette_fused[i]
807
+ red = (RGB >> 16) & 0xFF
808
+ green = (RGB >> 8) & 0xFF
809
+ blue = RGB & 0xFF
810
+ row.append(i)
811
+ row.append(RGB)
812
+ row.append(red)
813
+ row.append(green)
814
+ row.append(blue)
815
+ values.append(row)
816
+ arrayListObjects2SQL(table_name, values)
817
+
818
+ '''
819
+
820
+
821
+ if __name__ == '__main__':
822
+
823
+ if True:
824
+ set_voxel_size('/E/Sample_images/FISH/Sample_transcription_dot_detection/manue_manually_segmented_images/training_set/tests/full_real_analysis_tst1/Coloc X2 2048/230314 Coloc X2 ZH-2A m 2048_2023_03_14__17_43_51(1)/FISH.db', (14,15,16))
825
+ import sys
826
+ sys.exit(0)
827
+
828
+ if True:
829
+ voxel_size =get_voxel_size(
830
+ '/E/Sample_images/FISH/Sample_transcription_dot_detection/manue_manually_segmented_images/training_set/tests/full_real_analysis_tst1/Coloc X2 2048/230314 Coloc X2 ZH-2A m 2048_2023_03_14__17_43_51(1)/FISH.db')
831
+ print(voxel_size)
832
+ print(voxel_size[0]*2)
833
+
834
+ import sys
835
+ sys.exit(0)
836
+
837
+ my_string = "'areas_without_holes'"
838
+ clean_string = my_string.strip("'")
839
+ print(clean_string)
840
+ print(get_tables('/media/teamPrudhomme/EqpPrudhomme2/Benoit_pr_Benjamin/coccinelles/latest images_20230614/raw images/N4N4_29_M_1a/ladybug_seg.db'))