-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_deband.py
156 lines (152 loc) · 5.22 KB
/
time_deband.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# import numpy as np
from astro_utils import *
from time import time
# import multiprocessing as mp
os.chdir('/home/innereye/astro/data/IR09022/')
hdu0 = fits.open('jw03368-o113_t007_nircam_clear-f150w_i2d.fits')
data = hdu0[1].data[3200:3600, 3200:3600]
hdu0.close()
clean = data.copy()
t0 = time()
clean = deband_layer(clean, func=np.nanpercentile)
print(np.round(time()-t0))
##
plt.figure()
plt.imshow(level_adjust(clean, factor=1), origin='lower', cmap='gray')
plt.show()
#
# def nanpercentile_worker(args):
# data, q = args
# return np.nanpercentile(data, q, axis=1)
#
#
# def parallel_nanpercentile(data, q, num_processes=8):
# pool = mp.Pool(num_processes)
# chunk_size = len(data) // num_processes
# results = pool.map(nanpercentile_worker, [(data[i:i+chunk_size], q) for i in range(0, len(data), chunk_size)])
# pool.close()
# pool.join()
# return np.concatenate(results)
#
#
# def smooth_width1(layer, win=101, prct=50):
# half0 = int(win / 2)
# half1 = win - half0
# smoothed = layer.copy()
# for ii in range(smoothed.shape[0]):
# toavg = np.nan * np.ones((layer.shape[1] + win - 1, win))
# # toavg = np.zeros((layer.shape[1] + win - 1, win))
# for shift in np.arange(win):
# toavg[shift:layer.shape[1] + shift, shift] = layer[ii, :]
# # smoothed[ii, :] = nanpercentile(toavg, prct)[half0:-half1 + 1]
# smoothed[ii, :] = parallel_nanpercentile(toavg, 10)[half0:-half1 + 1]
# print(f'{ii}/{smoothed.shape[0]-1}', end='\r')
# return smoothed
#
#
# def deband_layer1(layer, win=101, prct=10):
# lp = smooth_width1(layer, win=win, prct=prct)
# hp = layer - lp
# lp = smooth_width1(lp.T, win=win, prct=prct).T
# clean = lp + hp
# clean[clean < 0] = 0
# return clean
# Example usage
# data = np.random.rand(1000, 1000) # Example 2D array
# q = 50 # Percentile value
# num_processes = 4 # Number of processes to use
#
# # result = parallel_nanpercentile(data, q, num_processes)
#
#
# os.chdir('/home/innereye/astro/data/IR09022/')
#
# # IR07251nircam.pkl
# # plt.imshow(level_adjust(hdu0[1].data[2000:3000, 3000:4000], factor=1), origin='lower', cmap='gray')
# ##
#
#
#
# def nanpercentile(arr, prct):
# axis = 1
# mask = ~np.isnan(arr)
# count = mask.sum(axis=axis)
# # groups = np.unique(count)
# # groups = groups[groups > 0]
# # percentile[
# percentile = np.zeros((arr.shape[0]))
# for g in range(len(groups)):
# pos = np.where(count == groups[g])
# values = arr[pos]
# values = np.nan_to_num(values, nan=(np.nanmin(arr) - 1))
# values = np.sort(values, axis=axis)
# values = values[:, -groups[g]:]
# percentile[pos] = np.percentile(values, prct, axis=axis)
# return percentile
#
# def nanpercentile1(arr, prct):
# axis = 1
# mask = ~np.isnan(arr)
# count = mask.sum(axis=axis)
# groups = np.unique(count)
# groups = groups[groups > 0]
# percentile = np.zeros((arr.shape[0]))
# for g in range(len(groups)):
# pos = np.where(count == groups[g])
# values = arr[pos]
# values = np.nan_to_num(values, nan=(np.nanmin(arr) - 1))
# values = np.sort(values, axis=axis)
# values = values[:, -groups[g]:]
# percentile[pos] = np.percentile(values, prct, axis=axis)
# return percentile
#
# def smooth_width2(layer, win=101, prct=50):
# half0 = int(win / 2)
# half1 = win - half0
# smoothed = layer.copy()
# for ii in range(smoothed.shape[0]):
# toavg = np.nan * np.ones((layer.shape[1] + win - 1, win))
# # toavg = np.zeros((layer.shape[1] + win - 1, win))
# for shift in np.arange(win):
# toavg[shift:layer.shape[1] + shift, shift] = layer[ii, :]
# smoothed[ii, :] = nanpercentile(toavg, prct)[half0:-half1 + 1]
# # smoothed[ii, :] = np.nanmedian(toavg, axis=1)[half0:-half1 + 1]
# print(f'{ii}/{smoothed.shape[0]-1}', end='\r')
# return smoothed
#
# def smooth_width1(arr, prct=10, win=101):
# till = int(win/2+0.5)
# fro = till-win
# result = np.zeros(arr.shape) * np.nan
# for hh in range(arr.shape[0]):
# row = np.zeros(arr.shape[1])
# for ii in range(int(win/2), int(arr.shape[0]-win/2+0.5)):
# # row[ii] = np.percentile(arr[hh, np.max([0, ii+fro]):np.min([len(arr)+1, ii+till])], prct)
# row[ii] = np.percentile(arr[hh, ii + fro:ii + till], prct)
# for ii in range(int(arr.shape[0]-win/2+0.5)):
# row[ii] = np.percentile(arr[hh, np.max([0, ii+fro]):np.min([len(arr)+1, ii+till])], prct)
# # row[ii] = np.percentile(arr[hh, ii + fro:ii + till], prct)
# result[hh, :] = row
# return result
#
#
# def deband_layer1(layer, win=101, prct=10):
# lp = smooth_width1(layer, win=win, prct=prct)
# hp = layer - lp
# lp = smooth_width1(lp.T, win=win, prct=prct).T
# clean = lp + hp
# clean[clean < 0] = 0
# return clean
#
# hdu0 = fits.open('jw03368-o113_t007_nircam_clear-f150w_i2d.fits')
# data = hdu0[1].data[3200:3600, 3200:3600]
# hdu0.close()
# clean = data.copy()
# t0 = time()
# clean = deband_layer1(clean)
# print(np.round(time()-t0))
# ##
# plt.figure()
# plt.imshow(level_adjust(clean, factor=1), origin='lower', cmap='gray')
# ##
#