Package qubx :: Package fast :: Module baseline
[hide private]
[frames] | no frames]

Source Code for Module qubx.fast.baseline

  1  """Compiled structure for linear baseline between nodes. 
  2   
  3  Copyright 2008-2012 Research Foundation State University of New York  
  4  This file is part of QUB Express.                                           
  5   
  6  QUB Express is free software; you can redistribute it and/or modify           
  7  it under the terms of the GNU General Public License as published by  
  8  the Free Software Foundation, either version 3 of the License, or     
  9  (at your option) any later version.                                   
 10   
 11  QUB Express is distributed in the hope that it will be useful,                
 12  but WITHOUT ANY WARRANTY; without even the implied warranty of        
 13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 14  GNU General Public License for more details.                          
 15   
 16  You should have received a copy of the GNU General Public License,    
 17  named LICENSE.txt, in the QUB Express program directory.  If not, see         
 18  <http://www.gnu.org/licenses/>.                                       
 19   
 20  """ 
 21   
 22  from qubx.fast.fast_utils import * 
 23   
 24   
25 -class BaselineNodeRec(Structure):
26 _fields_ = [('point', c_int), 27 ('value', c_float)]
28 PBaselineNode = POINTER(BaselineNodeRec) 29 PPBaselineNode = POINTER(PBaselineNode) 30
31 -class BaselineSegRec(Structure):
32 _fields_ = [('first', c_int), 33 ('last', c_int)]
34 PBaselineSeg = POINTER(BaselineSegRec) 35 36 qubfast.QUB_BaselineSeg_Create.argtypes = (c_int, c_int) 37 qubfast.QUB_BaselineSeg_Create.restype = PBaselineSeg 38 qubfast.QUB_BaselineSeg_Free.argtypes = (PBaselineSeg,) 39 qubfast.QUB_BaselineSeg_Free.restype = None 40 qubfast.QUB_BaselineSeg_GetNodeCount.argtypes = (PBaselineSeg,) 41 qubfast.QUB_BaselineSeg_GetNodeCount.restype = c_int 42 qubfast.QUB_BaselineSeg_GetFirstNode.argtypes = (PBaselineSeg,) 43 qubfast.QUB_BaselineSeg_GetFirstNode.restype = PBaselineNode 44 qubfast.QUB_BaselineSeg_GetLastNode.argtypes = (PBaselineSeg,) 45 qubfast.QUB_BaselineSeg_GetLastNode.restype = PBaselineNode 46 qubfast.QUB_BaselineSeg_GetNodeBefore.argtypes = (PBaselineSeg, PBaselineNode) 47 qubfast.QUB_BaselineSeg_GetNodeBefore.restype = PBaselineNode 48 qubfast.QUB_BaselineSeg_GetNodeAfter.argtypes = (PBaselineSeg, PBaselineNode) 49 qubfast.QUB_BaselineSeg_GetNodeAfter.restype = PBaselineNode 50 qubfast.QUB_BaselineSeg_InterpolateValueAt.argtypes = (PBaselineSeg, c_int) 51 qubfast.QUB_BaselineSeg_InterpolateValueAt.restype = c_float 52 qubfast.QUB_BaselineSeg_GetRangeAt.argtypes = (PBaselineSeg, c_int, PPBaselineNode, PPBaselineNode) 53 qubfast.QUB_BaselineSeg_GetRangeAt.restype = c_int 54 qubfast.QUB_BaselineSeg_GetRangeBefore.argtypes = (PBaselineSeg, PPBaselineNode, PPBaselineNode) 55 qubfast.QUB_BaselineSeg_GetRangeBefore.restype = c_int 56 qubfast.QUB_BaselineSeg_GetRangeAfter.argtypes = (PBaselineSeg, PPBaselineNode, PPBaselineNode) 57 qubfast.QUB_BaselineSeg_GetRangeAfter.restype = c_int 58 qubfast.QUB_BaselineSeg_AddNode.argtypes = (PBaselineSeg, c_int, c_float) 59 qubfast.QUB_BaselineSeg_AddNode.restype = PBaselineNode 60 qubfast.QUB_BaselineSeg_AddNodes.argtypes = (PBaselineSeg, c_int, c_int_p, c_float_p) 61 qubfast.QUB_BaselineSeg_AddNodes.restype = PBaselineNode 62 qubfast.QUB_BaselineSeg_ClearNodes.argtypes = (PBaselineSeg, c_int, c_int, c_int) 63 qubfast.QUB_BaselineSeg_ClearNodes.restype = None 64 65
66 -class BaselineSeg(object):
67 - def __init__(self, first, last):
68 self.obj = qubfast.QUB_BaselineSeg_Create(first, last) 69 self.first = first 70 self.last = last
71 - def __del__(self):
72 qubfast.QUB_BaselineSeg_Free(self.obj)
73 - def __len__(self):
74 return qubfast.QUB_BaselineSeg_GetNodeCount(self.obj)
75 - def first_node(self):
76 pnode = qubfast.QUB_BaselineSeg_GetFirstNode(self.obj) 77 if pnode: 78 return BaselineNode(self.obj, pnode) 79 else: 80 return None
81 - def last_node(self):
82 pnode = qubfast.QUB_BaselineSeg_GetLastNode(self.obj) 83 if pnode: 84 return BaselineNode(self.obj, pnode) 85 else: 86 return None
87 - def interpolate(self, point):
88 return qubfast.QUB_BaselineSeg_InterpolateValueAt(self.obj, point)
89 # next 3 return non-zero (negative) for error e.g. out-of-range / past-the-end
90 - def range_at(self, point):
91 a = PBaselineNode() 92 b = PBaselineNode() 93 qubfast.QUB_BaselineSeg_GetRangeAt(self.obj, point, byref(a), byref(b)) 94 if a and b: 95 return BaselineRange(BaselineNode(self.obj, a), BaselineNode(self.obj, b)) 96 else: 97 return None
98 - def add_node(self, point, value):
99 qubfast.QUB_BaselineSeg_AddNode(self.obj, point, value)
100 - def add_nodes(self, points, values):
101 pp = numpy.array(points, dtype='int32') 102 vv = numpy.array(values, dtype='float32') 103 n = min(len(pp), len(vv)) 104 if n: 105 qubfast.QUB_BaselineSeg_AddNodes(self.obj, n, cdata(pp, c_int_p), cdata(vv, c_float_p))
106 - def clear_nodes(self, first, last, repairFirstLast=True):
107 qubfast.QUB_BaselineSeg_ClearNodes(self.obj, first, last, int(repairFirstLast))
108
109 -class BaselineNode(object):
110 - def __init__(self, obj, pnode):
111 self.obj = obj 112 self.pnode = pnode 113 self.point = pnode[0].point 114 self.value = pnode[0].value
115 - def prev(self):
116 pnode = qubfast.QUB_BaselineSeg_GetNodeBefore(self.obj, self.pnode) 117 if pnode: 118 return BaselineNode(self.obj, pnode) 119 else: 120 return None
121 - def next(self):
122 pnode = qubfast.QUB_BaselineSeg_GetNodeAfter(self.obj, self.pnode) 123 if pnode: 124 return BaselineNode(self.obj, pnode) 125 else: 126 return None
127
128 -class BaselineRange(object):
129 - def __init__(self, node_a, node_b):
130 self.node_a = node_a 131 self.node_b = node_b
132 - def prev(self):
133 pnode_a = PBaselineNode() 134 pnode_b = PBaselineNode() 135 ctypes.memmove(byref(pnode_a), byref(self.node_a.pnode), sizeof(pnode_a)) 136 ctypes.memmove(byref(pnode_b), byref(self.node_b.pnode), sizeof(pnode_b)) 137 if qubfast.QUB_BaselineSeg_GetRangeBefore(self.node_a.obj, byref(pnode_a), byref(pnode_b)): 138 return None 139 return BaselineRange(BaselineNode(self.node_a.obj, pnode_a), BaselineNode(self.node_a.obj, pnode_b))
140 - def next(self):
141 pnode_a = PBaselineNode() 142 pnode_b = PBaselineNode() 143 ctypes.memmove(byref(pnode_a), byref(self.node_a.pnode), sizeof(pnode_a)) 144 ctypes.memmove(byref(pnode_b), byref(self.node_b.pnode), sizeof(pnode_b)) 145 if qubfast.QUB_BaselineSeg_GetRangeAfter(self.node_a.obj, byref(pnode_a), byref(pnode_b)): 146 return None 147 return BaselineRange(BaselineNode(self.node_a.obj, pnode_a), BaselineNode(self.node_a.obj, pnode_b))
148