Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge changes from jwst-kpi xara version #6

Draft
wants to merge 56 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
2e12410
Adding skeleton for KPI3Pipeline and draft NIRISS pupil model
Mar 9, 2022
dc04797
First functional version of KPI3Pipeline for NIRCam and NIRISS
Mar 30, 2022
1ff1048
Save output as KPFITS file and shift in complex visibility space
Apr 7, 2022
5741b01
Adding tutorial notebook, empirical uncertainties step, and CLEARP pu…
Apr 22, 2022
3e88125
Fixing bugs with tutorial notebook
Apr 22, 2022
c1a2ea7
Update CLEAR and CLEARP masks
vandalt Apr 29, 2022
a427d37
Merge pull request #2 from vandalt/calwebb-masks
kammerje Apr 29, 2022
fab27d4
Fix integer pixel recentering
May 10, 2022
ee463ef
Merge branch 'develop' of https://github.com/kammerje/xara into develop
May 10, 2022
560da3b
Basic formatting changes
vandalt May 22, 2022
eb8da42
Refactor block to open fits in function
vandalt May 22, 2022
20c2aaf
Refactor code to load data into separate function
vandalt May 22, 2022
44aed86
Use separate function to get output basename
vandalt May 22, 2022
7f1c613
Full Python .gitignore
vandalt Apr 8, 2022
06c3dc6
Add user-accessible option to show plots
vandalt May 23, 2022
ce23cb0
Add option to crop NIRISS data
vandalt May 23, 2022
c017c41
Formatting (blank lines) in utils file
vandalt May 23, 2022
3533b3c
Merge pull request #5 from vandalt/calwebb-fmt
kammerje May 23, 2022
0ea2d20
Merge pull request #6 from vandalt/calwebb-plotting
kammerje May 23, 2022
2bddc78
Merge pull request #7 from vandalt/calwebb-crop
kammerje May 23, 2022
d39e8fc
Include pupil models
vandalt May 24, 2022
335e42f
Fallback to local repo for local install pupil models
vandalt May 24, 2022
bbc133c
Merge pull request #10 from vandalt/fix-pupil-import
kammerje May 24, 2022
eca7f56
Dirty fix for issue with pupil rotation
May 24, 2022
c456dcb
Allow user to specify pupil model path
May 25, 2022
dd77d0f
Add hexagonal pupil models
Jun 10, 2022
3cc3204
Remove pipeline files
vandalt Aug 4, 2022
506d059
Remove jwst directory with pupil models
vandalt Aug 4, 2022
d0348d3
Remove example JWST notebook
vandalt Aug 4, 2022
571bd88
Remove JWST data files from setup
vandalt Aug 4, 2022
c441faf
Add hexagon function from opticstools
vandalt Aug 4, 2022
247a7ca
Merge pull request #12 from vandalt/calwebb-kpi-ignore
kammerje Aug 5, 2022
c0c172b
Merge pull request #13 from vandalt/calwebb-nopipeline
kammerje Aug 5, 2022
1572d8e
np.float -> float
vandalt Aug 5, 2022
5603587
Update docstrings for new args following calwebb updates
vandalt Sep 8, 2022
6cee2c0
Use xara defaults for plots
vandalt Sep 8, 2022
ff4f3af
If statement formatting
vandalt Sep 8, 2022
4f0c90b
If statement formatting
vandalt Sep 8, 2022
b3ddfba
Make integer-pixel recentering independent of sgmask
vandalt Sep 8, 2022
4206e9f
Fix normalization when setting is false
katherinethibault Sep 8, 2022
2c7f7d8
Merge pull request #14 from vandalt/fix-np-float
kammerje Sep 12, 2022
8796205
Merge pull request #15 from vandalt/document-xara
kammerje Sep 12, 2022
97ee2aa
Merge pull request #16 from vandalt/update-recenter
kammerje Sep 12, 2022
d7d0ffc
Merge remote-tracking branch 'upstream/master' into test-merge-master
vandalt Sep 19, 2022
de79a34
Go back to xymax=None (more generic)
vandalt Sep 19, 2022
ca2626b
Merge remote-tracking branch 'katherine/Issue#2' into test-merge-master
vandalt Sep 19, 2022
64dc621
Merge pull request #18 from vandalt/test-merge-master
kammerje Sep 27, 2022
00efbb4
Put back Kpi3Pipeline
Nov 2, 2022
79961cd
Use hexagon function from core (fix import error)
vandalt Jan 31, 2023
77ee9fa
Remove deprecated `np.float` in favor of built-in `float`
vandalt Jan 31, 2023
d64f483
use `==` instead of `is` for string comparison
vandalt Jan 31, 2023
ead7827
Merge pull request #19 from vandalt/reapply
kammerje Feb 3, 2023
f8f0f0d
Remove calwebb-kpi leftover files
vandalt Jul 12, 2023
23cc8ff
Formatting
vandalt Jul 12, 2023
2716c14
Use `self.KPM` instead of old alias `self.KerPhi` when creating KPM HDU
vandalt Jul 12, 2023
ecdb354
Disable pyright type checking
vandalt Jul 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
154 changes: 144 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,148 @@
# compiled python modules
*.pyc
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# emacs temporary save files
*~
# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# setuptools distribution folder
/dist/
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# python egg metadata, regenerated from source files by setuptools
/*.egg-info
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# build folder
/build/
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# emacs temporary save files
*~
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.pyright]
typeCheckingMode = "off"
112 changes: 99 additions & 13 deletions xara/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,13 @@ def find_fourier_origin(img, mykpo, m2pix, bmax=6.0):

# Find Fourier plane coordinates which will be considered for the re-centering
uv_dist = np.sqrt(mykpo.kpi.UVC[:, 0]**2+mykpo.kpi.UVC[:, 1]**2)
uv_cutoff = np.where(uv_dist < float(r_cutoff))[0]
uv_cutoff = np.where(uv_dist < float(bmax))[0]

# Find best sub-pixel shift
img_fft = np.fft.rfft2(img_cent)
best_xy_shift = leastsq(func=fourier_phase_resid_2d,
x0=np.array([0., 0.]),
args=(img_fft, m2pix, uv, uv_cutoff),
args=(img_fft, mykpo, m2pix, uv, uv_cutoff),
ftol=1E-1)[0]

# Return best shift
Expand Down Expand Up @@ -514,7 +514,8 @@ def fourier_phase_resid_2d(xy, img_fft, mykpo, m2pix, uv, uv_cutoff):


# =========================================================================
def determine_origin(img, mask=None, algo="BCEN", verbose=True, wmin=10.0):
def determine_origin(img, mask=None, algo="BCEN", verbose=True, wmin=10.0,
mykpo=None, m2pix=None, bmax=None):
''' ------------------------------------------------------------
Determines the origin of the image, using among possible algorithms.

Expand All @@ -540,6 +541,11 @@ def determine_origin(img, mask=None, algo="BCEN", verbose=True, wmin=10.0):
if "cog" in algo.lower():
(x0, y0) = centroid(img1, verbose)

elif "fpnm" in algo.lower():
if mykpo is None or m2pix is None or bmax is None:
raise ValueError("Need mykpo, m2pix, and bmax parameters")
(x0, y0) = find_fourier_origin(img1, mykpo, m2pix, bmax)

else:
(x0, y0) = find_psf_center(img1, verbose, nbit=10, wmin=wmin)

Expand All @@ -548,7 +554,8 @@ def determine_origin(img, mask=None, algo="BCEN", verbose=True, wmin=10.0):

# =========================================================================
def recenter(im0, mask=None, algo="BCEN", subpix=True, between=False,
verbose=True):
verbose=True, return_center=False, dxdy=None, mykpo=None,
m2pix=None, bmax=None):
''' ------------------------------------------------------------
Re-centering algorithm of a 2D image im0 for kernel-analysis

Expand All @@ -563,6 +570,18 @@ def recenter(im0, mask=None, algo="BCEN", subpix=True, between=False,
- subpix: sub-pixel recentering (boolean, default=True)
- between: center in between 4 pixels (boolean, default=False)
- verbose: display some additional info (boolean, default=True)
- return_center: return center coordinate in original image (boolean, default=False)
- dxdy: center coordinates to use, origin is found with `algo` if None (Tuple[float], default=None)
- mykpo: KPO object, used to determine origin with FPNM (xara.kpo.KPO, default=None)
- m2pix: Meter to pixel scaling parameter for FPNM (float, default=None)
- bmax: Max baseline (in m) for FPNM (float, default=None)

Returns:
-------
- img: The recentered image
- If `return_center` is True:
+ dx_temp: x coordinate of the center in original image
+ dy_temp: y coordinate of the center in original image

Remarks:
-------
Expand All @@ -572,18 +591,25 @@ def recenter(im0, mask=None, algo="BCEN", subpix=True, between=False,

ysz, xsz = im0.shape

(x0, y0) = determine_origin(im0, mask=mask, algo=algo, verbose=verbose)
if dxdy is None:
(x0, y0) = determine_origin(im0, mask=mask, algo=algo, verbose=verbose,
mykpo=mykpo, m2pix=m2pix, bmax=bmax)

dy, dx = (y0-ysz/2), (x0-xsz/2)
if between:
dy += 0.5
dx += 0.5
dy, dx = (y0-ysz/2), (x0-xsz/2)
if between:
dy += 0.5
dx += 0.5

if verbose:
print("centroid: dx=%+5.2f, dy=%+5.2f\n" % (dx, dy),
end="", flush=True)
if verbose:
print("centroid: dx=%+5.2f, dy=%+5.2f\n" % (dx, dy),
end="", flush=True)

else:
dx, dy = dxdy

# integer pixel recentering first
dx_temp = dx
dy_temp = dy
im0 = np.roll(np.roll(im0, -int(round(dx)), axis=1),
-int(round(dy)), axis=0)

Expand Down Expand Up @@ -619,7 +645,10 @@ def recenter(im0, mask=None, algo="BCEN", subpix=True, between=False,
offset = np.exp(1j*slope)
dummy = np.real(shift(ifft(offset * fft(shift(im)))))
im0 = dummy[oriy:oriy+ysz, orix:orix+xsz]
return im0
if not return_center:
return im0
else:
return im0, dx_temp, dy_temp


# =========================================================================
Expand Down Expand Up @@ -780,6 +809,63 @@ def uv_phase_regrid_matrix(UVD, UVS, rad):
return GG


def hexagon(dim, width, interp_edge=True):
"""This function creates a hexagon.

Adapted from: https://github.com/mikeireland/opticstools/blob/master/opticstools/utils.py#L164
by Mike Ireland under MIT License

Parameters
----------
dim: int
Size of the 2D array
width: int
flat-to-flat width of the hexagon

Returns
-------
pupil: float array (sz,sz)
2D array hexagonal pupil mask
"""
x = np.arange(dim) - dim / 2.0
xy = np.meshgrid(x, x)
xx = xy[1]
yy = xy[0]
hex = np.zeros((dim, dim))
scale = 1.5
offset = 0.5
if interp_edge:
# !!! Not fully implemented yet. Need to compute the orthogonal distance
# from each line and accurately find fractional area of each pixel.
hex = (
np.minimum(np.maximum(width / 2 - yy + offset, 0), 1)
* np.minimum(np.maximum(width / 2 + yy + offset, 0), 1)
* np.minimum(
np.maximum((width - np.sqrt(3) * xx - yy + offset) * scale, 0), 1
)
* np.minimum(
np.maximum((width - np.sqrt(3) * xx + yy + offset) * scale, 0), 1
)
* np.minimum(
np.maximum((width + np.sqrt(3) * xx - yy + offset) * scale, 0), 1
)
* np.minimum(
np.maximum((width + np.sqrt(3) * xx + yy + offset) * scale, 0), 1
)
)
else:
w = np.where(
(yy < width / 2)
* (yy > (-width / 2))
* (yy < (width - np.sqrt(3) * xx))
* (yy > (-width + np.sqrt(3) * xx))
* (yy < (width + np.sqrt(3) * xx))
* (yy > (-width - np.sqrt(3) * xx))
)
hex[w] = 1.0
return hex


# =========================================================================
def create_discrete_model(apert, ppscale, step, binary=True, tmin=0.8):
'''------------------------------------------------------------------
Expand Down