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

[Bug]: Adding list attributes to non-existing field raises TypeError #394

Open
rettigl opened this issue Jul 29, 2024 · 1 comment
Open
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@rettigl
Copy link
Collaborator

rettigl commented Jul 29, 2024

Contact Details

[email protected]

What happened?

If I try to add attributes for a non-existing field (e.g. because the field was not found in the data), then the write raises a TypeError if one of the attributes contains a list.
This seems to be happening in this line:

dataset = self.ensure_and_get_parent_node(

An example can be created by removing the value for one of the transformations in the mpes example, then the @axis attribute raises this error.

Relevant log output

Input:
del sp.binned.attrs["metadata"]["file"]["trARPES:Carving:TRX.RBV"]
sp.save(data_path + "/binned.nxs")

Output:
# save to NXmpes NeXus (including standardized metadata)
del sp.binned.attrs["metadata"]["file"]["trARPES:Carving:TRX.RBV"]
sp.save(data_path + "/binned.nxs")

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/pynxtools/src/pynxtools/dataconverter/writer.py:326, in Writer._put_data_into_hdf5(self)
    324 else:
    325     # consider changing the name here the lvalue can also be group!
--> 326     dataset = self.ensure_and_get_parent_node(
    327         path, self.data.undocumented.keys()
    328     )
    329     dataset.attrs[entry_name[1:]] = data

File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/pynxtools/src/pynxtools/dataconverter/writer.py:248, in Writer.ensure_and_get_parent_node(self, path, undocumented_paths)
    246 grp = parent.create_group(parent_path_hdf5)
--> 248 attrs = self.__nxdl_to_attrs(parent_path)
    250 if attrs is not None:

File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/pynxtools/src/pynxtools/dataconverter/writer.py:236, in Writer.__nxdl_to_attrs(self, path)
    235     if key in self.data:
--> 236         elem.attrib[attr_name.get("name")] = self.data[key]
    238 return elem.attrib

File src/lxml/etree.pyx:2447, in lxml.etree._Attrib.__setitem__()

File src/lxml/apihelpers.pxi:594, in lxml.etree._setAttributeValue()

File src/lxml/apihelpers.pxi:1539, in lxml.etree._utf8()

TypeError: Argument must be bytes or unicode, got 'list'

The above exception was the direct cause of the following exception:

OSError                                   Traceback (most recent call last)
Cell In[20], line 3
      1 # save to NXmpes NeXus (including standardized metadata)
      2 del sp.binned.attrs["metadata"]["file"]["trARPES:Carving:TRX.RBV"]
----> 3 sp.save(data_path + "/binned.nxs")

File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/sed/sed/core/processor.py:2537, in SedProcessor.save(self, faddr, **kwds)
   2534     if "eln_data" in kwds:
   2535         input_files.append(kwds.pop("eln_data"))
-> 2537     to_nexus(
   2538         data=data,
   2539         faddr=faddr,
   2540         reader=reader,
   2541         definition=definition,
   2542         input_files=input_files,
   2543         **kwds,
   2544     )
   2546 else:
   2547     raise NotImplementedError(
   2548         f"Unrecognized file format: {extension}.",
   2549     )

File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/sed/sed/io/nexus.py:40, in to_nexus(data, faddr, reader, definition, input_files, **kwds)
     37 else:
     38     input_files = tuple(input_files)
---> 40 convert(
     41     input_file=input_files,
     42     objects=(data),
     43     reader=reader,
     44     nxdl=definition,
     45     output=faddr,
     46     **kwds,
     47 )

File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/pynxtools/src/pynxtools/dataconverter/convert.py:241, in convert(input_file, reader, nxdl, output, skip_verify, **kwargs)
    231 data = transfer_data_into_template(
    232     input_file=input_file,
    233     reader=reader,
   (...)
    237     **kwargs,
    238 )
    240 helpers.add_default_root_attributes(data=data, filename=os.path.basename(output))
--> 241 Writer(data=data, nxdl_f_path=nxdl_f_path, output_path=output).write()
    243 logger.info(f"The output file generated: {output}.")

File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/pynxtools/src/pynxtools/dataconverter/writer.py:339, in Writer.write(self)
    337 """Writes the NeXus file with previously validated data from the reader with NXDL attrs."""
    338 try:
--> 339     self._put_data_into_hdf5()
    340 finally:
    341     self.output_nexus.close()

File /mnt/pcshare/users/Laurenz/AreaB/transformations_testing/pynxtools/src/pynxtools/dataconverter/writer.py:331, in Writer._put_data_into_hdf5(self)
    329         dataset.attrs[entry_name[1:]] = data
    330 except Exception as exc:
--> 331     raise IOError(
    332         f"Unknown error occured writing the path: {path} "
    333         f"with the following message: {str(exc)}"
    334     ) from exc

OSError: Unknown error occured writing the path: /ENTRY[entry]/SAMPLE[sample]/TRANSFORMATIONS[transformations]/AXISNAME[trans_x]/@depends_on with the following message: Argument must be bytes or unicode, got 'list'
@rettigl rettigl added the bug Something isn't working label Jul 29, 2024
@lukaspie
Copy link
Collaborator

I can confirm that this error occurs with the latest pynxtools and pynxtools-mpes branches. I guess the default behavior should be that these attributes are not written at all and there's a warning, right?

@lukaspie lukaspie added the good first issue Good for newcomers label Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants