Skip to content

Commit

Permalink
Return output filename
Browse files Browse the repository at this point in the history
jsy1001 committed Sep 17, 2018
1 parent fabf3fe commit 5e2c33d
Showing 2 changed files with 57 additions and 25 deletions.
46 changes: 33 additions & 13 deletions oirunner/runbsmem.py
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ def reconst_grey_basic(datafile: str,
modeltype: int = DEFAULT_MT,
modelwidth: float = DEFAULT_MW,
uvmax: float = None,
alpha: float = None) -> None:
alpha: float = None) -> str:
"""Reconstruct a grey image by running bsmem once.
Args:
@@ -146,16 +146,21 @@ def reconst_grey_basic(datafile: str,
uvmax: Maximum uv radius to select (waves).
alpha: Regularization hyperparameter.
Returns:
Output FITS filename.
"""
run_bsmem_using_model(datafile, _get_outputfile(datafile, 1), dim,
outputfile = _get_outputfile(datafile, 1)
run_bsmem_using_model(datafile, outputfile, dim,
modeltype, modelwidth,
pixelsize=pixelsize, uvmax=uvmax, alpha=alpha)
return outputfile


def reconst_grey_basic_using_image(datafile: str,
imagefile: str,
uvmax: float = None,
alpha: float = None) -> None:
alpha: float = None) -> str:
"""Reconstruct a grey image by running bsmem once using a prior image.
Args:
@@ -164,13 +169,18 @@ def reconst_grey_basic_using_image(datafile: str,
uvmax: Maximum uv radius to select (waves).
alpha: Regularization hyperparameter.
Returns:
Output FITS filename.
"""
outputfile = _get_outputfile(datafile, 1)
with fits.open(imagefile) as hdulist:
imagehdu = hdulist[0]
dim = imagehdu.data.shape[0]
pixelsize = get_pixelsize(imagehdu)
run_bsmem_using_image(datafile, _get_outputfile(datafile, 1), dim,
pixelsize, imagehdu, uvmax=uvmax, alpha=alpha)
run_bsmem_using_image(datafile, outputfile, dim, pixelsize, imagehdu,
uvmax=uvmax, alpha=alpha)
return outputfile


def reconst_grey_2step(datafile: str,
@@ -181,7 +191,7 @@ def reconst_grey_2step(datafile: str,
uvmax1: float = 1.1e8,
alpha: float = None,
fwhm: float = 1.25,
threshold: float = 0.05) -> None:
threshold: float = 0.05) -> str:
"""Reconstruct a grey image by running bsmem twice.
Args:
@@ -195,23 +205,28 @@ def reconst_grey_2step(datafile: str,
fwhm: FWHM of Gaussian to convolve 1st run output with (mas).
threshold: Threshold (relative to peak) to apply to 1st run output.
Returns:
Output FITS filename.
"""
# :TODO: intelligent defaults for uvmax1, fwhm?
out1file = _get_outputfile(datafile, 1)
run_bsmem_using_model(datafile, out1file, dim, modeltype, modelwidth,
pixelsize=pixelsize, uvmax=uvmax1, alpha=alpha)
with fits.open(out1file) as hdulist:
imagehdu = makesf(hdulist[0], fwhm, threshold)
run_bsmem_using_image(datafile, _get_outputfile(datafile, 2),
dim, pixelsize, imagehdu, alpha=alpha)
out2file = _get_outputfile(datafile, 2)
run_bsmem_using_image(datafile, out2file, dim, pixelsize, imagehdu,
alpha=alpha)
return out2file


def reconst_grey_2step_using_image(datafile: str,
imagefile: str,
uvmax1: float = 1.1e8,
alpha: float = None,
fwhm: float = 1.25,
threshold: float = 0.05) -> None:
threshold: float = 0.05) -> str:
"""Reconstruct a grey image by running bsmem twice using a prior image.
Args:
@@ -222,15 +237,20 @@ def reconst_grey_2step_using_image(datafile: str,
fwhm: FWHM of Gaussian to convolve 1st run output with (mas).
threshold: Threshold (relative to peak) to apply to 1st run output.
Returns:
Output FITS filename.
"""
out1file = _get_outputfile(datafile, 1)
with fits.open(imagefile) as hdulist:
image1hdu = hdulist[0]
dim = image1hdu.data.shape[0]
pixelsize = get_pixelsize(image1hdu)
run_bsmem_using_image(datafile, out1file, dim,
pixelsize, image1hdu, uvmax=uvmax1, alpha=alpha)
run_bsmem_using_image(datafile, out1file, dim, pixelsize, image1hdu,
uvmax=uvmax1, alpha=alpha)
out2file = _get_outputfile(datafile, 2)
with fits.open(out1file) as hdulist:
image2hdu = makesf(hdulist[0], fwhm, threshold)
run_bsmem_using_image(datafile, _get_outputfile(datafile, 2),
dim, pixelsize, image2hdu, alpha=alpha)
run_bsmem_using_image(datafile, out2file, dim, pixelsize, image2hdu,
alpha=alpha)
return out2file
36 changes: 24 additions & 12 deletions tests/test_runbsmem.py
Original file line number Diff line number Diff line change
@@ -23,36 +23,48 @@ def test_grey_basic(self):
with tempfile.TemporaryDirectory() as dirname:
tempdatafile = os.path.join(dirname, os.path.basename(DATAFILE))
copyfile(DATAFILE, tempdatafile)
runbs.reconst_grey_basic(tempdatafile)
runbs.reconst_grey_basic(tempdatafile, pixelsize=0.25)
runbs.reconst_grey_basic(tempdatafile, uvmax=1.1e8)
runbs.reconst_grey_basic(tempdatafile, alpha=4000.)
runbs.reconst_grey_basic(tempdatafile, pixelsize=0.25,
uvmax=1.1e8, alpha=4000.)
out = runbs.reconst_grey_basic(tempdatafile)
self.assertTrue(os.path.exists(out))
out = runbs.reconst_grey_basic(tempdatafile, pixelsize=0.25)
self.assertTrue(os.path.exists(out))
out = runbs.reconst_grey_basic(tempdatafile, uvmax=1.1e8)
self.assertTrue(os.path.exists(out))
out = runbs.reconst_grey_basic(tempdatafile, alpha=4000.)
self.assertTrue(os.path.exists(out))
out = runbs.reconst_grey_basic(tempdatafile, pixelsize=0.25,
uvmax=1.1e8, alpha=4000.)
self.assertTrue(os.path.exists(out))

@unittest.skipUnless(HAVE_BSMEM, "requires bsmem")
def test_grey_basic_using_image(self):
"""Test grey reconstruction using a prior image"""
with tempfile.TemporaryDirectory() as dirname:
tempdatafile = os.path.join(dirname, os.path.basename(DATAFILE))
copyfile(DATAFILE, tempdatafile)
runbs.reconst_grey_basic_using_image(tempdatafile, IMAGEFILE)
runbs.reconst_grey_basic(tempdatafile, uvmax=1.1e8)
runbs.reconst_grey_basic(tempdatafile, alpha=4000.)
runbs.reconst_grey_basic(tempdatafile, uvmax=1.1e8, alpha=4000.)
out = runbs.reconst_grey_basic_using_image(tempdatafile, IMAGEFILE)
self.assertTrue(os.path.exists(out))
out = runbs.reconst_grey_basic(tempdatafile, uvmax=1.1e8)
self.assertTrue(os.path.exists(out))
out = runbs.reconst_grey_basic(tempdatafile, alpha=4000.)
self.assertTrue(os.path.exists(out))
out = runbs.reconst_grey_basic(tempdatafile,
uvmax=1.1e8, alpha=4000.)
self.assertTrue(os.path.exists(out))

@unittest.skipUnless(HAVE_BSMEM, "requires bsmem")
def test_grey_2step(self):
"""Test two-step grey reconstruction"""
with tempfile.TemporaryDirectory() as dirname:
tempdatafile = os.path.join(dirname, os.path.basename(DATAFILE))
copyfile(DATAFILE, tempdatafile)
runbs.reconst_grey_2step(tempdatafile, 0.25)
out = runbs.reconst_grey_2step(tempdatafile, 0.25)
self.assertTrue(os.path.exists(out))

@unittest.skipUnless(HAVE_BSMEM, "requires bsmem")
def test_grey_2step_using_image(self):
"""Test two-step grey reconstruction using a prior image"""
with tempfile.TemporaryDirectory() as dirname:
tempdatafile = os.path.join(dirname, os.path.basename(DATAFILE))
copyfile(DATAFILE, tempdatafile)
runbs.reconst_grey_2step_using_image(tempdatafile, IMAGEFILE)
out = runbs.reconst_grey_2step_using_image(tempdatafile, IMAGEFILE)
self.assertTrue(os.path.exists(out))

0 comments on commit 5e2c33d

Please sign in to comment.