Plsek commited on
Commit
a8b8828
·
1 Parent(s): 7bcd4a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -50
app.py CHANGED
@@ -81,6 +81,22 @@ def cut(data0, wcs0, scale=1):
81
 
82
  return data, wcs
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  def decompose_cavity(pred, th2=0.7, amin=10):
85
  X, Y = pred.nonzero()
86
  data = np.array([X,Y]).reshape(2, -1)
@@ -159,61 +175,53 @@ if uploaded_file is not None:
159
 
160
  image = np.log10(data+1)
161
  plot_image(image, scale)
162
-
163
- if detect:
164
 
165
- with col4:
 
166
  st.markdown("""<style>[data-baseweb="select"] {margin-top: -36px;}</style>""", unsafe_allow_html=True)
167
  threshold = st.slider("", 0.0, 1.0, 0.0, 0.05, label_visibility="hidden")
 
 
 
168
 
169
- data, wcs = cut(data, wcs, scale=scale)
170
- image = np.log10(data+1)
171
-
172
- y_pred = 0
173
- for j in [0,1,2,3]:
174
- rotated = np.rot90(image, j)
175
- pred = model.predict(rotated.reshape(1, 128, 128, 1)).reshape(128 ,128)
176
- pred = np.rot90(pred, -j)
177
- y_pred += pred / 4
178
-
179
  # np.save("pred.npy", y_pred)
180
 
181
- # try: y_pred = np.load("pred.npy")
182
- # except: y_pred = np.zeros((128,128))
183
- try: y_pred
184
- except: y_pred = np.zeros((128,128))
185
- y_pred_th = np.where(y_pred > threshold, y_pred, 0)
186
- # np.save("thresh.npy", y_pred)
187
 
188
- plot_prediction(y_pred_th)
189
-
190
- if decompose:
191
- # y_pred = np.load("thresh.npy")
192
-
193
- cavs = decompose_cavity(y_pred_th)
194
-
195
- # ccd = CCDData(y_pred, unit="adu", wcs=wcs)
196
- # ccd.write(f"predictions/predicted.fits", overwrite=True)
197
- image_decomposed = np.zeros((128,128))
198
- for i, cav in enumerate(cavs):
199
- # ccd = CCDData(cav, unit="adu", wcs=wcs)
200
- # ccd.write(f"predictions/predicted_{i+1}.fits", overwrite=True)
201
- image_decomposed += (i+1) * np.where(cav > 0, 1, 0)
202
 
203
- # shutil.make_archive("predictions.zip", 'zip', "predictions")
204
- # np.save("decomposed.npy", image_decomposed)
205
-
206
- # try: image_decomposed = np.load("decomposed.npy")
207
- # except: image_decomposed = np.zeros((128,128))
208
- try: image_decomposed
209
- except: image_decomposed = np.zeros((128,128))
210
- plot_decomposed(image_decomposed)
211
-
212
- # shutil.make_archive("predictions", 'zip', "predictions")
213
-
214
- # with col6:
215
- # ccd = CCDData(y_pred, unit="adu", wcs=wcs)
216
- # # with open('predictions.zip', 'rb') as f:
217
- # # res = f.read()
218
- # st.markdown("""<style>[data-baseweb="select"] {margin-top: 16px;}</style>""", unsafe_allow_html=True)
219
- # download = st.download_button(label="Download", data=ccd, file_name='prediction.fits', mime="application/octet-stream")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  return data, wcs
83
 
84
+
85
+ @st.cache_data
86
+ def cut_n_predict(data, scale):
87
+ data, wcs = cut(data, wcs, scale=scale)
88
+ image = np.log10(data+1)
89
+
90
+ y_pred = 0
91
+ for j in [0,1,2,3]:
92
+ rotated = np.rot90(image, j)
93
+ pred = model.predict(rotated.reshape(1, 128, 128, 1)).reshape(128 ,128)
94
+ pred = np.rot90(pred, -j)
95
+ y_pred += pred / 4
96
+
97
+ return y_pred
98
+
99
+ # Define function to decomposed prediction into cavities
100
  def decompose_cavity(pred, th2=0.7, amin=10):
101
  X, Y = pred.nonzero()
102
  data = np.array([X,Y]).reshape(2, -1)
 
175
 
176
  image = np.log10(data+1)
177
  plot_image(image, scale)
 
 
178
 
179
+
180
+ with col4:
181
  st.markdown("""<style>[data-baseweb="select"] {margin-top: -36px;}</style>""", unsafe_allow_html=True)
182
  threshold = st.slider("", 0.0, 1.0, 0.0, 0.05, label_visibility="hidden")
183
+
184
+ if detect:
185
+ y_pred = predict(data, scale)
186
 
 
 
 
 
 
 
 
 
 
 
187
  # np.save("pred.npy", y_pred)
188
 
189
+ # try: y_pred = np.load("pred.npy")
190
+ # except: y_pred = np.zeros((128,128))
191
+ try: y_pred
192
+ except: y_pred = np.zeros((128,128))
193
+ y_pred_th = np.where(y_pred > threshold, y_pred, 0)
194
+ # np.save("thresh.npy", y_pred)
195
 
196
+ plot_prediction(y_pred_th)
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
+ if decompose:
199
+ # y_pred = np.load("thresh.npy")
200
+
201
+ cavs = decompose_cavity(y_pred_th)
202
+
203
+ # ccd = CCDData(y_pred, unit="adu", wcs=wcs)
204
+ # ccd.write(f"predictions/predicted.fits", overwrite=True)
205
+ image_decomposed = np.zeros((128,128))
206
+ for i, cav in enumerate(cavs):
207
+ # ccd = CCDData(cav, unit="adu", wcs=wcs)
208
+ # ccd.write(f"predictions/predicted_{i+1}.fits", overwrite=True)
209
+ image_decomposed += (i+1) * np.where(cav > 0, 1, 0)
210
+
211
+ # shutil.make_archive("predictions.zip", 'zip', "predictions")
212
+ # np.save("decomposed.npy", image_decomposed)
213
+
214
+ # try: image_decomposed = np.load("decomposed.npy")
215
+ # except: image_decomposed = np.zeros((128,128))
216
+ try: image_decomposed
217
+ except: image_decomposed = np.zeros((128,128))
218
+ plot_decomposed(image_decomposed)
219
+
220
+ # shutil.make_archive("predictions", 'zip', "predictions")
221
+
222
+ # with col6:
223
+ # ccd = CCDData(y_pred, unit="adu", wcs=wcs)
224
+ # # with open('predictions.zip', 'rb') as f:
225
+ # # res = f.read()
226
+ # st.markdown("""<style>[data-baseweb="select"] {margin-top: 16px;}</style>""", unsafe_allow_html=True)
227
+ # download = st.download_button(label="Download", data=ccd, file_name='prediction.fits', mime="application/octet-stream")