def getSynapse(self, id=-1, fromId=-1, toId=-1): ''' 取得特定突触 :param id: str or int 突触id :param fromId: str or int 输入神经元 :param toId: str or int 输出神经元 :return: ''' if id != -1: return collections.first(self.synapses, lambda s: s.id == id) return collections.first( self.synapses, lambda s: s.fromId == fromId and s.toId == toId)
def getVariable(self,name): ''' 取得变量 :param name: str 变量名 :return: ''' return collections.first(self.variables, lambda var: var.nameInfo.hasName(name))
def getVariableValue(self,name,default=0.0): ''' 取得变量的值 :param name: str 变量名 :param default: float 缺省值 0.0 :return: ''' var = collections.first(self.variables, lambda var: var.nameInfo.hasName(name)) if var is None: return default return var.value
def __setitem__(self, key, value): # 在变量集合中查找 var = collections.first(self.variables, lambda var: var.nameInfo.hasName(key)) if var is not None: var.value = value return # 在状态集合中查找 self.states[key] = value
def getNeuron(self, id=-1, layer=-1, xhInLayer=-1, coord=None): ''' 查找满足特定条件神经元(先按id,再按坐标,在按层和层内序号) :param id: 神经元id,有效则会优先查找 :param layer: 所在层 :param xhInLayer: 层内序号 :param coord: 坐标 :return: ''' if id > 0: #优先按照id查找 ns = self.getNeurons() return collections.first(ns, lambda n: n.id == id) if coord is not None: #其次按照特定坐标寻找 ns = self.getNeurons() return collections.first(ns, lambda n: n.coord == coord) #查找特定层中某个序号的神经元 ns = self.getNeurons(layer=layer) if not collections.isEmpty(ns): return None if xhInLayer >= len(ns): return None return ns[xhInLayer]
def _get_grid_by_value(self, values, activation_intensity_list, createifnotexisted=True): ''' 计算值在特征空间所属的网格 :param values: Union(list,array) 值 :param activation_intensity_list: list values对所有节点的激活强度列表 :param createifnotexisted: Bool 若网格不存在则创建一个 :return: tuple grid元组, list grid各维的序号 list grid中心点值 float values最大激活强度 int values最大激活强度对应的节点序号 ''' dimension = len(values) no, center = [], [] for i in range(dimension): clip = self.gene.clip[i] if len(self.gene.clip) > i else [0., 1.] grid_width = (clip[1] - clip[0]) / self.net.definition.box.grid_size index = int((values[i] - clip[0]) / grid_width) no.append(index) center.append( (index * grid_width + (index + 1) * grid_width) / 2.0) grid = collections.first(self.grids, lambda x: np.equal(x['grid_xh'], no)) if grid is None and createifnotexisted: node_index = -1 if len(activation_intensity_list) > 0: asc_index = np.argsort(activation_intensity_list) max_intersity, node_index = activation_intensity_list[ asc_index[-1]], asc_index[-1] grid = { 'grid_xh': no, 'grid_center': center, 'samples_count': 0, 'node_id': self.nodes[node_index].id if node_index >= 0 else -1 } self.grids.append(grid) grid['samples_count'] += 1 return grid, no, center
def __getitem__(self, item): ''' 取索引 :param item:如果索引项是变量集合中的名字,则返回该变量的值;如果是状态集合中状态的名称,则返回状态的值 :return: ''' # 在变量集合中查找 var = collections.first(self.variables,lambda var:var.nameInfo.hasName(item)) if var is not None:return var.value # 在状态集合中查找 #if item in self.states.keys(): if item in self.states: return self.states[item] # 在参数集合中查找 if item in self.params.keys(): return self.params[item] return super.__getitem__(item)
def dump_resume(self): # Check if the data exists save_resume_data: bool = False try: # Destructive unpacking of the tuple # Discarding start and end since they are not required for (_, _), data in self.data_unfinished: # Check if there is any element is None # Since there is no need to create resume file otherwise if data is not None: save_resume_data = True except (NameError, ValueError, AttributeError): # Exists if the variable doesn't exists or data is incompatible pass # If there is data if save_resume_data: print('Saving resume data ...') with open('resume.pyb', 'wb') as f: # Dump the data using serialization hook dump(ClientInfo(first(self.checks), self.data_unfinished), f) else: print('No data was fetched ...')
def getAttention(cls,name): return collections.first(attention_operations,lambda x:x.name == name)
def getSpecie(self, specieId): return collections.first(self.species, lambda s: s.id == specieId)
def getInd(self, id): return collections.first(self.inds, lambda ind: ind.id == id)
def test_first(self): self.assertEqual(first([x for x in range(10)]), 0) self.assertEqual(first([]), None)
def get_file_size(self) -> None: self.generate_connections(sockets=(0, )) self.file_size = int( notnone(first(notnone(self._get(self.conns[0], Request.FILE_SIZE)))))
def get_file_name(self): self.generate_connections(sockets=(0, )) self.file_name = str( notnone(first(notnone(self._get(self.conns[0], Request.FILE_NAME)))))