Spaces:
Sleeping
Sleeping
File size: 13,588 Bytes
0f8ddfb 07a5d20 126fff1 81dbe3d e59d9f1 785d3cd 8e18767 17836b2 8e18767 a26308f 17836b2 8e18767 9c1b1da 8e18767 a26308f 9c1b1da 54f693c 0f8ddfb cc35c28 0f8ddfb 17836b2 0f8ddfb cc35c28 0f8ddfb ff2b00a cc35c28 ff2b00a 1e12d5e cc35c28 ff2b00a 07a5d20 2d22901 07a5d20 181eb38 126fff1 56ff5cb 07a5d20 56ff5cb 126fff1 2d22901 56ff5cb 2d22901 56ff5cb 2d22901 56ff5cb 2d22901 181eb38 54f693c 81f62ec 0f8ddfb 17836b2 126fff1 54f693c 126fff1 54f693c 126fff1 55b708c 54f693c 126fff1 1e12d5e 54f693c 1e12d5e 54f693c 07a5d20 05bc090 1e12d5e 785d3cd 87ee564 126fff1 54f693c 126fff1 54f693c 81dbe3d 54f693c 126fff1 54f693c 126fff1 54f693c 0f8ddfb 54f693c 20981db 54f693c 785d3cd 54f693c 785d3cd f37dc6c 5dc1952 785d3cd f37dc6c 785d3cd f37dc6c 785d3cd 54f693c 785d3cd 54f693c 785d3cd 17836b2 54f693c 6a96058 785d3cd 54f693c 785d3cd 4f6586f 785d3cd |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
import streamlit as st
from PIL import Image, ImageFilter
import numpy as np
import pandas as pd
from streamlit_cropper import st_cropper
# Mutation site headers removed 3614,
mutation_site_headers_actual = [
3244, 3297, 3350, 3399, 3455, 3509, 3562,
3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
4089, 4145, 4190, 4245, 4298, 4349, 4402, 4455,
4510, 4561, 4615, 4668, 4720, 4773, 4828, 4882
]
# Thresholds for each mutation site removed 3614: 0.091557752,
thresholds_actual = pd.Series({
3244: 1.094293328, 3297: 0.924916122, 3350: 0.664586629, 3399: 0.91573613,
3455: 1.300869714, 3509: 1.821975901, 3562: 1.178862418,
3665: 0.298697327, 3720: 0.58379781, 3773: 0.891088481, 3824: 1.145509641,
3879: 0.81833191, 3933: 2.93084335, 3985: 1.593758847, 4039: 0.966055013,
4089: 1.465671338, 4145: 0.30309335, 4190: 1.321615138, 4245: 1.709752495,
4298: 0.868534701, 4349: 1.222907645, 4402: 0.58873557, 4455: 1.185522985,
4510: 1.266797682, 4561: 1.109913024, 4615: 1.181106084, 4668: 1.408533949,
4720: 0.714151142, 4773: 1.471959437, 4828: 0.95879943, 4882: 1.464503885
})
# Mutation site headers reordered: 4402 to 3244, 4882 to 4455
mutation_site_headers = [
4402, 4349, 4298, 4245, 4190, 4145, 4089, 4039,
3985, 3933, 3879, 3824, 3773, 3720, 3665,
3562, 3509, 3455, 3399, 3350, 3297, 3244, # 1β23
4882, 4828, 4773, 4720, 4668, 4615, 4561, 4510, 4455 # 24β32
]
# Thresholds reordered accordingly
thresholds = pd.Series({
4402: 0.58873557, 4349: 1.222907645, 4298: 0.868534701, 4245: 1.709752495,
4190: 1.321615138, 4145: 0.30309335, 4089: 1.465671338, 4039: 0.966055013,
3985: 1.593758847, 3933: 2.93084335, 3879: 0.81833191, 3824: 1.145509641,
3773: 0.891088481, 3720: 0.58379781, 3665: 0.298697327,
3562: 1.178862418, 3509: 1.821975901, 3455: 1.300869714, 3399: 0.91573613,
3350: 0.664586629, 3297: 0.924916122, 3244: 1.094293328,
4882: 1.464503885, 4828: 0.95879943, 4773: 1.471959437, 4720: 0.714151142,
4668: 1.408533949, 4615: 1.181106084, 4561: 1.109913024, 4510: 1.266797682, 4455: 1.185522985
})
# === Utility functions ===
# Voyager ASCII 6-bit conversion table
voyager_table = {
i: ch for i, ch in enumerate([
' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
'3', '4', '5', '6', '7', '8', '9', '.', '(', ')',
'+', '-', '*', '/', '=', '$', '!', ':', '%', '"',
'#', '@', '\'', '?', '&'
])
}
reverse_voyager_table = {v: k for k, v in voyager_table.items()}
# === Utility functions ===
def string_to_binary_labels(s: str) -> list[int]:
bits = []
for char in s:
val = reverse_voyager_table.get(char.upper(), 0)
char_bits = [(val >> bit) & 1 for bit in range(5, -1, -1)]
bits.extend(char_bits)
return bits
def binary_labels_to_string(bits: list[int]) -> str:
chars = []
for i in range(0, len(bits), 6):
chunk = bits[i:i+6]
if len(chunk) < 6:
chunk += [0] * (6 - len(chunk))
val = sum(b << (5 - j) for j, b in enumerate(chunk))
chars.append(voyager_table.get(val, '?'))
return ''.join(chars)
# def string_to_binary_labels(s: str) -> list[int]:
# bits = []
# for char in s:
# ascii_code = ord(char)
# char_bits = [(ascii_code >> bit) & 1 for bit in range(7, -1, -1)]
# bits.extend(char_bits)
# return bits
# def binary_labels_to_string(bits: list[int]) -> str:
# chars = []
# for i in range(0, len(bits), 8):
# byte = bits[i:i+8]
# if len(byte) < 8:
# byte += [0] * (8 - len(byte))
# ascii_val = sum(b << (7 - j) for j, b in enumerate(byte))
# chars.append(chr(ascii_val))
# return ''.join(chars)
def clean_image(img: Image.Image, min_size: int = 256) -> Image.Image:
img = img.convert("RGB")
if img.width < min_size or img.height < min_size:
img = img.resize((min_size, min_size))
img = img.filter(ImageFilter.GaussianBlur(radius=1))
return img
def image_to_binary_labels_rgb(img: Image.Image, max_pixels: int = 256) -> list[int]:
img = clean_image(img)
img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
img_array = np.array(img)
flat_pixels = img_array.reshape(-1, 3)
bits = []
for pixel in flat_pixels:
for channel in pixel:
channel_bits = [(channel >> bit) & 1 for bit in range(7, -1, -1)]
bits.extend(channel_bits)
return bits
def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
total_pixels = len(binary_labels) // 24
if width is None or height is None:
side = int(np.ceil(np.sqrt(total_pixels)))
width = height = side
needed_pixels = width * height
needed_bits = needed_pixels * 24
if len(binary_labels) < needed_bits:
binary_labels += [0] * (needed_bits - len(binary_labels))
pixels = []
for i in range(0, needed_bits, 24):
r_bits = binary_labels[i:i+8]
g_bits = binary_labels[i+8:i+16]
b_bits = binary_labels[i+16:i+24]
r = sum(b << (7-j) for j, b in enumerate(r_bits))
g = sum(b << (7-j) for j, b in enumerate(g_bits))
b = sum(b << (7-j) for j, b in enumerate(b_bits))
pixels.append((r, g, b))
array = np.array(pixels, dtype=np.uint8).reshape((height, width, 3))
img = Image.fromarray(array, mode='RGB')
return img
# === Streamlit App ===
st.title("ASCII & Binary Label Converter")
tab1, tab2, tab3 = st.tabs(["Text to Binary Labels", "Image to Binary Labels", "EF β Binary"])
# Tab 1: Text to Binary
with tab1:
user_input = st.text_input("Enter text", value="DNA")
if user_input:
ascii_codes = [ord(c) for c in user_input]
binary_labels = string_to_binary_labels(user_input)
st.subheader("ASCII Codes")
st.write(ascii_codes)
st.subheader("Binary Labels per Character")
grouped = [binary_labels[i:i+6] for i in range(0, len(binary_labels), 6)]
for i, bits in enumerate(grouped):
st.write(f"'{user_input[i]}' β {bits}")
st.subheader("Binary Labels (31-bit groups)")
groups = []
for i in range(0, len(binary_labels), 31):
group = binary_labels[i:i+31]
group += [0] * (31 - len(group))
groups.append(group + [sum(group)])
df = pd.DataFrame(groups, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
st.dataframe(df)
st.download_button("Download as CSV", df.to_csv(index=False), "text_32_binary_labels.csv")
# Additional table with ascending mutation site headers (3244 to 4455)
ascending_headers = sorted([h for h in mutation_site_headers if h <= 4455])
df_sorted = df[[str(h) for h in ascending_headers if str(h) in df.columns]]
st.subheader("Binary Labels (Ascending Order 3244 β 4455)")
st.dataframe(df_sorted)
st.download_button("Download Ascending Order CSV", df_sorted.to_csv(index=False), "text_binary_labels_ascending.csv")
# st.subheader("Binary Labels (27-bit groups)")
# groups = []
# for i in range(0, len(binary_labels), 27):
# group = binary_labels[i:i+27]
# group += [0] * (27 - len(group))
# groups.append(group + [sum(group)])
# df_27 = pd.DataFrame(groups, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
# st.dataframe(df_27)
# st.download_button("Download as CSV", df_27.to_csv(index=False), "text_27_binary_labels.csv")
# Tab 2: Image to Binary
with tab2:
uploaded = st.file_uploader("Upload an image (jpg/png)", type=["jpg", "jpeg", "png"])
if uploaded:
img = Image.open(uploaded)
st.image(img, caption="Original", use_column_width=True)
cropped = st_cropper(img, realtime_update=True, box_color="blue", aspect_ratio=None)
st.image(cropped, caption="Cropped", use_column_width=True)
max_pixels = st.slider("Max pixels to encode", 32, 1024, 256, 32)
binary_labels = image_to_binary_labels_rgb(cropped, max_pixels=max_pixels)
st.subheader("Binary Labels from Image")
groups = []
for i in range(0, len(binary_labels), 32):
group = binary_labels[i:i+32]
group += [0] * (32 - len(group))
groups.append(group + [sum(group)])
df = pd.DataFrame(groups, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
st.dataframe(df)
st.subheader("Reconstructed Image")
recon = binary_labels_to_rgb_image(binary_labels)
st.image(recon, caption="Reconstructed", use_column_width=True)
st.download_button("Download CSV", df.to_csv(index=False), "image_binary_labels.csv")
# Tab 3: EF β Binary
with tab3:
st.write("Upload an Editing Frequency CSV or enter manually:")
st.write("**Note:** Please upload CSV files **without column headers**, in ascending order from 3244 to 4455.")
ef_file = st.file_uploader("Upload EF CSV", type=["csv"], key="ef")
ascending_input_headers = sorted([h for h in mutation_site_headers if 3244 <= h <= 4455])
if ef_file:
ef_df = pd.read_csv(ef_file, header=None)
ef_df.columns = [str(site) for site in ascending_input_headers]
else:
ef_df = pd.DataFrame(columns=[str(site) for site in ascending_input_headers])
edited_df = st.data_editor(ef_df, num_rows="dynamic")
if st.button("Convert to Binary Labels"):
# Use ascending headers to create binary first
binary_part = pd.DataFrame()
for col in ascending_input_headers:
col_str = str(col)
threshold = thresholds[col]
binary_part[col_str] = (edited_df[col_str].astype(float) >= threshold).astype(int)
# Rearranged for output: custom order from mutation_site_headers
binary_reordered = binary_part[[str(h) for h in mutation_site_headers if str(h) in binary_part.columns]]
def color_binary(val):
if val == 1: return "background-color: lightgreen"
if val == 0: return "background-color: lightcoral"
return ""
st.subheader("Binary Labels (Reordered 4402β3244, 4882β4455)")
styled = binary_reordered.style.applymap(color_binary)
st.dataframe(styled)
st.download_button("Download CSV", binary_reordered.to_csv(index=False), "ef_binary_labels.csv")
# === NEW: Continuous decoding across rows ===
all_bits = binary_reordered.values.flatten().tolist()
decoded_string = binary_labels_to_string(all_bits)
st.subheader("Decoded String (continuous across rows)")
st.write(decoded_string)
# Optional: ascending order output
binary_ascending = binary_part[[str(h) for h in ascending_input_headers if str(h) in binary_part.columns]]
st.subheader("Binary Labels (Ascending 3244β4455)")
st.dataframe(binary_ascending)
st.download_button("Download Ascending Order CSV", binary_ascending.to_csv(index=False), "ef_binary_labels_ascending.csv")
# # Tab 3: EF β Binary
# with tab3:
# st.write("Upload an Editing Frequency CSV or enter manually:")
# st.write("**Note:** Please upload CSV files **without column headers**. Just the 31 editing frequencies per row.")
# ef_file = st.file_uploader("Upload EF CSV", type=["csv"], key="ef")
# if ef_file:
# # Read CSV without headers and assign mutation site headers
# ef_df = pd.read_csv(ef_file, header=None)
# ef_df.columns = [str(site) for site in mutation_site_headers]
# else:
# ef_df = pd.DataFrame(columns=[str(site) for site in mutation_site_headers])
# edited_df = st.data_editor(ef_df, num_rows="dynamic")
# if st.button("Convert to Binary Labels"):
# int_map = {str(k): k for k in thresholds.index}
# matching_cols = [col for col in edited_df.columns if col in int_map]
# binary_part = pd.DataFrame()
# for col in matching_cols:
# col_threshold = thresholds[int_map[col]]
# binary_part[col] = (edited_df[col].astype(float) >= col_threshold).astype(int)
# non_binary_part = edited_df.drop(columns=matching_cols, errors='ignore')
# binary_df = pd.concat([non_binary_part, binary_part], axis=1)
# def color_binary(val):
# if val == 1: return "background-color: lightgreen"
# if val == 0: return "background-color: lightcoral"
# return ""
# st.subheader("Binary Labels")
# styled = binary_df.style.applymap(color_binary, subset=matching_cols)
# st.dataframe(styled)
# st.download_button("Download CSV", binary_df.to_csv(index=False), "ef_binary_labels.csv")
# # Convert to bitstrings and strings
# binary_strings = []
# decoded_strings = []
# for _, row in binary_part.iterrows():
# bitlist = row.values.tolist()
# bitstring = ''.join(str(b) for b in bitlist)
# binary_strings.append(bitstring)
# decoded_strings.append(binary_labels_to_string(bitlist))
# st.subheader("Binary as Bitstrings")
# for b in binary_strings:
# st.code(b)
# st.subheader("Decoded Voyager Strings")
# for s in decoded_strings:
# st.write(s)
|