PureOne's picture
Release YEORIN-X v4 reproducible research and engineering package
871e4fa verified
Raw History Blame Contribute Delete
1.38 kB
"""Optional compiled selector. Missing library is explicit; no silent claims."""
import ctypes as c
from pathlib import Path
import numpy as np
class NativeSelector:
def __init__(self,path):
self.path=str(Path(path).resolve());self.lib=c.CDLL(self.path)
self.fn=self.lib.yr3_select
p=np.ctypeslib.ndpointer(dtype=np.float64,ndim=1,flags='C_CONTIGUOUS')
self.fn.argtypes=[c.c_int,c.c_int,p,p,p,p,p]+[c.c_double]*7+[c.c_int,p]
self.fn.restype=c.c_int
def select(self,moments,energy,maximum,centers,radii,slack,width,height,fov,pitch_cap,pixel_cap,path_cost=0.,strict=False):
m=np.ascontiguousarray(moments,dtype=np.float64);e=np.ascontiguousarray(energy,dtype=np.float64);mx=np.ascontiguousarray(maximum,dtype=np.float64)
ct=np.ascontiguousarray(centers,dtype=np.float64);rd=np.ascontiguousarray(radii,dtype=np.float64)
if m.ndim!=3 or m.shape[1:]!=(2,15) or e.shape!=(len(m),) or mx.shape!=e.shape or ct.ndim!=2 or ct.shape[1]!=2 or rd.shape!=ct.shape:
raise ValueError('invalid native selector shapes')
out=np.zeros(4)
index=self.fn(len(m),len(ct),m.ravel(),e.ravel(),mx.ravel(),ct.ravel(),rd.ravel(),slack,width,height,fov,pitch_cap,pixel_cap,path_cost,int(strict),out)
if index==-3:raise ValueError('native validation rejected arguments')
return int(index),out