repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
listlengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
listlengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
ZELLMECHANIK-DRESDEN/dclab
dclab/features/fl_crosstalk.py
get_compensation_matrix
def get_compensation_matrix(ct21, ct31, ct12, ct32, ct13, ct23): """Compute crosstalk inversion matrix The spillover matrix is | | c11 c12 c13 | | | c21 c22 c23 | | | c31 c32 c33 | The diagonal elements are set to 1, i.e. ct11 = c22 = c33 = 1 Parameters ---------- cij: float Spill from channel i to channel j Returns ------- inv: np.ndarray Compensation matrix (inverted spillover matrix) """ ct11 = 1 ct22 = 1 ct33 = 1 if ct21 < 0: raise ValueError("ct21 matrix element must not be negative!") if ct31 < 0: raise ValueError("ct31 matrix element must not be negative!") if ct12 < 0: raise ValueError("ct12 matrix element must not be negative!") if ct32 < 0: raise ValueError("ct32 matrix element must not be negative!") if ct13 < 0: raise ValueError("ct13 matrix element must not be negative!") if ct23 < 0: raise ValueError("ct23 matrix element must not be negative!") crosstalk = np.array([[ct11, ct12, ct13], [ct21, ct22, ct23], [ct31, ct32, ct33], ]) return np.linalg.inv(crosstalk)
python
def get_compensation_matrix(ct21, ct31, ct12, ct32, ct13, ct23): """Compute crosstalk inversion matrix The spillover matrix is | | c11 c12 c13 | | | c21 c22 c23 | | | c31 c32 c33 | The diagonal elements are set to 1, i.e. ct11 = c22 = c33 = 1 Parameters ---------- cij: float Spill from channel i to channel j Returns ------- inv: np.ndarray Compensation matrix (inverted spillover matrix) """ ct11 = 1 ct22 = 1 ct33 = 1 if ct21 < 0: raise ValueError("ct21 matrix element must not be negative!") if ct31 < 0: raise ValueError("ct31 matrix element must not be negative!") if ct12 < 0: raise ValueError("ct12 matrix element must not be negative!") if ct32 < 0: raise ValueError("ct32 matrix element must not be negative!") if ct13 < 0: raise ValueError("ct13 matrix element must not be negative!") if ct23 < 0: raise ValueError("ct23 matrix element must not be negative!") crosstalk = np.array([[ct11, ct12, ct13], [ct21, ct22, ct23], [ct31, ct32, ct33], ]) return np.linalg.inv(crosstalk)
[ "def", "get_compensation_matrix", "(", "ct21", ",", "ct31", ",", "ct12", ",", "ct32", ",", "ct13", ",", "ct23", ")", ":", "ct11", "=", "1", "ct22", "=", "1", "ct33", "=", "1", "if", "ct21", "<", "0", ":", "raise", "ValueError", "(", "\"ct21 matrix el...
Compute crosstalk inversion matrix The spillover matrix is | | c11 c12 c13 | | | c21 c22 c23 | | | c31 c32 c33 | The diagonal elements are set to 1, i.e. ct11 = c22 = c33 = 1 Parameters ---------- cij: float Spill from channel i to channel j Returns ------- inv: np.ndarray Compensation matrix (inverted spillover matrix)
[ "Compute", "crosstalk", "inversion", "matrix" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/fl_crosstalk.py#L9-L58
ZELLMECHANIK-DRESDEN/dclab
dclab/features/fl_crosstalk.py
correct_crosstalk
def correct_crosstalk(fl1, fl2, fl3, fl_channel, ct21=0, ct31=0, ct12=0, ct32=0, ct13=0, ct23=0): """Perform crosstalk correction Parameters ---------- fli: int, float, or np.ndarray Measured fluorescence signals fl_channel: int (1, 2, or 3) The channel number for which the crosstalk-corrected signal should be computed cij: float Spill (crosstalk or bleed-through) from channel i to channel j This spill is computed from the fluorescence signal of e.g. single-stained positive control cells; It is defined by the ratio of the fluorescence signals of the two channels, i.e cij = flj / fli. See Also -------- get_compensation_matrix: compute the inverse crosstalk matrix Notes ----- If there are only two channels (e.g. fl1 and fl2), then the crosstalk to and from the other channel (ct31, ct32, ct13, ct23) should be set to zero. """ fl_channel = int(fl_channel) if fl_channel not in [1, 2, 3]: raise ValueError("`fl_channel` must be 1, 2, or 3!") minv = get_compensation_matrix(ct21=ct21, ct31=ct31, ct12=ct12, ct32=ct32, ct13=ct13, ct23=ct23) col = minv[:, fl_channel - 1].flatten() flout = col[0] * fl1 + col[1] * fl2 + col[2] * fl3 return flout
python
def correct_crosstalk(fl1, fl2, fl3, fl_channel, ct21=0, ct31=0, ct12=0, ct32=0, ct13=0, ct23=0): """Perform crosstalk correction Parameters ---------- fli: int, float, or np.ndarray Measured fluorescence signals fl_channel: int (1, 2, or 3) The channel number for which the crosstalk-corrected signal should be computed cij: float Spill (crosstalk or bleed-through) from channel i to channel j This spill is computed from the fluorescence signal of e.g. single-stained positive control cells; It is defined by the ratio of the fluorescence signals of the two channels, i.e cij = flj / fli. See Also -------- get_compensation_matrix: compute the inverse crosstalk matrix Notes ----- If there are only two channels (e.g. fl1 and fl2), then the crosstalk to and from the other channel (ct31, ct32, ct13, ct23) should be set to zero. """ fl_channel = int(fl_channel) if fl_channel not in [1, 2, 3]: raise ValueError("`fl_channel` must be 1, 2, or 3!") minv = get_compensation_matrix(ct21=ct21, ct31=ct31, ct12=ct12, ct32=ct32, ct13=ct13, ct23=ct23) col = minv[:, fl_channel - 1].flatten() flout = col[0] * fl1 + col[1] * fl2 + col[2] * fl3 return flout
[ "def", "correct_crosstalk", "(", "fl1", ",", "fl2", ",", "fl3", ",", "fl_channel", ",", "ct21", "=", "0", ",", "ct31", "=", "0", ",", "ct12", "=", "0", ",", "ct32", "=", "0", ",", "ct13", "=", "0", ",", "ct23", "=", "0", ")", ":", "fl_channel",...
Perform crosstalk correction Parameters ---------- fli: int, float, or np.ndarray Measured fluorescence signals fl_channel: int (1, 2, or 3) The channel number for which the crosstalk-corrected signal should be computed cij: float Spill (crosstalk or bleed-through) from channel i to channel j This spill is computed from the fluorescence signal of e.g. single-stained positive control cells; It is defined by the ratio of the fluorescence signals of the two channels, i.e cij = flj / fli. See Also -------- get_compensation_matrix: compute the inverse crosstalk matrix Notes ----- If there are only two channels (e.g. fl1 and fl2), then the crosstalk to and from the other channel (ct31, ct32, ct13, ct23) should be set to zero.
[ "Perform", "crosstalk", "correction" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/fl_crosstalk.py#L61-L98
ZELLMECHANIK-DRESDEN/dclab
dclab/features/inert_ratio.py
cont_moments_cv
def cont_moments_cv(cont, flt_epsilon=1.19209e-07, dbl_epsilon=2.2204460492503131e-16): """Compute the moments of a contour The moments are computed in the same way as they are computed in OpenCV's `contourMoments` in `moments.cpp`. Parameters ---------- cont: array of shape (N,2) The contour for which to compute the moments. flt_epsilon: float The value of ``FLT_EPSILON`` in OpenCV/gcc. dbl_epsilon: float The value of ``DBL_EPSILON`` in OpenCV/gcc. Returns ------- moments: dict A dictionary of moments. If the moment `m00` is smaller than half of `flt_epsilon`, `None` is returned. """ # Make sure we have no unsigned integers if np.issubdtype(cont.dtype, np.unsignedinteger): cont = cont.astype(np.int) xi = cont[:, 0] yi = cont[:, 1] xi_1 = np.roll(xi, -1) yi_1 = np.roll(yi, -1) xi_12 = xi_1**2 yi_12 = yi_1**2 xi2 = xi**2 yi2 = yi**2 dxy = xi_1 * yi - xi * yi_1 xii_1 = xi_1 + xi yii_1 = yi_1 + yi a00 = np.sum(dxy) a10 = np.sum(dxy * xii_1) a01 = np.sum(dxy * yii_1) a20 = np.sum(dxy * (xi_1 * xii_1 + xi2)) a11 = np.sum(dxy * (xi_1 * (yii_1 + yi_1) + xi * (yii_1 + yi))) a02 = np.sum(dxy * (yi_1 * yii_1 + yi2)) a30 = np.sum(dxy * xii_1 * (xi_12 + xi2)) a03 = np.sum(dxy * yii_1 * (yi_12 + yi2)) a21 = np.sum(dxy * (xi_12 * (3 * yi_1 + yi) + 2 * xi * xi_1 * yii_1 + xi2 * (yi_1 + 3 * yi))) a12 = np.sum(dxy * (yi_12 * (3 * xi_1 + xi) + 2 * yi * yi_1 * xii_1 + yi2 * (xi_1 + 3 * xi))) if abs(a00) > flt_epsilon: db1_2 = 0.5 db1_6 = 0.16666666666666666666666666666667 db1_12 = 0.083333333333333333333333333333333 db1_24 = 0.041666666666666666666666666666667 db1_20 = 0.05 db1_60 = 0.016666666666666666666666666666667 if a00 < 0: db1_2 *= -1 db1_6 *= -1 db1_12 *= -1 db1_24 *= -1 db1_20 *= -1 db1_60 *= -1 m = dict(m00=a00 * db1_2, m10=a10 * db1_6, m01=a01 * db1_6, m20=a20 * db1_12, m11=a11 * db1_24, m02=a02 * db1_12, m30=a30 * db1_20, m21=a21 * db1_60, m12=a12 * db1_60, m03=a03 * db1_20, ) if m["m00"] > dbl_epsilon: # Center of gravity cx = m["m10"]/m["m00"] cy = m["m01"]/m["m00"] else: cx = 0 cy = 0 # central second order moments m["mu20"] = m["m20"] - m["m10"]*cx m["mu11"] = m["m11"] - m["m10"]*cy m["mu02"] = m["m02"] - m["m01"]*cy m["mu30"] = m["m30"] - cx*(3*m["mu20"] + cx*m["m10"]) m["mu21"] = m["m21"] - cx*(2*m["mu11"] + cx*m["m01"]) - cy*m["mu20"] m["mu12"] = m["m12"] - cy*(2*m["mu11"] + cy*m["m10"]) - cx*m["mu02"] m["mu03"] = m["m03"] - cy*(3*m["mu02"] + cy*m["m01"]) return m else: return None
python
def cont_moments_cv(cont, flt_epsilon=1.19209e-07, dbl_epsilon=2.2204460492503131e-16): """Compute the moments of a contour The moments are computed in the same way as they are computed in OpenCV's `contourMoments` in `moments.cpp`. Parameters ---------- cont: array of shape (N,2) The contour for which to compute the moments. flt_epsilon: float The value of ``FLT_EPSILON`` in OpenCV/gcc. dbl_epsilon: float The value of ``DBL_EPSILON`` in OpenCV/gcc. Returns ------- moments: dict A dictionary of moments. If the moment `m00` is smaller than half of `flt_epsilon`, `None` is returned. """ # Make sure we have no unsigned integers if np.issubdtype(cont.dtype, np.unsignedinteger): cont = cont.astype(np.int) xi = cont[:, 0] yi = cont[:, 1] xi_1 = np.roll(xi, -1) yi_1 = np.roll(yi, -1) xi_12 = xi_1**2 yi_12 = yi_1**2 xi2 = xi**2 yi2 = yi**2 dxy = xi_1 * yi - xi * yi_1 xii_1 = xi_1 + xi yii_1 = yi_1 + yi a00 = np.sum(dxy) a10 = np.sum(dxy * xii_1) a01 = np.sum(dxy * yii_1) a20 = np.sum(dxy * (xi_1 * xii_1 + xi2)) a11 = np.sum(dxy * (xi_1 * (yii_1 + yi_1) + xi * (yii_1 + yi))) a02 = np.sum(dxy * (yi_1 * yii_1 + yi2)) a30 = np.sum(dxy * xii_1 * (xi_12 + xi2)) a03 = np.sum(dxy * yii_1 * (yi_12 + yi2)) a21 = np.sum(dxy * (xi_12 * (3 * yi_1 + yi) + 2 * xi * xi_1 * yii_1 + xi2 * (yi_1 + 3 * yi))) a12 = np.sum(dxy * (yi_12 * (3 * xi_1 + xi) + 2 * yi * yi_1 * xii_1 + yi2 * (xi_1 + 3 * xi))) if abs(a00) > flt_epsilon: db1_2 = 0.5 db1_6 = 0.16666666666666666666666666666667 db1_12 = 0.083333333333333333333333333333333 db1_24 = 0.041666666666666666666666666666667 db1_20 = 0.05 db1_60 = 0.016666666666666666666666666666667 if a00 < 0: db1_2 *= -1 db1_6 *= -1 db1_12 *= -1 db1_24 *= -1 db1_20 *= -1 db1_60 *= -1 m = dict(m00=a00 * db1_2, m10=a10 * db1_6, m01=a01 * db1_6, m20=a20 * db1_12, m11=a11 * db1_24, m02=a02 * db1_12, m30=a30 * db1_20, m21=a21 * db1_60, m12=a12 * db1_60, m03=a03 * db1_20, ) if m["m00"] > dbl_epsilon: # Center of gravity cx = m["m10"]/m["m00"] cy = m["m01"]/m["m00"] else: cx = 0 cy = 0 # central second order moments m["mu20"] = m["m20"] - m["m10"]*cx m["mu11"] = m["m11"] - m["m10"]*cy m["mu02"] = m["m02"] - m["m01"]*cy m["mu30"] = m["m30"] - cx*(3*m["mu20"] + cx*m["m10"]) m["mu21"] = m["m21"] - cx*(2*m["mu11"] + cx*m["m01"]) - cy*m["mu20"] m["mu12"] = m["m12"] - cy*(2*m["mu11"] + cy*m["m10"]) - cx*m["mu02"] m["mu03"] = m["m03"] - cy*(3*m["mu02"] + cy*m["m01"]) return m else: return None
[ "def", "cont_moments_cv", "(", "cont", ",", "flt_epsilon", "=", "1.19209e-07", ",", "dbl_epsilon", "=", "2.2204460492503131e-16", ")", ":", "# Make sure we have no unsigned integers", "if", "np", ".", "issubdtype", "(", "cont", ".", "dtype", ",", "np", ".", "unsig...
Compute the moments of a contour The moments are computed in the same way as they are computed in OpenCV's `contourMoments` in `moments.cpp`. Parameters ---------- cont: array of shape (N,2) The contour for which to compute the moments. flt_epsilon: float The value of ``FLT_EPSILON`` in OpenCV/gcc. dbl_epsilon: float The value of ``DBL_EPSILON`` in OpenCV/gcc. Returns ------- moments: dict A dictionary of moments. If the moment `m00` is smaller than half of `flt_epsilon`, `None` is returned.
[ "Compute", "the", "moments", "of", "a", "contour" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/inert_ratio.py#L10-L114
ZELLMECHANIK-DRESDEN/dclab
dclab/features/inert_ratio.py
get_inert_ratio_cvx
def get_inert_ratio_cvx(cont): """Compute the inertia ratio of the convex hull of a contour The inertia ratio is computed from the central second order of moments along x (mu20) and y (mu02) via `sqrt(mu20/mu02)`. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_cvx: float or ndarray of size N The inertia ratio of the contour's convex hull Notes ----- The contour moments mu20 and mu02 are computed the same way they are computed in OpenCV's `moments.cpp`. See Also -------- get_inert_ratio_raw: Compute inertia ratio of a raw contour References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Central_moments>`__ - `<https://github.com/opencv/opencv/blob/ f81370232a651bdac5042efe907bcaa50a66c487/modules/imgproc/src/ moments.cpp#L93>`__ """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) inert_ratio_cvx = np.zeros(length, dtype=float) * np.nan for ii in range(length): try: chull = ssp.ConvexHull(cont[ii]) except ssp.qhull.QhullError: pass else: hull = cont[ii][chull.vertices, :] inert_ratio_cvx[ii] = get_inert_ratio_raw(hull) if not ret_list: inert_ratio_cvx = inert_ratio_cvx[0] return inert_ratio_cvx
python
def get_inert_ratio_cvx(cont): """Compute the inertia ratio of the convex hull of a contour The inertia ratio is computed from the central second order of moments along x (mu20) and y (mu02) via `sqrt(mu20/mu02)`. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_cvx: float or ndarray of size N The inertia ratio of the contour's convex hull Notes ----- The contour moments mu20 and mu02 are computed the same way they are computed in OpenCV's `moments.cpp`. See Also -------- get_inert_ratio_raw: Compute inertia ratio of a raw contour References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Central_moments>`__ - `<https://github.com/opencv/opencv/blob/ f81370232a651bdac5042efe907bcaa50a66c487/modules/imgproc/src/ moments.cpp#L93>`__ """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) inert_ratio_cvx = np.zeros(length, dtype=float) * np.nan for ii in range(length): try: chull = ssp.ConvexHull(cont[ii]) except ssp.qhull.QhullError: pass else: hull = cont[ii][chull.vertices, :] inert_ratio_cvx[ii] = get_inert_ratio_raw(hull) if not ret_list: inert_ratio_cvx = inert_ratio_cvx[0] return inert_ratio_cvx
[ "def", "get_inert_ratio_cvx", "(", "cont", ")", ":", "if", "isinstance", "(", "cont", ",", "np", ".", "ndarray", ")", ":", "# If cont is an array, it is not a list of contours,", "# because contours can have different lengths.", "cont", "=", "[", "cont", "]", "ret_list"...
Compute the inertia ratio of the convex hull of a contour The inertia ratio is computed from the central second order of moments along x (mu20) and y (mu02) via `sqrt(mu20/mu02)`. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_cvx: float or ndarray of size N The inertia ratio of the contour's convex hull Notes ----- The contour moments mu20 and mu02 are computed the same way they are computed in OpenCV's `moments.cpp`. See Also -------- get_inert_ratio_raw: Compute inertia ratio of a raw contour References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Central_moments>`__ - `<https://github.com/opencv/opencv/blob/ f81370232a651bdac5042efe907bcaa50a66c487/modules/imgproc/src/ moments.cpp#L93>`__
[ "Compute", "the", "inertia", "ratio", "of", "the", "convex", "hull", "of", "a", "contour" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/inert_ratio.py#L117-L178
ZELLMECHANIK-DRESDEN/dclab
dclab/features/inert_ratio.py
get_inert_ratio_prnc
def get_inert_ratio_prnc(cont): """Compute principal inertia ratio of a contour The principal inertia ratio is rotation-invariant, which makes it applicable to reservoir measurements where e.g. cells are not aligned with the channel. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_prnc: float or ndarray of size N The principal inertia ratio of the contour """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) inert_ratio_prnc = np.zeros(length, dtype=float) * np.nan for ii in range(length): moments = cont_moments_cv(cont[ii]) if moments is not None: # orientation of the contour orient = 0.5 * np.arctan2(2 * moments['mu11'], moments['mu02'] - moments['mu20']) # require floating point array (only copy if necessary) cc = np.array(cont[ii], dtype=float, copy=False) # rotate contour rho = np.sqrt(cc[:, 0]**2 + cc[:, 1]**2) phi = np.arctan2(cc[:, 1], cc[:, 0]) + orient + np.pi / 2 cc[:, 0] = rho * np.cos(phi) cc[:, 1] = rho * np.sin(phi) # compute inertia ratio of rotated contour mprnc = cont_moments_cv(cc) inert_ratio_prnc[ii] = np.sqrt(mprnc["mu20"] / mprnc["mu02"]) if not ret_list: inert_ratio_prnc = inert_ratio_prnc[0] return inert_ratio_prnc
python
def get_inert_ratio_prnc(cont): """Compute principal inertia ratio of a contour The principal inertia ratio is rotation-invariant, which makes it applicable to reservoir measurements where e.g. cells are not aligned with the channel. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_prnc: float or ndarray of size N The principal inertia ratio of the contour """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) inert_ratio_prnc = np.zeros(length, dtype=float) * np.nan for ii in range(length): moments = cont_moments_cv(cont[ii]) if moments is not None: # orientation of the contour orient = 0.5 * np.arctan2(2 * moments['mu11'], moments['mu02'] - moments['mu20']) # require floating point array (only copy if necessary) cc = np.array(cont[ii], dtype=float, copy=False) # rotate contour rho = np.sqrt(cc[:, 0]**2 + cc[:, 1]**2) phi = np.arctan2(cc[:, 1], cc[:, 0]) + orient + np.pi / 2 cc[:, 0] = rho * np.cos(phi) cc[:, 1] = rho * np.sin(phi) # compute inertia ratio of rotated contour mprnc = cont_moments_cv(cc) inert_ratio_prnc[ii] = np.sqrt(mprnc["mu20"] / mprnc["mu02"]) if not ret_list: inert_ratio_prnc = inert_ratio_prnc[0] return inert_ratio_prnc
[ "def", "get_inert_ratio_prnc", "(", "cont", ")", ":", "if", "isinstance", "(", "cont", ",", "np", ".", "ndarray", ")", ":", "# If cont is an array, it is not a list of contours,", "# because contours can have different lengths.", "cont", "=", "[", "cont", "]", "ret_list...
Compute principal inertia ratio of a contour The principal inertia ratio is rotation-invariant, which makes it applicable to reservoir measurements where e.g. cells are not aligned with the channel. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_prnc: float or ndarray of size N The principal inertia ratio of the contour
[ "Compute", "principal", "inertia", "ratio", "of", "a", "contour" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/inert_ratio.py#L181-L233
ZELLMECHANIK-DRESDEN/dclab
dclab/features/inert_ratio.py
get_inert_ratio_raw
def get_inert_ratio_raw(cont): """Compute the inertia ratio of a contour The inertia ratio is computed from the central second order of moments along x (mu20) and y (mu02) via `sqrt(mu20/mu02)`. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_raw: float or ndarray of size N The inertia ratio of the contour Notes ----- The contour moments mu20 and mu02 are computed the same way they are computed in OpenCV's `moments.cpp`. See Also -------- get_inert_ratio_cvx: Compute inertia ratio of the convex hull of a contour References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Central_moments>`__ - `<https://github.com/opencv/opencv/blob/ f81370232a651bdac5042efe907bcaa50a66c487/modules/imgproc/src/ moments.cpp#L93>`__ """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) inert_ratio_raw = np.zeros(length, dtype=float) * np.nan for ii in range(length): moments = cont_moments_cv(cont[ii]) if moments is not None: inert_ratio_raw[ii] = np.sqrt(moments["mu20"]/moments["mu02"]) if not ret_list: inert_ratio_raw = inert_ratio_raw[0] return inert_ratio_raw
python
def get_inert_ratio_raw(cont): """Compute the inertia ratio of a contour The inertia ratio is computed from the central second order of moments along x (mu20) and y (mu02) via `sqrt(mu20/mu02)`. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_raw: float or ndarray of size N The inertia ratio of the contour Notes ----- The contour moments mu20 and mu02 are computed the same way they are computed in OpenCV's `moments.cpp`. See Also -------- get_inert_ratio_cvx: Compute inertia ratio of the convex hull of a contour References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Central_moments>`__ - `<https://github.com/opencv/opencv/blob/ f81370232a651bdac5042efe907bcaa50a66c487/modules/imgproc/src/ moments.cpp#L93>`__ """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) inert_ratio_raw = np.zeros(length, dtype=float) * np.nan for ii in range(length): moments = cont_moments_cv(cont[ii]) if moments is not None: inert_ratio_raw[ii] = np.sqrt(moments["mu20"]/moments["mu02"]) if not ret_list: inert_ratio_raw = inert_ratio_raw[0] return inert_ratio_raw
[ "def", "get_inert_ratio_raw", "(", "cont", ")", ":", "if", "isinstance", "(", "cont", ",", "np", ".", "ndarray", ")", ":", "# If cont is an array, it is not a list of contours,", "# because contours can have different lengths.", "cont", "=", "[", "cont", "]", "ret_list"...
Compute the inertia ratio of a contour The inertia ratio is computed from the central second order of moments along x (mu20) and y (mu02) via `sqrt(mu20/mu02)`. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- inert_ratio_raw: float or ndarray of size N The inertia ratio of the contour Notes ----- The contour moments mu20 and mu02 are computed the same way they are computed in OpenCV's `moments.cpp`. See Also -------- get_inert_ratio_cvx: Compute inertia ratio of the convex hull of a contour References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Central_moments>`__ - `<https://github.com/opencv/opencv/blob/ f81370232a651bdac5042efe907bcaa50a66c487/modules/imgproc/src/ moments.cpp#L93>`__
[ "Compute", "the", "inertia", "ratio", "of", "a", "contour" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/inert_ratio.py#L236-L292
ZELLMECHANIK-DRESDEN/dclab
dclab/features/inert_ratio.py
get_tilt
def get_tilt(cont): """Compute tilt of raw contour relative to channel axis Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- tilt: float or ndarray of size N Tilt of the contour in the interval [0, PI/2] References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Examples_2>`__ """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) tilt = np.zeros(length, dtype=float) * np.nan for ii in range(length): moments = cont_moments_cv(cont[ii]) if moments is not None: # orientation of the contour oii = 0.5 * np.arctan2(2 * moments['mu11'], moments['mu02'] - moments['mu20']) # +PI/2 because relative to channel axis tilt[ii] = oii + np.pi/2 # restrict to interval [0,PI/2] tilt = np.mod(tilt, np.pi) tilt[tilt > np.pi/2] -= np.pi tilt = np.abs(tilt) if not ret_list: tilt = tilt[0] return tilt
python
def get_tilt(cont): """Compute tilt of raw contour relative to channel axis Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- tilt: float or ndarray of size N Tilt of the contour in the interval [0, PI/2] References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Examples_2>`__ """ if isinstance(cont, np.ndarray): # If cont is an array, it is not a list of contours, # because contours can have different lengths. cont = [cont] ret_list = False else: ret_list = True length = len(cont) tilt = np.zeros(length, dtype=float) * np.nan for ii in range(length): moments = cont_moments_cv(cont[ii]) if moments is not None: # orientation of the contour oii = 0.5 * np.arctan2(2 * moments['mu11'], moments['mu02'] - moments['mu20']) # +PI/2 because relative to channel axis tilt[ii] = oii + np.pi/2 # restrict to interval [0,PI/2] tilt = np.mod(tilt, np.pi) tilt[tilt > np.pi/2] -= np.pi tilt = np.abs(tilt) if not ret_list: tilt = tilt[0] return tilt
[ "def", "get_tilt", "(", "cont", ")", ":", "if", "isinstance", "(", "cont", ",", "np", ".", "ndarray", ")", ":", "# If cont is an array, it is not a list of contours,", "# because contours can have different lengths.", "cont", "=", "[", "cont", "]", "ret_list", "=", ...
Compute tilt of raw contour relative to channel axis Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. Returns ------- tilt: float or ndarray of size N Tilt of the contour in the interval [0, PI/2] References ---------- - `<https://en.wikipedia.org/wiki/Image_moment#Examples_2>`__
[ "Compute", "tilt", "of", "raw", "contour", "relative", "to", "channel", "axis" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/inert_ratio.py#L295-L344
deep-compute/funcserver
funcserver/funcserver.py
tag
def tag(*tags): ''' Constructs a decorator that tags a function with specified strings (@tags). The tags on the decorated function are available via fn.tags ''' def dfn(fn): _tags = getattr(fn, 'tags', set()) _tags.update(tags) fn.tags = _tags return fn return dfn
python
def tag(*tags): ''' Constructs a decorator that tags a function with specified strings (@tags). The tags on the decorated function are available via fn.tags ''' def dfn(fn): _tags = getattr(fn, 'tags', set()) _tags.update(tags) fn.tags = _tags return fn return dfn
[ "def", "tag", "(", "*", "tags", ")", ":", "def", "dfn", "(", "fn", ")", ":", "_tags", "=", "getattr", "(", "fn", ",", "'tags'", ",", "set", "(", ")", ")", "_tags", ".", "update", "(", "tags", ")", "fn", ".", "tags", "=", "_tags", "return", "f...
Constructs a decorator that tags a function with specified strings (@tags). The tags on the decorated function are available via fn.tags
[ "Constructs", "a", "decorator", "that", "tags", "a", "function", "with", "specified", "strings", "(" ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L42-L53
deep-compute/funcserver
funcserver/funcserver.py
raw
def raw(mime='application/octet-stream'): ''' Constructs a decorator that marks the fn as raw response format ''' def dfn(fn): tags = getattr(fn, 'tags', set()) tags.add('raw') fn.tags = tags fn.mime = getattr(fn, 'mime', mime) return fn return dfn
python
def raw(mime='application/octet-stream'): ''' Constructs a decorator that marks the fn as raw response format ''' def dfn(fn): tags = getattr(fn, 'tags', set()) tags.add('raw') fn.tags = tags fn.mime = getattr(fn, 'mime', mime) return fn return dfn
[ "def", "raw", "(", "mime", "=", "'application/octet-stream'", ")", ":", "def", "dfn", "(", "fn", ")", ":", "tags", "=", "getattr", "(", "fn", ",", "'tags'", ",", "set", "(", ")", ")", "tags", ".", "add", "(", "'raw'", ")", "fn", ".", "tags", "=",...
Constructs a decorator that marks the fn as raw response format
[ "Constructs", "a", "decorator", "that", "marks", "the", "fn", "as", "raw", "response", "format" ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L69-L80
deep-compute/funcserver
funcserver/funcserver.py
WSConnection.open
def open(self, pysession_id): ''' Called when client opens connection. Initialization is done here. ''' self.id = id(self) self.funcserver = self.application.funcserver self.pysession_id = pysession_id # register this connection with node self.state = self.funcserver.websocks[self.id] = {'id': self.id, 'sock': self}
python
def open(self, pysession_id): ''' Called when client opens connection. Initialization is done here. ''' self.id = id(self) self.funcserver = self.application.funcserver self.pysession_id = pysession_id # register this connection with node self.state = self.funcserver.websocks[self.id] = {'id': self.id, 'sock': self}
[ "def", "open", "(", "self", ",", "pysession_id", ")", ":", "self", ".", "id", "=", "id", "(", "self", ")", "self", ".", "funcserver", "=", "self", ".", "application", ".", "funcserver", "self", ".", "pysession_id", "=", "pysession_id", "# register this con...
Called when client opens connection. Initialization is done here.
[ "Called", "when", "client", "opens", "connection", ".", "Initialization", "is", "done", "here", "." ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L115-L125
deep-compute/funcserver
funcserver/funcserver.py
WSConnection.on_message
def on_message(self, msg): ''' Called when client sends a message. Supports a python debugging console. This forms the "eval" part of a standard read-eval-print loop. Currently the only implementation of the python console is in the WebUI but the implementation of a terminal based console is planned. ''' msg = json.loads(msg) psession = self.funcserver.pysessions.get(self.pysession_id, None) if psession is None: interpreter = PyInterpreter(self.funcserver.define_python_namespace()) psession = dict(interpreter=interpreter, socks=set([self.id])) self.funcserver.pysessions[self.pysession_id] = psession else: interpreter = psession['interpreter'] psession['socks'].add(self.id) code = msg['code'] msg_id = msg['id'] stdout = sys.stdout try: sys.stdout = cStringIO.StringIO() interpreter.runsource(code) output = sys.stdout.getvalue() or interpreter.output if isinstance(output, list): output = ''.join(output) interpreter.output = [] finally: sys.stdout = stdout msg = {'type': MSG_TYPE_CONSOLE, 'id': msg_id, 'data': output} self.send_message(msg)
python
def on_message(self, msg): ''' Called when client sends a message. Supports a python debugging console. This forms the "eval" part of a standard read-eval-print loop. Currently the only implementation of the python console is in the WebUI but the implementation of a terminal based console is planned. ''' msg = json.loads(msg) psession = self.funcserver.pysessions.get(self.pysession_id, None) if psession is None: interpreter = PyInterpreter(self.funcserver.define_python_namespace()) psession = dict(interpreter=interpreter, socks=set([self.id])) self.funcserver.pysessions[self.pysession_id] = psession else: interpreter = psession['interpreter'] psession['socks'].add(self.id) code = msg['code'] msg_id = msg['id'] stdout = sys.stdout try: sys.stdout = cStringIO.StringIO() interpreter.runsource(code) output = sys.stdout.getvalue() or interpreter.output if isinstance(output, list): output = ''.join(output) interpreter.output = [] finally: sys.stdout = stdout msg = {'type': MSG_TYPE_CONSOLE, 'id': msg_id, 'data': output} self.send_message(msg)
[ "def", "on_message", "(", "self", ",", "msg", ")", ":", "msg", "=", "json", ".", "loads", "(", "msg", ")", "psession", "=", "self", ".", "funcserver", ".", "pysessions", ".", "get", "(", "self", ".", "pysession_id", ",", "None", ")", "if", "psession"...
Called when client sends a message. Supports a python debugging console. This forms the "eval" part of a standard read-eval-print loop. Currently the only implementation of the python console is in the WebUI but the implementation of a terminal based console is planned.
[ "Called", "when", "client", "sends", "a", "message", "." ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L127-L164
deep-compute/funcserver
funcserver/funcserver.py
WSConnection.on_close
def on_close(self): ''' Called when client closes this connection. Cleanup is done here. ''' if self.id in self.funcserver.websocks: self.funcserver.websocks[self.id] = None ioloop = tornado.ioloop.IOLoop.instance() ioloop.add_callback(lambda: self.funcserver.websocks.pop(self.id, None)) psession = self.funcserver.pysessions.get(self.pysession_id, None) if psession: psession['socks'].remove(self.id) if not psession['socks']: del self.funcserver.pysessions[self.pysession_id]
python
def on_close(self): ''' Called when client closes this connection. Cleanup is done here. ''' if self.id in self.funcserver.websocks: self.funcserver.websocks[self.id] = None ioloop = tornado.ioloop.IOLoop.instance() ioloop.add_callback(lambda: self.funcserver.websocks.pop(self.id, None)) psession = self.funcserver.pysessions.get(self.pysession_id, None) if psession: psession['socks'].remove(self.id) if not psession['socks']: del self.funcserver.pysessions[self.pysession_id]
[ "def", "on_close", "(", "self", ")", ":", "if", "self", ".", "id", "in", "self", ".", "funcserver", ".", "websocks", ":", "self", ".", "funcserver", ".", "websocks", "[", "self", ".", "id", "]", "=", "None", "ioloop", "=", "tornado", ".", "ioloop", ...
Called when client closes this connection. Cleanup is done here.
[ "Called", "when", "client", "closes", "this", "connection", ".", "Cleanup", "is", "done", "here", "." ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L166-L181
deep-compute/funcserver
funcserver/funcserver.py
RPCHandler._clean_kwargs
def _clean_kwargs(self, kwargs, fn): ''' Remove unexpected keyword arguments from the set of received keyword arguments. ''' # Do not do the cleaning if server config # doesnt ask to ignore if not self.server.IGNORE_UNEXPECTED_KWARGS: return kwargs expected_kwargs = set(inspect.getargspec(fn).args) got_kwargs = set(kwargs.keys()) unexpected_kwargs = got_kwargs - expected_kwargs for k in unexpected_kwargs: del kwargs[k] return kwargs
python
def _clean_kwargs(self, kwargs, fn): ''' Remove unexpected keyword arguments from the set of received keyword arguments. ''' # Do not do the cleaning if server config # doesnt ask to ignore if not self.server.IGNORE_UNEXPECTED_KWARGS: return kwargs expected_kwargs = set(inspect.getargspec(fn).args) got_kwargs = set(kwargs.keys()) unexpected_kwargs = got_kwargs - expected_kwargs for k in unexpected_kwargs: del kwargs[k] return kwargs
[ "def", "_clean_kwargs", "(", "self", ",", "kwargs", ",", "fn", ")", ":", "# Do not do the cleaning if server config", "# doesnt ask to ignore", "if", "not", "self", ".", "server", ".", "IGNORE_UNEXPECTED_KWARGS", ":", "return", "kwargs", "expected_kwargs", "=", "set",...
Remove unexpected keyword arguments from the set of received keyword arguments.
[ "Remove", "unexpected", "keyword", "arguments", "from", "the", "set", "of", "received", "keyword", "arguments", "." ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L301-L317
deep-compute/funcserver
funcserver/funcserver.py
Server.dump_stacks
def dump_stacks(self): ''' Dumps the stack of all threads. This function is meant for debugging. Useful when a deadlock happens. borrowed from: http://blog.ziade.org/2012/05/25/zmq-and-gevent-debugging-nightmares/ ''' dump = [] # threads threads = dict([(th.ident, th.name) for th in threading.enumerate()]) for thread, frame in sys._current_frames().items(): if thread not in threads: continue dump.append('Thread 0x%x (%s)\n' % (thread, threads[thread])) dump.append(''.join(traceback.format_stack(frame))) dump.append('\n') return ''.join(dump)
python
def dump_stacks(self): ''' Dumps the stack of all threads. This function is meant for debugging. Useful when a deadlock happens. borrowed from: http://blog.ziade.org/2012/05/25/zmq-and-gevent-debugging-nightmares/ ''' dump = [] # threads threads = dict([(th.ident, th.name) for th in threading.enumerate()]) for thread, frame in sys._current_frames().items(): if thread not in threads: continue dump.append('Thread 0x%x (%s)\n' % (thread, threads[thread])) dump.append(''.join(traceback.format_stack(frame))) dump.append('\n') return ''.join(dump)
[ "def", "dump_stacks", "(", "self", ")", ":", "dump", "=", "[", "]", "# threads", "threads", "=", "dict", "(", "[", "(", "th", ".", "ident", ",", "th", ".", "name", ")", "for", "th", "in", "threading", ".", "enumerate", "(", ")", "]", ")", "for", ...
Dumps the stack of all threads. This function is meant for debugging. Useful when a deadlock happens. borrowed from: http://blog.ziade.org/2012/05/25/zmq-and-gevent-debugging-nightmares/
[ "Dumps", "the", "stack", "of", "all", "threads", ".", "This", "function", "is", "meant", "for", "debugging", ".", "Useful", "when", "a", "deadlock", "happens", "." ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L522-L542
deep-compute/funcserver
funcserver/funcserver.py
Server.define_log_pre_format_hooks
def define_log_pre_format_hooks(self): """ adds a hook to send to websocket if the run command was selected """ hooks = super(Server, self).define_log_pre_format_hooks() # NOTE enabling logs only on debug mode if self.args.func == self.run and self.args.debug: hooks.append(self._send_log_to_ws) return hooks
python
def define_log_pre_format_hooks(self): """ adds a hook to send to websocket if the run command was selected """ hooks = super(Server, self).define_log_pre_format_hooks() # NOTE enabling logs only on debug mode if self.args.func == self.run and self.args.debug: hooks.append(self._send_log_to_ws) return hooks
[ "def", "define_log_pre_format_hooks", "(", "self", ")", ":", "hooks", "=", "super", "(", "Server", ",", "self", ")", ".", "define_log_pre_format_hooks", "(", ")", "# NOTE enabling logs only on debug mode", "if", "self", ".", "args", ".", "func", "==", "self", "....
adds a hook to send to websocket if the run command was selected
[ "adds", "a", "hook", "to", "send", "to", "websocket", "if", "the", "run", "command", "was", "selected" ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L563-L572
deep-compute/funcserver
funcserver/funcserver.py
Server.run
def run(self): """ prepares the api and starts the tornado funcserver """ self.log_id = 0 # all active websockets and their state self.websocks = {} # all active python interpreter sessions self.pysessions = {} if self.DISABLE_REQUESTS_DEBUG_LOGS: disable_requests_debug_logs() self.threadpool = ThreadPool(self.THREADPOOL_WORKERS) self.api = None # tornado app object base_handlers = self.prepare_base_handlers() handlers = self.prepare_handlers() self.template_loader = TemplateLoader([resolve_path(self.TEMPLATE_PATH)]) _ = self.prepare_template_loader(self.template_loader) if _ is not None: self.template_loader = _ shclass = CustomStaticFileHandler shclass.PATHS.append(resolve_path(self.STATIC_PATH)) _ = self.prepare_static_paths(shclass.PATHS) if _ is not None: shclass.PATHS = _ self.static_handler_class = shclass self.nav_tabs = [('Home', '/')] if self.args.debug: self.nav_tabs += [('Console', '/console'), ('Logs', '/logs')] self.nav_tabs = self.prepare_nav_tabs(self.nav_tabs) settings = { 'static_path': '<DUMMY-INEXISTENT-PATH>', 'static_handler_class': self.static_handler_class, 'template_loader': self.template_loader, 'compress_response': True, 'debug': self.args.debug, } all_handlers = handlers + base_handlers self.app = self.APP_CLASS(**settings) self.app.add_handlers(self.VIRTUAL_HOST, all_handlers) sys.funcserver = self.app.funcserver = self self.api = self.prepare_api() if self.api is not None and not hasattr(self.api, 'log'): self.api.log = self.log if self.args.port != 0: self.app.listen(self.args.port) tornado.ioloop.IOLoop.instance().start()
python
def run(self): """ prepares the api and starts the tornado funcserver """ self.log_id = 0 # all active websockets and their state self.websocks = {} # all active python interpreter sessions self.pysessions = {} if self.DISABLE_REQUESTS_DEBUG_LOGS: disable_requests_debug_logs() self.threadpool = ThreadPool(self.THREADPOOL_WORKERS) self.api = None # tornado app object base_handlers = self.prepare_base_handlers() handlers = self.prepare_handlers() self.template_loader = TemplateLoader([resolve_path(self.TEMPLATE_PATH)]) _ = self.prepare_template_loader(self.template_loader) if _ is not None: self.template_loader = _ shclass = CustomStaticFileHandler shclass.PATHS.append(resolve_path(self.STATIC_PATH)) _ = self.prepare_static_paths(shclass.PATHS) if _ is not None: shclass.PATHS = _ self.static_handler_class = shclass self.nav_tabs = [('Home', '/')] if self.args.debug: self.nav_tabs += [('Console', '/console'), ('Logs', '/logs')] self.nav_tabs = self.prepare_nav_tabs(self.nav_tabs) settings = { 'static_path': '<DUMMY-INEXISTENT-PATH>', 'static_handler_class': self.static_handler_class, 'template_loader': self.template_loader, 'compress_response': True, 'debug': self.args.debug, } all_handlers = handlers + base_handlers self.app = self.APP_CLASS(**settings) self.app.add_handlers(self.VIRTUAL_HOST, all_handlers) sys.funcserver = self.app.funcserver = self self.api = self.prepare_api() if self.api is not None and not hasattr(self.api, 'log'): self.api.log = self.log if self.args.port != 0: self.app.listen(self.args.port) tornado.ioloop.IOLoop.instance().start()
[ "def", "run", "(", "self", ")", ":", "self", ".", "log_id", "=", "0", "# all active websockets and their state", "self", ".", "websocks", "=", "{", "}", "# all active python interpreter sessions", "self", ".", "pysessions", "=", "{", "}", "if", "self", ".", "D...
prepares the api and starts the tornado funcserver
[ "prepares", "the", "api", "and", "starts", "the", "tornado", "funcserver" ]
train
https://github.com/deep-compute/funcserver/blob/ce3418cb4a0cb85f0a3cbf86d12ea9733ca23f23/funcserver/funcserver.py#L656-L713
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
_get_sql
def _get_sql(filename): """Returns the contents of the sql file from the given ``filename``.""" with open(os.path.join(SQL_DIR, filename), 'r') as f: return f.read()
python
def _get_sql(filename): """Returns the contents of the sql file from the given ``filename``.""" with open(os.path.join(SQL_DIR, filename), 'r') as f: return f.read()
[ "def", "_get_sql", "(", "filename", ")", ":", "with", "open", "(", "os", ".", "path", ".", "join", "(", "SQL_DIR", ",", "filename", ")", ",", "'r'", ")", "as", "f", ":", "return", "f", ".", "read", "(", ")" ]
Returns the contents of the sql file from the given ``filename``.
[ "Returns", "the", "contents", "of", "the", "sql", "file", "from", "the", "given", "filename", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L32-L35
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
verify_id_n_version
def verify_id_n_version(id, version): """Given an ``id`` and ``version``, verify the identified content exists. """ stmt = _get_sql('verify-id-and-version.sql') args = dict(id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: valid = cursor.fetchone()[0] except TypeError: raise NotFound(join_ident_hash(id, version)) return True
python
def verify_id_n_version(id, version): """Given an ``id`` and ``version``, verify the identified content exists. """ stmt = _get_sql('verify-id-and-version.sql') args = dict(id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: valid = cursor.fetchone()[0] except TypeError: raise NotFound(join_ident_hash(id, version)) return True
[ "def", "verify_id_n_version", "(", "id", ",", "version", ")", ":", "stmt", "=", "_get_sql", "(", "'verify-id-and-version.sql'", ")", "args", "=", "dict", "(", "id", "=", "id", ",", "version", "=", "version", ")", "with", "db_connect", "(", ")", "as", "db...
Given an ``id`` and ``version``, verify the identified content exists.
[ "Given", "an", "id", "and", "version", "verify", "the", "identified", "content", "exists", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L38-L52
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_id_n_version
def get_id_n_version(ident_hash): """From the given ``ident_hash`` return the id and version.""" try: id, version = split_ident_hash(ident_hash) except IdentHashMissingVersion: # XXX Don't import from views... And don't use httpexceptions from pyramid.httpexceptions import HTTPNotFound from cnxarchive.views.helpers import get_latest_version try: version = get_latest_version(ident_hash) except HTTPNotFound: raise NotFound(ident_hash) id, version = split_ident_hash(join_ident_hash(ident_hash, version)) else: verify_id_n_version(id, version) return id, version
python
def get_id_n_version(ident_hash): """From the given ``ident_hash`` return the id and version.""" try: id, version = split_ident_hash(ident_hash) except IdentHashMissingVersion: # XXX Don't import from views... And don't use httpexceptions from pyramid.httpexceptions import HTTPNotFound from cnxarchive.views.helpers import get_latest_version try: version = get_latest_version(ident_hash) except HTTPNotFound: raise NotFound(ident_hash) id, version = split_ident_hash(join_ident_hash(ident_hash, version)) else: verify_id_n_version(id, version) return id, version
[ "def", "get_id_n_version", "(", "ident_hash", ")", ":", "try", ":", "id", ",", "version", "=", "split_ident_hash", "(", "ident_hash", ")", "except", "IdentHashMissingVersion", ":", "# XXX Don't import from views... And don't use httpexceptions", "from", "pyramid", ".", ...
From the given ``ident_hash`` return the id and version.
[ "From", "the", "given", "ident_hash", "return", "the", "id", "and", "version", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L55-L71
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_type
def get_type(ident_hash): """Return the database type for the given ``ident_hash`` As of now, this could either be a 'Module' or 'Collection'. """ id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-type.sql') args = dict(id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) type = cursor.fetchone()[0] return type
python
def get_type(ident_hash): """Return the database type for the given ``ident_hash`` As of now, this could either be a 'Module' or 'Collection'. """ id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-type.sql') args = dict(id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) type = cursor.fetchone()[0] return type
[ "def", "get_type", "(", "ident_hash", ")", ":", "id", ",", "version", "=", "get_id_n_version", "(", "ident_hash", ")", "stmt", "=", "_get_sql", "(", "'get-type.sql'", ")", "args", "=", "dict", "(", "id", "=", "id", ",", "version", "=", "version", ")", ...
Return the database type for the given ``ident_hash`` As of now, this could either be a 'Module' or 'Collection'.
[ "Return", "the", "database", "type", "for", "the", "given", "ident_hash", "As", "of", "now", "this", "could", "either", "be", "a", "Module", "or", "Collection", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L74-L88
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_metadata
def get_metadata(ident_hash): """Return the dictionary of metadata from the database. This data is keyed using the cnx-epub data structure. """ id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-metadata.sql') args = dict(id=id, version=version) # FIXME The license_url and license_text metadata attributes need to # change to a License structure similar to what is used in cnx-authoring. with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: metadata = cursor.fetchone()[0] except TypeError: raise NotFound(ident_hash) return metadata
python
def get_metadata(ident_hash): """Return the dictionary of metadata from the database. This data is keyed using the cnx-epub data structure. """ id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-metadata.sql') args = dict(id=id, version=version) # FIXME The license_url and license_text metadata attributes need to # change to a License structure similar to what is used in cnx-authoring. with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: metadata = cursor.fetchone()[0] except TypeError: raise NotFound(ident_hash) return metadata
[ "def", "get_metadata", "(", "ident_hash", ")", ":", "id", ",", "version", "=", "get_id_n_version", "(", "ident_hash", ")", "stmt", "=", "_get_sql", "(", "'get-metadata.sql'", ")", "args", "=", "dict", "(", "id", "=", "id", ",", "version", "=", "version", ...
Return the dictionary of metadata from the database. This data is keyed using the cnx-epub data structure.
[ "Return", "the", "dictionary", "of", "metadata", "from", "the", "database", ".", "This", "data", "is", "keyed", "using", "the", "cnx", "-", "epub", "data", "structure", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L91-L111
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_content
def get_content(ident_hash, context=None): """Returns the content for the given ``ident_hash``. ``context`` is optionally ident-hash used to find the content within the context of a Collection ident_hash. """ id, version = get_id_n_version(ident_hash) filename = 'index.cnxml.html' if context is not None: stmt = _get_sql('get-baked-content.sql') args = dict(id=id, version=version, context=context) else: stmt = _get_sql('get-content.sql') args = dict(id=id, version=version, filename=filename) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: content, _ = cursor.fetchone() except TypeError: raise ContentNotFound(ident_hash, context, filename) return content[:]
python
def get_content(ident_hash, context=None): """Returns the content for the given ``ident_hash``. ``context`` is optionally ident-hash used to find the content within the context of a Collection ident_hash. """ id, version = get_id_n_version(ident_hash) filename = 'index.cnxml.html' if context is not None: stmt = _get_sql('get-baked-content.sql') args = dict(id=id, version=version, context=context) else: stmt = _get_sql('get-content.sql') args = dict(id=id, version=version, filename=filename) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: content, _ = cursor.fetchone() except TypeError: raise ContentNotFound(ident_hash, context, filename) return content[:]
[ "def", "get_content", "(", "ident_hash", ",", "context", "=", "None", ")", ":", "id", ",", "version", "=", "get_id_n_version", "(", "ident_hash", ")", "filename", "=", "'index.cnxml.html'", "if", "context", "is", "not", "None", ":", "stmt", "=", "_get_sql", ...
Returns the content for the given ``ident_hash``. ``context`` is optionally ident-hash used to find the content within the context of a Collection ident_hash.
[ "Returns", "the", "content", "for", "the", "given", "ident_hash", ".", "context", "is", "optionally", "ident", "-", "hash", "used", "to", "find", "the", "content", "within", "the", "context", "of", "a", "Collection", "ident_hash", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L114-L137
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_file_info
def get_file_info(hash, context=None): """Returns information about the file, identified by ``hash``. If the `context` (an ident-hash) is supplied, the information returned will be specific to that context. """ if context is None: stmt = _get_sql('get-file-info.sql') args = dict(hash=hash) else: stmt = _get_sql('get-file-info-in-context.sql') id, version = get_id_n_version(context) args = dict(hash=hash, id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: filename, media_type = cursor.fetchone() except TypeError: raise FileNotFound(hash) return filename, media_type
python
def get_file_info(hash, context=None): """Returns information about the file, identified by ``hash``. If the `context` (an ident-hash) is supplied, the information returned will be specific to that context. """ if context is None: stmt = _get_sql('get-file-info.sql') args = dict(hash=hash) else: stmt = _get_sql('get-file-info-in-context.sql') id, version = get_id_n_version(context) args = dict(hash=hash, id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: filename, media_type = cursor.fetchone() except TypeError: raise FileNotFound(hash) return filename, media_type
[ "def", "get_file_info", "(", "hash", ",", "context", "=", "None", ")", ":", "if", "context", "is", "None", ":", "stmt", "=", "_get_sql", "(", "'get-file-info.sql'", ")", "args", "=", "dict", "(", "hash", "=", "hash", ")", "else", ":", "stmt", "=", "_...
Returns information about the file, identified by ``hash``. If the `context` (an ident-hash) is supplied, the information returned will be specific to that context.
[ "Returns", "information", "about", "the", "file", "identified", "by", "hash", ".", "If", "the", "context", "(", "an", "ident", "-", "hash", ")", "is", "supplied", "the", "information", "returned", "will", "be", "specific", "to", "that", "context", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L140-L161
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_file
def get_file(hash): """Return the contents of the file as a ``memoryview``.""" stmt = _get_sql('get-file.sql') args = dict(hash=hash) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: file, _ = cursor.fetchone() except TypeError: raise FileNotFound(hash) return memoryview(file[:])
python
def get_file(hash): """Return the contents of the file as a ``memoryview``.""" stmt = _get_sql('get-file.sql') args = dict(hash=hash) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: file, _ = cursor.fetchone() except TypeError: raise FileNotFound(hash) return memoryview(file[:])
[ "def", "get_file", "(", "hash", ")", ":", "stmt", "=", "_get_sql", "(", "'get-file.sql'", ")", "args", "=", "dict", "(", "hash", "=", "hash", ")", "with", "db_connect", "(", ")", "as", "db_conn", ":", "with", "db_conn", ".", "cursor", "(", ")", "as",...
Return the contents of the file as a ``memoryview``.
[ "Return", "the", "contents", "of", "the", "file", "as", "a", "memoryview", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L164-L176
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_registered_files
def get_registered_files(ident_hash): """Returns a list SHA1 hashes for registered file entries identified by the given module ``ident_hash``. Note, it's possible for a module to reference a file without having a registered file entry for it. Note, all files are included, including the raw form of the content. """ id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-registered-files-info.sql') args = dict(id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) rows = cursor.fetchall() if rows is None: rows = [] hashes = list(set([sha1 for sha1, _, __ in rows])) return hashes
python
def get_registered_files(ident_hash): """Returns a list SHA1 hashes for registered file entries identified by the given module ``ident_hash``. Note, it's possible for a module to reference a file without having a registered file entry for it. Note, all files are included, including the raw form of the content. """ id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-registered-files-info.sql') args = dict(id=id, version=version) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) rows = cursor.fetchall() if rows is None: rows = [] hashes = list(set([sha1 for sha1, _, __ in rows])) return hashes
[ "def", "get_registered_files", "(", "ident_hash", ")", ":", "id", ",", "version", "=", "get_id_n_version", "(", "ident_hash", ")", "stmt", "=", "_get_sql", "(", "'get-registered-files-info.sql'", ")", "args", "=", "dict", "(", "id", "=", "id", ",", "version", ...
Returns a list SHA1 hashes for registered file entries identified by the given module ``ident_hash``. Note, it's possible for a module to reference a file without having a registered file entry for it. Note, all files are included, including the raw form of the content.
[ "Returns", "a", "list", "SHA1", "hashes", "for", "registered", "file", "entries", "identified", "by", "the", "given", "module", "ident_hash", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L179-L201
openstax/cnx-archive
cnxarchive/scripts/export_epub/db.py
get_tree
def get_tree(ident_hash, baked=False): """Return a tree structure of the Collection""" id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-tree.sql') args = dict(id=id, version=version, baked=baked) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: tree = cursor.fetchone()[0] except TypeError: raise NotFound(ident_hash) if tree is None: raise NotFound(ident_hash) return tree
python
def get_tree(ident_hash, baked=False): """Return a tree structure of the Collection""" id, version = get_id_n_version(ident_hash) stmt = _get_sql('get-tree.sql') args = dict(id=id, version=version, baked=baked) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute(stmt, args) try: tree = cursor.fetchone()[0] except TypeError: raise NotFound(ident_hash) if tree is None: raise NotFound(ident_hash) return tree
[ "def", "get_tree", "(", "ident_hash", ",", "baked", "=", "False", ")", ":", "id", ",", "version", "=", "get_id_n_version", "(", "ident_hash", ")", "stmt", "=", "_get_sql", "(", "'get-tree.sql'", ")", "args", "=", "dict", "(", "id", "=", "id", ",", "ver...
Return a tree structure of the Collection
[ "Return", "a", "tree", "structure", "of", "the", "Collection" ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/db.py#L204-L221
openstax/cnx-archive
cnxarchive/scripts/inject_resource.py
guess_media_type
def guess_media_type(filepath): """Returns the media-type of the file at the given ``filepath``""" o = subprocess.check_output(['file', '--mime-type', '-Lb', filepath]) o = o.strip() return o
python
def guess_media_type(filepath): """Returns the media-type of the file at the given ``filepath``""" o = subprocess.check_output(['file', '--mime-type', '-Lb', filepath]) o = o.strip() return o
[ "def", "guess_media_type", "(", "filepath", ")", ":", "o", "=", "subprocess", ".", "check_output", "(", "[", "'file'", ",", "'--mime-type'", ",", "'-Lb'", ",", "filepath", "]", ")", "o", "=", "o", ".", "strip", "(", ")", "return", "o" ]
Returns the media-type of the file at the given ``filepath``
[ "Returns", "the", "media", "-", "type", "of", "the", "file", "at", "the", "given", "filepath" ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/inject_resource.py#L29-L33
openstax/cnx-archive
cnxarchive/scripts/inject_resource.py
lookup_module_ident
def lookup_module_ident(id, version): """Return the ``module_ident`` for the given ``id`` & major and minor version as a tuple. """ with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute( "SELECT module_ident FROM modules " "WHERE uuid = %s " "AND CONCAT_WS('.', major_version, minor_version) = %s", (id, version)) try: mident = cursor.fetchone()[0] except (IndexError, TypeError): ident_hash = join_ident_hash(id, version) raise RuntimeError("Content at {} does not exist." .format(ident_hash)) return mident
python
def lookup_module_ident(id, version): """Return the ``module_ident`` for the given ``id`` & major and minor version as a tuple. """ with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute( "SELECT module_ident FROM modules " "WHERE uuid = %s " "AND CONCAT_WS('.', major_version, minor_version) = %s", (id, version)) try: mident = cursor.fetchone()[0] except (IndexError, TypeError): ident_hash = join_ident_hash(id, version) raise RuntimeError("Content at {} does not exist." .format(ident_hash)) return mident
[ "def", "lookup_module_ident", "(", "id", ",", "version", ")", ":", "with", "db_connect", "(", ")", "as", "db_conn", ":", "with", "db_conn", ".", "cursor", "(", ")", "as", "cursor", ":", "cursor", ".", "execute", "(", "\"SELECT module_ident FROM modules \"", ...
Return the ``module_ident`` for the given ``id`` & major and minor version as a tuple.
[ "Return", "the", "module_ident", "for", "the", "given", "id", "&", "major", "and", "minor", "version", "as", "a", "tuple", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/inject_resource.py#L47-L65
openstax/cnx-archive
cnxarchive/scripts/inject_resource.py
insert_file
def insert_file(file, media_type): """Upsert the ``file`` and ``media_type`` into the files table. Returns the ``fileid`` and ``sha1`` of the upserted file. """ resource_hash = get_file_sha1(file) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute("SELECT fileid FROM files WHERE sha1 = %s", (resource_hash,)) try: fileid = cursor.fetchone()[0] except (IndexError, TypeError): cursor.execute("INSERT INTO files (file, media_type) " "VALUES (%s, %s)" "RETURNING fileid", (psycopg2.Binary(file.read()), media_type,)) fileid = cursor.fetchone()[0] return fileid, resource_hash
python
def insert_file(file, media_type): """Upsert the ``file`` and ``media_type`` into the files table. Returns the ``fileid`` and ``sha1`` of the upserted file. """ resource_hash = get_file_sha1(file) with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute("SELECT fileid FROM files WHERE sha1 = %s", (resource_hash,)) try: fileid = cursor.fetchone()[0] except (IndexError, TypeError): cursor.execute("INSERT INTO files (file, media_type) " "VALUES (%s, %s)" "RETURNING fileid", (psycopg2.Binary(file.read()), media_type,)) fileid = cursor.fetchone()[0] return fileid, resource_hash
[ "def", "insert_file", "(", "file", ",", "media_type", ")", ":", "resource_hash", "=", "get_file_sha1", "(", "file", ")", "with", "db_connect", "(", ")", "as", "db_conn", ":", "with", "db_conn", ".", "cursor", "(", ")", "as", "cursor", ":", "cursor", ".",...
Upsert the ``file`` and ``media_type`` into the files table. Returns the ``fileid`` and ``sha1`` of the upserted file.
[ "Upsert", "the", "file", "and", "media_type", "into", "the", "files", "table", ".", "Returns", "the", "fileid", "and", "sha1", "of", "the", "upserted", "file", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/inject_resource.py#L68-L86
openstax/cnx-archive
cnxarchive/scripts/inject_resource.py
upsert_module_file
def upsert_module_file(module_ident, fileid, filename): """Upsert a file associated with ``fileid`` with ``filename`` as a module_files entry associated with content at ``module_ident``. """ with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute("SELECT true FROM module_files " "WHERE module_ident = %s " "AND filename = %s", (module_ident, filename,)) try: cursor.fetchone()[0] except (IndexError, TypeError): cursor.execute("INSERT INTO module_files " "(module_ident, fileid, filename) " "VALUES (%s, %s, %s)", (module_ident, fileid, filename,)) else: cursor.execute("UPDATE module_files " "SET (fileid) = (%s) " "WHERE module_ident = %s AND filename = %s", (fileid, module_ident, filename,))
python
def upsert_module_file(module_ident, fileid, filename): """Upsert a file associated with ``fileid`` with ``filename`` as a module_files entry associated with content at ``module_ident``. """ with db_connect() as db_conn: with db_conn.cursor() as cursor: cursor.execute("SELECT true FROM module_files " "WHERE module_ident = %s " "AND filename = %s", (module_ident, filename,)) try: cursor.fetchone()[0] except (IndexError, TypeError): cursor.execute("INSERT INTO module_files " "(module_ident, fileid, filename) " "VALUES (%s, %s, %s)", (module_ident, fileid, filename,)) else: cursor.execute("UPDATE module_files " "SET (fileid) = (%s) " "WHERE module_ident = %s AND filename = %s", (fileid, module_ident, filename,))
[ "def", "upsert_module_file", "(", "module_ident", ",", "fileid", ",", "filename", ")", ":", "with", "db_connect", "(", ")", "as", "db_conn", ":", "with", "db_conn", ".", "cursor", "(", ")", "as", "cursor", ":", "cursor", ".", "execute", "(", "\"SELECT true...
Upsert a file associated with ``fileid`` with ``filename`` as a module_files entry associated with content at ``module_ident``.
[ "Upsert", "a", "file", "associated", "with", "fileid", "with", "filename", "as", "a", "module_files", "entry", "associated", "with", "content", "at", "module_ident", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/inject_resource.py#L89-L111
openstax/cnx-archive
cnxarchive/scripts/inject_resource.py
inject_resource
def inject_resource(ident_hash, file, filename, media_type): """Injects the contents of ``file`` (a file-like object) into the database as ``filename`` with ``media_type`` in association with the content at ``ident_hash``. """ resource_hash = get_file_sha1(file) with db_connect() as db_conn: with db_conn.cursor() as cursor: s_ident_hash = split_ident_hash(ident_hash) module_ident = lookup_module_ident(*s_ident_hash) fileid, resource_hash = insert_file(file, media_type) upsert_module_file(module_ident, fileid, filename) return resource_hash
python
def inject_resource(ident_hash, file, filename, media_type): """Injects the contents of ``file`` (a file-like object) into the database as ``filename`` with ``media_type`` in association with the content at ``ident_hash``. """ resource_hash = get_file_sha1(file) with db_connect() as db_conn: with db_conn.cursor() as cursor: s_ident_hash = split_ident_hash(ident_hash) module_ident = lookup_module_ident(*s_ident_hash) fileid, resource_hash = insert_file(file, media_type) upsert_module_file(module_ident, fileid, filename) return resource_hash
[ "def", "inject_resource", "(", "ident_hash", ",", "file", ",", "filename", ",", "media_type", ")", ":", "resource_hash", "=", "get_file_sha1", "(", "file", ")", "with", "db_connect", "(", ")", "as", "db_conn", ":", "with", "db_conn", ".", "cursor", "(", ")...
Injects the contents of ``file`` (a file-like object) into the database as ``filename`` with ``media_type`` in association with the content at ``ident_hash``.
[ "Injects", "the", "contents", "of", "file", "(", "a", "file", "-", "like", "object", ")", "into", "the", "database", "as", "filename", "with", "media_type", "in", "association", "with", "the", "content", "at", "ident_hash", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/inject_resource.py#L114-L127
ZELLMECHANIK-DRESDEN/dclab
dclab/features/contour.py
get_contour
def get_contour(mask): """Compute the image contour from a mask The contour is computed in a very inefficient way using scikit-image and a conversion of float coordinates to pixel coordinates. Parameters ---------- mask: binary ndarray of shape (M,N) or (K,M,N) The mask outlining the pixel positions of the event. If a 3d array is given, then `K` indexes the individual contours. Returns ------- cont: ndarray or list of K ndarrays of shape (J,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. """ if isinstance(mask, np.ndarray) and len(mask.shape) == 2: mask = [mask] ret_list = False else: ret_list = True contours = [] for mi in mask: c0 = find_contours(mi.transpose(), level=.9999, positive_orientation="low", fully_connected="high")[0] # round all coordinates to pixel values c1 = np.asarray(np.round(c0), int) # remove duplicates c2 = remove_duplicates(c1) contours.append(c2) if ret_list: return contours else: return contours[0]
python
def get_contour(mask): """Compute the image contour from a mask The contour is computed in a very inefficient way using scikit-image and a conversion of float coordinates to pixel coordinates. Parameters ---------- mask: binary ndarray of shape (M,N) or (K,M,N) The mask outlining the pixel positions of the event. If a 3d array is given, then `K` indexes the individual contours. Returns ------- cont: ndarray or list of K ndarrays of shape (J,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. """ if isinstance(mask, np.ndarray) and len(mask.shape) == 2: mask = [mask] ret_list = False else: ret_list = True contours = [] for mi in mask: c0 = find_contours(mi.transpose(), level=.9999, positive_orientation="low", fully_connected="high")[0] # round all coordinates to pixel values c1 = np.asarray(np.round(c0), int) # remove duplicates c2 = remove_duplicates(c1) contours.append(c2) if ret_list: return contours else: return contours[0]
[ "def", "get_contour", "(", "mask", ")", ":", "if", "isinstance", "(", "mask", ",", "np", ".", "ndarray", ")", "and", "len", "(", "mask", ".", "shape", ")", "==", "2", ":", "mask", "=", "[", "mask", "]", "ret_list", "=", "False", "else", ":", "ret...
Compute the image contour from a mask The contour is computed in a very inefficient way using scikit-image and a conversion of float coordinates to pixel coordinates. Parameters ---------- mask: binary ndarray of shape (M,N) or (K,M,N) The mask outlining the pixel positions of the event. If a 3d array is given, then `K` indexes the individual contours. Returns ------- cont: ndarray or list of K ndarrays of shape (J,2) A 2D array that holds the contour of an event (in pixels) e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour.
[ "Compute", "the", "image", "contour", "from", "a", "mask" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/contour.py#L13-L54
simse/pymitv
pymitv/discover.py
Discover.scan
def scan(self, stop_on_first=True, base_ip=0): """Scans the local network for TVs.""" tvs = [] # Check if base_ip has been passed if base_ip == 0: # Find IP address of computer pymitv is running on sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(("8.8.8.8", 80)) ip = sock.getsockname()[0] sock.close() # Get IP and compose a base like 192.168.1.xxx ip_parts = ip.split('.') base_ip = ip_parts[0] + '.' + ip_parts[1] + '.' + ip_parts[2] # Loop through every IP and check if TV is alive for ip_suffix in range(2, 256): ip_check = '{}.{}'.format(base_ip, ip_suffix) if self.check_ip(ip_check): tvs.append(ip_check) if stop_on_first: break return tvs
python
def scan(self, stop_on_first=True, base_ip=0): """Scans the local network for TVs.""" tvs = [] # Check if base_ip has been passed if base_ip == 0: # Find IP address of computer pymitv is running on sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(("8.8.8.8", 80)) ip = sock.getsockname()[0] sock.close() # Get IP and compose a base like 192.168.1.xxx ip_parts = ip.split('.') base_ip = ip_parts[0] + '.' + ip_parts[1] + '.' + ip_parts[2] # Loop through every IP and check if TV is alive for ip_suffix in range(2, 256): ip_check = '{}.{}'.format(base_ip, ip_suffix) if self.check_ip(ip_check): tvs.append(ip_check) if stop_on_first: break return tvs
[ "def", "scan", "(", "self", ",", "stop_on_first", "=", "True", ",", "base_ip", "=", "0", ")", ":", "tvs", "=", "[", "]", "# Check if base_ip has been passed\r", "if", "base_ip", "==", "0", ":", "# Find IP address of computer pymitv is running on\r", "sock", "=", ...
Scans the local network for TVs.
[ "Scans", "the", "local", "network", "for", "TVs", "." ]
train
https://github.com/simse/pymitv/blob/03213f591d70fbf90ba2b6af372e474c9bfb99f6/pymitv/discover.py#L13-L39
simse/pymitv
pymitv/discover.py
Discover.check_ip
def check_ip(ip, log=False): """Attempts a connection to the TV and checks if there really is a TV.""" if log: print('Checking ip: {}...'.format(ip)) request_timeout = 0.1 try: tv_url = 'http://{}:6095/request?action=isalive'.format(ip) request = requests.get(tv_url, timeout=request_timeout) except requests.exceptions.ConnectTimeout: return False return request.status_code == 200
python
def check_ip(ip, log=False): """Attempts a connection to the TV and checks if there really is a TV.""" if log: print('Checking ip: {}...'.format(ip)) request_timeout = 0.1 try: tv_url = 'http://{}:6095/request?action=isalive'.format(ip) request = requests.get(tv_url, timeout=request_timeout) except requests.exceptions.ConnectTimeout: return False return request.status_code == 200
[ "def", "check_ip", "(", "ip", ",", "log", "=", "False", ")", ":", "if", "log", ":", "print", "(", "'Checking ip: {}...'", ".", "format", "(", "ip", ")", ")", "request_timeout", "=", "0.1", "try", ":", "tv_url", "=", "'http://{}:6095/request?action=isalive'",...
Attempts a connection to the TV and checks if there really is a TV.
[ "Attempts", "a", "connection", "to", "the", "TV", "and", "checks", "if", "there", "really", "is", "a", "TV", "." ]
train
https://github.com/simse/pymitv/blob/03213f591d70fbf90ba2b6af372e474c9bfb99f6/pymitv/discover.py#L42-L55
xenon-middleware/pyxenon
xenon/oop.py
get_field_type
def get_field_type(f): """Obtain the type name of a GRPC Message field.""" types = (t[5:] for t in dir(f) if t[:4] == 'TYPE' and getattr(f, t) == f.type) return next(types)
python
def get_field_type(f): """Obtain the type name of a GRPC Message field.""" types = (t[5:] for t in dir(f) if t[:4] == 'TYPE' and getattr(f, t) == f.type) return next(types)
[ "def", "get_field_type", "(", "f", ")", ":", "types", "=", "(", "t", "[", "5", ":", "]", "for", "t", "in", "dir", "(", "f", ")", "if", "t", "[", ":", "4", "]", "==", "'TYPE'", "and", "getattr", "(", "f", ",", "t", ")", "==", "f", ".", "ty...
Obtain the type name of a GRPC Message field.
[ "Obtain", "the", "type", "name", "of", "a", "GRPC", "Message", "field", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L48-L52
xenon-middleware/pyxenon
xenon/oop.py
get_field_description
def get_field_description(f): """Get the type description of a GRPC Message field.""" type_name = get_field_type(f) if type_name == 'MESSAGE' and \ {sf.name for sf in f.message_type.fields} == {'key', 'value'}: return 'map<string, string>' elif type_name == 'MESSAGE': return f.message_type.full_name elif type_name == 'ENUM': return f.enum_type.full_name else: return type_name.lower()
python
def get_field_description(f): """Get the type description of a GRPC Message field.""" type_name = get_field_type(f) if type_name == 'MESSAGE' and \ {sf.name for sf in f.message_type.fields} == {'key', 'value'}: return 'map<string, string>' elif type_name == 'MESSAGE': return f.message_type.full_name elif type_name == 'ENUM': return f.enum_type.full_name else: return type_name.lower()
[ "def", "get_field_description", "(", "f", ")", ":", "type_name", "=", "get_field_type", "(", "f", ")", "if", "type_name", "==", "'MESSAGE'", "and", "{", "sf", ".", "name", "for", "sf", "in", "f", ".", "message_type", ".", "fields", "}", "==", "{", "'ke...
Get the type description of a GRPC Message field.
[ "Get", "the", "type", "description", "of", "a", "GRPC", "Message", "field", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L55-L66
xenon-middleware/pyxenon
xenon/oop.py
make_static_request
def make_static_request(method, *args, **kwargs): """Creates a request from a static method function call.""" if args and not use_signature: raise NotImplementedError("Only keyword arguments allowed in Python2") if use_signature: new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()} new_args = tuple(unwrap(value) for value in args) bound_args = method.signature.bind( None, *new_args, **new_kwargs).arguments # if we encounter any Enum arguments, replace them with their value for k in bound_args: if isinstance(bound_args[k], Enum): bound_args[k] = bound_args[k].value new_kwargs = {kw: v for kw, v in bound_args.items() if kw != 'cls'} else: new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()} return method.request_type(**new_kwargs)
python
def make_static_request(method, *args, **kwargs): """Creates a request from a static method function call.""" if args and not use_signature: raise NotImplementedError("Only keyword arguments allowed in Python2") if use_signature: new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()} new_args = tuple(unwrap(value) for value in args) bound_args = method.signature.bind( None, *new_args, **new_kwargs).arguments # if we encounter any Enum arguments, replace them with their value for k in bound_args: if isinstance(bound_args[k], Enum): bound_args[k] = bound_args[k].value new_kwargs = {kw: v for kw, v in bound_args.items() if kw != 'cls'} else: new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()} return method.request_type(**new_kwargs)
[ "def", "make_static_request", "(", "method", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "args", "and", "not", "use_signature", ":", "raise", "NotImplementedError", "(", "\"Only keyword arguments allowed in Python2\"", ")", "if", "use_signature", ":...
Creates a request from a static method function call.
[ "Creates", "a", "request", "from", "a", "static", "method", "function", "call", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L188-L209
xenon-middleware/pyxenon
xenon/oop.py
make_request
def make_request(self, method, *args, **kwargs): """Creates a request from a method function call.""" if args and not use_signature: raise NotImplementedError("Only keyword arguments allowed in Python2") new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()} if use_signature: new_args = tuple(unwrap(value) for value in args) bound_args = method.signature.bind( unwrap(self), *new_args, **new_kwargs).arguments # if we encounter any Enum arguments, replace them with their value def translate_enum(arg): return arg.value if isinstance(arg, Enum) else arg for k in bound_args: if isinstance(bound_args[k], str): continue if isinstance(bound_args[k], dict): continue try: x = [translate_enum(arg) for arg in bound_args[k]] bound_args[k] = x except TypeError: bound_args[k] = translate_enum(bound_args[k]) # replace `self` with the correct keyword new_kwargs = {(kw if kw != 'self' else method.field_name): v for kw, v in bound_args.items()} # args = tuple(x.value if isinstance(x, Enum) else x for x in args) else: new_kwargs[self.field_name] = unwrap(self) return method.request_type(**new_kwargs)
python
def make_request(self, method, *args, **kwargs): """Creates a request from a method function call.""" if args and not use_signature: raise NotImplementedError("Only keyword arguments allowed in Python2") new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()} if use_signature: new_args = tuple(unwrap(value) for value in args) bound_args = method.signature.bind( unwrap(self), *new_args, **new_kwargs).arguments # if we encounter any Enum arguments, replace them with their value def translate_enum(arg): return arg.value if isinstance(arg, Enum) else arg for k in bound_args: if isinstance(bound_args[k], str): continue if isinstance(bound_args[k], dict): continue try: x = [translate_enum(arg) for arg in bound_args[k]] bound_args[k] = x except TypeError: bound_args[k] = translate_enum(bound_args[k]) # replace `self` with the correct keyword new_kwargs = {(kw if kw != 'self' else method.field_name): v for kw, v in bound_args.items()} # args = tuple(x.value if isinstance(x, Enum) else x for x in args) else: new_kwargs[self.field_name] = unwrap(self) return method.request_type(**new_kwargs)
[ "def", "make_request", "(", "self", ",", "method", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "args", "and", "not", "use_signature", ":", "raise", "NotImplementedError", "(", "\"Only keyword arguments allowed in Python2\"", ")", "new_kwargs", "="...
Creates a request from a method function call.
[ "Creates", "a", "request", "from", "a", "method", "function", "call", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L212-L248
xenon-middleware/pyxenon
xenon/oop.py
method_wrapper
def method_wrapper(m): """Generates a method from a `GrpcMethod` definition.""" if m.is_simple: def simple_method(self): """TODO: no docstring!""" return apply_transform( self.__service__, m.output_transform, grpc_call(self.__service__, m, unwrap(self))) return simple_method elif m.input_transform is not None: def transform_method(self, *args, **kwargs): """TODO: no docstring!""" request = m.input_transform(self, *args, **kwargs) return apply_transform( self.__service__, m.output_transform, grpc_call(self.__service__, m, request)) return transform_method elif m.static: def static_method(cls, *args, **kwargs): """TODO: no docstring!""" request = make_static_request(m, *args, **kwargs) return apply_transform( cls.__stub__(__server__), m.output_transform, grpc_call(cls.__stub__(__server__), m, request)) return static_method else: def request_method(self, *args, **kwargs): """TODO: no docstring!""" request = make_request(self, m, *args, **kwargs) return apply_transform( self.__service__, m.output_transform, grpc_call(self.__service__, m, request)) return request_method
python
def method_wrapper(m): """Generates a method from a `GrpcMethod` definition.""" if m.is_simple: def simple_method(self): """TODO: no docstring!""" return apply_transform( self.__service__, m.output_transform, grpc_call(self.__service__, m, unwrap(self))) return simple_method elif m.input_transform is not None: def transform_method(self, *args, **kwargs): """TODO: no docstring!""" request = m.input_transform(self, *args, **kwargs) return apply_transform( self.__service__, m.output_transform, grpc_call(self.__service__, m, request)) return transform_method elif m.static: def static_method(cls, *args, **kwargs): """TODO: no docstring!""" request = make_static_request(m, *args, **kwargs) return apply_transform( cls.__stub__(__server__), m.output_transform, grpc_call(cls.__stub__(__server__), m, request)) return static_method else: def request_method(self, *args, **kwargs): """TODO: no docstring!""" request = make_request(self, m, *args, **kwargs) return apply_transform( self.__service__, m.output_transform, grpc_call(self.__service__, m, request)) return request_method
[ "def", "method_wrapper", "(", "m", ")", ":", "if", "m", ".", "is_simple", ":", "def", "simple_method", "(", "self", ")", ":", "\"\"\"TODO: no docstring!\"\"\"", "return", "apply_transform", "(", "self", ".", "__service__", ",", "m", ".", "output_transform", ",...
Generates a method from a `GrpcMethod` definition.
[ "Generates", "a", "method", "from", "a", "GrpcMethod", "definition", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L276-L316
xenon-middleware/pyxenon
xenon/oop.py
GrpcMethod.request_name
def request_name(self): """Generate the name of the request.""" if self.static and not self.uses_request: return 'Empty' if not self.uses_request: return None if isinstance(self.uses_request, str): return self.uses_request return to_camel_case(self.name) + "Request"
python
def request_name(self): """Generate the name of the request.""" if self.static and not self.uses_request: return 'Empty' if not self.uses_request: return None if isinstance(self.uses_request, str): return self.uses_request return to_camel_case(self.name) + "Request"
[ "def", "request_name", "(", "self", ")", ":", "if", "self", ".", "static", "and", "not", "self", ".", "uses_request", ":", "return", "'Empty'", "if", "not", "self", ".", "uses_request", ":", "return", "None", "if", "isinstance", "(", "self", ".", "uses_r...
Generate the name of the request.
[ "Generate", "the", "name", "of", "the", "request", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L104-L115
xenon-middleware/pyxenon
xenon/oop.py
GrpcMethod.request_type
def request_type(self): """Retrieve the type of the request, by fetching it from `xenon.proto.xenon_pb2`.""" if self.static and not self.uses_request: return getattr(xenon_pb2, 'Empty') if not self.uses_request: return None return getattr(xenon_pb2, self.request_name)
python
def request_type(self): """Retrieve the type of the request, by fetching it from `xenon.proto.xenon_pb2`.""" if self.static and not self.uses_request: return getattr(xenon_pb2, 'Empty') if not self.uses_request: return None return getattr(xenon_pb2, self.request_name)
[ "def", "request_type", "(", "self", ")", ":", "if", "self", ".", "static", "and", "not", "self", ".", "uses_request", ":", "return", "getattr", "(", "xenon_pb2", ",", "'Empty'", ")", "if", "not", "self", ".", "uses_request", ":", "return", "None", "retur...
Retrieve the type of the request, by fetching it from `xenon.proto.xenon_pb2`.
[ "Retrieve", "the", "type", "of", "the", "request", "by", "fetching", "it", "from", "xenon", ".", "proto", ".", "xenon_pb2", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L118-L127
xenon-middleware/pyxenon
xenon/oop.py
GrpcMethod.signature
def signature(self): """Create a signature for this method, only in Python > 3.4""" if not use_signature: raise NotImplementedError("Python 3 only.") if self.static: parameters = \ (Parameter(name='cls', kind=Parameter.POSITIONAL_ONLY),) else: parameters = \ (Parameter(name='self', kind=Parameter.POSITIONAL_ONLY),) if self.input_transform: return signature(self.input_transform) if self.uses_request: fields = get_fields(self.request_type) if not self.static: if self.field_name not in fields: raise NameError("field '{}' not found in {}".format( self.field_name, self.request_name)) fields.remove(self.field_name) parameters += tuple( Parameter(name=name, kind=Parameter.POSITIONAL_OR_KEYWORD, default=None) for name in fields) return Signature(parameters)
python
def signature(self): """Create a signature for this method, only in Python > 3.4""" if not use_signature: raise NotImplementedError("Python 3 only.") if self.static: parameters = \ (Parameter(name='cls', kind=Parameter.POSITIONAL_ONLY),) else: parameters = \ (Parameter(name='self', kind=Parameter.POSITIONAL_ONLY),) if self.input_transform: return signature(self.input_transform) if self.uses_request: fields = get_fields(self.request_type) if not self.static: if self.field_name not in fields: raise NameError("field '{}' not found in {}".format( self.field_name, self.request_name)) fields.remove(self.field_name) parameters += tuple( Parameter(name=name, kind=Parameter.POSITIONAL_OR_KEYWORD, default=None) for name in fields) return Signature(parameters)
[ "def", "signature", "(", "self", ")", ":", "if", "not", "use_signature", ":", "raise", "NotImplementedError", "(", "\"Python 3 only.\"", ")", "if", "self", ".", "static", ":", "parameters", "=", "(", "Parameter", "(", "name", "=", "'cls'", ",", "kind", "="...
Create a signature for this method, only in Python > 3.4
[ "Create", "a", "signature", "for", "this", "method", "only", "in", "Python", ">", "3", ".", "4" ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L131-L161
xenon-middleware/pyxenon
xenon/oop.py
GrpcMethod.docstring
def docstring(self, servicer): """Generate a doc-string.""" s = getattr(servicer, to_lower_camel_case(self.name)).__doc__ \ or "TODO: no docstring in .proto file" if self.uses_request: s += "\n" for field in get_fields(self.request_type): if field != self.field_name: type_info = get_field_description( self.request_type.DESCRIPTOR.fields_by_name[field]) s += " :param {}: {}\n".format(field, field) s += " :type {0}: {1}\n".format(field, type_info) return s
python
def docstring(self, servicer): """Generate a doc-string.""" s = getattr(servicer, to_lower_camel_case(self.name)).__doc__ \ or "TODO: no docstring in .proto file" if self.uses_request: s += "\n" for field in get_fields(self.request_type): if field != self.field_name: type_info = get_field_description( self.request_type.DESCRIPTOR.fields_by_name[field]) s += " :param {}: {}\n".format(field, field) s += " :type {0}: {1}\n".format(field, type_info) return s
[ "def", "docstring", "(", "self", ",", "servicer", ")", ":", "s", "=", "getattr", "(", "servicer", ",", "to_lower_camel_case", "(", "self", ".", "name", ")", ")", ".", "__doc__", "or", "\"TODO: no docstring in .proto file\"", "if", "self", ".", "uses_request", ...
Generate a doc-string.
[ "Generate", "a", "doc", "-", "string", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/oop.py#L164-L178
openstax/cnx-archive
cnxarchive/utils/text.py
slugify
def slugify(string): """Return a slug for the unicode_string. (lowercase, only letters and numbers, hyphens replace spaces) """ filtered_string = [] if isinstance(string, str): string = unicode(string, 'utf-8') for i in unicodedata.normalize('NFKC', string): cat = unicodedata.category(i)[0] # filter out all the non letter and non number characters from the # input (L is letter and N is number) if cat in 'LN' or i in '-_': filtered_string.append(i) elif cat in 'Z': filtered_string.append(' ') return re.sub('\s+', '-', ''.join(filtered_string)).lower()
python
def slugify(string): """Return a slug for the unicode_string. (lowercase, only letters and numbers, hyphens replace spaces) """ filtered_string = [] if isinstance(string, str): string = unicode(string, 'utf-8') for i in unicodedata.normalize('NFKC', string): cat = unicodedata.category(i)[0] # filter out all the non letter and non number characters from the # input (L is letter and N is number) if cat in 'LN' or i in '-_': filtered_string.append(i) elif cat in 'Z': filtered_string.append(' ') return re.sub('\s+', '-', ''.join(filtered_string)).lower()
[ "def", "slugify", "(", "string", ")", ":", "filtered_string", "=", "[", "]", "if", "isinstance", "(", "string", ",", "str", ")", ":", "string", "=", "unicode", "(", "string", ",", "'utf-8'", ")", "for", "i", "in", "unicodedata", ".", "normalize", "(", ...
Return a slug for the unicode_string. (lowercase, only letters and numbers, hyphens replace spaces)
[ "Return", "a", "slug", "for", "the", "unicode_string", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/utils/text.py#L16-L32
openstax/cnx-archive
cnxarchive/utils/text.py
utf8
def utf8(item): """Change all python2 str/bytes instances to unicode/python3 str.""" if isinstance(item, list): return [utf8(i) for i in item] if isinstance(item, tuple): return tuple([utf8(i) for i in item]) if isinstance(item, dict): return {utf8(k): utf8(v) for k, v in item.items()} try: return item.decode('utf-8') except: # bare except since this method is supposed to be safe anywhere return item
python
def utf8(item): """Change all python2 str/bytes instances to unicode/python3 str.""" if isinstance(item, list): return [utf8(i) for i in item] if isinstance(item, tuple): return tuple([utf8(i) for i in item]) if isinstance(item, dict): return {utf8(k): utf8(v) for k, v in item.items()} try: return item.decode('utf-8') except: # bare except since this method is supposed to be safe anywhere return item
[ "def", "utf8", "(", "item", ")", ":", "if", "isinstance", "(", "item", ",", "list", ")", ":", "return", "[", "utf8", "(", "i", ")", "for", "i", "in", "item", "]", "if", "isinstance", "(", "item", ",", "tuple", ")", ":", "return", "tuple", "(", ...
Change all python2 str/bytes instances to unicode/python3 str.
[ "Change", "all", "python2", "str", "/", "bytes", "instances", "to", "unicode", "/", "python3", "str", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/utils/text.py#L35-L46
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/filter.py
Filter.update
def update(self, force=[]): """Update the filters according to `self.rtdc_ds.config["filtering"]` Parameters ---------- force : list A list of feature names that must be refiltered with min/max values. """ # These lists may help us become very fast in the future newkeys = [] oldvals = [] newvals = [] cfg_cur = self.rtdc_ds.config["filtering"] cfg_old = self._old_config # Determine which data was updated for skey in list(cfg_cur.keys()): if skey not in cfg_old: cfg_old[skey] = None if cfg_cur[skey] != cfg_old[skey]: newkeys.append(skey) oldvals.append(cfg_old[skey]) newvals.append(cfg_cur[skey]) # 1. Filter all feature min/max values. # This line gets the feature names that must be filtered. col2filter = [] for k in newkeys: # k[:-4] because we want to crop " min" and " max" if k[:-4] in dfn.scalar_feature_names: col2filter.append(k[:-4]) for f in force: # Manually add forced features if f in dfn.scalar_feature_names: col2filter.append(f) else: # Make sure the feature name is valid. raise ValueError("Unknown feature name {}".format(f)) col2filter = np.unique(col2filter) for col in col2filter: if col in self.rtdc_ds: fstart = col + " min" fend = col + " max" # Get the current feature filter col_filt = self[col] # If min and max exist and if they are not identical: if (fstart in cfg_cur and fend in cfg_cur and cfg_cur[fstart] != cfg_cur[fend]): # TODO: speedup # Here one could check for smaller values in the # lists oldvals/newvals that we defined above. # Be sure to check against force in that case! ivalstart = cfg_cur[fstart] ivalend = cfg_cur[fend] if ivalstart > ivalend: msg = "inverting filter: {} > {}".format(fstart, fend) warnings.warn(msg) ivalstart, ivalend = ivalend, ivalstart data = self.rtdc_ds[col] col_filt[:] = (ivalstart <= data)*(data <= ivalend) else: col_filt[:] = True # 2. Filter with polygon filters # check if something has changed pf_id = "polygon filters" if ( (pf_id in cfg_cur and pf_id not in cfg_old) or (pf_id in cfg_cur and pf_id in cfg_old and cfg_cur[pf_id] != cfg_old[pf_id])): self.polygon[:] = True # perform polygon filtering for p in PolygonFilter.instances: if p.unique_id in cfg_cur[pf_id]: # update self.polygon # iterate through axes datax = self.rtdc_ds[p.axes[0]] datay = self.rtdc_ds[p.axes[1]] self.polygon *= p.filter(datax, datay) # 3. Invalid filters self.invalid[:] = True if cfg_cur["remove invalid events"]: for col in dfn.scalar_feature_names: if col in self.rtdc_ds: data = self.rtdc_ds[col] invalid = np.isinf(data) | np.isnan(data) self.invalid *= ~invalid # 4. Finally combine all filters # get a list of all filters self.all[:] = True if cfg_cur["enable filters"]: for col in self._filters: self.all[:] *= self._filters[col] self.all[:] *= self.invalid self.all[:] *= self.manual self.all[:] *= self.polygon # Filter with configuration keyword argument "limit events". # This additional step limits the total number of events in # self.all. if cfg_cur["limit events"] > 0: limit = cfg_cur["limit events"] sub = self.all[self.all] _f, idx = downsampling.downsample_rand(sub, samples=limit, ret_idx=True) sub[~idx] = False self.all[self.all] = sub # Actual filtering is then done during plotting self._old_config = self.rtdc_ds.config.copy()["filtering"]
python
def update(self, force=[]): """Update the filters according to `self.rtdc_ds.config["filtering"]` Parameters ---------- force : list A list of feature names that must be refiltered with min/max values. """ # These lists may help us become very fast in the future newkeys = [] oldvals = [] newvals = [] cfg_cur = self.rtdc_ds.config["filtering"] cfg_old = self._old_config # Determine which data was updated for skey in list(cfg_cur.keys()): if skey not in cfg_old: cfg_old[skey] = None if cfg_cur[skey] != cfg_old[skey]: newkeys.append(skey) oldvals.append(cfg_old[skey]) newvals.append(cfg_cur[skey]) # 1. Filter all feature min/max values. # This line gets the feature names that must be filtered. col2filter = [] for k in newkeys: # k[:-4] because we want to crop " min" and " max" if k[:-4] in dfn.scalar_feature_names: col2filter.append(k[:-4]) for f in force: # Manually add forced features if f in dfn.scalar_feature_names: col2filter.append(f) else: # Make sure the feature name is valid. raise ValueError("Unknown feature name {}".format(f)) col2filter = np.unique(col2filter) for col in col2filter: if col in self.rtdc_ds: fstart = col + " min" fend = col + " max" # Get the current feature filter col_filt = self[col] # If min and max exist and if they are not identical: if (fstart in cfg_cur and fend in cfg_cur and cfg_cur[fstart] != cfg_cur[fend]): # TODO: speedup # Here one could check for smaller values in the # lists oldvals/newvals that we defined above. # Be sure to check against force in that case! ivalstart = cfg_cur[fstart] ivalend = cfg_cur[fend] if ivalstart > ivalend: msg = "inverting filter: {} > {}".format(fstart, fend) warnings.warn(msg) ivalstart, ivalend = ivalend, ivalstart data = self.rtdc_ds[col] col_filt[:] = (ivalstart <= data)*(data <= ivalend) else: col_filt[:] = True # 2. Filter with polygon filters # check if something has changed pf_id = "polygon filters" if ( (pf_id in cfg_cur and pf_id not in cfg_old) or (pf_id in cfg_cur and pf_id in cfg_old and cfg_cur[pf_id] != cfg_old[pf_id])): self.polygon[:] = True # perform polygon filtering for p in PolygonFilter.instances: if p.unique_id in cfg_cur[pf_id]: # update self.polygon # iterate through axes datax = self.rtdc_ds[p.axes[0]] datay = self.rtdc_ds[p.axes[1]] self.polygon *= p.filter(datax, datay) # 3. Invalid filters self.invalid[:] = True if cfg_cur["remove invalid events"]: for col in dfn.scalar_feature_names: if col in self.rtdc_ds: data = self.rtdc_ds[col] invalid = np.isinf(data) | np.isnan(data) self.invalid *= ~invalid # 4. Finally combine all filters # get a list of all filters self.all[:] = True if cfg_cur["enable filters"]: for col in self._filters: self.all[:] *= self._filters[col] self.all[:] *= self.invalid self.all[:] *= self.manual self.all[:] *= self.polygon # Filter with configuration keyword argument "limit events". # This additional step limits the total number of events in # self.all. if cfg_cur["limit events"] > 0: limit = cfg_cur["limit events"] sub = self.all[self.all] _f, idx = downsampling.downsample_rand(sub, samples=limit, ret_idx=True) sub[~idx] = False self.all[self.all] = sub # Actual filtering is then done during plotting self._old_config = self.rtdc_ds.config.copy()["filtering"]
[ "def", "update", "(", "self", ",", "force", "=", "[", "]", ")", ":", "# These lists may help us become very fast in the future", "newkeys", "=", "[", "]", "oldvals", "=", "[", "]", "newvals", "=", "[", "]", "cfg_cur", "=", "self", ".", "rtdc_ds", ".", "con...
Update the filters according to `self.rtdc_ds.config["filtering"]` Parameters ---------- force : list A list of feature names that must be refiltered with min/max values.
[ "Update", "the", "filters", "according", "to", "self", ".", "rtdc_ds", ".", "config", "[", "filtering", "]" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/filter.py#L49-L170
ZELLMECHANIK-DRESDEN/dclab
dclab/external/skimage/_find_contours.py
find_contours
def find_contours(array, level, fully_connected='low', positive_orientation='low'): """Find iso-valued contours in a 2D array for a given level value. Uses the "marching squares" method to compute a the iso-valued contours of the input 2D array for a particular level value. Array values are linearly interpolated to provide better precision for the output contours. Parameters ---------- array : 2D ndarray of double Input data in which to find contours. level : float Value along which to find contours in the array. fully_connected : str, {'low', 'high'} Indicates whether array elements below the given level value are to be considered fully-connected (and hence elements above the value will only be face connected), or vice-versa. (See notes below for details.) positive_orientation : either 'low' or 'high' Indicates whether the output contours will produce positively-oriented polygons around islands of low- or high-valued elements. If 'low' then contours will wind counter- clockwise around elements below the iso-value. Alternately, this means that low-valued elements are always on the left of the contour. (See below for details.) Returns ------- contours : list of (n,2)-ndarrays Each contour is an ndarray of shape ``(n, 2)``, consisting of n ``(row, column)`` coordinates along the contour. Notes ----- The marching squares algorithm is a special case of the marching cubes algorithm [1]_. A simple explanation is available here:: http://www.essi.fr/~lingrand/MarchingCubes/algo.html There is a single ambiguous case in the marching squares algorithm: when a given ``2 x 2``-element square has two high-valued and two low-valued elements, each pair diagonally adjacent. (Where high- and low-valued is with respect to the contour value sought.) In this case, either the high-valued elements can be 'connected together' via a thin isthmus that separates the low-valued elements, or vice-versa. When elements are connected together across a diagonal, they are considered 'fully connected' (also known as 'face+vertex-connected' or '8-connected'). Only high-valued or low-valued elements can be fully-connected, the other set will be considered as 'face-connected' or '4-connected'. By default, low-valued elements are considered fully-connected; this can be altered with the 'fully_connected' parameter. Output contours are not guaranteed to be closed: contours which intersect the array edge will be left open. All other contours will be closed. (The closed-ness of a contours can be tested by checking whether the beginning point is the same as the end point.) Contours are oriented. By default, array values lower than the contour value are to the left of the contour and values greater than the contour value are to the right. This means that contours will wind counter-clockwise (i.e. in 'positive orientation') around islands of low-valued pixels. This behavior can be altered with the 'positive_orientation' parameter. The order of the contours in the output list is determined by the position of the smallest ``x,y`` (in lexicographical order) coordinate in the contour. This is a side-effect of how the input array is traversed, but can be relied upon. .. warning:: Array coordinates/values are assumed to refer to the *center* of the array element. Take a simple example input: ``[0, 1]``. The interpolated position of 0.5 in this array is midway between the 0-element (at ``x=0``) and the 1-element (at ``x=1``), and thus would fall at ``x=0.5``. This means that to find reasonable contours, it is best to find contours midway between the expected "light" and "dark" values. In particular, given a binarized array, *do not* choose to find contours at the low or high value of the array. This will often yield degenerate contours, especially around structures that are a single array element wide. Instead choose a middle value, as above. References ---------- .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High Resolution 3D Surface Construction Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170). Examples -------- >>> a = np.zeros((3, 3)) >>> a[0, 0] = 1 >>> a array([[ 1., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]) >>> find_contours(a, 0.5) [array([[ 0. , 0.5], [ 0.5, 0. ]])] """ array = np.asarray(array, dtype=np.double) if array.ndim != 2: raise ValueError('Only 2D arrays are supported.') level = float(level) if (fully_connected not in _param_options or positive_orientation not in _param_options): raise ValueError('Parameters "fully_connected" and' ' "positive_orientation" must be either "high" or' ' "low".') point_list = _find_contours_cy.iterate_and_store(array, level, fully_connected == 'high') contours = _assemble_contours(_take_2(point_list)) if positive_orientation == 'high': contours = [c[::-1] for c in contours] return contours
python
def find_contours(array, level, fully_connected='low', positive_orientation='low'): """Find iso-valued contours in a 2D array for a given level value. Uses the "marching squares" method to compute a the iso-valued contours of the input 2D array for a particular level value. Array values are linearly interpolated to provide better precision for the output contours. Parameters ---------- array : 2D ndarray of double Input data in which to find contours. level : float Value along which to find contours in the array. fully_connected : str, {'low', 'high'} Indicates whether array elements below the given level value are to be considered fully-connected (and hence elements above the value will only be face connected), or vice-versa. (See notes below for details.) positive_orientation : either 'low' or 'high' Indicates whether the output contours will produce positively-oriented polygons around islands of low- or high-valued elements. If 'low' then contours will wind counter- clockwise around elements below the iso-value. Alternately, this means that low-valued elements are always on the left of the contour. (See below for details.) Returns ------- contours : list of (n,2)-ndarrays Each contour is an ndarray of shape ``(n, 2)``, consisting of n ``(row, column)`` coordinates along the contour. Notes ----- The marching squares algorithm is a special case of the marching cubes algorithm [1]_. A simple explanation is available here:: http://www.essi.fr/~lingrand/MarchingCubes/algo.html There is a single ambiguous case in the marching squares algorithm: when a given ``2 x 2``-element square has two high-valued and two low-valued elements, each pair diagonally adjacent. (Where high- and low-valued is with respect to the contour value sought.) In this case, either the high-valued elements can be 'connected together' via a thin isthmus that separates the low-valued elements, or vice-versa. When elements are connected together across a diagonal, they are considered 'fully connected' (also known as 'face+vertex-connected' or '8-connected'). Only high-valued or low-valued elements can be fully-connected, the other set will be considered as 'face-connected' or '4-connected'. By default, low-valued elements are considered fully-connected; this can be altered with the 'fully_connected' parameter. Output contours are not guaranteed to be closed: contours which intersect the array edge will be left open. All other contours will be closed. (The closed-ness of a contours can be tested by checking whether the beginning point is the same as the end point.) Contours are oriented. By default, array values lower than the contour value are to the left of the contour and values greater than the contour value are to the right. This means that contours will wind counter-clockwise (i.e. in 'positive orientation') around islands of low-valued pixels. This behavior can be altered with the 'positive_orientation' parameter. The order of the contours in the output list is determined by the position of the smallest ``x,y`` (in lexicographical order) coordinate in the contour. This is a side-effect of how the input array is traversed, but can be relied upon. .. warning:: Array coordinates/values are assumed to refer to the *center* of the array element. Take a simple example input: ``[0, 1]``. The interpolated position of 0.5 in this array is midway between the 0-element (at ``x=0``) and the 1-element (at ``x=1``), and thus would fall at ``x=0.5``. This means that to find reasonable contours, it is best to find contours midway between the expected "light" and "dark" values. In particular, given a binarized array, *do not* choose to find contours at the low or high value of the array. This will often yield degenerate contours, especially around structures that are a single array element wide. Instead choose a middle value, as above. References ---------- .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High Resolution 3D Surface Construction Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170). Examples -------- >>> a = np.zeros((3, 3)) >>> a[0, 0] = 1 >>> a array([[ 1., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]) >>> find_contours(a, 0.5) [array([[ 0. , 0.5], [ 0.5, 0. ]])] """ array = np.asarray(array, dtype=np.double) if array.ndim != 2: raise ValueError('Only 2D arrays are supported.') level = float(level) if (fully_connected not in _param_options or positive_orientation not in _param_options): raise ValueError('Parameters "fully_connected" and' ' "positive_orientation" must be either "high" or' ' "low".') point_list = _find_contours_cy.iterate_and_store(array, level, fully_connected == 'high') contours = _assemble_contours(_take_2(point_list)) if positive_orientation == 'high': contours = [c[::-1] for c in contours] return contours
[ "def", "find_contours", "(", "array", ",", "level", ",", "fully_connected", "=", "'low'", ",", "positive_orientation", "=", "'low'", ")", ":", "array", "=", "np", ".", "asarray", "(", "array", ",", "dtype", "=", "np", ".", "double", ")", "if", "array", ...
Find iso-valued contours in a 2D array for a given level value. Uses the "marching squares" method to compute a the iso-valued contours of the input 2D array for a particular level value. Array values are linearly interpolated to provide better precision for the output contours. Parameters ---------- array : 2D ndarray of double Input data in which to find contours. level : float Value along which to find contours in the array. fully_connected : str, {'low', 'high'} Indicates whether array elements below the given level value are to be considered fully-connected (and hence elements above the value will only be face connected), or vice-versa. (See notes below for details.) positive_orientation : either 'low' or 'high' Indicates whether the output contours will produce positively-oriented polygons around islands of low- or high-valued elements. If 'low' then contours will wind counter- clockwise around elements below the iso-value. Alternately, this means that low-valued elements are always on the left of the contour. (See below for details.) Returns ------- contours : list of (n,2)-ndarrays Each contour is an ndarray of shape ``(n, 2)``, consisting of n ``(row, column)`` coordinates along the contour. Notes ----- The marching squares algorithm is a special case of the marching cubes algorithm [1]_. A simple explanation is available here:: http://www.essi.fr/~lingrand/MarchingCubes/algo.html There is a single ambiguous case in the marching squares algorithm: when a given ``2 x 2``-element square has two high-valued and two low-valued elements, each pair diagonally adjacent. (Where high- and low-valued is with respect to the contour value sought.) In this case, either the high-valued elements can be 'connected together' via a thin isthmus that separates the low-valued elements, or vice-versa. When elements are connected together across a diagonal, they are considered 'fully connected' (also known as 'face+vertex-connected' or '8-connected'). Only high-valued or low-valued elements can be fully-connected, the other set will be considered as 'face-connected' or '4-connected'. By default, low-valued elements are considered fully-connected; this can be altered with the 'fully_connected' parameter. Output contours are not guaranteed to be closed: contours which intersect the array edge will be left open. All other contours will be closed. (The closed-ness of a contours can be tested by checking whether the beginning point is the same as the end point.) Contours are oriented. By default, array values lower than the contour value are to the left of the contour and values greater than the contour value are to the right. This means that contours will wind counter-clockwise (i.e. in 'positive orientation') around islands of low-valued pixels. This behavior can be altered with the 'positive_orientation' parameter. The order of the contours in the output list is determined by the position of the smallest ``x,y`` (in lexicographical order) coordinate in the contour. This is a side-effect of how the input array is traversed, but can be relied upon. .. warning:: Array coordinates/values are assumed to refer to the *center* of the array element. Take a simple example input: ``[0, 1]``. The interpolated position of 0.5 in this array is midway between the 0-element (at ``x=0``) and the 1-element (at ``x=1``), and thus would fall at ``x=0.5``. This means that to find reasonable contours, it is best to find contours midway between the expected "light" and "dark" values. In particular, given a binarized array, *do not* choose to find contours at the low or high value of the array. This will often yield degenerate contours, especially around structures that are a single array element wide. Instead choose a middle value, as above. References ---------- .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High Resolution 3D Surface Construction Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170). Examples -------- >>> a = np.zeros((3, 3)) >>> a[0, 0] = 1 >>> a array([[ 1., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]) >>> find_contours(a, 0.5) [array([[ 0. , 0.5], [ 0.5, 0. ]])]
[ "Find", "iso", "-", "valued", "contours", "in", "a", "2D", "array", "for", "a", "given", "level", "value", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/external/skimage/_find_contours.py#L9-L124
openstax/cnx-archive
cnxarchive/database.py
get_module_ident_from_ident_hash
def get_module_ident_from_ident_hash(ident_hash, cursor): """Return the moduleid for a given ``ident_hash``.""" try: uuid, (mj_ver, mn_ver) = split_ident_hash( ident_hash, split_version=True) except IdentHashMissingVersion as e: uuid, mj_ver, mn_ver = e.id, None, None args = [uuid] stmt = "SELECT module_ident FROM {} WHERE uuid = %s" table_name = 'modules' if mj_ver is None: table_name = 'latest_modules' else: args.append(mj_ver) stmt += " AND major_version = %s" if mn_ver is not None: args.append(mn_ver) stmt += " AND minor_version = %s" stmt = stmt.format(table_name) cursor.execute(stmt, args) try: module_ident = cursor.fetchone()[0] except TypeError: # NoneType module_ident = None return module_ident
python
def get_module_ident_from_ident_hash(ident_hash, cursor): """Return the moduleid for a given ``ident_hash``.""" try: uuid, (mj_ver, mn_ver) = split_ident_hash( ident_hash, split_version=True) except IdentHashMissingVersion as e: uuid, mj_ver, mn_ver = e.id, None, None args = [uuid] stmt = "SELECT module_ident FROM {} WHERE uuid = %s" table_name = 'modules' if mj_ver is None: table_name = 'latest_modules' else: args.append(mj_ver) stmt += " AND major_version = %s" if mn_ver is not None: args.append(mn_ver) stmt += " AND minor_version = %s" stmt = stmt.format(table_name) cursor.execute(stmt, args) try: module_ident = cursor.fetchone()[0] except TypeError: # NoneType module_ident = None return module_ident
[ "def", "get_module_ident_from_ident_hash", "(", "ident_hash", ",", "cursor", ")", ":", "try", ":", "uuid", ",", "(", "mj_ver", ",", "mn_ver", ")", "=", "split_ident_hash", "(", "ident_hash", ",", "split_version", "=", "True", ")", "except", "IdentHashMissingVers...
Return the moduleid for a given ``ident_hash``.
[ "Return", "the", "moduleid", "for", "a", "given", "ident_hash", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L98-L123
openstax/cnx-archive
cnxarchive/database.py
get_tree
def get_tree(ident_hash, cursor, as_collated=False): """Return a JSON representation of the binder tree for ``ident_hash``.""" uuid, version = split_ident_hash(ident_hash) cursor.execute(SQL['get-tree-by-uuid-n-version'], (uuid, version, as_collated,)) try: tree = cursor.fetchone()[0] except TypeError: # NoneType raise ContentNotFound() if type(tree) in (type(''), type(u'')): return json.loads(tree) else: return tree
python
def get_tree(ident_hash, cursor, as_collated=False): """Return a JSON representation of the binder tree for ``ident_hash``.""" uuid, version = split_ident_hash(ident_hash) cursor.execute(SQL['get-tree-by-uuid-n-version'], (uuid, version, as_collated,)) try: tree = cursor.fetchone()[0] except TypeError: # NoneType raise ContentNotFound() if type(tree) in (type(''), type(u'')): return json.loads(tree) else: return tree
[ "def", "get_tree", "(", "ident_hash", ",", "cursor", ",", "as_collated", "=", "False", ")", ":", "uuid", ",", "version", "=", "split_ident_hash", "(", "ident_hash", ")", "cursor", ".", "execute", "(", "SQL", "[", "'get-tree-by-uuid-n-version'", "]", ",", "("...
Return a JSON representation of the binder tree for ``ident_hash``.
[ "Return", "a", "JSON", "representation", "of", "the", "binder", "tree", "for", "ident_hash", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L126-L138
openstax/cnx-archive
cnxarchive/database.py
get_collated_content
def get_collated_content(ident_hash, context_ident_hash, cursor): """Return collated content for ``ident_hash``.""" cursor.execute(SQL['get-collated-content'], (ident_hash, context_ident_hash,)) try: return cursor.fetchone()[0] except TypeError: # NoneType return
python
def get_collated_content(ident_hash, context_ident_hash, cursor): """Return collated content for ``ident_hash``.""" cursor.execute(SQL['get-collated-content'], (ident_hash, context_ident_hash,)) try: return cursor.fetchone()[0] except TypeError: # NoneType return
[ "def", "get_collated_content", "(", "ident_hash", ",", "context_ident_hash", ",", "cursor", ")", ":", "cursor", ".", "execute", "(", "SQL", "[", "'get-collated-content'", "]", ",", "(", "ident_hash", ",", "context_ident_hash", ",", ")", ")", "try", ":", "retur...
Return collated content for ``ident_hash``.
[ "Return", "collated", "content", "for", "ident_hash", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L141-L148
openstax/cnx-archive
cnxarchive/database.py
get_module_uuid
def get_module_uuid(plpy, moduleid): """Retrieve page uuid from legacy moduleid.""" plan = plpy.prepare("SELECT uuid FROM modules WHERE moduleid = $1;", ('text',)) result = plpy.execute(plan, (moduleid,), 1) if result: return result[0]['uuid']
python
def get_module_uuid(plpy, moduleid): """Retrieve page uuid from legacy moduleid.""" plan = plpy.prepare("SELECT uuid FROM modules WHERE moduleid = $1;", ('text',)) result = plpy.execute(plan, (moduleid,), 1) if result: return result[0]['uuid']
[ "def", "get_module_uuid", "(", "plpy", ",", "moduleid", ")", ":", "plan", "=", "plpy", ".", "prepare", "(", "\"SELECT uuid FROM modules WHERE moduleid = $1;\"", ",", "(", "'text'", ",", ")", ")", "result", "=", "plpy", ".", "execute", "(", "plan", ",", "(", ...
Retrieve page uuid from legacy moduleid.
[ "Retrieve", "page", "uuid", "from", "legacy", "moduleid", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L151-L157
openstax/cnx-archive
cnxarchive/database.py
get_current_module_ident
def get_current_module_ident(moduleid, plpy): """Retrieve module_ident for a given moduleid. Note that module_ident is used only for internal database relational associations, and is equivalent to a uuid@version for a given document. """ plan = plpy.prepare('''\ SELECT m.module_ident FROM modules m WHERE m.moduleid = $1 ORDER BY revised DESC''', ('text',)) results = plpy.execute(plan, (moduleid,), 1) if results: return results[0]['module_ident']
python
def get_current_module_ident(moduleid, plpy): """Retrieve module_ident for a given moduleid. Note that module_ident is used only for internal database relational associations, and is equivalent to a uuid@version for a given document. """ plan = plpy.prepare('''\ SELECT m.module_ident FROM modules m WHERE m.moduleid = $1 ORDER BY revised DESC''', ('text',)) results = plpy.execute(plan, (moduleid,), 1) if results: return results[0]['module_ident']
[ "def", "get_current_module_ident", "(", "moduleid", ",", "plpy", ")", ":", "plan", "=", "plpy", ".", "prepare", "(", "'''\\\n SELECT m.module_ident FROM modules m\n WHERE m.moduleid = $1 ORDER BY revised DESC'''", ",", "(", "'text'", ",", ")", ")", "results", ...
Retrieve module_ident for a given moduleid. Note that module_ident is used only for internal database relational associations, and is equivalent to a uuid@version for a given document.
[ "Retrieve", "module_ident", "for", "a", "given", "moduleid", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L160-L171
openstax/cnx-archive
cnxarchive/database.py
get_minor_version
def get_minor_version(module_ident, plpy): """Retrieve minor version only given module_ident.""" # Make sure to always return the max minor version that is already in the # database, in case the given module_ident is not the latest version plan = plpy.prepare('''\ WITH t AS ( SELECT uuid, major_version FROM modules WHERE module_ident = $1 ) SELECT MAX(m.minor_version) AS minor_version FROM modules m, t WHERE m.uuid = t.uuid AND m.major_version = t.major_version ''', ('integer',)) results = plpy.execute(plan, (module_ident,), 1) return results[0]['minor_version']
python
def get_minor_version(module_ident, plpy): """Retrieve minor version only given module_ident.""" # Make sure to always return the max minor version that is already in the # database, in case the given module_ident is not the latest version plan = plpy.prepare('''\ WITH t AS ( SELECT uuid, major_version FROM modules WHERE module_ident = $1 ) SELECT MAX(m.minor_version) AS minor_version FROM modules m, t WHERE m.uuid = t.uuid AND m.major_version = t.major_version ''', ('integer',)) results = plpy.execute(plan, (module_ident,), 1) return results[0]['minor_version']
[ "def", "get_minor_version", "(", "module_ident", ",", "plpy", ")", ":", "# Make sure to always return the max minor version that is already in the", "# database, in case the given module_ident is not the latest version", "plan", "=", "plpy", ".", "prepare", "(", "'''\\\n WITH ...
Retrieve minor version only given module_ident.
[ "Retrieve", "minor", "version", "only", "given", "module_ident", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L174-L189
openstax/cnx-archive
cnxarchive/database.py
get_collections
def get_collections(module_ident, plpy): """Get all the collections that the module is part of.""" # Make sure to only return one match per collection and only if it is the # latest collection (which may not be the same as what is in # latest_modules) plan = plpy.prepare(''' WITH RECURSIVE t(node, parent, path, document) AS ( SELECT tr.nodeid, tr.parent_id, ARRAY[tr.nodeid], tr.documentid FROM trees tr WHERE tr.documentid = $1 and tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, path || ARRAY[c.nodeid], c.documentid FROM trees c JOIN t ON (c.nodeid = t.parent) WHERE not c.nodeid = ANY(t.path) ), latest(module_ident) AS ( SELECT module_ident FROM ( SELECT m.module_ident, m.revised, MAX(m.revised) OVER (PARTITION BY m.uuid) as latest FROM modules m where m.portal_type = 'Collection' ) r WHERE r.revised = r.latest ) SELECT module_ident FROM t, latest WHERE latest.module_ident = t.document ''', ('integer',)) for i in plpy.execute(plan, (module_ident,)): yield i['module_ident']
python
def get_collections(module_ident, plpy): """Get all the collections that the module is part of.""" # Make sure to only return one match per collection and only if it is the # latest collection (which may not be the same as what is in # latest_modules) plan = plpy.prepare(''' WITH RECURSIVE t(node, parent, path, document) AS ( SELECT tr.nodeid, tr.parent_id, ARRAY[tr.nodeid], tr.documentid FROM trees tr WHERE tr.documentid = $1 and tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, path || ARRAY[c.nodeid], c.documentid FROM trees c JOIN t ON (c.nodeid = t.parent) WHERE not c.nodeid = ANY(t.path) ), latest(module_ident) AS ( SELECT module_ident FROM ( SELECT m.module_ident, m.revised, MAX(m.revised) OVER (PARTITION BY m.uuid) as latest FROM modules m where m.portal_type = 'Collection' ) r WHERE r.revised = r.latest ) SELECT module_ident FROM t, latest WHERE latest.module_ident = t.document ''', ('integer',)) for i in plpy.execute(plan, (module_ident,)): yield i['module_ident']
[ "def", "get_collections", "(", "module_ident", ",", "plpy", ")", ":", "# Make sure to only return one match per collection and only if it is the", "# latest collection (which may not be the same as what is in", "# latest_modules)", "plan", "=", "plpy", ".", "prepare", "(", "'''\nWI...
Get all the collections that the module is part of.
[ "Get", "all", "the", "collections", "that", "the", "module", "is", "part", "of", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L201-L228
openstax/cnx-archive
cnxarchive/database.py
get_subcols
def get_subcols(module_ident, plpy): """Get all the sub-collections that the module is part of.""" plan = plpy.prepare(''' WITH RECURSIVE t(node, parent, path, document) AS ( SELECT tr.nodeid, tr.parent_id, ARRAY[tr.nodeid], tr.documentid FROM trees tr WHERE tr.documentid = $1 and tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, path || ARRAY[c.nodeid], c.documentid FROM trees c JOIN t ON (c.nodeid = t.parent) WHERE not c.nodeid = ANY(t.path) ) SELECT DISTINCT m.module_ident FROM t JOIN modules m ON (t.document = m.module_ident) WHERE m.portal_type = 'SubCollection' ORDER BY m.module_ident ''', ('integer',)) for i in plpy.execute(plan, (module_ident,)): yield i['module_ident']
python
def get_subcols(module_ident, plpy): """Get all the sub-collections that the module is part of.""" plan = plpy.prepare(''' WITH RECURSIVE t(node, parent, path, document) AS ( SELECT tr.nodeid, tr.parent_id, ARRAY[tr.nodeid], tr.documentid FROM trees tr WHERE tr.documentid = $1 and tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, path || ARRAY[c.nodeid], c.documentid FROM trees c JOIN t ON (c.nodeid = t.parent) WHERE not c.nodeid = ANY(t.path) ) SELECT DISTINCT m.module_ident FROM t JOIN modules m ON (t.document = m.module_ident) WHERE m.portal_type = 'SubCollection' ORDER BY m.module_ident ''', ('integer',)) for i in plpy.execute(plan, (module_ident,)): yield i['module_ident']
[ "def", "get_subcols", "(", "module_ident", ",", "plpy", ")", ":", "plan", "=", "plpy", ".", "prepare", "(", "'''\n WITH RECURSIVE t(node, parent, path, document) AS (\n SELECT tr.nodeid, tr.parent_id, ARRAY[tr.nodeid], tr.documentid\n FROM trees tr\n WHERE tr.docu...
Get all the sub-collections that the module is part of.
[ "Get", "all", "the", "sub", "-", "collections", "that", "the", "module", "is", "part", "of", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L231-L249
openstax/cnx-archive
cnxarchive/database.py
rebuild_collection_tree
def rebuild_collection_tree(old_collection_ident, new_document_id_map, plpy): """Create a new tree for the collection based on the old tree. This uses new document ids, replacing old ones. """ get_tree = plpy.prepare(''' WITH RECURSIVE t(node, parent, document, title, childorder, latest, path) AS (SELECT tr.nodeid, tr.parent_id, tr.documentid, tr.title, tr.childorder, tr.latest, ARRAY[tr.nodeid] FROM trees tr WHERE tr.documentid = $1 AND tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, c.documentid, c.title, c.childorder, c.latest, path || ARRAY[c.nodeid] FROM trees c JOIN t ON (c.parent_id = t.node) WHERE not c.nodeid = ANY(t.path) ) SELECT * FROM t ''', ('integer',)) def get_old_tree(): return plpy.execute(get_tree, (old_collection_ident,)) tree = {} # { old_nodeid: {'data': ...}, ...} children = {} # { nodeid: [child_nodeid, ...], child_nodeid: [...]} for i in get_old_tree(): tree[i['node']] = {'data': i, 'new_nodeid': None} children.setdefault(i['parent'], []) children[i['parent']].append(i['node']) insert_tree = plpy.prepare(''' INSERT INTO trees (nodeid, parent_id, documentid, title, childorder, latest) VALUES (DEFAULT, $1, $2, $3, $4, $5) RETURNING nodeid ''', ('integer', 'integer', 'text', 'integer', 'boolean')) def execute(fields): results = plpy.execute(insert_tree, fields, 1) return results[0]['nodeid'] root_node = children[None][0] def build_tree(node, parent): data = tree[node]['data'] new_node = execute([parent, new_document_id_map.get(data['document'], data['document']), data['title'], data['childorder'], data['latest']]) for i in children.get(node, []): build_tree(i, new_node) build_tree(root_node, None)
python
def rebuild_collection_tree(old_collection_ident, new_document_id_map, plpy): """Create a new tree for the collection based on the old tree. This uses new document ids, replacing old ones. """ get_tree = plpy.prepare(''' WITH RECURSIVE t(node, parent, document, title, childorder, latest, path) AS (SELECT tr.nodeid, tr.parent_id, tr.documentid, tr.title, tr.childorder, tr.latest, ARRAY[tr.nodeid] FROM trees tr WHERE tr.documentid = $1 AND tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, c.documentid, c.title, c.childorder, c.latest, path || ARRAY[c.nodeid] FROM trees c JOIN t ON (c.parent_id = t.node) WHERE not c.nodeid = ANY(t.path) ) SELECT * FROM t ''', ('integer',)) def get_old_tree(): return plpy.execute(get_tree, (old_collection_ident,)) tree = {} # { old_nodeid: {'data': ...}, ...} children = {} # { nodeid: [child_nodeid, ...], child_nodeid: [...]} for i in get_old_tree(): tree[i['node']] = {'data': i, 'new_nodeid': None} children.setdefault(i['parent'], []) children[i['parent']].append(i['node']) insert_tree = plpy.prepare(''' INSERT INTO trees (nodeid, parent_id, documentid, title, childorder, latest) VALUES (DEFAULT, $1, $2, $3, $4, $5) RETURNING nodeid ''', ('integer', 'integer', 'text', 'integer', 'boolean')) def execute(fields): results = plpy.execute(insert_tree, fields, 1) return results[0]['nodeid'] root_node = children[None][0] def build_tree(node, parent): data = tree[node]['data'] new_node = execute([parent, new_document_id_map.get(data['document'], data['document']), data['title'], data['childorder'], data['latest']]) for i in children.get(node, []): build_tree(i, new_node) build_tree(root_node, None)
[ "def", "rebuild_collection_tree", "(", "old_collection_ident", ",", "new_document_id_map", ",", "plpy", ")", ":", "get_tree", "=", "plpy", ".", "prepare", "(", "'''\n WITH RECURSIVE t(node, parent, document, title, childorder, latest, path)\n AS (SELECT tr.nodeid, tr.parent...
Create a new tree for the collection based on the old tree. This uses new document ids, replacing old ones.
[ "Create", "a", "new", "tree", "for", "the", "collection", "based", "on", "the", "old", "tree", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L252-L302
openstax/cnx-archive
cnxarchive/database.py
republish_collection
def republish_collection(submitter, submitlog, next_minor_version, collection_ident, plpy, revised=None): """Insert a new row for collection_ident with a new version. Returns the module_ident of the row inserted. """ sql = ''' INSERT INTO modules (portal_type, moduleid, uuid, version, name, created, revised, abstractid, licenseid, doctype, submitter, submitlog, stateid, parent, language, authors, maintainers, licensors, parentauthors, google_analytics, buylink, print_style, major_version, minor_version) SELECT m.portal_type, m.moduleid, m.uuid, m.version, m.name, m.created, {}, m.abstractid, m.licenseid, m.doctype, $3, $4, m.stateid, m.parent, m.language, m.authors, m.maintainers, m.licensors, m.parentauthors, m.google_analytics, m.buylink, m.print_style, m.major_version, $1 FROM modules m WHERE m.module_ident = $2 RETURNING module_ident ''' if revised is None: sql = sql.format('CURRENT_TIMESTAMP') types = ('integer', 'integer', 'text', 'text') params = (next_minor_version, collection_ident, submitter, submitlog) else: sql = sql.format('$5') types = ('integer', 'integer', 'text', 'text', 'timestamp') params = (next_minor_version, collection_ident, submitter, submitlog, revised) plan = plpy.prepare(sql, types) new_ident = plpy.execute(plan, params, 1)[0]['module_ident'] plan = plpy.prepare("""\ INSERT INTO modulekeywords (module_ident, keywordid) SELECT $1, keywordid FROM modulekeywords WHERE module_ident = $2""", ('integer', 'integer')) plpy.execute(plan, (new_ident, collection_ident,)) plan = plpy.prepare("""\ INSERT INTO moduletags (module_ident, tagid) SELECT $1, tagid FROM moduletags WHERE module_ident = $2""", ('integer', 'integer')) plpy.execute(plan, (new_ident, collection_ident,)) plan = plpy.prepare("""\ INSERT INTO module_files (module_ident, fileid, filename) SELECT $1, fileid, filename FROM module_files WHERE module_ident = $2 and filename != 'collection.xml'""", ('integer', 'integer')) plpy.execute(plan, (new_ident, collection_ident,)) return new_ident
python
def republish_collection(submitter, submitlog, next_minor_version, collection_ident, plpy, revised=None): """Insert a new row for collection_ident with a new version. Returns the module_ident of the row inserted. """ sql = ''' INSERT INTO modules (portal_type, moduleid, uuid, version, name, created, revised, abstractid, licenseid, doctype, submitter, submitlog, stateid, parent, language, authors, maintainers, licensors, parentauthors, google_analytics, buylink, print_style, major_version, minor_version) SELECT m.portal_type, m.moduleid, m.uuid, m.version, m.name, m.created, {}, m.abstractid, m.licenseid, m.doctype, $3, $4, m.stateid, m.parent, m.language, m.authors, m.maintainers, m.licensors, m.parentauthors, m.google_analytics, m.buylink, m.print_style, m.major_version, $1 FROM modules m WHERE m.module_ident = $2 RETURNING module_ident ''' if revised is None: sql = sql.format('CURRENT_TIMESTAMP') types = ('integer', 'integer', 'text', 'text') params = (next_minor_version, collection_ident, submitter, submitlog) else: sql = sql.format('$5') types = ('integer', 'integer', 'text', 'text', 'timestamp') params = (next_minor_version, collection_ident, submitter, submitlog, revised) plan = plpy.prepare(sql, types) new_ident = plpy.execute(plan, params, 1)[0]['module_ident'] plan = plpy.prepare("""\ INSERT INTO modulekeywords (module_ident, keywordid) SELECT $1, keywordid FROM modulekeywords WHERE module_ident = $2""", ('integer', 'integer')) plpy.execute(plan, (new_ident, collection_ident,)) plan = plpy.prepare("""\ INSERT INTO moduletags (module_ident, tagid) SELECT $1, tagid FROM moduletags WHERE module_ident = $2""", ('integer', 'integer')) plpy.execute(plan, (new_ident, collection_ident,)) plan = plpy.prepare("""\ INSERT INTO module_files (module_ident, fileid, filename) SELECT $1, fileid, filename FROM module_files WHERE module_ident = $2 and filename != 'collection.xml'""", ('integer', 'integer')) plpy.execute(plan, (new_ident, collection_ident,)) return new_ident
[ "def", "republish_collection", "(", "submitter", ",", "submitlog", ",", "next_minor_version", ",", "collection_ident", ",", "plpy", ",", "revised", "=", "None", ")", ":", "sql", "=", "'''\n INSERT INTO modules (portal_type, moduleid, uuid, version, name, created,\n ...
Insert a new row for collection_ident with a new version. Returns the module_ident of the row inserted.
[ "Insert", "a", "new", "row", "for", "collection_ident", "with", "a", "new", "version", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L305-L359
openstax/cnx-archive
cnxarchive/database.py
set_version
def set_version(portal_type, legacy_version, td): """Set the major_version and minor_version if they are not set.""" modified = 'OK' legacy_major, legacy_minor = legacy_version.split('.') if portal_type == 'Collection': # For collections, both major and minor needs to be set modified = 'MODIFY' td['new']['major_version'] = int(legacy_minor) if td['new']['minor_version'] is None: td['new']['minor_version'] = 1 elif portal_type == 'Module': # For modules, major should be set and minor should be None # N.B. a very few older modules had major=2 and minor zero-based. # The odd math below adds one to the minor for those modified = 'MODIFY' td['new']['major_version'] = int(legacy_minor)+(int(legacy_major)-1) td['new']['minor_version'] = None return modified
python
def set_version(portal_type, legacy_version, td): """Set the major_version and minor_version if they are not set.""" modified = 'OK' legacy_major, legacy_minor = legacy_version.split('.') if portal_type == 'Collection': # For collections, both major and minor needs to be set modified = 'MODIFY' td['new']['major_version'] = int(legacy_minor) if td['new']['minor_version'] is None: td['new']['minor_version'] = 1 elif portal_type == 'Module': # For modules, major should be set and minor should be None # N.B. a very few older modules had major=2 and minor zero-based. # The odd math below adds one to the minor for those modified = 'MODIFY' td['new']['major_version'] = int(legacy_minor)+(int(legacy_major)-1) td['new']['minor_version'] = None return modified
[ "def", "set_version", "(", "portal_type", ",", "legacy_version", ",", "td", ")", ":", "modified", "=", "'OK'", "legacy_major", ",", "legacy_minor", "=", "legacy_version", ".", "split", "(", "'.'", ")", "if", "portal_type", "==", "'Collection'", ":", "# For col...
Set the major_version and minor_version if they are not set.
[ "Set", "the", "major_version", "and", "minor_version", "if", "they", "are", "not", "set", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L362-L382
openstax/cnx-archive
cnxarchive/database.py
republish_module
def republish_module(td, plpy): """When a module is republished, create new minor versions of collections. All collections (including subcollections) that this module is contained in part of will need to be updated (a minor update). e.g. there is a collection c1 v2.1, which contains a chapter sc1 v2.1, which contains a module m1 v3. When m1 is updated, we will have a new row in the modules table with m1 v4. This trigger will create increment the minor versions of c1 and sc1, so we'll have c1 v2.2, and sc1 v2.2. However, another chapter sc2 will stay at v2.1. We need to create a collection tree for c1 v2.2 which is exactly the same as c1 v2.1, but with m1 v4 instead of m1 v3, and sc1 v2.2 and c1 v2.2 instead of sc1 2.1 and c1 v2.1 """ portal_type = td['new']['portal_type'] modified = 'OK' moduleid = td['new']['moduleid'] legacy_version = td['new']['version'] submitter = td['new']['submitter'] submitlog = td['new']['submitlog'] modified = set_version(portal_type, legacy_version, td) current_module_ident = get_current_module_ident(moduleid, plpy) if current_module_ident: # need to overide autogen uuid to keep it constant per moduleid uuid = get_module_uuid(plpy, moduleid) td['new']['uuid'] = uuid modified = 'MODIFY' else: # nothing to do if the module/collection is new return modified if portal_type != 'Module': # nothing else to do if something else is being published return modified # Module is republished replace_map = {current_module_ident: td['new']['module_ident']} # find the nested subcollections the module is in, and # republish them, as well, adding to map, for all collections # Note that map is for all subcollections, regardless of what # collection they are contained in. for sub_id in get_subcols(current_module_ident, plpy): minor = next_version(sub_id, plpy) new_subcol_ident = republish_collection(submitter, submitlog, minor, sub_id, plpy) replace_map[sub_id] = new_subcol_ident # Now do each collection that contains this module for collection_id in get_collections(current_module_ident, plpy): minor = next_version(collection_id, plpy) new_ident = republish_collection(submitter, submitlog, minor, collection_id, plpy) replace_map[collection_id] = new_ident rebuild_collection_tree(collection_id, replace_map, plpy) return modified
python
def republish_module(td, plpy): """When a module is republished, create new minor versions of collections. All collections (including subcollections) that this module is contained in part of will need to be updated (a minor update). e.g. there is a collection c1 v2.1, which contains a chapter sc1 v2.1, which contains a module m1 v3. When m1 is updated, we will have a new row in the modules table with m1 v4. This trigger will create increment the minor versions of c1 and sc1, so we'll have c1 v2.2, and sc1 v2.2. However, another chapter sc2 will stay at v2.1. We need to create a collection tree for c1 v2.2 which is exactly the same as c1 v2.1, but with m1 v4 instead of m1 v3, and sc1 v2.2 and c1 v2.2 instead of sc1 2.1 and c1 v2.1 """ portal_type = td['new']['portal_type'] modified = 'OK' moduleid = td['new']['moduleid'] legacy_version = td['new']['version'] submitter = td['new']['submitter'] submitlog = td['new']['submitlog'] modified = set_version(portal_type, legacy_version, td) current_module_ident = get_current_module_ident(moduleid, plpy) if current_module_ident: # need to overide autogen uuid to keep it constant per moduleid uuid = get_module_uuid(plpy, moduleid) td['new']['uuid'] = uuid modified = 'MODIFY' else: # nothing to do if the module/collection is new return modified if portal_type != 'Module': # nothing else to do if something else is being published return modified # Module is republished replace_map = {current_module_ident: td['new']['module_ident']} # find the nested subcollections the module is in, and # republish them, as well, adding to map, for all collections # Note that map is for all subcollections, regardless of what # collection they are contained in. for sub_id in get_subcols(current_module_ident, plpy): minor = next_version(sub_id, plpy) new_subcol_ident = republish_collection(submitter, submitlog, minor, sub_id, plpy) replace_map[sub_id] = new_subcol_ident # Now do each collection that contains this module for collection_id in get_collections(current_module_ident, plpy): minor = next_version(collection_id, plpy) new_ident = republish_collection(submitter, submitlog, minor, collection_id, plpy) replace_map[collection_id] = new_ident rebuild_collection_tree(collection_id, replace_map, plpy) return modified
[ "def", "republish_module", "(", "td", ",", "plpy", ")", ":", "portal_type", "=", "td", "[", "'new'", "]", "[", "'portal_type'", "]", "modified", "=", "'OK'", "moduleid", "=", "td", "[", "'new'", "]", "[", "'moduleid'", "]", "legacy_version", "=", "td", ...
When a module is republished, create new minor versions of collections. All collections (including subcollections) that this module is contained in part of will need to be updated (a minor update). e.g. there is a collection c1 v2.1, which contains a chapter sc1 v2.1, which contains a module m1 v3. When m1 is updated, we will have a new row in the modules table with m1 v4. This trigger will create increment the minor versions of c1 and sc1, so we'll have c1 v2.2, and sc1 v2.2. However, another chapter sc2 will stay at v2.1. We need to create a collection tree for c1 v2.2 which is exactly the same as c1 v2.1, but with m1 v4 instead of m1 v3, and sc1 v2.2 and c1 v2.2 instead of sc1 2.1 and c1 v2.1
[ "When", "a", "module", "is", "republished", "create", "new", "minor", "versions", "of", "collections", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L385-L446
openstax/cnx-archive
cnxarchive/database.py
republish_module_trigger
def republish_module_trigger(plpy, td): """Trigger called from postgres database when republishing a module. When a module is republished, the versions of the collections that it is part of will need to be updated (a minor update). e.g. there is a collection c1 v2.1, which contains module m1 v3 m1 is updated, we have a new row in the modules table with m1 v4 this trigger will create increment the minor version of c1, so we'll have c1 v2.2 we need to create a collection tree for c1 v2.2 which is exactly the same as c1 v2.1, but with m1 v4 instead of m1 v3, and c1 v2.2 instead of c1 v2.2 """ # Is this an insert from legacy? Legacy always supplies the version. is_legacy_publication = td['new']['version'] is not None if not is_legacy_publication: # Bail out, because this trigger only applies to legacy publications. return "OK" plpy.log('Trigger fired on %s' % (td['new']['moduleid'],)) modified = republish_module(td, plpy) plpy.log('modified: {}'.format(modified)) plpy.log('insert values:\n{}\n'.format('\n'.join([ '{}: {}'.format(key, value) for key, value in td['new'].items()]))) return modified
python
def republish_module_trigger(plpy, td): """Trigger called from postgres database when republishing a module. When a module is republished, the versions of the collections that it is part of will need to be updated (a minor update). e.g. there is a collection c1 v2.1, which contains module m1 v3 m1 is updated, we have a new row in the modules table with m1 v4 this trigger will create increment the minor version of c1, so we'll have c1 v2.2 we need to create a collection tree for c1 v2.2 which is exactly the same as c1 v2.1, but with m1 v4 instead of m1 v3, and c1 v2.2 instead of c1 v2.2 """ # Is this an insert from legacy? Legacy always supplies the version. is_legacy_publication = td['new']['version'] is not None if not is_legacy_publication: # Bail out, because this trigger only applies to legacy publications. return "OK" plpy.log('Trigger fired on %s' % (td['new']['moduleid'],)) modified = republish_module(td, plpy) plpy.log('modified: {}'.format(modified)) plpy.log('insert values:\n{}\n'.format('\n'.join([ '{}: {}'.format(key, value) for key, value in td['new'].items()]))) return modified
[ "def", "republish_module_trigger", "(", "plpy", ",", "td", ")", ":", "# Is this an insert from legacy? Legacy always supplies the version.", "is_legacy_publication", "=", "td", "[", "'new'", "]", "[", "'version'", "]", "is", "not", "None", "if", "not", "is_legacy_public...
Trigger called from postgres database when republishing a module. When a module is republished, the versions of the collections that it is part of will need to be updated (a minor update). e.g. there is a collection c1 v2.1, which contains module m1 v3 m1 is updated, we have a new row in the modules table with m1 v4 this trigger will create increment the minor version of c1, so we'll have c1 v2.2 we need to create a collection tree for c1 v2.2 which is exactly the same as c1 v2.1, but with m1 v4 instead of m1 v3, and c1 v2.2 instead of c1 v2.2
[ "Trigger", "called", "from", "postgres", "database", "when", "republishing", "a", "module", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L449-L480
openstax/cnx-archive
cnxarchive/database.py
assign_moduleid_default_trigger
def assign_moduleid_default_trigger(plpy, td): """Trigger to fill in legacy ``moduleid`` when publishing. This correctly assigns ``moduleid`` value to cnx-publishing publications. This does NOT include matching the ``moduleid`` to previous revision by way of ``uuid``. This correctly updates the sequence values when a legacy publication specifies the ``moduleid`` value. This is because legacy does not know about nor use the sequence values when setting legacy ``moduleid``. """ modified_state = "OK" portal_type = td['new']['portal_type'] uuid = td['new']['uuid'] moduleid = td['new']['moduleid'] version = td['new']['version'] # Is this an insert from legacy? Legacy always supplies the version. is_legacy_publication = version is not None if moduleid is None: # If the moduleid is not supplied, it is a new publication. if portal_type in ("Collection", "SubCollection"): prefix, sequence_name = 'col', "collectionid_seq" else: prefix, sequence_name = 'm', "moduleid_seq" plan = plpy.prepare("SELECT $1 || nextval($2)::text AS moduleid", ['text', 'text']) row = plpy.execute(plan, (prefix, sequence_name,), 1) moduleid = row[0]['moduleid'] modified_state = "MODIFY" td['new']['moduleid'] = moduleid elif is_legacy_publication and moduleid is not None: # Set the sequence value based on what legacy gave us. plan = plpy.prepare("""\ SELECT setval($1, max(substr(moduleid, $2)::int)) FROM ( SELECT moduleid from modules where portal_type in ($3,$4) UNION ALL SELECT $4) AS all_together""", ['text', 'int', 'text', 'text']) args = [] if portal_type == 'Collection': args.append('collectionid_seq') args.append(4) args.extend(('Collection', 'SubCollection')) elif portal_type == 'Module': args.append('moduleid_seq') args.append(2) args.extend(('Module', 'CompositeModule')) args.append(moduleid) if len(args) == 4: plpy.execute(plan, args) plpy.log("Fixed identifier and version for publication at '{}' " "with the following values: {} and {}" .format(uuid, moduleid, version)) return modified_state
python
def assign_moduleid_default_trigger(plpy, td): """Trigger to fill in legacy ``moduleid`` when publishing. This correctly assigns ``moduleid`` value to cnx-publishing publications. This does NOT include matching the ``moduleid`` to previous revision by way of ``uuid``. This correctly updates the sequence values when a legacy publication specifies the ``moduleid`` value. This is because legacy does not know about nor use the sequence values when setting legacy ``moduleid``. """ modified_state = "OK" portal_type = td['new']['portal_type'] uuid = td['new']['uuid'] moduleid = td['new']['moduleid'] version = td['new']['version'] # Is this an insert from legacy? Legacy always supplies the version. is_legacy_publication = version is not None if moduleid is None: # If the moduleid is not supplied, it is a new publication. if portal_type in ("Collection", "SubCollection"): prefix, sequence_name = 'col', "collectionid_seq" else: prefix, sequence_name = 'm', "moduleid_seq" plan = plpy.prepare("SELECT $1 || nextval($2)::text AS moduleid", ['text', 'text']) row = plpy.execute(plan, (prefix, sequence_name,), 1) moduleid = row[0]['moduleid'] modified_state = "MODIFY" td['new']['moduleid'] = moduleid elif is_legacy_publication and moduleid is not None: # Set the sequence value based on what legacy gave us. plan = plpy.prepare("""\ SELECT setval($1, max(substr(moduleid, $2)::int)) FROM ( SELECT moduleid from modules where portal_type in ($3,$4) UNION ALL SELECT $4) AS all_together""", ['text', 'int', 'text', 'text']) args = [] if portal_type == 'Collection': args.append('collectionid_seq') args.append(4) args.extend(('Collection', 'SubCollection')) elif portal_type == 'Module': args.append('moduleid_seq') args.append(2) args.extend(('Module', 'CompositeModule')) args.append(moduleid) if len(args) == 4: plpy.execute(plan, args) plpy.log("Fixed identifier and version for publication at '{}' " "with the following values: {} and {}" .format(uuid, moduleid, version)) return modified_state
[ "def", "assign_moduleid_default_trigger", "(", "plpy", ",", "td", ")", ":", "modified_state", "=", "\"OK\"", "portal_type", "=", "td", "[", "'new'", "]", "[", "'portal_type'", "]", "uuid", "=", "td", "[", "'new'", "]", "[", "'uuid'", "]", "moduleid", "=", ...
Trigger to fill in legacy ``moduleid`` when publishing. This correctly assigns ``moduleid`` value to cnx-publishing publications. This does NOT include matching the ``moduleid`` to previous revision by way of ``uuid``. This correctly updates the sequence values when a legacy publication specifies the ``moduleid`` value. This is because legacy does not know about nor use the sequence values when setting legacy ``moduleid``.
[ "Trigger", "to", "fill", "in", "legacy", "moduleid", "when", "publishing", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L483-L541
openstax/cnx-archive
cnxarchive/database.py
assign_version_default_trigger
def assign_version_default_trigger(plpy, td): """Trigger to fill in legacy data fields. A compatibilty trigger to fill in legacy data fields that are not populated when inserting publications from cnx-publishing. If this is not a legacy publication the ``version`` will be set based on the ``major_version`` value. """ modified_state = "OK" portal_type = td['new']['portal_type'] version = td['new']['version'] minor_version = td['new']['minor_version'] # Set the minor version on collections, because by default it is # None/Null, which is the correct default for modules. if minor_version is None and portal_type in ('Collection', 'SubCollection'): modified_state = "MODIFY" td['new']['minor_version'] = 1 # Set the legacy version field based on the major version. if version is None: major_version = td['new']['major_version'] version = "1.{}".format(major_version) modified_state = "MODIFY" td['new']['version'] = version return modified_state
python
def assign_version_default_trigger(plpy, td): """Trigger to fill in legacy data fields. A compatibilty trigger to fill in legacy data fields that are not populated when inserting publications from cnx-publishing. If this is not a legacy publication the ``version`` will be set based on the ``major_version`` value. """ modified_state = "OK" portal_type = td['new']['portal_type'] version = td['new']['version'] minor_version = td['new']['minor_version'] # Set the minor version on collections, because by default it is # None/Null, which is the correct default for modules. if minor_version is None and portal_type in ('Collection', 'SubCollection'): modified_state = "MODIFY" td['new']['minor_version'] = 1 # Set the legacy version field based on the major version. if version is None: major_version = td['new']['major_version'] version = "1.{}".format(major_version) modified_state = "MODIFY" td['new']['version'] = version return modified_state
[ "def", "assign_version_default_trigger", "(", "plpy", ",", "td", ")", ":", "modified_state", "=", "\"OK\"", "portal_type", "=", "td", "[", "'new'", "]", "[", "'portal_type'", "]", "version", "=", "td", "[", "'new'", "]", "[", "'version'", "]", "minor_version...
Trigger to fill in legacy data fields. A compatibilty trigger to fill in legacy data fields that are not populated when inserting publications from cnx-publishing. If this is not a legacy publication the ``version`` will be set based on the ``major_version`` value.
[ "Trigger", "to", "fill", "in", "legacy", "data", "fields", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L544-L572
openstax/cnx-archive
cnxarchive/database.py
assign_document_controls_default_trigger
def assign_document_controls_default_trigger(plpy, td): """Trigger to fill in document_controls when legacy publishes. A compatibilty trigger to fill in ``uuid`` and ``licenseid`` columns of the ``document_controls`` table that are not populated when inserting publications from legacy. This uuid default is not on ``modules.uuid`` column itself, because the value needs to be loosely associated with the ``document_controls`` entry to prevent uuid collisions and bridge the pending publications gap. """ modified_state = "OK" uuid = td['new']['uuid'] # Only do the procedure if this is a legacy publication. if uuid is None: modified_state = "MODIFY" plan = plpy.prepare("""\ INSERT INTO document_controls (uuid, licenseid) VALUES (DEFAULT, $1) RETURNING uuid""", ('integer',)) uuid_ = plpy.execute(plan, (td['new']['licenseid'],))[0]['uuid'] td['new']['uuid'] = uuid_ return modified_state
python
def assign_document_controls_default_trigger(plpy, td): """Trigger to fill in document_controls when legacy publishes. A compatibilty trigger to fill in ``uuid`` and ``licenseid`` columns of the ``document_controls`` table that are not populated when inserting publications from legacy. This uuid default is not on ``modules.uuid`` column itself, because the value needs to be loosely associated with the ``document_controls`` entry to prevent uuid collisions and bridge the pending publications gap. """ modified_state = "OK" uuid = td['new']['uuid'] # Only do the procedure if this is a legacy publication. if uuid is None: modified_state = "MODIFY" plan = plpy.prepare("""\ INSERT INTO document_controls (uuid, licenseid) VALUES (DEFAULT, $1) RETURNING uuid""", ('integer',)) uuid_ = plpy.execute(plan, (td['new']['licenseid'],))[0]['uuid'] td['new']['uuid'] = uuid_ return modified_state
[ "def", "assign_document_controls_default_trigger", "(", "plpy", ",", "td", ")", ":", "modified_state", "=", "\"OK\"", "uuid", "=", "td", "[", "'new'", "]", "[", "'uuid'", "]", "# Only do the procedure if this is a legacy publication.", "if", "uuid", "is", "None", ":...
Trigger to fill in document_controls when legacy publishes. A compatibilty trigger to fill in ``uuid`` and ``licenseid`` columns of the ``document_controls`` table that are not populated when inserting publications from legacy. This uuid default is not on ``modules.uuid`` column itself, because the value needs to be loosely associated with the ``document_controls`` entry to prevent uuid collisions and bridge the pending publications gap.
[ "Trigger", "to", "fill", "in", "document_controls", "when", "legacy", "publishes", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L575-L599
openstax/cnx-archive
cnxarchive/database.py
upsert_document_acl_trigger
def upsert_document_acl_trigger(plpy, td): """Trigger for filling in acls when legacy publishes. A compatibility trigger to upsert authorization control entries (ACEs) for legacy publications. """ modified_state = "OK" uuid_ = td['new']['uuid'] authors = td['new']['authors'] and td['new']['authors'] or [] maintainers = td['new']['maintainers'] and td['new']['maintainers'] or [] is_legacy_publication = td['new']['version'] is not None if not is_legacy_publication: return modified_state # Upsert all authors and maintainers into the ACL # to give them publish permission. permissibles = [] permissibles.extend(authors) permissibles.extend(maintainers) permissibles = set([(uid, 'publish',) for uid in permissibles]) plan = plpy.prepare("""\ SELECT user_id, permission FROM document_acl WHERE uuid = $1""", ['uuid']) existing_permissibles = set([(r['user_id'], r['permission'],) for r in plpy.execute(plan, (uuid_,))]) new_permissibles = permissibles.difference(existing_permissibles) for uid, permission in new_permissibles: plan = plpy.prepare("""\ INSERT INTO document_acl (uuid, user_id, permission) VALUES ($1, $2, $3)""", ['uuid', 'text', 'permission_type']) plpy.execute(plan, (uuid_, uid, permission,))
python
def upsert_document_acl_trigger(plpy, td): """Trigger for filling in acls when legacy publishes. A compatibility trigger to upsert authorization control entries (ACEs) for legacy publications. """ modified_state = "OK" uuid_ = td['new']['uuid'] authors = td['new']['authors'] and td['new']['authors'] or [] maintainers = td['new']['maintainers'] and td['new']['maintainers'] or [] is_legacy_publication = td['new']['version'] is not None if not is_legacy_publication: return modified_state # Upsert all authors and maintainers into the ACL # to give them publish permission. permissibles = [] permissibles.extend(authors) permissibles.extend(maintainers) permissibles = set([(uid, 'publish',) for uid in permissibles]) plan = plpy.prepare("""\ SELECT user_id, permission FROM document_acl WHERE uuid = $1""", ['uuid']) existing_permissibles = set([(r['user_id'], r['permission'],) for r in plpy.execute(plan, (uuid_,))]) new_permissibles = permissibles.difference(existing_permissibles) for uid, permission in new_permissibles: plan = plpy.prepare("""\ INSERT INTO document_acl (uuid, user_id, permission) VALUES ($1, $2, $3)""", ['uuid', 'text', 'permission_type']) plpy.execute(plan, (uuid_, uid, permission,))
[ "def", "upsert_document_acl_trigger", "(", "plpy", ",", "td", ")", ":", "modified_state", "=", "\"OK\"", "uuid_", "=", "td", "[", "'new'", "]", "[", "'uuid'", "]", "authors", "=", "td", "[", "'new'", "]", "[", "'authors'", "]", "and", "td", "[", "'new'...
Trigger for filling in acls when legacy publishes. A compatibility trigger to upsert authorization control entries (ACEs) for legacy publications.
[ "Trigger", "for", "filling", "in", "acls", "when", "legacy", "publishes", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L602-L636
openstax/cnx-archive
cnxarchive/database.py
upsert_users_from_legacy_publication_trigger
def upsert_users_from_legacy_publication_trigger(plpy, td): """A compatibility trigger to upsert users from legacy persons table.""" modified_state = "OK" authors = td['new']['authors'] and td['new']['authors'] or [] maintainers = td['new']['maintainers'] and td['new']['maintainers'] or [] licensors = td['new']['licensors'] and td['new']['licensors'] or [] is_legacy_publication = td['new']['version'] is not None if not is_legacy_publication: return modified_state # Upsert all roles into the users table. users = [] users.extend(authors) users.extend(maintainers) users.extend(licensors) users = list(set(users)) plan = plpy.prepare("""\ SELECT username FROM users WHERE username = any($1)""", ['text[]']) existing_users = set([r['username'] for r in plpy.execute(plan, (users,))]) new_users = set(users).difference(existing_users) for username in new_users: plan = plpy.prepare("""\ INSERT INTO users (username, first_name, last_name, full_name, title) SELECT personid, firstname, surname, fullname, honorific FROM persons where personid = $1""", ['text']) plpy.execute(plan, (username,)) return modified_state
python
def upsert_users_from_legacy_publication_trigger(plpy, td): """A compatibility trigger to upsert users from legacy persons table.""" modified_state = "OK" authors = td['new']['authors'] and td['new']['authors'] or [] maintainers = td['new']['maintainers'] and td['new']['maintainers'] or [] licensors = td['new']['licensors'] and td['new']['licensors'] or [] is_legacy_publication = td['new']['version'] is not None if not is_legacy_publication: return modified_state # Upsert all roles into the users table. users = [] users.extend(authors) users.extend(maintainers) users.extend(licensors) users = list(set(users)) plan = plpy.prepare("""\ SELECT username FROM users WHERE username = any($1)""", ['text[]']) existing_users = set([r['username'] for r in plpy.execute(plan, (users,))]) new_users = set(users).difference(existing_users) for username in new_users: plan = plpy.prepare("""\ INSERT INTO users (username, first_name, last_name, full_name, title) SELECT personid, firstname, surname, fullname, honorific FROM persons where personid = $1""", ['text']) plpy.execute(plan, (username,)) return modified_state
[ "def", "upsert_users_from_legacy_publication_trigger", "(", "plpy", ",", "td", ")", ":", "modified_state", "=", "\"OK\"", "authors", "=", "td", "[", "'new'", "]", "[", "'authors'", "]", "and", "td", "[", "'new'", "]", "[", "'authors'", "]", "or", "[", "]",...
A compatibility trigger to upsert users from legacy persons table.
[ "A", "compatibility", "trigger", "to", "upsert", "users", "from", "legacy", "persons", "table", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L639-L670
openstax/cnx-archive
cnxarchive/database.py
insert_users_for_optional_roles_trigger
def insert_users_for_optional_roles_trigger(plpy, td): """Trigger to update users from optional roles entries. A compatibility trigger to insert users from moduleoptionalroles records. This is primarily for legacy compatibility, but it is not possible to tell whether the entry came from legacy or cnx-publishing. Therefore, we only insert into users. """ modified_state = "OK" users = td['new']['personids'] and td['new']['personids'] or [] plan = plpy.prepare("""\ SELECT username FROM users WHERE username = any($1)""", ['text[]']) existing_users = set([r['username'] for r in plpy.execute(plan, (users,))]) new_users = set(users).difference(existing_users) for username in new_users: plan = plpy.prepare("""\ INSERT INTO users (username, first_name, last_name, full_name, title) SELECT personid, firstname, surname, fullname, honorific FROM persons where personid = $1""", ['text']) plpy.execute(plan, (username,)) return modified_state
python
def insert_users_for_optional_roles_trigger(plpy, td): """Trigger to update users from optional roles entries. A compatibility trigger to insert users from moduleoptionalroles records. This is primarily for legacy compatibility, but it is not possible to tell whether the entry came from legacy or cnx-publishing. Therefore, we only insert into users. """ modified_state = "OK" users = td['new']['personids'] and td['new']['personids'] or [] plan = plpy.prepare("""\ SELECT username FROM users WHERE username = any($1)""", ['text[]']) existing_users = set([r['username'] for r in plpy.execute(plan, (users,))]) new_users = set(users).difference(existing_users) for username in new_users: plan = plpy.prepare("""\ INSERT INTO users (username, first_name, last_name, full_name, title) SELECT personid, firstname, surname, fullname, honorific FROM persons where personid = $1""", ['text']) plpy.execute(plan, (username,)) return modified_state
[ "def", "insert_users_for_optional_roles_trigger", "(", "plpy", ",", "td", ")", ":", "modified_state", "=", "\"OK\"", "users", "=", "td", "[", "'new'", "]", "[", "'personids'", "]", "and", "td", "[", "'new'", "]", "[", "'personids'", "]", "or", "[", "]", ...
Trigger to update users from optional roles entries. A compatibility trigger to insert users from moduleoptionalroles records. This is primarily for legacy compatibility, but it is not possible to tell whether the entry came from legacy or cnx-publishing. Therefore, we only insert into users.
[ "Trigger", "to", "update", "users", "from", "optional", "roles", "entries", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L673-L697
openstax/cnx-archive
cnxarchive/database.py
add_module_file
def add_module_file(plpy, td): """Database trigger for adding a module file. When a legacy ``index.cnxml`` is added, this trigger transforms it into html and stores it as ``index.cnxml.html``. When a cnx-publishing ``index.cnxml.html`` is added, this trigger checks if ``index.html.cnxml`` exists before transforming it into cnxml and stores it as ``index.html.cnxml``. Note, we do not use ``index.html`` over ``index.cnxml.html``, because legacy allows users to name files ``index.html``. """ module_ident = td['new']['module_ident'] filename = td['new']['filename'] msg = "produce {}->{} for module_ident = {}" def check_for(filenames, module_ident): """Find filenames associated with module_ident. Check for a file at ``filename`` associated with module at ``module_ident``. """ stmt = plpy.prepare("""\ SELECT TRUE AS exists FROM module_files WHERE filename = $1 AND module_ident = $2""", ['text', 'integer']) any_exist = False for filename in filenames: result = plpy.execute(stmt, [filename, module_ident]) try: exists = result[0]['exists'] except IndexError: exists = False any_exist = any_exist or exists return any_exist # Declare the content producer function variable, # because it is possible that it will not be assigned. producer_func = None if filename == 'index.cnxml': new_filenames = ('index.cnxml.html',) # Transform content to html. other_exists = check_for(new_filenames, module_ident) if not other_exists: msg = msg.format('cnxml', 'html', module_ident) producer_func = produce_html_for_module elif filename == 'index.cnxml.html': new_filenames = ('index.html.cnxml', 'index.cnxml',) # Transform content to cnxml. other_exists = check_for(new_filenames, module_ident) if not other_exists: msg = msg.format('html', 'cnxml', module_ident) producer_func = produce_cnxml_for_module else: # Not one of the special named files. return # skip plpy.info(msg) if producer_func is not None: producer_func(plpy, module_ident, source_filename=filename, destination_filenames=new_filenames) _transform_abstract(plpy, module_ident) return
python
def add_module_file(plpy, td): """Database trigger for adding a module file. When a legacy ``index.cnxml`` is added, this trigger transforms it into html and stores it as ``index.cnxml.html``. When a cnx-publishing ``index.cnxml.html`` is added, this trigger checks if ``index.html.cnxml`` exists before transforming it into cnxml and stores it as ``index.html.cnxml``. Note, we do not use ``index.html`` over ``index.cnxml.html``, because legacy allows users to name files ``index.html``. """ module_ident = td['new']['module_ident'] filename = td['new']['filename'] msg = "produce {}->{} for module_ident = {}" def check_for(filenames, module_ident): """Find filenames associated with module_ident. Check for a file at ``filename`` associated with module at ``module_ident``. """ stmt = plpy.prepare("""\ SELECT TRUE AS exists FROM module_files WHERE filename = $1 AND module_ident = $2""", ['text', 'integer']) any_exist = False for filename in filenames: result = plpy.execute(stmt, [filename, module_ident]) try: exists = result[0]['exists'] except IndexError: exists = False any_exist = any_exist or exists return any_exist # Declare the content producer function variable, # because it is possible that it will not be assigned. producer_func = None if filename == 'index.cnxml': new_filenames = ('index.cnxml.html',) # Transform content to html. other_exists = check_for(new_filenames, module_ident) if not other_exists: msg = msg.format('cnxml', 'html', module_ident) producer_func = produce_html_for_module elif filename == 'index.cnxml.html': new_filenames = ('index.html.cnxml', 'index.cnxml',) # Transform content to cnxml. other_exists = check_for(new_filenames, module_ident) if not other_exists: msg = msg.format('html', 'cnxml', module_ident) producer_func = produce_cnxml_for_module else: # Not one of the special named files. return # skip plpy.info(msg) if producer_func is not None: producer_func(plpy, module_ident, source_filename=filename, destination_filenames=new_filenames) _transform_abstract(plpy, module_ident) return
[ "def", "add_module_file", "(", "plpy", ",", "td", ")", ":", "module_ident", "=", "td", "[", "'new'", "]", "[", "'module_ident'", "]", "filename", "=", "td", "[", "'new'", "]", "[", "'filename'", "]", "msg", "=", "\"produce {}->{} for module_ident = {}\"", "d...
Database trigger for adding a module file. When a legacy ``index.cnxml`` is added, this trigger transforms it into html and stores it as ``index.cnxml.html``. When a cnx-publishing ``index.cnxml.html`` is added, this trigger checks if ``index.html.cnxml`` exists before transforming it into cnxml and stores it as ``index.html.cnxml``. Note, we do not use ``index.html`` over ``index.cnxml.html``, because legacy allows users to name files ``index.html``.
[ "Database", "trigger", "for", "adding", "a", "module", "file", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L700-L764
openstax/cnx-archive
cnxarchive/database.py
_transform_abstract
def _transform_abstract(plpy, module_ident): """Transform abstract, bi-directionally. Transforms an abstract using one of content columns ('abstract' or 'html') to determine which direction the transform will go (cnxml->html or html->cnxml). A transform is done on either one of them to make the other value. If no value is supplied, the trigger raises an error. If both values are supplied, the trigger will skip. """ plan = plpy.prepare("""\ SELECT a.abstractid, a.abstract, a.html FROM modules AS m NATURAL JOIN abstracts AS a WHERE m.module_ident = $1""", ('integer',)) result = plpy.execute(plan, (module_ident,), 1)[0] abstractid, cnxml, html = ( result['abstractid'], result['abstract'], result['html']) if cnxml is not None and html is not None: return # skip # TODO Prevent blank abstracts (abstract = null & html = null). msg = "produce {}->{} for abstractid={}" if cnxml is None: # Transform html->cnxml msg = msg.format('html', 'cnxml', abstractid) content = html column = 'abstract' transform_func = transform_abstract_to_cnxml else: # Transform cnxml->html msg = msg.format('cnxml', 'html', abstractid) content = cnxml column = 'html' transform_func = transform_abstract_to_html content, messages = transform_func(content, module_ident, plpy) plan = plpy.prepare( "UPDATE abstracts SET {} = $1 WHERE abstractid = $2".format(column), ('text', 'integer')) plpy.execute(plan, (content, abstractid,)) return msg
python
def _transform_abstract(plpy, module_ident): """Transform abstract, bi-directionally. Transforms an abstract using one of content columns ('abstract' or 'html') to determine which direction the transform will go (cnxml->html or html->cnxml). A transform is done on either one of them to make the other value. If no value is supplied, the trigger raises an error. If both values are supplied, the trigger will skip. """ plan = plpy.prepare("""\ SELECT a.abstractid, a.abstract, a.html FROM modules AS m NATURAL JOIN abstracts AS a WHERE m.module_ident = $1""", ('integer',)) result = plpy.execute(plan, (module_ident,), 1)[0] abstractid, cnxml, html = ( result['abstractid'], result['abstract'], result['html']) if cnxml is not None and html is not None: return # skip # TODO Prevent blank abstracts (abstract = null & html = null). msg = "produce {}->{} for abstractid={}" if cnxml is None: # Transform html->cnxml msg = msg.format('html', 'cnxml', abstractid) content = html column = 'abstract' transform_func = transform_abstract_to_cnxml else: # Transform cnxml->html msg = msg.format('cnxml', 'html', abstractid) content = cnxml column = 'html' transform_func = transform_abstract_to_html content, messages = transform_func(content, module_ident, plpy) plan = plpy.prepare( "UPDATE abstracts SET {} = $1 WHERE abstractid = $2".format(column), ('text', 'integer')) plpy.execute(plan, (content, abstractid,)) return msg
[ "def", "_transform_abstract", "(", "plpy", ",", "module_ident", ")", ":", "plan", "=", "plpy", ".", "prepare", "(", "\"\"\"\\\nSELECT a.abstractid, a.abstract, a.html\nFROM modules AS m NATURAL JOIN abstracts AS a\nWHERE m.module_ident = $1\"\"\"", ",", "(", "'integer'", ",", "...
Transform abstract, bi-directionally. Transforms an abstract using one of content columns ('abstract' or 'html') to determine which direction the transform will go (cnxml->html or html->cnxml). A transform is done on either one of them to make the other value. If no value is supplied, the trigger raises an error. If both values are supplied, the trigger will skip.
[ "Transform", "abstract", "bi", "-", "directionally", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L767-L807
openstax/cnx-archive
cnxarchive/database.py
get_collection_tree
def get_collection_tree(collection_ident, cursor): """Build and retrieve json tree representation of a book.""" cursor.execute(''' WITH RECURSIVE t(node, parent, document, path) AS ( SELECT tr.nodeid, tr.parent_id, tr.documentid, ARRAY[tr.nodeid] FROM trees tr WHERE tr.documentid = %s and tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, c.documentid, path || ARRAY[c.nodeid] FROM trees c JOIN t ON c.parent_id = t.node WHERE NOT c.nodeid = ANY(t.path) ) SELECT t.document, m.portal_type FROM t JOIN modules m ON t.document = m.module_ident''', [collection_ident]) for i in cursor.fetchall(): yield i
python
def get_collection_tree(collection_ident, cursor): """Build and retrieve json tree representation of a book.""" cursor.execute(''' WITH RECURSIVE t(node, parent, document, path) AS ( SELECT tr.nodeid, tr.parent_id, tr.documentid, ARRAY[tr.nodeid] FROM trees tr WHERE tr.documentid = %s and tr.is_collated = 'False' UNION ALL SELECT c.nodeid, c.parent_id, c.documentid, path || ARRAY[c.nodeid] FROM trees c JOIN t ON c.parent_id = t.node WHERE NOT c.nodeid = ANY(t.path) ) SELECT t.document, m.portal_type FROM t JOIN modules m ON t.document = m.module_ident''', [collection_ident]) for i in cursor.fetchall(): yield i
[ "def", "get_collection_tree", "(", "collection_ident", ",", "cursor", ")", ":", "cursor", ".", "execute", "(", "'''\n WITH RECURSIVE t(node, parent, document, path) AS (\n SELECT tr.nodeid, tr.parent_id, tr.documentid, ARRAY[tr.nodeid]\n FROM trees tr\n WHERE tr.docum...
Build and retrieve json tree representation of a book.
[ "Build", "and", "retrieve", "json", "tree", "representation", "of", "a", "book", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L810-L826
openstax/cnx-archive
cnxarchive/database.py
get_module_can_publish
def get_module_can_publish(cursor, id): """Return userids allowed to publish this book.""" cursor.execute(""" SELECT DISTINCT user_id FROM document_acl WHERE uuid = %s AND permission = 'publish'""", (id,)) return [i[0] for i in cursor.fetchall()]
python
def get_module_can_publish(cursor, id): """Return userids allowed to publish this book.""" cursor.execute(""" SELECT DISTINCT user_id FROM document_acl WHERE uuid = %s AND permission = 'publish'""", (id,)) return [i[0] for i in cursor.fetchall()]
[ "def", "get_module_can_publish", "(", "cursor", ",", "id", ")", ":", "cursor", ".", "execute", "(", "\"\"\"\nSELECT DISTINCT user_id\nFROM document_acl\nWHERE uuid = %s AND permission = 'publish'\"\"\"", ",", "(", "id", ",", ")", ")", "return", "[", "i", "[", "0", "]"...
Return userids allowed to publish this book.
[ "Return", "userids", "allowed", "to", "publish", "this", "book", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/database.py#L829-L835
openstax/cnx-archive
cnxarchive/scripts/export_epub/modeling.py
tree_to_nodes
def tree_to_nodes(tree, context=None, metadata=None): """Assembles ``tree`` nodes into object models. If ``context`` is supplied, it will be used to contextualize the contents of the nodes. Metadata will pass non-node identifying values down to child nodes, if not overridden (license, timestamps, etc) """ nodes = [] for item in tree['contents']: if 'contents' in item: sub_nodes = tree_to_nodes(item, context=context, metadata=metadata) if metadata is None: metadata = {} else: metadata = metadata.copy() for key in ('title', 'id', 'shortid', 'cnx-archive-uri', 'cnx-archive-shortid'): if key in metadata: metadata.pop(key) for key in ('title', 'id', 'shortId'): if item.get(key): metadata[key] = item[key] if item[key] != 'subcol': if key == 'id': metadata['cnx-archive-uri'] = item[key] elif key == 'shortId': metadata['cnx-archive-shortid'] = item[key] titles = _title_overrides_from_tree(item) if item.get('id') is not None: tbinder = cnxepub.Binder(item.get('id'), sub_nodes, metadata=metadata, title_overrides=titles) else: tbinder = cnxepub.TranslucentBinder(sub_nodes, metadata=metadata, title_overrides=titles) nodes.append(tbinder) else: doc = document_factory(item['id'], context=context) for key in ('title', 'id', 'shortId'): if item.get(key): doc.metadata[key] = item[key] if key == 'id': doc.metadata['cnx-archive-uri'] = item[key] elif key == 'shortId': doc.metadata['cnx-archive-shortid'] = item[key] nodes.append(doc) return nodes
python
def tree_to_nodes(tree, context=None, metadata=None): """Assembles ``tree`` nodes into object models. If ``context`` is supplied, it will be used to contextualize the contents of the nodes. Metadata will pass non-node identifying values down to child nodes, if not overridden (license, timestamps, etc) """ nodes = [] for item in tree['contents']: if 'contents' in item: sub_nodes = tree_to_nodes(item, context=context, metadata=metadata) if metadata is None: metadata = {} else: metadata = metadata.copy() for key in ('title', 'id', 'shortid', 'cnx-archive-uri', 'cnx-archive-shortid'): if key in metadata: metadata.pop(key) for key in ('title', 'id', 'shortId'): if item.get(key): metadata[key] = item[key] if item[key] != 'subcol': if key == 'id': metadata['cnx-archive-uri'] = item[key] elif key == 'shortId': metadata['cnx-archive-shortid'] = item[key] titles = _title_overrides_from_tree(item) if item.get('id') is not None: tbinder = cnxepub.Binder(item.get('id'), sub_nodes, metadata=metadata, title_overrides=titles) else: tbinder = cnxepub.TranslucentBinder(sub_nodes, metadata=metadata, title_overrides=titles) nodes.append(tbinder) else: doc = document_factory(item['id'], context=context) for key in ('title', 'id', 'shortId'): if item.get(key): doc.metadata[key] = item[key] if key == 'id': doc.metadata['cnx-archive-uri'] = item[key] elif key == 'shortId': doc.metadata['cnx-archive-shortid'] = item[key] nodes.append(doc) return nodes
[ "def", "tree_to_nodes", "(", "tree", ",", "context", "=", "None", ",", "metadata", "=", "None", ")", ":", "nodes", "=", "[", "]", "for", "item", "in", "tree", "[", "'contents'", "]", ":", "if", "'contents'", "in", "item", ":", "sub_nodes", "=", "tree...
Assembles ``tree`` nodes into object models. If ``context`` is supplied, it will be used to contextualize the contents of the nodes. Metadata will pass non-node identifying values down to child nodes, if not overridden (license, timestamps, etc)
[ "Assembles", "tree", "nodes", "into", "object", "models", ".", "If", "context", "is", "supplied", "it", "will", "be", "used", "to", "contextualize", "the", "contents", "of", "the", "nodes", ".", "Metadata", "will", "pass", "non", "-", "node", "identifying", ...
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/modeling.py#L77-L126
openstax/cnx-archive
cnxarchive/scripts/export_epub/modeling.py
create_epub
def create_epub(ident_hash, file, format='raw'): """Creates an epub from an ``ident_hash``, which is output to the given ``file`` (a file-like object). Returns None, writes to the given ``file``. """ model = factory(ident_hash, baked=(format != 'raw')) if isinstance(model, cnxepub.Document): model = cnxepub.TranslucentBinder(nodes=[model]) cnxepub.make_epub(model, file)
python
def create_epub(ident_hash, file, format='raw'): """Creates an epub from an ``ident_hash``, which is output to the given ``file`` (a file-like object). Returns None, writes to the given ``file``. """ model = factory(ident_hash, baked=(format != 'raw')) if isinstance(model, cnxepub.Document): model = cnxepub.TranslucentBinder(nodes=[model]) cnxepub.make_epub(model, file)
[ "def", "create_epub", "(", "ident_hash", ",", "file", ",", "format", "=", "'raw'", ")", ":", "model", "=", "factory", "(", "ident_hash", ",", "baked", "=", "(", "format", "!=", "'raw'", ")", ")", "if", "isinstance", "(", "model", ",", "cnxepub", ".", ...
Creates an epub from an ``ident_hash``, which is output to the given ``file`` (a file-like object). Returns None, writes to the given ``file``.
[ "Creates", "an", "epub", "from", "an", "ident_hash", "which", "is", "output", "to", "the", "given", "file", "(", "a", "file", "-", "like", "object", ")", ".", "Returns", "None", "writes", "to", "the", "given", "file", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/export_epub/modeling.py#L161-L170
openstax/cnx-archive
cnxarchive/views/exports.py
get_export
def get_export(request): """Retrieve an export file.""" settings = get_current_registry().settings exports_dirs = settings['exports-directories'].split() args = request.matchdict ident_hash, type = args['ident_hash'], args['type'] id, version = split_ident_hash(ident_hash) with db_connect() as db_connection: with db_connection.cursor() as cursor: try: results = get_export_files(cursor, id, version, [type], exports_dirs, read_file=True) if not results: raise httpexceptions.HTTPNotFound() filename, mimetype, size, modtime, state, file_content \ = results[0] except ExportError as e: logger.debug(str(e)) raise httpexceptions.HTTPNotFound() if state == 'missing': raise httpexceptions.HTTPNotFound() encoded_filename = urllib.quote(filename.encode('utf-8')) resp = request.response resp.status = "200 OK" resp.content_type = mimetype # Need both filename and filename* below for various browsers # See: https://fastmail.blog/2011/06/24/download-non-english-filenames/ resp.content_disposition = "attachment; filename={fname};" \ " filename*=UTF-8''{fname}".format( fname=encoded_filename) resp.body = file_content # Remove version and extension from filename, to recover title slug slug_title = '-'.join(encoded_filename.split('-')[:-1]) resp.headerlist.append( ('Link', '<https://{}/contents/{}/{}> ;rel="Canonical"'.format( request.host, id, slug_title))) return resp
python
def get_export(request): """Retrieve an export file.""" settings = get_current_registry().settings exports_dirs = settings['exports-directories'].split() args = request.matchdict ident_hash, type = args['ident_hash'], args['type'] id, version = split_ident_hash(ident_hash) with db_connect() as db_connection: with db_connection.cursor() as cursor: try: results = get_export_files(cursor, id, version, [type], exports_dirs, read_file=True) if not results: raise httpexceptions.HTTPNotFound() filename, mimetype, size, modtime, state, file_content \ = results[0] except ExportError as e: logger.debug(str(e)) raise httpexceptions.HTTPNotFound() if state == 'missing': raise httpexceptions.HTTPNotFound() encoded_filename = urllib.quote(filename.encode('utf-8')) resp = request.response resp.status = "200 OK" resp.content_type = mimetype # Need both filename and filename* below for various browsers # See: https://fastmail.blog/2011/06/24/download-non-english-filenames/ resp.content_disposition = "attachment; filename={fname};" \ " filename*=UTF-8''{fname}".format( fname=encoded_filename) resp.body = file_content # Remove version and extension from filename, to recover title slug slug_title = '-'.join(encoded_filename.split('-')[:-1]) resp.headerlist.append( ('Link', '<https://{}/contents/{}/{}> ;rel="Canonical"'.format( request.host, id, slug_title))) return resp
[ "def", "get_export", "(", "request", ")", ":", "settings", "=", "get_current_registry", "(", ")", ".", "settings", "exports_dirs", "=", "settings", "[", "'exports-directories'", "]", ".", "split", "(", ")", "args", "=", "request", ".", "matchdict", "ident_hash...
Retrieve an export file.
[ "Retrieve", "an", "export", "file", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/exports.py#L50-L89
openstax/cnx-archive
cnxarchive/views/exports.py
get_export_files
def get_export_files(cursor, id, version, types, exports_dirs, read_file=True): """Retrieve files associated with document.""" request = get_current_request() type_info = dict(request.registry.settings['_type_info']) metadata = get_content_metadata(id, version, cursor) legacy_id = metadata['legacy_id'] legacy_version = metadata['legacy_version'] reachable_dirs = [dir for dir in exports_dirs if safe_stat(dir)] # 1 result per type, in the same order as the given types results = [] for type in list(types): if type not in type_info: raise ExportError("invalid type '{}' requested.".format(type)) file_extension = type_info[type]['file_extension'] # skip module PDFs if metadata['mediaType'] == MODULE_MIMETYPE and \ file_extension == 'pdf': continue mimetype = type_info[type]['mimetype'] filename = '{}@{}.{}'.format(id, version, file_extension) legacy_filenames = [ '{}-{}.{}'.format(legacy_id, legacy_version, ext) for ext in LEGACY_EXTENSION_MAP[file_extension] ] slugify_title_filename = u'{}-{}.{}'.format(slugify(metadata['title']), version, file_extension) for dir in reachable_dirs: filepath = os.path.join(dir, filename) try: if read_file: with open(filepath, 'r') as file: stats = os.fstat(file.fileno()) contents = file.read() else: stats = os.stat(filepath) contents = None modtime = fromtimestamp(int(stats.st_mtime)) results.append((slugify_title_filename, mimetype, stats.st_size, modtime, 'good', contents)) break except EnvironmentError: pass else: # Let's see if the legacy file's there and make the new link legacy_file_found = False for dir in reachable_dirs: filepath = os.path.join(dir, filename) legacy_filepaths = [os.path.join(dir, fn) for fn in legacy_filenames] for legacy_filepath in legacy_filepaths: try: if read_file: with open(legacy_filepath, 'r') as file: stats = os.fstat(file.fileno()) contents = file.read() else: stats = os.stat(legacy_filepath) contents = None modtime = fromtimestamp(stats.st_mtime) os.link(legacy_filepath, filepath) results.append((slugify_title_filename, mimetype, stats.st_size, modtime, 'good', contents)) legacy_file_found = True break except EnvironmentError: pass if legacy_file_found: break else: filenames = [filename] + legacy_filenames log_formatted_filenames = '\n'.join([' - {}'.format(x) for x in filenames]) logger.error("Could not find a file for '{}' at version '{}' " "with any of the following file names:\n{}" .format(id, version, log_formatted_filenames)) # No file, return "missing" state results.append((slugify_title_filename, mimetype, 0, None, 'missing', None)) return results
python
def get_export_files(cursor, id, version, types, exports_dirs, read_file=True): """Retrieve files associated with document.""" request = get_current_request() type_info = dict(request.registry.settings['_type_info']) metadata = get_content_metadata(id, version, cursor) legacy_id = metadata['legacy_id'] legacy_version = metadata['legacy_version'] reachable_dirs = [dir for dir in exports_dirs if safe_stat(dir)] # 1 result per type, in the same order as the given types results = [] for type in list(types): if type not in type_info: raise ExportError("invalid type '{}' requested.".format(type)) file_extension = type_info[type]['file_extension'] # skip module PDFs if metadata['mediaType'] == MODULE_MIMETYPE and \ file_extension == 'pdf': continue mimetype = type_info[type]['mimetype'] filename = '{}@{}.{}'.format(id, version, file_extension) legacy_filenames = [ '{}-{}.{}'.format(legacy_id, legacy_version, ext) for ext in LEGACY_EXTENSION_MAP[file_extension] ] slugify_title_filename = u'{}-{}.{}'.format(slugify(metadata['title']), version, file_extension) for dir in reachable_dirs: filepath = os.path.join(dir, filename) try: if read_file: with open(filepath, 'r') as file: stats = os.fstat(file.fileno()) contents = file.read() else: stats = os.stat(filepath) contents = None modtime = fromtimestamp(int(stats.st_mtime)) results.append((slugify_title_filename, mimetype, stats.st_size, modtime, 'good', contents)) break except EnvironmentError: pass else: # Let's see if the legacy file's there and make the new link legacy_file_found = False for dir in reachable_dirs: filepath = os.path.join(dir, filename) legacy_filepaths = [os.path.join(dir, fn) for fn in legacy_filenames] for legacy_filepath in legacy_filepaths: try: if read_file: with open(legacy_filepath, 'r') as file: stats = os.fstat(file.fileno()) contents = file.read() else: stats = os.stat(legacy_filepath) contents = None modtime = fromtimestamp(stats.st_mtime) os.link(legacy_filepath, filepath) results.append((slugify_title_filename, mimetype, stats.st_size, modtime, 'good', contents)) legacy_file_found = True break except EnvironmentError: pass if legacy_file_found: break else: filenames = [filename] + legacy_filenames log_formatted_filenames = '\n'.join([' - {}'.format(x) for x in filenames]) logger.error("Could not find a file for '{}' at version '{}' " "with any of the following file names:\n{}" .format(id, version, log_formatted_filenames)) # No file, return "missing" state results.append((slugify_title_filename, mimetype, 0, None, 'missing', None)) return results
[ "def", "get_export_files", "(", "cursor", ",", "id", ",", "version", ",", "types", ",", "exports_dirs", ",", "read_file", "=", "True", ")", ":", "request", "=", "get_current_request", "(", ")", "type_info", "=", "dict", "(", "request", ".", "registry", "."...
Retrieve files associated with document.
[ "Retrieve", "files", "associated", "with", "document", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/exports.py#L92-L178
openstax/cnx-archive
cnxarchive/views/helpers.py
get_content_metadata
def get_content_metadata(id, version, cursor): """Return metadata related to the content from the database.""" # Do the module lookup args = dict(id=id, version=version) # FIXME We are doing two queries here that can hopefully be # condensed into one. cursor.execute(SQL['get-module-metadata'], args) try: result = cursor.fetchone()[0] # version is what we want to return, but in the sql we're using # current_version because otherwise there's a "column reference is # ambiguous" error result['version'] = result.pop('current_version') # FIXME We currently have legacy 'portal_type' names in the database. # Future upgrades should replace the portal type with a mimetype # of 'application/vnd.org.cnx.(module|collection|folder|<etc>)'. # Until then we will do the replacement here. result['mediaType'] = portaltype_to_mimetype(result['mediaType']) return result except (TypeError, IndexError,): # None returned raise httpexceptions.HTTPNotFound()
python
def get_content_metadata(id, version, cursor): """Return metadata related to the content from the database.""" # Do the module lookup args = dict(id=id, version=version) # FIXME We are doing two queries here that can hopefully be # condensed into one. cursor.execute(SQL['get-module-metadata'], args) try: result = cursor.fetchone()[0] # version is what we want to return, but in the sql we're using # current_version because otherwise there's a "column reference is # ambiguous" error result['version'] = result.pop('current_version') # FIXME We currently have legacy 'portal_type' names in the database. # Future upgrades should replace the portal type with a mimetype # of 'application/vnd.org.cnx.(module|collection|folder|<etc>)'. # Until then we will do the replacement here. result['mediaType'] = portaltype_to_mimetype(result['mediaType']) return result except (TypeError, IndexError,): # None returned raise httpexceptions.HTTPNotFound()
[ "def", "get_content_metadata", "(", "id", ",", "version", ",", "cursor", ")", ":", "# Do the module lookup", "args", "=", "dict", "(", "id", "=", "id", ",", "version", "=", "version", ")", "# FIXME We are doing two queries here that can hopefully be", "# condens...
Return metadata related to the content from the database.
[ "Return", "metadata", "related", "to", "the", "content", "from", "the", "database", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/helpers.py#L61-L83
ZELLMECHANIK-DRESDEN/dclab
dclab/kde_contours.py
find_contours_level
def find_contours_level(density, x, y, level, closed=False): """Find iso-valued density contours for a given level value Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` level: float between 0 and 1 Value along which to find contours in `kde` relative to its maximum kde Returns ------- contours: list of ndarrays of shape (P, 2) Contours found for the given level value See Also -------- skimage.measure.find_contours: Contour finding algorithm used """ if level >= 1 or level <= 0: raise ValueError("`level` must be in (0,1), got '{}'!".format(level)) # level relative to maximum level = level * density.max() # xy coordinates if len(x.shape) == 2: assert np.all(x[:, 0] == x[:, 1]) x = x[:, 0] if len(y.shape) == 2: assert np.all(y[0, :] == y[1, :]) y = y[0, :] if closed: # find closed contours density = np.pad(density, ((1, 1), (1, 1)), mode="constant") offset = 1 else: # leave contours open at kde boundary offset = 0 conts_idx = find_contours(density, level) conts_xy = [] for cc in conts_idx: cx = np.interp(x=cc[:, 0]-offset, xp=range(x.size), fp=x) cy = np.interp(x=cc[:, 1]-offset, xp=range(y.size), fp=y) conts_xy.append(np.stack((cx, cy), axis=1)) return conts_xy
python
def find_contours_level(density, x, y, level, closed=False): """Find iso-valued density contours for a given level value Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` level: float between 0 and 1 Value along which to find contours in `kde` relative to its maximum kde Returns ------- contours: list of ndarrays of shape (P, 2) Contours found for the given level value See Also -------- skimage.measure.find_contours: Contour finding algorithm used """ if level >= 1 or level <= 0: raise ValueError("`level` must be in (0,1), got '{}'!".format(level)) # level relative to maximum level = level * density.max() # xy coordinates if len(x.shape) == 2: assert np.all(x[:, 0] == x[:, 1]) x = x[:, 0] if len(y.shape) == 2: assert np.all(y[0, :] == y[1, :]) y = y[0, :] if closed: # find closed contours density = np.pad(density, ((1, 1), (1, 1)), mode="constant") offset = 1 else: # leave contours open at kde boundary offset = 0 conts_idx = find_contours(density, level) conts_xy = [] for cc in conts_idx: cx = np.interp(x=cc[:, 0]-offset, xp=range(x.size), fp=x) cy = np.interp(x=cc[:, 1]-offset, xp=range(y.size), fp=y) conts_xy.append(np.stack((cx, cy), axis=1)) return conts_xy
[ "def", "find_contours_level", "(", "density", ",", "x", ",", "y", ",", "level", ",", "closed", "=", "False", ")", ":", "if", "level", ">=", "1", "or", "level", "<=", "0", ":", "raise", "ValueError", "(", "\"`level` must be in (0,1), got '{}'!\"", ".", "for...
Find iso-valued density contours for a given level value Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` level: float between 0 and 1 Value along which to find contours in `kde` relative to its maximum kde Returns ------- contours: list of ndarrays of shape (P, 2) Contours found for the given level value See Also -------- skimage.measure.find_contours: Contour finding algorithm used
[ "Find", "iso", "-", "valued", "density", "contours", "for", "a", "given", "level", "value" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/kde_contours.py#L12-L67
ZELLMECHANIK-DRESDEN/dclab
dclab/kde_contours.py
get_quantile_levels
def get_quantile_levels(density, x, y, xp, yp, q, normalize=True): """Compute density levels for given quantiles by interpolation For a given 2D density, compute the density levels at which the resulting contours contain the fraction `1-q` of all data points. E.g. for a measurement of 1000 events, all contours at the level corresponding to a quantile of `q=0.95` (95th percentile) contain 50 events (5%). Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` xp: 1d ndarray of size D Event x-data from which to compute the quantile yp: 1d ndarray of size D Event y-data from which to compute the quantile q: array_like or float between 0 and 1 Quantile along which to find contours in `kde` relative to its maximum normalize: bool Whether output levels should be normalized to the maximum of `density` Returns ------- level: float Contours level corresponding to the given quantile Notes ----- NaN-values events in `xp` and `yp` are ignored. """ # xy coordinates if len(x.shape) == 2: assert np.all(x[:, 0] == x[:, 1]) x = x[:, 0] if len(y.shape) == 2: assert np.all(y[0, :] == y[1, :]) y = y[0, :] # remove bad events bad = get_bad_vals(xp, yp) xp = xp[~bad] yp = yp[~bad] # Normalize interpolation data such that the spacing for # x and y is about the same during interpolation. x_norm = x.max() x = x / x_norm xp = xp / x_norm y_norm = y.max() y = y / y_norm yp = yp / y_norm # Perform interpolation dp = spint.interpn((x, y), density, (xp, yp), method='linear', bounds_error=False, fill_value=0) if normalize: dp /= density.max() if not np.isscalar(q): q = np.array(q) plev = np.nanpercentile(dp, q=q*100) return plev
python
def get_quantile_levels(density, x, y, xp, yp, q, normalize=True): """Compute density levels for given quantiles by interpolation For a given 2D density, compute the density levels at which the resulting contours contain the fraction `1-q` of all data points. E.g. for a measurement of 1000 events, all contours at the level corresponding to a quantile of `q=0.95` (95th percentile) contain 50 events (5%). Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` xp: 1d ndarray of size D Event x-data from which to compute the quantile yp: 1d ndarray of size D Event y-data from which to compute the quantile q: array_like or float between 0 and 1 Quantile along which to find contours in `kde` relative to its maximum normalize: bool Whether output levels should be normalized to the maximum of `density` Returns ------- level: float Contours level corresponding to the given quantile Notes ----- NaN-values events in `xp` and `yp` are ignored. """ # xy coordinates if len(x.shape) == 2: assert np.all(x[:, 0] == x[:, 1]) x = x[:, 0] if len(y.shape) == 2: assert np.all(y[0, :] == y[1, :]) y = y[0, :] # remove bad events bad = get_bad_vals(xp, yp) xp = xp[~bad] yp = yp[~bad] # Normalize interpolation data such that the spacing for # x and y is about the same during interpolation. x_norm = x.max() x = x / x_norm xp = xp / x_norm y_norm = y.max() y = y / y_norm yp = yp / y_norm # Perform interpolation dp = spint.interpn((x, y), density, (xp, yp), method='linear', bounds_error=False, fill_value=0) if normalize: dp /= density.max() if not np.isscalar(q): q = np.array(q) plev = np.nanpercentile(dp, q=q*100) return plev
[ "def", "get_quantile_levels", "(", "density", ",", "x", ",", "y", ",", "xp", ",", "yp", ",", "q", ",", "normalize", "=", "True", ")", ":", "# xy coordinates", "if", "len", "(", "x", ".", "shape", ")", "==", "2", ":", "assert", "np", ".", "all", "...
Compute density levels for given quantiles by interpolation For a given 2D density, compute the density levels at which the resulting contours contain the fraction `1-q` of all data points. E.g. for a measurement of 1000 events, all contours at the level corresponding to a quantile of `q=0.95` (95th percentile) contain 50 events (5%). Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` xp: 1d ndarray of size D Event x-data from which to compute the quantile yp: 1d ndarray of size D Event y-data from which to compute the quantile q: array_like or float between 0 and 1 Quantile along which to find contours in `kde` relative to its maximum normalize: bool Whether output levels should be normalized to the maximum of `density` Returns ------- level: float Contours level corresponding to the given quantile Notes ----- NaN-values events in `xp` and `yp` are ignored.
[ "Compute", "density", "levels", "for", "given", "quantiles", "by", "interpolation" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/kde_contours.py#L70-L143
ZELLMECHANIK-DRESDEN/dclab
dclab/kde_contours.py
_find_quantile_level
def _find_quantile_level(density, x, y, xp, yp, quantile, acc=.01, ret_err=False): """Find density level for a given data quantile by iteration Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` xp: 1d ndarray of size D Event x-data from which to compute the quantile yp: 1d ndarray of size D Event y-data from which to compute the quantile quantile: float between 0 and 1 Quantile along which to find contours in `kde` relative to its maximum acc: float Desired absolute accuracy (stopping criterion) of the contours ret_err: bool If True, also return the absolute error Returns ------- level: float Contours level corresponding to the given quantile Notes ----- A much more faster method (using interpolation) is implemented in :func:`get_quantile_levels`. NaN-values events in `xp` and `yp` are ignored. See Also -------- skimage.measure.find_contours: Contour finding algorithm """ if quantile >= 1 or quantile <= 0: raise ValueError("Invalid value for `quantile`: {}".format(quantile)) # remove bad events bad = get_bad_vals(xp, yp) xp = xp[~bad] yp = yp[~bad] # initial guess level = quantile # error of current iteration err = 1 # iteration factor (guarantees convergence) itfac = 1 # total number of events nev = xp.size while np.abs(err) > acc: # compute contours conts = find_contours_level(density, x, y, level, closed=True) # compute number of points in contour isin = 0 for ii in range(nev): for cc in conts: isin += PolygonFilter.point_in_poly((xp[ii], yp[ii]), poly=cc) break # no need to check other contours err = quantile - (nev - isin) / nev level += err * itfac itfac *= .9 if ret_err: return level, err else: return level
python
def _find_quantile_level(density, x, y, xp, yp, quantile, acc=.01, ret_err=False): """Find density level for a given data quantile by iteration Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` xp: 1d ndarray of size D Event x-data from which to compute the quantile yp: 1d ndarray of size D Event y-data from which to compute the quantile quantile: float between 0 and 1 Quantile along which to find contours in `kde` relative to its maximum acc: float Desired absolute accuracy (stopping criterion) of the contours ret_err: bool If True, also return the absolute error Returns ------- level: float Contours level corresponding to the given quantile Notes ----- A much more faster method (using interpolation) is implemented in :func:`get_quantile_levels`. NaN-values events in `xp` and `yp` are ignored. See Also -------- skimage.measure.find_contours: Contour finding algorithm """ if quantile >= 1 or quantile <= 0: raise ValueError("Invalid value for `quantile`: {}".format(quantile)) # remove bad events bad = get_bad_vals(xp, yp) xp = xp[~bad] yp = yp[~bad] # initial guess level = quantile # error of current iteration err = 1 # iteration factor (guarantees convergence) itfac = 1 # total number of events nev = xp.size while np.abs(err) > acc: # compute contours conts = find_contours_level(density, x, y, level, closed=True) # compute number of points in contour isin = 0 for ii in range(nev): for cc in conts: isin += PolygonFilter.point_in_poly((xp[ii], yp[ii]), poly=cc) break # no need to check other contours err = quantile - (nev - isin) / nev level += err * itfac itfac *= .9 if ret_err: return level, err else: return level
[ "def", "_find_quantile_level", "(", "density", ",", "x", ",", "y", ",", "xp", ",", "yp", ",", "quantile", ",", "acc", "=", ".01", ",", "ret_err", "=", "False", ")", ":", "if", "quantile", ">=", "1", "or", "quantile", "<=", "0", ":", "raise", "Value...
Find density level for a given data quantile by iteration Parameters ---------- density: 2d ndarray of shape (M, N) Kernel density estimate for which to compute the contours x: 2d ndarray of shape (M, N) or 1d ndarray of size M X-values corresponding to `kde` y: 2d ndarray of shape (M, N) or 1d ndarray of size M Y-values corresponding to `kde` xp: 1d ndarray of size D Event x-data from which to compute the quantile yp: 1d ndarray of size D Event y-data from which to compute the quantile quantile: float between 0 and 1 Quantile along which to find contours in `kde` relative to its maximum acc: float Desired absolute accuracy (stopping criterion) of the contours ret_err: bool If True, also return the absolute error Returns ------- level: float Contours level corresponding to the given quantile Notes ----- A much more faster method (using interpolation) is implemented in :func:`get_quantile_levels`. NaN-values events in `xp` and `yp` are ignored. See Also -------- skimage.measure.find_contours: Contour finding algorithm
[ "Find", "density", "level", "for", "a", "given", "data", "quantile", "by", "iteration" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/kde_contours.py#L146-L220
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/write_hdf5.py
write
def write(path_or_h5file, data={}, meta={}, logs={}, mode="reset", compression=None): """Write data to an RT-DC file Parameters ---------- path: path or h5py.File The path or the hdf5 file object to write to. data: dict-like The data to store. Each key of `data` must be a valid feature name (see `dclab.dfn.feature_names`). The data type must be given according to the feature type: - scalar feature: 1d ndarray of size `N`, any dtype, with the number of events `N`. - contour: list of `N` 2d ndarrays of shape `(2,C)`, any dtype, with each ndarray containing the x- and y- coordinates of `C` contour points in pixels. - image: 3d ndarray of shape `(N,A,B)`, is converted to uint8, with the image dimensions `(x,y) = (A,B)` - mask: 3d ndarray of shape `(N,A,B)`, is converted to bool, with the image dimensions `(x,y) = (A,B)` - trace: 2d ndarray of shape `(N,T)`, any dtype with a globally constant trace length `T`. meta: dict of dicts The meta data to store (see `dclab.dfn.config_keys`). Each key depicts a meta data section name whose data is given as a dictionary, e.g. meta = {"imaging": {"exposure time": 20, "flash duration": 2, ... }, "setup": {"channel width": 20, "chip region": "channel", ... }, ... } Only section key names and key values therein registered in dclab are allowed and are converted to the pre-defined dtype. logs: dict of lists Each key of `logs` refers to a list of strings that contains logging information. Each item in the list can be considered to be one line in the log file. mode: str Defines how the input `data` and `logs` are stored: - "append": append new data to existing Datasets; the opened `h5py.File` object is returned (used in real- time data storage) - "replace": replace keys given by `data` and `logs`; the opened `h5py.File` object is closed and `None` is returned (used for ancillary feature storage) - "reset": do not keep any previous data; the opened `h5py.File` object is closed and `None` is returned (default) compression: str Compression method for "contour", "image", and "trace" data as well as logs; one of [None, "lzf", "gzip", "szip"]. Notes ----- If `data` is an instance of RTDCBase, then `meta` must be set to `data.config`, otherwise no meta data will be saved. """ if mode not in ["append", "replace", "reset"]: raise ValueError("`mode` must be one of [append, replace, reset]") if compression not in [None, "gzip", "lzf", "szip"]: raise ValueError("`compression` must be one of [gzip, lzf, szip]") if (not hasattr(data, "__iter__") or not hasattr(data, "__contains__") or not hasattr(data, "__getitem__") or isinstance(data, (list, np.ndarray))): msg = "`data` must be dict-like" raise ValueError(msg) # Check meta data for sec in meta: if sec not in dfn.config_keys: # only allow writing of meta data that are not editable # by the user (not dclab.dfn.CFG_ANALYSIS) msg = "Meta data section not defined in dclab: {}".format(sec) raise ValueError(msg) for ck in meta[sec]: if ck not in dfn.config_keys[sec]: msg = "Meta key not defined in dclab: {}:{}".format(sec, ck) raise ValueError(msg) # Check feature keys feat_keys = [] for kk in data: if kk in dfn.feature_names: feat_keys.append(kk) else: raise ValueError("Unknown key '{}'!".format(kk)) # verify trace names if kk == "trace": for sk in data["trace"]: if sk not in dfn.FLUOR_TRACES: msg = "Unknown trace key: {}".format(sk) raise ValueError(msg) # Create file # (this should happen after all checks) if isinstance(path_or_h5file, h5py.File): h5obj = path_or_h5file else: if mode == "reset": h5mode = "w" else: h5mode = "a" h5obj = h5py.File(path_or_h5file, mode=h5mode) # update version # - if it is not already in the hdf5 file (prevent override) # - if it is explicitly given in meta (append to old version string) if ("setup:software version" not in h5obj.attrs or ("setup" in meta and "software version" in meta["setup"])): thisver = "dclab {}".format(version) if "setup" in meta and "software version" in meta["setup"]: oldver = meta["setup"]["software version"] thisver = "{} | {}".format(oldver, thisver) if "setup" not in meta: meta["setup"] = {} meta["setup"]["software version"] = thisver # Write meta for sec in meta: for ck in meta[sec]: idk = "{}:{}".format(sec, ck) conffunc = dfn.config_funcs[sec][ck] h5obj.attrs[idk] = conffunc(meta[sec][ck]) # Write data # create events group if "events" not in h5obj: h5obj.create_group("events") events = h5obj["events"] # remove previous data if mode == "replace": for rk in feat_keys: if rk in events: del events[rk] # store experimental data for fk in feat_keys: if fk in dfn.scalar_feature_names: store_scalar(h5group=events, name=fk, data=data[fk]) elif fk == "contour": store_contour(h5group=events, data=data["contour"], compression=compression) elif fk == "image": store_image(h5group=events, data=data["image"], compression=compression) elif fk == "mask": store_mask(h5group=events, data=data["mask"], compression=compression) elif fk == "trace": store_trace(h5group=events, data=data["trace"], compression=compression) # Write logs if "logs" not in h5obj: h5obj.create_group("logs") log_group = h5obj["logs"] # remove previous data if mode == "replace": for rl in logs: if rl in log_group: del log_group[rl] dt = h5py.special_dtype(vlen=hdf5_str) for lkey in logs: ldata = logs[lkey] if isinstance(ldata, (str, hdf5_str)): # single event ldata = [ldata] lnum = len(ldata) if lkey not in log_group: log_dset = log_group.create_dataset(lkey, (lnum,), dtype=dt, maxshape=(None,), chunks=True, fletcher32=True, compression=compression) for ii, line in enumerate(ldata): log_dset[ii] = line else: log_dset = log_group[lkey] oldsize = log_dset.shape[0] log_dset.resize(oldsize + lnum, axis=0) for ii, line in enumerate(ldata): log_dset[oldsize + ii] = line if mode == "append": return h5obj else: h5obj.close() return None
python
def write(path_or_h5file, data={}, meta={}, logs={}, mode="reset", compression=None): """Write data to an RT-DC file Parameters ---------- path: path or h5py.File The path or the hdf5 file object to write to. data: dict-like The data to store. Each key of `data` must be a valid feature name (see `dclab.dfn.feature_names`). The data type must be given according to the feature type: - scalar feature: 1d ndarray of size `N`, any dtype, with the number of events `N`. - contour: list of `N` 2d ndarrays of shape `(2,C)`, any dtype, with each ndarray containing the x- and y- coordinates of `C` contour points in pixels. - image: 3d ndarray of shape `(N,A,B)`, is converted to uint8, with the image dimensions `(x,y) = (A,B)` - mask: 3d ndarray of shape `(N,A,B)`, is converted to bool, with the image dimensions `(x,y) = (A,B)` - trace: 2d ndarray of shape `(N,T)`, any dtype with a globally constant trace length `T`. meta: dict of dicts The meta data to store (see `dclab.dfn.config_keys`). Each key depicts a meta data section name whose data is given as a dictionary, e.g. meta = {"imaging": {"exposure time": 20, "flash duration": 2, ... }, "setup": {"channel width": 20, "chip region": "channel", ... }, ... } Only section key names and key values therein registered in dclab are allowed and are converted to the pre-defined dtype. logs: dict of lists Each key of `logs` refers to a list of strings that contains logging information. Each item in the list can be considered to be one line in the log file. mode: str Defines how the input `data` and `logs` are stored: - "append": append new data to existing Datasets; the opened `h5py.File` object is returned (used in real- time data storage) - "replace": replace keys given by `data` and `logs`; the opened `h5py.File` object is closed and `None` is returned (used for ancillary feature storage) - "reset": do not keep any previous data; the opened `h5py.File` object is closed and `None` is returned (default) compression: str Compression method for "contour", "image", and "trace" data as well as logs; one of [None, "lzf", "gzip", "szip"]. Notes ----- If `data` is an instance of RTDCBase, then `meta` must be set to `data.config`, otherwise no meta data will be saved. """ if mode not in ["append", "replace", "reset"]: raise ValueError("`mode` must be one of [append, replace, reset]") if compression not in [None, "gzip", "lzf", "szip"]: raise ValueError("`compression` must be one of [gzip, lzf, szip]") if (not hasattr(data, "__iter__") or not hasattr(data, "__contains__") or not hasattr(data, "__getitem__") or isinstance(data, (list, np.ndarray))): msg = "`data` must be dict-like" raise ValueError(msg) # Check meta data for sec in meta: if sec not in dfn.config_keys: # only allow writing of meta data that are not editable # by the user (not dclab.dfn.CFG_ANALYSIS) msg = "Meta data section not defined in dclab: {}".format(sec) raise ValueError(msg) for ck in meta[sec]: if ck not in dfn.config_keys[sec]: msg = "Meta key not defined in dclab: {}:{}".format(sec, ck) raise ValueError(msg) # Check feature keys feat_keys = [] for kk in data: if kk in dfn.feature_names: feat_keys.append(kk) else: raise ValueError("Unknown key '{}'!".format(kk)) # verify trace names if kk == "trace": for sk in data["trace"]: if sk not in dfn.FLUOR_TRACES: msg = "Unknown trace key: {}".format(sk) raise ValueError(msg) # Create file # (this should happen after all checks) if isinstance(path_or_h5file, h5py.File): h5obj = path_or_h5file else: if mode == "reset": h5mode = "w" else: h5mode = "a" h5obj = h5py.File(path_or_h5file, mode=h5mode) # update version # - if it is not already in the hdf5 file (prevent override) # - if it is explicitly given in meta (append to old version string) if ("setup:software version" not in h5obj.attrs or ("setup" in meta and "software version" in meta["setup"])): thisver = "dclab {}".format(version) if "setup" in meta and "software version" in meta["setup"]: oldver = meta["setup"]["software version"] thisver = "{} | {}".format(oldver, thisver) if "setup" not in meta: meta["setup"] = {} meta["setup"]["software version"] = thisver # Write meta for sec in meta: for ck in meta[sec]: idk = "{}:{}".format(sec, ck) conffunc = dfn.config_funcs[sec][ck] h5obj.attrs[idk] = conffunc(meta[sec][ck]) # Write data # create events group if "events" not in h5obj: h5obj.create_group("events") events = h5obj["events"] # remove previous data if mode == "replace": for rk in feat_keys: if rk in events: del events[rk] # store experimental data for fk in feat_keys: if fk in dfn.scalar_feature_names: store_scalar(h5group=events, name=fk, data=data[fk]) elif fk == "contour": store_contour(h5group=events, data=data["contour"], compression=compression) elif fk == "image": store_image(h5group=events, data=data["image"], compression=compression) elif fk == "mask": store_mask(h5group=events, data=data["mask"], compression=compression) elif fk == "trace": store_trace(h5group=events, data=data["trace"], compression=compression) # Write logs if "logs" not in h5obj: h5obj.create_group("logs") log_group = h5obj["logs"] # remove previous data if mode == "replace": for rl in logs: if rl in log_group: del log_group[rl] dt = h5py.special_dtype(vlen=hdf5_str) for lkey in logs: ldata = logs[lkey] if isinstance(ldata, (str, hdf5_str)): # single event ldata = [ldata] lnum = len(ldata) if lkey not in log_group: log_dset = log_group.create_dataset(lkey, (lnum,), dtype=dt, maxshape=(None,), chunks=True, fletcher32=True, compression=compression) for ii, line in enumerate(ldata): log_dset[ii] = line else: log_dset = log_group[lkey] oldsize = log_dset.shape[0] log_dset.resize(oldsize + lnum, axis=0) for ii, line in enumerate(ldata): log_dset[oldsize + ii] = line if mode == "append": return h5obj else: h5obj.close() return None
[ "def", "write", "(", "path_or_h5file", ",", "data", "=", "{", "}", ",", "meta", "=", "{", "}", ",", "logs", "=", "{", "}", ",", "mode", "=", "\"reset\"", ",", "compression", "=", "None", ")", ":", "if", "mode", "not", "in", "[", "\"append\"", ","...
Write data to an RT-DC file Parameters ---------- path: path or h5py.File The path or the hdf5 file object to write to. data: dict-like The data to store. Each key of `data` must be a valid feature name (see `dclab.dfn.feature_names`). The data type must be given according to the feature type: - scalar feature: 1d ndarray of size `N`, any dtype, with the number of events `N`. - contour: list of `N` 2d ndarrays of shape `(2,C)`, any dtype, with each ndarray containing the x- and y- coordinates of `C` contour points in pixels. - image: 3d ndarray of shape `(N,A,B)`, is converted to uint8, with the image dimensions `(x,y) = (A,B)` - mask: 3d ndarray of shape `(N,A,B)`, is converted to bool, with the image dimensions `(x,y) = (A,B)` - trace: 2d ndarray of shape `(N,T)`, any dtype with a globally constant trace length `T`. meta: dict of dicts The meta data to store (see `dclab.dfn.config_keys`). Each key depicts a meta data section name whose data is given as a dictionary, e.g. meta = {"imaging": {"exposure time": 20, "flash duration": 2, ... }, "setup": {"channel width": 20, "chip region": "channel", ... }, ... } Only section key names and key values therein registered in dclab are allowed and are converted to the pre-defined dtype. logs: dict of lists Each key of `logs` refers to a list of strings that contains logging information. Each item in the list can be considered to be one line in the log file. mode: str Defines how the input `data` and `logs` are stored: - "append": append new data to existing Datasets; the opened `h5py.File` object is returned (used in real- time data storage) - "replace": replace keys given by `data` and `logs`; the opened `h5py.File` object is closed and `None` is returned (used for ancillary feature storage) - "reset": do not keep any previous data; the opened `h5py.File` object is closed and `None` is returned (default) compression: str Compression method for "contour", "image", and "trace" data as well as logs; one of [None, "lzf", "gzip", "szip"]. Notes ----- If `data` is an instance of RTDCBase, then `meta` must be set to `data.config`, otherwise no meta data will be saved.
[ "Write", "data", "to", "an", "RT", "-", "DC", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/write_hdf5.py#L137-L342
openstax/cnx-archive
cnxarchive/search.py
_convert
def _convert(tup, dictlist): """ :param tup: a list of tuples :param di: a dictionary converted from tup :return: dictionary """ di = {} for a, b in tup: di.setdefault(a, []).append(b) for key, val in di.items(): dictlist.append((key, val)) return dictlist
python
def _convert(tup, dictlist): """ :param tup: a list of tuples :param di: a dictionary converted from tup :return: dictionary """ di = {} for a, b in tup: di.setdefault(a, []).append(b) for key, val in di.items(): dictlist.append((key, val)) return dictlist
[ "def", "_convert", "(", "tup", ",", "dictlist", ")", ":", "di", "=", "{", "}", "for", "a", ",", "b", "in", "tup", ":", "di", ".", "setdefault", "(", "a", ",", "[", "]", ")", ".", "append", "(", "b", ")", "for", "key", ",", "val", "in", "di"...
:param tup: a list of tuples :param di: a dictionary converted from tup :return: dictionary
[ ":", "param", "tup", ":", "a", "list", "of", "tuples", ":", "param", "di", ":", "a", "dictionary", "converted", "from", "tup", ":", "return", ":", "dictionary" ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L374-L385
openstax/cnx-archive
cnxarchive/search.py
_upper
def _upper(val_list): """ :param val_list: a list of strings :return: a list of upper-cased strings """ res = [] for ele in val_list: res.append(ele.upper()) return res
python
def _upper(val_list): """ :param val_list: a list of strings :return: a list of upper-cased strings """ res = [] for ele in val_list: res.append(ele.upper()) return res
[ "def", "_upper", "(", "val_list", ")", ":", "res", "=", "[", "]", "for", "ele", "in", "val_list", ":", "res", ".", "append", "(", "ele", ".", "upper", "(", ")", ")", "return", "res" ]
:param val_list: a list of strings :return: a list of upper-cased strings
[ ":", "param", "val_list", ":", "a", "list", "of", "strings", ":", "return", ":", "a", "list", "of", "upper", "-", "cased", "strings" ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L388-L396
openstax/cnx-archive
cnxarchive/search.py
_build_search
def _build_search(structured_query): """Construct search statment for db execution. Produces the search statement and argument dictionary to be executed by the DBAPI v2 execute method. For example, ``cursor.execute(*_build_search(query, weights))`` :param query: containing terms, filters, and sorts. :type query: Query :param weights: weight values to assign to each keyword search field :type weights: dictionary of field names to weight integers :returns: the build statement and the arguments used against it :rtype: a two value tuple of a SQL template and a dictionary of arguments to pass into that template """ arguments = {} # get text terms and filter out common words text_terms = [term for ttype, term in structured_query.terms if ttype == 'text'] text_terms_wo_stopwords = [term for term in text_terms if term.lower() not in STOPWORDS] # sql where clauses conditions = {'text_terms': '', 'pubYear': '', 'authorID': '', 'type': '', 'keyword': '', 'subject': '', 'language': '', 'title': '', 'author': '', 'abstract': ''} # if there are other search terms (not type "text") or if the text # terms do not only consist of stopwords, then use the text terms # without stopwords if arguments or text_terms_wo_stopwords: text_terms = text_terms_wo_stopwords arguments.update({'text_terms': ' '.join(text_terms)}) # raise Exception(structured_query.filters, structured_query.terms) if len(text_terms) > 0: conditions['text_terms'] = 'AND module_idx \ @@ plainto_tsquery(%(text_terms)s)' # build fulltext keys fulltext_key = [] for term in text_terms_wo_stopwords: fulltext_key.append(term + '-::-fulltext') arguments.update({'fulltext_key': ';--;'.join(fulltext_key)}) idx = 0 invalid_filters = [] filters = _convert(structured_query.filters, []) while idx < len(filters): keyword = filters[idx][0] value = filters[idx][1] if keyword == 'pubYear': conditions['pubYear'] = 'AND extract(year from cm.revised) = \ %(pubYear)s' arguments.update({'pubYear': value[0]}) elif keyword == 'authorID': conditions['authorID'] = 'AND ARRAY[%(authorID)s] \ <@ cm.authors' arguments.update({'authorID': value[0]}) elif keyword == 'type': value[0] = value[0].lower() conditions['type'] = 'AND cm.portal_type = %(type)s' if value[0] != 'book' and value[0] != 'collection' and \ value[0] != 'page' and value[0] != 'module': invalid_filters.append(idx) value[0] = 'Collection' if (value[0] == 'book' or value[0] == 'collection') else 'Module' arguments.update({'type': value[0]}) elif keyword == 'keyword': value = _upper(value) conditions['keyword'] = 'AND cm.module_ident = \ ANY(WITH target AS ( \ SELECT lm.module_ident AS id, \ array_agg(UPPER(kw.word)) AS akw \ FROM latest_modules lm, \ modulekeywords mk, \ keywords kw \ WHERE lm.module_ident = mk.module_ident \ AND kw.keywordid = mk.keywordid \ GROUP BY id) \ SELECT target.id FROM target WHERE \ target.akw @> %(keyword)s)' arguments.update({'keyword': value}) elif keyword == 'subject': conditions['subject'] = 'AND cm.module_ident = \ ANY(WITH sub AS ( \ SELECT module_ident AS id, \ array_agg(tag) AS atag \ FROM latest_modules \ NATURAL JOIN \ moduletags NATURAL JOIN \ tags GROUP BY id) \ SELECT sub.id FROM sub WHERE \ sub.atag @> %(subject)s)' arguments.update({'subject': value}) elif keyword == 'language': conditions['language'] = 'AND cm.language = %(language)s' arguments.update({'language': value[0]}) elif keyword == 'title': conditions['title'] = 'AND strip_html(cm.name) ~* \ %(title)s' arguments.update({'title': value[0]}) elif keyword == 'author': conditions['author'] = 'AND cm.module_ident = \ ANY(WITH name AS ( \ SELECT username FROM users u WHERE \ u.first_name||\' \'||u.last_name \ ~* %(author)s) \ SELECT lm.module_ident \ FROM latest_modules lm \ JOIN name n ON ARRAY[n.username] \ <@ lm.authors)' arguments.update({'author': value[0]}) elif keyword == 'abstract': conditions['abstract'] = 'AND cm.module_ident = \ ANY(SELECT lm.module_ident FROM \ latest_modules lm, \ abstracts ab WHERE\ lm.abstractid = ab.abstractid \ AND ab.abstract \ ~* %(abstract)s)' arguments.update({'abstract': value[0]}) else: # Invalid filter! invalid_filters.append(idx) idx += 1 if len(invalid_filters) == len(structured_query.filters) and \ len(structured_query.terms) == 0: # Either query terms are all invalid filters # or we received a null query. # Clear the filter list in this case. structured_query.filters = [] return None, None for invalid_filter_idx in invalid_filters: # Remove invalid filters. del structured_query.filters[invalid_filter_idx] # Add the arguments for sorting. sorts = ['portal_type'] if structured_query.sorts: for sort in structured_query.sorts: # These sort values are not the name of the column used # in the database. stmt = _transmute_sort(sort) sorts.append(stmt) sorts.extend(('weight DESC', 'uuid DESC',)) sorts = ', '.join(sorts) statement = SQL_QUICK_SELECT_WRAPPER.format(conditions['pubYear'], conditions['authorID'], conditions['type'], conditions['keyword'], conditions['subject'], conditions['text_terms'], conditions['language'], conditions['title'], conditions['author'], conditions['abstract'], sorts=sorts) return statement, arguments
python
def _build_search(structured_query): """Construct search statment for db execution. Produces the search statement and argument dictionary to be executed by the DBAPI v2 execute method. For example, ``cursor.execute(*_build_search(query, weights))`` :param query: containing terms, filters, and sorts. :type query: Query :param weights: weight values to assign to each keyword search field :type weights: dictionary of field names to weight integers :returns: the build statement and the arguments used against it :rtype: a two value tuple of a SQL template and a dictionary of arguments to pass into that template """ arguments = {} # get text terms and filter out common words text_terms = [term for ttype, term in structured_query.terms if ttype == 'text'] text_terms_wo_stopwords = [term for term in text_terms if term.lower() not in STOPWORDS] # sql where clauses conditions = {'text_terms': '', 'pubYear': '', 'authorID': '', 'type': '', 'keyword': '', 'subject': '', 'language': '', 'title': '', 'author': '', 'abstract': ''} # if there are other search terms (not type "text") or if the text # terms do not only consist of stopwords, then use the text terms # without stopwords if arguments or text_terms_wo_stopwords: text_terms = text_terms_wo_stopwords arguments.update({'text_terms': ' '.join(text_terms)}) # raise Exception(structured_query.filters, structured_query.terms) if len(text_terms) > 0: conditions['text_terms'] = 'AND module_idx \ @@ plainto_tsquery(%(text_terms)s)' # build fulltext keys fulltext_key = [] for term in text_terms_wo_stopwords: fulltext_key.append(term + '-::-fulltext') arguments.update({'fulltext_key': ';--;'.join(fulltext_key)}) idx = 0 invalid_filters = [] filters = _convert(structured_query.filters, []) while idx < len(filters): keyword = filters[idx][0] value = filters[idx][1] if keyword == 'pubYear': conditions['pubYear'] = 'AND extract(year from cm.revised) = \ %(pubYear)s' arguments.update({'pubYear': value[0]}) elif keyword == 'authorID': conditions['authorID'] = 'AND ARRAY[%(authorID)s] \ <@ cm.authors' arguments.update({'authorID': value[0]}) elif keyword == 'type': value[0] = value[0].lower() conditions['type'] = 'AND cm.portal_type = %(type)s' if value[0] != 'book' and value[0] != 'collection' and \ value[0] != 'page' and value[0] != 'module': invalid_filters.append(idx) value[0] = 'Collection' if (value[0] == 'book' or value[0] == 'collection') else 'Module' arguments.update({'type': value[0]}) elif keyword == 'keyword': value = _upper(value) conditions['keyword'] = 'AND cm.module_ident = \ ANY(WITH target AS ( \ SELECT lm.module_ident AS id, \ array_agg(UPPER(kw.word)) AS akw \ FROM latest_modules lm, \ modulekeywords mk, \ keywords kw \ WHERE lm.module_ident = mk.module_ident \ AND kw.keywordid = mk.keywordid \ GROUP BY id) \ SELECT target.id FROM target WHERE \ target.akw @> %(keyword)s)' arguments.update({'keyword': value}) elif keyword == 'subject': conditions['subject'] = 'AND cm.module_ident = \ ANY(WITH sub AS ( \ SELECT module_ident AS id, \ array_agg(tag) AS atag \ FROM latest_modules \ NATURAL JOIN \ moduletags NATURAL JOIN \ tags GROUP BY id) \ SELECT sub.id FROM sub WHERE \ sub.atag @> %(subject)s)' arguments.update({'subject': value}) elif keyword == 'language': conditions['language'] = 'AND cm.language = %(language)s' arguments.update({'language': value[0]}) elif keyword == 'title': conditions['title'] = 'AND strip_html(cm.name) ~* \ %(title)s' arguments.update({'title': value[0]}) elif keyword == 'author': conditions['author'] = 'AND cm.module_ident = \ ANY(WITH name AS ( \ SELECT username FROM users u WHERE \ u.first_name||\' \'||u.last_name \ ~* %(author)s) \ SELECT lm.module_ident \ FROM latest_modules lm \ JOIN name n ON ARRAY[n.username] \ <@ lm.authors)' arguments.update({'author': value[0]}) elif keyword == 'abstract': conditions['abstract'] = 'AND cm.module_ident = \ ANY(SELECT lm.module_ident FROM \ latest_modules lm, \ abstracts ab WHERE\ lm.abstractid = ab.abstractid \ AND ab.abstract \ ~* %(abstract)s)' arguments.update({'abstract': value[0]}) else: # Invalid filter! invalid_filters.append(idx) idx += 1 if len(invalid_filters) == len(structured_query.filters) and \ len(structured_query.terms) == 0: # Either query terms are all invalid filters # or we received a null query. # Clear the filter list in this case. structured_query.filters = [] return None, None for invalid_filter_idx in invalid_filters: # Remove invalid filters. del structured_query.filters[invalid_filter_idx] # Add the arguments for sorting. sorts = ['portal_type'] if structured_query.sorts: for sort in structured_query.sorts: # These sort values are not the name of the column used # in the database. stmt = _transmute_sort(sort) sorts.append(stmt) sorts.extend(('weight DESC', 'uuid DESC',)) sorts = ', '.join(sorts) statement = SQL_QUICK_SELECT_WRAPPER.format(conditions['pubYear'], conditions['authorID'], conditions['type'], conditions['keyword'], conditions['subject'], conditions['text_terms'], conditions['language'], conditions['title'], conditions['author'], conditions['abstract'], sorts=sorts) return statement, arguments
[ "def", "_build_search", "(", "structured_query", ")", ":", "arguments", "=", "{", "}", "# get text terms and filter out common words", "text_terms", "=", "[", "term", "for", "ttype", ",", "term", "in", "structured_query", ".", "terms", "if", "ttype", "==", "'text'...
Construct search statment for db execution. Produces the search statement and argument dictionary to be executed by the DBAPI v2 execute method. For example, ``cursor.execute(*_build_search(query, weights))`` :param query: containing terms, filters, and sorts. :type query: Query :param weights: weight values to assign to each keyword search field :type weights: dictionary of field names to weight integers :returns: the build statement and the arguments used against it :rtype: a two value tuple of a SQL template and a dictionary of arguments to pass into that template
[ "Construct", "search", "statment", "for", "db", "execution", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L403-L567
openstax/cnx-archive
cnxarchive/search.py
search
def search(query, query_type=DEFAULT_QUERY_TYPE): """Search database using parsed query. Executes a database search query from the given ``query`` (a ``Query`` object) and optionally accepts a list of search weights. By default, the search results are ordered by weight. :param query: containing terms, filters, and sorts. :type query: Query :returns: a sequence of records that match the query conditions :rtype: QueryResults (which is a sequence of QueryRecord objects) """ # Build the SQL statement. statement, arguments = _build_search(query) # Execute the SQL. if statement is None and arguments is None: return QueryResults([], [], 'AND') with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(statement, arguments) search_results = cursor.fetchall() # Wrap the SQL results. return QueryResults(search_results, query, query_type)
python
def search(query, query_type=DEFAULT_QUERY_TYPE): """Search database using parsed query. Executes a database search query from the given ``query`` (a ``Query`` object) and optionally accepts a list of search weights. By default, the search results are ordered by weight. :param query: containing terms, filters, and sorts. :type query: Query :returns: a sequence of records that match the query conditions :rtype: QueryResults (which is a sequence of QueryRecord objects) """ # Build the SQL statement. statement, arguments = _build_search(query) # Execute the SQL. if statement is None and arguments is None: return QueryResults([], [], 'AND') with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(statement, arguments) search_results = cursor.fetchall() # Wrap the SQL results. return QueryResults(search_results, query, query_type)
[ "def", "search", "(", "query", ",", "query_type", "=", "DEFAULT_QUERY_TYPE", ")", ":", "# Build the SQL statement.", "statement", ",", "arguments", "=", "_build_search", "(", "query", ")", "# Execute the SQL.", "if", "statement", "is", "None", "and", "arguments", ...
Search database using parsed query. Executes a database search query from the given ``query`` (a ``Query`` object) and optionally accepts a list of search weights. By default, the search results are ordered by weight. :param query: containing terms, filters, and sorts. :type query: Query :returns: a sequence of records that match the query conditions :rtype: QueryResults (which is a sequence of QueryRecord objects)
[ "Search", "database", "using", "parsed", "query", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L570-L594
openstax/cnx-archive
cnxarchive/search.py
Query.fix_quotes
def fix_quotes(cls, query_string): """Heuristic attempt to fix unbalanced quotes in query_string.""" if query_string.count('"') % 2 == 0: # no unbalanced quotes to fix return query_string fields = [] # contains what's matched by the regexp # e.g. fields = ['sort:pubDate', 'author:"first last"'] def f(match): fields.append(match.string[match.start():match.end()]) return '' # terms will be all the search terms that don't have a field terms = re.sub(r'[^\s:]*:("[^"]*"|[^\s]*)', f, query_string) query_string = '{}" {}'.format(terms.strip(), ' '.join(fields)) return query_string
python
def fix_quotes(cls, query_string): """Heuristic attempt to fix unbalanced quotes in query_string.""" if query_string.count('"') % 2 == 0: # no unbalanced quotes to fix return query_string fields = [] # contains what's matched by the regexp # e.g. fields = ['sort:pubDate', 'author:"first last"'] def f(match): fields.append(match.string[match.start():match.end()]) return '' # terms will be all the search terms that don't have a field terms = re.sub(r'[^\s:]*:("[^"]*"|[^\s]*)', f, query_string) query_string = '{}" {}'.format(terms.strip(), ' '.join(fields)) return query_string
[ "def", "fix_quotes", "(", "cls", ",", "query_string", ")", ":", "if", "query_string", ".", "count", "(", "'\"'", ")", "%", "2", "==", "0", ":", "# no unbalanced quotes to fix", "return", "query_string", "fields", "=", "[", "]", "# contains what's matched by the ...
Heuristic attempt to fix unbalanced quotes in query_string.
[ "Heuristic", "attempt", "to", "fix", "unbalanced", "quotes", "in", "query_string", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L106-L122
openstax/cnx-archive
cnxarchive/search.py
Query.from_raw_query
def from_raw_query(cls, query_string): """Parse raw string to query. Given a raw string (typically typed by the user), parse to a structured format and initialize the class. """ try: node_tree = grammar.parse(query_string) except IncompleteParseError: query_string = cls.fix_quotes(query_string) node_tree = grammar.parse(query_string) structured_query = DictFormater().visit(node_tree) return cls([t for t in structured_query if t[1].lower() not in STOPWORDS])
python
def from_raw_query(cls, query_string): """Parse raw string to query. Given a raw string (typically typed by the user), parse to a structured format and initialize the class. """ try: node_tree = grammar.parse(query_string) except IncompleteParseError: query_string = cls.fix_quotes(query_string) node_tree = grammar.parse(query_string) structured_query = DictFormater().visit(node_tree) return cls([t for t in structured_query if t[1].lower() not in STOPWORDS])
[ "def", "from_raw_query", "(", "cls", ",", "query_string", ")", ":", "try", ":", "node_tree", "=", "grammar", ".", "parse", "(", "query_string", ")", "except", "IncompleteParseError", ":", "query_string", "=", "cls", ".", "fix_quotes", "(", "query_string", ")",...
Parse raw string to query. Given a raw string (typically typed by the user), parse to a structured format and initialize the class.
[ "Parse", "raw", "string", "to", "query", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L125-L140
openstax/cnx-archive
cnxarchive/search.py
QueryRecord.highlighted_abstract
def highlighted_abstract(self): """Highlight the found terms in the abstract text.""" abstract_terms = self.fields.get('abstract', []) if abstract_terms: sql = _read_sql_file('highlighted-abstract') else: sql = _read_sql_file('get-abstract') arguments = {'id': self['id'], 'query': ' & '.join(abstract_terms), } with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(sql, arguments) hl_abstract = cursor.fetchone() if hl_abstract: return hl_abstract[0]
python
def highlighted_abstract(self): """Highlight the found terms in the abstract text.""" abstract_terms = self.fields.get('abstract', []) if abstract_terms: sql = _read_sql_file('highlighted-abstract') else: sql = _read_sql_file('get-abstract') arguments = {'id': self['id'], 'query': ' & '.join(abstract_terms), } with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(sql, arguments) hl_abstract = cursor.fetchone() if hl_abstract: return hl_abstract[0]
[ "def", "highlighted_abstract", "(", "self", ")", ":", "abstract_terms", "=", "self", ".", "fields", ".", "get", "(", "'abstract'", ",", "[", "]", ")", "if", "abstract_terms", ":", "sql", "=", "_read_sql_file", "(", "'highlighted-abstract'", ")", "else", ":",...
Highlight the found terms in the abstract text.
[ "Highlight", "the", "found", "terms", "in", "the", "abstract", "text", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L177-L192
openstax/cnx-archive
cnxarchive/search.py
QueryRecord.highlighted_fulltext
def highlighted_fulltext(self): """Highlight the found terms in the fulltext.""" terms = self.fields.get('fulltext', []) if not terms: return None arguments = {'id': self['id'], 'query': ' & '.join(terms), } with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(_read_sql_file('highlighted-fulltext'), arguments) hl_fulltext = cursor.fetchone()[0] return hl_fulltext
python
def highlighted_fulltext(self): """Highlight the found terms in the fulltext.""" terms = self.fields.get('fulltext', []) if not terms: return None arguments = {'id': self['id'], 'query': ' & '.join(terms), } with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(_read_sql_file('highlighted-fulltext'), arguments) hl_fulltext = cursor.fetchone()[0] return hl_fulltext
[ "def", "highlighted_fulltext", "(", "self", ")", ":", "terms", "=", "self", ".", "fields", ".", "get", "(", "'fulltext'", ",", "[", "]", ")", "if", "not", "terms", ":", "return", "None", "arguments", "=", "{", "'id'", ":", "self", "[", "'id'", "]", ...
Highlight the found terms in the fulltext.
[ "Highlight", "the", "found", "terms", "in", "the", "fulltext", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/search.py#L195-L208
ZELLMECHANIK-DRESDEN/dclab
dclab/external/statsmodels/nonparametric/kernel_density.py
KDEMultivariate.pdf
def pdf(self, data_predict=None): r""" Evaluate the probability density function. Parameters ---------- data_predict: array_like, optional Points to evaluate at. If unspecified, the training data is used. Returns ------- pdf_est: array_like Probability density function evaluated at `data_predict`. Notes ----- The probability density is given by the generalized product kernel estimator: .. math:: K_{h}(X_{i},X_{j}) = \prod_{s=1}^{q}h_{s}^{-1}k\left(\frac{X_{is}-X_{js}}{h_{s}}\right) """ if data_predict is None: data_predict = self.data else: data_predict = _adjust_shape(data_predict, self.k_vars) pdf_est = [] for i in range(np.shape(data_predict)[0]): pdf_est.append(gpke(self.bw, data=self.data, data_predict=data_predict[i, :], var_type=self.var_type) / self.nobs) pdf_est = np.squeeze(pdf_est) return pdf_est
python
def pdf(self, data_predict=None): r""" Evaluate the probability density function. Parameters ---------- data_predict: array_like, optional Points to evaluate at. If unspecified, the training data is used. Returns ------- pdf_est: array_like Probability density function evaluated at `data_predict`. Notes ----- The probability density is given by the generalized product kernel estimator: .. math:: K_{h}(X_{i},X_{j}) = \prod_{s=1}^{q}h_{s}^{-1}k\left(\frac{X_{is}-X_{js}}{h_{s}}\right) """ if data_predict is None: data_predict = self.data else: data_predict = _adjust_shape(data_predict, self.k_vars) pdf_est = [] for i in range(np.shape(data_predict)[0]): pdf_est.append(gpke(self.bw, data=self.data, data_predict=data_predict[i, :], var_type=self.var_type) / self.nobs) pdf_est = np.squeeze(pdf_est) return pdf_est
[ "def", "pdf", "(", "self", ",", "data_predict", "=", "None", ")", ":", "if", "data_predict", "is", "None", ":", "data_predict", "=", "self", ".", "data", "else", ":", "data_predict", "=", "_adjust_shape", "(", "data_predict", ",", "self", ".", "k_vars", ...
r""" Evaluate the probability density function. Parameters ---------- data_predict: array_like, optional Points to evaluate at. If unspecified, the training data is used. Returns ------- pdf_est: array_like Probability density function evaluated at `data_predict`. Notes ----- The probability density is given by the generalized product kernel estimator: .. math:: K_{h}(X_{i},X_{j}) = \prod_{s=1}^{q}h_{s}^{-1}k\left(\frac{X_{is}-X_{js}}{h_{s}}\right)
[ "r", "Evaluate", "the", "probability", "density", "function", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/external/statsmodels/nonparametric/kernel_density.py#L126-L160
xenon-middleware/pyxenon
xenon/compat.py
find_xenon_grpc_jar
def find_xenon_grpc_jar(): """Find the Xenon-GRPC jar-file, windows version.""" prefix = Path(sys.prefix) locations = [ prefix / 'lib', prefix / 'local' / 'lib' ] for location in locations: jar_file = location / 'xenon-grpc-{}-all.jar'.format( xenon_grpc_version) if not jar_file.exists(): continue else: return str(jar_file) return None
python
def find_xenon_grpc_jar(): """Find the Xenon-GRPC jar-file, windows version.""" prefix = Path(sys.prefix) locations = [ prefix / 'lib', prefix / 'local' / 'lib' ] for location in locations: jar_file = location / 'xenon-grpc-{}-all.jar'.format( xenon_grpc_version) if not jar_file.exists(): continue else: return str(jar_file) return None
[ "def", "find_xenon_grpc_jar", "(", ")", ":", "prefix", "=", "Path", "(", "sys", ".", "prefix", ")", "locations", "=", "[", "prefix", "/", "'lib'", ",", "prefix", "/", "'local'", "/", "'lib'", "]", "for", "location", "in", "locations", ":", "jar_file", ...
Find the Xenon-GRPC jar-file, windows version.
[ "Find", "the", "Xenon", "-", "GRPC", "jar", "-", "file", "windows", "version", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/compat.py#L16-L34
xenon-middleware/pyxenon
xenon/compat.py
kill_process
def kill_process(process): """Kill the process group associated with the given process. (posix)""" logger = logging.getLogger('xenon') logger.info('Terminating Xenon-GRPC server.') os.kill(process.pid, signal.SIGINT) process.wait()
python
def kill_process(process): """Kill the process group associated with the given process. (posix)""" logger = logging.getLogger('xenon') logger.info('Terminating Xenon-GRPC server.') os.kill(process.pid, signal.SIGINT) process.wait()
[ "def", "kill_process", "(", "process", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "'xenon'", ")", "logger", ".", "info", "(", "'Terminating Xenon-GRPC server.'", ")", "os", ".", "kill", "(", "process", ".", "pid", ",", "signal", ".", "SIGIN...
Kill the process group associated with the given process. (posix)
[ "Kill", "the", "process", "group", "associated", "with", "the", "given", "process", ".", "(", "posix", ")" ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/compat.py#L37-L42
xenon-middleware/pyxenon
xenon/compat.py
start_xenon_server
def start_xenon_server(port=50051, disable_tls=False): """Start the server.""" jar_file = find_xenon_grpc_jar() if not jar_file: raise RuntimeError("Could not find 'xenon-grpc' jar file.") cmd = ['java', '-jar', jar_file, '-p', str(port)] if not disable_tls: crt_file, key_file = create_self_signed_cert() cmd.extend([ '--server-cert-chain', str(crt_file), '--server-private-key', str(key_file), '--client-cert-chain', str(crt_file)]) else: crt_file = key_file = None process = subprocess.Popen( cmd, bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return process, crt_file, key_file
python
def start_xenon_server(port=50051, disable_tls=False): """Start the server.""" jar_file = find_xenon_grpc_jar() if not jar_file: raise RuntimeError("Could not find 'xenon-grpc' jar file.") cmd = ['java', '-jar', jar_file, '-p', str(port)] if not disable_tls: crt_file, key_file = create_self_signed_cert() cmd.extend([ '--server-cert-chain', str(crt_file), '--server-private-key', str(key_file), '--client-cert-chain', str(crt_file)]) else: crt_file = key_file = None process = subprocess.Popen( cmd, bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return process, crt_file, key_file
[ "def", "start_xenon_server", "(", "port", "=", "50051", ",", "disable_tls", "=", "False", ")", ":", "jar_file", "=", "find_xenon_grpc_jar", "(", ")", "if", "not", "jar_file", ":", "raise", "RuntimeError", "(", "\"Could not find 'xenon-grpc' jar file.\"", ")", "cmd...
Start the server.
[ "Start", "the", "server", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/compat.py#L45-L69
simse/pymitv
pymitv/control.py
Control.send_keystrokes
def send_keystrokes(ip, keystrokes, wait=False): """Connects to TV and sends keystroke via HTTP.""" tv_url = 'http://{}:6095/controller?action=keyevent&keycode='.format(ip) for keystroke in keystrokes: if keystroke == 'wait' or wait is True: time.sleep(0.7) else: request = requests.get(tv_url + keystroke) if request.status_code != 200: return False return True
python
def send_keystrokes(ip, keystrokes, wait=False): """Connects to TV and sends keystroke via HTTP.""" tv_url = 'http://{}:6095/controller?action=keyevent&keycode='.format(ip) for keystroke in keystrokes: if keystroke == 'wait' or wait is True: time.sleep(0.7) else: request = requests.get(tv_url + keystroke) if request.status_code != 200: return False return True
[ "def", "send_keystrokes", "(", "ip", ",", "keystrokes", ",", "wait", "=", "False", ")", ":", "tv_url", "=", "'http://{}:6095/controller?action=keyevent&keycode='", ".", "format", "(", "ip", ")", "for", "keystroke", "in", "keystrokes", ":", "if", "keystroke", "==...
Connects to TV and sends keystroke via HTTP.
[ "Connects", "to", "TV", "and", "sends", "keystroke", "via", "HTTP", "." ]
train
https://github.com/simse/pymitv/blob/03213f591d70fbf90ba2b6af372e474c9bfb99f6/pymitv/control.py#L29-L43
simse/pymitv
pymitv/control.py
Control.mute
def mute(ip): """Polyfill for muting the TV.""" tv_url = 'http://{}:6095/controller?action=keyevent&keycode='.format(ip) count = 0 while count > 30: count = count + 1 request = requests.get(tv_url + 'volumedown') if request.status_code != 200: return False return True
python
def mute(ip): """Polyfill for muting the TV.""" tv_url = 'http://{}:6095/controller?action=keyevent&keycode='.format(ip) count = 0 while count > 30: count = count + 1 request = requests.get(tv_url + 'volumedown') if request.status_code != 200: return False return True
[ "def", "mute", "(", "ip", ")", ":", "tv_url", "=", "'http://{}:6095/controller?action=keyevent&keycode='", ".", "format", "(", "ip", ")", "count", "=", "0", "while", "count", ">", "30", ":", "count", "=", "count", "+", "1", "request", "=", "requests", ".",...
Polyfill for muting the TV.
[ "Polyfill", "for", "muting", "the", "TV", "." ]
train
https://github.com/simse/pymitv/blob/03213f591d70fbf90ba2b6af372e474c9bfb99f6/pymitv/control.py#L46-L58
openstax/cnx-archive
cnxarchive/views/in_book_search.py
in_book_search
def in_book_search(request): """Full text, in-book search.""" results = {} args = request.matchdict ident_hash = args['ident_hash'] args['search_term'] = request.params.get('q', '') query_type = request.params.get('query_type', '') combiner = '' if query_type: if query_type.lower() == 'or': combiner = '_or' id, version = split_ident_hash(ident_hash) args['uuid'] = id args['version'] = version with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(SQL['get-collated-state'], args) res = cursor.fetchall() if res and res[0][0]: statement = SQL['get-in-collated-book-search'] else: statement = SQL['get-in-book-search'] cursor.execute(statement.format(combiner=combiner), args) res = cursor.fetchall() results['results'] = {'query': [], 'total': len(res), 'items': []} results['results']['query'] = { 'id': ident_hash, 'search_term': args['search_term'], } for uuid, version, title, snippet, matches, rank in res: results['results']['items'].append({ 'rank': '{}'.format(rank), 'id': '{}@{}'.format(uuid, version), 'title': '{}'.format(title), 'snippet': '{}'.format(snippet), 'matches': '{}'.format(matches), }) resp = request.response resp.status = '200 OK' resp.content_type = 'application/json' resp.body = json.dumps(results) return resp
python
def in_book_search(request): """Full text, in-book search.""" results = {} args = request.matchdict ident_hash = args['ident_hash'] args['search_term'] = request.params.get('q', '') query_type = request.params.get('query_type', '') combiner = '' if query_type: if query_type.lower() == 'or': combiner = '_or' id, version = split_ident_hash(ident_hash) args['uuid'] = id args['version'] = version with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(SQL['get-collated-state'], args) res = cursor.fetchall() if res and res[0][0]: statement = SQL['get-in-collated-book-search'] else: statement = SQL['get-in-book-search'] cursor.execute(statement.format(combiner=combiner), args) res = cursor.fetchall() results['results'] = {'query': [], 'total': len(res), 'items': []} results['results']['query'] = { 'id': ident_hash, 'search_term': args['search_term'], } for uuid, version, title, snippet, matches, rank in res: results['results']['items'].append({ 'rank': '{}'.format(rank), 'id': '{}@{}'.format(uuid, version), 'title': '{}'.format(title), 'snippet': '{}'.format(snippet), 'matches': '{}'.format(matches), }) resp = request.response resp.status = '200 OK' resp.content_type = 'application/json' resp.body = json.dumps(results) return resp
[ "def", "in_book_search", "(", "request", ")", ":", "results", "=", "{", "}", "args", "=", "request", ".", "matchdict", "ident_hash", "=", "args", "[", "'ident_hash'", "]", "args", "[", "'search_term'", "]", "=", "request", ".", "params", ".", "get", "(",...
Full text, in-book search.
[ "Full", "text", "in", "-", "book", "search", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/in_book_search.py#L34-L84
openstax/cnx-archive
cnxarchive/views/in_book_search.py
in_book_search_highlighted_results
def in_book_search_highlighted_results(request): """In-book search - returns a highlighted version of the HTML.""" results = {} args = request.matchdict ident_hash = args['ident_hash'] page_ident_hash = args['page_ident_hash'] try: page_uuid, _ = split_ident_hash(page_ident_hash) except IdentHashShortId as e: page_uuid = get_uuid(e.id) except IdentHashMissingVersion as e: page_uuid = e.id args['page_uuid'] = page_uuid args['search_term'] = request.params.get('q', '') query_type = request.params.get('query_type', '') combiner = '' if query_type: if query_type.lower() == 'or': combiner = '_or' # Get version from URL params id, version = split_ident_hash(ident_hash) args['uuid'] = id args['version'] = version with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(SQL['get-collated-state'], args) res = cursor.fetchall() if res and res[0][0]: statement = SQL['get-in-collated-book-search-full-page'] else: statement = SQL['get-in-book-search-full-page'] cursor.execute(statement.format(combiner=combiner), args) res = cursor.fetchall() results['results'] = {'query': [], 'total': len(res), 'items': []} results['results']['query'] = { 'search_term': args['search_term'], 'collection_id': ident_hash, } for uuid, version, title, headline, rank in res: results['results']['items'].append({ 'rank': '{}'.format(rank), 'id': '{}'.format(page_ident_hash), 'title': '{}'.format(title), 'html': '{}'.format(headline), }) resp = request.response resp.status = '200 OK' resp.content_type = 'application/json' resp.body = json.dumps(results) return resp
python
def in_book_search_highlighted_results(request): """In-book search - returns a highlighted version of the HTML.""" results = {} args = request.matchdict ident_hash = args['ident_hash'] page_ident_hash = args['page_ident_hash'] try: page_uuid, _ = split_ident_hash(page_ident_hash) except IdentHashShortId as e: page_uuid = get_uuid(e.id) except IdentHashMissingVersion as e: page_uuid = e.id args['page_uuid'] = page_uuid args['search_term'] = request.params.get('q', '') query_type = request.params.get('query_type', '') combiner = '' if query_type: if query_type.lower() == 'or': combiner = '_or' # Get version from URL params id, version = split_ident_hash(ident_hash) args['uuid'] = id args['version'] = version with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(SQL['get-collated-state'], args) res = cursor.fetchall() if res and res[0][0]: statement = SQL['get-in-collated-book-search-full-page'] else: statement = SQL['get-in-book-search-full-page'] cursor.execute(statement.format(combiner=combiner), args) res = cursor.fetchall() results['results'] = {'query': [], 'total': len(res), 'items': []} results['results']['query'] = { 'search_term': args['search_term'], 'collection_id': ident_hash, } for uuid, version, title, headline, rank in res: results['results']['items'].append({ 'rank': '{}'.format(rank), 'id': '{}'.format(page_ident_hash), 'title': '{}'.format(title), 'html': '{}'.format(headline), }) resp = request.response resp.status = '200 OK' resp.content_type = 'application/json' resp.body = json.dumps(results) return resp
[ "def", "in_book_search_highlighted_results", "(", "request", ")", ":", "results", "=", "{", "}", "args", "=", "request", ".", "matchdict", "ident_hash", "=", "args", "[", "'ident_hash'", "]", "page_ident_hash", "=", "args", "[", "'page_ident_hash'", "]", "try", ...
In-book search - returns a highlighted version of the HTML.
[ "In", "-", "book", "search", "-", "returns", "a", "highlighted", "version", "of", "the", "HTML", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/in_book_search.py#L89-L149
ZELLMECHANIK-DRESDEN/dclab
dclab/external/statsmodels/nonparametric/_kernel_base.py
_adjust_shape
def _adjust_shape(dat, k_vars): """ Returns an array of shape (nobs, k_vars) for use with `gpke`.""" dat = np.asarray(dat) if dat.ndim > 2: dat = np.squeeze(dat) if dat.ndim == 1 and k_vars > 1: # one obs many vars nobs = 1 elif dat.ndim == 1 and k_vars == 1: # one obs one var nobs = len(dat) else: if np.shape(dat)[0] == k_vars and np.shape(dat)[1] != k_vars: dat = dat.T nobs = np.shape(dat)[0] # ndim >1 so many obs many vars dat = np.reshape(dat, (nobs, k_vars)) return dat
python
def _adjust_shape(dat, k_vars): """ Returns an array of shape (nobs, k_vars) for use with `gpke`.""" dat = np.asarray(dat) if dat.ndim > 2: dat = np.squeeze(dat) if dat.ndim == 1 and k_vars > 1: # one obs many vars nobs = 1 elif dat.ndim == 1 and k_vars == 1: # one obs one var nobs = len(dat) else: if np.shape(dat)[0] == k_vars and np.shape(dat)[1] != k_vars: dat = dat.T nobs = np.shape(dat)[0] # ndim >1 so many obs many vars dat = np.reshape(dat, (nobs, k_vars)) return dat
[ "def", "_adjust_shape", "(", "dat", ",", "k_vars", ")", ":", "dat", "=", "np", ".", "asarray", "(", "dat", ")", "if", "dat", ".", "ndim", ">", "2", ":", "dat", "=", "np", ".", "squeeze", "(", "dat", ")", "if", "dat", ".", "ndim", "==", "1", "...
Returns an array of shape (nobs, k_vars) for use with `gpke`.
[ "Returns", "an", "array", "of", "shape", "(", "nobs", "k_vars", ")", "for", "use", "with", "gpke", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/external/statsmodels/nonparametric/_kernel_base.py#L129-L145
ZELLMECHANIK-DRESDEN/dclab
dclab/external/statsmodels/nonparametric/_kernel_base.py
gpke
def gpke(bw, data, data_predict, var_type, ckertype='gaussian', okertype='wangryzin', ukertype='aitchisonaitken', tosum=True): r""" Returns the non-normalized Generalized Product Kernel Estimator Parameters ---------- bw: 1-D ndarray The user-specified bandwidth parameters. data: 1D or 2-D ndarray The training data. data_predict: 1-D ndarray The evaluation points at which the kernel estimation is performed. var_type: str, optional The variable type (continuous, ordered, unordered). ckertype: str, optional The kernel used for the continuous variables. okertype: str, optional The kernel used for the ordered discrete variables. ukertype: str, optional The kernel used for the unordered discrete variables. tosum : bool, optional Whether or not to sum the calculated array of densities. Default is True. Returns ------- dens: array-like The generalized product kernel density estimator. Notes ----- The formula for the multivariate kernel estimator for the pdf is: .. math:: f(x)=\frac{1}{nh_{1}...h_{q}}\sum_{i=1}^ {n}K\left(\frac{X_{i}-x}{h}\right) where .. math:: K\left(\frac{X_{i}-x}{h}\right) = k\left( \frac{X_{i1}-x_{1}}{h_{1}}\right)\times k\left( \frac{X_{i2}-x_{2}}{h_{2}}\right)\times...\times k\left(\frac{X_{iq}-x_{q}}{h_{q}}\right) """ kertypes = dict(c=ckertype, o=okertype, u=ukertype) Kval = np.empty(data.shape) for ii, vtype in enumerate(var_type): func = kernel_func[kertypes[vtype]] Kval[:, ii] = func(bw[ii], data[:, ii], data_predict[ii]) iscontinuous = np.array([c == 'c' for c in var_type]) dens = Kval.prod(axis=1) / np.prod(bw[iscontinuous]) if tosum: return dens.sum(axis=0) else: return dens
python
def gpke(bw, data, data_predict, var_type, ckertype='gaussian', okertype='wangryzin', ukertype='aitchisonaitken', tosum=True): r""" Returns the non-normalized Generalized Product Kernel Estimator Parameters ---------- bw: 1-D ndarray The user-specified bandwidth parameters. data: 1D or 2-D ndarray The training data. data_predict: 1-D ndarray The evaluation points at which the kernel estimation is performed. var_type: str, optional The variable type (continuous, ordered, unordered). ckertype: str, optional The kernel used for the continuous variables. okertype: str, optional The kernel used for the ordered discrete variables. ukertype: str, optional The kernel used for the unordered discrete variables. tosum : bool, optional Whether or not to sum the calculated array of densities. Default is True. Returns ------- dens: array-like The generalized product kernel density estimator. Notes ----- The formula for the multivariate kernel estimator for the pdf is: .. math:: f(x)=\frac{1}{nh_{1}...h_{q}}\sum_{i=1}^ {n}K\left(\frac{X_{i}-x}{h}\right) where .. math:: K\left(\frac{X_{i}-x}{h}\right) = k\left( \frac{X_{i1}-x_{1}}{h_{1}}\right)\times k\left( \frac{X_{i2}-x_{2}}{h_{2}}\right)\times...\times k\left(\frac{X_{iq}-x_{q}}{h_{q}}\right) """ kertypes = dict(c=ckertype, o=okertype, u=ukertype) Kval = np.empty(data.shape) for ii, vtype in enumerate(var_type): func = kernel_func[kertypes[vtype]] Kval[:, ii] = func(bw[ii], data[:, ii], data_predict[ii]) iscontinuous = np.array([c == 'c' for c in var_type]) dens = Kval.prod(axis=1) / np.prod(bw[iscontinuous]) if tosum: return dens.sum(axis=0) else: return dens
[ "def", "gpke", "(", "bw", ",", "data", ",", "data_predict", ",", "var_type", ",", "ckertype", "=", "'gaussian'", ",", "okertype", "=", "'wangryzin'", ",", "ukertype", "=", "'aitchisonaitken'", ",", "tosum", "=", "True", ")", ":", "kertypes", "=", "dict", ...
r""" Returns the non-normalized Generalized Product Kernel Estimator Parameters ---------- bw: 1-D ndarray The user-specified bandwidth parameters. data: 1D or 2-D ndarray The training data. data_predict: 1-D ndarray The evaluation points at which the kernel estimation is performed. var_type: str, optional The variable type (continuous, ordered, unordered). ckertype: str, optional The kernel used for the continuous variables. okertype: str, optional The kernel used for the ordered discrete variables. ukertype: str, optional The kernel used for the unordered discrete variables. tosum : bool, optional Whether or not to sum the calculated array of densities. Default is True. Returns ------- dens: array-like The generalized product kernel density estimator. Notes ----- The formula for the multivariate kernel estimator for the pdf is: .. math:: f(x)=\frac{1}{nh_{1}...h_{q}}\sum_{i=1}^ {n}K\left(\frac{X_{i}-x}{h}\right) where .. math:: K\left(\frac{X_{i}-x}{h}\right) = k\left( \frac{X_{i1}-x_{1}}{h_{1}}\right)\times k\left( \frac{X_{i2}-x_{2}}{h_{2}}\right)\times...\times k\left(\frac{X_{iq}-x_{q}}{h_{q}}\right)
[ "r", "Returns", "the", "non", "-", "normalized", "Generalized", "Product", "Kernel", "Estimator" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/external/statsmodels/nonparametric/_kernel_base.py#L148-L204
ZELLMECHANIK-DRESDEN/dclab
dclab/external/statsmodels/nonparametric/_kernel_base.py
GenericKDE._compute_bw
def _compute_bw(self, bw): """ Computes the bandwidth of the data. Parameters ---------- bw: array_like or str If array_like: user-specified bandwidth. If a string, should be one of: - cv_ml: cross validation maximum likelihood - normal_reference: normal reference rule of thumb - cv_ls: cross validation least squares Notes ----- The default values for bw is 'normal_reference'. """ if bw is None: bw = 'normal_reference' if not isinstance(bw, string_types): self._bw_method = "user-specified" res = np.asarray(bw) else: # The user specified a bandwidth selection method self._bw_method = bw # Workaround to avoid instance methods in __dict__ if bw == 'normal_reference': bwfunc = self._normal_reference elif bw == 'cv_ml': bwfunc = self._cv_ml else: # bw == 'cv_ls' bwfunc = self._cv_ls res = bwfunc() return res
python
def _compute_bw(self, bw): """ Computes the bandwidth of the data. Parameters ---------- bw: array_like or str If array_like: user-specified bandwidth. If a string, should be one of: - cv_ml: cross validation maximum likelihood - normal_reference: normal reference rule of thumb - cv_ls: cross validation least squares Notes ----- The default values for bw is 'normal_reference'. """ if bw is None: bw = 'normal_reference' if not isinstance(bw, string_types): self._bw_method = "user-specified" res = np.asarray(bw) else: # The user specified a bandwidth selection method self._bw_method = bw # Workaround to avoid instance methods in __dict__ if bw == 'normal_reference': bwfunc = self._normal_reference elif bw == 'cv_ml': bwfunc = self._cv_ml else: # bw == 'cv_ls' bwfunc = self._cv_ls res = bwfunc() return res
[ "def", "_compute_bw", "(", "self", ",", "bw", ")", ":", "if", "bw", "is", "None", ":", "bw", "=", "'normal_reference'", "if", "not", "isinstance", "(", "bw", ",", "string_types", ")", ":", "self", ".", "_bw_method", "=", "\"user-specified\"", "res", "=",...
Computes the bandwidth of the data. Parameters ---------- bw: array_like or str If array_like: user-specified bandwidth. If a string, should be one of: - cv_ml: cross validation maximum likelihood - normal_reference: normal reference rule of thumb - cv_ls: cross validation least squares Notes ----- The default values for bw is 'normal_reference'.
[ "Computes", "the", "bandwidth", "of", "the", "data", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/external/statsmodels/nonparametric/_kernel_base.py#L20-L56
ZELLMECHANIK-DRESDEN/dclab
dclab/external/statsmodels/nonparametric/_kernel_base.py
GenericKDE._set_defaults
def _set_defaults(self, defaults): """Sets the default values for the efficient estimation""" self.n_res = defaults.n_res self.n_sub = defaults.n_sub self.randomize = defaults.randomize self.return_median = defaults.return_median self.efficient = defaults.efficient self.return_only_bw = defaults.return_only_bw self.n_jobs = defaults.n_jobs
python
def _set_defaults(self, defaults): """Sets the default values for the efficient estimation""" self.n_res = defaults.n_res self.n_sub = defaults.n_sub self.randomize = defaults.randomize self.return_median = defaults.return_median self.efficient = defaults.efficient self.return_only_bw = defaults.return_only_bw self.n_jobs = defaults.n_jobs
[ "def", "_set_defaults", "(", "self", ",", "defaults", ")", ":", "self", ".", "n_res", "=", "defaults", ".", "n_res", "self", ".", "n_sub", "=", "defaults", ".", "n_sub", "self", ".", "randomize", "=", "defaults", ".", "randomize", "self", ".", "return_me...
Sets the default values for the efficient estimation
[ "Sets", "the", "default", "values", "for", "the", "efficient", "estimation" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/external/statsmodels/nonparametric/_kernel_base.py#L58-L66
mhostetter/nhl
docs/conf.py
get_version
def get_version(): """Return package version from setup.cfg""" config = RawConfigParser() config.read(os.path.join('..', 'setup.cfg')) return config.get('metadata', 'version')
python
def get_version(): """Return package version from setup.cfg""" config = RawConfigParser() config.read(os.path.join('..', 'setup.cfg')) return config.get('metadata', 'version')
[ "def", "get_version", "(", ")", ":", "config", "=", "RawConfigParser", "(", ")", "config", ".", "read", "(", "os", ".", "path", ".", "join", "(", "'..'", ",", "'setup.cfg'", ")", ")", "return", "config", ".", "get", "(", "'metadata'", ",", "'version'",...
Return package version from setup.cfg
[ "Return", "package", "version", "from", "setup", ".", "cfg" ]
train
https://github.com/mhostetter/nhl/blob/32c91cc392826e9de728563d57ab527421734ee1/docs/conf.py#L27-L31