<commit_before>"""
    CanSAS data reader - new recursive cansasVersion.
"""
############################################################################
#This software was developed by the University of Tennessee as part of the
#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
#project funded by the US National Science Foundation. 
#If you use DANSE applications to do scientific research that leads to 
#publication, we ask that you acknowledge the use of the software with the 
#following sentence:
#This work benefited from DANSE software developed under NSF award DMR-0520547. 
#copyright 2008,2009 University of Tennessee
#############################################################################

import logging
import numpy
import os
import sys
from sans.dataloader.data_info import Data1D
from sans.dataloader.data_info import Collimation
from sans.dataloader.data_info import Detector
from sans.dataloader.data_info import Process
from sans.dataloader.data_info import Aperture
import xml_reader
import xml.dom.minidom
from cansas_constants import cansasConstants

_ZERO = 1e-16
HAS_CONVERTER = True
try:
    from sans.data_util.nxsunit import Converter
except:
    HAS_CONVERTER = False
    
CANSAS_FORMAT = cansasConstants.CANSAS_FORMAT
CANSAS_NS = cansasConstants.CANSAS_NS
ALLOW_ALL = True


def write_node(doc, parent, name, value, attr={}):
    """
    :param doc: document DOM
    :param parent: parent node
    :param name: tag of the element
    :param value: value of the child text node
    :param attr: attribute dictionary
    
    :return: True if something was appended, otherwise False
    """
    if value is not None:
        node = doc.createElement(name)
        node.appendChild(doc.createTextNode(str(value)))
        for item in attr:
            node.setAttribute(item, attr[item])
        parent.appendChild(node)
        return True
    return False
                

def get_content(location, node):
    """
    Get the first instance of the content of a xpath location.
    
    :param location: xpath location
    :param node: node to start at
    
    :return: Element, or None
    """
    nodes = node.xpath(location, namespaces={'ns': CANSAS_NS})
    
    if len(nodes) > 0:
        return nodes[0]
    else:
        return None


def get_float(location, node):
    """
    Get the content of a node as a float
    
    :param location: xpath location
    :param node: node to start at
    """
    nodes = node.xpath(location, namespaces={'ns': CANSAS_NS})
    
    value = None
    attr = {}
    if len(nodes) > 0:
        try:
            value = float(nodes[0].text)
        except:
            # Could not pass, skip and return None
            msg = "cansas_reader.get_float: could not "
            msg += " convert '%s' to float" % nodes[0].text
            logging.error(msg)
        if nodes[0].get('unit') is not None:
            attr['unit'] = nodes[0].get('unit')
    return value, attr



class CANSASError(Exception):
    """Base class all CANSAS reader exceptions are derived"""
    pass

class NotCANSASFileError(CANSASError):
    def __init__(self):
        self.value = "This is not a proper CanSAS file."
    def __str__(self):
        return repr(self.value)

class Reader():
    """
    Class to load cansas 1D XML files
    
    :Dependencies:
        The CanSAS reader requires PyXML 0.8.4 or later.
    """
    ##CanSAS version - defaults to version 1.0
    cansasVersion = "1.0"
    ##Data reader
    reader = xml_reader.XMLreader()
    errors = []
    
    type_name = "canSAS"
    
    ## Wildcards
    type = ["XML files (*.xml)|*.xml"]
    ## List of allowed extensions
    ext = ['.xml', '.XML']
    
    ## Flag to bypass extension check
    allow_all = True
    
    def __init__(self):
        ## List of errors
        self.errors = []
        
    def isCansas(self):
        """
        Checks to see if the xml file is a CanSAS file
        """
        if self.reader.validateXML():
            xmlns = self.reader.xmlroot.keys()
            if (CANSAS_NS.get(self.cansasVersion).get("ns") == self.reader.xmlroot.get(xmlns[1]).rsplit(" ")[0]):
                return True
        return False
    
    def read(self, xml):
        """
        Validate and read in an xml file in the canSAS format.
        
        :param xml: A canSAS file path in proper XML format
        """
        # X - Q value; Y - Intensity (Abs)
        x = numpy.empty(0)
        y = numpy.empty(0)
        dx = numpy.empty(0)
        dy = numpy.empty(0)
        dxl = numpy.empty(0)
        dxw = numpy.empty(0)
        
        # output - Final list of Data1D objects
        output = []
        # ns - Namespace hierarchy for current xml object
        ns = []
        
        # Check that the file exists
        if os.path.isfile(xml):
            basename = os.path.basename(xml)
            _, extension = os.path.splitext(basename)
            # If the fiel type is not allowed, return nothing
            if extension in self.ext or self.allow_all:
                base_name = xml_reader.__file__
                base = base_name.split("\\sans\\")[0]
                
                # Load in the xml file and get the cansas version from the header
                self.reader.setXMLFile(xml)
                root = self.reader.xmlroot
                if root is None:
                    root = {}
                self.cansasVersion = root.get("version", "1.0")
                
                # Generic values for the cansas file based on the version
                cansas_defaults = CANSAS_NS.get(self.cansasVersion, "1.0")
                schema_path = "{0}\\sans\\dataloader\\readers\\schema\\{1}".format(base, cansas_defaults.get("schema")).replace("\\", "/")
                
                # Link a schema to the XML file.
                self.reader.setSchema(schema_path)
            
                # Try to load the file, but raise an error if unable to.
                # Check the file matches the XML schema
                try:
                    if self.isCansas():
                        # Get each SASentry from the XML file and add it to a list.
                        entry_list = root.xpath('/ns:SASroot/ns:SASentry',
                                                     namespaces={'ns': cansas_defaults.get("ns")})
                        ns.append("SASentry")
                        
                        # If there are multiple files, modify the name for each is unique
                        multipleFiles = len(entry_list) - 1
                        n = 0
                        name = basename
                        # Parse each SASentry item
                        for entry in entry_list:
                            
                            # Define a new Data1D object with zeroes for x and y
                            data1D = Data1D(x,y,dx,dy)<commit_msg>Fixing the file path issue in the Ubuntu build.<commit_after>"""
    CanSAS data reader - new recursive cansasVersion.
"""
############################################################################
#This software was developed by the University of Tennessee as part of the
#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
#project funded by the US National Science Foundation. 
#If you use DANSE applications to do scientific research that leads to 
#publication, we ask that you acknowledge the use of the software with the 
#following sentence:
#This work benefited from DANSE software developed under NSF award DMR-0520547. 
#copyright 2008,2009 University of Tennessee
#############################################################################

import logging
import numpy
import os
import sys
from sans.dataloader.data_info import Data1D
from sans.dataloader.data_info import Collimation
from sans.dataloader.data_info import Detector
from sans.dataloader.data_info import Process
from sans.dataloader.data_info import Aperture
import xml_reader
import xml.dom.minidom
from cansas_constants import cansasConstants

_ZERO = 1e-16
HAS_CONVERTER = True
try:
    from sans.data_util.nxsunit import Converter
except:
    HAS_CONVERTER = False
    
CANSAS_FORMAT = cansasConstants.CANSAS_FORMAT
CANSAS_NS = cansasConstants.CANSAS_NS
ALLOW_ALL = True


def write_node(doc, parent, name, value, attr={}):
    """
    :param doc: document DOM
    :param parent: parent node
    :param name: tag of the element
    :param value: value of the child text node
    :param attr: attribute dictionary
    
    :return: True if something was appended, otherwise False
    """
    if value is not None:
        node = doc.createElement(name)
        node.appendChild(doc.createTextNode(str(value)))
        for item in attr:
            node.setAttribute(item, attr[item])
        parent.appendChild(node)
        return True
    return False
                

def get_content(location, node):
    """
    Get the first instance of the content of a xpath location.
    
    :param location: xpath location
    :param node: node to start at
    
    :return: Element, or None
    """
    nodes = node.xpath(location, namespaces={'ns': CANSAS_NS})
    
    if len(nodes) > 0:
        return nodes[0]
    else:
        return None


def get_float(location, node):
    """
    Get the content of a node as a float
    
    :param location: xpath location
    :param node: node to start at
    """
    nodes = node.xpath(location, namespaces={'ns': CANSAS_NS})
    
    value = None
    attr = {}
    if len(nodes) > 0:
        try:
            value = float(nodes[0].text)
        except:
            # Could not pass, skip and return None
            msg = "cansas_reader.get_float: could not "
            msg += " convert '%s' to float" % nodes[0].text
            logging.error(msg)
        if nodes[0].get('unit') is not None:
            attr['unit'] = nodes[0].get('unit')
    return value, attr



class CANSASError(Exception):
    """Base class all CANSAS reader exceptions are derived"""
    pass

class NotCANSASFileError(CANSASError):
    def __init__(self):
        self.value = "This is not a proper CanSAS file."
    def __str__(self):
        return repr(self.value)

class Reader():
    """
    Class to load cansas 1D XML files
    
    :Dependencies:
        The CanSAS reader requires PyXML 0.8.4 or later.
    """
    ##CanSAS version - defaults to version 1.0
    cansasVersion = "1.0"
    ##Data reader
    reader = xml_reader.XMLreader()
    errors = []
    
    type_name = "canSAS"
    
    ## Wildcards
    type = ["XML files (*.xml)|*.xml"]
    ## List of allowed extensions
    ext = ['.xml', '.XML']
    
    ## Flag to bypass extension check
    allow_all = True
    
    def __init__(self):
        ## List of errors
        self.errors = []
        
    def isCansas(self):
        """
        Checks to see if the xml file is a CanSAS file
        """
        if self.reader.validateXML():
            xmlns = self.reader.xmlroot.keys()
            if (CANSAS_NS.get(self.cansasVersion).get("ns") == self.reader.xmlroot.get(xmlns[1]).rsplit(" ")[0]):
                return True
        return False
    
    def read(self, xml):
        """
        Validate and read in an xml file in the canSAS format.
        
        :param xml: A canSAS file path in proper XML format
        """
        # X - Q value; Y - Intensity (Abs)
        x = numpy.empty(0)
        y = numpy.empty(0)
        dx = numpy.empty(0)
        dy = numpy.empty(0)
        dxl = numpy.empty(0)
        dxw = numpy.empty(0)
        
        # output - Final list of Data1D objects
        output = []
        # ns - Namespace hierarchy for current xml object
        ns = []
        
        # Check that the file exists
        if os.path.isfile(xml):
            basename = os.path.basename(xml)
            _, extension = os.path.splitext(basename)
            # If the fiel type is not allowed, return nothing
            if extension in self.ext or self.allow_all:
                base_name = xml_reader.__file__
                base_name = base_name.replace("\\","/")
                base = base_name.split("/sans/")[0]
                
                # Load in the xml file and get the cansas version from the header
                self.reader.setXMLFile(xml)
                root = self.reader.xmlroot
                if root is None:
                    root = {}
                self.cansasVersion = root.get("version", "1.0")
                
                # Generic values for the cansas file based on the version
                cansas_defaults = CANSAS_NS.get(self.cansasVersion, "1.0")
                schema_path = "{0}/sans/dataloader/readers/schema/{1}".format(base, cansas_defaults.get("schema")).replace("\\", "/")
                
                # Link a schema to the XML file.
                self.reader.setSchema(schema_path)
            
                # Try to load the file, but raise an error if unable to.
                # Check the file matches the XML schema
                try:
                    if self.isCansas():
                        # Get each SASentry from the XML file and add it to a list.
                        entry_list = root.xpath('/ns:SASroot/ns:SASentry',
                                                     namespaces={'ns': cansas_defaults.get("ns")})
                        ns.append("SASentry")
                        
                        # If there are multiple files, modify the name for each is unique
                        multipleFiles = len(entry_list) - 1
                        n = 0
                        name = basename
                        # Parse each SASentry item
                        for entry in entry_list:
                            
                            # Define a new Data1D object with zeroes for x and y
                            data1D = Data1D(x,y,dx,dy)
{'fraction_non_alphanumeric': 0.10724825451666307, 'fraction_numerical': 0.00791765637371338, 'mean_word_length': 1.8873649210307564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 42}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2130690', 'n_tokens_mistral': 3918, 'n_tokens_neox': 3780, 'n_words': 1251}