Skip to content

Can't save image metadata #32

Closed
Closed

Description

@simppakheikki

I noticed an issue when trying to save image metadata using mrd.StreamItem.ImageComplexFloat data format.
Instead of adding metadata to the file, it throughs an error.
I use the MRD library version 2.1.2-preview. I have Window 11 with Matlab R2023b.
The error should be reproduced by running this code:

clear
w = mrd.binary.MrdWriter("testfile.mrd");
h = mrd.Header();
w.write_header(h);
img = complex(randn(100,50),randn(100,50));
im_head = mrd.ImageHeader('image_type',mrd.ImageType.COMPLEX);
meta = dictionary();
meta("StudyDescription") = "Image title";
meta("InversionTime") = 3;
meta("EchoTime") = 6;
meta("FlipAngle") = 90;
img_obj = mrd.ImageComplexFloat("data",img,"head",im_head,"meta",meta);
w.write_data(mrd.StreamItem.ImageComplexFloat(img_obj));
w.end_data();
w.close();

Output should be this error:

Unrecognized method, property, or field 'is_trivially_serializable' for class
'yardl.binary.UnionSerializer'.

Error in yardl.binary.VectorSerializerBase/write (line 40)
                if self.item_serializer_.is_trivially_serializable()

Error in yardl.binary.MapSerializer/write (line 33)
                self.value_serializer_.write(outstream, vs(i));

Error in yardl.binary.RecordSerializer/write_ (line 27)
                fs.write(outstream, field_value);

Error in mrd.binary.ImageSerializer/write (line 18)
      self.write_(outstream, value.head, value.data, value.meta);

Error in yardl.binary.UnionSerializer/write (line 48)
            serializer.write(outstream, value.value);

Error in yardl.binary.StreamSerializer/write (line 45)
                        self.item_serializer_.write(outstream, transpose(values(:, i)));

Error in mrd.binary.MrdWriter/write_data_ (line 26)
      self.data_serializer.write(self.stream_, value);

Error in mrd.MrdWriterBase/write_data (line 39)
      self.write_data_(value);

Error in mrd_error (line 16)
w.write_data(mrd.StreamItem.ImageComplexFloat(img_obj));

I expect that image metadata should save normally.
That is the simplest way tor reproduce the error.
I wonder if the error is caused by yardl because it appears to be that it cannot hand maps correctly. Image metadata has been defined as a map which is producing issues.

Activity

  1. changed the title [-]Can not save image metadata[/-] [+]Can't save image metadata[/+] on Aug 28, 2025
  2. naegelejd commented on Dec 18, 2025

    @naegelejd
    Collaborator

    Thank you for opening this issue. There are two issues here:

    1. A bug in yardl for which I opened an issue: MATLAB UnionSerializer should derive from yardl.binary.TypeSerializer microsoft/yardl#260
    2. You need to wrap values in the corresponding ImageMetaValue constructor to store them in the ImageMeta, e.g.
    w = mrd.binary.MrdWriter("testfile.mrd");
    h = mrd.Header();
    w.write_header(h);
    data = complex(randn(100,50),randn(100,50));
    head = mrd.ImageHeader(image_type = mrd.ImageType.COMPLEX);
    meta = dictionary();
    meta("StudyDescription") = mrd.ImageMetaValue.String("Image Title");
    meta("InversionTime") = mrd.ImageMetaValue.Int64(3);
    meta("EchoTime") = mrd.ImageMetaValue.Int64(6);
    meta("FlipAngle") = mrd.ImageMetaValue.Float64(90);
    img = mrd.ImageComplexFloat(data=data, head=head, meta=meta);
    w.write_data(mrd.StreamItem.ImageComplexFloat(img));
    w.end_data();
    w.close();

    This is because MATLAB's dictionary is statically typed, unlike in Python, for example.

  3. simppakheikki commented on Jan 2, 2026

    @simppakheikki
    Author

    Hi and thanks for help!

    Unfortunately, your piece of advice did not work since it gave exactly same error.
    What version and branch of MRD library you have? Which commit?

    Same error again
    `
    Unrecognized method, property, or field 'is_trivially_serializable' for class 'yardl.binary.UnionSerializer'.

    Error in yardl.binary.VectorSerializerBase/write (line 40)
    if self.item_serializer_.is_trivially_serializable()

    Error in yardl.binary.MapSerializer/write (line 33)
    self.value_serializer_.write(outstream, vs(i));

    Error in yardl.binary.RecordSerializer/write_ (line 27)
    fs.write(outstream, field_value);

    Error in mrd.binary.ImageSerializer/write (line 18)
    self.write_(outstream, value.head, value.data, value.meta);

    Error in yardl.binary.UnionSerializer/write (line 48)
    serializer.write(outstream, value.value);

    Error in yardl.binary.StreamSerializer/write (line 45)
    self.item_serializer_.write(outstream, transpose(values(:, i)));

    Error in mrd.binary.MrdWriter/write_data_ (line 26)
    self.data_serializer.write(self.stream_, value);

    Error in mrd.MrdWriterBase/write_data (line 39)
    self.write_data_(value);

    Error in untitled (line 12)
    w.write_data(mrd.StreamItem.ImageComplexFloat(img));
    `

  4. naegelejd commented on Jan 15, 2026

    @naegelejd
    Collaborator

    This will not work until the bug in Yardl is fixed and this repository is updated to use the new, fixed version of Yardl.

    That being said, I fixed the bug in Yardl, so we just need to update MRD to use yardl v0.6.6. I'll do that soon.

  5. naegelejd commented on Jan 29, 2026

    @naegelejd
    Collaborator

    This bug is now fixed in the main branch (97d3730) and the fix will be included in the next release, 2.2.0.

  6. added theissue type on Jan 29, 2026
  7. simppakheikki commented on Feb 17, 2026

    @simppakheikki
    Author

    Hi,

    Your fix works! I managed to update my code.
    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions