|
| 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()) |
0 commit comments