Skip to content

Commit 1d9c113

Browse files
Update the CI with the new ZTF image (#160)
* Update the CI with the new ZTF image * Bring back image classification functionalities in a new module cutouts * Run tests for cutouts
1 parent 68c231a commit 1d9c113

File tree

6 files changed

+137
-8
lines changed

6 files changed

+137
-8
lines changed

.github/workflows/run_test.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
container: ["julienpeloton/fink-ci:latest"]
18+
container: ["julienpeloton/fink-ci-ztf:latest"]
1919

2020
container:
2121
image: ${{ matrix.container }}
@@ -32,8 +32,6 @@ jobs:
3232
run: |
3333
python -m pip install --upgrade pip
3434
pip install -r requirements.txt
35-
pip install gatspy
36-
pip install .
3735
3836
- name: Run tests
3937
run: |

fink_utils/cutouts/__init__.py

Whitespace-only changes.

fink_utils/cutouts/utils.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Copyright 2021-2025 AstroLab Software
2+
# Author: Roman Le Montagner
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import gzip
16+
import io
17+
from astropy.io import fits
18+
import numpy as np
19+
20+
from fink_utils.test.tester import regular_unit_tests
21+
22+
23+
def unzip_cutout(stamp):
24+
"""Extract an image from a gzip format file
25+
26+
Notes
27+
-----
28+
Image is contained on a fits format file. Due to a significant number of corrupted images,
29+
a correction step is applied to remove nan values, negative values and corrected the wrong
30+
shapes of the images
31+
32+
Parameters
33+
----------
34+
stamp: gzip format file
35+
an image in a fits file compressed into a gzip format file
36+
37+
Returns
38+
-------
39+
out: 2D numpy array
40+
alert image after extraction from gzip format and correction of all kinds of problems
41+
"""
42+
with gzip.open(io.BytesIO(stamp), "rb") as fits_file:
43+
with fits.open(io.BytesIO(fits_file.read())) as hdul:
44+
img = hdul[0].data[::-1]
45+
img = np.where(img < 0, 0, img)
46+
if np.shape(img) != (63, 63):
47+
img_zeros = np.zeros((63, 63))
48+
idx = np.where(np.logical_not(np.isnan(img)))
49+
img_zeros[idx] = img[idx]
50+
return img_zeros
51+
return np.nan_to_num(img)
52+
53+
54+
def sigmoid(img):
55+
"""Compute the sigmoid term of the normalization function
56+
57+
Notes
58+
-----
59+
the alpha parameter is the standard deviation of the image and
60+
the beta parameter is the mean of the image
61+
62+
Parameters
63+
----------
64+
img: 2D numpy array
65+
alert image after extraction from gzip format
66+
67+
Returns
68+
-------
69+
out: float
70+
the sigmoid term for the image normalisation
71+
72+
Examples
73+
--------
74+
>>> test_1 = np.array([[0, 1, 2], [3, 40, 5], [2, 1, 0]])
75+
>>> test_2 = np.array([[0, 0, 0], [1, 0.5, 1], [1, 1, 1]])
76+
>>> sigmoid(test_1)
77+
array([[ 0.37861437, 0.39822621, 0.41817028],
78+
[ 0.43838554, 0.94307749, 0.47936865],
79+
[ 0.41817028, 0.39822621, 0.37861437]])
80+
>>> sigmoid(test_2)
81+
array([[ 0.20850741, 0.20850741, 0.20850741],
82+
[ 0.70033103, 0.43966158, 0.70033103],
83+
[ 0.70033103, 0.70033103, 0.70033103]])
84+
"""
85+
img_mean, img_std = img.mean(), img.std()
86+
img_normalize = (img - img_mean) / img_std
87+
inv_norm = -img_normalize
88+
exp_norm = np.exp(inv_norm)
89+
return 1 / (1 + exp_norm)
90+
91+
92+
def img_normalizer(img, vmin=0, vmax=1):
93+
"""Compute a non-linear normalisation thanks to sigmoid function of the image.
94+
95+
Parameters
96+
----------
97+
img: 2D numpy array
98+
alert image after extraction from gzip format
99+
100+
Returns
101+
-------
102+
out: 2D numpy array
103+
image where all values is now bounded between vmin and vmax.
104+
The range is distributed in a non-linear manner due to sigmoid function
105+
106+
Examples
107+
--------
108+
>>> test_1 = np.array([[0, 1, 2], [3, 40, 5], [2, 1, 0]])
109+
>>> test_2 = np.array([[0, 0, 0], [1, 0.5, 1], [1, 1, 1]])
110+
>>> img_normalizer(test_1)
111+
array([[ 0.37861437, 0.39822621, 0.41817028],
112+
[ 0.43838554, 0.94307749, 0.47936865],
113+
[ 0.41817028, 0.39822621, 0.37861437]])
114+
>>> img_normalizer(test_2, vmin = -255, vmax = 255)
115+
array([[-148.66122095, -148.66122095, -148.66122095],
116+
[ 102.16882492, -30.77259383, 102.16882492],
117+
[ 102.16882492, 102.16882492, 102.16882492]])
118+
"""
119+
return (vmax - vmin) * sigmoid(img) + vmin
120+
121+
122+
if __name__ == "__main__":
123+
"""Execute the unit test suite"""
124+
125+
# Run the Spark test suite
126+
regular_unit_tests(globals())

fink_utils/slack_bot/bot_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import fink_utils.slack_bot.bot as slack_bot
1010

11-
from fink_science.image_classification.utils import img_normalizer
11+
from fink_utils.cutouts.utils import img_normalizer
1212
from fink_utils.logging.logs import init_logging
1313
from fink_utils.slack_bot.msg_builder import Message, Divider, Header
1414
from fink_utils.slack_bot.section import Section, TypeText

requirements.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
# General
2-
numpy>=1.21.6
3-
pandas>=1.1.5
4-
requests>=2.22.0
52
pytest>=5.3.5
6-
slack_sdk>=3.26.2

run_test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ do
5858
$filename
5959
done
6060

61+
for filename in fink_utils/cutouts/*.py
62+
do
63+
echo $filename
64+
coverage run \
65+
--source=${ROOTPATH} \
66+
--rcfile .coveragerc \
67+
$filename
68+
done
69+
6170
echo fink_utils/slack_bot/bot_test.py
6271
coverage run --source=${ROOTPATH} --rcfile .coveragerc fink_utils/slack_bot/bot_test.py
6372

0 commit comments

Comments
 (0)