diff --git a/corrlib/initialization.py b/corrlib/initialization.py index bdf9cee..99f90d1 100644 --- a/corrlib/initialization.py +++ b/corrlib/initialization.py @@ -71,6 +71,7 @@ def _create_config(path: Path, tracker: str, cached: bool) -> ConfigParser: 'db': 'backlogger.db', 'projects_path': 'projects', 'archive_path': 'archive', + 'plot_path': 'plots', 'toml_imports_path': 'toml_imports', 'import_scripts_path': 'import_scripts', } @@ -113,6 +114,7 @@ def create(path: Path, tracker: str = 'datalad', cached: bool = True) -> None: os.chmod(path / config['paths']['db'], 0o666) os.makedirs(path / config['paths']['projects_path']) os.makedirs(path / config['paths']['archive_path']) + os.makedirs(path / config['paths']['plot_path']) os.makedirs(path / config['paths']['toml_imports_path']) os.makedirs(path / config['paths']['import_scripts_path'] / 'template.py') with open(path / ".gitignore", "w") as fp: diff --git a/corrlib/input/openQCD.py b/corrlib/input/openQCD.py index c8eef72..9dfe85f 100644 --- a/corrlib/input/openQCD.py +++ b/corrlib/input/openQCD.py @@ -4,8 +4,10 @@ import os import fnmatch from typing import Any, Optional from pathlib import Path +import matplotlib.pyplot as plt from ..pars.openQCD import ms1 from ..pars.openQCD import qcd2 +from ..tools import get_plot_dir @@ -164,7 +166,7 @@ def read_rwms(path: Path, project: str, dir_in_project: str, param: dict[str, An return rw_dict -def extract_t0(path: Path, project: str, dir_in_project: str, param: dict[str, Any], prefix: str, dtr_read: int, xmin: int, spatial_extent: int, fit_range: int = 5, postfix: str="", names: Optional[list[str]]=None, files: Optional[list[str]]=None, +def extract_t0(path: Path, project: str, dir_in_project: str, ensemble: str, param: dict[str, Any], prefix: str, dtr_read: int, xmin: int, spatial_extent: int, fit_range: int = 5, postfix: str="", names: Optional[list[str]]=None, files: Optional[list[str]]=None, r_start: list[int]=[], r_stop: list[int]=[], r_step:int=1) -> dict[str, Any]: """ Extract t0 measurements from the project. @@ -224,7 +226,7 @@ def extract_t0(path: Path, project: str, dir_in_project: str, param: dict[str, A if not r_stop == []: kwargs['r_stop'] = r_stop kwargs['r_step'] = r_step - + kwargs['plot_fit'] = True t0 = input.extract_t0(directory, prefix, dtr_read, @@ -234,6 +236,10 @@ def extract_t0(path: Path, project: str, dir_in_project: str, param: dict[str, A c=0.3, **kwargs ) + plot_dir = path / get_plot_dir(path) / ensemble / project + if not os.path.exists(plot_dir): + os.makedirs(plot_dir) + plt.savefig(plot_dir / "t0.pdf") par_list= [] for k in ["integrator", "eps", "ntot", "dnms"]: par_list.append(str(param[k])) @@ -244,7 +250,7 @@ def extract_t0(path: Path, project: str, dir_in_project: str, param: dict[str, A return t0_dict -def extract_t1(path: Path, project: str, dir_in_project: str, param: dict[str, Any], prefix: str, dtr_read: int, xmin: int, spatial_extent: int, fit_range: int = 5, postfix: str = "", names: Optional[list[str]]=None, files: Optional[list[str]]=None, +def extract_t1(path: Path, project: str, dir_in_project: str, ensemble: str, param: dict[str, Any], prefix: str, dtr_read: int, xmin: int, spatial_extent: int, fit_range: int = 5, postfix: str = "", names: Optional[list[str]]=None, files: Optional[list[str]]=None, r_start: list[int]=[], r_stop: list[int]=[], r_step:int=1) -> dict[str, Any]: """ Extract t1 measurements from the project. @@ -302,6 +308,7 @@ def extract_t1(path: Path, project: str, dir_in_project: str, param: dict[str, A if not r_stop == []: kwargs['r_stop'] = r_stop kwargs['r_step'] = r_step + kwargs['plot_fit'] = True t0 = input.extract_t0(directory, prefix, dtr_read, @@ -311,6 +318,10 @@ def extract_t1(path: Path, project: str, dir_in_project: str, param: dict[str, A c=2./3, **kwargs ) + plot_dir = path / get_plot_dir(path) / ensemble / project + if not os.path.exists(plot_dir): + os.makedirs(plot_dir) + plt.savefig(plot_dir / "t1.pdf") par_list= [] for k in ["integrator", "eps", "ntot", "dnms"]: par_list.append(str(param[k])) diff --git a/corrlib/meas_io.py b/corrlib/meas_io.py index 6b6e5f1..52e307e 100644 --- a/corrlib/meas_io.py +++ b/corrlib/meas_io.py @@ -6,7 +6,7 @@ import json from typing import Union from pyerrors import Obs, Corr, dump_object, load_object from hashlib import sha256 -from .tools import get_db_file, cache_enabled +from .tools import get_db_file, cache_enabled, get_plot_dir from .tracker import get, save, unlock import shutil from typing import Any @@ -104,6 +104,8 @@ def write_measurement(path: Path, ensemble: str, measurement: dict[str, dict[str subkeys.append(subkey) pars[subkey] = json.dumps(parameters["rw_fcts"][i]) elif ms_type in ['t0', 't1']: + plot_file = path / get_plot_dir(path) / ensemble / uuid / (ms_type + ".pdf") + files_to_save.append(plot_file) if parameter_file is not None: parameters = openQCD.load_ms3_infile(path, uuid, parameter_file) else: diff --git a/corrlib/toml.py b/corrlib/toml.py index 1f4e300..76533f4 100644 --- a/corrlib/toml.py +++ b/corrlib/toml.py @@ -229,14 +229,14 @@ def import_toml(path: Path, file: str, copy_file: bool=True) -> None: for rwp in ["integrator", "eps", "ntot", "dnms"]: param[rwp] = "Unknown" param['type'] = 't0' - measurement = openQCD.extract_t0(path, uuid, md['path'], param, str(md["prefix"]), int(md["dtr_read"]), int(md["xmin"]), int(md["spatial_extent"]), + measurement = openQCD.extract_t0(path, uuid, md['path'], ensemble, param, str(md["prefix"]), int(md["dtr_read"]), int(md["xmin"]), int(md["spatial_extent"]), fit_range=int(md.get('fit_range', 5)), postfix=str(md.get('postfix', '')), names=md.get('names', []), files=md.get('files', []), r_start=md.get('r_start', []), r_stop=md.get('r_stop', []), r_step=md.get('r_step', 1)) elif md['measurement'] == 't1': if 'param_file' in md: param = openQCD.load_ms3_infile(path, uuid, md['param_file']) param['type'] = 't1' - measurement = openQCD.extract_t1(path, uuid, md['path'], param, str(md["prefix"]), int(md["dtr_read"]), int(md["xmin"]), int(md["spatial_extent"]), + measurement = openQCD.extract_t1(path, uuid, md['path'], ensemble, param, str(md["prefix"]), int(md["dtr_read"]), int(md["xmin"]), int(md["spatial_extent"]), fit_range=int(md.get('fit_range', 5)), postfix=str(md.get('postfix', '')), names=md.get('names', []), files=md.get('files', []), r_start=md.get('r_start', []), r_stop=md.get('r_stop', []), r_step=md.get('r_step', 1)) write_measurement(path, ensemble, measurement, uuid, project['code'], (md['param_file'] if 'param_file' in md else None)) diff --git a/corrlib/tools.py b/corrlib/tools.py index 9ce194b..8dec61a 100644 --- a/corrlib/tools.py +++ b/corrlib/tools.py @@ -129,6 +129,33 @@ def get_db_file(path: Path) -> Path: return db_file +def get_plot_dir(path: Path) -> Path: + """ + Get the plots directory associated with the library at the given path. + + Parameters + ---------- + path: str + The path of the library. + + Returns + ------- + db_file: str + The file holding the database. + """ + path = Path(path) + if not os.path.exists(path): + raise FileNotFoundError(f"Corrlib path {path} does not exist.") + config_path = path / CONFIG_FILENAME + config = ConfigParser() + if os.path.exists(config_path): + config.read(config_path) + else: + raise FileNotFoundError("Configuration file not found.") + plot_dir = Path(config.get('paths', 'plot_dir', fallback='plots')) + return plot_dir + + def cache_enabled(path: Path) -> bool: """ Check, whether the library is cached. diff --git a/pyproject.toml b/pyproject.toml index faf7e6c..68abbe8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ dependencies = [ 'pyerrors>=2.11.1', "datalad>=1.1.0", 'typer>=0.12.5', + "matplotlib>=3.10.7", ] description = "Python correlation library" authors = [