Skip to content

Replace missing opentps.core.examples imports with local helper modules#6

Merged
Eliot-P merged 4 commits intomainfrom
copilot/remove-nonexistent-module
Feb 3, 2026
Merged

Replace missing opentps.core.examples imports with local helper modules#6
Eliot-P merged 4 commits intomainfrom
copilot/remove-nonexistent-module

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 3, 2026

Multiple example scripts imported from opentps.core.examples.{syntheticData,showStuff} which no longer exists in the opentps package.

Changes

Created local helper modules in examples/:

  • syntheticData.py - synthetic CT/4DCT generation (createSynthetic3DCT, createSynthetic4DCT, getPhasesPositions)
  • showStuff.py - visualization utilities (showModelWithAnimatedFields, dose/DVH plotting functions)

Updated 14 example scripts with direct imports:

# Before
from opentps.core.examples.syntheticData import *

# After  
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from syntheticData import *

Design notes:

  • Helper modules excluded from sphinx-gallery via existing filename_pattern: r'run_.*\.py$' config
  • No __init__.py files required
  • Standard library path manipulation (sys, os)

Affected: dynamicData (6 files), imageProcessing (4 files), Segmentation, multiplePythonEnv (2 files), registration (2 files)

Original prompt

examples/dynamicData/run_exampleMidP.py
examples/imageProcessing/exampleTransform3DCupy.py
examples/dynamicData/exampleInterFractionChanges.py
examples/Segmentation/run_Segmentation.py
examples/imageProcessing/cupyVSsitkTransforms.py

requires some opentps.core.example module that is no longer availiable. However, her is the script that they were using:

showStuff.py

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])

...


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 3, 2026 10:23
Co-authored-by: Eliot-P <54665331+Eliot-P@users.noreply.github.com>
…s.path manipulation

Co-authored-by: Eliot-P <54665331+Eliot-P@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove dependency on opentps.core.example module Replace missing opentps.core.examples with local helper modules Feb 3, 2026
Copilot AI requested a review from Eliot-P February 3, 2026 10:28
Co-authored-by: Eliot-P <54665331+Eliot-P@users.noreply.github.com>
Copilot AI changed the title Replace missing opentps.core.examples with local helper modules Replace missing opentps.core.examples imports with local helper modules Feb 3, 2026
Copy link
Copy Markdown
Member

@Eliot-P Eliot-P left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR was done using Copilot agent

@Eliot-P Eliot-P marked this pull request as ready for review February 3, 2026 11:01
@Eliot-P Eliot-P merged commit 048b699 into main Feb 3, 2026
@Eliot-P Eliot-P deleted the copilot/remove-nonexistent-module branch February 3, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants