From a910d56469e7ab10d7d268bed88e1f0afcc1398f Mon Sep 17 00:00:00 2001 From: David Manthey Date: Wed, 9 Oct 2024 12:21:14 -0400 Subject: [PATCH 1/4] Support numpy >= 2.x on Python >= 3.8 --- src/dicomweb_client/file.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/dicomweb_client/file.py b/src/dicomweb_client/file.py index 47eab49..a6504d7 100644 --- a/src/dicomweb_client/file.py +++ b/src/dicomweb_client/file.py @@ -857,11 +857,12 @@ def _update_db(self): getattr(ds, 'NumberOfFrames', '1') ), number_of_pixels_per_frame=int( - np.prod([ + (math.prod # type: ignore + if hasattr(math, 'prod') else np.prod)([ ds.Rows, ds.Columns, ds.SamplesPerPixel, - ]) + ]) ), transfer_syntax_uid=transfer_syntax_uid, bits_allocated=ds.BitsAllocated @@ -2027,11 +2028,12 @@ def insert_instances( getattr(ds, 'NumberOfFrames', '1') ), number_of_pixels_per_frame=int( - np.prod([ + (math.prod # type: ignore + if hasattr(math, 'prod') else np.prod)([ ds.Rows, ds.Columns, ds.SamplesPerPixel - ]) + ]) ), transfer_syntax_uid=ds.file_meta.TransferSyntaxUID, bits_allocated=ds.BitsAllocated From 0049c6961161076e427fc206a081dd69f82fa746 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Tue, 15 Oct 2024 15:30:05 -0400 Subject: [PATCH 2/4] Centralize and explain the use of _prod --- src/dicomweb_client/file.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/dicomweb_client/file.py b/src/dicomweb_client/file.py index a6504d7..66c5be3 100644 --- a/src/dicomweb_client/file.py +++ b/src/dicomweb_client/file.py @@ -584,6 +584,10 @@ def __init__( end = time.time() elapsed = round(end - start) logger.info(f'updated database in {elapsed} seconds') + # numpy 2 no longer has prod, but Python >= 3.8 does. We either have + # one or the other, so use the python math.prod method when available + # and fall abck to np if not. + self._prod = math.prod if hasattr(math, 'prod') else np.prod def __getstate__(self) -> dict: """Customize state for serialization via pickle module. @@ -857,12 +861,11 @@ def _update_db(self): getattr(ds, 'NumberOfFrames', '1') ), number_of_pixels_per_frame=int( - (math.prod # type: ignore - if hasattr(math, 'prod') else np.prod)([ + self._prod([ # type: ignore ds.Rows, ds.Columns, ds.SamplesPerPixel, - ]) + ]) ), transfer_syntax_uid=transfer_syntax_uid, bits_allocated=ds.BitsAllocated @@ -2028,12 +2031,11 @@ def insert_instances( getattr(ds, 'NumberOfFrames', '1') ), number_of_pixels_per_frame=int( - (math.prod # type: ignore - if hasattr(math, 'prod') else np.prod)([ + self._prod([ # type: ignore ds.Rows, ds.Columns, ds.SamplesPerPixel - ]) + ]) ), transfer_syntax_uid=ds.file_meta.TransferSyntaxUID, bits_allocated=ds.BitsAllocated From 10764275e0387d6113650e82dce4be1590765f21 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Wed, 16 Oct 2024 09:32:05 -0400 Subject: [PATCH 3/4] Change when we determine if math has prod This needs to be done before we try to compute a prod. --- src/dicomweb_client/file.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/dicomweb_client/file.py b/src/dicomweb_client/file.py index 66c5be3..d873be6 100644 --- a/src/dicomweb_client/file.py +++ b/src/dicomweb_client/file.py @@ -565,6 +565,11 @@ def __init__( self._create_db() + # numpy 2 no longer has prod, but Python >= 3.8 does. We either have + # one or the other, so use the python math.prod method when available + # and fall abck to np if not. + self._prod = math.prod if hasattr(math, 'prod') else np.prod + self._attributes = { _QueryResourceType.STUDIES: self._get_attributes( _QueryResourceType.STUDIES @@ -584,10 +589,6 @@ def __init__( end = time.time() elapsed = round(end - start) logger.info(f'updated database in {elapsed} seconds') - # numpy 2 no longer has prod, but Python >= 3.8 does. We either have - # one or the other, so use the python math.prod method when available - # and fall abck to np if not. - self._prod = math.prod if hasattr(math, 'prod') else np.prod def __getstate__(self) -> dict: """Customize state for serialization via pickle module. From 9cfbe00da6cc24c9afa7154bac35d08752d7f1ac Mon Sep 17 00:00:00 2001 From: David Manthey Date: Wed, 16 Oct 2024 10:09:57 -0400 Subject: [PATCH 4/4] Refactor to avoid mypy issues --- src/dicomweb_client/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dicomweb_client/file.py b/src/dicomweb_client/file.py index d873be6..3e16c80 100644 --- a/src/dicomweb_client/file.py +++ b/src/dicomweb_client/file.py @@ -568,7 +568,7 @@ def __init__( # numpy 2 no longer has prod, but Python >= 3.8 does. We either have # one or the other, so use the python math.prod method when available # and fall abck to np if not. - self._prod = math.prod if hasattr(math, 'prod') else np.prod + self._prod = getattr(math, 'prod', np.prod) self._attributes = { _QueryResourceType.STUDIES: self._get_attributes(