Before importing, make sure to run
source setup.bash
in the root directory, and that you are using the python3 kernel.
Import everything with:
In [1]:
from kbmodpy import kbmod as kb
import numpy
path = "../data/demo/"
In [2]:
p = kb.psf(1.0)
In [3]:
p.get_array()
There are several methods that get information about its properties
In [4]:
p.get_dim() # dimension of kernel width and height
p.get_radius() # distance from center of kernel to edge
p.get_size() # total number of pixels in the kernel
p.get_sum() # total sum of all pixels in the kernel, should be close to 1.0
Out[4]:
The entire kernel can be printed out (note: prints to the console, not the notebook)
In [5]:
p.print_psf()
In [2]:
im = kb.layered_image("image2", 100, 100, 5.0, 25.0, 0.0)
# name, width, height, background_noise_sigma, variance, capture_time
B. Load a file
In [7]:
im = kb.layered_image(path+"CORR40535777.fits")
Artificial objects can easily be added into a layered_image
In [8]:
im.add_object(20.0, 35.0, 2500.0, p)
# x, y, flux, psf
The image pixels can be retrieved as a 2D numpy array
In [9]:
pixels = im.science()
pixels
Out[9]:
The image can mask itself by providing a bitmask of flags (note: masked pixels are set to -9999.9 so they can be distinguished later from 0.0 pixles)
In [3]:
flags = ~0
flag_exceptions = [32,39]
# mask all of pixels with flags except those with specifed combiniations
im.apply_mask_flags( flags, flag_exceptions )
The image can be convolved with a psf kernel
In [26]:
im.convolve(p)
# note: This function is called interally by stack_search and doesn't need to be
# used directy. It is only exposed because it happens to be a fast
# implementation of a generally useful function
The image at any point can be saved to a file
In [27]:
im.save_layers(path+"/out/") # file will use original name
Get properties
In [28]:
im.get_width()
im.get_height()
im.get_time()
im.get_ppi() # pixels per image, width*height
Out[28]:
In [13]:
count = 10
imlist = [ kb.layered_image("img"+str(n), 50, 50, 10.0, 5.0, n/count) for n in range(count) ]
stack = kb.image_stack( imlist )
# this creates a stack with 10 50x50 images, and times ranging from 0 to 1
A shortcut is provided to initialize a stack automatically from a list of files
In [30]:
import os
files = os.listdir(path)
files = [path+f for f in files if '.fits' in f]
files.sort()
files
Out[30]:
In [31]:
stack = kb.image_stack(files)
A master mask can be generated and applied to the stack
In [32]:
stack.apply_master_mask( int('100111', 2), 2 ) # flags, threshold
stack.save_master_mask("mask.fits")
Manually set the times the images in the stack were taken
In [15]:
stack.set_times( [0,2,3,4.5,5,6,7,10,11,14] )
Most features of the layered_image can be used on the whole stack
In [16]:
stack.apply_mask_flags(flags, flag_exceptions)
stack.convolve(p)
stack.get_width()
stack.get_height()
stack.get_ppi()
stack.get_images() # retrieves list of layered_images back from the stack
stack.get_times()
Out[16]:
In [11]:
search = kb.stack_search( stack, p )
To save psi and images, a directory with "psi" and "phi" folders must be specified.
In [12]:
search.save_psi_phi("/home/kbmod-usr/cuda-workspace/kbmod/search/output")
Launch a search
In [69]:
search.gpu(10, 10, 0.0, 1.0, 20.0, 50.0)
# angle_steps, velocity_steps, min_angle, max_angle, min_velocity, max_velocity
Save the results to a files
note: format is {x, y, xv, yv, likelihood, flux}
In [55]:
search.save_results(path+"results.txt", 0.05)
# path, fraction of total results to save in file
Trajectories can be retrieved directly from search without writing and reading to file.
However, this is not recommended for a large number of trajectories, as it is not returned as a numpy array, but as a list of the trajectory objects described below
In [51]:
top_results = search.get_results(0, 100)
# start, count
In [52]:
best = top_results[0]
In [57]:
# these numbers are wild because mask flags and search parameters above were chosen randomly
best.flux
best.lh
best.x
best.y
best.x_v
best.y_v
Out[57]:
tests/test_search.py shows a simple example of how to generate a set of images, add an artificial source, and recover it with search
In [ ]: