Skip to content

Commit

Permalink
improvement: allow getting acdc version from cli with -v or -i
Browse files Browse the repository at this point in the history
  • Loading branch information
ElpadoCan committed Nov 27, 2024
1 parent 61ae424 commit a292d74
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 16 deletions.
19 changes: 19 additions & 0 deletions cellacdc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ def run():
from cellacdc.config import parser_args

PARAMS_PATH = parser_args['params']

if parser_args['version'] or parser_args['info']:
import platform, sys
from cellacdc import cellacdc_path
from cellacdc.myutils import get_date_from_version, read_version
version = read_version()
release_date = get_date_from_version(
version, package='cellacdc'
)
py_ver = sys.version_info
python_version = f'{py_ver.major}.{py_ver.minor}.{py_ver.micro}'
print('='*100)
print(f'Cell-ACDC version {version}')
print(f'Released on: {release_date}')
print(f'Installed in "{cellacdc_path}"')
print(f'Python {python_version}')
print(f'Platform: {platform.platform()}')
print('='*100)
exit()

if PARAMS_PATH:
_run.run_cli(PARAMS_PATH)
Expand Down
17 changes: 16 additions & 1 deletion cellacdc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ def _resizeWarningHandler(self, msg_type, msg_log_context, msg_string):
help=('Path of the ".ini" workflow file')
)

ap.add_argument(
'-v', '--version', action='store_true',
help=(
'Get information about Cell-ACDC version and environment'
)
)

ap.add_argument(
'-i', '--info', action='store_true',
help=(
'Get information about Cell-ACDC version and environment'
)
)

ap.add_argument(
'-d', '--debug', action='store_true',
help=(
Expand All @@ -78,7 +92,8 @@ def _resizeWarningHandler(self, msg_type, msg_log_context, msg_string):
parser_args = vars(parser_args)
if os.path.exists(debug_true_filepath):
parser_args['debug'] = True
except:
except Exception as err:
import pdb; pdb.set_trace()
print('Importing from notebook, ignoring Cell-ACDC argument parser...')
parser_args = {}
parser_args['debug'] = False
Expand Down
97 changes: 82 additions & 15 deletions cellacdc/myutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,9 @@ def check_napari_plugin(plugin_name, module_name, parent=None):
raise e

def _install_pip_package(pkg_name):
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-U', pkg_name])
subprocess.check_call(
[sys.executable, '-m', 'pip', 'install', '-U', pkg_name]
)

def uninstall_pip_package(pkg_name):
subprocess.check_call(
Expand Down Expand Up @@ -2509,7 +2511,13 @@ def check_install_cellpose(version: str='2.0'):
)

def check_install_baby():
check_install_package('baby', pypi_name='baby-seg')
check_install_package(
'TensorFlow',
pypi_name='tensorflow',
import_pkg_name='tensorflow',
max_version='2.14'
)
check_install_package('baby', pypi_name='baby-seg', import_pkg_name='baby')

def check_install_yeaz():
check_install_torch()
Expand Down Expand Up @@ -2542,6 +2550,23 @@ def check_pkg_version(import_pkg_name, min_version, raise_err=True):
else:
return is_version_correct

def check_pkg_max_version(import_pkg_name, max_version, raise_err=True):
is_version_correct = False
try:
from packaging import version
installed_version = get_package_version(import_pkg_name)
if version.parse(installed_version) <= version.parse(max_version):
is_version_correct = True
except Exception as err:
is_version_correct = False

if raise_err and not is_version_correct:
raise ModuleNotFoundError(
f'{import_pkg_name}<={max_version} not installed.'
)
else:
return is_version_correct

def install_package_conda(conda_pkg_name, channel='conda-forge'):
try:
commad = f'conda install -c {channel} -y {conda_pkg_name}'
Expand Down Expand Up @@ -2608,7 +2633,8 @@ def check_install_package(
caller_name='Cell-ACDC',
force_upgrade=False,
upgrade=False,
min_version=''
min_version='',
max_version=''
):
"""Try to import a package. If import fails, ask user to install it
automatically.
Expand Down Expand Up @@ -2646,7 +2672,11 @@ def check_install_package(
If not empty it must be a valid version `major[.minor][.patch]` where
minor and patch are optional. If the installed package is older the
upgrade will be forced.
max_version : str, optional
If not empty it must be a valid version `major[.minor][.patch]` where
minor and patch are optional. If the installed package is newer the
upgrade will be forced.
Raises
------
ModuleNotFoundError
Expand All @@ -2666,11 +2696,14 @@ def check_install_package(
f'User requested to forcefully upgrade the package "{pkg_name}"')
if min_version:
check_pkg_version(import_pkg_name, min_version)
if max_version:
check_pkg_max_version(import_pkg_name, max_version)
except ModuleNotFoundError:
proceed = _install_package_msg(
pkg_name, note=note, parent=parent, upgrade=upgrade,
is_cli=is_cli, caller_name=caller_name, logger_func=logger_func,
pkg_command=pypi_name
pkg_command=pypi_name, max_version=max_version,
min_version=min_version
)
if pypi_name:
pkg_name = pypi_name
Expand All @@ -2683,13 +2716,20 @@ def check_install_package(
return traceback.format_exc()
try:
if pkg_name == 'tensorflow':
_install_tensorflow()
_install_tensorflow(
max_version=max_version, min_version=min_version
)
elif pkg_name == 'deepsea':
_install_deepsea()
elif pkg_name == 'segment_anything':
_install_segment_anything()
else:
_install_pip_package(pkg_name)
pkg_command = _get_pkg_command_pip_install(
pkg_name,
max_version=max_version,
min_version=min_version
)
_install_pip_package(pkg_command)
except Exception as e:
printl(traceback.format_exc())
_inform_install_package_failed(
Expand Down Expand Up @@ -2769,17 +2809,20 @@ def download_fiji(logger_func=print):

def _install_package_msg(
pkg_name, note='', parent=None, upgrade=False, caller_name='Cell-ACDC',
is_cli=False, pkg_command='', logger_func=print
is_cli=False, pkg_command='', logger_func=print, max_version='',
min_version=''
):
if is_cli:
proceed = _install_package_cli_msg(
pkg_name, note=note, upgrade=upgrade, caller_name=caller_name,
pkg_command=pkg_command, logger_func=logger_func
pkg_command=pkg_command, max_version=max_version,
min_version=min_version, logger_func=logger_func
)
else:
proceed = _install_package_gui_msg(
pkg_name, note=note, parent=parent, upgrade=upgrade,
caller_name=caller_name, pkg_command=pkg_command,
max_version=max_version, min_version=min_version,
logger_func=logger_func
)
return proceed
Expand Down Expand Up @@ -2866,13 +2909,28 @@ def _install_pytorch_cli(
args = selected_command.split()[1:]
subprocess.check_call([sys.executable, *args], shell=True)

def _get_pkg_command_pip_install(pkg_command, max_version='', min_version=''):
if min_version:
pkg_command = f'{pkg_command}>{min_version}'
if max_version:
pkg_command = f'{pkg_command},'

if max_version:
pkg_command = f'{pkg_command}<={max_version}'
return pkg_command

def _install_package_cli_msg(
pkg_name, note='', upgrade=False, caller_name='Cell-ACDC',
logger_func=print, pkg_command=''
logger_func=print, pkg_command='', max_version='',
min_version=''
):
if not pkg_command:
pkg_command = pkg_name

pkg_command = _get_pkg_command_pip_install(
pkg_command, max_version=max_version, min_version=min_version
)

if upgrade:
action = 'upgrade'
else:
Expand All @@ -2883,7 +2941,7 @@ def _install_package_cli_msg(
f'{separator}\n{caller_name} needs to {action} {pkg_name}\n\n'
'You can choose to install it now or stop the process and install it '
'later with the following command:\n\n'
f'pip install --upgrade {pkg_command}\n'
f'pip install --upgrade "{pkg_command}"\n'
)
logger_func(txt)
install_command = f'pip install --upgrade {pkg_command}'
Expand All @@ -2902,7 +2960,7 @@ def _install_package_cli_msg(

def _install_package_gui_msg(
pkg_name, note='', parent=None, upgrade=False, caller_name='Cell-ACDC',
pkg_command='', logger_func=None
pkg_command='', logger_func=None, max_version='', min_version=''
):
msg = widgets.myMessageBox(parent=parent)
if upgrade:
Expand All @@ -2915,6 +2973,10 @@ def _install_package_gui_msg(
if not pkg_command:
pkg_command = pkg_name

pkg_command = _get_pkg_command_pip_install(
pkg_command, max_version=max_version, min_version=min_version
)

command_html = pkg_command.lower().replace('<', '&lt;').replace('>', '&gt;')
command = f'pip install --upgrade {command_html}'

Expand All @@ -2938,13 +3000,18 @@ def _install_package_gui_msg(
)
return msg.clickedButton == okButton

def _install_tensorflow():
def _install_tensorflow(max_version='', min_version=''):
cpu = platform.processor()
pkg_command = _get_pkg_command_pip_install(
'tensorflow',
max_version=max_version,
min_version=min_version
)
if is_mac and cpu == 'arm':
args = ['conda install -y -c conda-forge tensorflow']
args = [f'conda install -y -c conda-forge "{pkg_command}"']
shell = True
else:
args = [sys.executable, '-m', 'pip', 'install', '-U', 'tensorflow']
args = [sys.executable, '-m', 'pip', 'install', '-U', pkg_command]
shell = False
subprocess.check_call(args, shell=shell)

Expand Down

0 comments on commit a292d74

Please sign in to comment.