def check(self, mode, triggers=None): """Check if timing is configured.""" if triggers is None: triggers = list() pvs_2_init = self.get_pvname_2_defval_dict(mode, triggers) for prop, defval in pvs_2_init.items(): try: prop_sts = _get_pair_sprb(prop)[1] except TypeError: continue pvobj = Timing._pvs[prop_sts] if not pvobj.wait_for_connection(TIMEOUT_CONNECTION): return False if prop_sts.propty_name == 'Src': defval = Timing.cycle_idx[prop_sts.device_name] if prop_sts.propty_name.endswith(('Duration', 'Delay')): tol = 0.008 * 15 if not _isclose(pvobj.value, defval, abs_tol=tol): # print(pvobj.pvname, pvobj.value, defval) return False elif isinstance(defval, (_np.ndarray, list, tuple)): if _np.any(pvobj.value[0:len(defval)] != defval): # print(pvobj.pvname, pvobj.value, defval) return False elif pvobj.value != defval: # print(pvobj.pvname, pvobj.value, defval) return False return True
def is_close(a, b, msg="", **isclose_kwargs): """Assert that math.isclose returns True based on the given values.""" assert _isclose(a, b, **isclose_kwargs), _msg_concat( msg, "Expected '{}' to be close to '{}'{}".format( a, b, _format_if(": kwargs: {}", isclose_kwargs)), )
def check(self, mode, triggers=None): """Check if timing is configured.""" if triggers is None: triggers = list() pvs_2_init = self.get_pvname_2_defval_dict(mode, triggers) for prop, defval in pvs_2_init.items(): prop_sts = prop.replace('SP', 'RB').replace('Sel', 'Sts') pvobj = Timing._pvs[prop_sts] if not pvobj.wait_for_connection(TIMEOUT_CONNECTION): return False if prop_sts.propty_name.endswith(('Duration', 'Delay')): tol = 0.008 * 15 if not _isclose(pvobj.value, defval, abs_tol=tol): # print(pvobj.pvname, pvobj.value, defval) return False elif isinstance(defval, (_np.ndarray, list, tuple)): if _np.any(pvobj.value[0:len(defval)] != defval): # print(pvobj.pvname, pvobj.value, defval) return False elif pvobj.value != defval: # print(pvobj.pvname, pvobj.value, defval) return False return True
def almost_equal(actual, expected, places=2, msg=''): # Set relative tolerance to 0 because we don't want that messing up the places check. relative_tolerance = 0 absolute_tolerance = 10.0**(-places) assert _isclose(expected, actual, rel_tol=relative_tolerance, abs_tol=absolute_tolerance), _msg_concat( msg, "Expected '{}' to be almost equal to '{}'".format( actual, expected))
def isclose(a: float, b: float) -> bool: """Determine whether two single-precision floating-point numbers are close in value. For the values to be considered close, the relative difference between them must be smaller than FLT_EPSILON. -inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is not close to anything, even itself. inf and -inf are only close to themselves. """ return _isclose(a, b, rel_tol=FLT_EPSILON)
def smart_dir_sync( src_dir, dst_dir, mtime_tolerance=2.0, pathname_xform=lambda sub_path, file: os.path.join(sub_path, file) ): """Recursively copy a directory, skipping any files that don't appear to require updating. :param str src_dir: The directory to copy from. :param str dst_dir: The directory to copy to. :param float mtime_tolerance: How many seconds to tolerate in differences between the mtime of a file in ``src_dir`` and an existing file of the same name in ``dst_dir``. This is especially useful for FAT filesystems, which have a 2-second resolution. :param function filename_xform: An optional function that translates a pathname from ``src_dir`` before being written to ``dst_dir``. This can be used for stripping out characters that may not be supported on the filesystem of ``dst_dir``. The function should take 2 strings as arguments, representing the current subdirectory(s), if any, under the ``dst_dir``; and the filename. The function should return a single string representing both strings after undergoing any desired transformations and concatenated together. """ for dir_path, dir_names, filenames in os.walk(src_dir): print('src_dir=' + src_dir + ', dir_path=' + dir_path) sub_path = os.path.relpath(dir_path, src_dir) for d in dir_names: src_pathname = os.path.join(src_dir, sub_path, d) dst_pathname = os.path.join(dst_dir, pathname_xform(sub_path, d)) if not os.path.exists(dst_pathname): os.mkdir(dst_pathname) for f in filenames: print('sub_path = ' + sub_path) src_pathname = os.path.join(src_dir, sub_path, f) dst_pathname = os.path.join(dst_dir, pathname_xform(sub_path, f)) print('< ' + src_pathname) print('> ' + dst_pathname) if ( os.path.exists(dst_pathname) and _isclose( os.path.getmtime(src_pathname), os.path.getmtime(dst_pathname), abs_tol=mtime_tolerance ) and os.path.getsize(dst_pathname) == os.path.getsize(src_pathname) ): continue shutil.copy(src_pathname, dst_pathname) set_mtime(dst_pathname, os.path.getmtime(src_pathname))
def almost_equal(actual, expected, places=2, msg=""): """Check that actual and expected are within `places` equal.""" # Set relative tolerance to 0 because we don't want that messing up the places check relative_tolerance = 0 absolute_tolerance = 10.0**(-places) if _isclose(expected, actual, rel_tol=relative_tolerance, abs_tol=absolute_tolerance): return "" return _msg_concat( msg, "Expected '{}' to be almost equal to '{}'".format(actual, expected))
def is_close(a, b, msg="", **isclose_kwargs): """Check that math.isclose returns True based on the given values.""" if _isclose(a, b, **isclose_kwargs): return "" return _msg_concat( msg, "Expected '{}' to be close to '{}', " "but they differ by '{}', a difference of '{}%'.{}".format( a, b, abs(a - b), percent_diff(a, b), _format_if(": kwargs: {}", isclose_kwargs), ), )
def is_close(a, b, msg='', **isclose_kwargs): '''Assert that math.isclose returns True based on the given values.''' assert _isclose(a, b, **isclose_kwargs), _msg_concat( msg, "Expected '{}' to be close to '{}'{}".format( a, b, _format_if(': kwargs: {}', isclose_kwargs)))
def igual(a,b): '''Função math.isclose com rel_tol=0.05''' return _isclose(a,b,rel_tol=0.05)
def _update_value(self, value): """ Update the value and depending on it the status and severity. """ value = self._constrain_value(value) status, severity = self._calculate_status_severity(value) old_value = self._attributes.get('value') if is_sequence(value) != is_sequence(old_value): # If old and new_value differ in wether they are sequences or not # we can't compare them. value_changed = True elif is_sequence(value) and len(value) != len(old_value): # If the lenth of both sequences are different we can't # compare them either. value_changed = True else: if self._type in (ca.Type.FLOAT, ca.Type.DOUBLE): # Floating point types can't be compare with the equal # operator because of rounding errors isclose = lambda a, b: _isclose(a, b, rel_tol=self._relative_tolerance, abs_tol=self._absolute_tolerance) if is_sequence(value): # sequences are compared element-wise if numpy and (isinstance(value, numpy.ndarray) or isinstance(old_value, numpy.ndarray)): value_changed = not numpy.allclose(value, old_value, rtol=self._relative_tolerance, atol=self._absolute_tolerance) else: value_changed = not all(map(lambda x: isclose(x[0], x[1]), zip(value, old_value))) else: value_changed = not isclose(value, old_value) else: # Integer types are compared with the equal operator if numpy and (isinstance(value, numpy.ndarray) or isinstance(old_value, numpy.ndarray)): value_changed = not numpy.all(numpy.equal(value, old_value)) else: # For simple sequences this compares element-wise value_changed = value != old_value if value_changed: self._attributes['value'] = value # Deadbands are only defined for number types check_deadbands = self._type not in (ca.Type.STRING, ca.Type.ENUM) if is_sequence(value) != is_sequence(old_value): # If the values differ in wether they are sequences or not, # deadbands can't be used. check_deadbands = False elif is_sequence(value) and len(value) != len(old_value): # If the lenth of both sequences are different we can't # use the deadbands either. check_deadbands = False if check_deadbands: if is_sequence(value): # Look at the maximum difference between the old values # and the new ones. diff = max(map(lambda x: abs(x[0] - x[1]), zip(value, old_value))) else: diff = abs(value - old_value) if diff >= self._value_deadband: self._outstanding_events |= ca.Events.VALUE if diff >= self._archive_deadband: self._outstanding_events |= ca.Events.ARCHIVE else: self._outstanding_events |= ca.Events.VALUE | ca.Events.ARCHIVE self._update_status_severity(status, severity)