|
| 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