-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_dependencies.py
More file actions
executable file
·156 lines (124 loc) · 5.54 KB
/
Copy pathinstall_dependencies.py
File metadata and controls
executable file
·156 lines (124 loc) · 5.54 KB
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
import subprocess
import sys
import os
import json
import re
try:
from tqdm import tqdm
except ImportError:
print("tqdm not found. Installing it first...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "tqdm"])
from tqdm import tqdm
"""
Description: This script installs all dependencies required to run the TMIDAS pipelines.
"""
def run_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
if process.returncode != 0:
print(f"Error executing command: {command}")
print(error.decode())
sys.exit(1)
return output.decode()
# Function to check if JDK is installed
def check_jdk_installed():
try:
output = run_command("javac -version")
print("JDK is already installed.")
return True
except SystemExit:
print("JDK is not installed.")
return False
# Function to prompt user to install JDK
def prompt_install_jdk():
print("To install the required dependencies, please install the default JDK (bioformats needs Java).")
print("You can do this by running:")
print("sudo apt install default-jdk")
# Ask for user confirmation to install JDK
response = input("Do you want to install the default JDK now? (y/n): ").strip().lower()
if response == 'y':
run_command("sudo apt update")
run_command("sudo apt install default-jdk -y")
# Set JAVA_HOME environment variable after installation
os.environ['JAVA_HOME'] = '/usr/lib/jvm/default-java'
print(f"JAVA_HOME is set to: {os.environ['JAVA_HOME']}")
else:
print("JDK installation skipped. The script may not work correctly without JDK.")
sys.exit(1)
# Check for JDK installation
if not check_jdk_installed():
prompt_install_jdk()
# Get the path to the conda executable
conda_executable = os.path.join(os.path.dirname(sys.executable), 'conda')
mamba_executable = os.path.join(os.path.dirname(sys.executable), 'mamba')
env_name = "tmidas-env"
# Initialize conda
print("Initializing conda...")
run_command(f"{conda_executable} init bash")
# Check if environment already exists
env_list = run_command(f"{conda_executable} env list --json").strip()
existing_envs = json.loads(env_list)['envs']
env_exists = any(path.endswith(env_name) for path in existing_envs)
if env_exists:
print(f"Environment {env_name} already exists. Updating packages...")
env_path = [path for path in existing_envs if path.endswith(env_name)][0]
else:
# Create the environment
print(f"Creating environment {env_name}...")
run_command(f"{conda_executable} create -n {env_name} python=3.9 -y")
# Get the path to the created environment
env_path = run_command(f"{conda_executable} env list --json").strip()
env_path = json.loads(env_path)['envs']
env_path = [path for path in env_path if path.endswith(env_name)][0]
# Set up the command prefix to run in the activated environment
cmd_prefix = f"{conda_executable} run -n {env_name} "
# Initialize mamba
def get_mamba_version(mamba_executable):
try:
output = subprocess.check_output([mamba_executable, "--version"], text=True).strip()
print(f"Detected mamba version string: {output}")
# Extract the first occurrence of something like 2.1.1
match = re.search(r"(\d+\.\d+(\.\d+)?)", output)
if not match:
raise ValueError("version number not found")
version_str = match.group(1)
major_version = int(version_str.split('.')[0])
return major_version
except Exception as e:
print(f"Could not determine mamba version ({e}). Assuming legacy syntax.")
return 1 # fallback for unknown or old versions
print("Initializing mamba...")
mamba_major = get_mamba_version(mamba_executable)
if mamba_major >= 2:
run_command(cmd_prefix + f"{mamba_executable} shell init --shell bash")
else:
run_command(cmd_prefix + f"{mamba_executable} init")
# Install dependencies
# These packages should be installed via conda for binary compatibility
conda_dependencies = [
'numpy<2', 'scikit-image', 'tifffile', 'imagecodecs', 'pillow',
'pandas', 'scipy', 'openslide', 'ocl-icd-system', 'cupy','javabridge'
]
# These can be safely installed via pip
pip_dependencies = [
'aicsimageio', 'opencv-python', 'readlif', 'SimpleITK',
'openslide-python', 'glob2', 'pytest', 'cucim', 'aicspylibczi', 'torch',
'torchvision', 'timm','tqdm', 'devbio-napari','siphash24'
]
print("Upgrading pip and setuptools...")
run_command(cmd_prefix + "python -m pip install -U setuptools pip")
print("Installing conda packages (for binary compatibility)...")
conda_deps_str = ' '.join(conda_dependencies)
run_command(cmd_prefix + f"{conda_executable} install -c conda-forge {conda_deps_str} -y")
print("Installing MobileSAM...")
run_command(cmd_prefix + "pip install --no-user git+https://github.com/ChaoningZhang/MobileSAM.git")
print("Installing python-bioformats without dependencies (keep conda javabridge for ABI compatibility)...")
run_command(cmd_prefix + "pip install --no-user --no-deps python-bioformats")
print("Installing pip packages...")
for dependency in tqdm(pip_dependencies, desc="Installing dependencies"):
run_command(cmd_prefix + f"pip install --no-user {dependency}")
print("Installing napari...")
run_command(cmd_prefix + "python -m pip install --no-user napari[all]")
print("Installing cellpose...")
run_command(cmd_prefix + "python -m pip install --no-user 'cellpose>=3.0.0,<4.0.0'")
print("All dependencies installed successfully.")