From 1f2f7bddeb12f35e29707e790cda663908d36131 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 10:18:24 +0000 Subject: [PATCH 1/4] Initial plan From 842cf55665fb809a82bf768b530ebfbe4fc5a0cd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 10:23:11 +0000 Subject: [PATCH 2/4] Add helper modules syntheticData.py and showStuff.py and update imports Co-authored-by: Eliot-P <54665331+Eliot-P@users.noreply.github.com> --- examples/Segmentation/run_Segmentation.py | 5 +- .../exampleInterFractionChanges.py | 5 +- .../run_exampleApplyBaselineShiftToModel.py | 5 +- ...ampleDeformableBreathigDataAugmentation.py | 5 +- .../run_exampleDeformationFromWeightMaps.py | 5 +- examples/dynamicData/run_exampleMidP.py | 5 +- .../imageProcessing/cupyVSsitkTransforms.py | 7 +- .../imageProcessing/exampleTransform3DCupy.py | 7 +- .../run_exampleApplyBaselineShift.py | 5 +- .../imageProcessing/run_exampleTransform3D.py | 7 +- .../run_backAndForthParent.py | 5 +- .../run_sharedMemoryParent.py | 5 +- examples/registration/run_exampleMorphons.py | 5 +- examples/registration/run_exampleRigid.py | 5 +- examples/showStuff.py | 324 ++++++++++++++++++ examples/syntheticData.py | 176 ++++++++++ 16 files changed, 559 insertions(+), 17 deletions(-) create mode 100644 examples/showStuff.py create mode 100644 examples/syntheticData.py diff --git a/examples/Segmentation/run_Segmentation.py b/examples/Segmentation/run_Segmentation.py index a24d086..0b545a5 100644 --- a/examples/Segmentation/run_Segmentation.py +++ b/examples/Segmentation/run_Segmentation.py @@ -31,7 +31,10 @@ from opentps.core.data.images import CTImage from opentps.core.processing.segmentation.segmentation3D import applyThreshold from opentps.core.processing.segmentation.segmentationCT import SegmentationCT -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/exampleInterFractionChanges.py b/examples/dynamicData/exampleInterFractionChanges.py index df22005..2fdb36c 100644 --- a/examples/dynamicData/exampleInterFractionChanges.py +++ b/examples/dynamicData/exampleInterFractionChanges.py @@ -46,7 +46,10 @@ from opentps.core.processing.deformableDataAugmentationToolBox.modelManipFunctions import * from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData, applyTransform3D from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D -from opentps.core.examples.syntheticData import createSynthetic4DCT +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import createSynthetic4DCT logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py b/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py index acc23da..78999af 100644 --- a/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py +++ b/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py @@ -34,7 +34,10 @@ from opentps.core.processing.imageProcessing.syntheticDeformation import applyBaselineShift from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data.dynamicData._dynamic3DSequence import Dynamic3DSequence -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py b/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py index 0a639e2..5f2655d 100644 --- a/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py +++ b/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py @@ -39,7 +39,10 @@ from opentps.core.processing.deformableDataAugmentationToolBox.generateDynamicSequencesFromModel import generateDynSeqFromBreathingSignalsAndModel from opentps.core.processing.imageProcessing.imageTransform3D import getVoxelIndexFromPosition from opentps.core.processing.imageProcessing.resampler3D import resample -from opentps.core.examples.syntheticData import* +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import* logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleDeformationFromWeightMaps.py b/examples/dynamicData/run_exampleDeformationFromWeightMaps.py index 6469ee3..950c69a 100644 --- a/examples/dynamicData/run_exampleDeformationFromWeightMaps.py +++ b/examples/dynamicData/run_exampleDeformationFromWeightMaps.py @@ -31,7 +31,10 @@ from opentps.core.processing.imageProcessing import resampler3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel -from opentps.core.examples.syntheticData import createSynthetic4DCT +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import createSynthetic4DCT from opentps.core.processing.deformableDataAugmentationToolBox.weightMaps import generateDeformationFromTrackers, generateDeformationFromTrackersAndWeightMaps logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleMidP.py b/examples/dynamicData/run_exampleMidP.py index a1f76d9..73e21f6 100644 --- a/examples/dynamicData/run_exampleMidP.py +++ b/examples/dynamicData/run_exampleMidP.py @@ -33,7 +33,10 @@ from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data.dynamicData._dynamic3DSequence import Dynamic3DSequence from opentps.core.data.images import CTImage -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import * logger = logging.getLogger(__name__) #%% diff --git a/examples/imageProcessing/cupyVSsitkTransforms.py b/examples/imageProcessing/cupyVSsitkTransforms.py index c2e44a5..466c0c0 100644 --- a/examples/imageProcessing/cupyVSsitkTransforms.py +++ b/examples/imageProcessing/cupyVSsitkTransforms.py @@ -33,8 +33,11 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -from opentps.core.examples.showStuff import showModelWithAnimatedFields -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from showStuff import showModelWithAnimatedFields +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/imageProcessing/exampleTransform3DCupy.py b/examples/imageProcessing/exampleTransform3DCupy.py index fb2a733..ac4356f 100644 --- a/examples/imageProcessing/exampleTransform3DCupy.py +++ b/examples/imageProcessing/exampleTransform3DCupy.py @@ -33,8 +33,11 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -from opentps.core.examples.showStuff import showModelWithAnimatedFields -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from showStuff import showModelWithAnimatedFields +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/imageProcessing/run_exampleApplyBaselineShift.py b/examples/imageProcessing/run_exampleApplyBaselineShift.py index 34eb315..39697e8 100644 --- a/examples/imageProcessing/run_exampleApplyBaselineShift.py +++ b/examples/imageProcessing/run_exampleApplyBaselineShift.py @@ -32,7 +32,10 @@ from opentps.core.data.images import CTImage from opentps.core.data.images import ROIMask from opentps.core.processing.imageProcessing.syntheticDeformation import applyBaselineShift -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/imageProcessing/run_exampleTransform3D.py b/examples/imageProcessing/run_exampleTransform3D.py index 72d6efd..bde92d3 100644 --- a/examples/imageProcessing/run_exampleTransform3D.py +++ b/examples/imageProcessing/run_exampleTransform3D.py @@ -31,8 +31,11 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -from opentps.core.examples.showStuff import showModelWithAnimatedFields -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from showStuff import showModelWithAnimatedFields +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/multiplePythonEnv/run_backAndForthParent.py b/examples/multiplePythonEnv/run_backAndForthParent.py index 67e2408..42d5ebf 100644 --- a/examples/multiplePythonEnv/run_backAndForthParent.py +++ b/examples/multiplePythonEnv/run_backAndForthParent.py @@ -42,7 +42,10 @@ from pathlib import Path import subprocess -from opentps.core.examples.syntheticData import createSynthetic3DCT +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import createSynthetic3DCT #%% # Set the child script environnement path and child scrip file path diff --git a/examples/multiplePythonEnv/run_sharedMemoryParent.py b/examples/multiplePythonEnv/run_sharedMemoryParent.py index 537b7b2..79c359c 100644 --- a/examples/multiplePythonEnv/run_sharedMemoryParent.py +++ b/examples/multiplePythonEnv/run_sharedMemoryParent.py @@ -33,7 +33,10 @@ from multiprocessing import shared_memory from pathlib import Path -from opentps.core.examples.syntheticData import createSynthetic3DCT +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import createSynthetic3DCT #%% # set the child script environnement path and child scrip file path diff --git a/examples/registration/run_exampleMorphons.py b/examples/registration/run_exampleMorphons.py index 2498647..a710e94 100644 --- a/examples/registration/run_exampleMorphons.py +++ b/examples/registration/run_exampleMorphons.py @@ -31,7 +31,10 @@ from opentps.core.data.images import CTImage from opentps.core.processing.registration.registrationMorphons import RegistrationMorphons -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/registration/run_exampleRigid.py b/examples/registration/run_exampleRigid.py index b7b4aaf..55e9518 100644 --- a/examples/registration/run_exampleRigid.py +++ b/examples/registration/run_exampleRigid.py @@ -31,7 +31,10 @@ #import the needed opentps.core packages from opentps.core.processing.registration.registrationRigid import RegistrationRigid -from opentps.core.examples.syntheticData import * +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData diff --git a/examples/showStuff.py b/examples/showStuff.py new file mode 100644 index 0000000..ef8f1f8 --- /dev/null +++ b/examples/showStuff.py @@ -0,0 +1,324 @@ +from matplotlib import pyplot as plt +from matplotlib.animation import FuncAnimation +from scipy.ndimage import binary_dilation +import numpy as np + +from opentps.core.processing.doseCalculation.doseCalculationConfig import DoseCalculationConfig +from opentps.core.processing.doseCalculation.protons.mcsquareDoseCalculator import MCsquareDoseCalculator +from opentps.core.io import mcsquareIO +from opentps.core.io.scannerReader import readScanner +from opentps.core.data import DVH +from opentps.core.processing.planOptimization.tools import evaluateClinical +from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D, resampleImage3D + +def showModelWithAnimatedFields(model): + + for field in model.deformationList: + field.resample(spacing=model.midp.spacing, gridSize=model.midp.gridSize, origin=model.midp.origin) + + y_slice = int(model.midp.gridSize[1] / 2) + + plt.figure() + fig = plt.gcf() + + def updateAnim(imageIndex): + fig.clear() + compX = model.deformationList[imageIndex].velocity.imageArray[:, y_slice, :, 0] + compZ = model.deformationList[imageIndex].velocity.imageArray[:, y_slice, :, 2] + plt.imshow(model.midp.imageArray[:, y_slice, :][::5, ::5], cmap='gray') + plt.quiver(compZ[::5, ::5], compX[::5, ::5], alpha=0.2, color='red', angles='xy', scale_units='xy', scale=5) + + anim = FuncAnimation(fig, updateAnim, frames=len(model.deformationList), interval=300) + + # anim.save('D:/anim.gif') + plt.show() + + +def show2DMaskBorder(filledMaskSlice, color='red'): + + dilatedROI = binary_dilation(filledMaskSlice) + border = np.logical_xor(dilatedROI, filledMaskSlice) + + return border + + +def simulateAndShowOptiDoseDVH(ct, plan, roi, refSolver, figName='', outPutPath='', show=True): + # MCsquare simulation + ctCalibration = readScanner(DoseCalculationConfig().scannerFolder) + bdl = mcsquareIO.readBDL(DoseCalculationConfig().bdlFile) + + # Configure MCsquare + mc2 = MCsquareDoseCalculator() + mc2.beamModel = bdl + mc2.ctCalibration = ctCalibration + mc2.nbPrimaries = 1e7 + doseImage = mc2.computeDose(ct, plan) + + # Compute DVH on resampled contour + target_DVH = DVH(roi, doseImage) + # lung_DVH = DVH() + print('D5 - D95 = {} Gy'.format(target_DVH.D5 - target_DVH.D95)) + clinROI = [roi.name, roi.name] + clinMetric = ["Dmin", "Dmax"] + clinLimit = [59., 61.] + clinObj = {'ROI': clinROI, 'Metric': clinMetric, 'Limit': clinLimit} + print('Clinical evaluation') + evaluateClinical(doseImage, [roi], clinObj) + + # center of mass + roi = resampleImage3DOnImage3D(roi, ct) + COM_coord = roi.centerOfMass + COM_index = roi.getVoxelIndexFromPosition(COM_coord) + X_coord = COM_index[0] + Y_coord = COM_index[1] + Z_coord = COM_index[2] + + contourTargetMask = roi.getBinaryContourMask() + + img_ct1 = ct.imageArray[:, :, Z_coord].transpose(1, 0) + img_mask1 = contourTargetMask.imageArray[:, :, Z_coord].transpose(1, 0) + img_dose1 = resampleImage3DOnImage3D(doseImage, ct) + img_dose1 = img_dose1.imageArray[:, :, Z_coord].transpose(1, 0) + + img_ct2 = ct.imageArray[X_coord, :, :].transpose(1, 0) + img_mask2 = contourTargetMask.imageArray[X_coord, :, :].transpose(1, 0) + img_dose2 = resampleImage3DOnImage3D(doseImage, ct) + img_dose2 = img_dose2.imageArray[X_coord, :, :].transpose(1, 0) + + img_ct3 = ct.imageArray[:, Y_coord, :].transpose(1, 0) + img_mask3 = contourTargetMask.imageArray[:, Y_coord, :].transpose(1, 0) + img_dose3 = resampleImage3DOnImage3D(doseImage, ct) + img_dose3 = img_dose3.imageArray[:, Y_coord, :].transpose(1, 0) + + # Display dose + fig, ax = plt.subplots(2, 3, figsize=(16, 9)) + fig.suptitle(figName) + + ax[0, 0].axes.get_xaxis().set_visible(False) + ax[0, 0].axes.get_yaxis().set_visible(False) + ax[0, 0].imshow(img_ct1, cmap='gray') + ax[0, 0].imshow(img_mask1, alpha=.2, cmap='binary') # PTV + dose = ax[0, 0].imshow(img_dose1, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[0, 0]) + + ax[0, 1].axes.get_xaxis().set_visible(False) + ax[0, 1].axes.get_yaxis().set_visible(False) + ax[0, 1].imshow(img_ct2, cmap='gray') + ax[0, 1].imshow(img_mask2, alpha=.2, cmap='binary') # PTV + dose = ax[0, 1].imshow(img_dose2, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[0, 1]) + + ax[0, 2].axes.get_xaxis().set_visible(False) + ax[0, 2].axes.get_yaxis().set_visible(False) + ax[0, 2].imshow(img_ct3, cmap='gray') + ax[0, 2].imshow(img_mask3, alpha=.2, cmap='binary') # PTV + dose = ax[0, 2].imshow(img_dose3, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[0, 2]) + + ax[1, 0].plot(target_DVH.histogram[0], target_DVH.histogram[1], label=target_DVH.name) + ax[1, 0].set_xlabel("Dose (Gy)") + ax[1, 0].set_ylabel("Volume (%)") + ax[1, 0].grid(True) + ax[1, 0].legend() + + convData = refSolver.getConvergenceData() + # print("convData['time']", convData['time']) + # print("convData['nIter']", convData['nIter']) + # print("convData['func_0']", len(convData['func_0'])) + # print('i had the issue here with len(convData[func_0]) = 215 --> odd number') + # print(len(np.arange(0, convData['time'], convData['time'] / convData['nIter']))) + timeAxis = np.arange(0, convData['time'], convData['time'] / convData['nIter']) + if len(timeAxis) > len(convData['func_0']): timeAxis = timeAxis[:-1] + ax[1, 1].plot(timeAxis, convData['func_0'], 'bo-', lw=2, label='Fidelity') + ax[1, 1].set_xlabel('Time (s)') + ax[1, 1].set_ylabel('Cost') + ax[1, 1].set_yscale('symlog') + ax4 = ax[1, 1].twiny() + ax4.set_xlabel('Iterations') + ax4.set_xlim(0, convData['nIter']) + ax[1, 1].grid(True) + plt.savefig(outPutPath + figName) + if show: + plt.show() + +def simulateAndShowDoseDVH(ct, plan, roi, figName='', outPutPath='', show=True): + # MCsquare simulation + ctCalibration = readScanner(DoseCalculationConfig().scannerFolder) + bdl = mcsquareIO.readBDL(DoseCalculationConfig().bdlFile) + + # Configure MCsquare + mc2 = MCsquareDoseCalculator() + mc2.beamModel = bdl + mc2.nbPrimaries = 5e4 + mc2.ctCalibration = ctCalibration + mc2.nbPrimaries = 1e7 + doseImage = mc2.computeDose(ct, plan) + + # Compute DVH on resampled contour + target_DVH = DVH(roi, doseImage) + # lung_DVH = DVH() + print('D5 - D95 = {} Gy'.format(target_DVH.D5 - target_DVH.D95)) + clinROI = [roi.name, roi.name] + clinMetric = ["Dmin", "Dmax"] + clinLimit = [59., 61.] + clinObj = {'ROI': clinROI, 'Metric': clinMetric, 'Limit': clinLimit} + print('Clinical evaluation') + evaluateClinical(doseImage, [roi], clinObj) + + # center of mass + roi = resampleImage3DOnImage3D(roi, ct) + COM_coord = roi.centerOfMass + COM_index = roi.getVoxelIndexFromPosition(COM_coord) + X_coord = COM_index[0] + Y_coord = COM_index[1] + Z_coord = COM_index[2] + + contourTargetMask = roi.getBinaryContourMask() + + img_ct1 = ct.imageArray[:, :, Z_coord].transpose(1, 0) + img_mask1 = contourTargetMask.imageArray[:, :, Z_coord].transpose(1, 0) + img_dose1 = resampleImage3DOnImage3D(doseImage, ct) + img_dose1 = img_dose1.imageArray[:, :, Z_coord].transpose(1, 0) + + img_ct2 = np.rot90(ct.imageArray[X_coord, :, :]) + img_mask2 = np.rot90(contourTargetMask.imageArray[X_coord, :, :]) + img_dose2 = resampleImage3DOnImage3D(doseImage, ct) + img_dose2 = np.rot90(img_dose2.imageArray[X_coord, :, :]) + + img_ct3 = np.rot90(ct.imageArray[:, Y_coord, :]) + img_mask3 = np.rot90(contourTargetMask.imageArray[:, Y_coord, :]) + img_dose3 = resampleImage3DOnImage3D(doseImage, ct) + img_dose3 = np.rot90(img_dose3.imageArray[:, Y_coord, :]) + + # Display dose + fig, ax = plt.subplots(2, 2, figsize=(16, 9)) + fig.suptitle(figName) + # ax[0, 0].axes.get_xaxis().set_visible(False) + ax[0, 0].axes.get_yaxis().set_visible(False) + ax[0, 0].imshow(img_ct1, cmap='gray') + ax[0, 0].imshow(img_mask1, alpha=.2, cmap='binary') # PTV + ax[0, 0].set_xlabel('Axial') + dose = ax[0, 0].imshow(img_dose1, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[0, 0]) + + # ax[0, 1].axes.get_xaxis().set_visible(False) + ax[0, 1].axes.get_yaxis().set_visible(False) + ax[0, 1].imshow(img_ct2, cmap='gray') + ax[0, 1].imshow(img_mask2, alpha=.2, cmap='binary') # PTV + ax[0, 1].set_xlabel('Sagittal') + dose = ax[0, 1].imshow(img_dose2, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[0, 1]) + + # ax[1, 0].axes.get_xaxis().set_visible(False) + ax[1, 0].axes.get_yaxis().set_visible(False) + ax[1, 0].imshow(img_ct3, cmap='gray') + ax[1, 0].imshow(img_mask3, alpha=.2, cmap='binary') # PTV + ax[1, 0].set_xlabel('Coronal') + dose = ax[1, 0].imshow(img_dose3, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[1, 0]) + + ax[1, 1].plot(target_DVH.histogram[0], target_DVH.histogram[1], label=target_DVH.name) + ax[1, 1].set_xlabel("Dose (Gy)") + ax[1, 1].set_ylabel("Volume (%)") + ax[1, 1].grid(True) + ax[1, 1].legend() + + plt.savefig(outPutPath + figName) + if show: + plt.show() + + return doseImage + +def showDoseAndDVH(ct, doseImage, roi, figName='', outPutPath='', show=True): + + # Compute DVH on resampled contour + target_DVH = DVH(roi, doseImage) + # lung_DVH = DVH() + print('D5 - D95 = {} Gy'.format(target_DVH.D5 - target_DVH.D95)) + clinROI = [roi.name, roi.name] + clinMetric = ["Dmin", "Dmax"] + clinLimit = [59., 61.] + clinObj = {'ROI': clinROI, 'Metric': clinMetric, 'Limit': clinLimit} + print('Clinical evaluation') + evaluateClinical(doseImage, [roi], clinObj) + + # center of mass + roi = resampleImage3DOnImage3D(roi, ct) + COM_coord = roi.centerOfMass + COM_index = roi.getVoxelIndexFromPosition(COM_coord) + X_coord = COM_index[0] + Y_coord = COM_index[1] + Z_coord = COM_index[2] + + contourTargetMask = roi.getBinaryContourMask() + + img_ct1 = ct.imageArray[:, :, Z_coord].transpose(1, 0) + img_mask1 = contourTargetMask.imageArray[:, :, Z_coord].transpose(1, 0) + img_dose1 = resampleImage3DOnImage3D(doseImage, ct) + img_dose1 = img_dose1.imageArray[:, :, Z_coord].transpose(1, 0) + + img_ct2 = np.rot90(ct.imageArray[X_coord, :, :]) + img_mask2 = np.rot90(contourTargetMask.imageArray[X_coord, :, :]) + img_dose2 = resampleImage3DOnImage3D(doseImage, ct) + img_dose2 = np.rot90(img_dose2.imageArray[X_coord, :, :]) + + img_ct3 = np.rot90(ct.imageArray[:, Y_coord, :]) + img_mask3 = np.rot90(contourTargetMask.imageArray[:, Y_coord, :]) + img_dose3 = resampleImage3DOnImage3D(doseImage, ct) + img_dose3 = np.rot90(img_dose3.imageArray[:, Y_coord, :]) + + # Display dose + fig, ax = plt.subplots(2, 2, figsize=(16, 9)) + fig.suptitle(figName) + # ax[0, 0].axes.get_xaxis().set_visible(False) + ax[0, 0].axes.get_yaxis().set_visible(False) + ax[0, 0].imshow(img_ct1, cmap='gray') + ax[0, 0].imshow(img_mask1, alpha=.2, cmap='binary') # PTV + ax[0, 0].set_xlabel('Axial') + dose = ax[0, 0].imshow(img_dose1, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[0, 0]) + + # ax[0, 1].axes.get_xaxis().set_visible(False) + ax[0, 1].axes.get_yaxis().set_visible(False) + ax[0, 1].imshow(img_ct2, cmap='gray') + ax[0, 1].imshow(img_mask2, alpha=.2, cmap='binary') # PTV + ax[0, 1].set_xlabel('Sagittal') + dose = ax[0, 1].imshow(img_dose2, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[0, 1]) + + # ax[1, 0].axes.get_xaxis().set_visible(False) + ax[1, 0].axes.get_yaxis().set_visible(False) + ax[1, 0].imshow(img_ct3, cmap='gray') + ax[1, 0].imshow(img_mask3, alpha=.2, cmap='binary') # PTV + ax[1, 0].set_xlabel('Coronal') + dose = ax[1, 0].imshow(img_dose3, cmap='jet', alpha=.2) + plt.colorbar(dose, ax=ax[1, 0]) + + ax[1, 1].plot(target_DVH.histogram[0], target_DVH.histogram[1], label=target_DVH.name) + ax[1, 1].set_xlabel("Dose (Gy)") + ax[1, 1].set_ylabel("Volume (%)") + ax[1, 1].grid(True) + ax[1, 1].legend() + + plt.savefig(outPutPath + figName) + if show: + plt.show() + + return doseImage + +def show3ViewsOnTarget(image, targetCOMInVoxel, imgName='', savingPath='', show=False): + fig, axs = plt.subplots(1, 3) + + fig.suptitle('3 plane view of ' + imgName + ' centered on target') + + axs[0].imshow(np.rot90(image.imageArray[:, targetCOMInVoxel[1], :]), cmap='gray') + axs[0].set_title('Coronal View') + axs[1].imshow(np.rot90(image.imageArray[targetCOMInVoxel[0], :, :]), cmap='gray') + axs[1].set_title('Sagittal View') + axs[2].imshow(np.rot90(image.imageArray[:, :, targetCOMInVoxel[2]], 3), cmap='gray') + axs[2].set_title('Axial View') + + if savingPath: + plt.savefig(savingPath + imgName + 'Target3DView') + if show: + plt.show() diff --git a/examples/syntheticData.py b/examples/syntheticData.py new file mode 100644 index 0000000..6f0cb71 --- /dev/null +++ b/examples/syntheticData.py @@ -0,0 +1,176 @@ +import numpy as np +import math +import matplotlib.pyplot as plt + +from opentps.core.data.dynamicData._dynamic3DSequence import Dynamic3DSequence +from opentps.core.data.images._ctImage import CTImage +from opentps.core.data.images._roiMask import ROIMask + + +def createSynthetic3DCT(diaphragmPos = 20, targetPos = [50, 100, 35], spacing=[1, 1, 2], returnTumorMask = False): + # GENERATE SYNTHETIC CT IMAGE + # background + im = np.full((170, 170, 100), -1000) + im[20:150, 70:130, :] = 0 + # left lung + im[30:70, 80:120, diaphragmPos:] = -800 + # right lung + im[100:140, 80:120, diaphragmPos:] = -800 + # target + im[targetPos[0]-5:targetPos[0]+5, targetPos[1]-5:targetPos[1]+5, targetPos[2]-5:targetPos[2]+5] = 0 + # vertebral column + im[80:90, 95:105, :] = 800 + # rib + im[22:26, 90:110, 46:50] = 800 + # couch + im[:, 130:135, :] = 100 + ct = CTImage(imageArray=im, name='fixed', origin=[0, 0, 0], spacing=spacing) + + # xSlice = 45 + # ySlice = 98 + # zSlice = 48 + # plt.figure() + # plt.subplot(1, 3, 1) + # plt.imshow(ct.imageArray[xSlice, :, :].T[::-1, ::1], cmap='gray', origin='upper', vmin=-1000, vmax=1000) + # plt.subplot(1, 3, 2) + # plt.imshow(ct.imageArray[:, ySlice, :].T[::-1, ::1], cmap='gray', origin='upper', vmin=-1000, vmax=1000) + # plt.subplot(1, 3, 3) + # plt.imshow(ct.imageArray[:, :, zSlice].T[::-1, ::1], cmap='gray', origin='upper', vmin=-1000, vmax=1000) + # plt.show() + + if returnTumorMask: + mask = np.full((170, 170, 100), 0) + mask[targetPos[0]-5:targetPos[0]+5, targetPos[1]-5:targetPos[1]+5, targetPos[2]-5:targetPos[2]+5] = 1 + roi = ROIMask(imageArray=mask, origin=[0, 0, 0], spacing=spacing) + + return ct, roi + + else: + return ct + + +def createSynthetic4DCT(numberOfPhases=4, spacing=[1, 1, 2], returnTumorMasks=False, motionNoise=True): + + # GENERATE SYNTHETIC 4D INPUT SEQUENCE + CT4D = Dynamic3DSequence() + + ## For the diaphragm position + diaphMotionAmp = 12 + diaphMinPos = 20 + diaphPosList = getPhasesPositions(numberOfPhases, diaphMinPos, diaphMinPos+diaphMotionAmp) + + if motionNoise: + diaphNoise = [[3, 1], + [6, -1], + [9, -1], + [12, 1], + [15, 1]] + else: + diaphNoise = [[3, 0], + [6, 0], + [9, 0], + [12, 0], + [15, 0]] + + for elemIdx in range(len(diaphNoise)): + if diaphNoise[elemIdx][0] <= numberOfPhases - 1: + diaphPosList[diaphNoise[elemIdx][0]] += diaphNoise[elemIdx][1] + # print('diaphPosList', diaphPosList) + + ## For the target z position + zMotionAmp = int(np.round(diaphMotionAmp * 0.8)) + zMinPos = 40 + zPosList = getPhasesPositions(numberOfPhases, zMinPos, zMinPos+zMotionAmp) + + if motionNoise: + zNoise = [[3, 1], + [6, -1], + [9, -1], + [12, 1], + [15, 1]] + else: + zNoise = [[3, 0], + [6, 0], + [9, 0], + [12, 0], + [15, 0]] + + for elemIdx in range(len(zNoise)): + if zNoise[elemIdx][0] <= numberOfPhases - 1: + zPosList[zNoise[elemIdx][0]] += zNoise[elemIdx][1] + # print('zPosList', zPosList) + + ## For the target x position + xMotionAmp = 6 + xMinPos = 42 + xPosList = getPhasesPositions(numberOfPhases, xMinPos, xMinPos+xMotionAmp) + + if motionNoise: + xNoise = [[3, 1], + [6, -1], + [9, -1], + [12, 1], + [15, 1]] + else: + xNoise = [[3, 0], + [6, 0], + [9, 0], + [12, 0], + [15, 0]] + + for elemIdx in range(len(xNoise)): + if xNoise[elemIdx][0] <= numberOfPhases-1: + xPosList[xNoise[elemIdx][0]] += xNoise[elemIdx][1] + + xPosList = np.roll(xPosList, 2) + # print('xPosList', xPosList) + + phaseList = [] + if returnTumorMasks: + maskList = [] + for phaseIndex in range(numberOfPhases): + phase, mask = createSynthetic3DCT(targetPos=[xPosList[phaseIndex], 95, zPosList[phaseIndex]], diaphragmPos=diaphPosList[phaseIndex], spacing=spacing, returnTumorMask=returnTumorMasks) + phaseList.append(phase) + maskList.append(mask) + + else: + for phaseIndex in range(numberOfPhases): + phase = createSynthetic3DCT(targetPos=[xPosList[phaseIndex], 95, zPosList[phaseIndex]], diaphragmPos=diaphPosList[phaseIndex], spacing=spacing) + phaseList.append(phase) + + CT4D.dyn3DImageList = phaseList + + # # DISPLAY RESULTS + # fig = plt.figure(tight_layout=True) + # gs = gridspec.GridSpec(2, numberOfPhases) + # + # xPosList = np.append(xPosList, xPosList[0]) + # zPosList = np.append(zPosList, zPosList[0]) + # + # ax = fig.add_subplot(gs[1, int(numberOfPhases/2)]) + # ax.plot(xPosList, zPosList) + # + # y_slice = 95 + # for i in range(numberOfPhases): + # ax = fig.add_subplot(gs[0, i]) + # ax.imshow(CT4D.dyn3DImageList[i].imageArray[:, y_slice, :].T[::-1, ::1], cmap='gray', origin='upper', vmin=-1000, vmax=1000) + # ax.title.set_text('Phase' + str(i)) + # ax.scatter(xPosList[i], CT4D.dyn3DImageList[i].imageArray.shape[2] - zPosList[i]) + # + # plt.show() + + if returnTumorMasks: + return CT4D, maskList + else: + return CT4D + +def getPhasesPositions(numberOfPhases, minValue, maxValue): + + angleList = np.linspace(0, 2 * math.pi, numberOfPhases + 1)[:-1] + cosList = np.cos(angleList) + + diff = maxValue - minValue + + posList = minValue + diff / 2 + cosList * diff / 2 + + return posList.astype(np.uint8) From d51ef7ca0d87e7df48861c9fe57ef4ab088a78cd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 10:27:07 +0000 Subject: [PATCH 3/4] Refactor to use relative imports with __init__.py files instead of sys.path manipulation Co-authored-by: Eliot-P <54665331+Eliot-P@users.noreply.github.com> --- examples/Segmentation/__init__.py | 1 + examples/Segmentation/run_Segmentation.py | 5 +---- examples/__init__.py | 2 ++ examples/dynamicData/__init__.py | 1 + examples/dynamicData/exampleInterFractionChanges.py | 5 +---- .../dynamicData/run_exampleApplyBaselineShiftToModel.py | 5 +---- .../run_exampleDeformableBreathigDataAugmentation.py | 5 +---- .../dynamicData/run_exampleDeformationFromWeightMaps.py | 5 +---- examples/dynamicData/run_exampleMidP.py | 5 +---- examples/imageProcessing/__init__.py | 1 + examples/imageProcessing/cupyVSsitkTransforms.py | 7 ++----- examples/imageProcessing/exampleTransform3DCupy.py | 7 ++----- examples/imageProcessing/run_exampleApplyBaselineShift.py | 5 +---- examples/imageProcessing/run_exampleTransform3D.py | 7 ++----- examples/multiplePythonEnv/__init__.py | 1 + examples/multiplePythonEnv/run_backAndForthParent.py | 5 +---- examples/multiplePythonEnv/run_sharedMemoryParent.py | 5 +---- examples/registration/__init__.py | 1 + examples/registration/run_exampleMorphons.py | 5 +---- examples/registration/run_exampleRigid.py | 5 +---- 20 files changed, 24 insertions(+), 59 deletions(-) create mode 100644 examples/Segmentation/__init__.py create mode 100644 examples/__init__.py create mode 100644 examples/dynamicData/__init__.py create mode 100644 examples/imageProcessing/__init__.py create mode 100644 examples/multiplePythonEnv/__init__.py create mode 100644 examples/registration/__init__.py diff --git a/examples/Segmentation/__init__.py b/examples/Segmentation/__init__.py new file mode 100644 index 0000000..fb50ab7 --- /dev/null +++ b/examples/Segmentation/__init__.py @@ -0,0 +1 @@ +# This file makes the Segmentation directory a Python package diff --git a/examples/Segmentation/run_Segmentation.py b/examples/Segmentation/run_Segmentation.py index 0b545a5..7a1f8b0 100644 --- a/examples/Segmentation/run_Segmentation.py +++ b/examples/Segmentation/run_Segmentation.py @@ -31,10 +31,7 @@ from opentps.core.data.images import CTImage from opentps.core.processing.segmentation.segmentation3D import applyThreshold from opentps.core.processing.segmentation.segmentationCT import SegmentationCT -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import * +from ..syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 0000000..62bb60c --- /dev/null +++ b/examples/__init__.py @@ -0,0 +1,2 @@ +# This file makes the examples directory a Python package +# It will not be processed by sphinx-gallery due to ignore_pattern diff --git a/examples/dynamicData/__init__.py b/examples/dynamicData/__init__.py new file mode 100644 index 0000000..0360c2c --- /dev/null +++ b/examples/dynamicData/__init__.py @@ -0,0 +1 @@ +# This file makes the dynamicData directory a Python package diff --git a/examples/dynamicData/exampleInterFractionChanges.py b/examples/dynamicData/exampleInterFractionChanges.py index 2fdb36c..0b46ff7 100644 --- a/examples/dynamicData/exampleInterFractionChanges.py +++ b/examples/dynamicData/exampleInterFractionChanges.py @@ -46,10 +46,7 @@ from opentps.core.processing.deformableDataAugmentationToolBox.modelManipFunctions import * from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData, applyTransform3D from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import createSynthetic4DCT +from ..syntheticData import createSynthetic4DCT logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py b/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py index 78999af..29811b4 100644 --- a/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py +++ b/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py @@ -34,10 +34,7 @@ from opentps.core.processing.imageProcessing.syntheticDeformation import applyBaselineShift from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data.dynamicData._dynamic3DSequence import Dynamic3DSequence -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import * +from ..syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py b/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py index 5f2655d..a84191f 100644 --- a/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py +++ b/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py @@ -39,10 +39,7 @@ from opentps.core.processing.deformableDataAugmentationToolBox.generateDynamicSequencesFromModel import generateDynSeqFromBreathingSignalsAndModel from opentps.core.processing.imageProcessing.imageTransform3D import getVoxelIndexFromPosition from opentps.core.processing.imageProcessing.resampler3D import resample -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import* +from ..syntheticData import* logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleDeformationFromWeightMaps.py b/examples/dynamicData/run_exampleDeformationFromWeightMaps.py index 950c69a..40162cc 100644 --- a/examples/dynamicData/run_exampleDeformationFromWeightMaps.py +++ b/examples/dynamicData/run_exampleDeformationFromWeightMaps.py @@ -31,10 +31,7 @@ from opentps.core.processing.imageProcessing import resampler3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import createSynthetic4DCT +from ..syntheticData import createSynthetic4DCT from opentps.core.processing.deformableDataAugmentationToolBox.weightMaps import generateDeformationFromTrackers, generateDeformationFromTrackersAndWeightMaps logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleMidP.py b/examples/dynamicData/run_exampleMidP.py index 73e21f6..13ea80e 100644 --- a/examples/dynamicData/run_exampleMidP.py +++ b/examples/dynamicData/run_exampleMidP.py @@ -33,10 +33,7 @@ from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data.dynamicData._dynamic3DSequence import Dynamic3DSequence from opentps.core.data.images import CTImage -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import * +from ..syntheticData import * logger = logging.getLogger(__name__) #%% diff --git a/examples/imageProcessing/__init__.py b/examples/imageProcessing/__init__.py new file mode 100644 index 0000000..d95cfd3 --- /dev/null +++ b/examples/imageProcessing/__init__.py @@ -0,0 +1 @@ +# This file makes the imageProcessing directory a Python package diff --git a/examples/imageProcessing/cupyVSsitkTransforms.py b/examples/imageProcessing/cupyVSsitkTransforms.py index 466c0c0..d565a6a 100644 --- a/examples/imageProcessing/cupyVSsitkTransforms.py +++ b/examples/imageProcessing/cupyVSsitkTransforms.py @@ -33,11 +33,8 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from showStuff import showModelWithAnimatedFields -from syntheticData import * +from ..showStuff import showModelWithAnimatedFields +from ..syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/imageProcessing/exampleTransform3DCupy.py b/examples/imageProcessing/exampleTransform3DCupy.py index ac4356f..6bdabe6 100644 --- a/examples/imageProcessing/exampleTransform3DCupy.py +++ b/examples/imageProcessing/exampleTransform3DCupy.py @@ -33,11 +33,8 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from showStuff import showModelWithAnimatedFields -from syntheticData import * +from ..showStuff import showModelWithAnimatedFields +from ..syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/imageProcessing/run_exampleApplyBaselineShift.py b/examples/imageProcessing/run_exampleApplyBaselineShift.py index 39697e8..f07176e 100644 --- a/examples/imageProcessing/run_exampleApplyBaselineShift.py +++ b/examples/imageProcessing/run_exampleApplyBaselineShift.py @@ -32,10 +32,7 @@ from opentps.core.data.images import CTImage from opentps.core.data.images import ROIMask from opentps.core.processing.imageProcessing.syntheticDeformation import applyBaselineShift -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import * +from ..syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/imageProcessing/run_exampleTransform3D.py b/examples/imageProcessing/run_exampleTransform3D.py index bde92d3..4cefbb8 100644 --- a/examples/imageProcessing/run_exampleTransform3D.py +++ b/examples/imageProcessing/run_exampleTransform3D.py @@ -31,11 +31,8 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from showStuff import showModelWithAnimatedFields -from syntheticData import * +from ..showStuff import showModelWithAnimatedFields +from ..syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/multiplePythonEnv/__init__.py b/examples/multiplePythonEnv/__init__.py new file mode 100644 index 0000000..344bc6e --- /dev/null +++ b/examples/multiplePythonEnv/__init__.py @@ -0,0 +1 @@ +# This file makes the multiplePythonEnv directory a Python package diff --git a/examples/multiplePythonEnv/run_backAndForthParent.py b/examples/multiplePythonEnv/run_backAndForthParent.py index 42d5ebf..b33a20c 100644 --- a/examples/multiplePythonEnv/run_backAndForthParent.py +++ b/examples/multiplePythonEnv/run_backAndForthParent.py @@ -42,10 +42,7 @@ from pathlib import Path import subprocess -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import createSynthetic3DCT +from ..syntheticData import createSynthetic3DCT #%% # Set the child script environnement path and child scrip file path diff --git a/examples/multiplePythonEnv/run_sharedMemoryParent.py b/examples/multiplePythonEnv/run_sharedMemoryParent.py index 79c359c..443386b 100644 --- a/examples/multiplePythonEnv/run_sharedMemoryParent.py +++ b/examples/multiplePythonEnv/run_sharedMemoryParent.py @@ -33,10 +33,7 @@ from multiprocessing import shared_memory from pathlib import Path -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import createSynthetic3DCT +from ..syntheticData import createSynthetic3DCT #%% # set the child script environnement path and child scrip file path diff --git a/examples/registration/__init__.py b/examples/registration/__init__.py new file mode 100644 index 0000000..8a28d2b --- /dev/null +++ b/examples/registration/__init__.py @@ -0,0 +1 @@ +# This file makes the registration directory a Python package diff --git a/examples/registration/run_exampleMorphons.py b/examples/registration/run_exampleMorphons.py index a710e94..704d962 100644 --- a/examples/registration/run_exampleMorphons.py +++ b/examples/registration/run_exampleMorphons.py @@ -31,10 +31,7 @@ from opentps.core.data.images import CTImage from opentps.core.processing.registration.registrationMorphons import RegistrationMorphons -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import * +from ..syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/registration/run_exampleRigid.py b/examples/registration/run_exampleRigid.py index 55e9518..4809745 100644 --- a/examples/registration/run_exampleRigid.py +++ b/examples/registration/run_exampleRigid.py @@ -31,10 +31,7 @@ #import the needed opentps.core packages from opentps.core.processing.registration.registrationRigid import RegistrationRigid -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from syntheticData import * +from ..syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData From c9b7e7c24c47ea114311d008e892287ed08a71b6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 10:52:51 +0000 Subject: [PATCH 4/4] Remove __init__.py files and use simple direct imports Co-authored-by: Eliot-P <54665331+Eliot-P@users.noreply.github.com> --- examples/Segmentation/__init__.py | 1 - examples/Segmentation/run_Segmentation.py | 4 +++- examples/__init__.py | 2 -- examples/dynamicData/__init__.py | 1 - examples/dynamicData/exampleInterFractionChanges.py | 4 +++- .../dynamicData/run_exampleApplyBaselineShiftToModel.py | 4 +++- .../run_exampleDeformableBreathigDataAugmentation.py | 4 +++- .../dynamicData/run_exampleDeformationFromWeightMaps.py | 4 +++- examples/dynamicData/run_exampleMidP.py | 4 +++- examples/imageProcessing/__init__.py | 1 - examples/imageProcessing/cupyVSsitkTransforms.py | 6 ++++-- examples/imageProcessing/exampleTransform3DCupy.py | 6 ++++-- examples/imageProcessing/run_exampleApplyBaselineShift.py | 4 +++- examples/imageProcessing/run_exampleTransform3D.py | 6 ++++-- examples/multiplePythonEnv/__init__.py | 1 - examples/multiplePythonEnv/run_backAndForthParent.py | 5 +++-- examples/multiplePythonEnv/run_sharedMemoryParent.py | 5 +++-- examples/registration/__init__.py | 1 - examples/registration/run_exampleMorphons.py | 4 +++- examples/registration/run_exampleRigid.py | 4 +++- 20 files changed, 45 insertions(+), 26 deletions(-) delete mode 100644 examples/Segmentation/__init__.py delete mode 100644 examples/__init__.py delete mode 100644 examples/dynamicData/__init__.py delete mode 100644 examples/imageProcessing/__init__.py delete mode 100644 examples/multiplePythonEnv/__init__.py delete mode 100644 examples/registration/__init__.py diff --git a/examples/Segmentation/__init__.py b/examples/Segmentation/__init__.py deleted file mode 100644 index fb50ab7..0000000 --- a/examples/Segmentation/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# This file makes the Segmentation directory a Python package diff --git a/examples/Segmentation/run_Segmentation.py b/examples/Segmentation/run_Segmentation.py index 7a1f8b0..c365204 100644 --- a/examples/Segmentation/run_Segmentation.py +++ b/examples/Segmentation/run_Segmentation.py @@ -31,7 +31,9 @@ from opentps.core.data.images import CTImage from opentps.core.processing.segmentation.segmentation3D import applyThreshold from opentps.core.processing.segmentation.segmentationCT import SegmentationCT -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/__init__.py b/examples/__init__.py deleted file mode 100644 index 62bb60c..0000000 --- a/examples/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# This file makes the examples directory a Python package -# It will not be processed by sphinx-gallery due to ignore_pattern diff --git a/examples/dynamicData/__init__.py b/examples/dynamicData/__init__.py deleted file mode 100644 index 0360c2c..0000000 --- a/examples/dynamicData/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# This file makes the dynamicData directory a Python package diff --git a/examples/dynamicData/exampleInterFractionChanges.py b/examples/dynamicData/exampleInterFractionChanges.py index 0b46ff7..e39de39 100644 --- a/examples/dynamicData/exampleInterFractionChanges.py +++ b/examples/dynamicData/exampleInterFractionChanges.py @@ -46,7 +46,9 @@ from opentps.core.processing.deformableDataAugmentationToolBox.modelManipFunctions import * from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData, applyTransform3D from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D -from ..syntheticData import createSynthetic4DCT +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import createSynthetic4DCT logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py b/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py index 29811b4..ddecaff 100644 --- a/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py +++ b/examples/dynamicData/run_exampleApplyBaselineShiftToModel.py @@ -34,7 +34,9 @@ from opentps.core.processing.imageProcessing.syntheticDeformation import applyBaselineShift from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data.dynamicData._dynamic3DSequence import Dynamic3DSequence -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py b/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py index a84191f..d9362ce 100644 --- a/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py +++ b/examples/dynamicData/run_exampleDeformableBreathigDataAugmentation.py @@ -39,7 +39,9 @@ from opentps.core.processing.deformableDataAugmentationToolBox.generateDynamicSequencesFromModel import generateDynSeqFromBreathingSignalsAndModel from opentps.core.processing.imageProcessing.imageTransform3D import getVoxelIndexFromPosition from opentps.core.processing.imageProcessing.resampler3D import resample -from ..syntheticData import* +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import* logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleDeformationFromWeightMaps.py b/examples/dynamicData/run_exampleDeformationFromWeightMaps.py index 40162cc..f2df82f 100644 --- a/examples/dynamicData/run_exampleDeformationFromWeightMaps.py +++ b/examples/dynamicData/run_exampleDeformationFromWeightMaps.py @@ -31,7 +31,9 @@ from opentps.core.processing.imageProcessing import resampler3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel -from ..syntheticData import createSynthetic4DCT +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import createSynthetic4DCT from opentps.core.processing.deformableDataAugmentationToolBox.weightMaps import generateDeformationFromTrackers, generateDeformationFromTrackersAndWeightMaps logger = logging.getLogger(__name__) diff --git a/examples/dynamicData/run_exampleMidP.py b/examples/dynamicData/run_exampleMidP.py index 13ea80e..13cb35c 100644 --- a/examples/dynamicData/run_exampleMidP.py +++ b/examples/dynamicData/run_exampleMidP.py @@ -33,7 +33,9 @@ from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data.dynamicData._dynamic3DSequence import Dynamic3DSequence from opentps.core.data.images import CTImage -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import * logger = logging.getLogger(__name__) #%% diff --git a/examples/imageProcessing/__init__.py b/examples/imageProcessing/__init__.py deleted file mode 100644 index d95cfd3..0000000 --- a/examples/imageProcessing/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# This file makes the imageProcessing directory a Python package diff --git a/examples/imageProcessing/cupyVSsitkTransforms.py b/examples/imageProcessing/cupyVSsitkTransforms.py index d565a6a..d982d56 100644 --- a/examples/imageProcessing/cupyVSsitkTransforms.py +++ b/examples/imageProcessing/cupyVSsitkTransforms.py @@ -33,8 +33,10 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -from ..showStuff import showModelWithAnimatedFields -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from showStuff import showModelWithAnimatedFields +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/imageProcessing/exampleTransform3DCupy.py b/examples/imageProcessing/exampleTransform3DCupy.py index 6bdabe6..40d947f 100644 --- a/examples/imageProcessing/exampleTransform3DCupy.py +++ b/examples/imageProcessing/exampleTransform3DCupy.py @@ -33,8 +33,10 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -from ..showStuff import showModelWithAnimatedFields -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from showStuff import showModelWithAnimatedFields +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/imageProcessing/run_exampleApplyBaselineShift.py b/examples/imageProcessing/run_exampleApplyBaselineShift.py index f07176e..1408525 100644 --- a/examples/imageProcessing/run_exampleApplyBaselineShift.py +++ b/examples/imageProcessing/run_exampleApplyBaselineShift.py @@ -32,7 +32,9 @@ from opentps.core.data.images import CTImage from opentps.core.data.images import ROIMask from opentps.core.processing.imageProcessing.syntheticDeformation import applyBaselineShift -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/imageProcessing/run_exampleTransform3D.py b/examples/imageProcessing/run_exampleTransform3D.py index 4cefbb8..3801004 100644 --- a/examples/imageProcessing/run_exampleTransform3D.py +++ b/examples/imageProcessing/run_exampleTransform3D.py @@ -31,8 +31,10 @@ from opentps.core.data.images import VectorField3D from opentps.core.data.dynamicData._dynamic3DModel import Dynamic3DModel from opentps.core.data._transform3D import Transform3D -from ..showStuff import showModelWithAnimatedFields -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from showStuff import showModelWithAnimatedFields +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData from opentps.core.processing.imageProcessing.resampler3D import resample diff --git a/examples/multiplePythonEnv/__init__.py b/examples/multiplePythonEnv/__init__.py deleted file mode 100644 index 344bc6e..0000000 --- a/examples/multiplePythonEnv/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# This file makes the multiplePythonEnv directory a Python package diff --git a/examples/multiplePythonEnv/run_backAndForthParent.py b/examples/multiplePythonEnv/run_backAndForthParent.py index b33a20c..1bb3a14 100644 --- a/examples/multiplePythonEnv/run_backAndForthParent.py +++ b/examples/multiplePythonEnv/run_backAndForthParent.py @@ -41,8 +41,9 @@ from subprocess import Popen, PIPE from pathlib import Path import subprocess - -from ..syntheticData import createSynthetic3DCT +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import createSynthetic3DCT #%% # Set the child script environnement path and child scrip file path diff --git a/examples/multiplePythonEnv/run_sharedMemoryParent.py b/examples/multiplePythonEnv/run_sharedMemoryParent.py index 443386b..9120719 100644 --- a/examples/multiplePythonEnv/run_sharedMemoryParent.py +++ b/examples/multiplePythonEnv/run_sharedMemoryParent.py @@ -32,8 +32,9 @@ import subprocess from multiprocessing import shared_memory from pathlib import Path - -from ..syntheticData import createSynthetic3DCT +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import createSynthetic3DCT #%% # set the child script environnement path and child scrip file path diff --git a/examples/registration/__init__.py b/examples/registration/__init__.py deleted file mode 100644 index 8a28d2b..0000000 --- a/examples/registration/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# This file makes the registration directory a Python package diff --git a/examples/registration/run_exampleMorphons.py b/examples/registration/run_exampleMorphons.py index 704d962..f493496 100644 --- a/examples/registration/run_exampleMorphons.py +++ b/examples/registration/run_exampleMorphons.py @@ -31,7 +31,9 @@ from opentps.core.data.images import CTImage from opentps.core.processing.registration.registrationMorphons import RegistrationMorphons -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import * logger = logging.getLogger(__name__) diff --git a/examples/registration/run_exampleRigid.py b/examples/registration/run_exampleRigid.py index 4809745..a9bda39 100644 --- a/examples/registration/run_exampleRigid.py +++ b/examples/registration/run_exampleRigid.py @@ -31,7 +31,9 @@ #import the needed opentps.core packages from opentps.core.processing.registration.registrationRigid import RegistrationRigid -from ..syntheticData import * +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from syntheticData import * from opentps.core.processing.imageProcessing.resampler3D import resampleImage3DOnImage3D from opentps.core.processing.imageProcessing.imageTransform3D import rotateData, translateData