Skip to content

Commit

Permalink
Merge pull request #65 from Athanaseus/region_stats
Browse files Browse the repository at this point in the history
Region stats
  • Loading branch information
Athanaseus committed Jun 23, 2023
2 parents d454d63 + 00afc08 commit 6d59134
Show file tree
Hide file tree
Showing 8 changed files with 2,576 additions and 278 deletions.
72 changes: 46 additions & 26 deletions .github/workflows/test_installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,55 @@ on:
pull_request:
branches: [ master ]

env:
POETRY_VERSION: 1.4.0

jobs:
build:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-20.04, ubuntu-22.04 ]
python-version: [3.8, 3.9]

python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade setuptools wheel
pip install flake8 pytest
pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest -s -vvv
- name: Test command
run: |
aimfast -h
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Cache Installations
id: cache-installs
uses: actions/cache@v3
with:
path: ~/.local
key: install-${{ env.INSTALL_CACHE_HASH }}-3

- name: Install Poetry
if: steps.cache-installs.outputs.cache-hit != 'true'
run: |
curl -sSL https://install.python-poetry.org | python3 - --version ${{ env.POETRY_VERSION }}
- name: Test poetry
run: poetry --version

- name: Checkout source
uses: actions/checkout@v2
with:
fetch-depth: 1

- name: Install aimfast
run: poetry install --extras "source_finders" --with tests

- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test command
run: poetry run aimfast -h

- name: Run testsuite
run: poetry run py.test -vvv aimfast/
46 changes: 45 additions & 1 deletion aimfast/aimfast.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tempfile
import numpy as np

from regions import Regions
from functools import partial
from collections import OrderedDict

Expand Down Expand Up @@ -527,6 +528,43 @@ def image_stats(image_data, test_normality=None, data_range=None):
return img_stats


def fix_wcs_fits(wcs, dropaxis=2):
"""This removes the degenerated dimensions in APLpy 2.X...
The input must be the object returned by aplpy.FITSFigure().
`dropaxis` is the index where to start dropping the axis (by default it assumes the 3rd,4th place).
"""
temp_wcs = wcs.dropaxis(dropaxis)
temp_wcs = temp_wcs.dropaxis(dropaxis)
return temp_wcs


def get_region_stats(fitsname, regions_file):
"""Extract flux densities measurements within the provided region"""
regions_stats = dict()
LOGGER.info(f'Reading region file: {regions_file}')
regions_list = Regions.read(regions_file, format='ds9')
LOGGER.info(f'Number of regions: {len(regions_list)}')
image = fitsio.open(fitsname)
image_data = image[0].data
fitsinfo = fitsInfo(fitsname)
wcs = fitsinfo['wcs']
beam = fitsinfo['b_size']
dra = fitsinfo['dra']
beam_area = (beam[0]*beam[1])/(dra*dra)
for i, input_region in enumerate(regions_list):
if hasattr(input_region, 'to_pixel'):
input_region = input_region.to_pixel(fix_wcs_fits(wcs))
mask = input_region.to_mask().to_image(image_data.shape[-2:])
data = mask * image_data[0][0]
#nndata=nndata[~np.isnan(data)]
nndata = np.flip(data, axis=0)
nndata = nndata[~np.isnan(nndata)]
nndata = nndata[nndata != -0.0]
stats = image_stats(nndata)
regions_stats[f'region-{i}'] = stats
return regions_stats


def normality_testing(data, test_normality='normaltest', data_range=None):
"""Performs a normality test on the image data.
Expand Down Expand Up @@ -2955,6 +2993,8 @@ def get_argparser():
help='Name of the mask image fits file')
argument('-fdr', '--fidelity-results', dest='json',
help='aimfast fidelity results file (JSON format)')
argument('-reg', '--input-regions', dest='reg',
help='Region file with regions to generate stats)')
# Source finding
argument('-c', '--config', dest='config',
help='Config file to run source finder of choice (YAML format)')
Expand Down Expand Up @@ -3178,7 +3218,7 @@ def main():
'DR_global_rms' : DR['global_rms'],
'DR_local_rms' : DR['local_rms']}})
output_dict[residual_label] = stats
elif args.residual:
elif args.residual and not args.reg:
if args.residual not in output_dict.keys():
if args.test_normality in ['shapiro', 'normaltest']:
stats = residual_image_stats(args.residual,
Expand Down Expand Up @@ -3426,6 +3466,10 @@ def main():
LOGGER.error(f"{R}Provide Centre coordinates in pixels "
f"and size of subimage(s).{W}")

if args.reg:
centre_coords = []
stats = get_region_stats(args.residual, args.reg)
output_dict[residual_label] = stats

if output_dict:
if args.outfile:
Expand Down
5 changes: 2 additions & 3 deletions aimfast/bin/aimfast → aimfast/main.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3

from aimfast.aimfast import main

main()
def driver():
main()
Loading

0 comments on commit 6d59134

Please sign in to comment.