-
-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MRG] Stage 1 of new pixels module #1985
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1985 +/- ##
==========================================
+ Coverage 97.97% 98.10% +0.12%
==========================================
Files 63 67 +4
Lines 10968 11710 +742
==========================================
+ Hits 10746 11488 +742
Misses 222 222 ☔ View full report in Codecov by Sentry. |
src/pydicom/pixels/decoders/base.py
Outdated
if self.photometric_interpretation == PI.YBR_FULL_422: | ||
# PS 3.3, Annex C.7.6.3 | ||
ybr_length = expected / 2 * 3 + expected / 2 * 3 % 2 | ||
if actual >= ybr_length: | ||
raise ValueError( | ||
"The number of bytes of pixel data is a third larger " | ||
f"than expected ({actual} vs {expected} bytes) which " | ||
"indicates the set photometric interpretation " | ||
"'YBR_FULL_422' is incorrect" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #1133, would you like a more detailed message @darcymason ?
def as_array( | ||
self, | ||
src: Buffer | Dataset, | ||
*, | ||
index: int | None = None, | ||
validate: bool = True, | ||
raw: bool = False, | ||
decoding_plugin: str = "", | ||
**kwargs: DecodeOptions, | ||
) -> "np.ndarray": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* `view_only`: :class:`bool` - if ``True`` then make a best effort | ||
attempt to return an :class:`~numpy.ndarray` that's a `view | ||
<https://numpy.org/doc/stable/user/basics.copies.html#view>`_ | ||
on the original buffer (default ``False``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
view_only
parameter and behaviour is related to #746
if force_ybr: | ||
arr = convert_color_space(arr, PI.RGB, PI.YBR_FULL) | ||
self.set_option("photometric_interpretation", PI.YBR_FULL) | ||
elif to_rgb: | ||
arr = convert_color_space(arr, PI.YBR_FULL, PI.RGB) | ||
self.set_option("photometric_interpretation", PI.RGB) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #1781 (and others), converts YBR to RGB by default
# Check to see if we have any more frames available | ||
# Should only apply to JPEG transfer syntaxes | ||
excess = [] | ||
for frame in frame_generator: | ||
if len(frame) == bytes_per_frame: | ||
excess.append(np.frombuffer(frame, dtype)) | ||
runner.set_option("number_of_frames", runner.number_of_frames + 1) | ||
|
||
if excess: | ||
warn_and_log( | ||
"More frames have been found in the encapsulated pixel data " | ||
"than expected from the supplied number of frames" | ||
) | ||
arr = np.concatenate([arr, *excess]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include any excess frames, related to #1666
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scaramallion - again a lot of new code, and I'm slower with reviewing than you have been with writing... I will finish it another day, but will post the few comments and questions I had so far.
I shall add that I like this refactoring, awesome work as usual!
decoding plugins. | ||
|
||
When possible it's recommended that the decoding function return the decoded | ||
pixel data as a :class:`bytearray` to minimise later memory usage. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minimize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/pydicom/pixels/decoders/base.py
Outdated
actual = len(self._src) | ||
|
||
if self.transfer_syntax.is_encapsulated: | ||
if actual == expected: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you add the padding before you make this check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, true. Will fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As usual, I only skimmed the tests, not checking any numbers - I trust that you to test the correct ones. Sorry for the long delay, I had kind of a stack overflow in my personal backlog...
No problem, thanks for looking at it |
Hey, I somehow missed this being MRG, haven't been checking in much lately because, well, Christmas etc., and I had a terrible cold. I'll check it out in the next couple of days. |
No worries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did about half of a review -- lots here! Just a few comments so far...
| | Key | Value | | ||
+==========================+=======================+================================================+ | ||
|:attr:`RLELosslessDecoder`| ``rle_segment_order`` | ``"<"`` for big endian segment order (default) | | ||
| | | or ``">"`` for little endian segment order | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the characters correct? ">" is big endian in Python struct module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it should be the other way around. Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So finally got through at least quickly going over most of the code, although with limited review of the tests, similar to @mrbean-bremen said.
This is quite the epic piece of work - it puts the pixel handling in a much better structure for future additions/changes.
src/pydicom/pixels/decoders/base.py
Outdated
|
||
8-bit pixel data encoded as **OW** using Explicit VR Big Endian will | ||
be yielded as-is and may need byte-swapping. To facilitate this | ||
an extra byte before the expected start (for and odd `index`) or after |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for an_ index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, here and in as_buffer
.. warning:: | ||
|
||
This method is not thread-safe. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking it could be thread-safe if the dict.pop()
was used again? And I guess a missing label
would have to be ignored.
Not that pydicom overall has actually tried to be thread-safe in any way, so I don't think it is worth worrying about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly I'm concerned about there being a single instance of the specific UID decoder, so any changes to it in one thread will propagate to the other threads. I've been trying to make everything threading/multiprocessing friendly (hopefully) since going off our citations in published papers a significant use case for our pixel data handling is machine learning/image processing pipelines.
I think this affects the encoder implementation too, I'm planning on refactoring it when I add in support for encoding J2K.
Describe the changes
pixels
modulepixels.decoders
modulepixels.decoders.base.Decoder
:as_array()
,iter_array()
,as_buffer()
anditer_buffer()
Decoder
tests/pixels/pixels_reference.py
because I'm really tired of writing the same tests over and over again.Notes:
pixels.enums
module.DecoderRunner.test_for()
methodas_array()
/iter_array()
/as_buffer()
/iter_buffer()
and referencing the corresponding decoder guides document page instead.pytest tests/pixels
ftw), but I can lump them back in with all the restFuture work
Stage 2
Stage 3
src
foras_array()
,iter_array()
,as_buffer()
anditer_buffer()
dcmread()
func(src: str | PathLike[str] | BinaryIO, ...) -> numpy.ndarray:
and a corresponding frame iteratorpxlread()
?pixel_array()
?read_array()
? Haven't thought of any really good names for it. I'd like some symmetry withdcmread()
though.And at some point
encoders
module underpixels
pixels
modulepixel_data_handlers
, switchDataset
pixel array methods overTasks