Skip to content

Commit

Permalink
more improving names+docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofelder committed Mar 26, 2024
1 parent d277e2e commit e64ac1a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cellfinder/core/download/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path

from cellfinder.core.download import models
from cellfinder.core.download.download import amend_cfg
from cellfinder.core.download.download import amend_user_configuration

home = Path.home()
DEFAULT_DOWNLOAD_DIRECTORY = home / ".cellfinder"
Expand Down Expand Up @@ -65,7 +65,7 @@ def main():
model_path = models.main(args.model, args.install_path)

if not args.no_amend_config:
amend_cfg(new_model_path=model_path)
amend_user_configuration(new_model_path=model_path)


if __name__ == "__main__":
Expand Down
45 changes: 37 additions & 8 deletions cellfinder/core/download/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from brainglobe_utils.general.system import disk_free_gb

from cellfinder.core.tools.source_files import (
custom_configuration_path,
default_configuration_path,
user_specific_configuration_path,
)


Expand Down Expand Up @@ -75,16 +75,45 @@ def download(
os.remove(download_path)


def amend_cfg(new_model_path=None):
print("Ensuring custom config file is correct")
def amend_user_configuration(new_model_path=None) -> None:
"""
Amends the user configuration to contain the configuration
in new_model_path, if specified.
Parameters
----------
new_model_path : str, optional
The path to the new model configuration.
"""
print("(Over-)writing custom user configuration")

original_config = default_configuration_path()
new_config = custom_configuration_path()
new_config = user_specific_configuration_path()
if new_model_path is not None:
write_model_to_cfg(new_model_path, original_config, new_config)


def write_model_to_cfg(new_model_path, orig_config, custom_config):
write_model_to_config(new_model_path, original_config, new_config)


def write_model_to_config(new_model_path, orig_config, custom_config):
"""
Update the model path in the custom configuration file, by
reading the lines in the original configuration file, replacing
the line starting with "model_path =" and writing these
lines to the custom file.
Parameters
----------
new_model_path : str
The new path to the model.
orig_config : str
The path to the original configuration file.
custom_config : str
The path to the custom configuration file to be created.
Returns
-------
None
"""
config_obj = get_config_obj(orig_config)
model_conf = config_obj["model"]
orig_path = model_conf["model_path"]
Expand Down
10 changes: 5 additions & 5 deletions cellfinder/core/tools/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import cellfinder.core.tools.tf as tf_tools
from cellfinder.core import logger
from cellfinder.core.download import models as model_download
from cellfinder.core.download.download import amend_cfg
from cellfinder.core.tools.source_files import custom_configuration_path
from cellfinder.core.download.download import amend_user_configuration
from cellfinder.core.tools.source_files import user_specific_configuration_path

home = Path.home()
DEFAULT_INSTALL_PATH = home / ".cellfinder"
Expand Down Expand Up @@ -49,18 +49,18 @@ def prep_models(
if model_weights_path is None:
logger.debug("No model supplied, so using the default")

config_file = custom_configuration_path()
config_file = user_specific_configuration_path()

if not Path(config_file).exists():
logger.debug("Custom config does not exist, downloading models")
model_path = model_download.main(model_name, install_path)
amend_cfg(new_model_path=model_path)
amend_user_configuration(new_model_path=model_path)

model_weights = get_model_weights(config_file)
if not model_weights.exists():
logger.debug("Model weights do not exist, downloading")
model_path = model_download.main(model_name, install_path)
amend_cfg(new_model_path=model_path)
amend_user_configuration(new_model_path=model_path)
model_weights = get_model_weights(config_file)
else:
model_weights = Path(model_weights_path)
Expand Down
8 changes: 4 additions & 4 deletions cellfinder/core/tools/source_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def default_configuration_path():
return Path(__file__).parent.parent / "config" / "cellfinder.conf"


def custom_configuration_path():
def user_specific_configuration_path():
"""
Returns the path to the custom configuration file for cellfinder.
Returns the path to the user-specific configuration file for cellfinder.
This function returns the path to the custom configuration file
for cellfinder. The custom configuration file is located in the
This function returns the path to the user-specific configuration file
for cellfinder. The user-specific configuration file is located in the
user's home directory under the ".cellfinder" folder and is named
"cellfinder.conf.custom".
Expand Down

0 comments on commit e64ac1a

Please sign in to comment.