Skip to content

Commit

Permalink
Replace from source dependency bunch with on-the-fly git clone for PyPI
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusge committed Feb 3, 2025
1 parent f6a04ad commit 51441b9
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 252 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fundus_image_toolbox/fovea_od_localization/models/*
fundus_image_toolbox/quality_prediction/models/*
fundus_image_toolbox/quality_prediction/data/*
fundus_image_toolbox/vessel_segmentation/segmentation/*
fundus_image_toolbox/vessel_segmentation/bunch/
fundus.jpg

# Others
Expand Down
16 changes: 9 additions & 7 deletions fundus_image_toolbox/vessel_segmentation/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ def clone_repo(
branch="main",
target_dir=f"./segmentation",
):
# Clone Segmentation Quality Control repo
os.makedirs(target_dir, exist_ok=True)
if not os.listdir(target_dir):
print(
f"The vessel segmentation folder is missing. Downloading it from {link}..."
f"Module missing, downloading it from {link}..."
)
pwd = os.getcwd()
os.system(f"git clone {link} {target_dir}")
Expand All @@ -33,6 +32,9 @@ def clone_repo(
pass
os.chdir(pwd)

def adjust_imports(target_dir):
# Make imports of segmentation module relative
if os.path.exists(target_dir):
print("Adjusting imports...")
add_dots_to_imports_in_folder(target_dir)
replace_args(target_dir)
Expand Down Expand Up @@ -63,11 +65,11 @@ def add_dots_to_imports(file_path):

# Check if is a local file in same directory
if os.path.exists(os.path.join(this_dir, fname)):
print(os.path.join(this_dir, fname + ".py"))
# print(os.path.join(this_dir, fname + ".py"))
is_local_file = True
# Check if is a local folder in parent directory
elif os.path.exists(os.path.join(par_dir, fname)):
print(os.path.join(par_dir, fname))
# print(os.path.join(par_dir, fname))
is_local_folder = True

c = ".." if is_local_folder else "."
Expand All @@ -83,8 +85,8 @@ def add_dots_to_imports(file_path):
imports = terms[-1].split(",")
imports = [i.replace(" ", "") for i in imports]

print("parents: ", parents)
print("imports: ", imports)
# print("parents: ", parents)
# print("imports: ", imports)

if len(parents) > 0:
file.write(
Expand All @@ -101,7 +103,7 @@ def add_dots_to_imports_in_folder(folder_path):
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".py"):
print("########\n", file)
# print("########\n", file)
add_dots_to_imports(os.path.join(root, file))

def replace_args(folder_path):
Expand Down
17 changes: 15 additions & 2 deletions fundus_image_toolbox/vessel_segmentation/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
from .segmentation.utils.notebook_utils import clahe_equalized, get_ensemble
from .segmentation.utils.model_definition import FR_UNet
except ImportError:
from .clone import clone_repo
from .clone import clone_repo, adjust_imports

this_dir = Path(__file__).resolve().parent
clone_repo(target_dir=(this_dir / "segmentation").__str__())
target_dir = (this_dir / "segmentation").__str__()
clone_repo(target_dir=target_dir)
adjust_imports(target_dir)
from .segmentation.utils.notebook_utils import clahe_equalized, get_ensemble
from .segmentation.utils.model_definition import FR_UNet

try:
# The pickled model is a bunch object but PyPI's bunch is too old.
import bunch
except ImportError:
from .clone import clone_repo

this_dir = Path(__file__).resolve().parent
clone_repo(target_dir=(this_dir / "bunch").__str__(), link="https://github.com/dsc/bunch", branch="master", commit="85ed6841bf5754867703e67324a4c82b66f1cd4b")
sys.path.append((this_dir / "bunch").__str__())
import bunch


class Parser(ArgumentParser):
def error(self, message):
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ authors = [
requires-python = ">=3.9"
dependencies = [
"albumentations",
"bunch @ git+https://github.com/dsc/bunch",
"captum>=0.7.0",
"gdown>=5.2.0",
"imgaug>=0.4.0",
Expand Down Expand Up @@ -59,4 +58,4 @@ include-package-data = true
where = ["."]

[tool.uv.workspace]
members = ["."]
members = ["."]
2 changes: 1 addition & 1 deletion todos.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

[x] circle_crop

[ ] Vessel Segmentation: Ask Patrick & Jerry if they could get rid of "bunch" dependency: "bunch" prevents the use of python >= 3.10. 31.1.205: Relaxed it using the development version from git.
[ ] Vessel Segmentation: Ask Patrick & Jerry if they could get rid of "bunch" dependency: "bunch" prevents the use of python >= 3.10. 31.1.205: PyPI does not accept bunch as a git dependency as there's an older version on PyPI. Hence now we clone it for the segmentation code on the fly.

[ ] Once Sarah has refactored her image cropping code to yield an image as output, add it to circle_crop, s.t. one can choose between the two algorithms

Expand Down
Loading

0 comments on commit 51441b9

Please sign in to comment.