Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jurjen93 committed Apr 22, 2021
1 parent 804121d commit b7153d3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 49 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/images
#ignore fits files
*.fits
/fits
#ignore environments
/venv
#notebooks
Expand All @@ -23,4 +24,5 @@ __pycache__
#csv files
*.csv
#files not for github
make_movie_highres.py
highres_*
leahfiles/
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ Run:\
where you can use the following flags
* ```-d``` -> Choose to download a specific fits file from the internet. Use ```1``` if you want to, leave empty otherwise.
* ```-csv``` -> Give a specific csv file with sources to include as cutouts in the poster. If you leave it empty, it goes through the whole field.
* ```-N``` -> Number of sources to use
* ```-fr``` -> Frame rate of the video. Advice is to use ```60``` to make the video smooth.
* ```-fi``` -> Fits file to use. (If you don't download your fits file)

Example:\
```python make_video.py -csv catalogue/catalogue_lockman.csv -d 1 -N 2 -f 60```\
```python make_video.py -csv catalogue/catalogue_lockman.csv -d 1 -f 60```\
See also the following blog for more information:
https://towardsdatascience.com/how-to-make-a-video-from-your-astronomy-images-957f1d40dea1

Expand Down
22 changes: 8 additions & 14 deletions make_movie.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import os
import warnings
from video.scripts.moviemaker import MovieMaker
from astropy.utils.data import get_pkg_data_filename
from timeit import default_timer as timer
import pandas as pd
import argparse
from numpy import sqrt
warnings.filterwarnings("ignore")

parser = argparse.ArgumentParser("Make movie from fits file.")
parser.add_argument('-d', '--downloading', type=int, help='Download your own data')
parser.add_argument('-csv', '--csvfile', help='Csv file with outliers with RA and DEC in degrees')
parser.add_argument('-fr', '--framerate', help='Frame rate of your video')
parser.add_argument('-fi', '--fits', type=str, help='Fits file to use')
parser.add_argument('-N', '--sourcenum', type=int, help='Number of sources from catalogue')
args = parser.parse_args()

def distance(obj_1, obj_2):
Expand All @@ -28,9 +25,6 @@ def isNaN(a):
if args.framerate: FRAMERATE = args.framerate
else: FRAMERATE = 60

if args.sourcenum: N = args.sourcenum
else: N = 2

if args.downloading == 1:
download = input('Paste here your url where to find the fits file: ')
fits_download = True
Expand All @@ -42,20 +36,20 @@ def isNaN(a):
if args.fits:
file = args.fits
else:
file = 'fits/lockman_hole.fits'
file = 'fits/elias.fits'
Movie = MovieMaker(fits_file=get_pkg_data_filename(file),
imsize=0.4,#defaul imsize
framerate=FRAMERATE, zoom_effect=False)

if args.csvfile:#go through all objects in csv file
df = pd.read_csv(args.csvfile)[['RA', 'DEC']]
df = pd.read_csv(args.csvfile)[['RA', 'DEC', 'imsize']]

start_coord = Movie.wcs.pixel_to_world(Movie.image_data.shape[0]/2, Movie.image_data.shape[0]/2)
start_dec = start_coord.dec.degree
start_ra = start_coord.ra.degree

Movie.zoom_in(N_frames=800, first_time=True)
for n in range(len(df)):#stack multiple sources
for n in range(len(df)-1):#stack multiple sources
if n > 0:
dist = distance([last_RA, last_DEC], [df['RA'].values[n], df['DEC'].values[n]])
move_to_frames = int(250*dist)
Expand All @@ -66,13 +60,13 @@ def isNaN(a):
Movie.move_to(N_frames=move_to_frames, ra=df['RA'].values[n], dec=df['DEC'].values[n])
Movie.zoom_in(N_frames=300, imsize_out=df['imsize'].values[n])
if n<len(df)-1 and df['imsize'].values[n+1]>df['imsize'].values[n]:
im_out = max(df['imsize'].values[n+1]+0.1, 0.3)
im_out = max(df['imsize'].values[n+1]+0.3, 0.3)
else:
im_out = max(df['imsize'].values[n] + 0.1, 0.3)
im_out = max(df['imsize'].values[n] + 0.3, 0.3)
Movie.zoom_out(N_frames=300, imsize_out=im_out)
last_RA, last_DEC = df['RA'].values[n], df['DEC'].values[n]
print(f"Number of frames {int(400*distance([start_ra, start_dec], [df['RA'].values[N-1], df['DEC'].values[N-1]]))}")
Movie.move_to(N_frames=int(400*distance([start_ra, start_dec], [df['RA'].values[N-1], df['DEC'].values[N-1]])), ra=start_ra, dec=start_dec)
print(f"Number of frames {int(400*distance([start_ra, start_dec], [df['RA'].values[-1], df['DEC'].values[-1]]))}")
Movie.move_to(N_frames=int(400*distance([start_ra, start_dec], [df['RA'].values[-1], df['DEC'].values[-1]])), ra=start_ra, dec=start_dec)
Movie.zoom_out(N_frames=800, imsize_out=2)
Movie.record()

Expand Down Expand Up @@ -117,7 +111,7 @@ def isNaN(a):
for position in positions
if not isNaN(Movie.image_data[position[0], position[1]])]]

Movie.imsize = 2*abs(fits_header['CDELT1']*fits_header['CRPIX1']/7)
Movie.imsize = 2*abs(fits_header['CDELT1']*fits_header['CRPIX1']/number_of_steps)
Movie.zoom_in(N_frames=600, first_time=True)
start_coord = Movie.wcs.pixel_to_world(Movie.image_data.shape[0] / 2, Movie.image_data.shape[0] / 2)
start_dec = start_coord.dec.degree
Expand Down
13 changes: 10 additions & 3 deletions poster/scripts/imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, fits_file=None, fits_download: bool=False, vmin: float = None
else:
self.vmin = vmin
if vmax is None:
self.vmax = np.nanstd(self.image_data)*12
self.vmax = np.nanstd(self.image_data)*20
else:
self.vmax = vmax
self.image_directory = image_directory
Expand All @@ -62,11 +62,14 @@ def __init__(self, fits_file=None, fits_download: bool=False, vmin: float = None
#Transfer function
if 'cutout' in self.fits_file:
self.image_data = gaussian_filter(self.tonemap(image_data=self.image_data, b=0.25, threshold=self.vmin), sigma=3)
elif 'ILTJ' in self.fits_file:
self.image_data = gaussian_filter(self.tonemap(image_data=self.image_data, b=0.25, threshold=self.vmin/1.5), sigma=4)
else:
if self.zoom_effect:
self.image_data = gaussian_filter(self.tonemap(image_data=self.image_data, b=0.5, threshold=0), sigma=2)
else:
self.image_data = gaussian_filter(self.tonemap(image_data=self.image_data, b=0.25, threshold=self.vmin / 100), sigma=1)
self.image_data = gaussian_filter(self.tonemap(image_data=self.image_data, b=0.25, threshold=self.vmin/100), sigma=1)


def tonemap(self, image_data=None, b: float = 0.25, threshold: float = None):
"""
Expand Down Expand Up @@ -126,7 +129,10 @@ def imaging(self, image_data=None, wcs=None, image_name: str = 'Nameless', dpi:
vmax = self.vmax
vmin = min((2/max(imsize,0.2))*(self.vmin/100), self.vmax/20)
if imsize<1:
vmax/=max(imsize,0.2)
vmax/=max(imsize, 0.2)
if imsize<0.05:
vmin+=imsize/20
image_data=gaussian_filter(image_data, sigma=min(10, int(imsize/0.05)))
plt.imshow(image_data,
norm=SymLogNorm(linthresh=vmin*20, vmin=self.vmin/1.4, vmax=vmax), origin='lower', cmap=cmap)
else:
Expand Down Expand Up @@ -206,6 +212,7 @@ def image_cutout(self, pos: tuple = None, size: tuple = (1000, 1000), dpi: float
:param save: save image (yes or no)
:param cmap: cmap of your image
:param text: text in the left down corner of your image
:param imsize: image size in degrees
"""
ra, dec = pos
pix_x, pix_y = self.to_pixel(ra, dec)
Expand Down
21 changes: 0 additions & 21 deletions produce_all.sh

This file was deleted.

50 changes: 42 additions & 8 deletions video/scripts/moviemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import warnings
from poster.scripts.imaging import ImagingLofar
from tqdm import tqdm
import cv2 as cv
from glob import glob
warnings.filterwarnings("ignore")

__all__ = ['MovieMaker']
Expand Down Expand Up @@ -63,8 +65,7 @@ def __getstate__(self):

def __setstate__(self, state):
""" This is called while unpickling. """
if self.process == 'multiprocess':
self.__dict__.update(state)
self.__dict__.update(state)

def make_frame(self, N, ra = None, dec = None, imsize: float = None, dpi: float = 300):
"""
Expand All @@ -77,7 +78,7 @@ def make_frame(self, N, ra = None, dec = None, imsize: float = None, dpi: float
:param dpi: dots per inch (pixel density)
"""
self.image_cutout(pos=(ra, dec),
size=(np.int(imsize/np.max(self.wcs.pixel_scale_matrix)*0.9), np.int(imsize/np.max(self.wcs.pixel_scale_matrix)*1.6)),
size=(np.int(imsize/np.max(self.wcs.pixel_scale_matrix)), np.int(imsize/np.max(self.wcs.pixel_scale_matrix)*1.77)),
dpi=dpi,
image_name=f'image_{str(N).rjust(5, "0")}.png',
cmap='CMRmap',
Expand All @@ -100,9 +101,10 @@ def make_frames(self):
with ThreadPool(8) as p:
p.starmap(self.make_frame, inputs)
elif self.process == 'multiprocess':
print(f"You are using {cpu_count()} cpu's for multiprocessing")
with Pool(cpu_count()) as p:
p.starmap(self.make_frame, inputs)
cpus=2
print(f"You are using {cpus} cpu's for multiprocessing")
with Pool(cpus) as p:
p.starmap(self.make_frame, inputs, chunksize=2)
else:
for inp in tqdm(inputs):
self.make_frame(inp[0], inp[1], inp[2], inp[3], inp[4])
Expand Down Expand Up @@ -153,7 +155,7 @@ def zoom_in(self, N_frames: int = None, first_time: bool=False, imsize_out: floa
self.ra = coordinates.ra.degree
else:
begin_size = self.imsize
end_size = max(imsize_out, 0.03)
end_size = imsize_out
self.imsizes = np.linspace(end_size, begin_size, N_frames)[::-1]
self.imsize = self.imsizes[-1]
self.ragrid = np.array([self.ra]*len(self.imsizes))
Expand All @@ -169,7 +171,7 @@ def zoom_out(self, N_frames: int = None, imsize_out: float = None):
:param imsize_out: Output image size.
"""
self.imsizes = np.linspace(self.imsize, imsize_out, N_frames)
self.imsize = self.imsizes[-1]
self.imsize = imsize_out
self.ragrid = np.array([self.ra]*len(self.imsizes))
self.decgrid = np.array([self.dec]*len(self.imsizes))
self.make_frames()
Expand All @@ -190,5 +192,37 @@ def record(self, audio: str = None):
print('Audio file does not exist')
return self

def crop_center(img,cropx,cropy):
y,x,_ = img.shape
startx = x//2-(cropx//2)
starty = y//2-(cropy//2)
return img[starty:starty+cropy,startx:startx+cropx,:]

def fading_effect(source, frames):
images = glob('/home/jurjen/Documents/Python/advanced_astro_visualiziation/video/frames_high/*')
img1 = cv.imread(sorted(images)[-1])
img1 = cv.GaussianBlur(img1, (5,5), 0)
img2 = cv.imread(glob(f'/home/jurjen/Documents/Python/advanced_astro_visualiziation/fits/highres_P205/{source}*MFS-image.png')[0])

#add extra empty rows to reshape img2
add_rows = int((img1.shape[1]/img1.shape[0]*img2.shape[1]-img2.shape[1])/2)
new = np.zeros((img2.shape[0], img2.shape[1]+add_rows*2, 3), dtype=np.float32)
new[:, add_rows:new.shape[1]-add_rows, :] = img2
img2 = new
#resize now img2
img1 = cv.resize(img1, (img2.shape[1], img2.shape[0]), interpolation=cv.INTER_AREA) # reshape image
for i in np.append(np.linspace(0, 1, frames//2), np.linspace(0, 1, frames//2)[::-1]):
alpha = i
beta = 1 - alpha
output = cv.addWeighted(img2, alpha, img1, beta, 0, dtype=cv.CV_32F)
output = crop_center(output, int((1 - i/4) * output.shape[1]), int((1 - i/4) * output.shape[0]))
n=1
if alpha==1:
n=frames//6
for r in range(n):
images = glob('/home/jurjen/Documents/Python/advanced_astro_visualiziation/video/frames_high/*')
new_im_name = f'/home/jurjen/Documents/Python/advanced_astro_visualiziation/video/frames_high/image_{str(len(images)).rjust(5, "0")}.png'
cv.imwrite(new_im_name, output)

if __name__ == '__main__':
print('Cannot call script directly.')

0 comments on commit b7153d3

Please sign in to comment.