AOT-biomaps 2.9.177__py3-none-any.whl → 2.9.261__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 (28) hide show
  1. AOT_biomaps/AOT_Acoustic/StructuredWave.py +2 -2
  2. AOT_biomaps/AOT_Acoustic/_mainAcoustic.py +11 -6
  3. AOT_biomaps/AOT_Experiment/Tomography.py +74 -4
  4. AOT_biomaps/AOT_Experiment/_mainExperiment.py +95 -55
  5. AOT_biomaps/AOT_Recon/AOT_Optimizers/DEPIERRO.py +48 -13
  6. AOT_biomaps/AOT_Recon/AOT_Optimizers/LS.py +9 -6
  7. AOT_biomaps/AOT_Recon/AOT_Optimizers/MAPEM.py +118 -38
  8. AOT_biomaps/AOT_Recon/AOT_Optimizers/MLEM.py +268 -102
  9. AOT_biomaps/AOT_Recon/AOT_Optimizers/PDHG.py +1 -1
  10. AOT_biomaps/AOT_Recon/AOT_PotentialFunctions/RelativeDifferences.py +10 -14
  11. AOT_biomaps/AOT_Recon/AOT_SparseSMatrix/SparseSMatrix_CSR.py +252 -0
  12. AOT_biomaps/AOT_Recon/AOT_SparseSMatrix/SparseSMatrix_SELL.py +322 -0
  13. AOT_biomaps/AOT_Recon/AOT_SparseSMatrix/__init__.py +2 -0
  14. AOT_biomaps/AOT_Recon/AlgebraicRecon.py +248 -141
  15. AOT_biomaps/AOT_Recon/AnalyticRecon.py +27 -42
  16. AOT_biomaps/AOT_Recon/BayesianRecon.py +84 -151
  17. AOT_biomaps/AOT_Recon/DeepLearningRecon.py +1 -1
  18. AOT_biomaps/AOT_Recon/PrimalDualRecon.py +69 -62
  19. AOT_biomaps/AOT_Recon/ReconEnums.py +27 -2
  20. AOT_biomaps/AOT_Recon/ReconTools.py +84 -13
  21. AOT_biomaps/AOT_Recon/__init__.py +1 -0
  22. AOT_biomaps/AOT_Recon/_mainRecon.py +72 -58
  23. AOT_biomaps/__init__.py +4 -93
  24. {aot_biomaps-2.9.177.dist-info → aot_biomaps-2.9.261.dist-info}/METADATA +2 -1
  25. aot_biomaps-2.9.261.dist-info/RECORD +46 -0
  26. aot_biomaps-2.9.177.dist-info/RECORD +0 -43
  27. {aot_biomaps-2.9.177.dist-info → aot_biomaps-2.9.261.dist-info}/WHEEL +0 -0
  28. {aot_biomaps-2.9.177.dist-info → aot_biomaps-2.9.261.dist-info}/top_level.txt +0 -0
@@ -1,39 +1,49 @@
1
+ import concurrent
1
2
  from ._mainRecon import Recon
2
- from .ReconEnums import ReconType, OptimizerType, ProcessType
3
+ from .ReconEnums import ReconType, OptimizerType, ProcessType, SMatrixType
3
4
  from .AOT_Optimizers import MLEM, LS
4
- from .ReconTools import check_gpu_memory, calculate_memory_requirement, mse
5
5
  from AOT_biomaps.Config import config
6
-
6
+ from .AOT_SparseSMatrix import SparseSMatrix_CSR, SparseSMatrix_SELL
7
7
 
8
8
  import os
9
- import sys
10
9
  import subprocess
11
- import warnings
12
10
  import numpy as np
13
11
  import matplotlib.pyplot as plt
14
12
  import matplotlib.animation as animation
15
13
  from IPython.display import HTML
16
14
  from datetime import datetime
17
15
  from tempfile import gettempdir
18
-
19
-
16
+ import cupy as cp
17
+ import cupyx.scipy.sparse as cpsparse
18
+ import gc
19
+ from tqdm import trange
20
20
 
21
21
  class AlgebraicRecon(Recon):
22
22
  """
23
23
  This class implements the Algebraic reconstruction process.
24
24
  It currently does not perform any operations but serves as a template for future implementations.
25
25
  """
26
- def __init__(self, opti = OptimizerType.MLEM, numIterations = 10000, numSubsets = 1, isSavingEachIteration=True, maxSaves = 5000, alpha = None, **kwargs):
26
+ def __init__(self, opti = OptimizerType.MLEM, numIterations = 10000, numSubsets = 1, isSavingEachIteration=True, maxSaves = 5000, alpha = None, denominatorThreshold = 1e-6, smatrixType = SMatrixType.SELL, sparseThreshold=0.1, device = None, **kwargs):
27
27
  super().__init__(**kwargs)
28
28
  self.reconType = ReconType.Algebraic
29
29
  self.optimizer = opti
30
30
  self.reconPhantom = []
31
31
  self.reconLaser = []
32
+ self.indices = []
32
33
  self.numIterations = numIterations
33
34
  self.numSubsets = numSubsets
34
35
  self.isSavingEachIteration = isSavingEachIteration
35
36
  self.maxSaves = maxSaves
37
+ self.denominatorThreshold = denominatorThreshold
36
38
  self.alpha = alpha # Regularization parameter for LS
39
+ self.device = device
40
+ self.SMatrix = None # system matrix
41
+ self.smatrixType = smatrixType # SMatrixType.DENSE if no sparsing, else SMatrixType.SELL or SMatrixType.CSR or SMatrixType.COO
42
+ # Sparse matrix attributes
43
+
44
+ self.sparseThreshold = sparseThreshold
45
+
46
+ self.Z_dim = None # Used for sparse matrix reconstruction
37
47
 
38
48
  if self.numIterations <= 0:
39
49
  raise ValueError("Number of iterations must be greater than 0.")
@@ -45,38 +55,26 @@ class AlgebraicRecon(Recon):
45
55
  raise TypeError("Number of subsets must be an integer.")
46
56
 
47
57
  print("Generating system matrix (processing acoustic fields)...")
48
- self.SMatrix = np.stack([ac_field.field for ac_field in self.experiment.AcousticFields], axis=-1)
58
+ if self.smatrixType == SMatrixType.DENSE:
59
+ self.SMatrix = self._fillDenseSMatrix()
60
+ else:
61
+ self.SMatrix = self._fillSparseSMatrix(isShowLogs=True)
49
62
 
50
63
  # PUBLIC METHODS
51
64
 
52
- def run(self, processType = ProcessType.PYTHON, withTumor= True):
65
+ def run(self, processType = ProcessType.PYTHON, withTumor= True, show_logs=True):
53
66
  """
54
67
  This method is a placeholder for the Algebraic reconstruction process.
55
68
  It currently does not perform any operations but serves as a template for future implementations.
56
69
  """
57
-
58
70
  if(processType == ProcessType.CASToR):
59
- self._AlgebraicReconCASToR(withTumor)
71
+ self._AlgebraicReconCASToR(withTumor=withTumor, show_logs=show_logs)
60
72
  elif(processType == ProcessType.PYTHON):
61
- self._AlgebraicReconPython(withTumor)
73
+ self._AlgebraicReconPython(withTumor=withTumor, show_logs=show_logs)
62
74
  else:
63
75
  raise ValueError(f"Unknown Algebraic reconstruction type: {processType}")
64
-
65
- def load_reconCASToR(self,withTumor = True):
66
- if withTumor:
67
- folder = 'results_withTumor'
68
- else:
69
- folder = 'results_withoutTumor'
70
-
71
- for thetaFiles in os.path.join(self.saveDir, folder + '_{}'):
72
- if thetaFiles.endswith('.hdr'):
73
- theta = Recon.load_recon(thetaFiles)
74
- if withTumor:
75
- self.reconPhantom.append(theta)
76
- else:
77
- self.reconLaser.append(theta)
78
-
79
- def plot_MSE(self, isSaving=True, log_scale_x=False, log_scale_y=False):
76
+
77
+ def plot_MSE(self, isSaving=True, log_scale_x=False, log_scale_y=False, show_logs=True):
80
78
  """
81
79
  Plot the Mean Squared Error (MSE) of the reconstruction.
82
80
 
@@ -91,8 +89,8 @@ class AlgebraicRecon(Recon):
91
89
  raise ValueError("MSE is empty. Please calculate MSE first.")
92
90
 
93
91
  best_idx = self.indices[np.argmin(self.MSE)]
94
-
95
- print(f"Lowest MSE = {np.min(self.MSE):.4f} at iteration {best_idx+1}")
92
+ if show_logs:
93
+ print(f"Lowest MSE = {np.min(self.MSE):.4f} at iteration {best_idx+1}")
96
94
  # Plot MSE curve
97
95
  plt.figure(figsize=(7, 5))
98
96
  plt.plot(self.indices, self.MSE, 'r-', label="MSE curve")
@@ -119,22 +117,19 @@ class AlgebraicRecon(Recon):
119
117
  scale_str = "_logx"
120
118
  elif log_scale_y:
121
119
  scale_str = "_logy"
122
- if self.optimizer == OptimizerType.MLEM:
123
- SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_MSE_plot_MLEM{scale_str}{date_str}.png')
124
- elif self.optimizer == OptimizerType.LS:
125
- SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_MSE_plot_LS{scale_str}{date_str}.png')
120
+ SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_MSE_plot_{self.optimizer.name}_{scale_str}{date_str}.png')
126
121
  plt.savefig(SavingFolder, dpi=300)
127
- print(f"MSE plot saved to {SavingFolder}")
122
+ if show_logs:
123
+ print(f"MSE plot saved to {SavingFolder}")
128
124
 
129
125
  plt.show()
130
126
 
131
- def show_MSE_bestRecon(self, isSaving=True):
127
+ def show_MSE_bestRecon(self, isSaving=True, show_logs=True):
132
128
  if not self.MSE:
133
129
  raise ValueError("MSE is empty. Please calculate MSE first.")
134
130
 
135
131
 
136
132
  best_idx = np.argmin(self.MSE)
137
- print(best_idx)
138
133
  best_recon = self.reconPhantom[best_idx]
139
134
 
140
135
  # Crée la figure et les axes
@@ -163,7 +158,6 @@ class AlgebraicRecon(Recon):
163
158
 
164
159
  # Right: Reconstruction at last iteration
165
160
  lastRecon = self.reconPhantom[-1]
166
- print(lastRecon.shape)
167
161
  if self.experiment.OpticImage.phantom.shape != lastRecon.shape:
168
162
  lastRecon = lastRecon.T
169
163
  im2 = axs[2].imshow(lastRecon,
@@ -190,17 +184,14 @@ class AlgebraicRecon(Recon):
190
184
  savePath = os.path.join(self.saveDir, 'results')
191
185
  if not os.path.exists(savePath):
192
186
  os.makedirs(savePath)
193
- if self.optimizer == OptimizerType.MLEM:
194
- namePath = f'{self.SMatrix.shape[3]}_SCANS_comparison_MSE_BestANDLastRecon_MLEM_Date_{date_str}.png'
195
- elif self.optimizer == OptimizerType.LS:
196
- namePath = f'{self.SMatrix.shape[3]}_SCANS_comparison_MSE_BestANDLastRecon_LS_Date_{date_str}.png'
197
- SavingFolder = os.path.join(savePath, namePath)
187
+ SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_comparison_MSE_BestANDLastRecon_{self.optimizer.name}_{date_str}.png')
198
188
  plt.savefig(SavingFolder, dpi=300, bbox_inches='tight')
199
- print(f"MSE plot saved to {SavingFolder}")
189
+ if show_logs:
190
+ print(f"MSE plot saved to {SavingFolder}")
200
191
 
201
192
  plt.show()
202
193
 
203
- def show_theta_animation(self, vmin=None, vmax=None, total_duration_ms=3000, save_path=None, max_frames=1000, isPropMSE=True):
194
+ def show_theta_animation(self, vmin=None, vmax=None, total_duration_ms=3000, save_path=None, max_frames=1000, isPropMSE=True, show_logs=True):
204
195
  """
205
196
  Show theta iteration animation with speed proportional to MSE acceleration.
206
197
  In "propMSE" mode: slow down when MSE changes rapidly, speed up when MSE stagnates.
@@ -303,18 +294,19 @@ class AlgebraicRecon(Recon):
303
294
  ani.save(save_path, writer=animation.PillowWriter(fps=100))
304
295
  elif save_path.endswith(".mp4"):
305
296
  ani.save(save_path, writer="ffmpeg", fps=30)
306
- print(f"Animation saved to {save_path}")
297
+ if show_logs:
298
+ print(f"Animation saved to {save_path}")
307
299
 
308
300
  plt.close(fig)
309
301
  return HTML(ani.to_jshtml())
310
302
 
311
- def plot_SSIM(self, isSaving=True, log_scale_x=False, log_scale_y=False):
303
+ def plot_SSIM(self, isSaving=True, log_scale_x=False, log_scale_y=False, show_logs=True):
312
304
  if not self.SSIM:
313
305
  raise ValueError("SSIM is empty. Please calculate SSIM first.")
314
306
 
315
307
  best_idx = self.indices[np.argmax(self.SSIM)]
316
-
317
- print(f"Highest SSIM = {np.max(self.SSIM):.4f} at iteration {best_idx+1}")
308
+ if show_logs:
309
+ print(f"Highest SSIM = {np.max(self.SSIM):.4f} at iteration {best_idx+1}")
318
310
  # Plot SSIM curve
319
311
  plt.figure(figsize=(7, 5))
320
312
  plt.plot(self.indices, self.SSIM, 'r-', label="SSIM curve")
@@ -341,16 +333,14 @@ class AlgebraicRecon(Recon):
341
333
  scale_str = "_logx"
342
334
  elif log_scale_y:
343
335
  scale_str = "_logy"
344
- if self.optimizer == OptimizerType.MLEM:
345
- SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_SSIM_plot_MLEM{scale_str}{date_str}.png')
346
- elif self.optimizer == OptimizerType.LS:
347
- SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_SSIM_plot_LS{scale_str}{date_str}.png')
336
+ SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_SSIM_plot_{self.optimizer.name}_{scale_str}{date_str}.png')
348
337
  plt.savefig(SavingFolder, dpi=300)
349
- print(f"SSIM plot saved to {SavingFolder}")
338
+ if show_logs:
339
+ print(f"SSIM plot saved to {SavingFolder}")
350
340
 
351
341
  plt.show()
352
342
 
353
- def show_SSIM_bestRecon(self, isSaving=True):
343
+ def show_SSIM_bestRecon(self, isSaving=True, show_logs=True):
354
344
 
355
345
  if not self.SSIM:
356
346
  raise ValueError("SSIM is empty. Please calculate SSIM first.")
@@ -361,9 +351,6 @@ class AlgebraicRecon(Recon):
361
351
  # ----------------- Plotting -----------------
362
352
  _, axs = plt.subplots(1, 3, figsize=(15, 5)) # 1 row, 3 columns
363
353
 
364
- # Normalization based on LAMBDA max
365
- lambda_max = np.max(self.experiment.OpticImage.laser.intensity)
366
-
367
354
  # Left: Best reconstructed image (normalized)
368
355
  im0 = axs[0].imshow(best_recon,
369
356
  extent=(self.experiment.params.general['Xrange'][0], self.experiment.params.general['Xrange'][1], self.experiment.params.general['Zrange'][1], self.experiment.params.general['Zrange'][0]),
@@ -396,12 +383,13 @@ class AlgebraicRecon(Recon):
396
383
  if isSaving:
397
384
  now = datetime.now()
398
385
  date_str = now.strftime("%Y_%d_%m_%y")
399
- SavingFolder = os.path.join(self.saveDir, 'results', f'comparison_SSIM_BestANDLastRecon{date_str}.png')
386
+ SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_comparison_SSIM_BestANDLastRecon_{self.optimizer.name}_{date_str}.png')
400
387
  plt.savefig(SavingFolder, dpi=300)
401
- print(f"SSIM plot saved to {SavingFolder}")
388
+ if show_logs:
389
+ print(f"SSIM plot saved to {SavingFolder}")
402
390
  plt.show()
403
391
 
404
- def plot_CRC_vs_Noise(self, ROI_mask = None, start=0, fin=None, step=10, save_path=None):
392
+ def plot_CRC_vs_Noise(self, use_ROI=True, fin=None, isSaving=True, show_logs=True):
405
393
  """
406
394
  Plot CRC (Contrast Recovery Coefficient) vs Noise for each iteration.
407
395
  """
@@ -418,12 +406,11 @@ class AlgebraicRecon(Recon):
418
406
  fin = len(self.reconPhantom) - 1
419
407
 
420
408
  iter_range = self.indices
421
-
422
- crc_values = []
423
- noise_values = []
424
409
 
425
410
  if self.CRC is None:
426
- self.calculateCRC(use_ROI=True)
411
+ self.calculateCRC(use_ROI=use_ROI)
412
+
413
+ noise_values = []
427
414
 
428
415
  for i in iter_range:
429
416
  recon_without_tumor = self.reconLaser[i].T
@@ -432,8 +419,8 @@ class AlgebraicRecon(Recon):
432
419
  noise_values.append(noise)
433
420
 
434
421
  plt.figure(figsize=(6, 5))
435
- plt.plot(noise_values, crc_values, 'o-', label='ML-EM')
436
- for i, (x, y) in zip(iter_range, zip(noise_values, crc_values)):
422
+ plt.plot(noise_values, self.CRC, 'o-', label=self.optimizer.name)
423
+ for i, (x, y) in zip(iter_range, zip(noise_values, self.CRC)):
437
424
  plt.text(x, y, str(i), fontsize=5.5, ha='left', va='bottom')
438
425
 
439
426
  plt.xlabel("Noise (mean absolute error)")
@@ -445,13 +432,16 @@ class AlgebraicRecon(Recon):
445
432
  plt.title("CRC vs Noise over Iterations")
446
433
  plt.grid(True)
447
434
  plt.legend()
448
-
449
- if save_path:
450
- plt.savefig(save_path, dpi=300)
451
- print(f"Figure saved to: {save_path}")
435
+ if isSaving:
436
+ now = datetime.now()
437
+ date_str = now.strftime("%Y_%d_%m_%y")
438
+ SavingFolder = os.path.join(self.saveDir, f'{self.SMatrix.shape[3]}_SCANS_CRCvsNOISE_{self.optimizer.name}_{date_str}.png')
439
+ plt.savefig(SavingFolder, dpi=300)
440
+ if show_logs:
441
+ print(f"CRCvsNOISE plot saved to {SavingFolder}")
452
442
  plt.show()
453
443
 
454
- def show_reconstruction_progress(self, start=0, fin=None, save_path=None, with_tumor=True):
444
+ def show_reconstruction_progress(self, start=0, fin=None, save_path=None, with_tumor=True, show_logs=True):
455
445
  """
456
446
  Show the reconstruction progress for either with or without tumor.
457
447
  If isPropMSE is True, the frame selection is adapted to MSE changes.
@@ -556,11 +546,12 @@ class AlgebraicRecon(Recon):
556
546
  else:
557
547
  save_path = f"{save_path}_{title_suffix}"
558
548
  plt.savefig(save_path, dpi=300)
559
- print(f"Figure saved to: {save_path}")
549
+ if show_logs:
550
+ print(f"Figure saved to: {save_path}")
560
551
 
561
552
  plt.show()
562
553
 
563
- def checkExistingFile(self, withTumor, date):
554
+ def checkExistingFile(self, date = None):
564
555
  """
565
556
  Check if the reconstruction file already exists, based on current instance parameters.
566
557
 
@@ -579,18 +570,15 @@ class AlgebraicRecon(Recon):
579
570
  if not os.path.exists(results_dir):
580
571
  os.makedirs(results_dir)
581
572
 
582
- filename = 'reconPhantom.npy' if withTumor else 'reconLaser.npy'
583
- filepath = os.path.join(results_dir, filename)
584
-
585
- if os.path.exists(filepath):
586
- return (True, filepath)
587
-
588
- return (False, filepath)
573
+ if os.path.exists(os.path.join(results_dir,"indices.npy")):
574
+ return (True, results_dir)
589
575
 
576
+ return (False, results_dir)
590
577
 
591
- def load(self, withTumor=True, results_date=None, optimizer=None, filePath=None):
578
+ def load(self, withTumor=True, results_date=None, optimizer=None, filePath=None, show_logs=True):
592
579
  """
593
- Load the reconstruction results (reconPhantom or reconLaser) and indices into self.
580
+ Load the reconstruction results (reconPhantom or reconLaser) and indices as lists of 2D np arrays into self.
581
+ If the loaded file is a 3D array, it is split into a list of 2D arrays.
594
582
  Args:
595
583
  withTumor: If True, loads reconPhantom (with tumor), else reconLaser (without tumor).
596
584
  results_date: Date string (format "ddmm") to specify which results to load. If None, uses the most recent date in saveDir.
@@ -603,39 +591,68 @@ class AlgebraicRecon(Recon):
603
591
  recon_path = filePath
604
592
  if not os.path.exists(recon_path):
605
593
  raise FileNotFoundError(f"No reconstruction file found at {recon_path}.")
606
- if withTumor:
607
- self.reconPhantom = np.load(recon_path, allow_pickle=True)
594
+ # Charge le fichier (3D ou liste de 2D)
595
+ data = np.load(recon_path, allow_pickle=True)
596
+ # Découpe en liste de 2D si c'est un tableau 3D
597
+ if isinstance(data, np.ndarray) and data.ndim == 3:
598
+ if withTumor:
599
+ self.reconPhantom = [data[i, :, :] for i in range(data.shape[0])]
600
+ else:
601
+ self.reconLaser = [data[i, :, :] for i in range(data.shape[0])]
608
602
  else:
609
- self.reconLaser = np.load(recon_path, allow_pickle=True)
610
- # Essayer de charger les indices (fichier avec suffixe "_indices.npy")
611
- base_dir, file_name = os.path.split(recon_path)
612
- file_base, _ = os.path.splitext(file_name)
613
- indices_path = os.path.join(base_dir, f"{file_base}_indices.npy")
603
+ # Sinon, suppose que c'est déjà une liste de 2D
604
+ if withTumor:
605
+ self.reconPhantom = data
606
+ else:
607
+ self.reconLaser = data
608
+ # Essayer de charger les indices
609
+ base_dir, _ = os.path.split(recon_path)
610
+ indices_path = os.path.join(base_dir, 'indices.npy')
614
611
  if os.path.exists(indices_path):
615
- self.indices = np.load(indices_path, allow_pickle=True)
612
+ indices_data = np.load(indices_path, allow_pickle=True)
613
+ if isinstance(indices_data, np.ndarray) and indices_data.ndim == 3:
614
+ self.indices = [indices_data[i, :, :] for i in range(indices_data.shape[0])]
615
+ else:
616
+ self.indices = indices_data
616
617
  else:
617
618
  self.indices = None
618
- print(f"Loaded reconstruction results and indices from {recon_path}")
619
+
620
+ if show_logs:
621
+ print(f"Loaded reconstruction results and indices from {recon_path}")
619
622
  else:
620
623
  # Mode chargement depuis le répertoire de résultats
621
624
  if self.saveDir is None:
622
625
  raise ValueError("Save directory is not specified. Please set saveDir before loading.")
623
- # Determine optimizer name for path matching
626
+ # Use current optimizer and potential function if not provided
624
627
  opt_name = optimizer.value if optimizer is not None else self.optimizer.value
628
+ # Build the base directory pattern
629
+ dir_pattern = f'results_*_{opt_name}'
630
+ # Add parameters to the pattern based on the optimizer
631
+ if optimizer is None:
632
+ optimizer = self.optimizer
633
+ if optimizer == OptimizerType.PPGMLEM:
634
+ beta_str = f'_Beta_{self.beta}'
635
+ delta_str = f'_Delta_{self.delta}'
636
+ gamma_str = f'_Gamma_{self.gamma}'
637
+ sigma_str = f'_Sigma_{self.sigma}'
638
+ dir_pattern += f'{beta_str}{delta_str}{gamma_str}{sigma_str}'
639
+ elif optimizer in (OptimizerType.PGC, OptimizerType.DEPIERRO95):
640
+ beta_str = f'_Beta_{self.beta}'
641
+ sigma_str = f'_Sigma_{self.sigma}'
642
+ dir_pattern += f'{beta_str}{sigma_str}'
625
643
  # Find the most recent results directory if no date is specified
626
644
  if results_date is None:
627
- # Cherche les dossiers au format results_ddmm_MLEM
628
- dirs = [
629
- d for d in os.listdir(self.saveDir)
630
- if os.path.isdir(os.path.join(self.saveDir, d))
631
- and re.match(r'results_\d{4}_' + re.escape(opt_name) + r'($|_)', d)
632
- ]
645
+ dirs = [d for d in os.listdir(self.saveDir) if os.path.isdir(os.path.join(self.saveDir, d)) and dir_pattern in d]
633
646
  if not dirs:
634
- raise FileNotFoundError(f"No results directory found for optimizer '{opt_name}' in {self.saveDir}.")
635
- dirs.sort(reverse=True) # Most recent first (tri alphabétique inverse)
647
+ raise FileNotFoundError(f"No matching results directory found for pattern '{dir_pattern}' in {self.saveDir}.")
648
+ dirs.sort(reverse=True) # Most recent first
636
649
  results_dir = os.path.join(self.saveDir, dirs[0])
637
650
  else:
638
651
  results_dir = os.path.join(self.saveDir, f'results_{results_date}_{opt_name}')
652
+ if optimizer == OptimizerType.MLEM:
653
+ pass
654
+ elif optimizer == OptimizerType.LS:
655
+ results_dir += f'_Alpha_{self.alpha}'
639
656
  if not os.path.exists(results_dir):
640
657
  raise FileNotFoundError(f"Directory {results_dir} does not exist.")
641
658
  # Load reconstruction results
@@ -643,61 +660,145 @@ class AlgebraicRecon(Recon):
643
660
  recon_path = os.path.join(results_dir, f'{recon_key}.npy')
644
661
  if not os.path.exists(recon_path):
645
662
  raise FileNotFoundError(f"No reconstruction file found at {recon_path}.")
646
- if withTumor:
647
- self.reconPhantom = np.load(recon_path, allow_pickle=True)
663
+ data = np.load(recon_path, allow_pickle=True)
664
+ if isinstance(data, np.ndarray) and data.ndim == 3:
665
+ if withTumor:
666
+ self.reconPhantom = [data[i, :, :] for i in range(data.shape[0])]
667
+ else:
668
+ self.reconLaser = [data[i, :, :] for i in range(data.shape[0])]
648
669
  else:
649
- self.reconLaser = np.load(recon_path, allow_pickle=True)
650
- # Try to load saved indices (if file exists)
651
- indices_path = os.path.join(results_dir, f'{recon_key}_indices.npy')
652
- if os.path.exists(indices_path):
653
- self.indices = np.load(indices_path, allow_pickle=True)
670
+ if withTumor:
671
+ self.reconPhantom = data
672
+ else:
673
+ self.reconLaser = data
674
+ # Load saved indices as list of 2D arrays
675
+ indices_path = os.path.join(results_dir, 'indices.npy')
676
+ if not os.path.exists(indices_path):
677
+ raise FileNotFoundError(f"No indices file found at {indices_path}.")
678
+ indices_data = np.load(indices_path, allow_pickle=True)
679
+ if isinstance(indices_data, np.ndarray) and indices_data.ndim == 3:
680
+ self.indices = [indices_data[i, :, :] for i in range(indices_data.shape[0])]
654
681
  else:
655
- self.indices = None
656
- print(f"Loaded reconstruction results and indices from {results_dir}")
657
-
682
+ self.indices = indices_data
683
+ if show_logs:
684
+ print(f"Loaded reconstruction results and indices from {results_dir}")
685
+
658
686
  def normalizeSMatrix(self):
659
687
  self.SMatrix = self.SMatrix / (float(self.experiment.params.acoustic['voltage'])*float(self.experiment.params.acoustic['sensitivity']))
660
688
 
661
689
  # PRIVATE METHODS
662
690
 
663
- def _AlgebraicReconPython(self,withTumor):
691
+ def _fillDenseSMatrix(self):
692
+ """
693
+ Construit une matrice dense en mémoire.
694
+ """
695
+ T, Z, X = self.experiment.AcousticFields[0].field.shape
696
+ N = len(self.experiment.AcousticFields)
697
+ S = np.empty((T, Z, X, N), dtype=np.float32)
698
+ def copy_block(i):
699
+ np.copyto(S[..., i], self.experiment.AcousticFields[i].field)
700
+ with concurrent.futures.ThreadPoolExecutor() as ex:
701
+ ex.map(copy_block, range(N))
702
+ return S
703
+
704
+
705
+ def _fillSparseSMatrix(self, isShowLogs=True):
706
+ if self.smatrixType == SMatrixType.CSR:
707
+ return self._fillSparseSMatrix_CSR(isShowLogs=isShowLogs)
708
+ if self.smatrixType == SMatrixType.COO:
709
+ raise NotImplementedError("COO sparse matrix not implemented yet.")
710
+ if self.smatrixType == SMatrixType.SELL:
711
+ return self._fillSparseSMatrix_SELL(isShowLogs=isShowLogs)
712
+
713
+ def _fillSparseSMatrix_CSR(self, isShowLogs=True):
714
+ """
715
+ Construit une matrice sparse CSR par morceaux sans concaténation intermédiaire.
716
+ Libère toute la mémoire temporaire à chaque étape.
717
+ """
718
+ sparse_matrix = SparseSMatrix_CSR(self.experiment,relative_threshold=self.sparseThreshold)
719
+ sparse_matrix.allocate()
720
+ if isShowLogs:
721
+ print(f" Sparse matrix size: {sparse_matrix.getMatrixSize()} GB")
722
+ print(f"Sparse matrix density: {sparse_matrix.compute_density()}")
723
+ return sparse_matrix
724
+
725
+ def _fillSparseSMatrix_SELL(self, isShowLogs=True):
726
+ """
727
+ Construit une matrice sparse SELL par morceaux sans concaténation intermédiaire.
728
+ Libère toute la mémoire temporaire à chaque étape.
729
+ """
730
+ sparse_matrix = SparseSMatrix_SELL(self.experiment,relative_threshold=self.sparseThreshold)
731
+ sparse_matrix.allocate()
732
+ if isShowLogs:
733
+ print(f" Sparse matrix size: {sparse_matrix.getMatrixSize()} GB")
734
+ print(f"Sparse matrix density: {sparse_matrix.compute_density()}")
735
+ return sparse_matrix
736
+
737
+ def _AlgebraicReconPython(self,withTumor, show_logs):
664
738
 
665
739
  if withTumor:
666
740
  if self.experiment.AOsignal_withTumor is None:
667
741
  raise ValueError("AO signal with tumor is not available. Please generate AO signal with tumor the experiment first in the experiment object.")
668
- else:
669
- y = self.experiment.AOsignal_withTumor
670
742
  else:
671
743
  if self.experiment.AOsignal_withoutTumor is None:
672
744
  raise ValueError("AO signal without tumor is not available. Please generate AO signal without tumor the experiment first in the experiment object.")
673
- else:
674
- y = self.experiment.AOsignal_withoutTumor
675
745
 
676
746
  if self.optimizer.value == OptimizerType.MLEM.value:
677
- self.reconPhantom, self.indices = MLEM(SMatrix=self.SMatrix,
678
- y=y,
679
- numIterations=self.numIterations,
680
- isSavingEachIteration=self.isSavingEachIteration,
681
- withTumor=withTumor,
682
- use_multi_gpu= self.isMultiGPU,
683
- use_numba= self.isMultiCPU,
684
- max_saves=self.maxSaves
685
- )
747
+ if withTumor:
748
+ self.reconPhantom, self.indices = MLEM(SMatrix=self.SMatrix,
749
+ y=self.experiment.AOsignal_withTumor,
750
+ numIterations=self.numIterations,
751
+ isSavingEachIteration=self.isSavingEachIteration,
752
+ withTumor=withTumor,
753
+ device=self.device,
754
+ use_numba=self.isMultiCPU,
755
+ denominator_threshold=self.denominatorThreshold,
756
+ max_saves=self.maxSaves,
757
+ show_logs=show_logs,
758
+ smatrixType=self.smatrixType,
759
+ Z=self.Z_dim
760
+ )
761
+ else:
762
+ self.reconLaser, self.indices = MLEM(SMatrix=self.SMatrix,
763
+ y=self.experiment.AOsignal_withoutTumor,
764
+ numIterations=self.numIterations,
765
+ isSavingEachIteration=self.isSavingEachIteration,
766
+ withTumor=withTumor,
767
+ device=self.device,
768
+ use_numba=self.isMultiCPU,
769
+ denominator_threshold=self.denominatorThreshold,
770
+ max_saves=self.maxSaves,
771
+ show_logs=show_logs,
772
+ smatrixType=self.smatrixType,
773
+ Z=self.Z_dim
774
+ )
686
775
  elif self.optimizer.value == OptimizerType.LS.value:
687
776
  if self.alpha is None:
688
777
  raise ValueError("Alpha (regularization parameter) must be set for LS reconstruction.")
689
- self.reconPhantom, self.indices = LS(SMatrix=self.SMatrix,
690
- y=y,
778
+ if withTumor:
779
+ self.reconPhantom, self.indices = LS(SMatrix=self.SMatrix,
780
+ y=self.experiment.AOsignal_withTumor,
691
781
  numIterations=self.numIterations,
692
782
  isSavingEachIteration=self.isSavingEachIteration,
693
783
  withTumor=withTumor,
694
784
  alpha=self.alpha,
695
785
  max_saves=self.maxSaves,
786
+ show_logs=show_logs
787
+ )
788
+ else:
789
+ self.reconLaser, self.indices = LS(SMatrix=self.SMatrix,
790
+ y=self.experiment.AOsignal_withoutTumor,
791
+ numIterations=self.numIterations,
792
+ isSavingEachIteration=self.isSavingEachIteration,
793
+ withTumor=withTumor,
794
+ alpha=self.alpha,
795
+ max_saves=self.maxSaves,
796
+ show_logs=show_logs
696
797
  )
697
798
  else:
698
799
  raise ValueError(f"Only MLEM and LS are supported for simple algebraic reconstruction. {self.optimizer.value} need Bayesian reconstruction")
699
800
 
700
- def _AlgebraicReconCASToR(self, withTumor):
801
+ def _AlgebraicReconCASToR(self,withTumor, show_logs):
701
802
  # Définir les chemins
702
803
  smatrix = os.path.join(self.saveDir, "system_matrix")
703
804
  if withTumor:
@@ -707,14 +808,16 @@ class AlgebraicRecon(Recon):
707
808
 
708
809
  # Vérifier et générer les fichiers d'entrée si nécessaire
709
810
  if not os.path.isfile(os.path.join(self.saveDir, fileName)):
710
- print(f"Fichier .cdh manquant. Génération de {fileName}...")
811
+ if show_logs:
812
+ print(f"Fichier .cdh manquant. Génération de {fileName}...")
711
813
  self.experiment.saveAOsignals_Castor(self.saveDir)
712
814
 
713
815
  # Vérifier/générer la matrice système
714
816
  if not os.path.isdir(smatrix):
715
817
  os.makedirs(smatrix, exist_ok=True)
716
818
  if not os.listdir(smatrix):
717
- print("Matrice système manquante. Génération...")
819
+ if show_logs:
820
+ print("Matrice système manquante. Génération...")
718
821
  self.experiment.saveAcousticFields(self.saveDir)
719
822
 
720
823
  # Vérifier que le fichier .cdh existe (redondant mais sûr)
@@ -753,8 +856,9 @@ class AlgebraicRecon(Recon):
753
856
  ]
754
857
 
755
858
  # Afficher la commande (pour débogage)
756
- print("Commande CASToR :")
757
- print(" ".join(cmd))
859
+ if show_logs:
860
+ print("Commande CASToR :")
861
+ print(" ".join(cmd))
758
862
 
759
863
  # Chemin du script temporaire
760
864
  recon_script_path = os.path.join(gettempdir(), 'recon.sh')
@@ -768,17 +872,20 @@ class AlgebraicRecon(Recon):
768
872
 
769
873
  # Rendre le script exécutable et l'exécuter
770
874
  subprocess.run(["chmod", "+x", recon_script_path], check=True)
771
- print(f"Exécution de la reconstruction avec CASToR...")
875
+ if show_logs:
876
+ print(f"Exécution de la reconstruction avec CASToR...")
772
877
  result = subprocess.run(recon_script_path, env=env, check=True, capture_output=True, text=True)
773
878
 
774
879
  # Afficher la sortie de CASToR (pour débogage)
775
- print("Sortie CASToR :")
776
- print(result.stdout)
777
- if result.stderr:
778
- print("Erreurs :")
779
- print(result.stderr)
780
-
781
- print("Reconstruction terminée avec succès.")
880
+ if show_logs:
881
+ print("Sortie CASToR :")
882
+ print(result.stdout)
883
+ if result.stderr:
884
+ print("Erreurs :")
885
+ print(result.stderr)
886
+
887
+ if show_logs:
888
+ print("Reconstruction terminée avec succès.")
782
889
  self.load_reconCASToR(withTumor=withTumor)
783
890
 
784
891
  # STATIC METHODS