sasdata.dataloader.readers.cansas_reader module

class sasdata.dataloader.readers.cansas_reader.Reader(xml=None, schema=None)

Bases: XMLreader

__annotations__ = {}
__doc__ = None
__module__ = 'sasdata.dataloader.readers.cansas_reader'
_add_intermediate()

This method stores any intermediate objects within the final data set after fully reading the set.

_check_origin(entry_node: ElementTree, doc: ElementTree)

Return the document, and the SASentry node associated with the data we just wrote. If the calling function was not the cansas reader, return a minidom object rather than an lxml object.

Parameters:
  • entry_node – lxml node ElementTree object to be appended to

  • doc – entire xml tree

_create_main_node() ElementTree

Creates the primary xml header used when writing to file

_define_mod_name()
_get_node_value(node: ElementTree, tagname: str)

Get the value of a node and any applicable units

Parameters:
  • node – The XML node to get the value of

  • tagname – The tagname of the node

_get_pi_string() str

Creates the processing instructions header for writing to file

_initialize_new_data_set(node=None)
_is_call_local()
_parse_entry(dom: ~lxml.etree.ElementTree, recurse: bool | None = False) -> (<class 'sasdata.dataloader.data_info.Data1D'>, None)
_store_content(location, node, variable, storage)

Get the content of a xpath location and store the result. The value is treated as a string.

The xpath location might or might not exist. If it does not exist, nothing is done

Parameters:
  • location – xpath location to fetch

  • node – node to read the data from

  • variable – name of the data member to store it in [string]

  • storage – data object that has the ‘variable’ data member

Returns:

return a list of errors

_store_float(location, node, variable, storage, optional=True)

Get the content of a xpath location and store the result. Check that the units are compatible with the destination. The value is expected to be a float.

The xpath location might or might not exist. If it does not exist, nothing is done

Parameters:
  • location – xpath location to fetch

  • node – node to read the data from

  • variable – name of the data member to store it in [string]

  • storage – data object that has the ‘variable’ data member

  • optional – if True, no exception will be raised if unit conversion can’t be done

Raises:

ValueError – raised when the units are not recognized

_to_xml_doc(datainfo: Data1D | Data2D)

Create an XML document to contain the content of a Data1D

Parameters:

datainfo – Data1D object

_unit_conversion(node: ~lxml.etree.ElementTree, tagname: str, node_value: float) -> (<class 'float'>, <class 'str'>)

A unit converter method used to convert the data included in the file to the default units listed in data_info

Parameters:
  • node – XML node

  • tagname – name of the node

  • node_value – The value of the current dom node

_write_collimation(datainfo: Data1D | Data2D, instr: ElementTree)

Writes the collimation information to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • instr – lxml node ElementTree object to be appended to

_write_data(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes 1D I and Q data to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

_write_data_2d(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes 2D data to the XML file

Parameters:
  • datainfo – The Data2D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

_write_detectors(datainfo: Data1D | Data2D, instr: ElementTree)

Writes the detector information to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • inst – lxml instrument node to be appended to

_write_instrument(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes the instrumental information to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

_write_notes(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes the notes to the XML file and creates an empty note if none exist

Parameters:
  • datainfo – The Data1D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

_write_process_notes(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes the process notes to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

_write_run_names(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes the run names to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

_write_sample_info(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes the sample information to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

_write_source(datainfo: Data1D | Data2D, instr: ElementTree)

Writes the source information to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • instr – instrument node to be appended to

_write_trans_spectrum(datainfo: Data1D | Data2D, entry_node: ElementTree)

Writes the transmission spectrum data to the XML file

Parameters:
  • datainfo – The Data1D object the information is coming from

  • entry_node – lxml node ElementTree object to be appended to

allow_all = True
base_ns = '{cansas1d/1.1}'
cansas_defaults = None
cansas_version = '1.1'
current_data1d = None
data = None
errors = {}
ext = ['.xml', '.svs']
frm = ''
get_file_contents()

Reader specific class to access the contents of the file All reader classes that inherit from FileReader must implement

invalid = True
is_cansas(ext: str | None = 'xml')

Checks to see if the XML file is a CanSAS file

Parameters:

ext – The file extension of the data file

Raises:

FileContentsException – Raised if XML file isn’t valid CanSAS

load_file_and_schema(xml_file: str, schema_path: str | None = '') bool
logging = None
names = None
ns_list = None
process_1d_data_object(tagname: str, data_point: float, unit: str, attr: dict)

Assign a 1D data variable to the appropriate plottable value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param unit: Unit of the data_point :param attr: Extra attributes :return: None

process_2d_data_object(tagname: str, data_point: float, unit: str)

Assign a 2D data variable to the appropriate plottable value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param unit: Unit of the data_point :return: None

process_collimation_data_object(tagname: str, data_point: float, unit: str)

Assign a collimation variable to the appropriate Collimation value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param unit: Unit of the data_point :return: None

process_detector_data_object(tagname: str, data_point: float, unit: str)

Assign a detector variable to the appropriate Detector value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param unit: Unit of the data_point :return: None

process_meta_data(tagname: str, data_point: float)

Any unrecognized tag should still be loaded - add to meta_data :param tagname: Name of the XML tag :param data_point: Data to be assigned :return: None

process_process_data_object(tagname: str, data_point: float, attr: dict)

Assign a process variable to the appropriate Process value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param attr: XML attributes :return: None

process_sample_data_object(tagname: str, data_point: float, unit: str)

Assign a sample data variable to the appropriate Sample value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param unit: Unit of the data_point :return: None

process_source_data_object(tagname: str, data_point: float, unit: str)

Assign a source variable to the appropriate Source value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param unit: Unit of the data_point :return: None

process_trans_spec_data_object(tagname: str, data_point: float, unit: str)

Assign a transmission spectrum data variable to the appropriate datainfo value :param tagname: Name of the XML tag :param data_point: Data to be assigned :param unit: Unit of the data_point :return: None

reset_state()

Resets the class state to a base case when loading a new data file so previous data files do not appear a second time

set_default_schema()
type = ['XML files (*.xml)|*.xml', 'SasView Save Files (*.svs)|*.svs']
type_name = 'canSAS'
write(filename: str, datainfo: Data1D | Data2D)

Write the content of a Data1D as a CanSAS XML file

Parameters:
  • filename – name of the file to write

  • datainfo – Data1D object

write_node(parent: ElementTree, name: str, value: float | str, attr: dict | None = None) bool
Parameters:
  • doc – document DOM

  • parent – parent node

  • name – tag of the element

  • value – value of the child text node

  • attr – attribute dictionary

Returns:

True if something was appended, otherwise False

sasdata.dataloader.readers.cansas_reader.get_content(location, node)

Get the first instance of the content of a xpath location.

Parameters:
  • location – xpath location

  • node – node to start at

Returns:

Element, or None

sasdata.dataloader.readers.cansas_reader.getattrchain(obj, chain, default=None)

Like getattr, but the attr may contain multiple parts separated by ‘.’

sasdata.dataloader.readers.cansas_reader.setattrchain(obj, chain, value)

Like setattr, but the attr may contain multiple parts separated by ‘.’

sasdata.dataloader.readers.cansas_reader.write_node(doc, parent, name, value, attr=None)
Parameters:
  • doc – document DOM

  • parent – parent node

  • name – tag of the element

  • value – value of the child text node

  • attr – attribute dictionary

Returns:

True if something was appended, otherwise False