Skip to content
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

Utils function for read/write value from unprocessed data #306

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ducquangkstn
Copy link
Contributor

Rationale:

  • According to this issue readNativeFrames Performance Optimizations #161, we have OOM issues and it happens when the DICOM is about 784Mb and server memory is 16Gb, and the DICOM contains multiple frames.
  • Reason 1: the pixel data is always int (64 bits - 8 bytes), even the bitAllocated can be 16 bit, which means this can takes up to 4 times of memory
  • Reason 2: the way the DICOM library storing data is [[[sample for sample in pixel] for pixel in frame] for frame in image]. B/c the number of pixel per frame is usually 1M (1000 * 1000 image) and sample per pixel can be 1 => this structure takes 4x of memory.
  • Anyway to change this issue direcly from the library will make a breaking change to end users.

Solution:

  • Thanks for contributors, we now have an option to skip processing data and the value of pixel data will be stored in byte array. It is called raw data or unprocessing data.
  • Instead of loading and manipulating the loaded 3-dimension data, we modify the data directly this unprocessed pixeldata. It can be called in-place read/write
  • This is an experimental MR. Just let me know what do you think about the interface.

// This avoids the case that we can not handle it, yet.
func IsSafeForUnprocessedValueDataHandling(info *PixelDataMetadata, unprocessedValueData []byte) error {
// https://dicom.innolitics.com/ciods/enhanced-mr-image/enhanced-mr-image/00280006
if info.PlanarConfiguration == 1 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can I use the testdata from https://github.com/pydicom/pydicom for the next MR supporting this case.

I am not good at understanding how licence works. So I am not sure whether it is appropriate.

Copy link
Owner

Choose a reason for hiding this comment

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

I think it's okay if we include their full license, but can you point me to the specific file, so I can see if it's distributed with a particular license?

Copy link
Owner

@suyashkumar suyashkumar left a comment

Choose a reason for hiding this comment

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

Thanks for this! Had a couple questions in the comments below. Is the main way this saves memory by only loading one frame at a time into the larger ints?

In general, having utilities for folks to work with the unprocessed pixel data after parsing does seem reasonable to me. That said, I wonder if we can potentially share some of the existing code to read native pixel data... will take a closer look next week or the following weekend to see if I can give a more direct suggestion. Thanks!

// This avoids the case that we can not handle it, yet.
func IsSafeForUnprocessedValueDataHandling(info *PixelDataMetadata, unprocessedValueData []byte) error {
// https://dicom.innolitics.com/ciods/enhanced-mr-image/enhanced-mr-image/00280006
if info.PlanarConfiguration == 1 {
Copy link
Owner

Choose a reason for hiding this comment

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

I think it's okay if we include their full license, but can you point me to the specific file, so I can see if it's distributed with a particular license?

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
var filesOut bytes.Buffer
require.NoError(t, dicom.Write(io.Writer(&filesOut), tc.existingData))
Copy link
Owner

Choose a reason for hiding this comment

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

nit: let's not use require/assert, and instead just use Go code to do comparisons, see https://google.github.io/styleguide/go/decisions#assert

// ReadUnprocessedValueData read the value of Dicom image directly
// from the byte array of PixelData.UnprocessedValueData with given frame ID.
// This ease the memory usage of reading DICOM image.
func ReadUnprocessedValueData(info *PixelDataMetadata, unprocessedValueData []byte, frameIndex int) ([][]int, error) {
Copy link
Owner

Choose a reason for hiding this comment

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

Since this returns [][]int, this will still have the same issue of expanding the memory from the smaller int to the 64-bit int right? Is the main place this saves memory in the fact that it only does one frame at a time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the main place this saves memory in the fact that it only does one frame at a time

Yes, most of the DICOM with the issues contains multiple frames.

But, I think I would change to a new structure of storing data.

B/c users need to adapt to new way of read/write pixel data anyway.

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.

None yet

2 participants