Changeset ef51b63 in sasview


Ignore:
Timestamp:
Sep 15, 2016 8:33:57 AM (8 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
4141155
Parents:
132db16
git-author:
Jeff Krzywon <krzywon@…> (08/05/16 11:17:27)
git-committer:
Piotr Rozyczko <rozyczko@…> (09/15/16 08:33:57)
Message:

Modified Cansas XML reader to attempt to load data that doesn't fully meet the cansas format.

Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/cansas_reader.py

    raf09f48 ref51b63  
    3434from xml.dom.minidom import parseString 
    3535 
     36## TODO: Refactor to load multiple <SASData> as separate Data1D objects 
     37## TODO: Refactor to allow invalid XML, but give a useful warning when loaded 
     38 
    3639_ZERO = 1e-16 
    3740PREPROCESS = "xmlpreprocess" 
     
    133136        return False 
    134137 
    135     def load_file_and_schema(self, xml_file): 
     138    def load_file_and_schema(self, xml_file, schema_path=""): 
    136139        """ 
    137140        Loads the file and associates a schema, if a known schema exists 
     
    149152        # Generic values for the cansas file based on the version 
    150153        cansas_defaults = CANSAS_NS.get(self.cansas_version, "1.0") 
    151         schema_path = "{0}/sas/sascalc/dataloader/readers/schema/{1}".format\ 
     154        if schema_path == "": 
     155            schema_path = "{0}/sas/sascalc/dataloader/readers/schema/{1}".format\ 
    152156                (base, cansas_defaults.get("schema")).replace("\\", "/") 
    153157 
     
    156160        return cansas_defaults 
    157161 
    158     def read(self, xml_file): 
     162    ## TODO: Test loading invalid CanSAS XML files and see if this works 
     163    ## TODO: Once works, try adding a warning that the data is invalid 
     164    def read(self, xml_file, schema_path=""): 
    159165        """ 
    160166        Validate and read in an xml_file file in the canSAS format. 
     
    174180            if extension in self.ext or self.allow_all: 
    175181                # Get the file location of 
    176                 cansas_defaults = self.load_file_and_schema(xml_file) 
     182                cansas_defaults = self.load_file_and_schema(xml_file, schema_path) 
    177183 
    178184                # Try to load the file, but raise an error if unable to. 
     
    225231                except: 
    226232                    # If the file does not match the schema, raise this error 
    227                     raise RuntimeError, "%s cannot be read" % xml_file 
     233                    schema_path = "{0}/sas/sascalc/dataloader/readers/schema/cansas1d_invalid.xsd" 
     234                    invalid_xml = self.find_invalid_xml() 
     235                    invalid_xml = "\n\nThe loaded xml file does not fully meet the CanSAS v1.x specification. SasView " + \ 
     236                                  "loaded as much of the data as possible.\n\n" + invalid_xml 
     237                    self.errors.add(invalid_xml) 
     238                    self.set_schema(schema_path) 
     239                    if self.is_cansas(): 
     240                        output = self.read(xml_file, schema_path) 
     241                    else: 
     242                        raise RuntimeError, "%s cannot be read" % xml_file 
    228243                return output 
    229244        # Return a list of parsed entries that dataloader can manage 
  • test/sasdataloader/test/utest_cansas.py

    r132db16 ref51b63  
    3737        self.cansas1d_slit = "cansas1d_slit.xml" 
    3838        self.cansas1d_units = "cansas1d_units.xml" 
     39        self.cansas1d_notitle = "cansas1d_notitle.xml" 
    3940        self.isis_1_0 = "ISIS_1_0.xml" 
    4041        self.isis_1_1 = "ISIS_1_1.xml" 
     
    171172 
    172173 
     174    def test_invalid_cansas(self): 
     175        list = self.loader.load(self.cansas1d_notitle) 
     176        data = list[0] 
     177        self.assertTrue(data.x.size == 2) 
     178        self.assertTrue(len(data.meta_data) == 3) 
     179        self.assertTrue(len(data.errors) == 1) 
     180        self.assertTrue(data.detector[0].distance_unit == "mm") 
     181        self.assertTrue(data.detector[0].name == "fictional hybrid") 
     182        self.assertTrue(data.detector[0].distance == 4150) 
     183 
     184 
    173185    def test_old_cansas_files(self): 
    174186        reader1 = XMLreader(self.cansas1d, self.schema_1_0) 
Note: See TracChangeset for help on using the changeset viewer.