def check_square(arg, message='Matrix is not square: %(actual)s', level=1): if np.size(arg) > 1: s = np.shape(arg) check(len(s) == 2 and s[0] == s[1], message=lambda: message % {'actual': s}, level=level) return arg
def xtimes(start, stop=None, step=None): checks.check_not_none(start) if step is None: if isinstance(start, (dt.date, dt.time, dt.datetime)) or isinstance(stop, (dt.date, dt.time, dt.datetime)): step = dt.timedelta(days=1) elif isinstance(start, float) or isinstance(stop, float): step = 1. else: step = 1 resultwrap = lambda x: x if isinstance(start, dt.time): start = dt.datetime.combine(dt.datetime(1,1,1,0,0,0), start) resultwrap = lambda x: x.time() if isinstance(stop, dt.time): stop = dt.datetime.combine(dt.datetime(1,1,1,0,0,0), stop) if stop is not None else None resultwrap = lambda x: x.time() stepfunc = step if checks.is_callable(step) else lambda x: step s = stepfunc(start) checks.check(npu.sign(s) != 0, 'Step must be positive or negative, not zero') if stop is None: while True: yield resultwrap(start) start += s s = stepfunc(start) else: while npu.sign(start - stop) == -npu.sign(s): yield resultwrap(start) start += s s = stepfunc(start)
def check_ndim_1( arg, message='Not a one-dimensional array; number of dimensions: %(actual)d', level=1): n = np.ndim(arg) check(n == 1, message=lambda: message % {'actual': n}, level=level) return arg
def wrapper(*args, **kwargs): # NB: No self if pre is not None: check(pre(*args, **kwargs), message=lambda: message, level=level) retval = func(*args, **kwargs) if post is not None: check(post(retval), message=lambda: message, level=level) return retval
def check_same_shape(arg1, arg2, message='Shapes differ: %(shape1)s vs %(shape2)s', level=1): s1 = np.shape(arg1) s2 = np.shape(arg2) check(s1 == s2, message=lambda: message % { 'shape1': s1, 'shape2': s2 }, level=level) return s1
def check_shape( arg, shape, message='Unexpected shape: actual=%(actual)s, expected=%(expected)s', level=1): s = np.shape(arg) check(s == shape, message=lambda: message % { 'actual': s, 'expected': shape }, level=level) return arg
def check_ncol( arg, ncol, message='Unexpected number of columns: actual=%(actual)d, expected=%(expected)d', level=1): n = npu.ncol(arg) check(n == ncol, message=lambda: message % { 'actual': n, 'expected': ncol }, level=level) return arg
def check_nrow( arg, nrow, message='Unexpected number of rows: actual=%(actual)d, expected=%(expected)d', level=1): n = npu.nrow(arg) check(n == nrow, message=lambda: message % { 'actual': n, 'expected': nrow }, level=level) return arg
def check_size( arg, size, message='Unexpected size: actual=%(actual)d, expected=%(expected)d', level=1): n = np.size(arg) check(n == size, message=lambda: message % { 'actual': n, 'expected': size }, level=level) return arg
def __init__(self, direction, name=None, host=None, port=22184, zipped=False): if name is None: name = random.choice(string.ascii_uppercase) + \ ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3)) checks.check_string(name, allow_none=False) checks.check(lambda: name.isalnum(), 'Pype\'s name is not alphanumeric') checks.check(lambda: len(name) > 0, 'Pype\'s name is an empty string') self._name = name self._main_topic = self._name self._main_topic_bytes = (self._main_topic + ' ').encode('utf-8') self._eof_topic = self._name + '!' self._eof_topic_bytes = (self._eof_topic + ' ').encode('utf-8') self._port = port self._context = zmq.Context() if direction == Direction.INCOMING: if host is None: host = 'localhost' self._host = host self._socket = self._context.socket(zmq.SUB) self._socket.connect('tcp://%s:%d' % (self._host, self._port)) self._socket.setsockopt_string(zmq.SUBSCRIBE, self._name) self._socket.setsockopt_string(zmq.SUBSCRIBE, self._eof_topic) elif direction == Direction.OUTGOING: if host is None: host = '*' self._host = host self._socket = self._context.socket(zmq.PUB) self._socket.bind('tcp://%s:%d' % (self._host, self._port)) else: raise ValueError('Unexpected direction: %s' % str(direction)) self._direction = direction self._closed = False self._zipped = zipped self._to_string_helper_Pype = None self._str_Pype = None
def checkcol(arg, message='Matrix is not a column: %(actual)s', level=1): s = np.shape(arg) check(len(s) == 2 and s[1] == 1, message=lambda: message % {'actual': s}, level=level) return arg
def visualize_grid_search(grid_search_result, aggregate_func=np.nanmean, empty_aggregate='none', fig=None, title=None, refresh_until_ready=False): if fig is None: fig = plt.figure() if title is None: title = grid_search_result.optimization_id fig.suptitle(title) param_names = list(grid_search_result.param_ranges.keys()) subplots = {} heatmaps = {} datas = {} for i1 in range(len(param_names)): param_name1 = param_names[i1] param_values1 = grid_search_result.param_ranges[param_name1] for i2 in range(i1): param_name2 = param_names[i2] param_values2 = grid_search_result.param_ranges[param_name2] data = np.empty((len(param_values1), len(param_values2)), dtype=object) for i in range(np.size(data)): data.flat[i] = [] datas[(i1, i2)] = data ax = fig.add_subplot( len(param_names) - 1, len(param_names) - 1, (i1 - 1) * (len(param_names) - 1) + i2 + 1) subplots[(i1, i2)] = ax initial_data = _aggregate(aggregate_func, datas[(i1, i2)], empty_aggregate) heatmaps[(i1, i2)] = ax.matshow(npu.apply(aggregate_func, initial_data), cmap='coolwarm') if i2 == i1 - 1: ax.set_xticklabels( [np.nan] + [0. if x == 1e-06 else x for x in param_values2], fontsize=6, rotation='vertical', verticalalignment='bottom') ax.xaxis.set_ticks_position('top') ax.set_yticklabels( [np.nan] + [0. if x == 1e-06 else x for x in param_values1], fontsize=6) ax.yaxis.set_ticks_position('right') else: ax.set_xticks([]) ax.set_yticks([]) if i1 == len(param_names) - 1: ax.set_xlabel(param_name2) if i2 == 0: ax.set_ylabel(param_name1) while True: all_ready = True for status in grid_search_result.evaluation_statuses: if not status.ready: all_ready = False else: checks.check( utils.sequence_eq(param_names, status.work.info['param_names'])) param_value_index_combinations = itertools.combinations( range(len(param_names)), 2) param_value_index_combinations = [ (i2, i1) for (i1, i2) in param_value_index_combinations if i1 != i2 ] for i1, i2 in param_value_index_combinations: param_value_index1 = status.work.info[ 'param_value_indices'][i1] param_value_index2 = status.work.info[ 'param_value_indices'][i2] if status.result.exception is not None: result = np.nan elif status.result.result is None: result = np.nan else: result = status.result.result datas[(i1, i2)][param_value_index1, param_value_index2].append(result) for i1 in range(len(param_names)): for i2 in range(i1): new_data = _aggregate(aggregate_func, datas[(i1, i2)], empty_aggregate) heatmaps[(i1, i2)].set_data(new_data) heatmaps[(i1, i2)].autoscale() if (not refresh_until_ready) or all_ready: break else: fig.canvas.draw() time.sleep(1) return fig