Skip to content

Commit

Permalink
Merge pull request #12 from alphal00p/madsymbolic
Browse files Browse the repository at this point in the history
One Single Symbolica
  • Loading branch information
ValentinHirschi authored Mar 22, 2024
2 parents 731ccaa + b519aa6 commit 507082c
Show file tree
Hide file tree
Showing 30 changed files with 449 additions and 131 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ coverage.xml
.pytest_cache/
cover/

# dependencies
python/gammaloop/dependencies/LOCK
python/gammaloop/dependencies/INSTALLED
python/gammaloop/dependencies/dependency_build.log
python/gammaloop/dependencies/venv/
python/gammaloop/dependencies/symbolica/

# Translations
*.mo
*.pot
Expand Down
20 changes: 9 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,24 @@ name = "gamma_net"
harness = false

[[bench]]
name="evaluate_net"
harness=false
name = "evaluate_net"
harness = false

[[example]]
name = "gamma_chains"
path= "examples/Rust/Tensors/gamma_chain.rs"
path = "examples/Rust/Tensors/gamma_chain.rs"

[[example]]
name = "gamma_network"
path="examples/Rust/Tensors/gamma_network.rs"
path = "examples/Rust/Tensors/gamma_network.rs"

[[example]]
name="evaluate_network"
path="examples/Rust/Tensors/evaluate_network.rs"
name = "evaluate_network"
path = "examples/Rust/Tensors/evaluate_network.rs"

[dependencies]
# You may need bleeding edge changes
symbolica = { git = "https://github.com/alphal00p/symbolica" }
#symbolica = "*"
#symbolica = { path = "../symbolica" }
symbolica = { path = "./python/gammaloop/dependencies/symbolica" }
#symbolica = { git = "https://github.com/alphal00p/symbolica" }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "*"
bincode = "1.3.3"
Expand Down Expand Up @@ -98,7 +96,7 @@ rand_xoshiro = "0.6.0"
funty = "2.0.0"
block-id = "0.2.1"
once_cell = "1.19.0"
enum_delegate = {git="https://gitlab.com/dawn_app/enum_delegate"}
enum_delegate = { git = "https://gitlab.com/dawn_app/enum_delegate" }

[dependencies.pyo3]
features = ["multiple-pymethods"]
Expand Down
43 changes: 33 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ See the [wiki](https://wiki.alphaloop.ch/) for more information on the project.

## Installation

All installations require Rust to be installed on your system.

You can easily instal Rust with [this one-liner](https://www.rust-lang.org/tools/install).

### > Installation using `pip`
```
pip install gammaloop
gammaloop --build_dependencies
source `gammaloop -venv`
```

### > Installation from sources
```
git clone https://github.com/alphal00p/gammaloop.git
cd gammaloop
python -m pip install -r ./python/gammaloop/requirements.txt
./bin/build_dependencies.sh
./bin/compile_bin.sh --release
./bin/compile_lib.sh --release
./bin/compile.sh --release
source python/gammaloop/dependencies/venv/bin/activate
```
The relevant binaries will then be in `./bin/` and the python module in `./python/gammalop`.
The relevant binaries will then be in `./bin/` and the gammaloop python module is located at `./python/gammaloop`.

## Tests

Expand All @@ -40,20 +44,32 @@ bash -c 'cd `python -c "import os; import gammaloop; print(os.path.dirname(gamma

### > Testing an installation from sources

Simply run:
Run:
```
/bin/run_tests.sh python
/bin/run_tests.sh rust
```

## Usage

`GammaLoop` can be used as a python command line interface (`gammaloop`) for scattering process code generation or also directly as a binary (`gammaloop_rust_cli`) for steering the Monte-Carlo integration. Both programs are installed as scripts in the `bin` directly when installing `GammaLoop` using `pip` or when installing it from sources.
There are three entry points to the `GammaLoop` functionalities:

1. Preferred method is through the Python command-line interface `gammaloop`.

2. Alternatively, the same functionalities can be accessed programmatically, e.g. in Jupyter notebook, through the Python API by importing the `gammaloop` library.

3. Finally, expert users may also find it useful to steer some of functionalities directly from the rust binary `gammaloop_rust_cli`.

### > Generating scattering process code with ./gammaloop
Both executables `gammaloop` and `gammaloop_rust_cli` are made available as scripts in the `bin` directory.
The `gammaloop` Python module is also exposed after installation and ready to be imported in user custom Python scripts.

### 1. Usage from the Python command-line interface: ./gammaloop

`GammaLoop` is typically used through the python command-line interface `gammaloop`.
Place your list of commands in a file named e.g. `cmd.gL`, for instance:

```
# Content of file 'cmd.gL'
import_model sm --format ufo
export_model ./sm.yaml --format yaml
```
Expand All @@ -70,11 +86,18 @@ to get an overview of available commands and:
./bin/gammaloop -c "help import_model"
```
to get help on any specific command.

You can find example of command files in the `<MODULE_PATH>/data/run_cards/` directory.

### > Steering Monte-Carlo integration with ./gammaloop_rust_cli
### 2. Usage from within a Jupyter notebook: the Python API

*WORK IN PROGRESS*

### 3. Usage from the rust binary executable: ./gammaloop_rust_cli

Steering a Monte-Carlo integration with `gammaloop_rust_cli` can be done by running:
All typical usecases of `GammaLoop` are available through the Python command-line interface mentioned earlier.
However, expert users might want to steer the Monte-Carlo integration directly using the `gammaloop_rust_cli` binary.
This is possible throught the `gammaloop_rust_cli` binary, which can be used as follows:
```
./bin/gammaloop_rust_cli --config <MODULE_PATH>/gammaloop/data/run_cards/rust_run_config.yaml
```
Expand Down
34 changes: 2 additions & 32 deletions bin/build_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,2 @@
#!/usr/bin/env bash

RETCODE=0;

clean_dependencies () {
cd dependencies/fjcore
rm -f *.o
make clean
cd ../..
}

build_dependencies () {
cd dependencies/fjcore
make -j8 &> /dev/null
RETCODE=$RETCODE+$?
cd ../..
}

if [ $# -eq 0 ]
then
echo "Building all dependencies...";
build_dependencies
elif [ "$1" == "clean" ]
then
echo "Cleaning all dependencies...";
clean_dependencies
else
echo "Invalid argument";
RETCODE=1;
fi

exit $(($RETCODE))
cd ./python/gammaloop
./bin/build_dependencies.sh "$@"
12 changes: 8 additions & 4 deletions bin/test_deployment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ maturin build --release --features "extension-module" -o /tmp/test_gammaloop_dep
RETCODE=$RETCODE+$?;
cd $TMPDIR/test_gammaloop_deployment
python -m venv venv
./venv/bin/python -m pip install --upgrade pip
./venv/bin/python -m pip install pytest
./venv/bin/python -m pip install `ls -1 $TMPDIR/test_gammaloop_deployment/wheel/*.whl`
source ./venv/bin/activate
python -m pip install --upgrade pip
python -m pip install pytest
python -m pip install `ls -1 $TMPDIR/test_gammaloop_deployment/wheel/*.whl`
RETCODE=$RETCODE+$?;
gammaloop --build_dependencies
RETCODE=$RETCODE+$?;
cd `ls -d1 ./venv/lib/python*/site-packages/gammaloop`
PATH=$TMPDIR"/test_gammaloop_deployment/bin"$PATH ../../../../bin/python -m pytest
source `gammaloop -venv`
python -m pytest
RETCODE=$RETCODE+$?;
exit $(($RETCODE))
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
if cfg!(feature = "python_api") {
pyo3_build_config::add_extension_module_link_args();
}
println!("cargo:rustc-link-search=./dependencies/fjcore");
println!("cargo:rustc-link-search=./python/gammaloop/dependencies/fjcore");
println!("cargo:rustc-link-lib=stdc++");
#[cfg(target_os = "macos")]
{
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ authors = [
{ name = "Valentin Hirschi" },
]

dependencies = ["PyYAML>=6.0.1", "symbolica>=0.1.0", "PyMuPDF>=1.23.4"]
dependencies = ["PyYAML>=6.0.1", "PyMuPDF>=1.23.4"]

classifiers = [
"Development Status :: 3 - Alpha",
Expand Down Expand Up @@ -72,6 +72,7 @@ include = ["python/gammaloop"]
exclude = [
"python/gammaloop/data/templates/drawing/combine_pages.py",
"python/gammaloop/data/models/*",
"python/gammaloop/dependencies/*",
"python/gammaloop/_gammaloop.so",
"python/gammaloop/requirements.txt",
"python/gammaloop/run_python_tests.sh",
Expand Down
119 changes: 115 additions & 4 deletions python/gammaloop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,86 @@
import os
from .interface.gammaloop_interface import CommandList
from .interface.gammaloop_interface import GammaLoop
from .misc.common import GL_DEBUG, GL_CONSOLE_HANDLER, register_symbolica, logger, DATA_PATH, GammaLoopError
import sys
from subprocess import Popen
from enum import StrEnum

__version__ = "0.0.1"

GL_PATH = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))


class CLIColour(StrEnum):
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'


def check_gammaloop_dependencies(clean_dependencies=False, build_dependencies=False, no_gammaloop_python_venv=False):

gammaloop_root_path = os.path.abspath(GL_PATH)

if clean_dependencies:
print("Cleaning gammaloop dependencies in '%s' using %s./bin/build_dependencies.sh clean%s ..." % (
os.path.abspath(os.path.join(gammaloop_root_path, 'dependencies')), CLIColour.GREEN, CLIColour.END))
Popen([f"bash {os.path.join('.', 'bin','build_dependencies.sh')} clean",],
shell=True, cwd=gammaloop_root_path).wait()
print("%sDependencies folder cleaned successfully.%s" %
(CLIColour.GREEN, CLIColour.END))
if not build_dependencies:
sys.exit(0)

if build_dependencies:
print("Building gammaloop dependencies in '%s' using the %s./bin/build_dependencies.sh%s script ..." % (
os.path.abspath(os.path.join(gammaloop_root_path, 'dependencies')), CLIColour.GREEN, CLIColour.END))
Popen([f"bash {os.path.join('.', 'bin', 'build_dependencies.sh')}",],
shell=True, cwd=gammaloop_root_path).wait()
if not os.path.isfile(os.path.join(gammaloop_root_path, 'dependencies', 'INSTALLED')):
print("%sCould not build the dependencies. Find more information in '%s'.%s" % (
CLIColour.RED, os.path.join(gammaloop_root_path, 'dependencies', 'dependency_build.log.log'), CLIColour.END))
sys.exit(1)
else:
print("%sDependencies built successfully.%s" %
(CLIColour.GREEN, CLIColour.END))
sys.exit(0)

if not os.path.isfile(os.path.join(gammaloop_root_path, 'dependencies', 'INSTALLED')):
print("\nGammaloop dependencies are %snot installed%s. Run '%sgammaloop --build_dependencies%s' to install them first. Exiting.\n" % (
CLIColour.RED, CLIColour.END, CLIColour.GREEN, CLIColour.END))
sys.exit(1)

venv_path = os.path.abspath(os.path.join(
gammaloop_root_path, 'dependencies', 'venv'))

if venv_path != os.path.abspath(os.path.join(os.path.dirname(sys.executable), os.path.pardir)):
print("%sWARNING:%s It is recommended to run gammaloop within its Python virtual environment, by issuing the command:\n\n%ssource %s/bin/activate%s\n" % (
CLIColour.YELLOW, CLIColour.END, CLIColour.GREEN, venv_path, CLIColour.END))
if not no_gammaloop_python_venv:
if not os.path.isfile(os.path.join(venv_path, 'site_paths.txt')):
print("%sWARNING:%s Could not find Python virtual environment list of sites in file '%s'.\nConsider running '%sgammaloop --clean_dependencies --build_dependencies%s' to re-install gammaloop dependencies.\n" % (
os.path.join(venv_path, 'venv_site_paths.txt'), CLIColour.GREEN, CLIColour.END))
else:
try:
with open(os.path.join(venv_path, 'site_paths.txt'), 'r') as f:
site_paths = eval(f.read())
except Exception as e:
site_paths = None
print("%sWARNING:%s Could not extract list of site paths of the Python virtual environment of gammaloop from file '%s' (error: %s%s%s).\nConsider running '%sgammaloop --clean_dependencies --build_dependencies%s' to re-install gammaloop dependencies.\n" % (
os.path.join(venv_path, 'site_paths.txt'), CLIColour.RED, str(e), CLIColour.END, CLIColour.GREEN, CLIColour.END))
if site_paths is not None:
site_paths = [sp for sp in site_paths if sp.startswith(
venv_path) and sp not in sys.path]
if len(site_paths) > 0:
print("%sINFO:%s The following paths have been automatically and temporarily added by gammaloop to your PYTHONPATH:\n%s" % (
CLIColour.GREEN, CLIColour.END, ', '.join(site_paths)))
for site_path in site_paths:
sys.path.insert(0, site_path)


def cli():
from argparse import ArgumentParser # pylint: disable=import-outside-toplevel
Expand All @@ -14,17 +90,52 @@ def cli():
prog='γLoop',
description='Compute differential cross-section with Local Unitarity')
argparser.add_argument('command_file', type=str,
help='Input file listing commands to run')
help='Input file listing commands to run', nargs='?')
argparser.add_argument('--debug', '-d', action='store_true',
default=False, help='Enable debug mode')
argparser.add_argument('--build_dependencies', '-bd', action='store_true',
default=False, help='Build dependencies')
argparser.add_argument('--clean_dependencies', '-cd', action='store_true',
default=False, help='Clean dependencies build')
argparser.add_argument('--no_gammaloop_python_venv', '-no_venv', action='store_true',
default=False, help='Do not add python virtual environment installed by gammaloop to the PYTHONPATH')
argparser.add_argument('--quiet', '-q', action='store_true',
default=False, help='Enable quiet mode')
argparser.add_argument('--no_file_handler', '-nfh', action='store_true',
default=False, help='Disable logging to files')
argparser.add_argument('--show_venv_activate_path', '-venv', action='store_true',
default=False, help='Show gammaloop Python venv activate path')
argparser.add_argument('--command', '-c', dest='run_command', action='store_true',
default=False, help='Directly run command specified')
args = argparser.parse_args()

if args.show_venv_activate_path:
venv_path = os.path.abspath(os.path.join(
GL_PATH, 'dependencies', 'venv', 'bin', 'activate'))
if not os.path.isfile(venv_path):
print("%sCould not find the gammaloop Python virtual environment activate script at '%s'.%s" % (
CLIColour.RED, venv_path, CLIColour.END))
print("Make sur to first run %sgammaloop --build_dependencies%s" %
(CLIColour.GREEN, venv_path, CLIColour.END))
sys.exit(1)
else:
print(venv_path)
sys.exit(0)

# Before importing anything of gammaloop, check the dependencies with proper arguments
check_gammaloop_dependencies(args.clean_dependencies, args.build_dependencies,
args.no_gammaloop_python_venv)

from .interface.gammaloop_interface import CommandList
from .interface.gammaloop_interface import GammaLoop
from .misc.common import GL_DEBUG, GL_CONSOLE_HANDLER, register_symbolica, logger, DATA_PATH, GammaLoopError

if args.command_file is None:
logger.critical(
"%sCommand file or string not specified.%s", CLIColour.RED, CLIColour.END)
argparser.print_help()
sys.exit(1)

if args.no_file_handler:
os.environ['GL_ENABLE_FILE_HANDLERS'] = 'FALSE'
else:
Expand Down
Loading

0 comments on commit 507082c

Please sign in to comment.