Nanosecond times in Python (#522)

* initital implementation

* datetime replaces with sls::Duration in Python C bindings

* using custom type caster

* fix for conversion to seconds

* added set_count in python

* common header for pybind11 includes

authored-by: Erik Frojdh <erik.frojdh@psi.ch>
This commit is contained in:
Erik Fröjdh
2022-08-26 11:48:40 +02:00
committed by GitHub
parent 3970ed2560
commit 045a28b5de
21 changed files with 2081 additions and 1800 deletions

View File

@ -3,22 +3,20 @@
import os
import sys
import numpy as np
from pathlib import Path
sys.path.append(os.path.join(os.getcwd(), 'bin'))
# from pathlib import Path
# sys.path.append(os.path.join(os.getcwd(), 'bin'))
from slsdet import Detector, Mythen3, Eiger, Jungfrau, DetectorDacs, Dac, Ctb, Gotthard2, Moench
from slsdet import dacIndex, readoutMode
from slsdet.lookup import view, find
import slsdet
from slsdet import DurationWrapper
d = Detector()
e = Eiger()
c = Ctb()
g = Gotthard2()
# j = Jungfrau()
# m = Mythen3()
m = Moench()
t = DurationWrapper(1.0)
d.setExptime(t)
# d.setExptime(0.3)

View File

@ -16,33 +16,40 @@ import time
from pathlib import Path
from parse import system_include_paths, clang_format_version
REDC = '\033[91m'
GREENC = '\033[92m'
ENDC = '\033[0m'
REDC = "\033[91m"
GREENC = "\033[92m"
ENDC = "\033[0m"
def red(msg):
return f'{REDC}{msg}{ENDC}'
return f"{REDC}{msg}{ENDC}"
def green(msg):
return f'{GREENC}{msg}{ENDC}'
return f"{GREENC}{msg}{ENDC}"
def check_clang_format_version(required_version):
if (ver := clang_format_version()) != required_version:
msg = red(f'Clang format version {required_version} required, detected: {ver}. Bye!')
msg = red(
f"Clang format version {required_version} required, detected: {ver}. Bye!"
)
print(msg)
sys.exit(1)
else:
msg = green(f'Found clang-format version {ver}')
msg = green(f"Found clang-format version {ver}")
print(msg)
def check_for_compile_commands_json(path):
# print(f"Looking for compile data base in: {path}")
compile_data_base_file = path/'compile_commands.json'
compile_data_base_file = path / "compile_commands.json"
if not compile_data_base_file.exists():
msg = red(f"No compile_commands.json file found in {path}. Bye!")
print(msg)
sys.exit(1)
else:
msg = green(f'Found: {compile_data_base_file}')
msg = green(f"Found: {compile_data_base_file}")
print(msg)
@ -50,15 +57,13 @@ default_build_path = "/home/l_frojdh/sls/build/"
fpath = "../../slsDetectorSoftware/src/Detector.cpp"
m = []
ag = []
lines = []
ag2 = []
cn = []
def get_arguments(node):
args = [a.type.spelling for a in node.get_arguments()]
args = [
@ -70,18 +75,19 @@ def get_arguments(node):
args = f", {args}"
return args
def get_arguments_with_default(node):
args = []
for arg in node.get_arguments():
tokens = [t.spelling for t in arg.get_tokens()]
# print(tokens)
if '=' in tokens:
if arg.type.spelling == "sls::Positions": #TODO! automate
if "=" in tokens:
if arg.type.spelling == "sls::Positions": # TODO! automate
args.append("py::arg() = Positions{}")
else:
args.append('py::arg()' + ''.join(tokens[tokens.index('='):]))
args.append("py::arg()" + "".join(tokens[tokens.index("=") :]))
else:
args.append('py::arg()')
args.append("py::arg()")
args = ", ".join(args)
if args:
args = f", {args}"
@ -93,17 +99,26 @@ def get_fdec(node):
if node.result_type.spelling:
return_type = node.result_type.spelling
else:
return_type = 'void'
return_type = "void"
if node.is_const_method():
const = 'const'
const = "const"
else:
const = ''
const = ""
args = ", ".join(args)
args = f'({return_type}(Detector::*)({args}){const})'
args = f"({return_type}(Detector::*)({args}){const})"
return args
def time_return_lambda(node, args):
names = ['a', 'b', 'c', 'd']
fa = [a.type.spelling for a in node.get_arguments()]
ca = ','.join(f'{arg} {n}' for arg, n in zip(fa, names))
na = ','.join(names[0:len(fa)])
s = f'CppDetectorApi.def("{node.spelling}",[](sls::Detector& self, {ca}){{ auto r = self.{node.spelling}({na}); \n return std::vector<sls::Duration>(r.begin(), r.end()); }}{args});'
return s
def visit(node):
if node.kind == cindex.CursorKind.CLASS_DECL:
if node.displayname == "Detector":
@ -113,15 +128,15 @@ def visit(node):
and child.access_specifier == cindex.AccessSpecifier.PUBLIC
):
m.append(child)
# args = get_arguments(child)
args = get_arguments_with_default(child)
fs = get_fdec(child)
lines.append(
f'.def("{child.spelling}",{fs} &Detector::{child.spelling}{args})'
f'CppDetectorApi.def("{child.spelling}",{fs} &Detector::{child.spelling}{args});'
)
if cargs.verbose:
print(f'&Detector::{child.spelling}{args})')
print(f"&Detector::{child.spelling}{args})")
cn.append(child)
for child in node.get_children():
visit(child)
@ -139,16 +154,16 @@ if __name__ == "__main__":
"-v",
"--verbose",
help="more output",
action='store_true',
action="store_true",
)
cargs = parser.parse_args()
check_clang_format_version(12)
check_for_compile_commands_json(cargs.build_path)
print("Parsing functions in Detector.h - ", end = "", flush = True)
print("Parsing functions in Detector.h - ", end="", flush=True)
t0 = time.perf_counter()
#parse functions
# parse functions
db = cindex.CompilationDatabase.fromDirectory(cargs.build_path)
index = cindex.Index.create()
args = db.getCompileCommands(fpath)
@ -159,10 +174,10 @@ if __name__ == "__main__":
args = args + incargs
tu = index.parse(fpath, args=args)
visit(tu.cursor)
print(green('OK'))
print(f'Parsing took {time.perf_counter()-t0:.3f}s')
print(green("OK"))
print(f"Parsing took {time.perf_counter()-t0:.3f}s")
print("Read detector_in.cpp - ", end = "")
print("Read detector_in.cpp - ", end="")
with open("../src/detector_in.cpp") as f:
data = f.read()
s = "".join(lines)
@ -170,16 +185,16 @@ if __name__ == "__main__":
text = data.replace("[[FUNCTIONS]]", s)
warning = "/* WARINING This file is auto generated any edits might be overwritten without warning */\n\n"
print(green("OK"))
print("Writing to detector.cpp - ", end = "")
print("Writing to detector.cpp - ", end="")
with open("../src/detector.cpp", "w") as f:
f.write(warning)
f.write(text)
print(green('OK'))
print(green("OK"))
# run clang format on the output
print('Running clang format on generated source -', end = "")
print("Running clang format on generated source -", end="")
subprocess.run(["clang-format", "../src/detector.cpp", "-i"])
print(green(" OK"))
print("Changes since last commit:")
subprocess.run(['git', 'diff', '../src/detector.cpp'])
subprocess.run(["git", "diff", "../src/detector.cpp"])