Skip to content

Commit 96655d8

Browse files
committed
Version 1.0.4.6: Make Mosek and pydicom optional dependency.
1 parent c28dd6a commit 96655d8

File tree

7 files changed

+53
-11
lines changed

7 files changed

+53
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img src="./images/PortPy_logo.png" width="40%" height="40%">
44
</p>
55

6-
![Version](https://img.shields.io/static/v1?label=latest&message=v1.0.4.5&color=darkgreen)
6+
![Version](https://img.shields.io/static/v1?label=latest&message=v1.0.4.6&color=darkgreen)
77
[![Total Downloads](https://static.pepy.tech/personalized-badge/portpy?period=total&units=international_system&left_color=grey&right_color=blue&left_text=total%20downloads)](https://pepy.tech/project/portpy?&left_text=totalusers)
88
[![Monthly Downloads](https://static.pepy.tech/badge/portpy/month)](https://pepy.tech/project/portpy)
99
# What is PortPy?

portpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "1.0.4.5"
1+
__version__ = "1.0.4.6"
22

33
from portpy import photon

portpy/photon/optimization.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,22 @@ def solve(self, return_cvxpy_prob=False, *args, **kwargs):
343343
problem = cp.Problem(cp.Minimize(cp.sum(self.obj)), constraints=self.constraints)
344344
print('Running Optimization..')
345345
t = time.time()
346-
problem.solve(*args, **kwargs)
346+
# Check if 'solver' is passed in args
347+
solver = kwargs.get('solver', None)
348+
if solver and solver.lower() == 'mosek':
349+
try:
350+
problem.solve(*args, **kwargs) # Attempt to solve with mosek
351+
except cp.error.SolverError as e:
352+
# Raise a custom error if MOSEK is not installed or available
353+
raise ImportError(
354+
"MOSEK solver is not installed. You can obtain the MOSEK license file by applying using an .edu account. \n"
355+
r"The license file should be placed in the directory C:\\Users\\username\\mosek."
356+
"\n To use MOSEK, install it using: pip install portpy[mosek].\n"
357+
"If a license is not available, you may try open-source or free solvers like SCS or ECOS. \n"
358+
"Please refer to the CVXPy documentation for more information about its various solvers.\n"
359+
) from e
360+
else:
361+
problem.solve(*args, **kwargs) # Continue solving with other solvers
347362
elapsed = time.time() - t
348363
self.obj_value = problem.value
349364
print("Optimal value: %s" % problem.value)

portpy/photon/utils/convert_dose_rt_dicom_to_portpy.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
from __future__ import annotations
33
import os
44
import SimpleITK as sitk
5-
from pydicom import dcmread
5+
try:
6+
from pydicom import dcmread
7+
pydicom_installed = True
8+
except ImportError:
9+
pydicom_installed = False
610
import numpy as np
711
from portpy.photon.ct import CT
812
from typing import TYPE_CHECKING
@@ -74,6 +78,10 @@ def get_ct_image(ct: CT):
7478

7579

7680
def convert_dose_rt_dicom_to_portpy(my_plan: Plan = None, ct: CT = None, dir_name: str = None, dose_file_name: str = None):
81+
if not pydicom_installed:
82+
raise ImportError(
83+
"Pydicom. To use this function, please install it with `pip install portpy[pydicom]`."
84+
)
7785
dicom_dose_image = read_dicom_dose(dir_name=dir_name, dose_file_name=dose_file_name)
7886
if ct is None:
7987
ct = my_plan.ct

portpy/photon/utils/write_rt_plan_imrt.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
from typing import TYPE_CHECKING
33
if TYPE_CHECKING:
44
from portpy.photon.plan import Plan
5-
from pydicom import dcmread
6-
from pydicom import Dataset, Sequence
5+
try:
6+
from pydicom import dcmread
7+
from pydicom import Dataset, Sequence
8+
pydicom_installed = True
9+
except ImportError:
10+
pydicom_installed = False
711

812

913
def write_rt_plan_imrt(my_plan: Plan, leaf_sequencing: dict, out_rt_plan_file: str, in_rt_plan_file: str):
@@ -23,6 +27,11 @@ def write_rt_plan_imrt(my_plan: Plan, leaf_sequencing: dict, out_rt_plan_file: s
2327
# bottom_right_y_mm = my_plan.beams.beams_dict['jaw_position'][0]['bottom_right_y_mm']
2428
# top_left_y_mm = my_plan.beams.beams_dict['jaw_position'][0]['top_left_y_mm']
2529

30+
if not pydicom_installed:
31+
raise ImportError(
32+
"Pydicom. To use this function, please install it with `pip install portpy[pydicom]`."
33+
)
34+
2635
# read rt plan file using pydicom
2736
ds = dcmread(in_rt_plan_file)
2837
for b in range(len(ds.BeamSequence)):

portpy/photon/utils/write_rt_plan_vmat.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
if TYPE_CHECKING:
44
from portpy.photon.plan import Plan
55
from portpy.photon.beam import Beams
6-
from pydicom import dcmread
7-
from pydicom import Dataset, Sequence
6+
try:
7+
from pydicom import dcmread
8+
from pydicom import Dataset, Sequence
9+
pydicom_installed = True
10+
except ImportError:
11+
pydicom_installed = False
812
import numpy as np
913

1014

@@ -173,7 +177,10 @@ def write_rt_plan_vmat(my_plan: Plan, out_rt_plan_file: str, in_rt_plan_file: st
173177
:param out_rt_plan_file: new rt plan which can be imported in TPS
174178
175179
"""
176-
180+
if not pydicom_installed:
181+
raise ImportError(
182+
"Pydicom. To use this function, please install it with `pip install portpy[pydicom]`."
183+
)
177184
# read rt plan file using pydicom
178185
ds = dcmread(in_rt_plan_file)
179186
del ds.BeamSequence

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def _get_portpy_photon_version():
5050
"cvxpy>=1.1.18",
5151
"ecos>=2.0.10",
5252
"h5py>=3.6.0",
53-
"Mosek>=9.3.14",
5453
"natsort>=8.1.0",
5554
"numpy>=1.15",
5655
"osqp>=0.4.1",
@@ -70,7 +69,11 @@ def _get_portpy_photon_version():
7069
"typing-extensions>=3.10.0.0",
7170
"scikit-image>=0.17.2",
7271
"patchify>=0.2.3",
73-
"pydicom>=2.2.0",
7472
],
73+
extras_require={
74+
'mosek': ["Mosek>=9.3.14"],
75+
'pydicom': ["pydicom>=2.2.0"],
76+
'full': ["Mosek>=9.3.14", "pydicom>=2.2.0"]
77+
}
7578

7679
)

0 commit comments

Comments
 (0)