def recalculate_position(self): """ takes all the positions of the associated observation list and recalculated a position """ global blah if self.stype == "real": print "! not supposed to do this with a real source.." return if len(self.associated_obs) == 0: return pos_array = numarray.fromlist( [[x.pos[0], x.pos[1], x.assumed_err[0], x.assumed_err[1]] for x in self.associated_obs]) raa = numarray.fromlist([x[0] for x in pos_array]) raerra = numarray.fromlist([x[2] for x in pos_array]) deca = numarray.fromlist([x[1] for x in pos_array]) decerra = numarray.fromlist([x[3] for x in pos_array]) ra = numarray.sum(raa / raerra**2) / numarray.sum(1.0 / raerra**2) dec = numarray.sum(deca / decerra**2) / numarray.sum(1.0 / decerra**2) raerr = math.sqrt(1.0 / numarray.sum(1.0 / raerra**2)) decerr = math.sqrt(1.0 / numarray.sum(1.0 / decerra**2)) self.current_pos = [ra, dec] self.current_err = [raerr, decerr]
def plot_source_info(self,event): ra = event.xdata dec = event.ydata #print (event.key,ra,dec) if event.key == 's': #print self.constructed_pos[:,0] #print self.constructed_pos[:,1] dist = numarray.sqrt( (self.constructed_pos[:,0] - ra)**2 + (self.constructed_pos[:,1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress(dist == min(dist), numarray.fromlist(range(len(self.constructed_source_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.constructed_source_list[the_source_ind[0]] print the_source dist = numarray.sqrt( (the_source.current_pos[0] - self.real_pos[:,0])**2 + (the_source.current_pos[1] - self.real_pos[:,1])**2) print "min distances to nearest real source = %f arcsec" % min(dist) the_source_ind = numarray.compress(dist == min(dist), numarray.fromlist(range(len(self.real_list)))) the_source = self.real_list[the_source_ind[0]] print "That real source is at ra=%f dec=%f" % (the_source.start_pos[0],the_source.start_pos[1]) if event.key == 'r': dist = numarray.sqrt( (self.real_pos[:,0] - ra)**2 + (self.real_pos[:,1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress(dist == min(dist), numarray.fromlist(range(len(self.real_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.real_list[the_source_ind[0]] print the_source
def plot_source_info(self, event): ra = event.xdata dec = event.ydata #print (event.key,ra,dec) if event.key == 's': #print self.constructed_pos[:,0] #print self.constructed_pos[:,1] dist = numarray.sqrt((self.constructed_pos[:, 0] - ra)**2 + (self.constructed_pos[:, 1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress( dist == min(dist), numarray.fromlist(range(len(self.constructed_source_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.constructed_source_list[the_source_ind[0]] print the_source dist = numarray.sqrt( (the_source.current_pos[0] - self.real_pos[:, 0])**2 + (the_source.current_pos[1] - self.real_pos[:, 1])**2) print "min distances to nearest real source = %f arcsec" % min( dist) the_source_ind = numarray.compress( dist == min(dist), numarray.fromlist(range(len(self.real_list)))) the_source = self.real_list[the_source_ind[0]] print "That real source is at ra=%f dec=%f" % ( the_source.start_pos[0], the_source.start_pos[1]) if event.key == 'r': dist = numarray.sqrt((self.real_pos[:, 0] - ra)**2 + (self.real_pos[:, 1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress( dist == min(dist), numarray.fromlist(range(len(self.real_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.real_list[the_source_ind[0]] print the_source
def Matrix(data, typecode=None, copy=1, savespace=0): """Matrix constructs new matrices from 2D nested lists of numbers""" if isinstance(data, type("")): raise TypeError("numerix Matrix does not support Numeric matrix string notation. Use nested lists.") a = fromlist(data, type=typecode) if a.rank == 0: a.shape = (1,1) elif a.rank == 1: a.shape = (1,) + a.shape a.__class__ = _Matrix return a
def recalculate_position(self): """ takes all the positions of the associated observation list and recalculated a position """ global blah if self.stype=="real": print "! not supposed to do this with a real source.." return if len(self.associated_obs) == 0: return pos_array = numarray.fromlist([[x.pos[0],x.pos[1],x.assumed_err[0],x.assumed_err[1]] for x in self.associated_obs]) raa = numarray.fromlist([x[0] for x in pos_array]) raerra = numarray.fromlist([x[2] for x in pos_array]) deca = numarray.fromlist([x[1] for x in pos_array]) decerra = numarray.fromlist([x[3] for x in pos_array]) ra = numarray.sum(raa/raerra**2)/numarray.sum(1.0/raerra**2) dec = numarray.sum(deca/decerra**2)/numarray.sum(1.0/decerra**2) raerr = math.sqrt(1.0/numarray.sum(1.0/raerra**2)) decerr = math.sqrt(1.0/numarray.sum(1.0/decerra**2)) self.current_pos = [ra,dec] self.current_err = [raerr,decerr]
def run(self, shuffle=True): constructed_source_list = [] observation_list = [] obslist = self.obj_dict.keys() if shuffle: random.seed() random.shuffle(obslist) random.shuffle(obslist) random.shuffle(obslist) print "shuffled" for theo in obslist: # choose a real source to draw an observation from o = obs(initial_pos=[theo[0], theo[1]], assumed_err=[theo[2], theo[3]]) o.pos = [theo[0], theo[1]] o.plot() tmp = o.is_associated_with_source(constructed_source_list) # print tmp if tmp['answer'] == True: # o.associate_with_source(tmp['sources']) for s in tmp['best_source']: # print (len(tmp['best_source']),o.pos,s.current_pos) s.add_associated_obs(copy.deepcopy(o)) s.recalculate_position() else: ## make a new source s = source(start_pos=copy.copy(o.pos),stype='constructed',start_err=copy.copy(o.assumed_err),current_pos=copy.copy(o.pos),\ current_err=copy.copy(o.assumed_err),associated_obs=[copy.deepcopy(o)]) print "1 new source" #print s #print s.associated_obs constructed_source_list.append(copy.deepcopy(s)) observation_list.append(copy.deepcopy(o)) for s in constructed_source_list: # print s s.plot('g^') self.constructed_source_list = constructed_source_list self.constructed_pos = (numarray.fromlist( [x.current_pos for x in self.constructed_source_list]))
def run(self,shuffle=True): constructed_source_list = [] observation_list = [] obslist = self.obj_dict.keys() if shuffle: random.seed() random.shuffle(obslist) random.shuffle(obslist) random.shuffle(obslist) print "shuffled" for theo in obslist: # choose a real source to draw an observation from o = obs(initial_pos=[theo[0],theo[1]],assumed_err=[theo[2],theo[3]]) o.pos = [theo[0],theo[1]] o.plot() tmp = o.is_associated_with_source(constructed_source_list) # print tmp if tmp['answer'] == True: # o.associate_with_source(tmp['sources']) for s in tmp['best_source']: # print (len(tmp['best_source']),o.pos,s.current_pos) s.add_associated_obs(copy.deepcopy(o)) s.recalculate_position() else: ## make a new source s = source(start_pos=copy.copy(o.pos),stype='constructed',start_err=copy.copy(o.assumed_err),current_pos=copy.copy(o.pos),\ current_err=copy.copy(o.assumed_err),associated_obs=[copy.deepcopy(o)]) print "1 new source" #print s #print s.associated_obs constructed_source_list.append(copy.deepcopy(s)) observation_list.append(copy.deepcopy(o)) for s in constructed_source_list: # print s s.plot('g^') self.constructed_source_list = constructed_source_list self.constructed_pos = (numarray.fromlist([x.current_pos for x in self.constructed_source_list]))
def Matrix(data, typecode=None, copy=1, savespace=0): if type(data) is types.StringType: data = _convert_from_string(data) a = numarray.fromlist(data, type=typecode) a.__class__ = _Matrix return a
def run(self,n_sources = 3, n_observations = 21, ra_range = [-20.0,20.0],dec_range=[-20.0,20.0],typical_err=0.3,reuse=True): global real_list clf() # make the real sources if reuse: try: type(real_list) == type([]) except NameError: reuse = False real_list = [] for ns in range(n_sources): if not reuse: real_list.append(source(start_pos=[random_array.uniform(ra_range[0],ra_range[1]),random_array.uniform(dec_range[0],dec_range[1])], stype='real',start_err=[0.0,0.0],associated_obs=[])) # print real_list[-1] real_list[ns].plot() constructed_source_list = [] observation_list = [] ## pick a vector of len n_obsevations sources to choose from from 0 --> n_source - 1 s_start_ind = random_array.randint(0,n_sources,shape=[n_observations]) for i in range(n_observations): # choose a real source to draw an observation from o = obs(initial_pos=real_list[s_start_ind[i]].start_pos,true_err=[[typical_err**2,0.0],[0.0,typical_err**2]],assumed_err=[1.1*typical_err,1.1*typical_err]) o.observe_pos() o.plot() tmp = o.is_associated_with_source(constructed_source_list) # print tmp if tmp['answer'] == True: # o.associate_with_source(tmp['sources']) for s in tmp['best_source']: # print (len(tmp['best_source']),o.pos,s.current_pos) s.add_associated_obs(copy.deepcopy(o)) s.recalculate_position() else: ## make a new source s = source(start_pos=copy.copy(o.pos),stype='constructed',start_err=copy.copy(o.assumed_err),current_pos=copy.copy(o.pos),\ current_err=copy.copy(o.assumed_err),associated_obs=[copy.deepcopy(o)]) print "new source" #print s #print s.associated_obs constructed_source_list.append(copy.deepcopy(s)) observation_list.append(copy.deepcopy(o)) for s in constructed_source_list: # print s s.plot('g^') ## do the comparisons between real and constructed sources for ns in range(n_sources): #real_list[ns].plot('ys') pass self.real_list = real_list self.constructed_source_list = constructed_source_list self.real_pos = (numarray.fromlist([x.start_pos for x in self.real_list])) self.constructed_pos = (numarray.fromlist([x.current_pos for x in self.constructed_source_list]))
def run(self, n_sources=3, n_observations=21, ra_range=[-20.0, 20.0], dec_range=[-20.0, 20.0], typical_err=0.3, reuse=True): global real_list clf() # make the real sources if reuse: try: type(real_list) == type([]) except NameError: reuse = False real_list = [] for ns in range(n_sources): if not reuse: real_list.append( source(start_pos=[ random_array.uniform(ra_range[0], ra_range[1]), random_array.uniform(dec_range[0], dec_range[1]) ], stype='real', start_err=[0.0, 0.0], associated_obs=[])) # print real_list[-1] real_list[ns].plot() constructed_source_list = [] observation_list = [] ## pick a vector of len n_obsevations sources to choose from from 0 --> n_source - 1 s_start_ind = random_array.randint(0, n_sources, shape=[n_observations]) for i in range(n_observations): # choose a real source to draw an observation from o = obs(initial_pos=real_list[s_start_ind[i]].start_pos, true_err=[[typical_err**2, 0.0], [0.0, typical_err**2]], assumed_err=[1.1 * typical_err, 1.1 * typical_err]) o.observe_pos() o.plot() tmp = o.is_associated_with_source(constructed_source_list) # print tmp if tmp['answer'] == True: # o.associate_with_source(tmp['sources']) for s in tmp['best_source']: # print (len(tmp['best_source']),o.pos,s.current_pos) s.add_associated_obs(copy.deepcopy(o)) s.recalculate_position() else: ## make a new source s = source(start_pos=copy.copy(o.pos),stype='constructed',start_err=copy.copy(o.assumed_err),current_pos=copy.copy(o.pos),\ current_err=copy.copy(o.assumed_err),associated_obs=[copy.deepcopy(o)]) print "new source" #print s #print s.associated_obs constructed_source_list.append(copy.deepcopy(s)) observation_list.append(copy.deepcopy(o)) for s in constructed_source_list: # print s s.plot('g^') ## do the comparisons between real and constructed sources for ns in range(n_sources): #real_list[ns].plot('ys') pass self.real_list = real_list self.constructed_source_list = constructed_source_list self.real_pos = (numarray.fromlist( [x.start_pos for x in self.real_list])) self.constructed_pos = (numarray.fromlist( [x.current_pos for x in self.constructed_source_list]))