wenjun99 commited on
Commit
17836b2
·
verified ·
1 Parent(s): c6fed48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -4,10 +4,23 @@ import numpy as np
4
  import pandas as pd
5
  from streamlit_cropper import st_cropper
6
 
7
- # Simple app: convert user input into ASCII codes and binary labels
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def string_to_binary_labels(s: str) -> list[int]:
10
- bits: list[int] = []
11
  for char in s:
12
  ascii_code = ord(char)
13
  char_bits = [(ascii_code >> bit) & 1 for bit in range(7, -1, -1)]
@@ -59,22 +72,15 @@ def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, heig
59
  img = Image.fromarray(array, mode='RGB')
60
  return img
61
 
62
- # Predefined headers for the 32 mutation sites
63
- mutation_site_headers = [
64
- 3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
65
- 3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
66
- 4089, 4145, 4190, 4245, 4298, 4349, 4402, 4455,
67
- 4510, 4561, 4615, 4668, 4720, 4773, 4828, 4882
68
- ]
69
-
70
- # Load thresholds from file
71
- thresholds = pd.read_csv("Column_Thresholds.csv", index_col=0).squeeze()
72
 
73
  st.title("ASCII & Binary Label Converter")
74
 
75
- # Create tabs
76
- tab1, tab2, tab3 = st.tabs(["Text to Binary Labels", "Image to Binary Labels", "EF -> Binary"])
77
 
 
78
  with tab1:
79
  st.write("Enter text to see its ASCII codes and corresponding binary labels:")
80
  user_input = st.text_input("Text Input", value="DNA")
@@ -114,6 +120,7 @@ with tab1:
114
  mime="text/csv"
115
  )
116
 
 
117
  with tab2:
118
  st.write("Upload an image (JPG or PNG) to convert it into binary labels:")
119
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
@@ -122,8 +129,8 @@ with tab2:
122
  img = Image.open(uploaded_file)
123
  st.image(img, caption="Uploaded Image", use_column_width=True)
124
 
125
- st.subheader("Crop the image with drag and select (1:1 aspect ratio)")
126
- cropped_img = st_cropper(img, realtime_update=True, box_color='blue', aspect_ratio=1.0)
127
 
128
  st.image(cropped_img, caption="Cropped Image", use_column_width=True)
129
 
@@ -158,6 +165,7 @@ with tab2:
158
  mime="text/csv"
159
  )
160
 
 
161
  with tab3:
162
  st.write("Upload an Editing Frequency CSV or fill in manually:")
163
  ef_file = st.file_uploader("Upload Editing Frequency CSV", type=["csv"], key="ef")
@@ -172,23 +180,24 @@ with tab3:
172
 
173
  if st.button("Convert to Binary Labels"):
174
  common_cols = list(set(edited_df.columns) & set(thresholds.index))
175
- binary_part = edited_df[common_cols].ge(thresholds[common_cols]).astype(int)
176
- non_binary_part = edited_df.drop(columns=common_cols, errors='ignore')
 
 
177
  binary_df = pd.concat([non_binary_part, binary_part], axis=1)
178
 
179
  def highlight_binary(val):
180
  color = 'lightgreen' if val == 1 else 'lightcoral'
181
  return f'background-color: {color}'
182
 
183
- styled_binary_df = binary_df.style.applymap(highlight_binary, subset=common_cols)
184
 
185
  st.subheader("Binary Labels")
186
  st.dataframe(styled_binary_df)
 
187
  st.download_button(
188
  label="Download Binary Labels Table as CSV",
189
  data=binary_df.to_csv(index=False),
190
  file_name="ef_binary_labels_table.csv",
191
  mime="text/csv"
192
  )
193
-
194
- # Future: integrate DNA editor mapping for each mutation site here
 
4
  import pandas as pd
5
  from streamlit_cropper import st_cropper
6
 
7
+ # Predefined headers for the 32 mutation sites
8
+ mutation_site_headers = [
9
+ 3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
10
+ 3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
11
+ 4089, 4145, 4190, 4245, 4298, 4349, 4402, 4455,
12
+ 4510, 4561, 4615, 4668, 4720, 4773, 4828, 4882
13
+ ]
14
+
15
+ # Load thresholds from file
16
+ thresholds = pd.read_csv("Column_Thresholds.csv", index_col=0).squeeze()
17
+
18
+ # -----------------------------------------
19
+ # Utility functions
20
+ # -----------------------------------------
21
 
22
  def string_to_binary_labels(s: str) -> list[int]:
23
+ bits = []
24
  for char in s:
25
  ascii_code = ord(char)
26
  char_bits = [(ascii_code >> bit) & 1 for bit in range(7, -1, -1)]
 
72
  img = Image.fromarray(array, mode='RGB')
73
  return img
74
 
75
+ # -----------------------------------------
76
+ # Streamlit App
77
+ # -----------------------------------------
 
 
 
 
 
 
 
78
 
79
  st.title("ASCII & Binary Label Converter")
80
 
81
+ tab1, tab2, tab3 = st.tabs(["Text to Binary Labels", "Image to Binary Labels", "EF → Binary"])
 
82
 
83
+ # ================= Tab 1 ===================
84
  with tab1:
85
  st.write("Enter text to see its ASCII codes and corresponding binary labels:")
86
  user_input = st.text_input("Text Input", value="DNA")
 
120
  mime="text/csv"
121
  )
122
 
123
+ # ================= Tab 2 ===================
124
  with tab2:
125
  st.write("Upload an image (JPG or PNG) to convert it into binary labels:")
126
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
 
129
  img = Image.open(uploaded_file)
130
  st.image(img, caption="Uploaded Image", use_column_width=True)
131
 
132
+ st.subheader("Crop the image with drag and select (Free aspect ratio)")
133
+ cropped_img = st_cropper(img, realtime_update=True, box_color='blue', aspect_ratio=None)
134
 
135
  st.image(cropped_img, caption="Cropped Image", use_column_width=True)
136
 
 
165
  mime="text/csv"
166
  )
167
 
168
+ # ================= Tab 3 ===================
169
  with tab3:
170
  st.write("Upload an Editing Frequency CSV or fill in manually:")
171
  ef_file = st.file_uploader("Upload Editing Frequency CSV", type=["csv"], key="ef")
 
180
 
181
  if st.button("Convert to Binary Labels"):
182
  common_cols = list(set(edited_df.columns) & set(thresholds.index))
183
+ numeric_cols = edited_df[common_cols].select_dtypes(include=[np.number]).columns.tolist()
184
+
185
+ binary_part = edited_df[numeric_cols].ge(thresholds[numeric_cols]).astype(int)
186
+ non_binary_part = edited_df.drop(columns=numeric_cols, errors='ignore')
187
  binary_df = pd.concat([non_binary_part, binary_part], axis=1)
188
 
189
  def highlight_binary(val):
190
  color = 'lightgreen' if val == 1 else 'lightcoral'
191
  return f'background-color: {color}'
192
 
193
+ styled_binary_df = binary_df.style.applymap(highlight_binary, subset=numeric_cols)
194
 
195
  st.subheader("Binary Labels")
196
  st.dataframe(styled_binary_df)
197
+
198
  st.download_button(
199
  label="Download Binary Labels Table as CSV",
200
  data=binary_df.to_csv(index=False),
201
  file_name="ef_binary_labels_table.csv",
202
  mime="text/csv"
203
  )