Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic fix avoiding writing to package files #394

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 39 additions & 10 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 (
source_config_cellfinder,
source_custom_config_cellfinder,
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")

original_config = source_config_cellfinder()
new_config = source_custom_config_cellfinder()
if new_model_path is not None:
write_model_to_cfg(new_model_path, original_config, new_config)
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")

def write_model_to_cfg(new_model_path, orig_config, custom_config):
original_config = default_configuration_path()
new_config = user_specific_configuration_path()
if new_model_path is not None:
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 source_custom_config_cellfinder
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 = source_custom_config_cellfinder()
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
24 changes: 21 additions & 3 deletions cellfinder/core/tools/source_files.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
from pathlib import Path


def source_config_cellfinder():
def default_configuration_path():
"""
Returns the default configuration path for cellfinder.

Returns:
Path: The default configuration path.
"""
return Path(__file__).parent.parent / "config" / "cellfinder.conf"


def source_custom_config_cellfinder():
return Path(__file__).parent.parent / "config" / "cellfinder.conf.custom"
def user_specific_configuration_path():
"""
Returns the path to the user-specific configuration file for cellfinder.

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".

Returns:
Path: The path to the custom configuration file.

"""
return Path.home() / ".cellfinder" / "cellfinder.conf.custom"
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
Loading