def collapse_at(stepmon, target=None, tolerance=0.005, \ generations=50, mask=None): '''return a set of indices where the parameters exhibit a dimensional collapse at the specified target. Dimensional collapse is defined by: change(param[i]) <= tolerance over N generations, where: change(param[i]) = max(param[i]) - min(param[i]) if target = None, or change(param[i]) = abs(param[i] - target) otherwise. target can be None, a single value, or a list of values of param length collapse will be ignored at any indices specififed in the mask ''' np = _m.numpy # reject bad masks if mask is None: pass elif type(mask) is set: for i in mask: if hasattr(i, '__len__'): msg = "bad element '%s' in mask" % str(i) raise ValueError(msg) else: msg = "%s is not a valid mask" % str(mask) raise TypeError(msg) # is max distance from target less than tolerance across all generations? tolerance = np.asarray(tolerance) tolerance.shape = (-1, 1) params = _m._solutions(stepmon, generations) if target is None: params = params.ptp(axis=0) <= tolerance else: params = abs(params - target).max(axis=0) <= tolerance # get tuple of indices of where collapsed params = np.where(params)[-1] # apply mask if mask is None: return set(params) mask = set(mask) return set(i for i in params if i not in mask)
def collapse_at(stepmon, target=None, tolerance=0.005, \ generations=50, mask=None): '''return a set of indices where the parameters exhibit a dimensional collapse at the specified target. Dimensional collapse is defined by: change(param[i]) <= tolerance over N generations, where: change(param[i]) = max(param[i]) - min(param[i]) if target = None, or change(param[i]) = abs(param[i] - target) otherwise. target can be None, a single value, or a list of values of param length collapse will be ignored at any indices specififed in the mask ''' np = _m.numpy # reject bad masks if mask is None: pass elif type(mask) is set: for i in mask: if hasattr(i, '__len__'): msg = "bad element '%s' in mask" % str(i) raise ValueError(msg) else: msg = "%s is not a valid mask" % str(mask) raise TypeError(msg) # is max distance from target less than tolerance across all generations? tolerance = np.asarray(tolerance) tolerance.shape = (-1,1) params = _m._solutions(stepmon, generations) if target is None: params = params.ptp(axis=0) <= tolerance else: params = abs(params - target).max(axis=0) <= tolerance # get tuple of indices of where collapsed params = np.where(params)[-1] # apply mask if mask is None: return set(params) mask = set(mask) return set(i for i in params if i not in mask)
def collapse_as(stepmon, offset=False, tolerance=0.005, \ generations=50, mask=None): '''return a set of pairs of indices where the parameters exhibit a dimensional collapse. Dimensional collapse is defined by: max(pairwise(parameters)) <= tolerance over N generations (offset=False), ptp(pairwise(parameters)) <= tolerance over N generations (offset=True). collapse will be ignored at any pairs of indices specififed in the mask. If single indices are provided, ignore all pairs with the given indices. ''' np = _m.numpy # reject bad masks if mask is None: pass elif type(mask) is set: for i in mask: if hasattr(i, '__len__') and len(i) != 2: msg = "bad element '%s' in mask" % str(i) raise ValueError(msg) else: msg = "%s is not a valid mask" % str(mask) raise TypeError(msg) # is the max position difference less than tolerance across all generations? distances = _m._solutions(stepmon, generations) #FIXME: HACK: array should be ndim=2... apparently sometimes it's ndim=3 if distances.ndim == 3 and distances.shape[-1] == 1: distances.shape = distances.shape[:-1] elif distances.ndim < 3: pass else: msg = 'could not extract pairwise distances from array with shape %s' % distances.shape raise ValueError(msg) nindices = distances.shape[-1] # get distances and pairs of indices from mystic.tools import pairwise distances, pairs = pairwise(distances, True) if offset: # tracking at a distance distances = distances.ptp(axis=0) <= tolerance else: # tracking with the same position distances = distances.max(axis=0) <= tolerance # get the (index1,index2) pairs where the collapse occurs if distances.ndim > 1: distances.shape = tuple(i for i in distances.shape if i != 1) or (1, ) distances = np.array(pairs)[distances] # apply mask if mask is None: return set(tuple(i) for i in distances) mask = selector(mask) return set(tuple(i) for i in distances if not mask(i))
def collapse_as(stepmon, offset=False, tolerance=0.005, \ generations=50, mask=None): '''return a set of pairs of indices where the parameters exhibit a dimensional collapse. Dimensional collapse is defined by: max(pairwise(parameters)) <= tolerance over N generations (offset=False), ptp(pairwise(parameters)) <= tolerance over N generations (offset=True). collapse will be ignored at any pairs of indices specififed in the mask. If single indices are provided, ignore all pairs with the given indices. ''' np = _m.numpy # reject bad masks if mask is None: pass elif type(mask) is set: for i in mask: if hasattr(i, '__len__') and len(i) != 2: msg = "bad element '%s' in mask" % str(i) raise ValueError(msg) else: msg = "%s is not a valid mask" % str(mask) raise TypeError(msg) # is the max position difference less than tolerance across all generations? distances = _m._solutions(stepmon, generations) #FIXME: HACK: array should be ndim=2... apparently sometimes it's ndim=3 if distances.ndim == 3 and distances.shape[-1] == 1: distances.shape = distances.shape[:-1] elif distances.ndim < 3: pass else: msg = 'could not extract pairwise distances from array with shape %s' % distances.shape raise ValueError(msg) nindices = distances.shape[-1] # get distances and pairs of indices from mystic.tools import pairwise distances, pairs = pairwise(distances, True) if offset: # tracking at a distance distances = distances.ptp(axis=0) <= tolerance else: # tracking with the same position distances = distances.max(axis=0) <= tolerance # get the (index1,index2) pairs where the collapse occurs if distances.ndim > 1: distances.shape = tuple(i for i in distances.shape if i != 1) or (1,) distances = np.array(pairs)[distances] # apply mask if mask is None: return set(tuple(i) for i in distances) mask = selector(mask) return set(tuple(i) for i in distances if not mask(i))