Skip to content

Commit c6a7ad3

Browse files
authored
Merge branch 'main' into feature/normalization
2 parents a2b1e55 + fc783e9 commit c6a7ad3

File tree

6 files changed

+105
-14
lines changed

6 files changed

+105
-14
lines changed

datalab/datalab_session/data_operations/median.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from datalab.datalab_session.data_operations.data_operation import BaseDataOperation
66
from datalab.datalab_session.exceptions import ClientAlertException
7-
from datalab.datalab_session.file_utils import create_fits, stack_arrays, create_jpgs
7+
from datalab.datalab_session.file_utils import create_fits, crop_arrays, create_jpgs
88
from datalab.datalab_session.s3_utils import save_fits_and_thumbnails
99

1010
log = logging.getLogger()
@@ -51,7 +51,8 @@ def operate(self):
5151

5252
image_data_list = self.get_fits_npdata(input, percent=0.4, cur_percent=0.0)
5353

54-
stacked_data = stack_arrays(image_data_list)
54+
cropped_data_list = crop_arrays(image_data_list)
55+
stacked_data = np.stack(cropped_data_list, axis=2)
5556

5657
# using the numpy library's median method
5758
median = np.median(stacked_data, axis=2)
@@ -64,6 +65,5 @@ def operate(self):
6465

6566
output = {'output_files': [output_file]}
6667

67-
self.set_percent_completion(1.0)
6868
self.set_output(output)
6969
log.info(f'Median output: {self.get_output()}')

datalab/datalab_session/data_operations/rgb_stack.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import logging
22

33
from astropy.io import fits
4+
import numpy as np
45

56
from datalab.datalab_session.data_operations.data_operation import BaseDataOperation
67
from datalab.datalab_session.exceptions import ClientAlertException
7-
from datalab.datalab_session.file_utils import get_fits, stack_arrays, create_fits, create_jpgs
8+
from datalab.datalab_session.file_utils import get_fits, crop_arrays, create_fits, create_jpgs
89
from datalab.datalab_session.s3_utils import save_fits_and_thumbnails
910

1011
log = logging.getLogger()
@@ -70,7 +71,9 @@ def operate(self):
7071

7172
# color photos take three files, so we store it as one fits file with a 3d SCI ndarray
7273
arrays = [fits.open(file)['SCI'].data for file in fits_paths]
73-
stacked_data = stack_arrays(arrays)
74+
cropped_data_list = crop_arrays(arrays)
75+
stacked_data = np.stack(cropped_data_list, axis=2)
76+
7477
fits_file = create_fits(self.cache_key, stacked_data)
7578

7679
output_file = save_fits_and_thumbnails(self.cache_key, fits_file, large_jpg_path, small_jpg_path)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import logging
2+
3+
import numpy as np
4+
5+
from datalab.datalab_session.data_operations.data_operation import BaseDataOperation
6+
from datalab.datalab_session.exceptions import ClientAlertException
7+
from datalab.datalab_session.file_utils import create_fits, create_jpgs, crop_arrays
8+
from datalab.datalab_session.s3_utils import save_fits_and_thumbnails
9+
10+
log = logging.getLogger()
11+
log.setLevel(logging.INFO)
12+
13+
14+
class Subtraction(BaseDataOperation):
15+
16+
@staticmethod
17+
def name():
18+
return 'Subtraction'
19+
20+
@staticmethod
21+
def description():
22+
return """
23+
The Subtraction operation takes in 1..n input images and calculated the subtraction value pixel-by-pixel.
24+
The output is a subtraction image for the n input images. This operation is commonly used for background subtraction.
25+
"""
26+
27+
@staticmethod
28+
def wizard_description():
29+
return {
30+
'name': Subtraction.name(),
31+
'description': Subtraction.description(),
32+
'category': 'image',
33+
'inputs': {
34+
'input_files': {
35+
'name': 'Input Files',
36+
'description': 'The input files to operate on',
37+
'type': 'file',
38+
'minimum': 1,
39+
'maximum': 999
40+
},
41+
'subtraction_file': {
42+
'name': 'Subtraction File',
43+
'description': 'This file will be subtracted from the input images.',
44+
'type': 'file',
45+
'minimum': 1,
46+
'maximum': 1
47+
}
48+
},
49+
}
50+
51+
def operate(self):
52+
53+
input_files = self.input_data.get('input_files', [])
54+
print(f'Input files: {input_files}')
55+
subtraction_file_input = self.input_data.get('subtraction_file', [])
56+
print(f'Subtraction file: {subtraction_file_input}')
57+
58+
if not subtraction_file_input:
59+
raise ClientAlertException('Missing a subtraction file')
60+
61+
if len(input_files) < 1:
62+
raise ClientAlertException('Need at least one input file')
63+
64+
log.info(f'Executing subtraction operation on {len(input_files)} files')
65+
66+
input_image_data_list = self.get_fits_npdata(input_files)
67+
self.set_percent_completion(.30)
68+
69+
subtraction_image = self.get_fits_npdata(subtraction_file_input)[0]
70+
self.set_percent_completion(.40)
71+
72+
outputs = []
73+
for index, input_image in enumerate(input_image_data_list):
74+
# crop the input_image and subtraction_image to the same size
75+
input_image, subtraction_image = crop_arrays([input_image, subtraction_image])
76+
77+
difference_array = np.subtract(input_image, subtraction_image)
78+
79+
fits_file = create_fits(self.cache_key, difference_array)
80+
large_jpg_path, small_jpg_path = create_jpgs(self.cache_key, fits_file)
81+
82+
output_file = save_fits_and_thumbnails(self.cache_key, fits_file, large_jpg_path, small_jpg_path, index)
83+
outputs.append(output_file)
84+
85+
self.set_percent_completion(self.get_percent_completion() + .50 * (index + 1) / len(input_files))
86+
87+
output = {'output_files': outputs}
88+
89+
self.set_output(output)
90+
log.info(f'Subtraction output: {self.get_output()}')

datalab/datalab_session/file_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def create_jpgs(cache_key, fits_paths: str, color=False) -> list:
7979

8080
return large_jpg_path, thumbnail_jpg_path
8181

82-
def stack_arrays(array_list: list):
82+
def crop_arrays(array_list: list):
8383
"""
8484
Takes a list of numpy arrays from fits images and stacks them to be a 3d numpy array
8585
cropped since fits images can be different sizes
@@ -88,8 +88,7 @@ def stack_arrays(array_list: list):
8888
min_y = min(arr.shape[1] for arr in array_list)
8989

9090
cropped_data_list = [arr[:min_x, :min_y] for arr in array_list]
91-
92-
return np.stack(cropped_data_list, axis=2)
91+
return cropped_data_list
9392

9493
def scale_points(height_1: int, width_1: int, height_2: int, width_2: int, x_points=[], y_points=[], flip_y = False, flip_x = False):
9594
"""

datalab/datalab_session/s3_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def save_fits_and_thumbnails(cache_key, fits_path, large_jpg_path, thumbnail_jpg
152152
'fits_url': fits_url,
153153
'large_url': large_jpg_url,
154154
'thumbnail_url': thumbnail_jpg_url,
155-
'basename': f'{bucket_key}',
155+
'basename': f'{cache_key}-{index}' if index else cache_key,
156156
'source': 'datalab'}
157157
)
158158

datalab/datalab_session/tests/test_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ def test_stack_arrays(self):
5353
test_array_1 = np.zeros((10, 20))
5454
test_array_2 = np.ones((20, 10))
5555

56-
stacked_array = stack_arrays([test_array_1, test_array_2])
57-
self.assertIsInstance(stacked_array, np.ndarray)
58-
self.assertEqual(stacked_array.shape, (10, 10, 2))
59-
self.assertEqual(stacked_array[:, :, 0].tolist(), np.zeros((10, 10)).tolist())
60-
self.assertEqual(stacked_array[:, :, 1].tolist(), np.ones((10, 10)).tolist())
56+
cropped_array = crop_arrays([test_array_1, test_array_2])
57+
self.assertEqual(len(cropped_array), 2)
58+
self.assertEqual(cropped_array[0].tolist(), np.zeros((10, 10)).tolist())
59+
self.assertEqual(cropped_array[1].tolist(), np.ones((10, 10)).tolist())
6160

6261
def test_scale_points(self):
6362
x_points = [1, 2, 3]

0 commit comments

Comments
 (0)