def avg_region(self, la_lim, lo_lim, nla=100, nlo=100, stellar_grid=False, reflection=False, temp_map=True): las = np.linspace(la_lim[0], la_lim[1], nla) los = np.linspace(lo_lim[0], lo_lim[1], nlo) for i in range(0, len(los)): if los[i] > np.pi: los[i] -= 2 * np.pi if los[i] < -np.pi: los[i] += 2 * np.pi tot_flux = 0.0 tot_weight = 0.0 for la in las: weight = np.cos(la) for lo in los: flux = sp.call_map_model(self, la, lo) if temp_map == False: tot_flux += weight * flux[0] else: tot_flux += weight * flux[1] tot_weight += weight avg = tot_flux / tot_weight return avg
def find_temp(self, p0, sign, stellar_grid=False, reflection=False, temp_map=True): out = minimize(self.find_func, p0, method='L-BFGS-B', bounds=((-np.pi / 2, np.pi / 2), (None, None)), args=(sign, temp_map)) p = out['x'] if p[1] > np.pi: revs = math.ceil(abs(p[1] / (2 * np.pi))) p[1] -= revs * 2 * np.pi if p[1] < -np.pi: revs = (math.ceil(abs(p[1] / (2 * np.pi)))) - 1 p[1] += revs * 2 * np.pi flux = sp.call_map_model(self, p[0], p[1]) if temp_map == True: f = flux[1] else: f = flux[0] return (f, out['x'] * 180 / np.pi)
def avg_region(self,la_lim,lo_lim,nla=100,nlo=100,stellar_grid=False,reflection=False,temp_map=True,pweight=1): las = np.linspace(la_lim[0],la_lim[1],nla) los = np.linspace(lo_lim[0],lo_lim[1],nlo) for i in range(0,len(los)): if los[i] > np.pi: los[i] -= 2*np.pi if los[i] < -np.pi: los[i] += 2*np.pi tot_flux = 0.0 tot_weight = 0.0 for la in las: weight = np.cos(la) for lo in los: flux = sp.call_map_model(self,la,lo) if temp_map == False: tot_flux += weight*flux[0] else: tot_flux += weight*(flux[1]**pweight) tot_weight += weight avg = tot_flux/tot_weight avg = avg**(1.0/pweight) return avg
def find_func(self, x, sign, temp_map): if x[1] > np.pi: revs = math.ceil(abs(x[1] / (2 * np.pi))) x[1] -= revs * 2 * np.pi if x[1] < -np.pi: revs = (math.ceil(abs(x[1] / (2 * np.pi)))) - 1 x[1] += revs * 2 * np.pi flux = sp.call_map_model(self, x[0], x[1]) if temp_map == True: return sign * flux[1] else: return sign * flux[0]
def find_func(self,x,sign,temp_map): if x[1] > np.pi: revs = math.ceil(abs(x[1]/(2*np.pi))) x[1] -= revs*2*np.pi if x[1] < -np.pi: revs = (math.ceil(abs(x[1]/(2*np.pi))))-1 x[1] += revs*2*np.pi flux = sp.call_map_model(self,x[0],x[1]) if temp_map == True: return sign*flux[1] else: return sign*flux[0]
def find_temp(self,p0,sign,stellar_grid=False,reflection=False,temp_map=True): out = minimize(self.find_func,p0,method='L-BFGS-B',bounds=((-np.pi/2,np.pi/2),(None,None)),args=(sign,temp_map)) p = out['x'] if p[1] > np.pi: revs = math.ceil(abs(p[1]/(2*np.pi))) p[1] -= revs*2*np.pi if p[1] < -np.pi: revs = (math.ceil(abs(p[1]/(2*np.pi))))-1 p[1] += revs*2*np.pi flux = sp.call_map_model(self,p[0],p[1]) if temp_map == True: f = flux[1] else: f = flux[0] return(f,out['x']*180/np.pi)
def plot_square(spider_params, ax=False, min_temp=False, max_temp=False, temp_map=False, min_bright=0.2, scale_planet=1.0, planet_cen=[0.0, 0.0], mycmap=plt.get_cmap('inferno'), show_cax=True, theme='white', show_axes=True, nla=100, nlo=100): if theme == 'black': bg = 'black' tc = ("#04d9ff") else: bg = 'white' tc = 'black' if ax == False: f, ax = plt.subplots(facecolor=bg) new_ax = True else: new_ax = False las = np.linspace(-np.pi / 2, np.pi / 2, nla) los = np.linspace(-np.pi, np.pi, nlo) plt.ylim(-90, 90) plt.xlim(-180, 180) fluxes = [] for la in las: row = [] for lo in los: flux = sp.call_map_model(spider_params, la, lo) if temp_map == False: row += [flux[0]] else: row += [flux[1]] fluxes += [row] fluxes = np.array(fluxes) if temp_map == False: fluxes = fluxes / np.max(fluxes) lala, lolo = np.meshgrid(los, las) plt.plot([0], [0], 'x', color=('#0cff0c'), ms=10, mew=2) ax.set_xlabel('longitude', color=tc) ax.set_ylabel('latitude', color=tc) ax.set_axis_bgcolor(bg) if show_axes == False: ax.spines['bottom'].set_color(bg) ax.spines['left'].set_color(bg) ax.spines['top'].set_color(bg) ax.spines['right'].set_color(bg) else: ax.spines['bottom'].set_color(tc) ax.spines['left'].set_color(tc) ax.spines['top'].set_color(tc) ax.spines['right'].set_color(tc) if show_axes == False: ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) else: ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(True) ax.tick_params(colors=tc) if show_cax == True: cax = plt.pcolor(lala * 180 / np.pi, lolo * 180 / np.pi, fluxes, cmap=mycmap) # cbar = plt.colorbar(mycax, cax=cax,ticks=[1100,1300,1500,1700,1900]) cbar = plt.colorbar() cbar.ax.tick_params(colors=tc) if temp_map == True: cbar.set_label('T (K)', color=tc) # horizontal colorbar else: cbar.set_label('Relative brightness', color=tc) # horizontal colorbar return ax if temp_map == True: b_i = 17 else: b_i = 16 if min_temp == False: temps = planet[:, b_i] min_temp = np.min(temps) max_temp = np.max(temps) dp = ((max_temp - min_temp) * min_bright) for j in range(0, len(planet)): val = (dp + planet[j][b_i] - min_temp) / (dp + max_temp - min_temp) c = mycmap(val) n = planet[j] r1 = n[13] * scale_planet r2 = n[14] * scale_planet radii = [r1, r2] thetas = np.linspace(n[10], n[11], 100) xs = np.outer(radii, np.cos(thetas)) + planet_cen[0] ys = np.outer(radii, np.sin(thetas)) + planet_cen[1] xs[1, :] = xs[1, ::-1] ys[1, :] = ys[1, ::-1] ax.fill(np.ravel(xs), np.ravel(ys), edgecolor=c, color=c, zorder=2) divider = make_axes_locatable(ax) # Append axes to the right of ax, with 20% width of ax # zero_temp = min_temp - dp zero_temp = min_temp data = [np.linspace(zero_temp, max_temp, 1000)] * 2 fake, fake_ax = plt.subplots() mycax = fake_ax.imshow(data, interpolation='none', cmap=mycmap) plt.close(fake) return ax
def retrieve_map_full_samples(degree=3,dataDir="data/sph_harmonic_coefficients_full_samples/hotspot/",isspider=True): tmp = np.load("{}spherearray_deg_{}.npz".format(dataDir,degree)) outDictionary = tmp['arr_0'].tolist() londim = 100 latdim = 100 samples = outDictionary['spherical coefficients'] # output from eigencurves waves = outDictionary['wavelength (um)'] bestsamples=outDictionary['best fit coefficients'] # best fit sample from eigencurves randomIndices = np.random.randint(0,len(samples),39) nRandom = len(randomIndices) fullMapArray = np.zeros([nRandom,len(waves),londim,latdim]) bestMapArray = np.zeros([len(waves),londim,latdim]) #SPIDERMAN stuff added by Megan if isspider: params0=sp.ModelParams(brightness_model='spherical') params0.nlayers=20 params0.t0=-2.21857/2. params0.per=2.21857567 params0.a_abs=0.0313 params0.inc=85.71 params0.ecc=0.0 params0.w=90. params0.rp=0.155313 params0.a=8.863 params0.p_u1=0. params0.p_u2=0. params0.degree=degree params0.la0=0. params0.lo0=0. if not isspider: inputArr=np.zeros([len(waves),bestsamples.shape[0]+1]) inputArr[:,0] = waves inputArr[:,1:] = bestsamples.transpose() wavelengths, lats, lons, maps = eigenmaps.generate_maps(inputArr,N_lon=londim, N_lat=latdim) bestMapArray=maps else: for i in np.arange(np.shape(waves)[0]): params0.sph=list(bestsamples[:,i]) nla=latdim nlo=londim las = np.linspace(-np.pi/2,np.pi/2,nla) los = np.linspace(-np.pi,np.pi,nlo) fluxes = [] for la in las: row = [] for lo in los: flux = sp.call_map_model(params0,la,lo) row += [flux[0]] fluxes += [row] fluxes = np.array(fluxes) lons, lats = np.meshgrid(los,las) bestMapArray[i,:,:] = fluxes for drawInd, draw in enumerate(samples[randomIndices]): if not isspider: inputArr = np.zeros([len(waves),samples.shape[1]+1]) inputArr[:,0] = waves inputArr[:,1:] = draw.transpose() wavelengths, lats, lons, maps = eigenmaps.generate_maps(inputArr, N_lon=londim, N_lat=latdim) fullMapArray[drawInd,:,:,:] = maps #MEGAN ADDED STUFF else: for i in np.arange(np.shape(waves)[0]): params0.sph=list(samples[drawInd,:,i]) nla=latdim nlo=londim las = np.linspace(-np.pi/2,np.pi/2,nla) los = np.linspace(-np.pi,np.pi,nlo) fluxes = [] for la in las: row = [] for lo in los: flux = sp.call_map_model(params0,la,lo) row += [flux[0]] fluxes += [row] fluxes = np.array(fluxes) lons, lats = np.meshgrid(los,las) #print(np.min(lats),np.min(lons),np.min(las),np.min(los)) #lats, lons, maps = testgenmaps.spmap(inputArr,londim, latdim) fullMapArray[drawInd,i,:,:] = fluxes ## note that the maps have the origin at the top ## so we have to flip the latitude array lats = np.flip(lats,axis=0) return fullMapArray, bestMapArray, lats, lons, waves
def find_groups(dataDir,ngroups=4,degree=2, londim=100, latdim=100, trySamples=45,extent=0.5,sortMethod='avg',isspider=True): """ Find the eigenspectra using k means clustering Parameters ---------- ngroups: int Number of eigenspectra to group results into degree: int Spherical harmonic degree to draw samples from testNum: int Test number (ie. lightcurve number 1,2, etc.) trySamples: int Number of samples to find groups with All samples take a long time so this takes a random subset of samples from which to draw posteriors sortMethod: str Method to sort the groups returned by K means clustering None, will not sort the output 'avg' will sort be the average of the spectrum 'middle' will sort by the flux in the middle of the spectrum extent: time covered by the eclipse/phase curve. Sets what portion of the map is used for clustering (e.g. full planet or dayside only) """ #samplesDir = "data/sph_harmonic_coefficients_full_samples" #dataDir = "{}/eclipse_lightcurve_test{}/".format(samplesDir,testNum) tmp = np.load("{}spherearray_deg_{}.npz".format(dataDir,degree)) outDictionary = tmp['arr_0'].tolist() samples = outDictionary['spherical coefficients'] # output from eigencurves if trySamples>len(samples): assert(trySamples<=len(samples)),("trySamples must be less than the total number of MCMC samples, "+str(len(samples))) eigenspectra_draws = [] kgroup_draws = [] uber_eigenlist=[[[[] for i in range(10)] for i in range(ngroups)] for i in range(trySamples)] if isspider: params0=sp.ModelParams(brightness_model='spherical') #megan added stuff params0.nlayers=20 params0.t0=-2.21857/2. params0.per=2.21857567 params0.a_abs=0.0313 params0.inc=85.71 params0.ecc=0.0 params0.w=90. params0.rp=0.155313 params0.a=8.863 params0.p_u1=0. params0.p_u2=0. params0.degree=degree params0.la0=0. params0.lo0=0. waves=np.array([2.41,2.59,2.77,2.95,3.13,3.31,3.49,3.67,3.85,4.03]) minlon=np.around(extent/2.*londim) #print(minlon) randomIndices = np.random.randint(0,len(samples),trySamples) for drawInd,draw in enumerate(samples[randomIndices]): ## Re-formatting here into a legacy system ## 1st dimension is wavelength ## 2nd dimensions is data (0th element = wavelength) ## (1: elements are spherical harmonic coefficients) if not isspider: inputArr = np.zeros([10,samples.shape[1]+1]) inputArr[:,0] = np.array([2.41,2.59,2.77,2.95,3.13,3.31,3.49,3.67,3.85,4.03]) inputArr[:,1:] = draw.transpose() waves, lats, lons, maps = eigenmaps.generate_maps(inputArr, N_lon=londim, N_lat=latdim) maps=maps[:,:,int(londim/2.-minlon):int(londim/2.+minlon)] #maps=np.zeros((np.shape(waves)[0],londim,latdim)) #full map else: maps=np.zeros((np.shape(waves)[0],latdim,int(minlon*2))) #only dayside #print(np.shape(maps)) for i in np.arange(np.shape(waves)[0]): params0.sph=list(samples[drawInd,:,i]) nla=latdim nlo=londim las = np.linspace(-np.pi/2,np.pi/2,nla) los = np.linspace(-np.pi,np.pi,nlo) fluxes = [] for la in las: row = [] for lo in los: flux = sp.call_map_model(params0,la,lo) row += [flux[0]] fluxes += [row] fluxes = np.array(fluxes) lons, lats = np.meshgrid(los,las) #print(np.min(lats),np.min(lons),np.min(las),np.min(los)) #lats, lons, maps = testgenmaps.spmap(inputArr,londim, latdim) #pdb.set_trace() maps[i,:,:] = fluxes[:,int(londim/2.-minlon):int(londim/2.+minlon)] kgroups = kmeans.kmeans(maps, ngroups) eigenspectra,eigenlist = bin_eigenspectra.bin_eigenspectra(maps, kgroups) eigenspectra_draws.append(eigenspectra) kgroup_draws.append(kgroups) for groupind in range(ngroups): for waveind in range(10): uber_eigenlist[drawInd][groupind][waveind]=eigenlist[groupind][waveind,:] if sortMethod is not None: eigenspectra_draws_final, kgroup_draws_final,uber_eigenlist_final = kmeans.sort_draws(eigenspectra_draws, kgroup_draws,uber_eigenlist, method=sortMethod) else: eigenspectra_draws_final, kgroup_draws_final,uber_eigenlist_final = eigenspectra_draws, kgroup_draws,uber_eigenlist return eigenspectra_draws_final, kgroup_draws_final,uber_eigenlist_final, maps
def plot_square(spider_params,ax=False,min_temp=False,max_temp=False,temp_map=False,min_bright=0.2,scale_planet=1.0,planet_cen=[0.0,0.0],mycmap=plt.get_cmap('inferno'),show_cax=True,theme='white',show_axes=True,nla=100,nlo=100): if theme == 'black': bg = 'black' tc = ("#04d9ff") else: bg = 'white' tc = 'black' if ax == False: f, ax = plt.subplots(facecolor=bg) new_ax = True else: new_ax = False las = np.linspace(-np.pi/2,np.pi/2,nla) los = np.linspace(-np.pi,np.pi,nlo) plt.ylim(-90,90) plt.xlim(-180,180) fluxes = [] for la in las: row = [] for lo in los: flux = sp.call_map_model(spider_params,la,lo) if temp_map == False: row += [flux[0]] else: row += [flux[1]] fluxes += [row] fluxes = np.array(fluxes) if temp_map == False: fluxes = fluxes/np.max(fluxes) lala, lolo = np.meshgrid(los,las) plt.plot([0],[0],'x',color=('#0cff0c'),ms=10,mew=2) ax.set_xlabel('longitude',color=tc) ax.set_ylabel('latitude',color=tc) ax.set_axis_bgcolor(bg) if show_axes == False: ax.spines['bottom'].set_color(bg) ax.spines['left'].set_color(bg) ax.spines['top'].set_color(bg) ax.spines['right'].set_color(bg) else: ax.spines['bottom'].set_color(tc) ax.spines['left'].set_color(tc) ax.spines['top'].set_color(tc) ax.spines['right'].set_color(tc) if show_axes == False: ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) else: ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(True) ax.tick_params(colors=tc) if show_cax == True: cax = plt.pcolor(lala*180/np.pi,lolo*180/np.pi,fluxes,cmap=mycmap) # cbar = plt.colorbar(mycax, cax=cax,ticks=[1100,1300,1500,1700,1900]) cbar = plt.colorbar() cbar.ax.tick_params(colors=tc) if temp_map == True: cbar.set_label('T (K)',color=tc) # horizontal colorbar else: cbar.set_label('Relative brightness',color=tc) # horizontal colorbar return ax if temp_map == True: b_i = 17 else: b_i = 16 if min_temp == False: temps = planet[:,b_i] min_temp = np.min(temps) max_temp = np.max(temps) dp = ((max_temp-min_temp)*min_bright) for j in range (0,len(planet)): val = (dp + planet[j][b_i]-min_temp)/(dp + max_temp-min_temp) c = mycmap(val) n = planet[j] r1 = n[13]*scale_planet r2 = n[14]*scale_planet radii = [r1,r2] thetas = np.linspace(n[10],n[11],100) xs = np.outer(radii, np.cos(thetas)) + planet_cen[0] ys = np.outer(radii, np.sin(thetas)) + planet_cen[1] xs[1,:] = xs[1,::-1] ys[1,:] = ys[1,::-1] ax.fill(np.ravel(xs), np.ravel(ys), edgecolor=c,color=c,zorder=2) divider = make_axes_locatable(ax) # Append axes to the right of ax, with 20% width of ax # zero_temp = min_temp - dp zero_temp = min_temp data = [np.linspace(zero_temp,max_temp,1000)]*2 fake, fake_ax = plt.subplots() mycax = fake_ax.imshow(data, interpolation='none', cmap=mycmap) plt.close(fake) return ax