def __new__(self, M=10, beta=1, phi=0, normalised=True, inverted=False): if not inverted: if phi == 0: wc = np.kaiser(M, beta) # Window coefficients win = wc / sum(wc) # Normalised window else: wc = np.kaiser(M, beta) # Window coefficients m = np.arange(0, M) # Create M indeces from 0 to 1 a = exp(-1j * 2 * pi * m * phi) # Steering vector ws = dot(wc, a) # Normalisation factor win = a * wc / ws # Steered and normalised window else: if phi == 0: wc = 1 / np.kaiser(M, beta) # Window coefficients win = wc / sum(wc) # Normalised window else: wc = 1 / np.kaiser(M, beta) # Window coefficients m = np.arange(0, M) # Create M indeces from 0 to 1 a = exp(-1j * 2 * pi * m * phi) # Steering vector ws = dot(wc, a) # Normalisation factor win = a * wc / ws # Steered and normalised window w = np.Ndarray.__new__(self, win) # axes=('M',), # desc = 'Kaiser (beta=%d, phi=%d)'%(beta,phi)) # shape_desc=('M','1')) return w
def create_modular_toy(edges, modular_edges): adjMatrix = np.zeros((15, 15)) module_1, module_2, module_3 = np.arange(5), np.arange(5, 10), np.arange( 10, 15) modules = [] modules.append(module_1), modules.append(module_2), modules.append( module_3) def add_modular_edge(): randomComm = seeded_rng.randint(0, 3) randEdge = seeded_rng.choice(modules[randomComm], 2, replace=False) while adjMatrix[randEdge[0]][randEdge[1]] != 0 or adjMatrix[ randEdge[1]][randEdge[0]] != 0: randomComm = seeded_rng.randint(0, 3) randEdge = seeded_rng.choice(modules[randomComm], 2, replace=False) adjMatrix[randEdge[0]][randEdge[1]] += 1 adjMatrix[randEdge[1]][randEdge[0]] += 1 def add_cross_edge(): #adds edge outside modules randEdge = seeded_rng.choice(15, 2, replace=False) while adjMatrix[randEdge[0]][randEdge[1]] != 0 or adjMatrix[randEdge[1]][randEdge[0]] != 0 or \ (randEdge[0] in module_1 and randEdge[1] in module_1) \ or (randEdge[0] in module_2 and randEdge[1] in module_2) or \ (randEdge[0] in module_3 and randEdge[1] in module_3): randEdge = seeded_rng.choice(15, 2, replace=False) adjMatrix[randEdge[0]][randEdge[1]] += 1 adjMatrix[randEdge[1]][randEdge[0]] += 1 for i in range(modular_edges): add_modular_edge() for i in range(edges - modular_edges): add_cross_edge() return adjMatrix
def create_undirected_network(N, edges): adjMatrix = np.zeros((N, N), dtype=float) perm = np.arange(N) for i in range(N - 1): adjMatrix[perm[i]][perm[i + 1]] = 1.0 adjMatrix[perm[i + 1]][perm[i]] = 1.0 for i in range(edges - N + 1): randEdge = seeded_rng.choice(N, 2, replace=False) while adjMatrix[randEdge[0]][randEdge[1]] != 0: randEdge = seeded_rng.choice(N, 2, replace=False) #print("STUCK") adjMatrix[randEdge[0]][randEdge[1]] = 1.0 adjMatrix[randEdge[1]][randEdge[0]] = 1.0 return adjMatrix
def __new__(self, M=10, phi=0, normalised=True): # Create the window if phi == 0: win = np.ones((M, ), dtype=None) / M else: wc = np.ones(M, dtype=complex) # Window coefficients m = np.arange(0, M) # Create M indeces from 0 to 1 a = exp(-1j * 2 * pi * m * phi) # Steering vector ws = dot(wc, a) # Normalisation factor win = a * wc / ws # Steered and normalised window w = np.Ndarray.__new__(self, win) # axes=('M',), # desc = 'Rectangular (phi=%d)'%phi) # desc='Rectangular (phi=%d)'%phi, # shape_desc=('M','1')) return w
def __new__(self, M=10, a=0.54, phi=0, normalised=True): # Create the window if phi == 0: wc = a + (1 - a) * np.cos(2 * pi * np.linspace(-0.5, 0.5, M)) win = wc / sum(wc) # Normalised window else: n = np.linspace(-0.5, 0.5, M) wc = a + (1 - a) * np.cos(2 * pi * n) # Window coefficients m = np.arange(0, M) # Create M indeces from 0 to 1 aa = exp(-1j * 2 * pi * m * phi) # Steering vector ws = dot(wc, aa) # Normalisation factor win = aa * wc / ws # Steered and normalised window w = np.Ndarray.__new__(self, win) # axes=('M',), # desc = 'Rectangular (phi=%d)'%phi) # desc='Rectangular (phi=%d)'%phi, # shape_desc=('M','1')) return w
def __find_new_slot__(self, root_node, conf): import mynumpy as np dummy_conf = {} db = Configurable.__db__ class_node = db.save(root_node, self.__class__.__name__, None, type='group') # If the configuration is new, set a new slot as default if conf == None: id = class_node._v_nchildren # If a configuration exists, set the old slot as default else: id = conf['id'] conf = conf['class_attr'] # Iterate over each of the index slots in the class is_equal = True for i in range(0,class_node._v_nchildren): id_node = db.save(class_node, i, None, type='id') is_equal = True # Iterate over all class variables for key,val in self.__dict__.iteritems(): if val != None: cn = val.__class__.__name__ vc = val.__class__ # For configurable types, just make sure their entry is the DB if issubclass(vc, Configurable): if cn not in root_node: is_equal = False break # The values of common types need to be compared aswell if vc == float or vc == long or vc == int or vc == complex or vc == str: # Fetch the data if it exists if key in id_node: db_val = getattr(id_node, key) else: is_equal = False break # Compare the values. If they are unequal, or the compare operation # is undefined for the two types, the values are treated as unequal. try: if val != db_val.read(): is_equal = False break except: is_equal = False break # Reading and comparing all arrays in the database is performance # intensive. To speed up the process the DB entry is only read if the # matrix dimensions are equal: elif issubclass(vc, np.Ndarray): # Fetch the data if it exists if key in id_node: db_val = getattr(id_node, key) else: is_equal = False break # Record the array lengths val_len = val.shape.__len__() db_val_len = db_val.shape.__len__() # Are the values scalars? if val_len==0 or (val_len==1 and val.shape==(1,)): if db_val_len==0 or (db_val_len==1 and db_val.shape==(1,)): db_val = db_val.read() if val != db_val: is_equal = False break # ...or arrays? else: # Make sure the DB entry is an array-type if db_val_len>1 or db_val.shape[0]>1: if db_val.shape == val.shape: db_val = db_val.read() t = val != db_val if (np.isscalar(t) and t) or t.any(): is_equal = False break else: is_equal = False break elif vc == tuple or vc == list: val_len = val.__len__() db_val = [] # Create the wrapper object and fill it with list/tuple items if vc == tuple: tw = TupleWrapper() else: tw = ListWrapper() for j in np.arange(0,val_len): setattr(tw, 'i%d'%j, val[j]) # Call its find new slot to recursively dig into the data was_equal,id2 = tw.__find_new_slot__(root_node, conf) # If any of the list items were unequal, we return if was_equal == False: is_equal = False break # If all contents are equal to that stored at a specific slot in the DB, # set the id of this class to match that DB slot. # TODO: Performance tip: I'm storing thing twice now.. if is_equal: id = i break return is_equal,id
def __load__(self, root_node, conf): import mynumpy as np import Coordinate ''' self_key The variable in the calling class that points to self db The database depth The recursive level''' # from TypeExtensions import Ndarray class_node = getattr(root_node, self.__class__.__name__) id = conf.pop('id') node = getattr(class_node,'i%d'%id) conf = conf['class_attr'] # Iterate over all class instance variables for key in conf: # Lookup the type of the variable try: class_type = conf[key].pop('class_type') except: class_type = conf[key] # Configurable types are first created, then asked to load themselves if issubclass(class_type, Configurable): # Hmm, this is some nasty piece of code; A 'lookahead' to fetch the # data of the base class in order to initialize the Ndarray with # that data.. if class_type == np.Ndarray: data = getattr(node,key).read() # tmp = class_type(data) # tmp.__load__(root_node, conf[key]) # axes = tmp.__dict__.pop('axes') # setattr(self, key, class_type(data, axes)) setattr(self, key, class_type(data)) # for subkey in tmp.__dict__: # setattr(getattr(self, key), subkey, getattr(tmp, subkey)) # TODO: Should make this more general, but no time for that now elif issubclass(class_type, Coordinate.Coordinate): data = getattr(node,key).read() setattr(self, key, class_type(x=data[:,0],y=data[:,1],z=data[:,2])) else: setattr(self, key, class_type()) getattr(self,key).__load__(root_node, conf[key]) # Unwrap tuples and lists if necessary: if class_type == TupleWrapper or class_type == ListWrapper: val = [] for i in np.arange(0,getattr(self,key).len): subval = getattr(getattr(self,key), 'i%d'%i) if subval == 'None': subval = None val.append(subval) delattr(self,key) if class_type == TupleWrapper: val = tuple(val) setattr(self, key, tuple(val)) elif class_type == DictWrapper: val = {} for subkey in self.__dict__: if subkey == 'None': val[None] = getattr(getattr(self,key),'None') else: val[subkey] = getattr(getattr(self,key), subkey) delattr(self,key) setattr(self, key, val) else: setattr(self, key, getattr(node,key).read())
def __find_new_slot__(self, root_node, conf): import mynumpy as np dummy_conf = {} db = Configurable.__db__ class_node = db.save(root_node, self.__class__.__name__, None, type='group') # If the configuration is new, set a new slot as default if conf == None: id = class_node._v_nchildren # If a configuration exists, set the old slot as default else: id = conf['id'] conf = conf['class_attr'] # Iterate over each of the index slots in the class is_equal = True for i in range(0, class_node._v_nchildren): id_node = db.save(class_node, i, None, type='id') is_equal = True # Iterate over all class variables for key, val in self.__dict__.iteritems(): if val != None: cn = val.__class__.__name__ vc = val.__class__ # For configurable types, just make sure their entry is the DB if issubclass(vc, Configurable): if cn not in root_node: is_equal = False break # The values of common types need to be compared aswell if vc == float or vc == long or vc == int or vc == complex or vc == str: # Fetch the data if it exists if key in id_node: db_val = getattr(id_node, key) else: is_equal = False break # Compare the values. If they are unequal, or the compare operation # is undefined for the two types, the values are treated as unequal. try: if val != db_val.read(): is_equal = False break except: is_equal = False break # Reading and comparing all arrays in the database is performance # intensive. To speed up the process the DB entry is only read if the # matrix dimensions are equal: elif issubclass(vc, np.Ndarray): # Fetch the data if it exists if key in id_node: db_val = getattr(id_node, key) else: is_equal = False break # Record the array lengths val_len = val.shape.__len__() db_val_len = db_val.shape.__len__() # Are the values scalars? if val_len == 0 or (val_len == 1 and val.shape == (1, )): if db_val_len == 0 or (db_val_len == 1 and db_val.shape == (1, )): db_val = db_val.read() if val != db_val: is_equal = False break # ...or arrays? else: # Make sure the DB entry is an array-type if db_val_len > 1 or db_val.shape[0] > 1: if db_val.shape == val.shape: db_val = db_val.read() t = val != db_val if (np.isscalar(t) and t) or t.any(): is_equal = False break else: is_equal = False break elif vc == tuple or vc == list: val_len = val.__len__() db_val = [] # Create the wrapper object and fill it with list/tuple items if vc == tuple: tw = TupleWrapper() else: tw = ListWrapper() for j in np.arange(0, val_len): setattr(tw, 'i%d' % j, val[j]) # Call its find new slot to recursively dig into the data was_equal, id2 = tw.__find_new_slot__(root_node, conf) # If any of the list items were unequal, we return if was_equal == False: is_equal = False break # If all contents are equal to that stored at a specific slot in the DB, # set the id of this class to match that DB slot. # TODO: Performance tip: I'm storing thing twice now.. if is_equal: id = i break return is_equal, id
def __load__(self, root_node, conf): import mynumpy as np import Coordinate ''' self_key The variable in the calling class that points to self db The database depth The recursive level''' # from TypeExtensions import Ndarray class_node = getattr(root_node, self.__class__.__name__) id = conf.pop('id') node = getattr(class_node, 'i%d' % id) conf = conf['class_attr'] # Iterate over all class instance variables for key in conf: # Lookup the type of the variable try: class_type = conf[key].pop('class_type') except: class_type = conf[key] # Configurable types are first created, then asked to load themselves if issubclass(class_type, Configurable): # Hmm, this is some nasty piece of code; A 'lookahead' to fetch the # data of the base class in order to initialize the Ndarray with # that data.. if class_type == np.Ndarray: data = getattr(node, key).read() # tmp = class_type(data) # tmp.__load__(root_node, conf[key]) # axes = tmp.__dict__.pop('axes') # setattr(self, key, class_type(data, axes)) setattr(self, key, class_type(data)) # for subkey in tmp.__dict__: # setattr(getattr(self, key), subkey, getattr(tmp, subkey)) # TODO: Should make this more general, but no time for that now elif issubclass(class_type, Coordinate.Coordinate): data = getattr(node, key).read() setattr( self, key, class_type(x=data[:, 0], y=data[:, 1], z=data[:, 2])) else: setattr(self, key, class_type()) getattr(self, key).__load__(root_node, conf[key]) # Unwrap tuples and lists if necessary: if class_type == TupleWrapper or class_type == ListWrapper: val = [] for i in np.arange(0, getattr(self, key).len): subval = getattr(getattr(self, key), 'i%d' % i) if subval == 'None': subval = None val.append(subval) delattr(self, key) if class_type == TupleWrapper: val = tuple(val) setattr(self, key, tuple(val)) elif class_type == DictWrapper: val = {} for subkey in self.__dict__: if subkey == 'None': val[None] = getattr(getattr(self, key), 'None') else: val[subkey] = getattr(getattr(self, key), subkey) delattr(self, key) setattr(self, key, val) else: setattr(self, key, getattr(node, key).read())