def remove_range(self, the_rng): middle = [] t_index = None for i in range(len(self.list)): rng = self.list[i] if (the_rng.intersects(rng)): if (t_index == None): t_index = i if (the_rng.lower >= rng.lower): if (rng.lower <= (the_rng.lower - 1)): middle.append(Range(rng.lower, the_rng.lower - 1)) if (the_rng.upper <= rng.upper): if ((the_rng.upper + 1) <= rng.upper): middle.append(Range(the_rng.upper + 1, rng.upper)) elif (rng.upper < the_rng.lower): pass elif (the_rng.upper < rng.lower): pass else: raise "SHOULD NEVER GET HERE", (rng, new_rng) if (t_index != None): self.list[t_index:(t_index + len(middle))] = middle
def test_bool(self): r = Range(10) o = range(10) self.assertEqual(bool(r), bool(o)) r = Range(0) o = range(0) self.assertEqual(bool(r), bool(o))
def test_lstretch(self, Range, extend_by, expected_result): if extend_by < 0: with pytest.raises(ValueError): Range.l_stretch(extend_by) assert Range.low == expected_result else: Range.l_stretch(extend_by) assert Range.low == expected_result
def read_range_from_string(s): list = string.split(s, '-') n = len(list) if (n == 2): return (Range(string.atoi(list[0]), string.atoi(list[1]))) elif (n == 1): return (Range(string.atoi(list[0]), string.atoi(list[0]))) else: raise "ERROR"
def test_stretch(self, Range, extend_by, expected_result): if extend_by < 0: with pytest.raises(ValueError): Range.stretch(extend_by) assert (Range.low, Range.high) == (min(expected_result), max(expected_result)) else: Range.stretch(extend_by) assert (Range.low, Range.high) == (min(expected_result), max(expected_result))
def test_large_nums(self): import sys ln = sys.maxsize + 1 r = Range(ln, 3 * ln, 1) o = range(ln, 3 * ln, 1) self.assertEqual(r._len, 2 * ln) self.assertEqual(r[0], o[0]) self.assertEqual(r[-1], r[-1]) self.assertIn(2 * ln, r) self.assertEqual(r.count(2 * ln), 1) self.assertEqual(r.index(2 * ln), o.index(2 * ln))
def test_out_of_bounds(): headers = ["Age", "Salary"] t = Item(pd.Series(data=np.array([25, 27]), index=headers), headers, sensitive_attr=None) c = Cluster(headers) c.ranges = {"Age": Range(20, 24), "Salary": Range(27, 32)} assert not c.within_bounds(t)
def test_reversed(self): self.assertEqual([x for x in reversed(Range(1, 10, 3))], [x for x in reversed(range(1, 10, 3))]) self.assertEqual([x for x in reversed(Range(-5, 5, 3))], [x for x in reversed(range(-5, 5, 3))]) self.assertEqual([x for x in reversed(Range(5, -5, -3))], [x for x in reversed(range(5, -5, -3))]) self.assertEqual([x for x in reversed(Range(-1, -10, -3))], [x for x in reversed(range(-1, -10, -3))])
def calc_cumulative(self): self.cumulative_array = [] for i, interval in enumerate(self.interval_array): if i == 0: new_interval = Range(interval.pt1, interval.pt2, interval.value * interval.get_value_dif()) else: new_interval = Range( interval.pt1, interval.pt2, (interval.value * interval.get_value_dif()) + self.cumulative_array[-1].value) ##new_interval.pprint() self.cumulative_array.append(new_interval)
def add_ranges(self, ranges): num_ranges = len(ranges) if ranges[0].type == "DESC": new_range = Range(ranges[0].pt2, ranges[0].pt1, 0) else: new_range = Range(ranges[0].pt1, ranges[0].pt2, 0) self.interval_array = [new_range] for i, r in enumerate(ranges[1:]): ##val = math.log1p(i) val = (i + 1) / num_ranges self.add_range(r, val)
def insert_subrange(self, offset, length, data=None): """ Unlike add_subrange() which appends a new subrange on top of current range, this method inserts a subrange between this byte range and existing subranges that are covered by this new subrange. It can be used for grouping for example. """ # Make sure that there is no subrange that spans the boundary (start and / or stop). subsubranges = list() tmp_range = Range(offset, offset + length) for sr in self.subranges: if sr < tmp_range: continue if sr > tmp_range: break # we have gone past anything that may overlap. no need to continue if sr in tmp_range: subsubranges.append(sr) else: raise ValueError('subrange %d-%d spans boundary' % (sr.start, sr.stop)) # Remove all subranges that belong to the new subrange for ssr in subsubranges: self.subranges.remove(ssr) # Add the new subrange new_sr = self.add_subrange(offset, length, data) # Put all the removed subranges back for ssr in subsubranges: new_ssr = new_sr.add_subrange(ssr.start - offset, len(ssr), data=ssr.data) new_ssr.subranges = ssr.subranges return new_sr
def get_regular_range_list(start, width, count): list = [] for i in xrange(count): abs_start = start + (i * width) r = Range(abs_start, abs_start + width - 1) list.append(r) return list
def set_range(self, key: str, value: Union[None, float, Iterable[float], Range]) -> bool: if key.lower().startswith('val'): self.val = value elif key.lower().startswith('ref'): self.ref = value else: if isinstance(value, (collections.Sequence, )): lo, up, distr = None, None, None if len(value) > 0: lo = value[0] if len(value) > 1: up = value[1] if len(value) > 2: distr = value[2] self.ranges[key] = Range(lo, up, distr) elif isinstance(value, Range): try: self.ranges[key] = value except: print('par 344', key, value, type(value)) print('par 345 self.ranges', self.ranges.keys()) print('par 346 self.ranges', self.ranges.values()) else: assert 0 return True
def check_pynagios_range(option, opt, value): """ This parses and returns the Nagios range value. """ try: return Range(value) except RangeValueError, e: raise OptionValueError("options %s: %s" % (opt, e.message))
def test_getitem(self): r = Range(0) self.assertRaises(IndexError, getitem, r, -1) self.assertRaises(IndexError, getitem, r, 0) self.assertRaises(IndexError, getitem, r, 1) r = Range(1) o = range(1) self.assertRaises(IndexError, getitem, r, -2) self.assertEqual(r[-1], o[-1]) self.assertEqual(r[0], o[0]) self.assertRaises(IndexError, getitem, r, 1) r = Range(1, 10) o = range(1, 10) self.assertEqual(r[5], o[5]) r = Range(-5, 5) o = range(-5, 5) self.assertEqual(r[2], o[2]) self.assertEqual(r[7], o[7]) r = Range(5, -5, -1) o = range(5, -5, -1) self.assertEqual(r[2], o[2]) self.assertEqual(r[7], o[7]) r = Range(-1, -10, -1) o = range(-1, -10, -1) self.assertEqual(r[5], o[5]) r = Range(10) self.assertRaises(TypeError, getitem, r, '1')
def add_key_pt(self, pt): ##right at start add new interval at start if pt.value < self.interval_array[0].pt1.value: new_range = Range(pt, self.interval_array[0].pt1, 0) self.interval_array.insert(0, new_range) ##right at end add new interval at end elif pt.value > self.interval_array[-1].pt2.value: new_range = Range(self.interval_array[-1].pt2, pt, 0) self.interval_array.append(new_range) ##insert somewhere in the middle else: spot = self.get_insertion_spot(pt) if spot == -1: pass else: (range1, range2) = Range.split(self.interval_array[spot], pt) self.interval_array[spot] = range1 self.interval_array.insert(spot + 1, range2)
def create_ranges(self, candles): ranges = [] i = 0 while i < len(candles) - 1: pt1 = Point("", candles[i].date, candles[i].close) pt2 = Point("", candles[i + 1].date, candles[i + 1].close) new_r = Range(pt1, pt2, 0) ranges.append(new_r) i += 1 return ranges
def test_range_update(): r = Range() r.update(9) assert r.upper == 9 r.update(-1) assert r.lower == -1 r.update(5) assert r.lower == -1 assert r.upper == 9
def check_arg_and_send(self, arg, func): t = type(arg) if (t == types.IntType): func(Range(arg, arg)) elif (t == RangeType): func(arg) elif (t == types.ListType): for item in arg: self.check_arg_and_send(item, func) else: raise "UNSUPORTED TYPE"
def __init__(self, headers: List[str]): """Initialises the cluster. """ self.contents: List[Item] = [] self.ranges: Dict[str, Range] = {} for header in headers: self.ranges[header] = Range() self.diversity: Set[Any] = set() self.sample_values: Dict[str, float] = {}
def recalc_interval_array(self, candle_num): pt1 = Point("", self.candles[candle_num - 1].date, self.candles[candle_num - 1].close) pt2 = Point("", self.candles[candle_num].date, self.candles[candle_num].close) new_r = Range(pt1, pt2, 0) self.ranges.pop(0) self.ranges.append(new_r) ##if candle_num %10 == 0: self.interval_array = self.create_interval_array(self.ranges)
def __init__(self, identifier: str='water', latex: Optional[str]='$H_2O$', comment: Optional[str]=None) -> None: """ Args: identifier: identifier of matter latex: Latex-version of identifier. If None, latex is identical with identifier comment: comment on matter """ super().__init__(identifier=identifier, latex=latex, comment=comment) self.version = '301017_dww' # reference point and operational range self.T.operational = Range(C2K(0), C2K(100)) self.T.ref = C2K(15) self.T.val = self.T.ref self.p.operational = Range(0. + atm(), 100e5 + atm()) self.p.ref = atm() self.p.val = self.p.ref # constants self.hMelt = 334.0e3 self.hVap = 2270.0e3 self.T_liq = C2K(0.0) self.T_boil = C2K(99.9839) self.composition['H2O'] = 100. # functions of temperature, pressure and spare parameter 'x' self.rho.calc = self._rho self.nu.calc = self._nu self.mu.calc = self._mu self.c_p.calc = self._c_p self.k.calc = self._k self.c_sound.calc = self._c_sound self.E.calc = self._E
def __init__(self, identifier: str='hydraulic_oil', latex: Optional[str]=None, comment: Optional[str]=None) -> None: super().__init__(identifier=identifier) """ Args: identifier: identifier of matter latex: Latex-version of identifier. If None, latex is identical with identifier comment: comment on matter """ super().__init__(identifier=identifier, latex=latex, comment=comment) self.version = '160118_dww' # reference point and operational range self.T.operational = Range(C2K(0), C2K(100)) self.T.ref = C2K(15) self.T.val = self.T.ref self.p.operational = Range(0. + atm(), 100e5 + atm()) self.p.ref = atm() self.p.val = self.p.ref # constants # self.hMelt = # self.hVap = # self.T_liq = C2K( ) # self.T_boil = C2K( ) self.composition['C12H24'] = 100 # TODO # functions of temperature, pressure and spare parameter 'x' self.rho.calc = self._rho self.nu.calc = self._nu self.mu.calc = self._mu self.c_p.calc = self._c_p self.k.calc = self._k
def add(self, arg): self.check_arg_and_send(arg, self.add_range) t = type(arg) if (t == types.IntType): self.add_range(Range(arg, arg)) elif (t == RangeType): self.add_range(arg) elif (t == types.ListType): for item in arg: self.add(item) else: raise "UNSUPORTED TYPE"
def weather_event(json_data, percentile): """ """ id = 2 precip_values = json_data['precip-mm'][percentile] available_n_values = json_data['available-n-g_m2'][percentile] day_range = 10 start_index = 0 end_index = start_index + day_range max_index = min(len(precip_values), len(available_n_values)) interval_range = Range() while(end_index < max_index): n_loss = available_n_values[start_index] - available_n_values[end_index] precip_sum = sum(precip_values[start_index:end_index]) if n_loss > 5 and precip_sum > .5: interval_range.insert(Interval(start_index, end_index)) start_index += 1 end_index += 1 current_interval = interval_range.head while current_interval: metadata = { "n_loss": available_n_values[current_interval.start] - available_n_values[current_interval.end], "precip_sum": sum(precip_values[current_interval.start:current_interval.end]), "start_day": current_interval.start, "end_day": current_interval.end } yield Event(id, metadata) current_interval = current_interval.next
def test_init(self): self.assertRaises( TypeError, Range, ) self.assertRaises(TypeError, Range, 1, 2, 3, 4) self.assertRaises(TypeError, Range, '1') class One(): def __index__(self): return 1 one = One() r = Range(one, one, one) self.assertEqual(r.start, 1) self.assertEqual(r.stop, 1) self.assertEqual(r.step, 1) r = Range(1) o = range(1) self.assertEqual(r.start, o.start) self.assertEqual(r.stop, o.stop) self.assertEqual(r.step, o.step) r = Range(1, 2) o = range(1, 2) self.assertEqual(r.start, o.start) self.assertEqual(r.stop, o.stop) self.assertEqual(r.step, o.step) r = Range(1, 2, 3) o = range(1, 2, 3) self.assertEqual(r.start, o.start) self.assertEqual(r.stop, o.stop) self.assertEqual(r.step, o.step) self.assertRaises(ValueError, Range, 1, 2, 0)
def get_map(): indent = 0 swLatitude = float(request.args['swLatitude']) swLongitude = float(request.args['swLongitude']) neLatitude = float(request.args['neLatitude']) neLongitude = float(request.args['neLongitude']) markerDiameterKM = float(request.args['markerDiameterKM']) if request.args.has_key('operators'): operators = request.args['operators'].split(",") else: operators = [] if request.args.has_key('minKw'): min_kw = float(request.args['minKw']) else: min_kw = -1 if request.args.has_key('maxKw'): max_kw = float(request.args['maxKw']) else: max_kw = -1 if min_kw >= 0 and max_kw >= 0: kw_range = Range(min_kw, max_kw) else: kw_range = None if request.args.has_key('connectionTypes'): connection_types = request.args['connectionTypes'].split(",") else: connection_types = [] ''' print('get_map(swLatitude=' + str(swLatitude) + ', swLongitude=' + str(swLongitude) + ', neLatitude=' + str(neLatitude) + ', neLongitude=' + str(neLongitude) + ', markerDiameterKM=' + str(markerDiameterKM) + ')') ''' geo_sw_ne = GeoSwNe(swLatitude, swLongitude, neLatitude, neLongitude) result = get_map_params(indent + 1, geo_sw_ne, markerDiameterKM, operators, kw_range, connection_types) exit(indent, 'get_map', '') return result
def test_iter_basic(self): self.assertEqual([x for x in Range(0)], [x for x in range(0)]) self.assertEqual([x for x in Range(1)], [x for x in range(1)]) self.assertEqual([x for x in Range(3)], [x for x in range(3)]) self.assertEqual([x for x in Range(1, 10, 3)], [x for x in range(1, 10, 3)]) self.assertEqual([x for x in Range(-5, 5, 3)], [x for x in range(-5, 5, 3)]) self.assertEqual([x for x in Range(5, -5, -3)], [x for x in range(5, -5, -3)]) self.assertEqual([x for x in Range(-1, -10, -3)], [x for x in range(-1, -10, -3)]) iterator = iter(Range(5)) self.assertIs(iterator, iter(iterator))
def __init__(self, identifier: str = 'Property', unit: str = '/', absolute: bool = True, latex: Optional[str] = None, val: Optional[Union[float, Iterable[float]]] = None, ref: Optional[Union[float, Iterable[float]]] = None, comment: Optional[str] = None, calc: Optional[Callable[..., float]] = None) -> None: super().__init__(identifier=identifier, unit=unit, absolute=absolute, latex=latex, val=val, ref=ref, comment=comment) # reference temperature gases: 15C, solids: 20C or 25C, liquids: 20C T_Celsius = 20. self.T = Parameter(identifier='T', unit='K', absolute=True) self.T.ref = C2K(T_Celsius) self.T['operational'] = Range(C2K(-40.), C2K(200.)) self.T.accuracy = Range('-1', '+1') self.p = Parameter(identifier='p', unit='Pa', absolute=True) self.p.ref = atm() self.p['operational'] = Range(0. + self.p.ref, 100e5 + self.p.ref) self.p.accuracy = Range('-1%FS', '+1%FS') self.x = Parameter(identifier='x', unit='/', absolute=True) self.x['operational'] = Range(0., 1.) self.x.ref = 0. self.accuracy = Range('-1%', '1%') self.repeatability = Range('-0.1', '+0.1', '95%') if calc is None: calc = lambda T, p, x: 1. self.calc = calc self.regression_coefficients: Optional[Iterable[float]] = None
def information_loss_given_t(self, item: Item, global_ranges: Dict[str, Range]) -> float: """Calculates the information loss upon adding <item> into this cluster Args: item: The tuple to calculate information loss based on global_ranges: The globally known ranges for each attribute Returns: The information loss given that we insert item into this cluster """ loss = 0 # For each range, check if <item> would extend it for header, header_range in self.ranges.items(): global_range = global_ranges[header] updated = Range(lower=min(header_range.lower, item.data[header]), upper=max(header_range.upper, item.data[header])) loss += updated / global_range return loss
def information_loss_given_c(self, cluster, global_ranges: Dict[str, Range]) -> float: """Calculates the information loss upon merging <cluster> into this cluster Args: cluster: The cluster to calculate information loss based on global_ranges: The globally known ranges for each attribute Returns: The information loss given that we merge cluster with this cluster """ loss = 0 # For each range, check if <item> would extend it for header, header_range in self.ranges.items(): global_range = global_ranges[header] updated = Range(lower=min(header_range.lower, cluster.ranges[header].lower), upper=max(header_range.upper, cluster.ranges[header].upper)) loss += updated / global_range return loss