Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -144,7 +144,7 @@ def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, heig
|
|
144 |
# === Streamlit App ===
|
145 |
|
146 |
st.title("ASCII & Binary Label Converter")
|
147 |
-
tab1, tab2
|
148 |
|
149 |
# Tab 1: Text to Binary
|
150 |
with tab1:
|
@@ -170,7 +170,7 @@ with tab1:
|
|
170 |
|
171 |
df = pd.DataFrame(groups, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
|
172 |
st.dataframe(df)
|
173 |
-
st.download_button("Download as CSV", df.to_csv(index=False), "
|
174 |
|
175 |
# Additional table with ascending mutation site headers (3244 to 4455)
|
176 |
ascending_headers = sorted([h for h in mutation_site_headers if h <= 4455])
|
@@ -191,57 +191,34 @@ with tab1:
|
|
191 |
# st.dataframe(df_27)
|
192 |
# st.download_button("Download as CSV", df_27.to_csv(index=False), "text_27_binary_labels.csv")
|
193 |
|
194 |
-
# Tab 2: Image to Binary
|
195 |
-
with tab2:
|
196 |
-
uploaded = st.file_uploader("Upload an image (jpg/png)", type=["jpg", "jpeg", "png"])
|
197 |
-
if uploaded:
|
198 |
-
img = Image.open(uploaded)
|
199 |
-
st.image(img, caption="Original", use_column_width=True)
|
200 |
-
cropped = st_cropper(img, realtime_update=True, box_color="blue", aspect_ratio=None)
|
201 |
-
st.image(cropped, caption="Cropped", use_column_width=True)
|
202 |
-
|
203 |
-
max_pixels = st.slider("Max pixels to encode", 32, 1024, 256, 32)
|
204 |
-
binary_labels = image_to_binary_labels_rgb(cropped, max_pixels=max_pixels)
|
205 |
-
|
206 |
-
st.subheader("Binary Labels from Image")
|
207 |
-
groups = []
|
208 |
-
for i in range(0, len(binary_labels), 32):
|
209 |
-
group = binary_labels[i:i+32]
|
210 |
-
group += [0] * (32 - len(group))
|
211 |
-
groups.append(group + [sum(group)])
|
212 |
-
df = pd.DataFrame(groups, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
|
213 |
-
st.dataframe(df)
|
214 |
-
|
215 |
-
st.subheader("Reconstructed Image")
|
216 |
-
recon = binary_labels_to_rgb_image(binary_labels)
|
217 |
-
st.image(recon, caption="Reconstructed", use_column_width=True)
|
218 |
-
st.download_button("Download CSV", df.to_csv(index=False), "image_binary_labels.csv")
|
219 |
-
|
220 |
# Tab 3: EF β Binary
|
221 |
-
with
|
222 |
st.write("Upload an Editing Frequency CSV or enter manually:")
|
223 |
st.write("**Note:** Please upload CSV files **without column headers**, in ascending order from 3244 to 4455.")
|
224 |
ef_file = st.file_uploader("Upload EF CSV", type=["csv"], key="ef")
|
225 |
|
226 |
ascending_input_headers = sorted([h for h in mutation_site_headers if 3244 <= h <= 4455])
|
|
|
227 |
|
228 |
if ef_file:
|
229 |
ef_df = pd.read_csv(ef_file, header=None)
|
230 |
ef_df.columns = [str(site) for site in ascending_input_headers]
|
|
|
|
|
231 |
else:
|
232 |
-
ef_df = pd.DataFrame(columns=[str(site) for site in ascending_input_headers])
|
233 |
|
234 |
edited_df = st.data_editor(ef_df, num_rows="dynamic")
|
235 |
|
236 |
if st.button("Convert to Binary Labels"):
|
237 |
-
# Use ascending headers to create binary first
|
238 |
binary_part = pd.DataFrame()
|
239 |
for col in ascending_input_headers:
|
240 |
col_str = str(col)
|
241 |
threshold = thresholds[col]
|
242 |
binary_part[col_str] = (edited_df[col_str].astype(float) >= threshold).astype(int)
|
|
|
|
|
243 |
|
244 |
-
# Rearranged for output: custom order from mutation_site_headers
|
245 |
binary_reordered = binary_part[[str(h) for h in mutation_site_headers if str(h) in binary_part.columns]]
|
246 |
|
247 |
def color_binary(val):
|
@@ -254,13 +231,11 @@ with tab3:
|
|
254 |
st.dataframe(styled)
|
255 |
st.download_button("Download CSV", binary_reordered.to_csv(index=False), "ef_binary_labels.csv")
|
256 |
|
257 |
-
# === NEW: Continuous decoding across rows ===
|
258 |
all_bits = binary_reordered.values.flatten().tolist()
|
259 |
decoded_string = binary_labels_to_string(all_bits)
|
260 |
st.subheader("Decoded String (continuous across rows)")
|
261 |
st.write(decoded_string)
|
262 |
|
263 |
-
# Optional: ascending order output
|
264 |
binary_ascending = binary_part[[str(h) for h in ascending_input_headers if str(h) in binary_part.columns]]
|
265 |
st.subheader("Binary Labels (Ascending 3244β4455)")
|
266 |
st.dataframe(binary_ascending)
|
@@ -321,3 +296,30 @@ with tab3:
|
|
321 |
# st.subheader("Decoded Voyager Strings")
|
322 |
# for s in decoded_strings:
|
323 |
# st.write(s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
# === Streamlit App ===
|
145 |
|
146 |
st.title("ASCII & Binary Label Converter")
|
147 |
+
tab1, tab2 = st.tabs(["Text to Binary Labels (31)", "EF β Binary (31)"])
|
148 |
|
149 |
# Tab 1: Text to Binary
|
150 |
with tab1:
|
|
|
170 |
|
171 |
df = pd.DataFrame(groups, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
|
172 |
st.dataframe(df)
|
173 |
+
st.download_button("Download as CSV", df.to_csv(index=False), "text_31_binary_labels.csv")
|
174 |
|
175 |
# Additional table with ascending mutation site headers (3244 to 4455)
|
176 |
ascending_headers = sorted([h for h in mutation_site_headers if h <= 4455])
|
|
|
191 |
# st.dataframe(df_27)
|
192 |
# st.download_button("Download as CSV", df_27.to_csv(index=False), "text_27_binary_labels.csv")
|
193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
# Tab 3: EF β Binary
|
195 |
+
with tab2:
|
196 |
st.write("Upload an Editing Frequency CSV or enter manually:")
|
197 |
st.write("**Note:** Please upload CSV files **without column headers**, in ascending order from 3244 to 4455.")
|
198 |
ef_file = st.file_uploader("Upload EF CSV", type=["csv"], key="ef")
|
199 |
|
200 |
ascending_input_headers = sorted([h for h in mutation_site_headers if 3244 <= h <= 4455])
|
201 |
+
high_index_headers = sorted([h for h in mutation_site_headers if h >= 4480])
|
202 |
|
203 |
if ef_file:
|
204 |
ef_df = pd.read_csv(ef_file, header=None)
|
205 |
ef_df.columns = [str(site) for site in ascending_input_headers]
|
206 |
+
for h in high_index_headers:
|
207 |
+
ef_df[str(h)] = 0 # add dummy columns for high index as 0
|
208 |
else:
|
209 |
+
ef_df = pd.DataFrame(columns=[str(site) for site in ascending_input_headers + high_index_headers])
|
210 |
|
211 |
edited_df = st.data_editor(ef_df, num_rows="dynamic")
|
212 |
|
213 |
if st.button("Convert to Binary Labels"):
|
|
|
214 |
binary_part = pd.DataFrame()
|
215 |
for col in ascending_input_headers:
|
216 |
col_str = str(col)
|
217 |
threshold = thresholds[col]
|
218 |
binary_part[col_str] = (edited_df[col_str].astype(float) >= threshold).astype(int)
|
219 |
+
for col in high_index_headers:
|
220 |
+
binary_part[str(col)] = 0
|
221 |
|
|
|
222 |
binary_reordered = binary_part[[str(h) for h in mutation_site_headers if str(h) in binary_part.columns]]
|
223 |
|
224 |
def color_binary(val):
|
|
|
231 |
st.dataframe(styled)
|
232 |
st.download_button("Download CSV", binary_reordered.to_csv(index=False), "ef_binary_labels.csv")
|
233 |
|
|
|
234 |
all_bits = binary_reordered.values.flatten().tolist()
|
235 |
decoded_string = binary_labels_to_string(all_bits)
|
236 |
st.subheader("Decoded String (continuous across rows)")
|
237 |
st.write(decoded_string)
|
238 |
|
|
|
239 |
binary_ascending = binary_part[[str(h) for h in ascending_input_headers if str(h) in binary_part.columns]]
|
240 |
st.subheader("Binary Labels (Ascending 3244β4455)")
|
241 |
st.dataframe(binary_ascending)
|
|
|
296 |
# st.subheader("Decoded Voyager Strings")
|
297 |
# for s in decoded_strings:
|
298 |
# st.write(s)
|
299 |
+
|
300 |
+
|
301 |
+
# # Tab 2: Image to Binary
|
302 |
+
# with tab2:
|
303 |
+
# uploaded = st.file_uploader("Upload an image (jpg/png)", type=["jpg", "jpeg", "png"])
|
304 |
+
# if uploaded:
|
305 |
+
# img = Image.open(uploaded)
|
306 |
+
# st.image(img, caption="Original", use_column_width=True)
|
307 |
+
# cropped = st_cropper(img, realtime_update=True, box_color="blue", aspect_ratio=None)
|
308 |
+
# st.image(cropped, caption="Cropped", use_column_width=True)
|
309 |
+
|
310 |
+
# max_pixels = st.slider("Max pixels to encode", 32, 1024, 256, 32)
|
311 |
+
# binary_labels = image_to_binary_labels_rgb(cropped, max_pixels=max_pixels)
|
312 |
+
|
313 |
+
# st.subheader("Binary Labels from Image")
|
314 |
+
# groups = []
|
315 |
+
# for i in range(0, len(binary_labels), 32):
|
316 |
+
# group = binary_labels[i:i+32]
|
317 |
+
# group += [0] * (32 - len(group))
|
318 |
+
# groups.append(group + [sum(group)])
|
319 |
+
# df = pd.DataFrame(groups, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
|
320 |
+
# st.dataframe(df)
|
321 |
+
|
322 |
+
# st.subheader("Reconstructed Image")
|
323 |
+
# recon = binary_labels_to_rgb_image(binary_labels)
|
324 |
+
# st.image(recon, caption="Reconstructed", use_column_width=True)
|
325 |
+
# st.download_button("Download CSV", df.to_csv(index=False), "image_binary_labels.csv")
|