def validate(self, text, pos): parts = text.split() if len(parts) == 0: if self._attribute.is_required(): return (QValidator.Intermediate, text, pos) else: return (QValidator.Acceptable, text, pos) elif len(parts) == 1: try: float(parts[0]) except ValueError: return (QValidator.Intermediate, text, pos) else: return (QValidator.Acceptable, text, pos) elif len(parts) == 2: try: float(parts[0]) validate_unit(parts[1]) except ValueError: return (QValidator.Intermediate, text, pos) else: return (QValidator.Acceptable, text, pos) else: return (QValidator.Invalid, text, pos)
def validate(self, text, pos): if not text: if self._attribute.is_required(): return (QValidator.Intermediate, text, pos) else: return (QValidator.Acceptable, text, pos) try: validate_unit(text) except ValueError: return (QValidator.Intermediate, text, pos) if self._valid_units is not None and text not in self._valid_units: return (QValidator.Intermediate, text, pos) return (QValidator.Acceptable, text, pos)
def _prepare_value(self, value): value = TextAttribute._prepare_value(self, value) value = value or self.default_unit try: value = validate_unit(value) except: pass return value
def __new__(cls, shape, dtype=np.float32, buffer=None, offset=0, strides=None, order=None, unit=None): validate_dtype(dtype) obj = super().__new__(cls, shape, dtype, buffer, offset, strides, order) if unit is not None: unit = validate_unit(unit) obj._unit = unit return obj
def testvalidate_unit(self): for unit in _UNITS: if unit in [u'\u00c5', 's', 'g']: validate_unit(unit) else: for prefix in _PREFIXES: validate_unit(prefix + unit) self.assertRaises(ValueError, validate_unit, 'Mg') self.assertRaises(ValueError, validate_unit, 'ks') self.assertRaises(ValueError, validate_unit, u'k\u00c5') self.assertRaises(ValueError, validate_unit, 'Km') self.assertRaises(ValueError, validate_unit, 'u') self.assertEqual('um', validate_unit(u'\u00b5m')) self.assertEqual('um', validate_unit('um')) self.assertEqual('um3', validate_unit(u'\u00b5m+3')) self.assertEqual('um-3', validate_unit(u'\u00b5m-3')) self.assertEqual('degrees', validate_unit(u'\u00b0'))
def _extract_detector(self, keywords): if 'SIGNALTYPE' not in keywords: return {} signal_type = keywords.get('SIGNALTYPE') # Enums is identical kwargs = {} kwargs['signal_type'] = signal_type kwargs['channel_count'] = keywords.getint('NPOINTS') quantity = keywords.get('XLABEL', 'Energy') unit = keywords.get('XUNITS') gain = keywords.getfloat('XPERCHAN') offset = keywords.getfloat('OFFSET') try: unit = validate_unit(unit) except ValueError as ex: # Attempt quick fix for common unit if 'angstroms' in unit: unit = 'nm' gain /= 10.0 offset /= 10.0 elif 'eV' in unit: unit = 'eV' else: raise ex kwargs['calibration'] = CalibrationLinear(quantity, unit, gain, offset) kwargs['measurement_unit'] = keywords.get('yunits') kwargs['elevation'] = (keywords.getfloat('ELEVANGLE'), 'degrees') kwargs['azimuth'] = (keywords.getfloat('AZIMANGLE'), 'degrees') kwargs['solid_angle'] = (keywords.getfloat('SOLIDANGLE'), 'sr') kwargs['semi_angle'] = (keywords.getfloat('COLLANGLE'), 'mrad') kwargs['collection_mode'] = \ _ELSDET_TO_COLLECTION_MODE.get(keywords.get('ELSDET')) if signal_type in [SIGNAL_TYPE_EDS, SIGNAL_TYPE_WDS]: window = Window() if 'TDEADLYR' in keywords: window.append_layer('Dead layer', (keywords.getfloat('TDEADLYR') * 1e4, 'um')) if 'TACTLYR' in keywords: window.append_layer('Active Layer', (keywords.getfloat('TACTLYR') * 1e4, 'um')) if 'TBEWIND' in keywords: window.append_layer('Be window', (keywords.getfloat('TBEWIND') * 1e4, 'um')) if 'TAUWIND' in keywords: window.append_layer('Au window', (keywords.getfloat('TAUWIND') * 1e4, 'um')) if 'TALWIND' in keywords: window.append_layer('Al window', (keywords.getfloat('TALWIND') * 1e4, 'um')) if 'TPYWIND' in keywords: window.append_layer('Pyrolene window', (keywords.getfloat('TPYWIND') * 1e4, 'um')) if 'TBNWIND' in keywords: window.append_layer('Boron-Nitride window', (keywords.getfloat('TBNWIND') * 1e4, 'um')) if 'TDIWIND' in keywords: window.append_layer('Diamond window', (keywords.getfloat('TDIWIND') * 1e4, 'um')) if 'THCWIND' in keywords: window.append_layer('HydroCarbon window', (keywords.getfloat('TDIWIND') * 1e4, 'um')) if window.layers: kwargs['window'] = window if signal_type == SIGNAL_TYPE_EDS: kwargs['technology'] = \ _EDSDET_TO_XEDS_TECHNOLOGY.get(keywords.get('EDSDET')) c = DetectorSpectrometerXEDS(**kwargs) elif signal_type == SIGNAL_TYPE_CLS: c = DetectorSpectrometerCL(**kwargs) else: c = DetectorSpectrometer(**kwargs) return {signal_type: c}
def _validate_value(self, value): TextAttribute._validate_value(self, value) if value is not None: validate_unit(value)