Skip to content

Commit 904ca63

Browse files
authored
Merge pull request #25 from CosmoStat/denoising_and_bss
gmca et mr_filter
2 parents 59c649d + f6358fb commit 904ca63

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

pycs/sparsity/sparse2d/mr_filter.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# @file mr_filter.py
2+
#
3+
# WAVELET FILTERING ROUTINE
4+
#
5+
# Functions for image denoising using mr_filter c++ binary
6+
#
7+
# @author Samuel Farrens
8+
# @version 1.0
9+
# @date 2015
10+
#
11+
12+
import numpy as np
13+
from os import remove
14+
from subprocess import check_call
15+
from subprocess import call
16+
from datetime import datetime
17+
from astropy.io import fits
18+
import shlex
19+
##
20+
# Function that calls mr_filter to perform a wavelet filtering on the
21+
# input data.
22+
#
23+
# @param[in] data: 2D Input array.
24+
# @param[in] opt: List of additonal mr_transform options.
25+
# @param[in] path: Path for output files.
26+
# @param[in] remove_files: Option to remove output files.
27+
#
28+
# @return Results of wavelet transform (and mr file name).
29+
#
30+
# %load_ext autoreload
31+
# %autoreload 2
32+
def mr_filter(data, opt=None, path='./', remove_files=True):
33+
34+
# Create a unique string using the current date and time.
35+
# print('mr_filter ', opt)
36+
unique_string = datetime.now().strftime('%Y.%m.%d_%H.%M.%S')
37+
result=0
38+
# Set the ouput file names.
39+
file_name = path + 'mr_temp_' + unique_string
40+
file_fits = file_name + '.fits'
41+
file_out = file_name + '_out.fits'
42+
43+
# Write the input data to a fits file.
44+
fits.writeto(file_fits, data)
45+
46+
cmd = 'mr_filter '
47+
48+
if isinstance(opt, type(None)):
49+
optF=' '
50+
else:
51+
optF=opt
52+
cmd = cmd + optF + ' ' + file_fits + ' ' + file_out
53+
# print 'CMD = ', cmd
54+
args = shlex.split(cmd)
55+
# print('args ', args)
56+
57+
call(args)
58+
59+
# Retrieve wavelet filtered data.
60+
result = fits.getdata(file_out)
61+
62+
# Return the mr_transform results (and the output file names).
63+
if remove_files:
64+
remove(file_fits)
65+
remove(file_out)
66+
return result
67+
else:
68+
return result

pycs/sparsity/sparse2d/mr_gmca.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# @file mr_gmca.py
2+
#
3+
# GMCA WRAPPER ROUTINE
4+
#
5+
# Functions for blind source separation using mr_gmca c++ binary
6+
#
7+
# @author Jean-Luc Starck
8+
# @version 1.0
9+
# @date 2015
10+
#
11+
12+
import numpy as np
13+
from os import remove
14+
from subprocess import check_call
15+
from subprocess import call
16+
from datetime import datetime
17+
from astropy.io import fits
18+
import shlex
19+
from pycs.misc.cosmostat_init import *
20+
from pycs.misc.cosmostat_init import writefits
21+
22+
##
23+
# Function that calls mr_gmca to perform blind source separation on the
24+
# input data.
25+
#
26+
# @param[in] data: 2D Input array.
27+
# @param[in] opt: List of additonal mr_gmca options.
28+
# @param[in] path: Path for output files.
29+
# @param[in] remove_files: Option to remove output files.
30+
#
31+
# @return Results of wavelet transform (and mr file name).
32+
#
33+
# %load_ext autoreload
34+
# %autoreload 2
35+
def mr_gmca(data, opt=None, path='./', remove_files=True, verbose=False, FileOut=None):
36+
37+
# Create a unique string using the current date and time.
38+
# print('mr_filter ', opt)
39+
prog="mr_gmca"
40+
unique_string = datetime.now().strftime('%Y.%m.%d_%H.%M.%S')
41+
result=0
42+
# Set the ouput file names.
43+
file_name = path + 'mr_temp_' + unique_string
44+
file_fits = file_name + '.fits'
45+
if FileOut is not None:
46+
file_out = FileOut
47+
else:
48+
file_out = file_name + '_out'
49+
50+
# Write the input data to a fits file.
51+
writefits(file_fits, data)
52+
53+
# print("PROG: ", prog)
54+
cmd = prog
55+
56+
if isinstance(opt, type(None)):
57+
optF=' '
58+
else:
59+
optF= opt
60+
if verbose:
61+
optF = optF + " -v "
62+
63+
cmd = cmd + " " + optF + " " + file_fits + " " + file_out
64+
if verbose:
65+
print ('CMD = ', cmd)
66+
67+
args = shlex.split(cmd)
68+
# print('args ', args)
69+
call(args)
70+
71+
# Retrieve wavelet filtered data.
72+
file_out_source = file_out + ".fits"
73+
file_out_mat = path + "xx_EstMixmat.fits"
74+
file_out_invmat = path + "xx_InvMixingMat.fits"
75+
76+
result = readfits(file_out_source)
77+
est_mixmat = readfits(file_out_mat)
78+
est_invmixmat = readfits (file_out_invmat)
79+
80+
81+
# Return the mr_transform results (and the output file names).
82+
if remove_files:
83+
remove(file_fits)
84+
remove(file_out_source)
85+
remove(file_out_mat)
86+
remove(file_out_invmat)
87+
88+
return result,est_mixmat,est_invmixmat
89+
else:
90+
return result,est_mixmat,est_invmixmat

0 commit comments

Comments
 (0)