diffpy.cmi package
Complex modeling infrastructure: a modular framework for multi-modal modeling of scientific data.
Submodules
diffpy.cmi.log module
Centralized logging utilities for the CMI package.
This module exposes a single package logger plog and helpers to
switch between a concise user mode and a verbose debug mode.
Modes
- user
Only
INFOandERROR/CRITICALrecords are shown.WARNINGandDEBUGare hidden.- debug
All levels are shown.
Notes
Use set_log_mode() in the CLI to toggle visibility. The logger itself
always emits at DEBUG level; a handler-side filter controls what is shown.
diffpy.cmi.conda module
- class diffpy.cmi.conda.CondaEnvInfo(available: bool, mamba: bool, env_name: str | None, prefix: str | None)[source]
Bases:
objectSnapshot of conda/mamba availability and the active environment.
- Parameters:
available (bool) – Whether
condais available onPATH.mamba (bool) – Whether
mambais available onPATH.env_name (str or None) – Name of the active environment, if known.
prefix (str or None) – Filesystem prefix of the active environment, if known.
- available: bool
- env_name: str | None
- mamba: bool
- prefix: str | None
- diffpy.cmi.conda.available() bool[source]
Return whether
condais available on PATH.- Returns:
Trueif thecondaexecutable can be invoked.- Return type:
bool
- diffpy.cmi.conda.env_info() CondaEnvInfo[source]
Return availability and active-environment metadata.
- Returns:
Structured information assembled from
conda info --jsonand availability probes.- Return type:
- diffpy.cmi.conda.install_specs(specs: Sequence[str], *, channel: str | None = None, default_channel: str = 'conda-forge') Tuple[str, int, str][source]
Install a batch of specs, preferring mamba then conda.
- Parameters:
specs (sequence of str) – Conda spec strings (e.g.,
"numpy>=1.24").channel (str or None, optional) – Preferred channel for this batch.
default_channel (str, optional) – Channel used when
channelis not given.
- Returns:
The solver used (
"mamba"or"conda"), the exit code, and the captured output (empty when streaming).- Return type:
tuple of (str, int, str)
Notes
This function logs at
INFOlevel when each batch starts, warns when a mamba batch fails and a fallback is attempted, and leaves error decisions to higher-level callers.
- diffpy.cmi.conda.list_installed_names() List[str][source]
Return conda package names from
conda list --json.Results are cached for the current process to reduce repeated shell calls.
- Returns:
Sorted unique package names.
- Return type:
list of str
- diffpy.cmi.conda.mamba_available() bool[source]
Return whether
mambais available on PATH.- Returns:
Trueif themambaexecutable can be invoked.- Return type:
bool
- diffpy.cmi.conda.run(cmd: Sequence[str], cwd: Path | None = None, *, capture: bool | None = None) Tuple[int, str][source]
Run a subprocess with sensible, mode-dependent defaults.
The function does not decide logging policy. It simply runs the command and returns the exit code and combined output. Visibility of the child output follows CMI’s current log mode:
In debug mode: stream output live by default.
In user mode: capture output quietly by default.
- Parameters:
cmd (sequence of str) – Command and arguments.
cwd (path-like, optional) – Working directory for the child process.
capture (bool or None, optional) – Override the default visibility.
Trueforces capture;Falseforces streaming;Noneselects the mode-dependent default.
- Returns:
Exit code and captured output (empty string when streaming).
- Return type:
tuple of (int, str)
Notes
When capturing in debug mode, the output is also echoed so developers still see live progress.
diffpy.cmi.packsmanager module
- class diffpy.cmi.packsmanager.PacksManager(root_path=None)[source]
Bases:
objectDiscovery, parsing, and installation for pack files.
- packs_dir
Absolute path to the installed packs directory. Defaults to requirements/packs under the installed package.
- Type:
pathlib.Path
- examples_dir
Absolute path to the installed examples directory. Defaults to docs/examples under the installed package.
- Type:
pathlib.Path
- available_examples() dict[str, List[tuple[str, Path]]][source]
Finds all examples for each pack and builds a dict.
- Parameters:
root_path (Path) – Root path to the examples directory.
- Returns:
A dictionary mapping pack names to lists of example names.
- Return type:
dict
- Raises:
FileNotFoundError – If the provided root_path does not exist or is not a directory.
- available_packs() List[str][source]
List all available packs.
- Returns:
Pack basenames available under
packs_dir.- Return type:
list of str
- check_pack(identifier: str | Path) bool[source]
Return whether a pack is installed.
- Parameters:
identifier (str or path-like) – Basename to the pack file.
- Returns:
Trueif the pack is installed,Falseotherwise.- Return type:
bool
- copy_examples(examples_to_copy: List[str], target_dir: Path | str = None, force: bool = False) None[source]
Copy examples or packs into the target or current working directory.
- Parameters:
examples_to_copy (list of str) – User-specified pack(s), example(s), or “all” to copy all.
target_dir (pathlib.Path or str, optional) – Target directory to copy examples into. Defaults to current working directory.
force (bool, optional) – Defaults to
False. IfTrue, existing files are overwritten and directories are merged (extra files in the target are preserved).
- install_pack(identifier: str | Path) None[source]
Install a pack and verify presence.
- Parameters:
identifier (str) – Basename to the pack file.
- diffpy.cmi.packsmanager.get_package_dir(root_path=None)[source]
Get the package directory as a context manager.
- Parameters:
root_path (str, optional) – Used for testing, overrides the files(__name__) call.
- Returns:
A context manager that yields a pathlib.Path to the package directory.
- Return type:
context manager
diffpy.cmi.installer module
- class diffpy.cmi.installer.ParsedReq(raw: str, kind: str, name: str | None = None, spec: str | None = None, channel: str | None = None)[source]
Bases:
objectParsed representation of a single requirement line.
A requirement line may declare a package or a script. Script lines set
nameto the basename (stem) of the script so presence checking can treat them like packages when appropriate.- Parameters:
raw (str) – Original, unmodified line (kept verbatim).
kind ({"script", "pkg", "skip"}) – Classification of the line.
name (str or None, optional) – Package name (for
pkg) or script basename (forscript).spec (str or None, optional) – Version specifier (e.g.,
">=1.2") for packages.channel (str or None, optional) – Optional channel (from
channel::spec) for packages.
- channel: str | None = None
- kind: str
- name: str | None = None
- raw: str
- spec: str | None = None
- diffpy.cmi.installer.install_requirements(reqs: List[ParsedReq], *, scripts_root: Path, default_channel: str = 'conda-forge') int[source]
Install requirements, run scripts, and verify presence.
Policy
Packages first: batch per channel; solver failures are treated as soft (final presence check decides).
Scripts next: run native scripts only; a non-zero exit is a hard failure.
Presence check: verify packages and non-meta scripts.
- param reqs:
Parsed requirements (from packs and profile extras).
- type reqs:
list of ParsedReq
- param scripts_root:
Directory containing relative scripts used by recipes.
- type scripts_root:
path-like
- param default_channel:
Default conda channel for batches. Defaults to
"conda-forge".- type default_channel:
str, optional
- returns:
0on success,1on failure.- rtype:
int
- diffpy.cmi.installer.parse_requirement_line(line: str) ParsedReq[source]
Parse a single requirement definition.
Supported forms
Scripts: a line whose first token ends with
.shor.bat. The script’s basename becomesParsedReq.name.Packages: an optional
channel::prefix followed by a valid PEP 508 requirement string (e.g.,"numpy>=1.24").
- param line:
Raw line from a requirements file.
- type line:
str
- returns:
Structured representation of the requirement.
- rtype:
ParsedReq
- diffpy.cmi.installer.presence_check(reqs: List[ParsedReq], *, check_meta: bool | None = True) Tuple[bool, List[str]][source]
Check whether requirements appear satisfied.
Semantics
Packages: mark missing when
_is_installed()isFalse.Scripts: use the basename as a pseudo-package name. * If the basename starts with
"_"(meta-scripts), count them asmissing only when
check_meta=True(profile checks). During post-install verificationcheck_meta=Falseso meta-scripts are ignored (script exit code already enforced).Otherwise, treat like a package and require the basename to be present.
- param reqs:
Parsed requirements to check.
- type reqs:
list of ParsedReq
- param check_meta:
Whether meta-scripts should be considered missing. Defaults to
True.- type check_meta:
bool, optional
- returns:
(ok, missing)whereokisTruewhen all checks pass andmissinglists raw lines deemed missing.- rtype:
tuple of (bool, list of str)
diffpy.cmi.fit_tools module
- diffpy.cmi.fit_tools.optimize_recipe(recipe, optimizer: str = 'leastsq', **kwargs)[source]
Optimize the recipe using any selected SciPy optimizer.
minimize the residuals (FitRecipe().residual) of a FitRecipe by using a SciPy optimization function.
- Parameters:
recipe (FitRecipe) – The FitRecipe to be optimized.
optimizer (str, optional) – The SciPy optimizer to use. Options are: ‘leastsq’, ‘least_squares’, ‘minimize’. Default is ‘leastsq’.
**kwargs – Additional keyword arguments to pass to the optimizer.
- diffpy.cmi.fit_tools.plot_results(x, yobs, ycalc, difference_offset=None)[source]
Plot the results contained within a refined FitRecipe.
- Parameters:
x (array-like) – The independent variable.
yobs (array-like) – The observed/experimental data.
ycalc (array-like) – The calculated/fitted data.
difference_offset (int or float, optional) – The y-axis offset for the difference curve. If None, defaults to -0.8 * max(yobs).
diffpy.cmi.cli module
diffpy.cmi.profilesmanager module
- class diffpy.cmi.profilesmanager.Profile(name: str, packs: List[str], extras: List[str], source: Path)[source]
Bases:
objectContainer for a resolved profile.
- Parameters:
name (str) – Profile name (defaults to the YAML stem).
packs (list of str) – Pack basenames this profile depends on.
extras (list of str) – Extra requirement lines (scripts or packages).
source (path-like) – Absolute path to the YAML file that defined the profile.
Format (Profile)
--------------
structure:: (All profile .yaml files must have the following) –
- packs:
<pack_name>
- extras:
<additional_package_name>
profiles. (The file name is the profile identifier used for installing)
example. (See requirements/profiles/all.yml for an)
- extras: List[str]
- name: str
- packs: List[str]
- source: Path
- class diffpy.cmi.profilesmanager.ProfilesManager(packs_mgr: PacksManager | None = None, root_path=None)[source]
Bases:
objectDiscovery, loading, checking and installation for profiles.
- packs_mgr
The packs manager used for discovery and installation policy.
- Type:
PacksManager, optional
- profiles_dir
Absolute path to the installed profiles directory. Defaults to requirements/profiles under the installed package.
- Type:
pathlib.Path
- available_profiles() List[str][source]
Return available installed profiles by basename.
- Returns:
Profile basenames available under
profiles_dir.- Return type:
list of str
- check_profile(identifier: str | Path) bool[source]
Return whether a profile appears installed on this system.
- Parameters:
identifier (str or path-like) – Basename or absolute YAML path.
- Returns:
Trueif all packages and non-meta scripts appear present.- Return type:
bool
- install(identifier: str | Path) None[source]
Install a profile and verify presence.
- Parameters:
identifier (str or path-like) – Basename or absolute YAML path.