Skip to content

Commit

Permalink
Merge pull request #1827 from McStasMcXtrace/willend-pygen-minor-revi…
Browse files Browse the repository at this point in the history
…sion

pygen minor revision: Build importable make() function and call from generic notebook
  • Loading branch information
willend authored Jan 28, 2025
2 parents 7b503d9 + d4b1cae commit 00b3563
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 101 deletions.
5 changes: 3 additions & 2 deletions cmake/support/run-scripts/labenv.bat.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ SET EXT=%%~xi

@IF "%EXT%"==".instr" (
@FLAVOR@-pygen %INSTRUMENT%
SET INSTRUMENT="%BASE%_generated.py"
goto :JupyText
sed s/INSTRUMENT/%BASE%/g %BINDIR%\@MCCODE_RELPATH_BINDIR2TOOLDIR@\Python\@MCCODE_PREFIX@run\template.ipynb.in > %BASE%_generated.ipynb
SET INSTRUMENT="%BASE%_generated.ipynb"
goto :JupyLaunch
) ELSE IF "%EXT%"==".py" (
goto :JupyLaunch
) ELSE IF "%EXT%"==".ipynb" (
Expand Down
189 changes: 98 additions & 91 deletions mcstas/src/pygen.c.in

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions meta-pkgs/windows/McStas-metapackage64.iss
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Source: "dist\mcstas-tools-python-mcdisplay-pyqtgraph-NSIS64-@[email protected]
Source: "dist\mcstas-tools-python-mcdisplay-matplotlib-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "dist\mcstas-tools-python-mcdisplay-mantid-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "dist\mcstas-tools-python-mcdoc-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "dist\mcstas-mcpl-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "Support\Miniforge3-Windows-x86_64.exe"; DestDir: "{tmp}"

[Run]
Expand All @@ -76,7 +75,6 @@ Filename: "{tmp}\mcstas-tools-python-mcdisplay-pyqtgraph-NSIS64-@VERSION@-mingw6
Filename: "{tmp}\mcstas-tools-python-mcdisplay-matplotlib-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\mcstas-tools-python-mcdisplay-mantid-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\mcstas-tools-python-mcdoc-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\mcstas-mcpl-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\docupdate.bat";

; NOTE: Don't use "Flags: ignoreversion" on any shared system files
2 changes: 0 additions & 2 deletions meta-pkgs/windows/McXtrace-metapackage64.iss
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Source: "dist\mcxtrace-tools-python-mxdisplay-webgl-NSIS64-@[email protected]
Source: "dist\mcxtrace-tools-python-mxdisplay-pyqtgraph-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "dist\mcxtrace-tools-python-mxdisplay-matplotlib-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "dist\mcxtrace-tools-python-mxdoc-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "dist\mcxtrace-mcpl-NSIS64-@[email protected]"; DestDir: "{tmp}"
Source: "Support\Miniforge3-Windows-x86_64.exe"; DestDir: "{tmp}"

[Run]
Expand All @@ -66,7 +65,6 @@ Filename: "{tmp}\mcxtrace-tools-python-mxdisplay-webgl-NSIS64-@VERSION@-mingw64.
Filename: "{tmp}\mcxtrace-tools-python-mxdisplay-pyqtgraph-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\mcxtrace-tools-python-mxdisplay-matplotlib-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\mcxtrace-tools-python-mxdoc-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\mcxtrace-mcpl-NSIS64-@[email protected]"; Parameters: "/S"
Filename: "{tmp}\docupdate.bat";

; NOTE: Don't use "Flags: ignoreversion" on any shared system files
2 changes: 2 additions & 0 deletions meta-pkgs/windows/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ channels:

dependencies:
- m2-msys2-runtime
- m2-which
- m2-sed
- m2w64-gcc
- m2w64-gsl
- msmpi
Expand Down
4 changes: 2 additions & 2 deletions tools/Python/mcrun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ install(
RENAME "${P}run.py"
)

# Other .py infrastructure
foreach(NAME "__init__.py" "log.py" "mccode.py" "optimisation.py" "mcsplit.py")
# Copy in place .py and other infrastructure
foreach(NAME "__init__.py" "log.py" "mccode.py" "optimisation.py" "mcsplit.py" "template.ipynb.in")
install(
FILES "${PROJECT_SOURCE_DIR}/${NAME}"
DESTINATION "${DEST_TOOLDIR}/${TOOLS_NAME}"
Expand Down
3 changes: 1 addition & 2 deletions tools/Python/mcrun/jupylab.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ canrun() {
if ( canrun ); then
INSTR=$1 # We can actually assume a single input here
BASE=`basename ${INSTR} .instr`
PYINSTR="${BASE}_generated.py"
JUPYBK="${BASE}_generated.ipynb"
@FLAVOR@-pygen ${INSTR}
jupytext --to ipynb ${PYINSTR}
sed s/INSTRUMENT/${BASE}/g ${UTILDIR}/template.ipynb.in > ${JUPYBK}
jupyter lab ${JUPYBK}
else
@FLAVOR@_errmsg Failed to run Python ${TOOL} - permissions or missing dependencies\?
Expand Down
159 changes: 159 additions & 0 deletions tools/Python/mcrun/template.ipynb.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import mcstasscript as ms\n",
"from INSTRUMENT_generated import make"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"instr=make()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use instr.settings() to add e.g. seed=1000, ncount=1e7, mpi=8, openacc=True, force_compile=False etc.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Show diagram\n",
"instr.show_diagram()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualise with default parameters (defaults to 'window'/pyqtgraph visualisation)\n",
"instr.show_instrument(format='window')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Generate a dataset with default parameters.\n",
"data = instr.backengine()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Overview plot:\n",
"ms.make_sub_plot(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Other useful commands follow...\n",
"# One plot pr. window\n",
"#ms.make_plot(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load another dataset\n",
"#data2 = ms.load_data('some_other_folder')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Adjusting a specific plot\n",
"#ms.name_plot_options(\\\"PSD_4PI\\\", data, log=1, colormap=\\\"hot\\\", orders_of_mag=5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Bring up the 'interface' - only relevant in Jupyter\n",
"#%matplotlib widget\n",
"#import mcstasscript.jb_interface as ms_widget\n",
"#ms_widget.show(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Bring up the simulation 'interface' - only relevant in Jupyter\n",
"#%matplotlib widget\n",
"#import mcstasscript.jb_interface as ms_widget\n",
"#sim_widget = ms_widget.SimInterface(instr)\n",
"#sim_widget.show_interface()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Acessing data from the interface\n",
"#data = sim_widget.get_data()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 00b3563

Please sign in to comment.