updated forseparating singlecell and spheroid
Browse files28 unmodified lines29303132333435363738394041424344453 unmodified lines4950515253545556575859606162636445 unmodified lines1101111121131141151161171181191201211221231241251263 unmodified lines13013113213313413513613713813914014114260 unmodified lines20320420520628 unmodified linesst.title("🔬 Shape2Force (S2F)")st.caption("Predict force maps from bright field microscopy images")# Foldersckp_folder = os.path.join(S2F_ROOT, "ckp")sample_folder = os.path.join(S2F_ROOT, "samples")ckp_files = []if os.path.isdir(ckp_folder):ckp_files = sorted([f for f in os.listdir(ckp_folder) if f.endswith(".pth")])SAMPLE_EXTENSIONS = (".tif", ".tiff", ".png", ".jpg", ".jpeg")sample_files = []if os.path.isdir(sample_folder):sample_files = sorted([f for f in os.listdir(sample_folder)if f.lower().endswith(SAMPLE_EXTENSIONS)])# Sidebar: model configurationwith st.sidebar:3 unmodified lines["single_cell", "spheroid"],format_func=lambda x: "Single cell" if x == "single_cell" else "Spheroid",horizontal=False,)if ckp_files:checkpoint = st.selectbox("Checkpoint",ckp_files,help="Select a .pth file from the ckp folder",)else:st.warning("No .pth files in ckp/ folder. Add checkpoints to load.")checkpoint = Nonesubstrate_config = None45 unmodified linesimg = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)uploaded.seek(0) # reset for potential re-readelse:if sample_files:selected_sample = st.selectbox("Select sample image",sample_files,format_func=lambda x: x,)if selected_sample:sample_path = os.path.join(sample_folder, selected_sample)img = cv2.imread(sample_path, cv2.IMREAD_GRAYSCALE)# Show sample thumbnailsst.caption("Sample images (add more to the `samples/` folder)")n_cols = min(4, len(sample_files))cols = st.columns(n_cols)for i, fname in enumerate(sample_files[:8]): # show up to 83 unmodified linesif sample_img is not None:st.image(sample_img, caption=fname, width='content')else:st.info("No sample images found. Add images to the `samples/` folder, or use Upload.")run = st.button("Run prediction", type="primary")has_image = img is not Noneif run and checkpoint and has_image:st.markdown(f"**Using checkpoint:** `{checkpoint}`")with st.spinner("Loading model and predicting..."):try:from predictor import S2FPredictor60 unmodified lines# Footerst.sidebar.divider()st.sidebar.caption("Place .pth checkpoints in ckp/, sample images in samples/")28 unmodified lines293031323334353637383940414243444546474849505152535455565758596061626364653 unmodified lines6970717273747576777879808182838485868788899045 unmodified lines1361371381391401411421431441451461471481491501511521531541551563 unmodified lines16016116216316416516616716816917017117260 unmodified lines23323423523628 unmodified linesst.title("🔬 Shape2Force (S2F)")st.caption("Predict force maps from bright field microscopy images")# Folders: checkpoints in subfolders by model type (single_cell / spheroid)ckp_base = os.path.join(S2F_ROOT, "ckp")# Fallback: use project root ckp when running from S2F repo (ckp at S2F/ckp/)if not os.path.isdir(ckp_base):project_root = os.path.dirname(S2F_ROOT)if os.path.isdir(os.path.join(project_root, "ckp")):ckp_base = os.path.join(project_root, "ckp")ckp_single_cell = os.path.join(ckp_base, "single_cell")ckp_spheroid = os.path.join(ckp_base, "spheroid")sample_base = os.path.join(S2F_ROOT, "samples")sample_single_cell = os.path.join(sample_base, "single_cell")sample_spheroid = os.path.join(sample_base, "spheroid")SAMPLE_EXTENSIONS = (".tif", ".tiff", ".png", ".jpg", ".jpeg")def get_ckp_files_for_model(model_type):"""Return list of .pth files in the checkpoint folder for the given model type."""folder = ckp_single_cell if model_type == "single_cell" else ckp_spheroidif os.path.isdir(folder):return sorted([f for f in os.listdir(folder) if f.endswith(".pth")])return []def get_sample_files_for_model(model_type):"""Return list of sample images in the sample folder for the given model type."""folder = sample_single_cell if model_type == "single_cell" else sample_spheroidif os.path.isdir(folder):return sorted([f for f in os.listdir(folder)if f.lower().endswith(SAMPLE_EXTENSIONS)])return []# Sidebar: model configurationwith st.sidebar:3 unmodified lines["single_cell", "spheroid"],format_func=lambda x: "Single cell" if x == "single_cell" else "Spheroid",horizontal=False,help="Single cell: substrate-aware force prediction. Spheroid: spheroid force maps.",)st.caption(f"Inference mode: **{'Single cell' if model_type == 'single_cell' else 'Spheroid'}**")ckp_files = get_ckp_files_for_model(model_type)ckp_folder = ckp_single_cell if model_type == "single_cell" else ckp_spheroidckp_subfolder_name = "single_cell" if model_type == "single_cell" else "spheroid"if ckp_files:checkpoint = st.selectbox("Checkpoint",ckp_files,help=f"Select a .pth file from ckp/{ckp_subfolder_name}/",)else:st.warning(f"No .pth files in ckp/{ckp_subfolder_name}/. Add checkpoints to load.")checkpoint = Nonesubstrate_config = None45 unmodified linesimg = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)uploaded.seek(0) # reset for potential re-readelse:sample_files = get_sample_files_for_model(model_type)sample_folder = sample_single_cell if model_type == "single_cell" else sample_spheroidsample_subfolder_name = "single_cell" if model_type == "single_cell" else "spheroid"if sample_files:selected_sample = st.selectbox("Select sample image",sample_files,format_func=lambda x: x,key=f"sample_{model_type}",)if selected_sample:sample_path = os.path.join(sample_folder, selected_sample)img = cv2.imread(sample_path, cv2.IMREAD_GRAYSCALE)# Show sample thumbnails (filtered by model type)st.caption(f"Sample images from `samples/{sample_subfolder_name}/`")n_cols = min(4, len(sample_files))cols = st.columns(n_cols)for i, fname in enumerate(sample_files[:8]): # show up to 83 unmodified linesif sample_img is not None:st.image(sample_img, caption=fname, width='content')else:st.info(f"No sample images in samples/{sample_subfolder_name}/. Add images or use Upload.")run = st.button("Run prediction", type="primary")has_image = img is not Noneif run and checkpoint and has_image:st.markdown(f"**Using checkpoint:** `ckp/{ckp_subfolder_name}/{checkpoint}`")with st.spinner("Loading model and predicting..."):try:from predictor import S2FPredictor60 unmodified lines# Footerst.sidebar.divider()st.sidebar.caption("Checkpoints: ckp/single_cell/ and ckp/spheroid/. Samples: samples/single_cell/ and samples/spheroid/")
S2FApp/app.py+47-17