id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
17,800
CellProfiler/centrosome
centrosome/neighmovetrack.py
NeighbourMovementTracking.calculate_costs
def calculate_costs(self, detections_1, detections_2, calculate_match_cost, params): """ Calculates assignment costs between detections and 'empty' spaces. The smaller cost the better. @param detections_1: cell list of size n in previous frame @param detections_2: cell list of size m in current frame @return: cost matrix (n+m)x(n+m) extended by cost of matching cells with emptiness """ global invalid_match size_sum = len(detections_1) + len(detections_2) # Cost matrix extended by matching cells with nothing # (for detection 1 it means losing cells, for detection 2 it means new cells). cost_matrix = numpy.zeros((size_sum, size_sum)) # lost cells cost cost_matrix[0:len(detections_1), len(detections_2):size_sum] = params["default_empty_cost"] + (1 - numpy.eye( len(detections_1), len(detections_1))) * invalid_match # new cells cost cost_matrix[len(detections_1):size_sum, 0:len(detections_2)] = params["default_empty_cost"] + (1 - numpy.eye( len(detections_2), len(detections_2))) * invalid_match # increase costs for reliable detections for row in [i for i in range(0, len(detections_1)) if detections_1[i].is_reliable() and ( not params["check_if_big"] or self.is_cell_big(detections_1[i]))]: cost_matrix[row, len(detections_2):size_sum] *= params["default_empty_reliable_cost_mult"] for col in [i for i in range(0, len(detections_2)) if detections_2[i].is_reliable() and ( not params["check_if_big"] or self.is_cell_big(detections_2[i]))]: cost_matrix[len(detections_1):size_sum, col] *= params["default_empty_reliable_cost_mult"] # calculate cost of matching cells def cost_if_not_too_far(detection_1, detection_2): if detection_1.distance(detection_2) <= self.parameters_tracking["max_distance"]: return calculate_match_cost(detection_1, detection_2) else: return invalid_match cost_matrix[0:len(detections_1), 0:len(detections_2)] = [[cost_if_not_too_far(d1, d2) for d2 in detections_2] for d1 in detections_1] return cost_matrix
python
def calculate_costs(self, detections_1, detections_2, calculate_match_cost, params): global invalid_match size_sum = len(detections_1) + len(detections_2) # Cost matrix extended by matching cells with nothing # (for detection 1 it means losing cells, for detection 2 it means new cells). cost_matrix = numpy.zeros((size_sum, size_sum)) # lost cells cost cost_matrix[0:len(detections_1), len(detections_2):size_sum] = params["default_empty_cost"] + (1 - numpy.eye( len(detections_1), len(detections_1))) * invalid_match # new cells cost cost_matrix[len(detections_1):size_sum, 0:len(detections_2)] = params["default_empty_cost"] + (1 - numpy.eye( len(detections_2), len(detections_2))) * invalid_match # increase costs for reliable detections for row in [i for i in range(0, len(detections_1)) if detections_1[i].is_reliable() and ( not params["check_if_big"] or self.is_cell_big(detections_1[i]))]: cost_matrix[row, len(detections_2):size_sum] *= params["default_empty_reliable_cost_mult"] for col in [i for i in range(0, len(detections_2)) if detections_2[i].is_reliable() and ( not params["check_if_big"] or self.is_cell_big(detections_2[i]))]: cost_matrix[len(detections_1):size_sum, col] *= params["default_empty_reliable_cost_mult"] # calculate cost of matching cells def cost_if_not_too_far(detection_1, detection_2): if detection_1.distance(detection_2) <= self.parameters_tracking["max_distance"]: return calculate_match_cost(detection_1, detection_2) else: return invalid_match cost_matrix[0:len(detections_1), 0:len(detections_2)] = [[cost_if_not_too_far(d1, d2) for d2 in detections_2] for d1 in detections_1] return cost_matrix
[ "def", "calculate_costs", "(", "self", ",", "detections_1", ",", "detections_2", ",", "calculate_match_cost", ",", "params", ")", ":", "global", "invalid_match", "size_sum", "=", "len", "(", "detections_1", ")", "+", "len", "(", "detections_2", ")", "# Cost matr...
Calculates assignment costs between detections and 'empty' spaces. The smaller cost the better. @param detections_1: cell list of size n in previous frame @param detections_2: cell list of size m in current frame @return: cost matrix (n+m)x(n+m) extended by cost of matching cells with emptiness
[ "Calculates", "assignment", "costs", "between", "detections", "and", "empty", "spaces", ".", "The", "smaller", "cost", "the", "better", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/neighmovetrack.py#L298-L341
17,801
CellProfiler/centrosome
centrosome/neighmovetrack.py
NeighbourMovementTracking.solve_assignement
def solve_assignement(self, costs): """ Solves assignment problem using Hungarian implementation by Brian M. Clapper. @param costs: square cost matrix @return: assignment function @rtype: int->int """ if costs is None or len(costs) == 0: return dict() n = costs.shape[0] pairs = [(i, j) for i in range(0, n) for j in range(0, n) if costs[i, j] < invalid_match] costs_list = [costs[i, j] for (i, j) in pairs] assignment = lapjv.lapjv(list(zip(*pairs))[0], list(zip(*pairs))[1], costs_list) indexes = enumerate(list(assignment[0])) return dict([(row, col) for row, col in indexes])
python
def solve_assignement(self, costs): if costs is None or len(costs) == 0: return dict() n = costs.shape[0] pairs = [(i, j) for i in range(0, n) for j in range(0, n) if costs[i, j] < invalid_match] costs_list = [costs[i, j] for (i, j) in pairs] assignment = lapjv.lapjv(list(zip(*pairs))[0], list(zip(*pairs))[1], costs_list) indexes = enumerate(list(assignment[0])) return dict([(row, col) for row, col in indexes])
[ "def", "solve_assignement", "(", "self", ",", "costs", ")", ":", "if", "costs", "is", "None", "or", "len", "(", "costs", ")", "==", "0", ":", "return", "dict", "(", ")", "n", "=", "costs", ".", "shape", "[", "0", "]", "pairs", "=", "[", "(", "i...
Solves assignment problem using Hungarian implementation by Brian M. Clapper. @param costs: square cost matrix @return: assignment function @rtype: int->int
[ "Solves", "assignment", "problem", "using", "Hungarian", "implementation", "by", "Brian", "M", ".", "Clapper", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/neighmovetrack.py#L363-L386
17,802
CellProfiler/centrosome
centrosome/smooth.py
circular_gaussian_kernel
def circular_gaussian_kernel(sd,radius): """Create a 2-d Gaussian convolution kernel sd - standard deviation of the gaussian in pixels radius - build a circular kernel that convolves all points in the circle bounded by this radius """ i,j = np.mgrid[-radius:radius+1,-radius:radius+1].astype(float) / radius mask = i**2 + j**2 <= 1 i = i * radius / sd j = j * radius / sd kernel = np.zeros((2*radius+1,2*radius+1)) kernel[mask] = np.e ** (-(i[mask]**2+j[mask]**2) / (2 * sd **2)) # # Normalize the kernel so that there is no net effect on a uniform image # kernel = kernel / np.sum(kernel) return kernel
python
def circular_gaussian_kernel(sd,radius): i,j = np.mgrid[-radius:radius+1,-radius:radius+1].astype(float) / radius mask = i**2 + j**2 <= 1 i = i * radius / sd j = j * radius / sd kernel = np.zeros((2*radius+1,2*radius+1)) kernel[mask] = np.e ** (-(i[mask]**2+j[mask]**2) / (2 * sd **2)) # # Normalize the kernel so that there is no net effect on a uniform image # kernel = kernel / np.sum(kernel) return kernel
[ "def", "circular_gaussian_kernel", "(", "sd", ",", "radius", ")", ":", "i", ",", "j", "=", "np", ".", "mgrid", "[", "-", "radius", ":", "radius", "+", "1", ",", "-", "radius", ":", "radius", "+", "1", "]", ".", "astype", "(", "float", ")", "/", ...
Create a 2-d Gaussian convolution kernel sd - standard deviation of the gaussian in pixels radius - build a circular kernel that convolves all points in the circle bounded by this radius
[ "Create", "a", "2", "-", "d", "Gaussian", "convolution", "kernel", "sd", "-", "standard", "deviation", "of", "the", "gaussian", "in", "pixels", "radius", "-", "build", "a", "circular", "kernel", "that", "convolves", "all", "points", "in", "the", "circle", ...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/smooth.py#L49-L68
17,803
CellProfiler/centrosome
centrosome/threshold.py
get_threshold
def get_threshold(threshold_method, threshold_modifier, image, mask=None, labels = None, threshold_range_min = None, threshold_range_max = None, threshold_correction_factor = 1.0, adaptive_window_size = 10, **kwargs): """Compute a threshold for an image threshold_method - one of the TM_ methods above threshold_modifier - TM_GLOBAL to calculate one threshold over entire image TM_ADAPTIVE to calculate a per-pixel threshold TM_PER_OBJECT to calculate a different threshold for each object image - a NxM numpy array of the image data Returns a tuple of local_threshold and global_threshold where: * global_threshold is the single number calculated using the threshold method over the whole image * local_threshold is the global_threshold for global methods. For adaptive and per-object thresholding, local_threshold is a matrix of threshold values representing the threshold to be applied at each pixel of the image. Different methods have optional and required parameters: Required: TM_PER_OBJECT: labels - a labels matrix that defines the extents of the individual objects to be thresholded separately. Optional: All: mask - a mask of the significant pixels in the image threshold_range_min, threshold_range_max - constrain the threshold values to be examined to values between these limits threshold_correction_factor - the calculated threshold is multiplied by this number to get the final threshold TM_MOG (mixture of Gaussians): object_fraction - fraction of image expected to be occupied by objects (pixels that are above the threshold) TM_OTSU - We have algorithms derived from Otsu. There is a three-class version of Otsu in addition to the two class. There is also an entropy measure in addition to the weighted variance. two_class_otsu - assume that the distribution represents two intensity classes if true, three if false. use_weighted_variance - use Otsu's weighted variance if true, an entropy measure if false assign_middle_to_foreground - assign pixels in the middle class in a three-class Otsu to the foreground if true or the background if false. """ global_threshold = get_global_threshold( threshold_method, image, mask, **kwargs) global_threshold *= threshold_correction_factor if not threshold_range_min is None: global_threshold = max(global_threshold, threshold_range_min) if not threshold_range_max is None: global_threshold = min(global_threshold, threshold_range_max) if threshold_modifier == TM_GLOBAL: local_threshold=global_threshold elif threshold_modifier == TM_ADAPTIVE: local_threshold = get_adaptive_threshold( threshold_method, image, global_threshold, mask, adaptive_window_size = adaptive_window_size, **kwargs) local_threshold = local_threshold * threshold_correction_factor elif threshold_modifier == TM_PER_OBJECT: local_threshold = get_per_object_threshold( threshold_method, image, global_threshold, mask, labels, threshold_range_min, threshold_range_max,**kwargs) local_threshold = local_threshold * threshold_correction_factor else: raise NotImplementedError("%s thresholding is not implemented"%(threshold_modifier)) if isinstance(local_threshold, np.ndarray): # # Constrain thresholds to within .7 to 1.5 of the global threshold. # threshold_range_min = max(threshold_range_min, global_threshold * .7) threshold_range_max = min(threshold_range_max, global_threshold * 1.5) if not threshold_range_min is None: local_threshold[local_threshold < threshold_range_min] = \ threshold_range_min if not threshold_range_max is None: local_threshold[local_threshold > threshold_range_max] = \ threshold_range_max if (threshold_modifier == TM_PER_OBJECT) and (labels is not None): local_threshold[labels == 0] = 1.0 else: if not threshold_range_min is None: local_threshold = max(local_threshold, threshold_range_min) if not threshold_range_max is None: local_threshold = min(local_threshold, threshold_range_max) return local_threshold, global_threshold
python
def get_threshold(threshold_method, threshold_modifier, image, mask=None, labels = None, threshold_range_min = None, threshold_range_max = None, threshold_correction_factor = 1.0, adaptive_window_size = 10, **kwargs): global_threshold = get_global_threshold( threshold_method, image, mask, **kwargs) global_threshold *= threshold_correction_factor if not threshold_range_min is None: global_threshold = max(global_threshold, threshold_range_min) if not threshold_range_max is None: global_threshold = min(global_threshold, threshold_range_max) if threshold_modifier == TM_GLOBAL: local_threshold=global_threshold elif threshold_modifier == TM_ADAPTIVE: local_threshold = get_adaptive_threshold( threshold_method, image, global_threshold, mask, adaptive_window_size = adaptive_window_size, **kwargs) local_threshold = local_threshold * threshold_correction_factor elif threshold_modifier == TM_PER_OBJECT: local_threshold = get_per_object_threshold( threshold_method, image, global_threshold, mask, labels, threshold_range_min, threshold_range_max,**kwargs) local_threshold = local_threshold * threshold_correction_factor else: raise NotImplementedError("%s thresholding is not implemented"%(threshold_modifier)) if isinstance(local_threshold, np.ndarray): # # Constrain thresholds to within .7 to 1.5 of the global threshold. # threshold_range_min = max(threshold_range_min, global_threshold * .7) threshold_range_max = min(threshold_range_max, global_threshold * 1.5) if not threshold_range_min is None: local_threshold[local_threshold < threshold_range_min] = \ threshold_range_min if not threshold_range_max is None: local_threshold[local_threshold > threshold_range_max] = \ threshold_range_max if (threshold_modifier == TM_PER_OBJECT) and (labels is not None): local_threshold[labels == 0] = 1.0 else: if not threshold_range_min is None: local_threshold = max(local_threshold, threshold_range_min) if not threshold_range_max is None: local_threshold = min(local_threshold, threshold_range_max) return local_threshold, global_threshold
[ "def", "get_threshold", "(", "threshold_method", ",", "threshold_modifier", ",", "image", ",", "mask", "=", "None", ",", "labels", "=", "None", ",", "threshold_range_min", "=", "None", ",", "threshold_range_max", "=", "None", ",", "threshold_correction_factor", "=...
Compute a threshold for an image threshold_method - one of the TM_ methods above threshold_modifier - TM_GLOBAL to calculate one threshold over entire image TM_ADAPTIVE to calculate a per-pixel threshold TM_PER_OBJECT to calculate a different threshold for each object image - a NxM numpy array of the image data Returns a tuple of local_threshold and global_threshold where: * global_threshold is the single number calculated using the threshold method over the whole image * local_threshold is the global_threshold for global methods. For adaptive and per-object thresholding, local_threshold is a matrix of threshold values representing the threshold to be applied at each pixel of the image. Different methods have optional and required parameters: Required: TM_PER_OBJECT: labels - a labels matrix that defines the extents of the individual objects to be thresholded separately. Optional: All: mask - a mask of the significant pixels in the image threshold_range_min, threshold_range_max - constrain the threshold values to be examined to values between these limits threshold_correction_factor - the calculated threshold is multiplied by this number to get the final threshold TM_MOG (mixture of Gaussians): object_fraction - fraction of image expected to be occupied by objects (pixels that are above the threshold) TM_OTSU - We have algorithms derived from Otsu. There is a three-class version of Otsu in addition to the two class. There is also an entropy measure in addition to the weighted variance. two_class_otsu - assume that the distribution represents two intensity classes if true, three if false. use_weighted_variance - use Otsu's weighted variance if true, an entropy measure if false assign_middle_to_foreground - assign pixels in the middle class in a three-class Otsu to the foreground if true or the background if false.
[ "Compute", "a", "threshold", "for", "an", "image", "threshold_method", "-", "one", "of", "the", "TM_", "methods", "above", "threshold_modifier", "-", "TM_GLOBAL", "to", "calculate", "one", "threshold", "over", "entire", "image", "TM_ADAPTIVE", "to", "calculate", ...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L64-L153
17,804
CellProfiler/centrosome
centrosome/threshold.py
get_global_threshold
def get_global_threshold(threshold_method, image, mask = None, **kwargs): """Compute a single threshold over the whole image""" if mask is not None and not np.any(mask): return 1 if threshold_method == TM_OTSU: fn = get_otsu_threshold elif threshold_method == TM_MOG: fn = get_mog_threshold elif threshold_method == TM_BACKGROUND: fn = get_background_threshold elif threshold_method == TM_ROBUST_BACKGROUND: fn = get_robust_background_threshold elif threshold_method == TM_RIDLER_CALVARD: fn = get_ridler_calvard_threshold elif threshold_method == TM_KAPUR: fn = get_kapur_threshold elif threshold_method == TM_MCT: fn = get_maximum_correlation_threshold else: raise NotImplementedError("%s algorithm not implemented"%(threshold_method)) kwargs = dict([(k, v) for k, v in kwargs.items() if k in fn.args]) return fn(image, mask, **kwargs)
python
def get_global_threshold(threshold_method, image, mask = None, **kwargs): if mask is not None and not np.any(mask): return 1 if threshold_method == TM_OTSU: fn = get_otsu_threshold elif threshold_method == TM_MOG: fn = get_mog_threshold elif threshold_method == TM_BACKGROUND: fn = get_background_threshold elif threshold_method == TM_ROBUST_BACKGROUND: fn = get_robust_background_threshold elif threshold_method == TM_RIDLER_CALVARD: fn = get_ridler_calvard_threshold elif threshold_method == TM_KAPUR: fn = get_kapur_threshold elif threshold_method == TM_MCT: fn = get_maximum_correlation_threshold else: raise NotImplementedError("%s algorithm not implemented"%(threshold_method)) kwargs = dict([(k, v) for k, v in kwargs.items() if k in fn.args]) return fn(image, mask, **kwargs)
[ "def", "get_global_threshold", "(", "threshold_method", ",", "image", ",", "mask", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "mask", "is", "not", "None", "and", "not", "np", ".", "any", "(", "mask", ")", ":", "return", "1", "if", "thresho...
Compute a single threshold over the whole image
[ "Compute", "a", "single", "threshold", "over", "the", "whole", "image" ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L155-L178
17,805
CellProfiler/centrosome
centrosome/threshold.py
get_per_object_threshold
def get_per_object_threshold(method, image, threshold, mask=None, labels=None, threshold_range_min = None, threshold_range_max = None, **kwargs): """Return a matrix giving threshold per pixel calculated per-object image - image to be thresholded mask - mask out "don't care" pixels labels - a label mask indicating object boundaries threshold - the global threshold """ if labels is None: labels = np.ones(image.shape,int) if not mask is None: labels[np.logical_not(mask)] = 0 label_extents = scipy.ndimage.find_objects(labels,np.max(labels)) local_threshold = np.ones(image.shape,image.dtype) for i, extent in enumerate(label_extents, start=1): label_mask = labels[extent]==i if not mask is None: label_mask = np.logical_and(mask[extent], label_mask) values = image[extent] per_object_threshold = get_global_threshold( method, values, mask = label_mask, **kwargs) local_threshold[extent][label_mask] = per_object_threshold return local_threshold
python
def get_per_object_threshold(method, image, threshold, mask=None, labels=None, threshold_range_min = None, threshold_range_max = None, **kwargs): if labels is None: labels = np.ones(image.shape,int) if not mask is None: labels[np.logical_not(mask)] = 0 label_extents = scipy.ndimage.find_objects(labels,np.max(labels)) local_threshold = np.ones(image.shape,image.dtype) for i, extent in enumerate(label_extents, start=1): label_mask = labels[extent]==i if not mask is None: label_mask = np.logical_and(mask[extent], label_mask) values = image[extent] per_object_threshold = get_global_threshold( method, values, mask = label_mask, **kwargs) local_threshold[extent][label_mask] = per_object_threshold return local_threshold
[ "def", "get_per_object_threshold", "(", "method", ",", "image", ",", "threshold", ",", "mask", "=", "None", ",", "labels", "=", "None", ",", "threshold_range_min", "=", "None", ",", "threshold_range_max", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if...
Return a matrix giving threshold per pixel calculated per-object image - image to be thresholded mask - mask out "don't care" pixels labels - a label mask indicating object boundaries threshold - the global threshold
[ "Return", "a", "matrix", "giving", "threshold", "per", "pixel", "calculated", "per", "-", "object", "image", "-", "image", "to", "be", "thresholded", "mask", "-", "mask", "out", "don", "t", "care", "pixels", "labels", "-", "a", "label", "mask", "indicating...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L249-L274
17,806
CellProfiler/centrosome
centrosome/threshold.py
mad
def mad(a): '''Calculate the median absolute deviation of a sample a - a numpy array-like collection of values returns the median of the deviation of a from its median. ''' a = np.asfarray(a).flatten() return np.median(np.abs(a - np.median(a)))
python
def mad(a): '''Calculate the median absolute deviation of a sample a - a numpy array-like collection of values returns the median of the deviation of a from its median. ''' a = np.asfarray(a).flatten() return np.median(np.abs(a - np.median(a)))
[ "def", "mad", "(", "a", ")", ":", "a", "=", "np", ".", "asfarray", "(", "a", ")", ".", "flatten", "(", ")", "return", "np", ".", "median", "(", "np", ".", "abs", "(", "a", "-", "np", ".", "median", "(", "a", ")", ")", ")" ]
Calculate the median absolute deviation of a sample a - a numpy array-like collection of values returns the median of the deviation of a from its median.
[ "Calculate", "the", "median", "absolute", "deviation", "of", "a", "sample", "a", "-", "a", "numpy", "array", "-", "like", "collection", "of", "values", "returns", "the", "median", "of", "the", "deviation", "of", "a", "from", "its", "median", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L520-L528
17,807
CellProfiler/centrosome
centrosome/threshold.py
get_kapur_threshold
def get_kapur_threshold(image, mask=None): """The Kapur, Sahoo, & Wong method of thresholding, adapted to log-space.""" cropped_image = np.array(image.flat) if mask is None else image[mask] if np.product(cropped_image.shape)<3: return 0 if np.min(cropped_image) == np.max(cropped_image): return cropped_image[0] log_image = np.log2(smooth_with_noise(cropped_image, 8)) min_log_image = np.min(log_image) max_log_image = np.max(log_image) histogram = scipy.ndimage.histogram(log_image, min_log_image, max_log_image, 256) histogram_values = (min_log_image + (max_log_image - min_log_image)* np.arange(256, dtype=float) / 255) # drop any zero bins keep = histogram != 0 histogram = histogram[keep] histogram_values = histogram_values[keep] # check for corner cases if np.product(histogram_values)==1: return 2**histogram_values[0] # Normalize to probabilities p = histogram.astype(float) / float(np.sum(histogram)) # Find the probabilities totals up to and above each possible threshold. lo_sum = np.cumsum(p); hi_sum = lo_sum[-1] - lo_sum; lo_e = np.cumsum(p * np.log2(p)); hi_e = lo_e[-1] - lo_e; # compute the entropies lo_entropy = lo_e / lo_sum - np.log2(lo_sum); hi_entropy = hi_e / hi_sum - np.log2(hi_sum); sum_entropy = lo_entropy[:-1] + hi_entropy[:-1]; sum_entropy[np.logical_not(np.isfinite(sum_entropy))] = np.Inf entry = np.argmin(sum_entropy); return 2**((histogram_values[entry] + histogram_values[entry+1]) / 2);
python
def get_kapur_threshold(image, mask=None): cropped_image = np.array(image.flat) if mask is None else image[mask] if np.product(cropped_image.shape)<3: return 0 if np.min(cropped_image) == np.max(cropped_image): return cropped_image[0] log_image = np.log2(smooth_with_noise(cropped_image, 8)) min_log_image = np.min(log_image) max_log_image = np.max(log_image) histogram = scipy.ndimage.histogram(log_image, min_log_image, max_log_image, 256) histogram_values = (min_log_image + (max_log_image - min_log_image)* np.arange(256, dtype=float) / 255) # drop any zero bins keep = histogram != 0 histogram = histogram[keep] histogram_values = histogram_values[keep] # check for corner cases if np.product(histogram_values)==1: return 2**histogram_values[0] # Normalize to probabilities p = histogram.astype(float) / float(np.sum(histogram)) # Find the probabilities totals up to and above each possible threshold. lo_sum = np.cumsum(p); hi_sum = lo_sum[-1] - lo_sum; lo_e = np.cumsum(p * np.log2(p)); hi_e = lo_e[-1] - lo_e; # compute the entropies lo_entropy = lo_e / lo_sum - np.log2(lo_sum); hi_entropy = hi_e / hi_sum - np.log2(hi_sum); sum_entropy = lo_entropy[:-1] + hi_entropy[:-1]; sum_entropy[np.logical_not(np.isfinite(sum_entropy))] = np.Inf entry = np.argmin(sum_entropy); return 2**((histogram_values[entry] + histogram_values[entry+1]) / 2);
[ "def", "get_kapur_threshold", "(", "image", ",", "mask", "=", "None", ")", ":", "cropped_image", "=", "np", ".", "array", "(", "image", ".", "flat", ")", "if", "mask", "is", "None", "else", "image", "[", "mask", "]", "if", "np", ".", "product", "(", ...
The Kapur, Sahoo, & Wong method of thresholding, adapted to log-space.
[ "The", "Kapur", "Sahoo", "&", "Wong", "method", "of", "thresholding", "adapted", "to", "log", "-", "space", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L587-L625
17,808
CellProfiler/centrosome
centrosome/threshold.py
weighted_variance
def weighted_variance(image, mask, binary_image): """Compute the log-transformed variance of foreground and background image - intensity image used for thresholding mask - mask of ignored pixels binary_image - binary image marking foreground and background """ if not np.any(mask): return 0 # # Clamp the dynamic range of the foreground # minval = np.max(image[mask])/256 if minval == 0: return 0 fg = np.log2(np.maximum(image[binary_image & mask], minval)) bg = np.log2(np.maximum(image[(~ binary_image) & mask], minval)) nfg = np.product(fg.shape) nbg = np.product(bg.shape) if nfg == 0: return np.var(bg) elif nbg == 0: return np.var(fg) else: return (np.var(fg) * nfg + np.var(bg)*nbg) / (nfg+nbg)
python
def weighted_variance(image, mask, binary_image): if not np.any(mask): return 0 # # Clamp the dynamic range of the foreground # minval = np.max(image[mask])/256 if minval == 0: return 0 fg = np.log2(np.maximum(image[binary_image & mask], minval)) bg = np.log2(np.maximum(image[(~ binary_image) & mask], minval)) nfg = np.product(fg.shape) nbg = np.product(bg.shape) if nfg == 0: return np.var(bg) elif nbg == 0: return np.var(fg) else: return (np.var(fg) * nfg + np.var(bg)*nbg) / (nfg+nbg)
[ "def", "weighted_variance", "(", "image", ",", "mask", ",", "binary_image", ")", ":", "if", "not", "np", ".", "any", "(", "mask", ")", ":", "return", "0", "#", "# Clamp the dynamic range of the foreground", "#", "minval", "=", "np", ".", "max", "(", "image...
Compute the log-transformed variance of foreground and background image - intensity image used for thresholding mask - mask of ignored pixels binary_image - binary image marking foreground and background
[ "Compute", "the", "log", "-", "transformed", "variance", "of", "foreground", "and", "background", "image", "-", "intensity", "image", "used", "for", "thresholding", "mask", "-", "mask", "of", "ignored", "pixels", "binary_image", "-", "binary", "image", "marking"...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L691-L718
17,809
CellProfiler/centrosome
centrosome/threshold.py
sum_of_entropies
def sum_of_entropies(image, mask, binary_image): """Bin the foreground and background pixels and compute the entropy of the distribution of points among the bins """ mask=mask.copy() mask[np.isnan(image)] = False if not np.any(mask): return 0 # # Clamp the dynamic range of the foreground # minval = np.max(image[mask])/256 if minval == 0: return 0 clamped_image = image.copy() clamped_image[clamped_image < minval] = minval # # Smooth image with -8 bits of noise # image = smooth_with_noise(clamped_image, 8) im_min = np.min(image) im_max = np.max(image) # # Figure out the bounds for the histogram # upper = np.log2(im_max) lower = np.log2(im_min) if upper == lower: # All values are the same, answer is log2 of # of pixels return math.log(np.sum(mask),2) # # Create log-transformed lists of points in the foreground and background # fg = image[binary_image & mask] bg = image[(~ binary_image) & mask] if len(fg) == 0 or len(bg) == 0: return 0 log_fg = np.log2(fg) log_bg = np.log2(bg) # # Make these into histograms hfg = numpy_histogram(log_fg, 256, range=(lower,upper))[0] hbg = numpy_histogram(log_bg, 256, range=(lower,upper))[0] #hfg = scipy.ndimage.histogram(log_fg,lower,upper,256) #hbg = scipy.ndimage.histogram(log_bg,lower,upper,256) # # Drop empty bins # hfg = hfg[hfg>0] hbg = hbg[hbg>0] if np.product(hfg.shape) == 0: hfg = np.ones((1,),int) if np.product(hbg.shape) == 0: hbg = np.ones((1,),int) # # Normalize # hfg = hfg.astype(float) / float(np.sum(hfg)) hbg = hbg.astype(float) / float(np.sum(hbg)) # # Compute sum of entropies # return np.sum(hfg * np.log2(hfg)) + np.sum(hbg*np.log2(hbg))
python
def sum_of_entropies(image, mask, binary_image): mask=mask.copy() mask[np.isnan(image)] = False if not np.any(mask): return 0 # # Clamp the dynamic range of the foreground # minval = np.max(image[mask])/256 if minval == 0: return 0 clamped_image = image.copy() clamped_image[clamped_image < minval] = minval # # Smooth image with -8 bits of noise # image = smooth_with_noise(clamped_image, 8) im_min = np.min(image) im_max = np.max(image) # # Figure out the bounds for the histogram # upper = np.log2(im_max) lower = np.log2(im_min) if upper == lower: # All values are the same, answer is log2 of # of pixels return math.log(np.sum(mask),2) # # Create log-transformed lists of points in the foreground and background # fg = image[binary_image & mask] bg = image[(~ binary_image) & mask] if len(fg) == 0 or len(bg) == 0: return 0 log_fg = np.log2(fg) log_bg = np.log2(bg) # # Make these into histograms hfg = numpy_histogram(log_fg, 256, range=(lower,upper))[0] hbg = numpy_histogram(log_bg, 256, range=(lower,upper))[0] #hfg = scipy.ndimage.histogram(log_fg,lower,upper,256) #hbg = scipy.ndimage.histogram(log_bg,lower,upper,256) # # Drop empty bins # hfg = hfg[hfg>0] hbg = hbg[hbg>0] if np.product(hfg.shape) == 0: hfg = np.ones((1,),int) if np.product(hbg.shape) == 0: hbg = np.ones((1,),int) # # Normalize # hfg = hfg.astype(float) / float(np.sum(hfg)) hbg = hbg.astype(float) / float(np.sum(hbg)) # # Compute sum of entropies # return np.sum(hfg * np.log2(hfg)) + np.sum(hbg*np.log2(hbg))
[ "def", "sum_of_entropies", "(", "image", ",", "mask", ",", "binary_image", ")", ":", "mask", "=", "mask", ".", "copy", "(", ")", "mask", "[", "np", ".", "isnan", "(", "image", ")", "]", "=", "False", "if", "not", "np", ".", "any", "(", "mask", ")...
Bin the foreground and background pixels and compute the entropy of the distribution of points among the bins
[ "Bin", "the", "foreground", "and", "background", "pixels", "and", "compute", "the", "entropy", "of", "the", "distribution", "of", "points", "among", "the", "bins" ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L720-L782
17,810
CellProfiler/centrosome
centrosome/threshold.py
log_transform
def log_transform(image): '''Renormalize image intensities to log space Returns a tuple of transformed image and a dictionary to be passed into inverse_log_transform. The minimum and maximum from the dictionary can be applied to an image by the inverse_log_transform to convert it back to its former intensity values. ''' orig_min, orig_max = scipy.ndimage.extrema(image)[:2] # # We add 1/2 bit noise to an 8 bit image to give the log a bottom # limage = image.copy() noise_min = orig_min + (orig_max-orig_min)/256.0+np.finfo(image.dtype).eps limage[limage < noise_min] = noise_min d = { "noise_min":noise_min} limage = np.log(limage) log_min, log_max = scipy.ndimage.extrema(limage)[:2] d["log_min"] = log_min d["log_max"] = log_max return stretch(limage), d
python
def log_transform(image): '''Renormalize image intensities to log space Returns a tuple of transformed image and a dictionary to be passed into inverse_log_transform. The minimum and maximum from the dictionary can be applied to an image by the inverse_log_transform to convert it back to its former intensity values. ''' orig_min, orig_max = scipy.ndimage.extrema(image)[:2] # # We add 1/2 bit noise to an 8 bit image to give the log a bottom # limage = image.copy() noise_min = orig_min + (orig_max-orig_min)/256.0+np.finfo(image.dtype).eps limage[limage < noise_min] = noise_min d = { "noise_min":noise_min} limage = np.log(limage) log_min, log_max = scipy.ndimage.extrema(limage)[:2] d["log_min"] = log_min d["log_max"] = log_max return stretch(limage), d
[ "def", "log_transform", "(", "image", ")", ":", "orig_min", ",", "orig_max", "=", "scipy", ".", "ndimage", ".", "extrema", "(", "image", ")", "[", ":", "2", "]", "#", "# We add 1/2 bit noise to an 8 bit image to give the log a bottom", "#", "limage", "=", "image...
Renormalize image intensities to log space Returns a tuple of transformed image and a dictionary to be passed into inverse_log_transform. The minimum and maximum from the dictionary can be applied to an image by the inverse_log_transform to convert it back to its former intensity values.
[ "Renormalize", "image", "intensities", "to", "log", "space", "Returns", "a", "tuple", "of", "transformed", "image", "and", "a", "dictionary", "to", "be", "passed", "into", "inverse_log_transform", ".", "The", "minimum", "and", "maximum", "from", "the", "dictiona...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L784-L804
17,811
CellProfiler/centrosome
centrosome/threshold.py
numpy_histogram
def numpy_histogram(a, bins=10, range=None, normed=False, weights=None): '''A version of numpy.histogram that accounts for numpy's version''' args = inspect.getargs(np.histogram.__code__)[0] if args[-1] == "new": return np.histogram(a, bins, range, normed, weights, new=True) return np.histogram(a, bins, range, normed, weights)
python
def numpy_histogram(a, bins=10, range=None, normed=False, weights=None): '''A version of numpy.histogram that accounts for numpy's version''' args = inspect.getargs(np.histogram.__code__)[0] if args[-1] == "new": return np.histogram(a, bins, range, normed, weights, new=True) return np.histogram(a, bins, range, normed, weights)
[ "def", "numpy_histogram", "(", "a", ",", "bins", "=", "10", ",", "range", "=", "None", ",", "normed", "=", "False", ",", "weights", "=", "None", ")", ":", "args", "=", "inspect", ".", "getargs", "(", "np", ".", "histogram", ".", "__code__", ")", "[...
A version of numpy.histogram that accounts for numpy's version
[ "A", "version", "of", "numpy", ".", "histogram", "that", "accounts", "for", "numpy", "s", "version" ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/threshold.py#L814-L819
17,812
CellProfiler/centrosome
centrosome/rankorder.py
rank_order
def rank_order(image, nbins=None): """Return an image of the same shape where each pixel has the rank-order value of the corresponding pixel in the image. The returned image's elements are of type np.uint32 which simplifies processing in C code. """ flat_image = image.ravel() sort_order = flat_image.argsort().astype(np.uint32) flat_image = flat_image[sort_order] sort_rank = np.zeros_like(sort_order) is_different = flat_image[:-1] != flat_image[1:] np.cumsum(is_different, out=sort_rank[1:]) original_values = np.zeros((sort_rank[-1]+1,),image.dtype) original_values[0] = flat_image[0] original_values[1:] = flat_image[1:][is_different] int_image = np.zeros_like(sort_order) int_image[sort_order] = sort_rank if nbins is not None: max_ranked_data = np.max(int_image) while max_ranked_data >= nbins: # # Decimate the bins until there are fewer than nbins # hist = np.bincount(int_image) # # Rank the bins from lowest count to highest order = np.argsort(hist) # # find enough to maybe decimate to nbins # candidates = order[:max_ranked_data+2-nbins] to_delete = np.zeros(max_ranked_data+2, bool) to_delete[candidates] = True # # Choose candidates that are either not next to others # or have an even index so as not to delete adjacent bins # td_mask = to_delete[:-1] & ( ((np.arange(max_ranked_data+1) & 2) == 0) | (~ to_delete[1:])) if td_mask[0]: td_mask[0] = False # # A value to be deleted has the same index as the following # value and the two end up being merged # rd_translation = np.cumsum(~td_mask)-1 # # Translate the rankings to the new space # int_image = rd_translation[int_image] # # Eliminate the bins with low counts # original_values = original_values[~td_mask] max_ranked_data = len(original_values)-1 return (int_image.reshape(image.shape), original_values)
python
def rank_order(image, nbins=None): flat_image = image.ravel() sort_order = flat_image.argsort().astype(np.uint32) flat_image = flat_image[sort_order] sort_rank = np.zeros_like(sort_order) is_different = flat_image[:-1] != flat_image[1:] np.cumsum(is_different, out=sort_rank[1:]) original_values = np.zeros((sort_rank[-1]+1,),image.dtype) original_values[0] = flat_image[0] original_values[1:] = flat_image[1:][is_different] int_image = np.zeros_like(sort_order) int_image[sort_order] = sort_rank if nbins is not None: max_ranked_data = np.max(int_image) while max_ranked_data >= nbins: # # Decimate the bins until there are fewer than nbins # hist = np.bincount(int_image) # # Rank the bins from lowest count to highest order = np.argsort(hist) # # find enough to maybe decimate to nbins # candidates = order[:max_ranked_data+2-nbins] to_delete = np.zeros(max_ranked_data+2, bool) to_delete[candidates] = True # # Choose candidates that are either not next to others # or have an even index so as not to delete adjacent bins # td_mask = to_delete[:-1] & ( ((np.arange(max_ranked_data+1) & 2) == 0) | (~ to_delete[1:])) if td_mask[0]: td_mask[0] = False # # A value to be deleted has the same index as the following # value and the two end up being merged # rd_translation = np.cumsum(~td_mask)-1 # # Translate the rankings to the new space # int_image = rd_translation[int_image] # # Eliminate the bins with low counts # original_values = original_values[~td_mask] max_ranked_data = len(original_values)-1 return (int_image.reshape(image.shape), original_values)
[ "def", "rank_order", "(", "image", ",", "nbins", "=", "None", ")", ":", "flat_image", "=", "image", ".", "ravel", "(", ")", "sort_order", "=", "flat_image", ".", "argsort", "(", ")", ".", "astype", "(", "np", ".", "uint32", ")", "flat_image", "=", "f...
Return an image of the same shape where each pixel has the rank-order value of the corresponding pixel in the image. The returned image's elements are of type np.uint32 which simplifies processing in C code.
[ "Return", "an", "image", "of", "the", "same", "shape", "where", "each", "pixel", "has", "the", "rank", "-", "order", "value", "of", "the", "corresponding", "pixel", "in", "the", "image", ".", "The", "returned", "image", "s", "elements", "are", "of", "typ...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/rankorder.py#L4-L61
17,813
CellProfiler/centrosome
centrosome/haralick.py
quantize
def quantize(image, nlevels): """Quantize an image into integers 0, 1, ..., nlevels - 1. image -- a numpy array of type float, range [0, 1] nlevels -- an integer """ tmp = np.array(image // (1.0 / nlevels), dtype='i1') return tmp.clip(0, nlevels - 1)
python
def quantize(image, nlevels): tmp = np.array(image // (1.0 / nlevels), dtype='i1') return tmp.clip(0, nlevels - 1)
[ "def", "quantize", "(", "image", ",", "nlevels", ")", ":", "tmp", "=", "np", ".", "array", "(", "image", "//", "(", "1.0", "/", "nlevels", ")", ",", "dtype", "=", "'i1'", ")", "return", "tmp", ".", "clip", "(", "0", ",", "nlevels", "-", "1", ")...
Quantize an image into integers 0, 1, ..., nlevels - 1. image -- a numpy array of type float, range [0, 1] nlevels -- an integer
[ "Quantize", "an", "image", "into", "integers", "0", "1", "...", "nlevels", "-", "1", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L26-L33
17,814
CellProfiler/centrosome
centrosome/haralick.py
cooccurrence
def cooccurrence(quantized_image, labels, scale_i=3, scale_j=0): """Calculates co-occurrence matrices for all the objects in the image. Return an array P of shape (nobjects, nlevels, nlevels) such that P[o, :, :] is the cooccurence matrix for object o. quantized_image -- a numpy array of integer type labels -- a numpy array of integer type scale -- an integer For each object O, the cooccurrence matrix is defined as follows. Given a row number I in the matrix, let A be the set of pixels in O with gray level I, excluding pixels in the rightmost S columns of the image. Let B be the set of pixels in O that are S pixels to the right of a pixel in A. Row I of the cooccurence matrix is the gray-level histogram of the pixels in B. """ labels = labels.astype(int) nlevels = quantized_image.max() + 1 nobjects = labels.max() if scale_i < 0: scale_i = -scale_i scale_j = -scale_j if scale_i == 0 and scale_j > 0: image_a = quantized_image[:, :-scale_j] image_b = quantized_image[:, scale_j:] labels_ab = labels_a = labels[:, :-scale_j] labels_b = labels[:, scale_j:] elif scale_i > 0 and scale_j == 0: image_a = quantized_image[:-scale_i, :] image_b = quantized_image[scale_i:, :] labels_ab = labels_a = labels[:-scale_i, :] labels_b = labels[scale_i:, :] elif scale_i > 0 and scale_j > 0: image_a = quantized_image[:-scale_i, :-scale_j] image_b = quantized_image[scale_i:, scale_j:] labels_ab = labels_a = labels[:-scale_i, :-scale_j] labels_b = labels[scale_i:, scale_j:] else: # scale_j should be negative image_a = quantized_image[:-scale_i, -scale_j:] image_b = quantized_image[scale_i:, :scale_j] labels_ab = labels_a = labels[:-scale_i, -scale_j:] labels_b = labels[scale_i:, :scale_j] equilabel = ((labels_a == labels_b) & (labels_a > 0)) if np.any(equilabel): Q = (nlevels*nlevels*(labels_ab[equilabel]-1)+ nlevels*image_a[equilabel]+image_b[equilabel]) R = np.bincount(Q) if R.size != nobjects*nlevels*nlevels: S = np.zeros(nobjects*nlevels*nlevels-R.size) R = np.hstack((R, S)) P = R.reshape(nobjects, nlevels, nlevels) pixel_count = fix(scind.sum(equilabel, labels_ab, np.arange(nobjects, dtype=np.int32)+1)) pixel_count = np.tile(pixel_count[:,np.newaxis,np.newaxis], (1,nlevels,nlevels)) return (P.astype(float) / pixel_count.astype(float), nlevels) else: return np.zeros((nobjects, nlevels, nlevels)), nlevels
python
def cooccurrence(quantized_image, labels, scale_i=3, scale_j=0): labels = labels.astype(int) nlevels = quantized_image.max() + 1 nobjects = labels.max() if scale_i < 0: scale_i = -scale_i scale_j = -scale_j if scale_i == 0 and scale_j > 0: image_a = quantized_image[:, :-scale_j] image_b = quantized_image[:, scale_j:] labels_ab = labels_a = labels[:, :-scale_j] labels_b = labels[:, scale_j:] elif scale_i > 0 and scale_j == 0: image_a = quantized_image[:-scale_i, :] image_b = quantized_image[scale_i:, :] labels_ab = labels_a = labels[:-scale_i, :] labels_b = labels[scale_i:, :] elif scale_i > 0 and scale_j > 0: image_a = quantized_image[:-scale_i, :-scale_j] image_b = quantized_image[scale_i:, scale_j:] labels_ab = labels_a = labels[:-scale_i, :-scale_j] labels_b = labels[scale_i:, scale_j:] else: # scale_j should be negative image_a = quantized_image[:-scale_i, -scale_j:] image_b = quantized_image[scale_i:, :scale_j] labels_ab = labels_a = labels[:-scale_i, -scale_j:] labels_b = labels[scale_i:, :scale_j] equilabel = ((labels_a == labels_b) & (labels_a > 0)) if np.any(equilabel): Q = (nlevels*nlevels*(labels_ab[equilabel]-1)+ nlevels*image_a[equilabel]+image_b[equilabel]) R = np.bincount(Q) if R.size != nobjects*nlevels*nlevels: S = np.zeros(nobjects*nlevels*nlevels-R.size) R = np.hstack((R, S)) P = R.reshape(nobjects, nlevels, nlevels) pixel_count = fix(scind.sum(equilabel, labels_ab, np.arange(nobjects, dtype=np.int32)+1)) pixel_count = np.tile(pixel_count[:,np.newaxis,np.newaxis], (1,nlevels,nlevels)) return (P.astype(float) / pixel_count.astype(float), nlevels) else: return np.zeros((nobjects, nlevels, nlevels)), nlevels
[ "def", "cooccurrence", "(", "quantized_image", ",", "labels", ",", "scale_i", "=", "3", ",", "scale_j", "=", "0", ")", ":", "labels", "=", "labels", ".", "astype", "(", "int", ")", "nlevels", "=", "quantized_image", ".", "max", "(", ")", "+", "1", "n...
Calculates co-occurrence matrices for all the objects in the image. Return an array P of shape (nobjects, nlevels, nlevels) such that P[o, :, :] is the cooccurence matrix for object o. quantized_image -- a numpy array of integer type labels -- a numpy array of integer type scale -- an integer For each object O, the cooccurrence matrix is defined as follows. Given a row number I in the matrix, let A be the set of pixels in O with gray level I, excluding pixels in the rightmost S columns of the image. Let B be the set of pixels in O that are S pixels to the right of a pixel in A. Row I of the cooccurence matrix is the gray-level histogram of the pixels in B.
[ "Calculates", "co", "-", "occurrence", "matrices", "for", "all", "the", "objects", "in", "the", "image", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L35-L95
17,815
CellProfiler/centrosome
centrosome/haralick.py
Haralick.H5
def H5(self): "Inverse difference moment." t = 1 + toeplitz(self.levels) ** 2 repeated = np.tile(t[np.newaxis], (self.nobjects, 1, 1)) return (1.0 / repeated * self.P).sum(2).sum(1)
python
def H5(self): "Inverse difference moment." t = 1 + toeplitz(self.levels) ** 2 repeated = np.tile(t[np.newaxis], (self.nobjects, 1, 1)) return (1.0 / repeated * self.P).sum(2).sum(1)
[ "def", "H5", "(", "self", ")", ":", "t", "=", "1", "+", "toeplitz", "(", "self", ".", "levels", ")", "**", "2", "repeated", "=", "np", ".", "tile", "(", "t", "[", "np", ".", "newaxis", "]", ",", "(", "self", ".", "nobjects", ",", "1", ",", ...
Inverse difference moment.
[ "Inverse", "difference", "moment", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L184-L188
17,816
CellProfiler/centrosome
centrosome/haralick.py
Haralick.H6
def H6(self): "Sum average." if not hasattr(self, '_H6'): self._H6 = ((self.rlevels2 + 2) * self.p_xplusy).sum(1) return self._H6
python
def H6(self): "Sum average." if not hasattr(self, '_H6'): self._H6 = ((self.rlevels2 + 2) * self.p_xplusy).sum(1) return self._H6
[ "def", "H6", "(", "self", ")", ":", "if", "not", "hasattr", "(", "self", ",", "'_H6'", ")", ":", "self", ".", "_H6", "=", "(", "(", "self", ".", "rlevels2", "+", "2", ")", "*", "self", ".", "p_xplusy", ")", ".", "sum", "(", "1", ")", "return"...
Sum average.
[ "Sum", "average", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L190-L194
17,817
CellProfiler/centrosome
centrosome/haralick.py
Haralick.H8
def H8(self): "Sum entropy." return -(self.p_xplusy * np.log(self.p_xplusy + self.eps)).sum(1)
python
def H8(self): "Sum entropy." return -(self.p_xplusy * np.log(self.p_xplusy + self.eps)).sum(1)
[ "def", "H8", "(", "self", ")", ":", "return", "-", "(", "self", ".", "p_xplusy", "*", "np", ".", "log", "(", "self", ".", "p_xplusy", "+", "self", ".", "eps", ")", ")", ".", "sum", "(", "1", ")" ]
Sum entropy.
[ "Sum", "entropy", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L201-L203
17,818
CellProfiler/centrosome
centrosome/haralick.py
Haralick.H10
def H10(self): "Difference variance." c = (self.rlevels * self.p_xminusy).sum(1) c1 = np.tile(c, (self.nlevels,1)).transpose() e = self.rlevels - c1 return (self.p_xminusy * e ** 2).sum(1)
python
def H10(self): "Difference variance." c = (self.rlevels * self.p_xminusy).sum(1) c1 = np.tile(c, (self.nlevels,1)).transpose() e = self.rlevels - c1 return (self.p_xminusy * e ** 2).sum(1)
[ "def", "H10", "(", "self", ")", ":", "c", "=", "(", "self", ".", "rlevels", "*", "self", ".", "p_xminusy", ")", ".", "sum", "(", "1", ")", "c1", "=", "np", ".", "tile", "(", "c", ",", "(", "self", ".", "nlevels", ",", "1", ")", ")", ".", ...
Difference variance.
[ "Difference", "variance", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L211-L216
17,819
CellProfiler/centrosome
centrosome/haralick.py
Haralick.H11
def H11(self): "Difference entropy." return -(self.p_xminusy * np.log(self.p_xminusy + self.eps)).sum(1)
python
def H11(self): "Difference entropy." return -(self.p_xminusy * np.log(self.p_xminusy + self.eps)).sum(1)
[ "def", "H11", "(", "self", ")", ":", "return", "-", "(", "self", ".", "p_xminusy", "*", "np", ".", "log", "(", "self", ".", "p_xminusy", "+", "self", ".", "eps", ")", ")", ".", "sum", "(", "1", ")" ]
Difference entropy.
[ "Difference", "entropy", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L218-L220
17,820
CellProfiler/centrosome
centrosome/haralick.py
Haralick.H12
def H12(self): "Information measure of correlation 1." maxima = np.vstack((self.hx, self.hy)).max(0) return (self.H9() - self.hxy1) / maxima
python
def H12(self): "Information measure of correlation 1." maxima = np.vstack((self.hx, self.hy)).max(0) return (self.H9() - self.hxy1) / maxima
[ "def", "H12", "(", "self", ")", ":", "maxima", "=", "np", ".", "vstack", "(", "(", "self", ".", "hx", ",", "self", ".", "hy", ")", ")", ".", "max", "(", "0", ")", "return", "(", "self", ".", "H9", "(", ")", "-", "self", ".", "hxy1", ")", ...
Information measure of correlation 1.
[ "Information", "measure", "of", "correlation", "1", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L222-L225
17,821
CellProfiler/centrosome
centrosome/haralick.py
Haralick.H13
def H13(self): "Information measure of correlation 2." # An imaginary result has been encountered once in the Matlab # version. The reason is unclear. return np.sqrt(1 - np.exp(-2 * (self.hxy2 - self.H9())))
python
def H13(self): "Information measure of correlation 2." # An imaginary result has been encountered once in the Matlab # version. The reason is unclear. return np.sqrt(1 - np.exp(-2 * (self.hxy2 - self.H9())))
[ "def", "H13", "(", "self", ")", ":", "# An imaginary result has been encountered once in the Matlab", "# version. The reason is unclear.", "return", "np", ".", "sqrt", "(", "1", "-", "np", ".", "exp", "(", "-", "2", "*", "(", "self", ".", "hxy2", "-", "self", ...
Information measure of correlation 2.
[ "Information", "measure", "of", "correlation", "2", "." ]
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/haralick.py#L227-L231
17,822
CellProfiler/centrosome
centrosome/zernike.py
construct_zernike_lookuptable
def construct_zernike_lookuptable(zernike_indexes): """Return a lookup table of the sum-of-factorial part of the radial polynomial of the zernike indexes passed zernike_indexes - an Nx2 array of the Zernike polynomials to be computed. """ n_max = np.max(zernike_indexes[:,0]) factorial = np.ones((1 + n_max,), dtype=float) factorial[1:] = np.cumproduct(np.arange(1, 1 + n_max, dtype=float)) width = int(n_max//2 + 1) lut = np.zeros((zernike_indexes.shape[0],width), dtype=float) for idx, (n, m) in enumerate(zernike_indexes): alt = 1 npmh = (n+m)//2 nmmh = (n-m)//2 for k in range(0,nmmh+1): lut[idx,k] = \ (alt * factorial[n-k] / (factorial[k]*factorial[npmh-k]*factorial[nmmh-k])) alt = -alt return lut
python
def construct_zernike_lookuptable(zernike_indexes): n_max = np.max(zernike_indexes[:,0]) factorial = np.ones((1 + n_max,), dtype=float) factorial[1:] = np.cumproduct(np.arange(1, 1 + n_max, dtype=float)) width = int(n_max//2 + 1) lut = np.zeros((zernike_indexes.shape[0],width), dtype=float) for idx, (n, m) in enumerate(zernike_indexes): alt = 1 npmh = (n+m)//2 nmmh = (n-m)//2 for k in range(0,nmmh+1): lut[idx,k] = \ (alt * factorial[n-k] / (factorial[k]*factorial[npmh-k]*factorial[nmmh-k])) alt = -alt return lut
[ "def", "construct_zernike_lookuptable", "(", "zernike_indexes", ")", ":", "n_max", "=", "np", ".", "max", "(", "zernike_indexes", "[", ":", ",", "0", "]", ")", "factorial", "=", "np", ".", "ones", "(", "(", "1", "+", "n_max", ",", ")", ",", "dtype", ...
Return a lookup table of the sum-of-factorial part of the radial polynomial of the zernike indexes passed zernike_indexes - an Nx2 array of the Zernike polynomials to be computed.
[ "Return", "a", "lookup", "table", "of", "the", "sum", "-", "of", "-", "factorial", "part", "of", "the", "radial", "polynomial", "of", "the", "zernike", "indexes", "passed", "zernike_indexes", "-", "an", "Nx2", "array", "of", "the", "Zernike", "polynomials", ...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/zernike.py#L13-L34
17,823
CellProfiler/centrosome
centrosome/zernike.py
score_zernike
def score_zernike(zf, radii, labels, indexes=None): """Score the output of construct_zernike_polynomials zf - the output of construct_zernike_polynomials which is I x J x K where K is the number of zernike polynomials computed radii - a vector of the radius of each of N labeled objects labels - a label matrix outputs a N x K matrix of the scores of each of the Zernikes for each labeled object. """ if indexes is None: indexes = np.arange(1,np.max(labels)+1,dtype=np.int32) else: indexes = np.array(indexes, dtype=np.int32) radii = np.asarray(radii, dtype=float) n = radii.size k = zf.shape[2] score = np.zeros((n,k)) if n == 0: return score areas = np.square(radii) areas *= np.pi for ki in range(k): zfk=zf[:,:,ki] real_score = scipy.ndimage.sum(zfk.real,labels,indexes) real_score = fixup_scipy_ndimage_result(real_score) imag_score = scipy.ndimage.sum(zfk.imag,labels,indexes) imag_score = fixup_scipy_ndimage_result(imag_score) # one_score = np.sqrt(real_score**2+imag_score**2) / areas np.square(real_score, out=real_score) np.square(imag_score, out=imag_score) one_score = real_score + imag_score np.sqrt(one_score, out=one_score) one_score /= areas score[:,ki] = one_score return score
python
def score_zernike(zf, radii, labels, indexes=None): if indexes is None: indexes = np.arange(1,np.max(labels)+1,dtype=np.int32) else: indexes = np.array(indexes, dtype=np.int32) radii = np.asarray(radii, dtype=float) n = radii.size k = zf.shape[2] score = np.zeros((n,k)) if n == 0: return score areas = np.square(radii) areas *= np.pi for ki in range(k): zfk=zf[:,:,ki] real_score = scipy.ndimage.sum(zfk.real,labels,indexes) real_score = fixup_scipy_ndimage_result(real_score) imag_score = scipy.ndimage.sum(zfk.imag,labels,indexes) imag_score = fixup_scipy_ndimage_result(imag_score) # one_score = np.sqrt(real_score**2+imag_score**2) / areas np.square(real_score, out=real_score) np.square(imag_score, out=imag_score) one_score = real_score + imag_score np.sqrt(one_score, out=one_score) one_score /= areas score[:,ki] = one_score return score
[ "def", "score_zernike", "(", "zf", ",", "radii", ",", "labels", ",", "indexes", "=", "None", ")", ":", "if", "indexes", "is", "None", ":", "indexes", "=", "np", ".", "arange", "(", "1", ",", "np", ".", "max", "(", "labels", ")", "+", "1", ",", ...
Score the output of construct_zernike_polynomials zf - the output of construct_zernike_polynomials which is I x J x K where K is the number of zernike polynomials computed radii - a vector of the radius of each of N labeled objects labels - a label matrix outputs a N x K matrix of the scores of each of the Zernikes for each labeled object.
[ "Score", "the", "output", "of", "construct_zernike_polynomials", "zf", "-", "the", "output", "of", "construct_zernike_polynomials", "which", "is", "I", "x", "J", "x", "K", "where", "K", "is", "the", "number", "of", "zernike", "polynomials", "computed", "radii", ...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/zernike.py#L104-L141
17,824
CellProfiler/centrosome
centrosome/lapjv.py
slow_augmenting_row_reduction
def slow_augmenting_row_reduction(n, ii, jj, idx, count, x, y, u, v, c): '''Perform the augmenting row reduction step from the Jonker-Volgenaut algorithm n - the number of i and j in the linear assignment problem ii - the unassigned i jj - the j-index of every entry in c idx - the index of the first entry for each i count - the number of entries for each i x - the assignment of j to i y - the assignment of i to j u - the dual variable "u" which will be updated. It should be initialized to zero for the first reduction transfer. v - the dual variable "v" which will be reduced in-place c - the cost for each entry. returns the new unassigned i ''' ####################################### # # From Jonker: # # procedure AUGMENTING ROW REDUCTION; # begin # LIST: = {all unassigned rows}; # for all i in LIST do # repeat # ul:=min {c[i,j]-v[j] for j=l ...n}; # select j1 with c [i,j 1] - v[j 1] = u1; # u2:=min {c[i,j]-v[j] for j=l ...n,j< >jl} ; # select j2 with c [i,j2] - v [j2] = u2 and j2 < >j 1 ; # u[i]:=u2; # if ul <u2 then v[jl]:=v[jl]-(u2-ul) # else if jl is assigned then jl : =j2; # k:=y [jl]; if k>0 then x [k]:=0; x[i]:=jl; y [ j l ] : = i ; i:=k # until ul =u2 (* no reduction transfer *) or k=0 i~* augmentation *) # end ii = list(ii) k = 0 limit = len(ii) free = [] while k < limit: i = ii[k] k += 1 j = jj[idx[i]:(idx[i] + count[i])] uu = c[idx[i]:(idx[i] + count[i])] - v[j] order = np.lexsort([uu]) u1, u2 = uu[order[:2]] j1,j2 = j[order[:2]] i1 = y[j1] if u1 < u2: v[j1] = v[j1] - u2 + u1 elif i1 != n: j1 = j2 i1 = y[j1] if i1 != n: if u1 < u2: k -= 1 ii[k] = i1 else: free.append(i1) x[i] = j1 y[j1] = i return np.array(free,np.uint32)
python
def slow_augmenting_row_reduction(n, ii, jj, idx, count, x, y, u, v, c): '''Perform the augmenting row reduction step from the Jonker-Volgenaut algorithm n - the number of i and j in the linear assignment problem ii - the unassigned i jj - the j-index of every entry in c idx - the index of the first entry for each i count - the number of entries for each i x - the assignment of j to i y - the assignment of i to j u - the dual variable "u" which will be updated. It should be initialized to zero for the first reduction transfer. v - the dual variable "v" which will be reduced in-place c - the cost for each entry. returns the new unassigned i ''' ####################################### # # From Jonker: # # procedure AUGMENTING ROW REDUCTION; # begin # LIST: = {all unassigned rows}; # for all i in LIST do # repeat # ul:=min {c[i,j]-v[j] for j=l ...n}; # select j1 with c [i,j 1] - v[j 1] = u1; # u2:=min {c[i,j]-v[j] for j=l ...n,j< >jl} ; # select j2 with c [i,j2] - v [j2] = u2 and j2 < >j 1 ; # u[i]:=u2; # if ul <u2 then v[jl]:=v[jl]-(u2-ul) # else if jl is assigned then jl : =j2; # k:=y [jl]; if k>0 then x [k]:=0; x[i]:=jl; y [ j l ] : = i ; i:=k # until ul =u2 (* no reduction transfer *) or k=0 i~* augmentation *) # end ii = list(ii) k = 0 limit = len(ii) free = [] while k < limit: i = ii[k] k += 1 j = jj[idx[i]:(idx[i] + count[i])] uu = c[idx[i]:(idx[i] + count[i])] - v[j] order = np.lexsort([uu]) u1, u2 = uu[order[:2]] j1,j2 = j[order[:2]] i1 = y[j1] if u1 < u2: v[j1] = v[j1] - u2 + u1 elif i1 != n: j1 = j2 i1 = y[j1] if i1 != n: if u1 < u2: k -= 1 ii[k] = i1 else: free.append(i1) x[i] = j1 y[j1] = i return np.array(free,np.uint32)
[ "def", "slow_augmenting_row_reduction", "(", "n", ",", "ii", ",", "jj", ",", "idx", ",", "count", ",", "x", ",", "y", ",", "u", ",", "v", ",", "c", ")", ":", "#######################################", "#", "# From Jonker:", "#", "# procedure AUGMENTING ROW RED...
Perform the augmenting row reduction step from the Jonker-Volgenaut algorithm n - the number of i and j in the linear assignment problem ii - the unassigned i jj - the j-index of every entry in c idx - the index of the first entry for each i count - the number of entries for each i x - the assignment of j to i y - the assignment of i to j u - the dual variable "u" which will be updated. It should be initialized to zero for the first reduction transfer. v - the dual variable "v" which will be reduced in-place c - the cost for each entry. returns the new unassigned i
[ "Perform", "the", "augmenting", "row", "reduction", "step", "from", "the", "Jonker", "-", "Volgenaut", "algorithm", "n", "-", "the", "number", "of", "i", "and", "j", "in", "the", "linear", "assignment", "problem", "ii", "-", "the", "unassigned", "i", "jj",...
7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a
https://github.com/CellProfiler/centrosome/blob/7bd9350a2d4ae1b215b81eabcecfe560bbb1f32a/centrosome/lapjv.py#L172-L235
17,825
koszullab/instaGRAAL
instagraal/linkage.py
collapse_degenerate_markers
def collapse_degenerate_markers(linkage_records): """Group all markers with no genetic distance as distinct features to generate a BED file with. Simple example with sixteen degenerate markers: >>> marker_features = [ ... ['36915_sctg_207_31842', 1, 0, 207, 31842], ... ['36941_sctg_207_61615', 1, 0, 207, 61615], ... ['36956_sctg_207_77757', 1, 0, 207, 77757], ... ['36957_sctg_207_78332', 1, 0, 207, 78332], ... ['36972_sctg_207_94039', 1, 0, 207, 94039], ... ['36788_sctg_207_116303', 1, 0.652, 207, 116303], ... ['36812_sctg_207_158925', 1, 1.25, 207, 158925], ... ['36819_sctg_207_165424', 1, 1.25, 207, 165424], ... ['36828_sctg_207_190813', 1, 1.25, 207, 190813], ... ['36830_sctg_207_191645', 1, 1.25, 207, 191645], ... ['36834_sctg_207_195961', 1, 1.25, 207, 195961], ... ['36855_sctg_207_233632', 1, 1.25, 207, 233632], ... ['36881_sctg_207_258658', 1, 1.25, 207, 258658], ... ['82072_sctg_486_41893', 1, 3.756, 486, 41893], ... ['85634_sctg_516_36614', 1, 3.756, 516, 36614], ... ['85638_sctg_516_50582', 1, 3.756, 516, 50582]] >>> len(marker_features) 16 >>> collapsed_features = collapse_degenerate_markers(marker_features) >>> len(collapsed_features) 5 The degenerate features (identical linkage group, genetic distance and original scaffold) are collapsed into a region: >>> collapsed_features[0] [1, 31842, 94039, 207] The format is [linkage group, start, end, original scaffold]. If a singleton (non-degenerate) feature is found, the region is simply a single point in the genome: >>> collapsed_features[1] [1, 116303, 116303, 207] so 'start' and 'end' are identical. Two markers are not considered degenerate if they belong to different original scaffolds, even if they are in terms of genetic linkage: >>> collapsed_features[2] [1, 158925, 258658, 207] >>> collapsed_features[3:] [[1, 41893, 41893, 486], [1, 36614, 50582, 516]] """ def degeneracy(linkage_record): linkage_group, genetic_distance, scaffold = ( linkage_record[1], linkage_record[2], linkage_record[3], ) return (linkage_group, genetic_distance, scaffold) degenerate_records = [] for _, degenerate_group in itertools.groupby( linkage_records, key=degeneracy ): group_list = list(degenerate_group) start_record, end_record = group_list[0], group_list[-1] assert (start_record[1], start_record[2], start_record[3]) == ( end_record[1], end_record[2], end_record[3], ) start_position = start_record[-1] end_position = end_record[-1] scaffold = start_record[3] linkage_group = start_record[1] record = [linkage_group, start_position, end_position, scaffold] degenerate_records.append(record) return degenerate_records
python
def collapse_degenerate_markers(linkage_records): def degeneracy(linkage_record): linkage_group, genetic_distance, scaffold = ( linkage_record[1], linkage_record[2], linkage_record[3], ) return (linkage_group, genetic_distance, scaffold) degenerate_records = [] for _, degenerate_group in itertools.groupby( linkage_records, key=degeneracy ): group_list = list(degenerate_group) start_record, end_record = group_list[0], group_list[-1] assert (start_record[1], start_record[2], start_record[3]) == ( end_record[1], end_record[2], end_record[3], ) start_position = start_record[-1] end_position = end_record[-1] scaffold = start_record[3] linkage_group = start_record[1] record = [linkage_group, start_position, end_position, scaffold] degenerate_records.append(record) return degenerate_records
[ "def", "collapse_degenerate_markers", "(", "linkage_records", ")", ":", "def", "degeneracy", "(", "linkage_record", ")", ":", "linkage_group", ",", "genetic_distance", ",", "scaffold", "=", "(", "linkage_record", "[", "1", "]", ",", "linkage_record", "[", "2", "...
Group all markers with no genetic distance as distinct features to generate a BED file with. Simple example with sixteen degenerate markers: >>> marker_features = [ ... ['36915_sctg_207_31842', 1, 0, 207, 31842], ... ['36941_sctg_207_61615', 1, 0, 207, 61615], ... ['36956_sctg_207_77757', 1, 0, 207, 77757], ... ['36957_sctg_207_78332', 1, 0, 207, 78332], ... ['36972_sctg_207_94039', 1, 0, 207, 94039], ... ['36788_sctg_207_116303', 1, 0.652, 207, 116303], ... ['36812_sctg_207_158925', 1, 1.25, 207, 158925], ... ['36819_sctg_207_165424', 1, 1.25, 207, 165424], ... ['36828_sctg_207_190813', 1, 1.25, 207, 190813], ... ['36830_sctg_207_191645', 1, 1.25, 207, 191645], ... ['36834_sctg_207_195961', 1, 1.25, 207, 195961], ... ['36855_sctg_207_233632', 1, 1.25, 207, 233632], ... ['36881_sctg_207_258658', 1, 1.25, 207, 258658], ... ['82072_sctg_486_41893', 1, 3.756, 486, 41893], ... ['85634_sctg_516_36614', 1, 3.756, 516, 36614], ... ['85638_sctg_516_50582', 1, 3.756, 516, 50582]] >>> len(marker_features) 16 >>> collapsed_features = collapse_degenerate_markers(marker_features) >>> len(collapsed_features) 5 The degenerate features (identical linkage group, genetic distance and original scaffold) are collapsed into a region: >>> collapsed_features[0] [1, 31842, 94039, 207] The format is [linkage group, start, end, original scaffold]. If a singleton (non-degenerate) feature is found, the region is simply a single point in the genome: >>> collapsed_features[1] [1, 116303, 116303, 207] so 'start' and 'end' are identical. Two markers are not considered degenerate if they belong to different original scaffolds, even if they are in terms of genetic linkage: >>> collapsed_features[2] [1, 158925, 258658, 207] >>> collapsed_features[3:] [[1, 41893, 41893, 486], [1, 36614, 50582, 516]]
[ "Group", "all", "markers", "with", "no", "genetic", "distance", "as", "distinct", "features", "to", "generate", "a", "BED", "file", "with", "." ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/linkage.py#L36-L123
17,826
koszullab/instaGRAAL
instagraal/linkage.py
linkage_group_ordering
def linkage_group_ordering(linkage_records): """Convert degenerate linkage records into ordered info_frags-like records for comparison purposes. Simple example: >>> linkage_records = [ ... ['linkage_group_1', 31842, 94039, 'sctg_207'], ... ['linkage_group_1', 95303, 95303, 'sctg_207'], ... ['linkage_group_2', 15892, 25865, 'sctg_308'], ... ['linkage_group_2', 41893, 41893, 'sctg_486'], ... ['linkage_group_3', 36614, 50582, 'sctg_516'], ... ] >>> ordering = linkage_group_ordering(linkage_records) Each key of the record is a newly-formed 'scaffold' (linkage group): >>> sorted(ordering.keys()) ['linkage_group_1', 'linkage_group_2', 'linkage_group_3'] Records are in the form [init_contig, frag_id, start, end, orientation]. Since fragment ids are meaningless in non-HiC contexts a negative identifier is set so it is understood that region was added due to linkage data (-1 is for recovering data after first-pass polishing and -2 is for sequence insertions after long read based polishing). >>> ordering['linkage_group_1'] [['sctg_207', -3, 31842, 94039, 1], ['sctg_207', -3, 95303, 95303, 1]] >>> ordering['linkage_group_2'] [['sctg_308', -3, 15892, 25865, 1], ['sctg_486', -3, 41893, 41893, 1]] Orientations are always set to 1 by default. >>> ordering['linkage_group_3'] [['sctg_516', -3, 36614, 50582, 1]] """ new_records = dict() for lg_name, linkage_group in itertools.groupby( linkage_records, operator.itemgetter(0) ): new_records[lg_name] = [] for record in linkage_group: init_contig = record[-1] start = record[1] end = record[2] new_record = [init_contig, -3, start, end, 1] new_records[lg_name].append(new_record) return new_records
python
def linkage_group_ordering(linkage_records): new_records = dict() for lg_name, linkage_group in itertools.groupby( linkage_records, operator.itemgetter(0) ): new_records[lg_name] = [] for record in linkage_group: init_contig = record[-1] start = record[1] end = record[2] new_record = [init_contig, -3, start, end, 1] new_records[lg_name].append(new_record) return new_records
[ "def", "linkage_group_ordering", "(", "linkage_records", ")", ":", "new_records", "=", "dict", "(", ")", "for", "lg_name", ",", "linkage_group", "in", "itertools", ".", "groupby", "(", "linkage_records", ",", "operator", ".", "itemgetter", "(", "0", ")", ")", ...
Convert degenerate linkage records into ordered info_frags-like records for comparison purposes. Simple example: >>> linkage_records = [ ... ['linkage_group_1', 31842, 94039, 'sctg_207'], ... ['linkage_group_1', 95303, 95303, 'sctg_207'], ... ['linkage_group_2', 15892, 25865, 'sctg_308'], ... ['linkage_group_2', 41893, 41893, 'sctg_486'], ... ['linkage_group_3', 36614, 50582, 'sctg_516'], ... ] >>> ordering = linkage_group_ordering(linkage_records) Each key of the record is a newly-formed 'scaffold' (linkage group): >>> sorted(ordering.keys()) ['linkage_group_1', 'linkage_group_2', 'linkage_group_3'] Records are in the form [init_contig, frag_id, start, end, orientation]. Since fragment ids are meaningless in non-HiC contexts a negative identifier is set so it is understood that region was added due to linkage data (-1 is for recovering data after first-pass polishing and -2 is for sequence insertions after long read based polishing). >>> ordering['linkage_group_1'] [['sctg_207', -3, 31842, 94039, 1], ['sctg_207', -3, 95303, 95303, 1]] >>> ordering['linkage_group_2'] [['sctg_308', -3, 15892, 25865, 1], ['sctg_486', -3, 41893, 41893, 1]] Orientations are always set to 1 by default. >>> ordering['linkage_group_3'] [['sctg_516', -3, 36614, 50582, 1]]
[ "Convert", "degenerate", "linkage", "records", "into", "ordered", "info_frags", "-", "like", "records", "for", "comparison", "purposes", "." ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/linkage.py#L133-L187
17,827
koszullab/instaGRAAL
instagraal/linkage.py
compare_orderings
def compare_orderings(info_frags_records, linkage_orderings): """Given linkage groups and info_frags records, link pseudo-chromosomes to scaffolds based on the initial contig composition of each group. Because info_frags records are usually richer and may contain contigs not found in linkage groups, those extra sequences are discarded. Example with two linkage groups and two chromosomes: >>> linkage_orderings = { ... 'linkage_group_1': [ ... ['sctg_516', -3, 36614, 50582, 1], ... ['sctg_486', -3, 41893, 41893, 1], ... ['sctg_486', -3, 50054, 62841, 1], ... ['sctg_207', -3, 31842, 94039, 1], ... ['sctg_558', -3, 51212, 54212, 1], ... ], ... 'linkage_group_2': [ ... ['sctg_308', -3, 15892, 25865, 1], ... ['sctg_842', -3, 0, 8974, 1], ... ['sctg_994', -3, 0, 81213, 1], ... ], ... } >>> info_frags = { ... 'scaffold_A': [ ... ['sctg_308', 996, 15892, 25865, 1], ... ['sctg_778', 1210, 45040, 78112, -1], ... ['sctg_842', 124, 0, 8974, 1], ... ], ... 'scaffold_B': [ ... ['sctg_516', 5, 0, 38000, 1], ... ['sctg_486', 47, 42050, 49000, 1], ... ['sctg_1755', 878, 95001, 10844, -1], ... ['sctg_842', 126, 19000, 26084, 1], ... ['sctg_207', 705, 45500, 87056, 1], ... ], ... 'scaffold_C': [ ... ['sctg_558', 745, 50045, 67851, 1], ... ['sctg_994', 12, 74201, 86010, -1], ... ], ... } >>> matching_pairs = compare_orderings(info_frags, linkage_orderings) >>> matching_pairs['scaffold_B'] (3, 'linkage_group_1', {'sctg_558': 'sctg_207'}) >>> matching_pairs['scaffold_A'] (2, 'linkage_group_2', {'sctg_994': 'sctg_842'}) """ scaffolds = info_frags_records.keys() linkage_groups = linkage_orderings.keys() best_matching_table = dict() for scaffold, linkage_group in itertools.product( scaffolds, linkage_groups ): lg_ordering = [ init_contig for init_contig, _ in itertools.groupby( linkage_orderings[linkage_group], operator.itemgetter(0) ) ] scaffold_ordering = [ init_contig for init_contig, bin_group in itertools.groupby( info_frags_records[scaffold], operator.itemgetter(0) ) if init_contig in lg_ordering ] overlap = set(lg_ordering).intersection(set(scaffold_ordering)) missing_locations = dict() for missing_block in sorted(set(lg_ordering) - set(overlap)): for i, init_contig in enumerate(lg_ordering): if init_contig == missing_block: try: block_before = lg_ordering[i - 1] except IndexError: block_before = "beginning" missing_locations[missing_block] = block_before try: if len(overlap) > best_matching_table[scaffold][0]: best_matching_table[scaffold] = ( len(overlap), linkage_group, missing_locations, ) except KeyError: best_matching_table[scaffold] = ( len(overlap), linkage_group, missing_locations, ) return best_matching_table
python
def compare_orderings(info_frags_records, linkage_orderings): scaffolds = info_frags_records.keys() linkage_groups = linkage_orderings.keys() best_matching_table = dict() for scaffold, linkage_group in itertools.product( scaffolds, linkage_groups ): lg_ordering = [ init_contig for init_contig, _ in itertools.groupby( linkage_orderings[linkage_group], operator.itemgetter(0) ) ] scaffold_ordering = [ init_contig for init_contig, bin_group in itertools.groupby( info_frags_records[scaffold], operator.itemgetter(0) ) if init_contig in lg_ordering ] overlap = set(lg_ordering).intersection(set(scaffold_ordering)) missing_locations = dict() for missing_block in sorted(set(lg_ordering) - set(overlap)): for i, init_contig in enumerate(lg_ordering): if init_contig == missing_block: try: block_before = lg_ordering[i - 1] except IndexError: block_before = "beginning" missing_locations[missing_block] = block_before try: if len(overlap) > best_matching_table[scaffold][0]: best_matching_table[scaffold] = ( len(overlap), linkage_group, missing_locations, ) except KeyError: best_matching_table[scaffold] = ( len(overlap), linkage_group, missing_locations, ) return best_matching_table
[ "def", "compare_orderings", "(", "info_frags_records", ",", "linkage_orderings", ")", ":", "scaffolds", "=", "info_frags_records", ".", "keys", "(", ")", "linkage_groups", "=", "linkage_orderings", ".", "keys", "(", ")", "best_matching_table", "=", "dict", "(", ")...
Given linkage groups and info_frags records, link pseudo-chromosomes to scaffolds based on the initial contig composition of each group. Because info_frags records are usually richer and may contain contigs not found in linkage groups, those extra sequences are discarded. Example with two linkage groups and two chromosomes: >>> linkage_orderings = { ... 'linkage_group_1': [ ... ['sctg_516', -3, 36614, 50582, 1], ... ['sctg_486', -3, 41893, 41893, 1], ... ['sctg_486', -3, 50054, 62841, 1], ... ['sctg_207', -3, 31842, 94039, 1], ... ['sctg_558', -3, 51212, 54212, 1], ... ], ... 'linkage_group_2': [ ... ['sctg_308', -3, 15892, 25865, 1], ... ['sctg_842', -3, 0, 8974, 1], ... ['sctg_994', -3, 0, 81213, 1], ... ], ... } >>> info_frags = { ... 'scaffold_A': [ ... ['sctg_308', 996, 15892, 25865, 1], ... ['sctg_778', 1210, 45040, 78112, -1], ... ['sctg_842', 124, 0, 8974, 1], ... ], ... 'scaffold_B': [ ... ['sctg_516', 5, 0, 38000, 1], ... ['sctg_486', 47, 42050, 49000, 1], ... ['sctg_1755', 878, 95001, 10844, -1], ... ['sctg_842', 126, 19000, 26084, 1], ... ['sctg_207', 705, 45500, 87056, 1], ... ], ... 'scaffold_C': [ ... ['sctg_558', 745, 50045, 67851, 1], ... ['sctg_994', 12, 74201, 86010, -1], ... ], ... } >>> matching_pairs = compare_orderings(info_frags, linkage_orderings) >>> matching_pairs['scaffold_B'] (3, 'linkage_group_1', {'sctg_558': 'sctg_207'}) >>> matching_pairs['scaffold_A'] (2, 'linkage_group_2', {'sctg_994': 'sctg_842'})
[ "Given", "linkage", "groups", "and", "info_frags", "records", "link", "pseudo", "-", "chromosomes", "to", "scaffolds", "based", "on", "the", "initial", "contig", "composition", "of", "each", "group", ".", "Because", "info_frags", "records", "are", "usually", "ri...
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/linkage.py#L190-L287
17,828
koszullab/instaGRAAL
instagraal/linkage.py
get_missing_blocks
def get_missing_blocks(info_frags_records, matching_pairs, linkage_orderings): """Get missing blocks in a scaffold based on the genetic map order. Given matching scaffold blocks/genetic map blocks (based on restriction sites and SNP markers, respectively), move around the scaffold blocks such that they map the genetic map order. Parameters ---------- info_frags_records : dict A dictionary representing the scaffolds and their block order as described in an info_frags.txt file matching_pairs : dict A list of best matching pairs in the form (scaffold_block, linkage group) linkage_orderings : dict A dictionary representing the genetic map and the linkage groups as described in csv file. Example ------- >>> linkage_orderings = { ... 'linkage_group_1': [ ... ['sctg_516', -3, 36614, 50582, 1], ... ['sctg_486', -3, 41893, 41893, 1], ... ['sctg_486', -3, 50054, 62841, 1], ... ['sctg_207', -3, 31842, 94039, 1], ... ['sctg_558', -3, 51212, 54212, 1], ... ], ... 'linkage_group_2': [ ... ['sctg_308', -3, 15892, 25865, 1], ... ['sctg_842', -3, 0, 8974, 1], ... ['sctg_994', -3, 0, 81213, 1], ... ], ... } >>> info_frags = { ... 'scaffold_A': [ ... ['sctg_308', 996, 15892, 25865, 1], ... ['sctg_778', 1210, 45040, 78112, -1], ... ['sctg_842', 124, 0, 8974, 1], ... ], ... 'scaffold_B': [ ... ['sctg_516', 5, 0, 38000, 1], ... ['sctg_486', 47, 42050, 49000, 1], ... ['sctg_1755', 878, 95001, 10844, -1], ... ['sctg_842', 126, 19000, 26084, 1], ... ['sctg_207', 705, 45500, 87056, 1], ... ], ... 'scaffold_C': [ ... ['sctg_558', 745, 50045, 67851, 1], ... ['sctg_994', 12, 74201, 86010, -1], ... ], ... } >>> matching_pairs = compare_orderings(info_frags, linkage_orderings) >>> new_records = get_missing_blocks(info_frags, matching_pairs, ... linkage_orderings) >>> for my_bin in new_records['scaffold_A']: ... print(list(my_bin)) ... ['sctg_308', 996, 15892, 25865, 1] ['sctg_778', 1210, 45040, 78112, -1] ['sctg_842', 124, 0, 8974, 1] ['sctg_842', 126, 19000, 26084, 1] ['sctg_994', 12, 74201, 86010, -1] >>> for my_bin in new_records['scaffold_B']: ... print(list(my_bin)) ... ['sctg_516', 5, 0, 38000, 1] ['sctg_486', 47, 42050, 49000, 1] ['sctg_1755', 878, 95001, 10844, -1] ['sctg_207', 705, 45500, 87056, 1] ['sctg_558', 745, 50045, 67851, 1] """ import logging import sys logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) logger = logging.getLogger() logger.setLevel(logging.DEBUG) touched_lgs = set() def record_length(key_record_tuple): return len(key_record_tuple[1]) new_scaffolds = copy.deepcopy(info_frags_records) for scaffold_name in collections.OrderedDict( sorted(new_scaffolds.items(), key=record_length, reverse=True) ): scaffold = new_scaffolds[scaffold_name] new_scaffold = [] corresponding_lg = matching_pairs[scaffold_name][1] if corresponding_lg in touched_lgs: continue else: touched_lgs.add(corresponding_lg) scaffold_block_names = {my_bin[0] for my_bin in scaffold} lg_block_names = [ my_block[0] for my_block in linkage_orderings[corresponding_lg] ] touched_bins = set() for lg_block_name in lg_block_names: if lg_block_name in scaffold_block_names: for my_bin in scaffold: if tuple(my_bin) in new_scaffold: continue elif ( my_bin[0] == lg_block_name or my_bin[0] not in lg_block_names ): new_scaffold.append(tuple(my_bin)) touched_bins.add(tuple(my_bin)) else: break for other_name, other_scaffold in new_scaffolds.items(): if other_name == scaffold_name: continue i = 0 for my_bin in other_scaffold: if tuple(my_bin) in new_scaffold: i += 1 continue elif my_bin[0] == lg_block_name: moved_bin = tuple(other_scaffold.pop(i)) new_scaffold.append(tuple(moved_bin)) touched_bins.add(tuple(moved_bin)) i -= 1 i += 1 for remaining_bin in scaffold: if tuple(remaining_bin) not in touched_bins: new_scaffold.append(tuple(remaining_bin)) touched_bins.add(tuple(remaining_bin)) if len(new_scaffold) > 0: new_scaffolds[scaffold_name] = new_scaffold return new_scaffolds
python
def get_missing_blocks(info_frags_records, matching_pairs, linkage_orderings): import logging import sys logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) logger = logging.getLogger() logger.setLevel(logging.DEBUG) touched_lgs = set() def record_length(key_record_tuple): return len(key_record_tuple[1]) new_scaffolds = copy.deepcopy(info_frags_records) for scaffold_name in collections.OrderedDict( sorted(new_scaffolds.items(), key=record_length, reverse=True) ): scaffold = new_scaffolds[scaffold_name] new_scaffold = [] corresponding_lg = matching_pairs[scaffold_name][1] if corresponding_lg in touched_lgs: continue else: touched_lgs.add(corresponding_lg) scaffold_block_names = {my_bin[0] for my_bin in scaffold} lg_block_names = [ my_block[0] for my_block in linkage_orderings[corresponding_lg] ] touched_bins = set() for lg_block_name in lg_block_names: if lg_block_name in scaffold_block_names: for my_bin in scaffold: if tuple(my_bin) in new_scaffold: continue elif ( my_bin[0] == lg_block_name or my_bin[0] not in lg_block_names ): new_scaffold.append(tuple(my_bin)) touched_bins.add(tuple(my_bin)) else: break for other_name, other_scaffold in new_scaffolds.items(): if other_name == scaffold_name: continue i = 0 for my_bin in other_scaffold: if tuple(my_bin) in new_scaffold: i += 1 continue elif my_bin[0] == lg_block_name: moved_bin = tuple(other_scaffold.pop(i)) new_scaffold.append(tuple(moved_bin)) touched_bins.add(tuple(moved_bin)) i -= 1 i += 1 for remaining_bin in scaffold: if tuple(remaining_bin) not in touched_bins: new_scaffold.append(tuple(remaining_bin)) touched_bins.add(tuple(remaining_bin)) if len(new_scaffold) > 0: new_scaffolds[scaffold_name] = new_scaffold return new_scaffolds
[ "def", "get_missing_blocks", "(", "info_frags_records", ",", "matching_pairs", ",", "linkage_orderings", ")", ":", "import", "logging", "import", "sys", "logging", ".", "basicConfig", "(", "stream", "=", "sys", ".", "stdout", ",", "level", "=", "logging", ".", ...
Get missing blocks in a scaffold based on the genetic map order. Given matching scaffold blocks/genetic map blocks (based on restriction sites and SNP markers, respectively), move around the scaffold blocks such that they map the genetic map order. Parameters ---------- info_frags_records : dict A dictionary representing the scaffolds and their block order as described in an info_frags.txt file matching_pairs : dict A list of best matching pairs in the form (scaffold_block, linkage group) linkage_orderings : dict A dictionary representing the genetic map and the linkage groups as described in csv file. Example ------- >>> linkage_orderings = { ... 'linkage_group_1': [ ... ['sctg_516', -3, 36614, 50582, 1], ... ['sctg_486', -3, 41893, 41893, 1], ... ['sctg_486', -3, 50054, 62841, 1], ... ['sctg_207', -3, 31842, 94039, 1], ... ['sctg_558', -3, 51212, 54212, 1], ... ], ... 'linkage_group_2': [ ... ['sctg_308', -3, 15892, 25865, 1], ... ['sctg_842', -3, 0, 8974, 1], ... ['sctg_994', -3, 0, 81213, 1], ... ], ... } >>> info_frags = { ... 'scaffold_A': [ ... ['sctg_308', 996, 15892, 25865, 1], ... ['sctg_778', 1210, 45040, 78112, -1], ... ['sctg_842', 124, 0, 8974, 1], ... ], ... 'scaffold_B': [ ... ['sctg_516', 5, 0, 38000, 1], ... ['sctg_486', 47, 42050, 49000, 1], ... ['sctg_1755', 878, 95001, 10844, -1], ... ['sctg_842', 126, 19000, 26084, 1], ... ['sctg_207', 705, 45500, 87056, 1], ... ], ... 'scaffold_C': [ ... ['sctg_558', 745, 50045, 67851, 1], ... ['sctg_994', 12, 74201, 86010, -1], ... ], ... } >>> matching_pairs = compare_orderings(info_frags, linkage_orderings) >>> new_records = get_missing_blocks(info_frags, matching_pairs, ... linkage_orderings) >>> for my_bin in new_records['scaffold_A']: ... print(list(my_bin)) ... ['sctg_308', 996, 15892, 25865, 1] ['sctg_778', 1210, 45040, 78112, -1] ['sctg_842', 124, 0, 8974, 1] ['sctg_842', 126, 19000, 26084, 1] ['sctg_994', 12, 74201, 86010, -1] >>> for my_bin in new_records['scaffold_B']: ... print(list(my_bin)) ... ['sctg_516', 5, 0, 38000, 1] ['sctg_486', 47, 42050, 49000, 1] ['sctg_1755', 878, 95001, 10844, -1] ['sctg_207', 705, 45500, 87056, 1] ['sctg_558', 745, 50045, 67851, 1]
[ "Get", "missing", "blocks", "in", "a", "scaffold", "based", "on", "the", "genetic", "map", "order", "." ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/linkage.py#L290-L435
17,829
koszullab/instaGRAAL
instagraal/parse_info_frags.py
parse_info_frags
def parse_info_frags(info_frags): """Import an info_frags.txt file and return a dictionary where each key is a newly formed scaffold and each value is the list of bins and their origin on the initial scaffolding. """ new_scaffolds = {} with open(info_frags, "r") as info_frags_handle: current_new_contig = None for line in info_frags_handle: if line.startswith(">"): current_new_contig = str(line[1:-1]) new_scaffolds[current_new_contig] = [] elif line.startswith("init_contig"): pass else: (init_contig, id_frag, orientation, pos_start, pos_end) = str( line[:-1] ).split("\t") start = int(pos_start) end = int(pos_end) ori = int(orientation) fragid = int(id_frag) assert start < end assert ori in {-1, 1} new_scaffolds[current_new_contig].append( [init_contig, fragid, start, end, ori] ) return new_scaffolds
python
def parse_info_frags(info_frags): new_scaffolds = {} with open(info_frags, "r") as info_frags_handle: current_new_contig = None for line in info_frags_handle: if line.startswith(">"): current_new_contig = str(line[1:-1]) new_scaffolds[current_new_contig] = [] elif line.startswith("init_contig"): pass else: (init_contig, id_frag, orientation, pos_start, pos_end) = str( line[:-1] ).split("\t") start = int(pos_start) end = int(pos_end) ori = int(orientation) fragid = int(id_frag) assert start < end assert ori in {-1, 1} new_scaffolds[current_new_contig].append( [init_contig, fragid, start, end, ori] ) return new_scaffolds
[ "def", "parse_info_frags", "(", "info_frags", ")", ":", "new_scaffolds", "=", "{", "}", "with", "open", "(", "info_frags", ",", "\"r\"", ")", "as", "info_frags_handle", ":", "current_new_contig", "=", "None", "for", "line", "in", "info_frags_handle", ":", "if"...
Import an info_frags.txt file and return a dictionary where each key is a newly formed scaffold and each value is the list of bins and their origin on the initial scaffolding.
[ "Import", "an", "info_frags", ".", "txt", "file", "and", "return", "a", "dictionary", "where", "each", "key", "is", "a", "newly", "formed", "scaffold", "and", "each", "value", "is", "the", "list", "of", "bins", "and", "their", "origin", "on", "the", "ini...
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/parse_info_frags.py#L39-L68
17,830
koszullab/instaGRAAL
instagraal/parse_info_frags.py
format_info_frags
def format_info_frags(info_frags): """A function to seamlessly run on either scaffold dictionaries or info_frags.txt files without having to check the input first. """ if isinstance(info_frags, dict): return info_frags else: try: scaffolds = parse_info_frags(info_frags) return scaffolds except OSError: print("Error when opening info_frags.txt") raise
python
def format_info_frags(info_frags): if isinstance(info_frags, dict): return info_frags else: try: scaffolds = parse_info_frags(info_frags) return scaffolds except OSError: print("Error when opening info_frags.txt") raise
[ "def", "format_info_frags", "(", "info_frags", ")", ":", "if", "isinstance", "(", "info_frags", ",", "dict", ")", ":", "return", "info_frags", "else", ":", "try", ":", "scaffolds", "=", "parse_info_frags", "(", "info_frags", ")", "return", "scaffolds", "except...
A function to seamlessly run on either scaffold dictionaries or info_frags.txt files without having to check the input first.
[ "A", "function", "to", "seamlessly", "run", "on", "either", "scaffold", "dictionaries", "or", "info_frags", ".", "txt", "files", "without", "having", "to", "check", "the", "input", "first", "." ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/parse_info_frags.py#L182-L194
17,831
koszullab/instaGRAAL
instagraal/parse_info_frags.py
plot_info_frags
def plot_info_frags(scaffolds): """A crude way to visualize new scaffolds according to their origin on the initial scaffolding. Each scaffold spawns a new plot. Orientations are represented by different colors. """ scaffolds = format_info_frags(scaffolds) for name, scaffold in scaffolds.items(): plt.figure() xs = range(len(scaffold)) color = [] names = {} ys = [] for my_bin in scaffold: current_color = "r" if my_bin[4] > 0 else "g" color += [current_color] name = my_bin[0] if name in names: ys.append(names[name]) else: names[name] = len(names) ys.append(names[name]) plt.scatter(xs, ys, c=color) plt.show()
python
def plot_info_frags(scaffolds): scaffolds = format_info_frags(scaffolds) for name, scaffold in scaffolds.items(): plt.figure() xs = range(len(scaffold)) color = [] names = {} ys = [] for my_bin in scaffold: current_color = "r" if my_bin[4] > 0 else "g" color += [current_color] name = my_bin[0] if name in names: ys.append(names[name]) else: names[name] = len(names) ys.append(names[name]) plt.scatter(xs, ys, c=color) plt.show()
[ "def", "plot_info_frags", "(", "scaffolds", ")", ":", "scaffolds", "=", "format_info_frags", "(", "scaffolds", ")", "for", "name", ",", "scaffold", "in", "scaffolds", ".", "items", "(", ")", ":", "plt", ".", "figure", "(", ")", "xs", "=", "range", "(", ...
A crude way to visualize new scaffolds according to their origin on the initial scaffolding. Each scaffold spawns a new plot. Orientations are represented by different colors.
[ "A", "crude", "way", "to", "visualize", "new", "scaffolds", "according", "to", "their", "origin", "on", "the", "initial", "scaffolding", ".", "Each", "scaffold", "spawns", "a", "new", "plot", ".", "Orientations", "are", "represented", "by", "different", "color...
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/parse_info_frags.py#L197-L221
17,832
koszullab/instaGRAAL
instagraal/parse_info_frags.py
remove_spurious_insertions
def remove_spurious_insertions(scaffolds): """Remove all bins whose left and right neighbors belong to the same, different scaffold. Example with three such insertions in two different scaffolds: >>> scaffolds = { ... "scaffold1": [ ... ["contig1", 0, 0, 100, 1], ... ["contig1", 1, 100, 200, 1], ... ["contig23", 53, 1845, 2058, -1], # <-- insertion ... ["contig1", 4, 254, 408, 1], ... ["contig1", 7, 805, 1253, 1], ... ["contig5", 23, 1500, 1605, -1], ... ["contig65", 405, 32145, 45548, -1], # <-- insertion ... ["contig5", 22, 1385, 1499, -1], ... ], ... "scaffold2": [ ... ["contig8", 0, 0, 250, 1], ... ["contig17", 2454, 8754, -1], # <-- insertion ... ["contig8", 2, 320, 480, 1], ... ], ... } >>> new_scaffolds = remove_spurious_insertions(scaffolds) >>> for my_bin in new_scaffolds['scaffold1']: ... print(my_bin) ... ['contig1', 0, 0, 100, 1] ['contig1', 1, 100, 200, 1] ['contig1', 4, 254, 408, 1] ['contig1', 7, 805, 1253, 1] ['contig5', 23, 1500, 1605, -1] ['contig5', 22, 1385, 1499, -1] >>> for my_bin in new_scaffolds['scaffold2']: ... print(my_bin) ... ['contig8', 0, 0, 250, 1] ['contig8', 2, 320, 480, 1] """ scaffolds = format_info_frags(scaffolds) new_scaffolds = {} for name, scaffold in scaffolds.items(): new_scaffold = [] if len(scaffold) > 2: for i in range(len(scaffold)): # First take care of edge cases: *-- or --* if i == 0: if not ( scaffold[i][0] != scaffold[i + 1][0] and scaffold[i + 1][0] == scaffold[i + 2][0] ): new_scaffold.append(scaffold[i]) elif i == len(scaffold) - 1: if not ( scaffold[i][0] != scaffold[i - 1][0] and scaffold[i - 1][0] == scaffold[i - 2][0] ): new_scaffold.append(scaffold[i]) # Otherwise, looking for -*- else: if not ( scaffold[i - 1][0] == scaffold[i + 1][0] and scaffold[i - 1][0] != scaffold[i][0] ): new_scaffold.append(scaffold[i]) else: # Can't remove insertions if 2 bins or less new_scaffold = copy.deepcopy(scaffold) new_scaffolds[name] = new_scaffold return new_scaffolds
python
def remove_spurious_insertions(scaffolds): scaffolds = format_info_frags(scaffolds) new_scaffolds = {} for name, scaffold in scaffolds.items(): new_scaffold = [] if len(scaffold) > 2: for i in range(len(scaffold)): # First take care of edge cases: *-- or --* if i == 0: if not ( scaffold[i][0] != scaffold[i + 1][0] and scaffold[i + 1][0] == scaffold[i + 2][0] ): new_scaffold.append(scaffold[i]) elif i == len(scaffold) - 1: if not ( scaffold[i][0] != scaffold[i - 1][0] and scaffold[i - 1][0] == scaffold[i - 2][0] ): new_scaffold.append(scaffold[i]) # Otherwise, looking for -*- else: if not ( scaffold[i - 1][0] == scaffold[i + 1][0] and scaffold[i - 1][0] != scaffold[i][0] ): new_scaffold.append(scaffold[i]) else: # Can't remove insertions if 2 bins or less new_scaffold = copy.deepcopy(scaffold) new_scaffolds[name] = new_scaffold return new_scaffolds
[ "def", "remove_spurious_insertions", "(", "scaffolds", ")", ":", "scaffolds", "=", "format_info_frags", "(", "scaffolds", ")", "new_scaffolds", "=", "{", "}", "for", "name", ",", "scaffold", "in", "scaffolds", ".", "items", "(", ")", ":", "new_scaffold", "=", ...
Remove all bins whose left and right neighbors belong to the same, different scaffold. Example with three such insertions in two different scaffolds: >>> scaffolds = { ... "scaffold1": [ ... ["contig1", 0, 0, 100, 1], ... ["contig1", 1, 100, 200, 1], ... ["contig23", 53, 1845, 2058, -1], # <-- insertion ... ["contig1", 4, 254, 408, 1], ... ["contig1", 7, 805, 1253, 1], ... ["contig5", 23, 1500, 1605, -1], ... ["contig65", 405, 32145, 45548, -1], # <-- insertion ... ["contig5", 22, 1385, 1499, -1], ... ], ... "scaffold2": [ ... ["contig8", 0, 0, 250, 1], ... ["contig17", 2454, 8754, -1], # <-- insertion ... ["contig8", 2, 320, 480, 1], ... ], ... } >>> new_scaffolds = remove_spurious_insertions(scaffolds) >>> for my_bin in new_scaffolds['scaffold1']: ... print(my_bin) ... ['contig1', 0, 0, 100, 1] ['contig1', 1, 100, 200, 1] ['contig1', 4, 254, 408, 1] ['contig1', 7, 805, 1253, 1] ['contig5', 23, 1500, 1605, -1] ['contig5', 22, 1385, 1499, -1] >>> for my_bin in new_scaffolds['scaffold2']: ... print(my_bin) ... ['contig8', 0, 0, 250, 1] ['contig8', 2, 320, 480, 1]
[ "Remove", "all", "bins", "whose", "left", "and", "right", "neighbors", "belong", "to", "the", "same", "different", "scaffold", "." ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/parse_info_frags.py#L224-L301
17,833
koszullab/instaGRAAL
instagraal/parse_info_frags.py
rearrange_intra_scaffolds
def rearrange_intra_scaffolds(scaffolds): """Rearranges all bins within each scaffold such that all bins belonging to the same initial contig are grouped together in the same order. When two such groups are found, the smaller one is moved to the larger one. """ scaffolds = format_info_frags(scaffolds) new_scaffolds = {} ordering = dict() for name, scaffold in scaffolds.items(): new_scaffold = [] ordering = dict() order = 0 my_blocks = [] for _, my_block in itertools.groupby(scaffold, operator.itemgetter(0)): my_bins = list(my_block) my_blocks.append(my_bins) block_length = len(my_bins) block_name = my_bins[0][0] if block_name in ordering.keys(): if block_length > ordering[block_name][1]: ordering[block_name] = (order, block_length) else: ordering[block_name] = (order, block_length) order += 1 def block_order(block): return ordering[block[0][0]][0] for my_block in sorted(my_blocks, key=block_order): for my_bin in my_block: new_scaffold.append(my_bin) new_scaffolds[name] = copy.deepcopy(new_scaffold) return new_scaffolds
python
def rearrange_intra_scaffolds(scaffolds): scaffolds = format_info_frags(scaffolds) new_scaffolds = {} ordering = dict() for name, scaffold in scaffolds.items(): new_scaffold = [] ordering = dict() order = 0 my_blocks = [] for _, my_block in itertools.groupby(scaffold, operator.itemgetter(0)): my_bins = list(my_block) my_blocks.append(my_bins) block_length = len(my_bins) block_name = my_bins[0][0] if block_name in ordering.keys(): if block_length > ordering[block_name][1]: ordering[block_name] = (order, block_length) else: ordering[block_name] = (order, block_length) order += 1 def block_order(block): return ordering[block[0][0]][0] for my_block in sorted(my_blocks, key=block_order): for my_bin in my_block: new_scaffold.append(my_bin) new_scaffolds[name] = copy.deepcopy(new_scaffold) return new_scaffolds
[ "def", "rearrange_intra_scaffolds", "(", "scaffolds", ")", ":", "scaffolds", "=", "format_info_frags", "(", "scaffolds", ")", "new_scaffolds", "=", "{", "}", "ordering", "=", "dict", "(", ")", "for", "name", ",", "scaffold", "in", "scaffolds", ".", "items", ...
Rearranges all bins within each scaffold such that all bins belonging to the same initial contig are grouped together in the same order. When two such groups are found, the smaller one is moved to the larger one.
[ "Rearranges", "all", "bins", "within", "each", "scaffold", "such", "that", "all", "bins", "belonging", "to", "the", "same", "initial", "contig", "are", "grouped", "together", "in", "the", "same", "order", ".", "When", "two", "such", "groups", "are", "found",...
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/parse_info_frags.py#L465-L508
17,834
koszullab/instaGRAAL
instagraal/parse_info_frags.py
write_fasta
def write_fasta( init_fasta, info_frags, output=DEFAULT_NEW_GENOME_NAME, junction=False ): """Convert an info_frags.txt file into a fasta file given a reference. Optionally adds junction sequences to reflect the possibly missing base pairs between two newly joined scaffolds. """ init_genome = { record.id: record.seq for record in SeqIO.parse(init_fasta, "fasta") } my_new_records = [] with open(info_frags, "r") as info_frags_handle: current_seq = "" current_id = None previous_contig = None for line in info_frags_handle: if line.startswith(">"): previous_contig = None if current_id is not None: new_record = SeqRecord( current_seq, id=current_id, description="" ) my_new_records.append(new_record) current_seq = "" current_id = str(line[1:]) elif line.startswith("init_contig"): previous_contig = None else: (init_contig, _, orientation, pos_start, pos_end) = str( line[:-1] ).split("\t") start = int(pos_start) end = int(pos_end) ori = int(orientation) assert start < end assert ori in {-1, 1} seq_to_add = init_genome[init_contig][start:end] if ori == 1: current_seq += seq_to_add elif ori == -1: current_seq += seq_to_add.reverse_complement() if junction and previous_contig not in {init_contig, None}: error_was_raised = False try: extra_seq = Seq(junction, IUPAC.ambiguous_dna) current_seq = extra_seq + current_seq except TypeError: if not error_was_raised: print("Invalid junction sequence") error_was_raised = True previous_contig = init_contig new_record = SeqRecord(current_seq, id=current_id, description="") my_new_records.append(new_record) SeqIO.write(my_new_records, output, "fasta")
python
def write_fasta( init_fasta, info_frags, output=DEFAULT_NEW_GENOME_NAME, junction=False ): init_genome = { record.id: record.seq for record in SeqIO.parse(init_fasta, "fasta") } my_new_records = [] with open(info_frags, "r") as info_frags_handle: current_seq = "" current_id = None previous_contig = None for line in info_frags_handle: if line.startswith(">"): previous_contig = None if current_id is not None: new_record = SeqRecord( current_seq, id=current_id, description="" ) my_new_records.append(new_record) current_seq = "" current_id = str(line[1:]) elif line.startswith("init_contig"): previous_contig = None else: (init_contig, _, orientation, pos_start, pos_end) = str( line[:-1] ).split("\t") start = int(pos_start) end = int(pos_end) ori = int(orientation) assert start < end assert ori in {-1, 1} seq_to_add = init_genome[init_contig][start:end] if ori == 1: current_seq += seq_to_add elif ori == -1: current_seq += seq_to_add.reverse_complement() if junction and previous_contig not in {init_contig, None}: error_was_raised = False try: extra_seq = Seq(junction, IUPAC.ambiguous_dna) current_seq = extra_seq + current_seq except TypeError: if not error_was_raised: print("Invalid junction sequence") error_was_raised = True previous_contig = init_contig new_record = SeqRecord(current_seq, id=current_id, description="") my_new_records.append(new_record) SeqIO.write(my_new_records, output, "fasta")
[ "def", "write_fasta", "(", "init_fasta", ",", "info_frags", ",", "output", "=", "DEFAULT_NEW_GENOME_NAME", ",", "junction", "=", "False", ")", ":", "init_genome", "=", "{", "record", ".", "id", ":", "record", ".", "seq", "for", "record", "in", "SeqIO", "."...
Convert an info_frags.txt file into a fasta file given a reference. Optionally adds junction sequences to reflect the possibly missing base pairs between two newly joined scaffolds.
[ "Convert", "an", "info_frags", ".", "txt", "file", "into", "a", "fasta", "file", "given", "a", "reference", ".", "Optionally", "adds", "junction", "sequences", "to", "reflect", "the", "possibly", "missing", "base", "pairs", "between", "two", "newly", "joined",...
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/parse_info_frags.py#L604-L664
17,835
koszullab/instaGRAAL
instagraal/parse_info_frags.py
is_block
def is_block(bin_list): """Check if a bin list has exclusively consecutive bin ids. """ id_set = set((my_bin[1] for my_bin in bin_list)) start_id, end_id = min(id_set), max(id_set) return id_set == set(range(start_id, end_id + 1))
python
def is_block(bin_list): id_set = set((my_bin[1] for my_bin in bin_list)) start_id, end_id = min(id_set), max(id_set) return id_set == set(range(start_id, end_id + 1))
[ "def", "is_block", "(", "bin_list", ")", ":", "id_set", "=", "set", "(", "(", "my_bin", "[", "1", "]", "for", "my_bin", "in", "bin_list", ")", ")", "start_id", ",", "end_id", "=", "min", "(", "id_set", ")", ",", "max", "(", "id_set", ")", "return",...
Check if a bin list has exclusively consecutive bin ids.
[ "Check", "if", "a", "bin", "list", "has", "exclusively", "consecutive", "bin", "ids", "." ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/parse_info_frags.py#L804-L810
17,836
koszullab/instaGRAAL
instagraal/leastsqbound.py
internal2external_grad
def internal2external_grad(xi, bounds): """ Calculate the internal to external gradiant Calculates the partial of external over internal """ ge = np.empty_like(xi) for i, (v, bound) in enumerate(zip(xi, bounds)): a = bound[0] # minimum b = bound[1] # maximum if a == None and b == None: # No constraints ge[i] = 1.0 elif b == None: # only min ge[i] = v / np.sqrt(v ** 2 + 1) elif a == None: # only max ge[i] = -v / np.sqrt(v ** 2 + 1) else: # both min and max ge[i] = (b - a) * np.cos(v) / 2. return ge
python
def internal2external_grad(xi, bounds): ge = np.empty_like(xi) for i, (v, bound) in enumerate(zip(xi, bounds)): a = bound[0] # minimum b = bound[1] # maximum if a == None and b == None: # No constraints ge[i] = 1.0 elif b == None: # only min ge[i] = v / np.sqrt(v ** 2 + 1) elif a == None: # only max ge[i] = -v / np.sqrt(v ** 2 + 1) else: # both min and max ge[i] = (b - a) * np.cos(v) / 2. return ge
[ "def", "internal2external_grad", "(", "xi", ",", "bounds", ")", ":", "ge", "=", "np", ".", "empty_like", "(", "xi", ")", "for", "i", ",", "(", "v", ",", "bound", ")", "in", "enumerate", "(", "zip", "(", "xi", ",", "bounds", ")", ")", ":", "a", ...
Calculate the internal to external gradiant Calculates the partial of external over internal
[ "Calculate", "the", "internal", "to", "external", "gradiant", "Calculates", "the", "partial", "of", "external", "over", "internal" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/leastsqbound.py#L19-L46
17,837
koszullab/instaGRAAL
instagraal/leastsqbound.py
internal2external
def internal2external(xi, bounds): """ Convert a series of internal variables to external variables""" xe = np.empty_like(xi) for i, (v, bound) in enumerate(zip(xi, bounds)): a = bound[0] # minimum b = bound[1] # maximum if a == None and b == None: # No constraints xe[i] = v elif b == None: # only min xe[i] = a - 1. + np.sqrt(v ** 2. + 1.) elif a == None: # only max xe[i] = b + 1. - np.sqrt(v ** 2. + 1.) else: # both min and max xe[i] = a + ((b - a) / 2.) * (np.sin(v) + 1.) return xe
python
def internal2external(xi, bounds): xe = np.empty_like(xi) for i, (v, bound) in enumerate(zip(xi, bounds)): a = bound[0] # minimum b = bound[1] # maximum if a == None and b == None: # No constraints xe[i] = v elif b == None: # only min xe[i] = a - 1. + np.sqrt(v ** 2. + 1.) elif a == None: # only max xe[i] = b + 1. - np.sqrt(v ** 2. + 1.) else: # both min and max xe[i] = a + ((b - a) / 2.) * (np.sin(v) + 1.) return xe
[ "def", "internal2external", "(", "xi", ",", "bounds", ")", ":", "xe", "=", "np", ".", "empty_like", "(", "xi", ")", "for", "i", ",", "(", "v", ",", "bound", ")", "in", "enumerate", "(", "zip", "(", "xi", ",", "bounds", ")", ")", ":", "a", "=", ...
Convert a series of internal variables to external variables
[ "Convert", "a", "series", "of", "internal", "variables", "to", "external", "variables" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/leastsqbound.py#L56-L78
17,838
koszullab/instaGRAAL
instagraal/leastsqbound.py
external2internal
def external2internal(xe, bounds): """ Convert a series of external variables to internal variables""" xi = np.empty_like(xe) for i, (v, bound) in enumerate(zip(xe, bounds)): a = bound[0] # minimum b = bound[1] # maximum if a == None and b == None: # No constraints xi[i] = v elif b == None: # only min xi[i] = np.sqrt((v - a + 1.) ** 2. - 1) elif a == None: # only max xi[i] = np.sqrt((b - v + 1.) ** 2. - 1) else: # both min and max xi[i] = np.arcsin((2. * (v - a) / (b - a)) - 1.) return xi
python
def external2internal(xe, bounds): xi = np.empty_like(xe) for i, (v, bound) in enumerate(zip(xe, bounds)): a = bound[0] # minimum b = bound[1] # maximum if a == None and b == None: # No constraints xi[i] = v elif b == None: # only min xi[i] = np.sqrt((v - a + 1.) ** 2. - 1) elif a == None: # only max xi[i] = np.sqrt((b - v + 1.) ** 2. - 1) else: # both min and max xi[i] = np.arcsin((2. * (v - a) / (b - a)) - 1.) return xi
[ "def", "external2internal", "(", "xe", ",", "bounds", ")", ":", "xi", "=", "np", ".", "empty_like", "(", "xe", ")", "for", "i", ",", "(", "v", ",", "bound", ")", "in", "enumerate", "(", "zip", "(", "xe", ",", "bounds", ")", ")", ":", "a", "=", ...
Convert a series of external variables to internal variables
[ "Convert", "a", "series", "of", "external", "variables", "to", "internal", "variables" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/leastsqbound.py#L81-L103
17,839
koszullab/instaGRAAL
instagraal/leastsqbound.py
calc_cov_x
def calc_cov_x(infodic, p): """ Calculate cov_x from fjac, ipvt and p as is done in leastsq """ fjac = infodic["fjac"] ipvt = infodic["ipvt"] n = len(p) # adapted from leastsq function in scipy/optimize/minpack.py perm = np.take(np.eye(n), ipvt - 1, 0) r = np.triu(np.transpose(fjac)[:n, :]) R = np.dot(r, perm) try: cov_x = np.linalg.inv(np.dot(np.transpose(R), R)) except LinAlgError: cov_x = None return cov_x
python
def calc_cov_x(infodic, p): fjac = infodic["fjac"] ipvt = infodic["ipvt"] n = len(p) # adapted from leastsq function in scipy/optimize/minpack.py perm = np.take(np.eye(n), ipvt - 1, 0) r = np.triu(np.transpose(fjac)[:n, :]) R = np.dot(r, perm) try: cov_x = np.linalg.inv(np.dot(np.transpose(R), R)) except LinAlgError: cov_x = None return cov_x
[ "def", "calc_cov_x", "(", "infodic", ",", "p", ")", ":", "fjac", "=", "infodic", "[", "\"fjac\"", "]", "ipvt", "=", "infodic", "[", "\"ipvt\"", "]", "n", "=", "len", "(", "p", ")", "# adapted from leastsq function in scipy/optimize/minpack.py", "perm", "=", ...
Calculate cov_x from fjac, ipvt and p as is done in leastsq
[ "Calculate", "cov_x", "from", "fjac", "ipvt", "and", "p", "as", "is", "done", "in", "leastsq" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/leastsqbound.py#L112-L129
17,840
koszullab/instaGRAAL
instagraal/leastsqbound.py
leastsqbound
def leastsqbound(func, x0, bounds, args=(), **kw): """ Constrained multivariant Levenberg-Marquard optimization Minimize the sum of squares of a given function using the Levenberg-Marquard algorithm. Contraints on parameters are inforced using variable transformations as described in the MINUIT User's Guide by Fred James and Matthias Winkler. Parameters: * func functions to call for optimization. * x0 Starting estimate for the minimization. * bounds (min,max) pair for each element of x, defining the bounds on that parameter. Use None for one of min or max when there is no bound in that direction. * args Any extra arguments to func are places in this tuple. Returns: (x,{cov_x,infodict,mesg},ier) Return is described in the scipy.optimize.leastsq function. x and con_v are corrected to take into account the parameter transformation, infodic is not corrected. Additional keyword arguments are passed directly to the scipy.optimize.leastsq algorithm. """ # check for full output if "full_output" in kw and kw["full_output"]: full = True else: full = False # convert x0 to internal variables i0 = external2internal(x0, bounds) # perfrom unconstrained optimization using internal variables r = leastsq(err, i0, args=(bounds, func, args), **kw) # unpack return convert to external variables and return if full: xi, cov_xi, infodic, mesg, ier = r xe = internal2external(xi, bounds) cov_xe = i2e_cov_x(xi, bounds, cov_xi) # XXX correct infodic 'fjac','ipvt', and 'qtf' return xe, cov_xe, infodic, mesg, ier else: xi, ier = r xe = internal2external(xi, bounds) return xe, ier
python
def leastsqbound(func, x0, bounds, args=(), **kw): # check for full output if "full_output" in kw and kw["full_output"]: full = True else: full = False # convert x0 to internal variables i0 = external2internal(x0, bounds) # perfrom unconstrained optimization using internal variables r = leastsq(err, i0, args=(bounds, func, args), **kw) # unpack return convert to external variables and return if full: xi, cov_xi, infodic, mesg, ier = r xe = internal2external(xi, bounds) cov_xe = i2e_cov_x(xi, bounds, cov_xi) # XXX correct infodic 'fjac','ipvt', and 'qtf' return xe, cov_xe, infodic, mesg, ier else: xi, ier = r xe = internal2external(xi, bounds) return xe, ier
[ "def", "leastsqbound", "(", "func", ",", "x0", ",", "bounds", ",", "args", "=", "(", ")", ",", "*", "*", "kw", ")", ":", "# check for full output", "if", "\"full_output\"", "in", "kw", "and", "kw", "[", "\"full_output\"", "]", ":", "full", "=", "True",...
Constrained multivariant Levenberg-Marquard optimization Minimize the sum of squares of a given function using the Levenberg-Marquard algorithm. Contraints on parameters are inforced using variable transformations as described in the MINUIT User's Guide by Fred James and Matthias Winkler. Parameters: * func functions to call for optimization. * x0 Starting estimate for the minimization. * bounds (min,max) pair for each element of x, defining the bounds on that parameter. Use None for one of min or max when there is no bound in that direction. * args Any extra arguments to func are places in this tuple. Returns: (x,{cov_x,infodict,mesg},ier) Return is described in the scipy.optimize.leastsq function. x and con_v are corrected to take into account the parameter transformation, infodic is not corrected. Additional keyword arguments are passed directly to the scipy.optimize.leastsq algorithm.
[ "Constrained", "multivariant", "Levenberg", "-", "Marquard", "optimization" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/leastsqbound.py#L132-L183
17,841
guykisel/inline-plz
inlineplz/interfaces/github.py
GitHubInterface.start_review
def start_review(self): """Mark our review as started.""" if self.set_status: self.github_repo.create_status( state="pending", description="Static analysis in progress.", context="inline-plz", sha=self.last_sha, )
python
def start_review(self): if self.set_status: self.github_repo.create_status( state="pending", description="Static analysis in progress.", context="inline-plz", sha=self.last_sha, )
[ "def", "start_review", "(", "self", ")", ":", "if", "self", ".", "set_status", ":", "self", ".", "github_repo", ".", "create_status", "(", "state", "=", "\"pending\"", ",", "description", "=", "\"Static analysis in progress.\"", ",", "context", "=", "\"inline-pl...
Mark our review as started.
[ "Mark", "our", "review", "as", "started", "." ]
b5b1744e9156e31f68b519c0d8022feff79888ae
https://github.com/guykisel/inline-plz/blob/b5b1744e9156e31f68b519c0d8022feff79888ae/inlineplz/interfaces/github.py#L199-L207
17,842
guykisel/inline-plz
inlineplz/interfaces/github.py
GitHubInterface.finish_review
def finish_review(self, success=True, error=False): """Mark our review as finished.""" if self.set_status: if error: self.github_repo.create_status( state="error", description="Static analysis error! inline-plz failed to run.", context="inline-plz", sha=self.last_sha, ) elif success: self.github_repo.create_status( state="success", description="Static analysis complete! No errors found in your PR.", context="inline-plz", sha=self.last_sha, ) else: self.github_repo.create_status( state="failure", description="Static analysis complete! Found errors in your PR.", context="inline-plz", sha=self.last_sha, )
python
def finish_review(self, success=True, error=False): if self.set_status: if error: self.github_repo.create_status( state="error", description="Static analysis error! inline-plz failed to run.", context="inline-plz", sha=self.last_sha, ) elif success: self.github_repo.create_status( state="success", description="Static analysis complete! No errors found in your PR.", context="inline-plz", sha=self.last_sha, ) else: self.github_repo.create_status( state="failure", description="Static analysis complete! Found errors in your PR.", context="inline-plz", sha=self.last_sha, )
[ "def", "finish_review", "(", "self", ",", "success", "=", "True", ",", "error", "=", "False", ")", ":", "if", "self", ".", "set_status", ":", "if", "error", ":", "self", ".", "github_repo", ".", "create_status", "(", "state", "=", "\"error\"", ",", "de...
Mark our review as finished.
[ "Mark", "our", "review", "as", "finished", "." ]
b5b1744e9156e31f68b519c0d8022feff79888ae
https://github.com/guykisel/inline-plz/blob/b5b1744e9156e31f68b519c0d8022feff79888ae/inlineplz/interfaces/github.py#L209-L232
17,843
guykisel/inline-plz
inlineplz/interfaces/github.py
GitHubInterface.out_of_date
def out_of_date(self): """Check if our local latest sha matches the remote latest sha""" try: latest_remote_sha = self.pr_commits(self.pull_request.refresh(True))[-1].sha print("Latest remote sha: {}".format(latest_remote_sha)) try: print("Ratelimit remaining: {}".format(self.github.ratelimit_remaining)) except Exception: print("Failed to look up ratelimit remaining") return self.last_sha != latest_remote_sha except IndexError: return False
python
def out_of_date(self): try: latest_remote_sha = self.pr_commits(self.pull_request.refresh(True))[-1].sha print("Latest remote sha: {}".format(latest_remote_sha)) try: print("Ratelimit remaining: {}".format(self.github.ratelimit_remaining)) except Exception: print("Failed to look up ratelimit remaining") return self.last_sha != latest_remote_sha except IndexError: return False
[ "def", "out_of_date", "(", "self", ")", ":", "try", ":", "latest_remote_sha", "=", "self", ".", "pr_commits", "(", "self", ".", "pull_request", ".", "refresh", "(", "True", ")", ")", "[", "-", "1", "]", ".", "sha", "print", "(", "\"Latest remote sha: {}\...
Check if our local latest sha matches the remote latest sha
[ "Check", "if", "our", "local", "latest", "sha", "matches", "the", "remote", "latest", "sha" ]
b5b1744e9156e31f68b519c0d8022feff79888ae
https://github.com/guykisel/inline-plz/blob/b5b1744e9156e31f68b519c0d8022feff79888ae/inlineplz/interfaces/github.py#L234-L245
17,844
guykisel/inline-plz
inlineplz/interfaces/github.py
GitHubInterface.position
def position(self, message): """Calculate position within the PR, which is not the line number""" if not message.line_number: message.line_number = 1 for patched_file in self.patch: target = patched_file.target_file.lstrip("b/") if target == message.path: offset = 1 for hunk in patched_file: for position, hunk_line in enumerate(hunk): if hunk_line.target_line_no == message.line_number: if not hunk_line.is_added: # if the line isn't an added line, we don't want to comment on it return return position + offset offset += len(hunk) + 1
python
def position(self, message): if not message.line_number: message.line_number = 1 for patched_file in self.patch: target = patched_file.target_file.lstrip("b/") if target == message.path: offset = 1 for hunk in patched_file: for position, hunk_line in enumerate(hunk): if hunk_line.target_line_no == message.line_number: if not hunk_line.is_added: # if the line isn't an added line, we don't want to comment on it return return position + offset offset += len(hunk) + 1
[ "def", "position", "(", "self", ",", "message", ")", ":", "if", "not", "message", ".", "line_number", ":", "message", ".", "line_number", "=", "1", "for", "patched_file", "in", "self", ".", "patch", ":", "target", "=", "patched_file", ".", "target_file", ...
Calculate position within the PR, which is not the line number
[ "Calculate", "position", "within", "the", "PR", "which", "is", "not", "the", "line", "number" ]
b5b1744e9156e31f68b519c0d8022feff79888ae
https://github.com/guykisel/inline-plz/blob/b5b1744e9156e31f68b519c0d8022feff79888ae/inlineplz/interfaces/github.py#L444-L461
17,845
koszullab/instaGRAAL
instagraal/pyramid_sparse.py
abs_contact_2_coo_file
def abs_contact_2_coo_file(abs_contact_file, coo_file): """Convert contact maps between old-style and new-style formats. A legacy function that converts contact maps from the older GRAAL format to the simpler instaGRAAL format. This is useful with datasets generated by Hi-C box. Parameters ---------- abs_contact_file : str, file or pathlib.Path The input old-style contact map. coo_file : str, file, or pathlib.Path The output path to the generated contact map; must be writable. """ sparse_dict = dict() h = open(abs_contact_file, "r") all_lines = h.readlines() n_lines = len(all_lines) for i in range(1, n_lines): line = all_lines[i] dat = line.split() mates = [int(dat[0]), int(dat[1])] mates.sort() f1 = mates[0] - 1 f2 = mates[1] - 1 if f1 in sparse_dict: if f2 in sparse_dict[f1]: sparse_dict[f1][f2] += 1 else: sparse_dict[f1][f2] = 1 else: sparse_dict[f1] = dict() sparse_dict[f1][f2] = 1 keys = list(sparse_dict.keys()) keys.sort() h.close() h_coo = open(coo_file, "w") h_coo.write("%s\t%s\t%s\n" % ("id_frag_a", "id_frag_b", "n_contact")) for fa in keys: d_fb = sparse_dict[fa] keys_b = list(d_fb.keys()) keys_b.sort() for fb in keys_b: nc = d_fb[fb] h_coo.write("%s\t%s\t%s\n" % (str(fa), str(fb), str(nc))) h_coo.close() h.close()
python
def abs_contact_2_coo_file(abs_contact_file, coo_file): sparse_dict = dict() h = open(abs_contact_file, "r") all_lines = h.readlines() n_lines = len(all_lines) for i in range(1, n_lines): line = all_lines[i] dat = line.split() mates = [int(dat[0]), int(dat[1])] mates.sort() f1 = mates[0] - 1 f2 = mates[1] - 1 if f1 in sparse_dict: if f2 in sparse_dict[f1]: sparse_dict[f1][f2] += 1 else: sparse_dict[f1][f2] = 1 else: sparse_dict[f1] = dict() sparse_dict[f1][f2] = 1 keys = list(sparse_dict.keys()) keys.sort() h.close() h_coo = open(coo_file, "w") h_coo.write("%s\t%s\t%s\n" % ("id_frag_a", "id_frag_b", "n_contact")) for fa in keys: d_fb = sparse_dict[fa] keys_b = list(d_fb.keys()) keys_b.sort() for fb in keys_b: nc = d_fb[fb] h_coo.write("%s\t%s\t%s\n" % (str(fa), str(fb), str(nc))) h_coo.close() h.close()
[ "def", "abs_contact_2_coo_file", "(", "abs_contact_file", ",", "coo_file", ")", ":", "sparse_dict", "=", "dict", "(", ")", "h", "=", "open", "(", "abs_contact_file", ",", "\"r\"", ")", "all_lines", "=", "h", ".", "readlines", "(", ")", "n_lines", "=", "len...
Convert contact maps between old-style and new-style formats. A legacy function that converts contact maps from the older GRAAL format to the simpler instaGRAAL format. This is useful with datasets generated by Hi-C box. Parameters ---------- abs_contact_file : str, file or pathlib.Path The input old-style contact map. coo_file : str, file, or pathlib.Path The output path to the generated contact map; must be writable.
[ "Convert", "contact", "maps", "between", "old", "-", "style", "and", "new", "-", "style", "formats", "." ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/pyramid_sparse.py#L332-L381
17,846
koszullab/instaGRAAL
instagraal/pyramid_sparse.py
fill_sparse_pyramid_level
def fill_sparse_pyramid_level(pyramid_handle, level, contact_file, nfrags): """Fill a level with sparse contact map data Fill values from the simple text matrix file to the hdf5-based pyramid level with contact data. Parameters ---------- pyramid_handle : h5py.File The hdf5 file handle containing the whole dataset. level : int The level (resolution) to be filled with contact data. contact_file : str, file or pathlib.Path The binned contact map file to be converted to hdf5 data. nfrags : int The number of fragments/bins in that specific level. """ sparse_dict = dict() h = open(contact_file, "r") all_lines = h.readlines() n_lines = len(all_lines) for i in range(1, n_lines): line = all_lines[i] dat = line.split() mates = [int(dat[0]), int(dat[1])] nc = int(dat[2]) mates.sort() f1 = mates[0] f2 = mates[1] if f1 in sparse_dict: if f2 in sparse_dict[f1]: sparse_dict[f1][f2] += nc else: sparse_dict[f1][f2] = nc else: sparse_dict[f1] = dict() sparse_dict[f1][f2] = nc keys = list(sparse_dict.keys()) keys.sort() out_r = [] out_c = [] out_d = [] for r in keys: data = sparse_dict[r] for c in list(data.keys()): out_r.append(r) out_c.append(c) out_d.append(data[c]) n_on_pxls = len(out_d) level_hdf5 = pyramid_handle.create_group(str(level)) data_2_sparse = level_hdf5.create_dataset("data", (3, n_on_pxls), "i") data_nfrags = level_hdf5.create_dataset("nfrags", (1, 1), "i") np_csr = np.zeros((3, n_on_pxls), dtype=np.int32) np_csr[0, :] = out_r np_csr[1, :] = out_c np_csr[2, :] = out_d data_2_sparse[0, :] = out_r data_2_sparse[1, :] = out_c data_2_sparse[2, :] = out_d data_nfrags[:] = nfrags
python
def fill_sparse_pyramid_level(pyramid_handle, level, contact_file, nfrags): sparse_dict = dict() h = open(contact_file, "r") all_lines = h.readlines() n_lines = len(all_lines) for i in range(1, n_lines): line = all_lines[i] dat = line.split() mates = [int(dat[0]), int(dat[1])] nc = int(dat[2]) mates.sort() f1 = mates[0] f2 = mates[1] if f1 in sparse_dict: if f2 in sparse_dict[f1]: sparse_dict[f1][f2] += nc else: sparse_dict[f1][f2] = nc else: sparse_dict[f1] = dict() sparse_dict[f1][f2] = nc keys = list(sparse_dict.keys()) keys.sort() out_r = [] out_c = [] out_d = [] for r in keys: data = sparse_dict[r] for c in list(data.keys()): out_r.append(r) out_c.append(c) out_d.append(data[c]) n_on_pxls = len(out_d) level_hdf5 = pyramid_handle.create_group(str(level)) data_2_sparse = level_hdf5.create_dataset("data", (3, n_on_pxls), "i") data_nfrags = level_hdf5.create_dataset("nfrags", (1, 1), "i") np_csr = np.zeros((3, n_on_pxls), dtype=np.int32) np_csr[0, :] = out_r np_csr[1, :] = out_c np_csr[2, :] = out_d data_2_sparse[0, :] = out_r data_2_sparse[1, :] = out_c data_2_sparse[2, :] = out_d data_nfrags[:] = nfrags
[ "def", "fill_sparse_pyramid_level", "(", "pyramid_handle", ",", "level", ",", "contact_file", ",", "nfrags", ")", ":", "sparse_dict", "=", "dict", "(", ")", "h", "=", "open", "(", "contact_file", ",", "\"r\"", ")", "all_lines", "=", "h", ".", "readlines", ...
Fill a level with sparse contact map data Fill values from the simple text matrix file to the hdf5-based pyramid level with contact data. Parameters ---------- pyramid_handle : h5py.File The hdf5 file handle containing the whole dataset. level : int The level (resolution) to be filled with contact data. contact_file : str, file or pathlib.Path The binned contact map file to be converted to hdf5 data. nfrags : int The number of fragments/bins in that specific level.
[ "Fill", "a", "level", "with", "sparse", "contact", "map", "data" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/pyramid_sparse.py#L384-L450
17,847
koszullab/instaGRAAL
instagraal/pyramid_sparse.py
init_frag_list
def init_frag_list(fragment_list, new_frag_list): """Adapt the original fragment list to fit the build function requirements Parameters ---------- fragment_list : str, file or pathlib.Path The input fragment list. new_frag_list : str, file or pathlib.Path The output fragment list to be written. Returns ------- i : int The number of records processed this way. """ handle_frag_list = open(fragment_list, "r") handle_new_frag_list = open(new_frag_list, "w") handle_new_frag_list.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( "id", "chrom", "start_pos", "end_pos", "size", "gc_content", "accu_frag", "frag_start", "frag_end", ) ) handle_frag_list.readline() i = 0 while 1: line_frag = handle_frag_list.readline() if not line_frag: handle_frag_list.close() handle_new_frag_list.close() break i += 1 data = line_frag.split("\t") id_init = data[0] contig_name = data[1] start_pos = data[2] end_pos = data[3] length_kb = data[4] gc_content = str(float(data[5])) accu_frag = str(1) frag_start = id_init frag_end = id_init handle_new_frag_list.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( id_init, contig_name, start_pos, end_pos, length_kb, gc_content, accu_frag, frag_start, frag_end, ) ) return i
python
def init_frag_list(fragment_list, new_frag_list): handle_frag_list = open(fragment_list, "r") handle_new_frag_list = open(new_frag_list, "w") handle_new_frag_list.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( "id", "chrom", "start_pos", "end_pos", "size", "gc_content", "accu_frag", "frag_start", "frag_end", ) ) handle_frag_list.readline() i = 0 while 1: line_frag = handle_frag_list.readline() if not line_frag: handle_frag_list.close() handle_new_frag_list.close() break i += 1 data = line_frag.split("\t") id_init = data[0] contig_name = data[1] start_pos = data[2] end_pos = data[3] length_kb = data[4] gc_content = str(float(data[5])) accu_frag = str(1) frag_start = id_init frag_end = id_init handle_new_frag_list.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( id_init, contig_name, start_pos, end_pos, length_kb, gc_content, accu_frag, frag_start, frag_end, ) ) return i
[ "def", "init_frag_list", "(", "fragment_list", ",", "new_frag_list", ")", ":", "handle_frag_list", "=", "open", "(", "fragment_list", ",", "\"r\"", ")", "handle_new_frag_list", "=", "open", "(", "new_frag_list", ",", "\"w\"", ")", "handle_new_frag_list", ".", "wri...
Adapt the original fragment list to fit the build function requirements Parameters ---------- fragment_list : str, file or pathlib.Path The input fragment list. new_frag_list : str, file or pathlib.Path The output fragment list to be written. Returns ------- i : int The number of records processed this way.
[ "Adapt", "the", "original", "fragment", "list", "to", "fit", "the", "build", "function", "requirements" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/pyramid_sparse.py#L453-L519
17,848
koszullab/instaGRAAL
instagraal/pyramid_sparse.py
pyramid.zoom_in_pixel
def zoom_in_pixel(self, curr_pixel): """ return the curr_frag at a higher resolution""" low_frag = curr_pixel[0] high_frag = curr_pixel[1] level = curr_pixel[2] if level > 0: str_level = str(level) low_sub_low = self.spec_level[str_level]["fragments_dict"][ low_frag ]["sub_low_index"] low_sub_high = self.spec_level[str_level]["fragments_dict"][ low_frag ]["sub_high_index"] high_sub_low = self.spec_level[str_level]["fragments_dict"][ high_frag ]["sub_low_index"] high_sub_high = self.spec_level[str_level]["fragments_dict"][ high_frag ]["sub_high_index"] vect = [low_sub_low, low_sub_high, high_sub_low, high_sub_high] new_pix_low = min(vect) new_pix_high = max(vect) new_level = level - 1 new_pixel = [new_pix_low, new_pix_high, new_level] else: new_pixel = curr_pixel return new_pixel
python
def zoom_in_pixel(self, curr_pixel): low_frag = curr_pixel[0] high_frag = curr_pixel[1] level = curr_pixel[2] if level > 0: str_level = str(level) low_sub_low = self.spec_level[str_level]["fragments_dict"][ low_frag ]["sub_low_index"] low_sub_high = self.spec_level[str_level]["fragments_dict"][ low_frag ]["sub_high_index"] high_sub_low = self.spec_level[str_level]["fragments_dict"][ high_frag ]["sub_low_index"] high_sub_high = self.spec_level[str_level]["fragments_dict"][ high_frag ]["sub_high_index"] vect = [low_sub_low, low_sub_high, high_sub_low, high_sub_high] new_pix_low = min(vect) new_pix_high = max(vect) new_level = level - 1 new_pixel = [new_pix_low, new_pix_high, new_level] else: new_pixel = curr_pixel return new_pixel
[ "def", "zoom_in_pixel", "(", "self", ",", "curr_pixel", ")", ":", "low_frag", "=", "curr_pixel", "[", "0", "]", "high_frag", "=", "curr_pixel", "[", "1", "]", "level", "=", "curr_pixel", "[", "2", "]", "if", "level", ">", "0", ":", "str_level", "=", ...
return the curr_frag at a higher resolution
[ "return", "the", "curr_frag", "at", "a", "higher", "resolution" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/pyramid_sparse.py#L1759-L1785
17,849
koszullab/instaGRAAL
instagraal/pyramid_sparse.py
pyramid.zoom_out_pixel
def zoom_out_pixel(self, curr_pixel): """ return the curr_frag at a lower resolution""" low_frag = curr_pixel[0] high_frag = curr_pixel[1] level = curr_pixel[2] str_level = str(level) if level < self.n_level - 1: low_super = self.spec_level[str_level]["fragments_dict"][low_frag][ "super_index" ] high_super = self.spec_level[str_level]["fragments_dict"][ high_frag ]["sub_index"] new_pix_low = min([low_super, high_super]) new_pix_high = max([low_super, high_super]) new_level = level + 1 new_pixel = [new_pix_low, new_pix_high, new_level] else: new_pixel = curr_pixel return new_pixel
python
def zoom_out_pixel(self, curr_pixel): low_frag = curr_pixel[0] high_frag = curr_pixel[1] level = curr_pixel[2] str_level = str(level) if level < self.n_level - 1: low_super = self.spec_level[str_level]["fragments_dict"][low_frag][ "super_index" ] high_super = self.spec_level[str_level]["fragments_dict"][ high_frag ]["sub_index"] new_pix_low = min([low_super, high_super]) new_pix_high = max([low_super, high_super]) new_level = level + 1 new_pixel = [new_pix_low, new_pix_high, new_level] else: new_pixel = curr_pixel return new_pixel
[ "def", "zoom_out_pixel", "(", "self", ",", "curr_pixel", ")", ":", "low_frag", "=", "curr_pixel", "[", "0", "]", "high_frag", "=", "curr_pixel", "[", "1", "]", "level", "=", "curr_pixel", "[", "2", "]", "str_level", "=", "str", "(", "level", ")", "if",...
return the curr_frag at a lower resolution
[ "return", "the", "curr_frag", "at", "a", "lower", "resolution" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/pyramid_sparse.py#L1787-L1807
17,850
koszullab/instaGRAAL
instagraal/pyramid_sparse.py
pyramid.zoom_in_area
def zoom_in_area(self, area): """ zoom in area""" x = area[0] y = area[1] level = x[2] logger.debug("x = {}".format(x)) logger.debug("y = {}".format(y)) logger.debug("level = {}".format(level)) if level == y[2] and level > 0: new_level = level - 1 high_x = self.zoom_in_pixel(x) high_y = self.zoom_in_pixel(y) new_x = [ min([high_x[0], high_y[0]]), min([high_x[1], high_y[1]]), new_level, ] new_y = [ max([high_x[0], high_y[0]]), max([high_x[1], high_y[1]]), new_level, ] new_area = [new_x, new_y] else: new_area = area return new_area
python
def zoom_in_area(self, area): x = area[0] y = area[1] level = x[2] logger.debug("x = {}".format(x)) logger.debug("y = {}".format(y)) logger.debug("level = {}".format(level)) if level == y[2] and level > 0: new_level = level - 1 high_x = self.zoom_in_pixel(x) high_y = self.zoom_in_pixel(y) new_x = [ min([high_x[0], high_y[0]]), min([high_x[1], high_y[1]]), new_level, ] new_y = [ max([high_x[0], high_y[0]]), max([high_x[1], high_y[1]]), new_level, ] new_area = [new_x, new_y] else: new_area = area return new_area
[ "def", "zoom_in_area", "(", "self", ",", "area", ")", ":", "x", "=", "area", "[", "0", "]", "y", "=", "area", "[", "1", "]", "level", "=", "x", "[", "2", "]", "logger", ".", "debug", "(", "\"x = {}\"", ".", "format", "(", "x", ")", ")", "logg...
zoom in area
[ "zoom", "in", "area" ]
1c02ca838e57d8178eec79f223644b2acd0153dd
https://github.com/koszullab/instaGRAAL/blob/1c02ca838e57d8178eec79f223644b2acd0153dd/instagraal/pyramid_sparse.py#L1809-L1835
17,851
guykisel/inline-plz
inlineplz/main.py
load_config
def load_config(args, config_path=".inlineplz.yml"): """Load inline-plz config from yaml config file with reasonable defaults.""" config = {} try: with open(config_path) as configfile: config = yaml.safe_load(configfile) or {} if config: print("Loaded config from {}".format(config_path)) pprint.pprint(config) except (IOError, OSError, yaml.parser.ParserError): traceback.print_exc() args = update_from_config(args, config) args.ignore_paths = args.__dict__.get("ignore_paths") or [ "node_modules", ".git", ".tox", "godeps", "vendor", "site-packages", "venv", ".env", "spec", "migrate", "bin", "fixtures", "cassettes", ".cache", ".idea", ".pytest_cache", "__pycache__", "dist", ] if config_path != ".inlineplz.yml": return args # fall back to config_dir inlineplz yaml if we didn't find one locally if args.config_dir and not config: new_config_path = os.path.join(args.config_dir, config_path) if os.path.exists(new_config_path): return load_config(args, new_config_path) return args
python
def load_config(args, config_path=".inlineplz.yml"): config = {} try: with open(config_path) as configfile: config = yaml.safe_load(configfile) or {} if config: print("Loaded config from {}".format(config_path)) pprint.pprint(config) except (IOError, OSError, yaml.parser.ParserError): traceback.print_exc() args = update_from_config(args, config) args.ignore_paths = args.__dict__.get("ignore_paths") or [ "node_modules", ".git", ".tox", "godeps", "vendor", "site-packages", "venv", ".env", "spec", "migrate", "bin", "fixtures", "cassettes", ".cache", ".idea", ".pytest_cache", "__pycache__", "dist", ] if config_path != ".inlineplz.yml": return args # fall back to config_dir inlineplz yaml if we didn't find one locally if args.config_dir and not config: new_config_path = os.path.join(args.config_dir, config_path) if os.path.exists(new_config_path): return load_config(args, new_config_path) return args
[ "def", "load_config", "(", "args", ",", "config_path", "=", "\".inlineplz.yml\"", ")", ":", "config", "=", "{", "}", "try", ":", "with", "open", "(", "config_path", ")", "as", "configfile", ":", "config", "=", "yaml", ".", "safe_load", "(", "configfile", ...
Load inline-plz config from yaml config file with reasonable defaults.
[ "Load", "inline", "-", "plz", "config", "from", "yaml", "config", "file", "with", "reasonable", "defaults", "." ]
b5b1744e9156e31f68b519c0d8022feff79888ae
https://github.com/guykisel/inline-plz/blob/b5b1744e9156e31f68b519c0d8022feff79888ae/inlineplz/main.py#L102-L143
17,852
guykisel/inline-plz
inlineplz/linter_runner.py
LinterRunner.cleanup
def cleanup(): """Delete standard installation directories.""" for install_dir in linters.INSTALL_DIRS: try: shutil.rmtree(install_dir, ignore_errors=True) except Exception: print( "{0}\nFailed to delete {1}".format( traceback.format_exc(), install_dir ) ) sys.stdout.flush()
python
def cleanup(): for install_dir in linters.INSTALL_DIRS: try: shutil.rmtree(install_dir, ignore_errors=True) except Exception: print( "{0}\nFailed to delete {1}".format( traceback.format_exc(), install_dir ) ) sys.stdout.flush()
[ "def", "cleanup", "(", ")", ":", "for", "install_dir", "in", "linters", ".", "INSTALL_DIRS", ":", "try", ":", "shutil", ".", "rmtree", "(", "install_dir", ",", "ignore_errors", "=", "True", ")", "except", "Exception", ":", "print", "(", "\"{0}\\nFailed to de...
Delete standard installation directories.
[ "Delete", "standard", "installation", "directories", "." ]
b5b1744e9156e31f68b519c0d8022feff79888ae
https://github.com/guykisel/inline-plz/blob/b5b1744e9156e31f68b519c0d8022feff79888ae/inlineplz/linter_runner.py#L167-L178
17,853
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseManager.get
def get(self, item): """ This additional code is necessary to properly return the 'volume' attribute of the instance as a CloudDatabaseVolume object instead of a raw dict. """ resource = super(CloudDatabaseManager, self).get(item) resource.volume = CloudDatabaseVolume(resource, resource.volume) return resource
python
def get(self, item): resource = super(CloudDatabaseManager, self).get(item) resource.volume = CloudDatabaseVolume(resource, resource.volume) return resource
[ "def", "get", "(", "self", ",", "item", ")", ":", "resource", "=", "super", "(", "CloudDatabaseManager", ",", "self", ")", ".", "get", "(", "item", ")", "resource", ".", "volume", "=", "CloudDatabaseVolume", "(", "resource", ",", "resource", ".", "volume...
This additional code is necessary to properly return the 'volume' attribute of the instance as a CloudDatabaseVolume object instead of a raw dict.
[ "This", "additional", "code", "is", "necessary", "to", "properly", "return", "the", "volume", "attribute", "of", "the", "instance", "as", "a", "CloudDatabaseVolume", "object", "instead", "of", "a", "raw", "dict", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L73-L81
17,854
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseManager._create_body
def _create_body(self, name, flavor=None, volume=None, databases=None, users=None, version=None, type=None): """ Used to create the dict required to create a Cloud Database instance. """ if flavor is None: flavor = 1 flavor_ref = self.api._get_flavor_ref(flavor) if volume is None: volume = 1 if databases is None: databases = [] if users is None: users = [] body = {"instance": { "name": name, "flavorRef": flavor_ref, "volume": {"size": volume}, "databases": databases, "users": users, }} if type is not None or version is not None: required = (type, version) if all(required): body['instance']['datastore'] = {"type": type, "version": version} else: raise exc.MissingCloudDatabaseParameter("Specifying a datastore" " requires both the datastore type as well as the version.") return body
python
def _create_body(self, name, flavor=None, volume=None, databases=None, users=None, version=None, type=None): if flavor is None: flavor = 1 flavor_ref = self.api._get_flavor_ref(flavor) if volume is None: volume = 1 if databases is None: databases = [] if users is None: users = [] body = {"instance": { "name": name, "flavorRef": flavor_ref, "volume": {"size": volume}, "databases": databases, "users": users, }} if type is not None or version is not None: required = (type, version) if all(required): body['instance']['datastore'] = {"type": type, "version": version} else: raise exc.MissingCloudDatabaseParameter("Specifying a datastore" " requires both the datastore type as well as the version.") return body
[ "def", "_create_body", "(", "self", ",", "name", ",", "flavor", "=", "None", ",", "volume", "=", "None", ",", "databases", "=", "None", ",", "users", "=", "None", ",", "version", "=", "None", ",", "type", "=", "None", ")", ":", "if", "flavor", "is"...
Used to create the dict required to create a Cloud Database instance.
[ "Used", "to", "create", "the", "dict", "required", "to", "create", "a", "Cloud", "Database", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L84-L114
17,855
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseManager.list_backups
def list_backups(self, instance=None, marker=0, limit=20): """ Returns a paginated list of backups, or just for a particular instance. """ return self.api._backup_manager.list(instance=instance, limit=limit, marker=marker)
python
def list_backups(self, instance=None, marker=0, limit=20): return self.api._backup_manager.list(instance=instance, limit=limit, marker=marker)
[ "def", "list_backups", "(", "self", ",", "instance", "=", "None", ",", "marker", "=", "0", ",", "limit", "=", "20", ")", ":", "return", "self", ".", "api", ".", "_backup_manager", ".", "list", "(", "instance", "=", "instance", ",", "limit", "=", "lim...
Returns a paginated list of backups, or just for a particular instance.
[ "Returns", "a", "paginated", "list", "of", "backups", "or", "just", "for", "a", "particular", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L152-L158
17,856
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseManager._list_backups_for_instance
def _list_backups_for_instance(self, instance, marker=0, limit=20): """ Instance-specific backups are handled through the instance manager, not the backup manager. """ uri = "/%s/%s/backups?limit=%d&marker=%d" % (self.uri_base, utils.get_id(instance), int(limit), int(marker)) resp, resp_body = self.api.method_get(uri) mgr = self.api._backup_manager return [CloudDatabaseBackup(mgr, backup) for backup in resp_body.get("backups")]
python
def _list_backups_for_instance(self, instance, marker=0, limit=20): uri = "/%s/%s/backups?limit=%d&marker=%d" % (self.uri_base, utils.get_id(instance), int(limit), int(marker)) resp, resp_body = self.api.method_get(uri) mgr = self.api._backup_manager return [CloudDatabaseBackup(mgr, backup) for backup in resp_body.get("backups")]
[ "def", "_list_backups_for_instance", "(", "self", ",", "instance", ",", "marker", "=", "0", ",", "limit", "=", "20", ")", ":", "uri", "=", "\"/%s/%s/backups?limit=%d&marker=%d\"", "%", "(", "self", ".", "uri_base", ",", "utils", ".", "get_id", "(", "instance...
Instance-specific backups are handled through the instance manager, not the backup manager.
[ "Instance", "-", "specific", "backups", "are", "handled", "through", "the", "instance", "manager", "not", "the", "backup", "manager", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L161-L173
17,857
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseUserManager.list_user_access
def list_user_access(self, user): """ Returns a list of all database names for which the specified user has access rights. """ user = utils.get_name(user) uri = "/%s/%s/databases" % (self.uri_base, user) try: resp, resp_body = self.api.method_get(uri) except exc.NotFound as e: raise exc.NoSuchDatabaseUser("User '%s' does not exist." % user) dbs = resp_body.get("databases", {}) return [CloudDatabaseDatabase(self, db) for db in dbs]
python
def list_user_access(self, user): user = utils.get_name(user) uri = "/%s/%s/databases" % (self.uri_base, user) try: resp, resp_body = self.api.method_get(uri) except exc.NotFound as e: raise exc.NoSuchDatabaseUser("User '%s' does not exist." % user) dbs = resp_body.get("databases", {}) return [CloudDatabaseDatabase(self, db) for db in dbs]
[ "def", "list_user_access", "(", "self", ",", "user", ")", ":", "user", "=", "utils", ".", "get_name", "(", "user", ")", "uri", "=", "\"/%s/%s/databases\"", "%", "(", "self", ".", "uri_base", ",", "user", ")", "try", ":", "resp", ",", "resp_body", "=", ...
Returns a list of all database names for which the specified user has access rights.
[ "Returns", "a", "list", "of", "all", "database", "names", "for", "which", "the", "specified", "user", "has", "access", "rights", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L267-L279
17,858
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseUserManager.grant_user_access
def grant_user_access(self, user, db_names, strict=True): """ Gives access to the databases listed in `db_names` to the user. You may pass in either a single db or a list of dbs. If any of the databases do not exist, a NoSuchDatabase exception will be raised, unless you specify `strict=False` in the call. """ user = utils.get_name(user) uri = "/%s/%s/databases" % (self.uri_base, user) db_names = self._get_db_names(db_names, strict=strict) dbs = [{"name": db_name} for db_name in db_names] body = {"databases": dbs} try: resp, resp_body = self.api.method_put(uri, body=body) except exc.NotFound as e: raise exc.NoSuchDatabaseUser("User '%s' does not exist." % user)
python
def grant_user_access(self, user, db_names, strict=True): user = utils.get_name(user) uri = "/%s/%s/databases" % (self.uri_base, user) db_names = self._get_db_names(db_names, strict=strict) dbs = [{"name": db_name} for db_name in db_names] body = {"databases": dbs} try: resp, resp_body = self.api.method_put(uri, body=body) except exc.NotFound as e: raise exc.NoSuchDatabaseUser("User '%s' does not exist." % user)
[ "def", "grant_user_access", "(", "self", ",", "user", ",", "db_names", ",", "strict", "=", "True", ")", ":", "user", "=", "utils", ".", "get_name", "(", "user", ")", "uri", "=", "\"/%s/%s/databases\"", "%", "(", "self", ".", "uri_base", ",", "user", ")...
Gives access to the databases listed in `db_names` to the user. You may pass in either a single db or a list of dbs. If any of the databases do not exist, a NoSuchDatabase exception will be raised, unless you specify `strict=False` in the call.
[ "Gives", "access", "to", "the", "databases", "listed", "in", "db_names", "to", "the", "user", ".", "You", "may", "pass", "in", "either", "a", "single", "db", "or", "a", "list", "of", "dbs", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L282-L298
17,859
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseBackupManager.list
def list(self, instance=None, limit=20, marker=0): """ Return a paginated list of backups, or just for a particular instance. """ if instance is None: return super(CloudDatabaseBackupManager, self).list() return self.api._manager._list_backups_for_instance(instance, limit=limit, marker=marker)
python
def list(self, instance=None, limit=20, marker=0): if instance is None: return super(CloudDatabaseBackupManager, self).list() return self.api._manager._list_backups_for_instance(instance, limit=limit, marker=marker)
[ "def", "list", "(", "self", ",", "instance", "=", "None", ",", "limit", "=", "20", ",", "marker", "=", "0", ")", ":", "if", "instance", "is", "None", ":", "return", "super", "(", "CloudDatabaseBackupManager", ",", "self", ")", ".", "list", "(", ")", ...
Return a paginated list of backups, or just for a particular instance.
[ "Return", "a", "paginated", "list", "of", "backups", "or", "just", "for", "a", "particular", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L331-L339
17,860
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.list_databases
def list_databases(self, limit=None, marker=None): """Returns a list of the names of all databases for this instance.""" return self._database_manager.list(limit=limit, marker=marker)
python
def list_databases(self, limit=None, marker=None): return self._database_manager.list(limit=limit, marker=marker)
[ "def", "list_databases", "(", "self", ",", "limit", "=", "None", ",", "marker", "=", "None", ")", ":", "return", "self", ".", "_database_manager", ".", "list", "(", "limit", "=", "limit", ",", "marker", "=", "marker", ")" ]
Returns a list of the names of all databases for this instance.
[ "Returns", "a", "list", "of", "the", "names", "of", "all", "databases", "for", "this", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L372-L374
17,861
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.list_users
def list_users(self, limit=None, marker=None): """Returns a list of the names of all users for this instance.""" return self._user_manager.list(limit=limit, marker=marker)
python
def list_users(self, limit=None, marker=None): return self._user_manager.list(limit=limit, marker=marker)
[ "def", "list_users", "(", "self", ",", "limit", "=", "None", ",", "marker", "=", "None", ")", ":", "return", "self", ".", "_user_manager", ".", "list", "(", "limit", "=", "limit", ",", "marker", "=", "marker", ")" ]
Returns a list of the names of all users for this instance.
[ "Returns", "a", "list", "of", "the", "names", "of", "all", "users", "for", "this", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L377-L379
17,862
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.get_user
def get_user(self, name): """ Finds the user in this instance with the specified name, and returns a CloudDatabaseUser object. If no match is found, a NoSuchDatabaseUser exception is raised. """ try: return self._user_manager.get(name) except exc.NotFound: raise exc.NoSuchDatabaseUser("No user by the name '%s' exists." % name)
python
def get_user(self, name): try: return self._user_manager.get(name) except exc.NotFound: raise exc.NoSuchDatabaseUser("No user by the name '%s' exists." % name)
[ "def", "get_user", "(", "self", ",", "name", ")", ":", "try", ":", "return", "self", ".", "_user_manager", ".", "get", "(", "name", ")", "except", "exc", ".", "NotFound", ":", "raise", "exc", ".", "NoSuchDatabaseUser", "(", "\"No user by the name '%s' exists...
Finds the user in this instance with the specified name, and returns a CloudDatabaseUser object. If no match is found, a NoSuchDatabaseUser exception is raised.
[ "Finds", "the", "user", "in", "this", "instance", "with", "the", "specified", "name", "and", "returns", "a", "CloudDatabaseUser", "object", ".", "If", "no", "match", "is", "found", "a", "NoSuchDatabaseUser", "exception", "is", "raised", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L382-L392
17,863
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.get_database
def get_database(self, name): """ Finds the database in this instance with the specified name, and returns a CloudDatabaseDatabase object. If no match is found, a NoSuchDatabase exception is raised. """ try: return [db for db in self.list_databases() if db.name == name][0] except IndexError: raise exc.NoSuchDatabase("No database by the name '%s' exists." % name)
python
def get_database(self, name): try: return [db for db in self.list_databases() if db.name == name][0] except IndexError: raise exc.NoSuchDatabase("No database by the name '%s' exists." % name)
[ "def", "get_database", "(", "self", ",", "name", ")", ":", "try", ":", "return", "[", "db", "for", "db", "in", "self", ".", "list_databases", "(", ")", "if", "db", ".", "name", "==", "name", "]", "[", "0", "]", "except", "IndexError", ":", "raise",...
Finds the database in this instance with the specified name, and returns a CloudDatabaseDatabase object. If no match is found, a NoSuchDatabase exception is raised.
[ "Finds", "the", "database", "in", "this", "instance", "with", "the", "specified", "name", "and", "returns", "a", "CloudDatabaseDatabase", "object", ".", "If", "no", "match", "is", "found", "a", "NoSuchDatabase", "exception", "is", "raised", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L395-L406
17,864
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.delete_database
def delete_database(self, name_or_obj): """ Deletes the specified database. If no database by that name exists, no exception will be raised; instead, nothing at all is done. """ name = utils.get_name(name_or_obj) self._database_manager.delete(name)
python
def delete_database(self, name_or_obj): name = utils.get_name(name_or_obj) self._database_manager.delete(name)
[ "def", "delete_database", "(", "self", ",", "name_or_obj", ")", ":", "name", "=", "utils", ".", "get_name", "(", "name_or_obj", ")", "self", ".", "_database_manager", ".", "delete", "(", "name", ")" ]
Deletes the specified database. If no database by that name exists, no exception will be raised; instead, nothing at all is done.
[ "Deletes", "the", "specified", "database", ".", "If", "no", "database", "by", "that", "name", "exists", "no", "exception", "will", "be", "raised", ";", "instead", "nothing", "at", "all", "is", "done", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L446-L453
17,865
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.delete_user
def delete_user(self, user): """ Deletes the specified user. If no user by that name exists, no exception will be raised; instead, nothing at all is done. """ name = utils.get_name(user) self._user_manager.delete(name)
python
def delete_user(self, user): name = utils.get_name(user) self._user_manager.delete(name)
[ "def", "delete_user", "(", "self", ",", "user", ")", ":", "name", "=", "utils", ".", "get_name", "(", "user", ")", "self", ".", "_user_manager", ".", "delete", "(", "name", ")" ]
Deletes the specified user. If no user by that name exists, no exception will be raised; instead, nothing at all is done.
[ "Deletes", "the", "specified", "user", ".", "If", "no", "user", "by", "that", "name", "exists", "no", "exception", "will", "be", "raised", ";", "instead", "nothing", "at", "all", "is", "done", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L499-L506
17,866
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.enable_root_user
def enable_root_user(self): """ Enables login from any host for the root user and provides the user with a generated root password. """ uri = "/instances/%s/root" % self.id resp, body = self.manager.api.method_post(uri) return body["user"]["password"]
python
def enable_root_user(self): uri = "/instances/%s/root" % self.id resp, body = self.manager.api.method_post(uri) return body["user"]["password"]
[ "def", "enable_root_user", "(", "self", ")", ":", "uri", "=", "\"/instances/%s/root\"", "%", "self", ".", "id", "resp", ",", "body", "=", "self", ".", "manager", ".", "api", ".", "method_post", "(", "uri", ")", "return", "body", "[", "\"user\"", "]", "...
Enables login from any host for the root user and provides the user with a generated root password.
[ "Enables", "login", "from", "any", "host", "for", "the", "root", "user", "and", "provides", "the", "user", "with", "a", "generated", "root", "password", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L509-L516
17,867
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.root_user_status
def root_user_status(self): """ Returns True or False, depending on whether the root user for this instance has been enabled. """ uri = "/instances/%s/root" % self.id resp, body = self.manager.api.method_get(uri) return body["rootEnabled"]
python
def root_user_status(self): uri = "/instances/%s/root" % self.id resp, body = self.manager.api.method_get(uri) return body["rootEnabled"]
[ "def", "root_user_status", "(", "self", ")", ":", "uri", "=", "\"/instances/%s/root\"", "%", "self", ".", "id", "resp", ",", "body", "=", "self", ".", "manager", ".", "api", ".", "method_get", "(", "uri", ")", "return", "body", "[", "\"rootEnabled\"", "]...
Returns True or False, depending on whether the root user for this instance has been enabled.
[ "Returns", "True", "or", "False", "depending", "on", "whether", "the", "root", "user", "for", "this", "instance", "has", "been", "enabled", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L519-L526
17,868
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.resize
def resize(self, flavor): """Set the size of this instance to a different flavor.""" # We need the flavorRef, not the flavor or size. flavorRef = self.manager.api._get_flavor_ref(flavor) body = {"flavorRef": flavorRef} self.manager.action(self, "resize", body=body)
python
def resize(self, flavor): # We need the flavorRef, not the flavor or size. flavorRef = self.manager.api._get_flavor_ref(flavor) body = {"flavorRef": flavorRef} self.manager.action(self, "resize", body=body)
[ "def", "resize", "(", "self", ",", "flavor", ")", ":", "# We need the flavorRef, not the flavor or size.", "flavorRef", "=", "self", ".", "manager", ".", "api", ".", "_get_flavor_ref", "(", "flavor", ")", "body", "=", "{", "\"flavorRef\"", ":", "flavorRef", "}",...
Set the size of this instance to a different flavor.
[ "Set", "the", "size", "of", "this", "instance", "to", "a", "different", "flavor", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L534-L539
17,869
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.resize_volume
def resize_volume(self, size): """Changes the size of the volume for this instance.""" curr_size = self.volume.size if size <= curr_size: raise exc.InvalidVolumeResize("The new volume size must be larger " "than the current volume size of '%s'." % curr_size) body = {"volume": {"size": size}} self.manager.action(self, "resize", body=body)
python
def resize_volume(self, size): curr_size = self.volume.size if size <= curr_size: raise exc.InvalidVolumeResize("The new volume size must be larger " "than the current volume size of '%s'." % curr_size) body = {"volume": {"size": size}} self.manager.action(self, "resize", body=body)
[ "def", "resize_volume", "(", "self", ",", "size", ")", ":", "curr_size", "=", "self", ".", "volume", ".", "size", "if", "size", "<=", "curr_size", ":", "raise", "exc", ".", "InvalidVolumeResize", "(", "\"The new volume size must be larger \"", "\"than the current ...
Changes the size of the volume for this instance.
[ "Changes", "the", "size", "of", "the", "volume", "for", "this", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L542-L549
17,870
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.list_backups
def list_backups(self, limit=20, marker=0): """ Returns a paginated list of backups for this instance. """ return self.manager._list_backups_for_instance(self, limit=limit, marker=marker)
python
def list_backups(self, limit=20, marker=0): return self.manager._list_backups_for_instance(self, limit=limit, marker=marker)
[ "def", "list_backups", "(", "self", ",", "limit", "=", "20", ",", "marker", "=", "0", ")", ":", "return", "self", ".", "manager", ".", "_list_backups_for_instance", "(", "self", ",", "limit", "=", "limit", ",", "marker", "=", "marker", ")" ]
Returns a paginated list of backups for this instance.
[ "Returns", "a", "paginated", "list", "of", "backups", "for", "this", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L552-L557
17,871
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseInstance.create_backup
def create_backup(self, name, description=None): """ Creates a backup of this instance, giving it the specified name along with an optional description. """ return self.manager.create_backup(self, name, description=description)
python
def create_backup(self, name, description=None): return self.manager.create_backup(self, name, description=description)
[ "def", "create_backup", "(", "self", ",", "name", ",", "description", "=", "None", ")", ":", "return", "self", ".", "manager", ".", "create_backup", "(", "self", ",", "name", ",", "description", "=", "description", ")" ]
Creates a backup of this instance, giving it the specified name along with an optional description.
[ "Creates", "a", "backup", "of", "this", "instance", "giving", "it", "the", "specified", "name", "along", "with", "an", "optional", "description", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L560-L565
17,872
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseClient.list_databases
def list_databases(self, instance, limit=None, marker=None): """Returns all databases for the specified instance.""" return instance.list_databases(limit=limit, marker=marker)
python
def list_databases(self, instance, limit=None, marker=None): return instance.list_databases(limit=limit, marker=marker)
[ "def", "list_databases", "(", "self", ",", "instance", ",", "limit", "=", "None", ",", "marker", "=", "None", ")", ":", "return", "instance", ".", "list_databases", "(", "limit", "=", "limit", ",", "marker", "=", "marker", ")" ]
Returns all databases for the specified instance.
[ "Returns", "all", "databases", "for", "the", "specified", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L700-L702
17,873
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseClient.create_database
def create_database(self, instance, name, character_set=None, collate=None): """Creates a database with the specified name on the given instance.""" return instance.create_database(name, character_set=character_set, collate=collate)
python
def create_database(self, instance, name, character_set=None, collate=None): return instance.create_database(name, character_set=character_set, collate=collate)
[ "def", "create_database", "(", "self", ",", "instance", ",", "name", ",", "character_set", "=", "None", ",", "collate", "=", "None", ")", ":", "return", "instance", ".", "create_database", "(", "name", ",", "character_set", "=", "character_set", ",", "collat...
Creates a database with the specified name on the given instance.
[ "Creates", "a", "database", "with", "the", "specified", "name", "on", "the", "given", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L706-L710
17,874
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseClient.list_users
def list_users(self, instance, limit=None, marker=None): """Returns all users for the specified instance.""" return instance.list_users(limit=limit, marker=marker)
python
def list_users(self, instance, limit=None, marker=None): return instance.list_users(limit=limit, marker=marker)
[ "def", "list_users", "(", "self", ",", "instance", ",", "limit", "=", "None", ",", "marker", "=", "None", ")", ":", "return", "instance", ".", "list_users", "(", "limit", "=", "limit", ",", "marker", "=", "marker", ")" ]
Returns all users for the specified instance.
[ "Returns", "all", "users", "for", "the", "specified", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L730-L732
17,875
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseClient.grant_user_access
def grant_user_access(self, instance, user, db_names, strict=True): """ Gives access to the databases listed in `db_names` to the user on the specified instance. """ return instance.grant_user_access(user, db_names, strict=strict)
python
def grant_user_access(self, instance, user, db_names, strict=True): return instance.grant_user_access(user, db_names, strict=strict)
[ "def", "grant_user_access", "(", "self", ",", "instance", ",", "user", ",", "db_names", ",", "strict", "=", "True", ")", ":", "return", "instance", ".", "grant_user_access", "(", "user", ",", "db_names", ",", "strict", "=", "strict", ")" ]
Gives access to the databases listed in `db_names` to the user on the specified instance.
[ "Gives", "access", "to", "the", "databases", "listed", "in", "db_names", "to", "the", "user", "on", "the", "specified", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L793-L798
17,876
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseClient.revoke_user_access
def revoke_user_access(self, instance, user, db_names, strict=True): """ Revokes access to the databases listed in `db_names` for the user on the specified instance. """ return instance.revoke_user_access(user, db_names, strict=strict)
python
def revoke_user_access(self, instance, user, db_names, strict=True): return instance.revoke_user_access(user, db_names, strict=strict)
[ "def", "revoke_user_access", "(", "self", ",", "instance", ",", "user", ",", "db_names", ",", "strict", "=", "True", ")", ":", "return", "instance", ".", "revoke_user_access", "(", "user", ",", "db_names", ",", "strict", "=", "strict", ")" ]
Revokes access to the databases listed in `db_names` for the user on the specified instance.
[ "Revokes", "access", "to", "the", "databases", "listed", "in", "db_names", "for", "the", "user", "on", "the", "specified", "instance", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L802-L807
17,877
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseClient.list_flavors
def list_flavors(self, limit=None, marker=None): """Returns a list of all available Flavors.""" return self._flavor_manager.list(limit=limit, marker=marker)
python
def list_flavors(self, limit=None, marker=None): return self._flavor_manager.list(limit=limit, marker=marker)
[ "def", "list_flavors", "(", "self", ",", "limit", "=", "None", ",", "marker", "=", "None", ")", ":", "return", "self", ".", "_flavor_manager", ".", "list", "(", "limit", "=", "limit", ",", "marker", "=", "marker", ")" ]
Returns a list of all available Flavors.
[ "Returns", "a", "list", "of", "all", "available", "Flavors", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L842-L844
17,878
pycontribs/pyrax
pyrax/clouddatabases.py
CloudDatabaseClient._get_flavor_ref
def _get_flavor_ref(self, flavor): """ Flavors are odd in that the API expects an href link, not an ID, as with nearly every other resource. This method takes either a CloudDatabaseFlavor object, a flavor ID, a RAM size, or a flavor name, and uses that to determine the appropriate href. """ flavor_obj = None if isinstance(flavor, CloudDatabaseFlavor): flavor_obj = flavor elif isinstance(flavor, int): # They passed an ID or a size try: flavor_obj = self.get_flavor(flavor) except exc.NotFound: # Must be either a size or bad ID, which will # be handled below pass if flavor_obj is None: # Try flavor name flavors = self.list_flavors() try: flavor_obj = [flav for flav in flavors if flav.name == flavor][0] except IndexError: # No such name; try matching RAM try: flavor_obj = [flav for flav in flavors if flav.ram == flavor][0] except IndexError: raise exc.FlavorNotFound("Could not determine flavor from " "'%s'." % flavor) # OK, we have a Flavor object. Get the href href = [link["href"] for link in flavor_obj.links if link["rel"] == "self"][0] return href
python
def _get_flavor_ref(self, flavor): flavor_obj = None if isinstance(flavor, CloudDatabaseFlavor): flavor_obj = flavor elif isinstance(flavor, int): # They passed an ID or a size try: flavor_obj = self.get_flavor(flavor) except exc.NotFound: # Must be either a size or bad ID, which will # be handled below pass if flavor_obj is None: # Try flavor name flavors = self.list_flavors() try: flavor_obj = [flav for flav in flavors if flav.name == flavor][0] except IndexError: # No such name; try matching RAM try: flavor_obj = [flav for flav in flavors if flav.ram == flavor][0] except IndexError: raise exc.FlavorNotFound("Could not determine flavor from " "'%s'." % flavor) # OK, we have a Flavor object. Get the href href = [link["href"] for link in flavor_obj.links if link["rel"] == "self"][0] return href
[ "def", "_get_flavor_ref", "(", "self", ",", "flavor", ")", ":", "flavor_obj", "=", "None", "if", "isinstance", "(", "flavor", ",", "CloudDatabaseFlavor", ")", ":", "flavor_obj", "=", "flavor", "elif", "isinstance", "(", "flavor", ",", "int", ")", ":", "# T...
Flavors are odd in that the API expects an href link, not an ID, as with nearly every other resource. This method takes either a CloudDatabaseFlavor object, a flavor ID, a RAM size, or a flavor name, and uses that to determine the appropriate href.
[ "Flavors", "are", "odd", "in", "that", "the", "API", "expects", "an", "href", "link", "not", "an", "ID", "as", "with", "nearly", "every", "other", "resource", ".", "This", "method", "takes", "either", "a", "CloudDatabaseFlavor", "object", "a", "flavor", "I...
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/clouddatabases.py#L852-L887
17,879
pycontribs/pyrax
pyrax/utils.py
runproc
def runproc(cmd): """ Convenience method for executing operating system commands. Accepts a single string that would be the command as executed on the command line. Returns a 2-tuple consisting of the output of (STDOUT, STDERR). In your code you should check for an empty STDERR output to determine if your command completed successfully. """ proc = Popen([cmd], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) stdoutdata, stderrdata = proc.communicate() return (stdoutdata, stderrdata)
python
def runproc(cmd): proc = Popen([cmd], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) stdoutdata, stderrdata = proc.communicate() return (stdoutdata, stderrdata)
[ "def", "runproc", "(", "cmd", ")", ":", "proc", "=", "Popen", "(", "[", "cmd", "]", ",", "shell", "=", "True", ",", "stdin", "=", "PIPE", ",", "stdout", "=", "PIPE", ",", "stderr", "=", "PIPE", ",", "close_fds", "=", "True", ")", "stdoutdata", ",...
Convenience method for executing operating system commands. Accepts a single string that would be the command as executed on the command line. Returns a 2-tuple consisting of the output of (STDOUT, STDERR). In your code you should check for an empty STDERR output to determine if your command completed successfully.
[ "Convenience", "method", "for", "executing", "operating", "system", "commands", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L39-L53
17,880
pycontribs/pyrax
pyrax/utils.py
_join_chars
def _join_chars(chars, length): """ Used by the random character functions. """ mult = int(length / len(chars)) + 1 mult_chars = chars * mult return "".join(random.sample(mult_chars, length))
python
def _join_chars(chars, length): mult = int(length / len(chars)) + 1 mult_chars = chars * mult return "".join(random.sample(mult_chars, length))
[ "def", "_join_chars", "(", "chars", ",", "length", ")", ":", "mult", "=", "int", "(", "length", "/", "len", "(", "chars", ")", ")", "+", "1", "mult_chars", "=", "chars", "*", "mult", "return", "\"\"", ".", "join", "(", "random", ".", "sample", "(",...
Used by the random character functions.
[ "Used", "by", "the", "random", "character", "functions", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L268-L274
17,881
pycontribs/pyrax
pyrax/utils.py
random_unicode
def random_unicode(length=20): """ Generates a random name; useful for testing. Returns an encoded string of the specified length containing unicode values up to code point 1000. """ def get_char(): return six.unichr(random.randint(32, 1000)) chars = u"".join([get_char() for ii in six.moves.range(length)]) return _join_chars(chars, length)
python
def random_unicode(length=20): def get_char(): return six.unichr(random.randint(32, 1000)) chars = u"".join([get_char() for ii in six.moves.range(length)]) return _join_chars(chars, length)
[ "def", "random_unicode", "(", "length", "=", "20", ")", ":", "def", "get_char", "(", ")", ":", "return", "six", ".", "unichr", "(", "random", ".", "randint", "(", "32", ",", "1000", ")", ")", "chars", "=", "u\"\"", ".", "join", "(", "[", "get_char"...
Generates a random name; useful for testing. Returns an encoded string of the specified length containing unicode values up to code point 1000.
[ "Generates", "a", "random", "name", ";", "useful", "for", "testing", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L277-L287
17,882
pycontribs/pyrax
pyrax/utils.py
coerce_to_list
def coerce_to_list(val): """ For parameters that can take either a single string or a list of strings, this function will ensure that the result is a list containing the passed values. """ if val: if not isinstance(val, (list, tuple)): val = [val] else: val = [] return val
python
def coerce_to_list(val): if val: if not isinstance(val, (list, tuple)): val = [val] else: val = [] return val
[ "def", "coerce_to_list", "(", "val", ")", ":", "if", "val", ":", "if", "not", "isinstance", "(", "val", ",", "(", "list", ",", "tuple", ")", ")", ":", "val", "=", "[", "val", "]", "else", ":", "val", "=", "[", "]", "return", "val" ]
For parameters that can take either a single string or a list of strings, this function will ensure that the result is a list containing the passed values.
[ "For", "parameters", "that", "can", "take", "either", "a", "single", "string", "or", "a", "list", "of", "strings", "this", "function", "will", "ensure", "that", "the", "result", "is", "a", "list", "containing", "the", "passed", "values", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L299-L310
17,883
pycontribs/pyrax
pyrax/utils.py
folder_size
def folder_size(pth, ignore=None): """ Returns the total bytes for the specified path, optionally ignoring any files which match the 'ignore' parameter. 'ignore' can either be a single string pattern, or a list of such patterns. """ if not os.path.isdir(pth): raise exc.FolderNotFound ignore = coerce_to_list(ignore) total = 0 for root, _, names in os.walk(pth): paths = [os.path.realpath(os.path.join(root, nm)) for nm in names] for pth in paths[::-1]: if not os.path.exists(pth): paths.remove(pth) elif match_pattern(pth, ignore): paths.remove(pth) total += sum(os.stat(pth).st_size for pth in paths) return total
python
def folder_size(pth, ignore=None): if not os.path.isdir(pth): raise exc.FolderNotFound ignore = coerce_to_list(ignore) total = 0 for root, _, names in os.walk(pth): paths = [os.path.realpath(os.path.join(root, nm)) for nm in names] for pth in paths[::-1]: if not os.path.exists(pth): paths.remove(pth) elif match_pattern(pth, ignore): paths.remove(pth) total += sum(os.stat(pth).st_size for pth in paths) return total
[ "def", "folder_size", "(", "pth", ",", "ignore", "=", "None", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "pth", ")", ":", "raise", "exc", ".", "FolderNotFound", "ignore", "=", "coerce_to_list", "(", "ignore", ")", "total", "=", "0",...
Returns the total bytes for the specified path, optionally ignoring any files which match the 'ignore' parameter. 'ignore' can either be a single string pattern, or a list of such patterns.
[ "Returns", "the", "total", "bytes", "for", "the", "specified", "path", "optionally", "ignoring", "any", "files", "which", "match", "the", "ignore", "parameter", ".", "ignore", "can", "either", "be", "a", "single", "string", "pattern", "or", "a", "list", "of"...
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L313-L334
17,884
pycontribs/pyrax
pyrax/utils.py
add_method
def add_method(obj, func, name=None): """Adds an instance method to an object.""" if name is None: name = func.__name__ if sys.version_info < (3,): method = types.MethodType(func, obj, obj.__class__) else: method = types.MethodType(func, obj) setattr(obj, name, method)
python
def add_method(obj, func, name=None): if name is None: name = func.__name__ if sys.version_info < (3,): method = types.MethodType(func, obj, obj.__class__) else: method = types.MethodType(func, obj) setattr(obj, name, method)
[ "def", "add_method", "(", "obj", ",", "func", ",", "name", "=", "None", ")", ":", "if", "name", "is", "None", ":", "name", "=", "func", ".", "__name__", "if", "sys", ".", "version_info", "<", "(", "3", ",", ")", ":", "method", "=", "types", ".", ...
Adds an instance method to an object.
[ "Adds", "an", "instance", "method", "to", "an", "object", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L337-L345
17,885
pycontribs/pyrax
pyrax/utils.py
wait_until
def wait_until(obj, att, desired, callback=None, interval=5, attempts=0, verbose=False, verbose_atts=None): """ When changing the state of an object, it will commonly be in a transitional state until the change is complete. This will reload the object every `interval` seconds, and check its `att` attribute until the `desired` value is reached, or until the maximum number of attempts is reached. The updated object is returned. It is up to the calling program to check the returned object to make sure that it successfully reached the desired state. Once the desired value of the attribute is reached, the method returns. If not, it will re-try until the attribute's value matches one of the `desired` values. By default (attempts=0) it will loop infinitely until the attribute reaches the desired value. You can optionally limit the number of times that the object is reloaded by passing a positive value to `attempts`. If the attribute has not reached the desired value by then, the method will exit. If `verbose` is True, each attempt will print out the current value of the watched attribute and the time that has elapsed since the original request. Also, if `verbose_atts` is specified, the values of those attributes will also be output. If `verbose` is False, then `verbose_atts` has no effect. Note that `desired` can be a list of values; if the attribute becomes equal to any of those values, this will succeed. For example, when creating a new cloud server, it will initially have a status of 'BUILD', and you can't work with it until its status is 'ACTIVE'. However, there might be a problem with the build process, and the server will change to a status of 'ERROR'. So for this case you need to set the `desired` parameter to `['ACTIVE', 'ERROR']`. If you simply pass 'ACTIVE' as the desired state, this will loop indefinitely if a build fails, as the server will never reach a status of 'ACTIVE'. Since this process of waiting can take a potentially long time, and will block your program's execution until the desired state of the object is reached, you may specify a callback function. The callback can be any callable that accepts a single parameter; the parameter it receives will be either the updated object (success), or None (failure). If a callback is specified, the program will return immediately after spawning the wait process in a separate thread. """ if callback: waiter = _WaitThread(obj=obj, att=att, desired=desired, callback=callback, interval=interval, attempts=attempts, verbose=verbose, verbose_atts=verbose_atts) waiter.start() return waiter else: return _wait_until(obj=obj, att=att, desired=desired, callback=None, interval=interval, attempts=attempts, verbose=verbose, verbose_atts=verbose_atts)
python
def wait_until(obj, att, desired, callback=None, interval=5, attempts=0, verbose=False, verbose_atts=None): if callback: waiter = _WaitThread(obj=obj, att=att, desired=desired, callback=callback, interval=interval, attempts=attempts, verbose=verbose, verbose_atts=verbose_atts) waiter.start() return waiter else: return _wait_until(obj=obj, att=att, desired=desired, callback=None, interval=interval, attempts=attempts, verbose=verbose, verbose_atts=verbose_atts)
[ "def", "wait_until", "(", "obj", ",", "att", ",", "desired", ",", "callback", "=", "None", ",", "interval", "=", "5", ",", "attempts", "=", "0", ",", "verbose", "=", "False", ",", "verbose_atts", "=", "None", ")", ":", "if", "callback", ":", "waiter"...
When changing the state of an object, it will commonly be in a transitional state until the change is complete. This will reload the object every `interval` seconds, and check its `att` attribute until the `desired` value is reached, or until the maximum number of attempts is reached. The updated object is returned. It is up to the calling program to check the returned object to make sure that it successfully reached the desired state. Once the desired value of the attribute is reached, the method returns. If not, it will re-try until the attribute's value matches one of the `desired` values. By default (attempts=0) it will loop infinitely until the attribute reaches the desired value. You can optionally limit the number of times that the object is reloaded by passing a positive value to `attempts`. If the attribute has not reached the desired value by then, the method will exit. If `verbose` is True, each attempt will print out the current value of the watched attribute and the time that has elapsed since the original request. Also, if `verbose_atts` is specified, the values of those attributes will also be output. If `verbose` is False, then `verbose_atts` has no effect. Note that `desired` can be a list of values; if the attribute becomes equal to any of those values, this will succeed. For example, when creating a new cloud server, it will initially have a status of 'BUILD', and you can't work with it until its status is 'ACTIVE'. However, there might be a problem with the build process, and the server will change to a status of 'ERROR'. So for this case you need to set the `desired` parameter to `['ACTIVE', 'ERROR']`. If you simply pass 'ACTIVE' as the desired state, this will loop indefinitely if a build fails, as the server will never reach a status of 'ACTIVE'. Since this process of waiting can take a potentially long time, and will block your program's execution until the desired state of the object is reached, you may specify a callback function. The callback can be any callable that accepts a single parameter; the parameter it receives will be either the updated object (success), or None (failure). If a callback is specified, the program will return immediately after spawning the wait process in a separate thread.
[ "When", "changing", "the", "state", "of", "an", "object", "it", "will", "commonly", "be", "in", "a", "transitional", "state", "until", "the", "change", "is", "complete", ".", "This", "will", "reload", "the", "object", "every", "interval", "seconds", "and", ...
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L373-L423
17,886
pycontribs/pyrax
pyrax/utils.py
_wait_until
def _wait_until(obj, att, desired, callback, interval, attempts, verbose, verbose_atts): """ Loops until either the desired value of the attribute is reached, or the number of attempts is exceeded. """ if not isinstance(desired, (list, tuple)): desired = [desired] if verbose_atts is None: verbose_atts = [] if not isinstance(verbose_atts, (list, tuple)): verbose_atts = [verbose_atts] infinite = (attempts == 0) attempt = 0 start = time.time() while infinite or (attempt < attempts): try: # For servers: obj.get() except AttributeError: try: # For other objects that don't support .get() obj = obj.manager.get(obj.id) except AttributeError: # punt raise exc.NoReloadError("The 'wait_until' method is not " "supported for '%s' objects." % obj.__class__) attval = getattr(obj, att) if verbose: elapsed = time.time() - start msgs = ["Current value of %s: %s (elapsed: %4.1f seconds)" % ( att, attval, elapsed)] for vatt in verbose_atts: vattval = getattr(obj, vatt, None) msgs.append("%s=%s" % (vatt, vattval)) print(" ".join(msgs)) if attval in desired: return obj time.sleep(interval) attempt += 1 return obj
python
def _wait_until(obj, att, desired, callback, interval, attempts, verbose, verbose_atts): if not isinstance(desired, (list, tuple)): desired = [desired] if verbose_atts is None: verbose_atts = [] if not isinstance(verbose_atts, (list, tuple)): verbose_atts = [verbose_atts] infinite = (attempts == 0) attempt = 0 start = time.time() while infinite or (attempt < attempts): try: # For servers: obj.get() except AttributeError: try: # For other objects that don't support .get() obj = obj.manager.get(obj.id) except AttributeError: # punt raise exc.NoReloadError("The 'wait_until' method is not " "supported for '%s' objects." % obj.__class__) attval = getattr(obj, att) if verbose: elapsed = time.time() - start msgs = ["Current value of %s: %s (elapsed: %4.1f seconds)" % ( att, attval, elapsed)] for vatt in verbose_atts: vattval = getattr(obj, vatt, None) msgs.append("%s=%s" % (vatt, vattval)) print(" ".join(msgs)) if attval in desired: return obj time.sleep(interval) attempt += 1 return obj
[ "def", "_wait_until", "(", "obj", ",", "att", ",", "desired", ",", "callback", ",", "interval", ",", "attempts", ",", "verbose", ",", "verbose_atts", ")", ":", "if", "not", "isinstance", "(", "desired", ",", "(", "list", ",", "tuple", ")", ")", ":", ...
Loops until either the desired value of the attribute is reached, or the number of attempts is exceeded.
[ "Loops", "until", "either", "the", "desired", "value", "of", "the", "attribute", "is", "reached", "or", "the", "number", "of", "attempts", "is", "exceeded", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L426-L466
17,887
pycontribs/pyrax
pyrax/utils.py
_parse_datetime_string
def _parse_datetime_string(val): """ Attempts to parse a string representation of a date or datetime value, and returns a datetime if successful. If not, a InvalidDateTimeString exception will be raised. """ dt = None lenval = len(val) fmt = {19: "%Y-%m-%d %H:%M:%S", 10: "%Y-%m-%d"}.get(lenval) if fmt is None: # Invalid date raise exc.InvalidDateTimeString("The supplied value '%s' does not " "match either of the formats 'YYYY-MM-DD HH:MM:SS' or " "'YYYY-MM-DD'." % val) return datetime.datetime.strptime(val, fmt)
python
def _parse_datetime_string(val): dt = None lenval = len(val) fmt = {19: "%Y-%m-%d %H:%M:%S", 10: "%Y-%m-%d"}.get(lenval) if fmt is None: # Invalid date raise exc.InvalidDateTimeString("The supplied value '%s' does not " "match either of the formats 'YYYY-MM-DD HH:MM:SS' or " "'YYYY-MM-DD'." % val) return datetime.datetime.strptime(val, fmt)
[ "def", "_parse_datetime_string", "(", "val", ")", ":", "dt", "=", "None", "lenval", "=", "len", "(", "val", ")", "fmt", "=", "{", "19", ":", "\"%Y-%m-%d %H:%M:%S\"", ",", "10", ":", "\"%Y-%m-%d\"", "}", ".", "get", "(", "lenval", ")", "if", "fmt", "i...
Attempts to parse a string representation of a date or datetime value, and returns a datetime if successful. If not, a InvalidDateTimeString exception will be raised.
[ "Attempts", "to", "parse", "a", "string", "representation", "of", "a", "date", "or", "datetime", "value", "and", "returns", "a", "datetime", "if", "successful", ".", "If", "not", "a", "InvalidDateTimeString", "exception", "will", "be", "raised", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L487-L501
17,888
pycontribs/pyrax
pyrax/utils.py
rfc2822_format
def rfc2822_format(val): """ Takes either a date, a datetime, or a string, and returns a string that represents the value in RFC 2822 format. If a string is passed it is returned unchanged. """ if isinstance(val, six.string_types): return val elif isinstance(val, (datetime.datetime, datetime.date)): # Convert to a timestamp val = time.mktime(val.timetuple()) if isinstance(val, numbers.Number): return email.utils.formatdate(val) else: # Bail return val
python
def rfc2822_format(val): if isinstance(val, six.string_types): return val elif isinstance(val, (datetime.datetime, datetime.date)): # Convert to a timestamp val = time.mktime(val.timetuple()) if isinstance(val, numbers.Number): return email.utils.formatdate(val) else: # Bail return val
[ "def", "rfc2822_format", "(", "val", ")", ":", "if", "isinstance", "(", "val", ",", "six", ".", "string_types", ")", ":", "return", "val", "elif", "isinstance", "(", "val", ",", "(", "datetime", ".", "datetime", ",", "datetime", ".", "date", ")", ")", ...
Takes either a date, a datetime, or a string, and returns a string that represents the value in RFC 2822 format. If a string is passed it is returned unchanged.
[ "Takes", "either", "a", "date", "a", "datetime", "or", "a", "string", "and", "returns", "a", "string", "that", "represents", "the", "value", "in", "RFC", "2822", "format", ".", "If", "a", "string", "is", "passed", "it", "is", "returned", "unchanged", "."...
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L531-L546
17,889
pycontribs/pyrax
pyrax/utils.py
get_id
def get_id(id_or_obj): """ Returns the 'id' attribute of 'id_or_obj' if present; if not, returns 'id_or_obj'. """ if isinstance(id_or_obj, six.string_types + (int,)): # It's an ID return id_or_obj try: return id_or_obj.id except AttributeError: return id_or_obj
python
def get_id(id_or_obj): if isinstance(id_or_obj, six.string_types + (int,)): # It's an ID return id_or_obj try: return id_or_obj.id except AttributeError: return id_or_obj
[ "def", "get_id", "(", "id_or_obj", ")", ":", "if", "isinstance", "(", "id_or_obj", ",", "six", ".", "string_types", "+", "(", "int", ",", ")", ")", ":", "# It's an ID", "return", "id_or_obj", "try", ":", "return", "id_or_obj", ".", "id", "except", "Attri...
Returns the 'id' attribute of 'id_or_obj' if present; if not, returns 'id_or_obj'.
[ "Returns", "the", "id", "attribute", "of", "id_or_obj", "if", "present", ";", "if", "not", "returns", "id_or_obj", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L565-L576
17,890
pycontribs/pyrax
pyrax/utils.py
get_name
def get_name(name_or_obj): """ Returns the 'name' attribute of 'name_or_obj' if present; if not, returns 'name_or_obj'. """ if isinstance(name_or_obj, six.string_types): # It's a name return name_or_obj try: return name_or_obj.name except AttributeError: raise exc.MissingName(name_or_obj)
python
def get_name(name_or_obj): if isinstance(name_or_obj, six.string_types): # It's a name return name_or_obj try: return name_or_obj.name except AttributeError: raise exc.MissingName(name_or_obj)
[ "def", "get_name", "(", "name_or_obj", ")", ":", "if", "isinstance", "(", "name_or_obj", ",", "six", ".", "string_types", ")", ":", "# It's a name", "return", "name_or_obj", "try", ":", "return", "name_or_obj", ".", "name", "except", "AttributeError", ":", "ra...
Returns the 'name' attribute of 'name_or_obj' if present; if not, returns 'name_or_obj'.
[ "Returns", "the", "name", "attribute", "of", "name_or_obj", "if", "present", ";", "if", "not", "returns", "name_or_obj", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L579-L590
17,891
pycontribs/pyrax
pyrax/utils.py
params_to_dict
def params_to_dict(params, dct): """ Updates the 'dct' dictionary with the 'params' dictionary, filtering out all those whose param value is None. """ for param, val in params.items(): if val is None: continue dct[param] = val return dct
python
def params_to_dict(params, dct): for param, val in params.items(): if val is None: continue dct[param] = val return dct
[ "def", "params_to_dict", "(", "params", ",", "dct", ")", ":", "for", "param", ",", "val", "in", "params", ".", "items", "(", ")", ":", "if", "val", "is", "None", ":", "continue", "dct", "[", "param", "]", "=", "val", "return", "dct" ]
Updates the 'dct' dictionary with the 'params' dictionary, filtering out all those whose param value is None.
[ "Updates", "the", "dct", "dictionary", "with", "the", "params", "dictionary", "filtering", "out", "all", "those", "whose", "param", "value", "is", "None", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L593-L602
17,892
pycontribs/pyrax
pyrax/utils.py
dict_to_qs
def dict_to_qs(dct): """ Takes a dictionary and uses it to create a query string. """ itms = ["%s=%s" % (key, val) for key, val in list(dct.items()) if val is not None] return "&".join(itms)
python
def dict_to_qs(dct): itms = ["%s=%s" % (key, val) for key, val in list(dct.items()) if val is not None] return "&".join(itms)
[ "def", "dict_to_qs", "(", "dct", ")", ":", "itms", "=", "[", "\"%s=%s\"", "%", "(", "key", ",", "val", ")", "for", "key", ",", "val", "in", "list", "(", "dct", ".", "items", "(", ")", ")", "if", "val", "is", "not", "None", "]", "return", "\"&\"...
Takes a dictionary and uses it to create a query string.
[ "Takes", "a", "dictionary", "and", "uses", "it", "to", "create", "a", "query", "string", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L605-L611
17,893
pycontribs/pyrax
pyrax/utils.py
match_pattern
def match_pattern(nm, patterns): """ Compares `nm` with the supplied patterns, and returns True if it matches at least one. Patterns are standard file-name wildcard strings, as defined in the `fnmatch` module. For example, the pattern "*.py" will match the names of all Python scripts. """ patterns = coerce_to_list(patterns) for pat in patterns: if fnmatch.fnmatch(nm, pat): return True return False
python
def match_pattern(nm, patterns): patterns = coerce_to_list(patterns) for pat in patterns: if fnmatch.fnmatch(nm, pat): return True return False
[ "def", "match_pattern", "(", "nm", ",", "patterns", ")", ":", "patterns", "=", "coerce_to_list", "(", "patterns", ")", "for", "pat", "in", "patterns", ":", "if", "fnmatch", ".", "fnmatch", "(", "nm", ",", "pat", ")", ":", "return", "True", "return", "F...
Compares `nm` with the supplied patterns, and returns True if it matches at least one. Patterns are standard file-name wildcard strings, as defined in the `fnmatch` module. For example, the pattern "*.py" will match the names of all Python scripts.
[ "Compares", "nm", "with", "the", "supplied", "patterns", "and", "returns", "True", "if", "it", "matches", "at", "least", "one", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L614-L627
17,894
pycontribs/pyrax
pyrax/utils.py
update_exc
def update_exc(exc, msg, before=True, separator="\n"): """ Adds additional text to an exception's error message. The new text will be added before the existing text by default; to append it after the original text, pass False to the `before` parameter. By default the old and new text will be separated by a newline. If you wish to use a different separator, pass that as the `separator` parameter. """ emsg = exc.message if before: parts = (msg, separator, emsg) else: parts = (emsg, separator, msg) new_msg = "%s%s%s" % parts new_args = (new_msg, ) + exc.args[1:] exc.message = new_msg exc.args = new_args return exc
python
def update_exc(exc, msg, before=True, separator="\n"): emsg = exc.message if before: parts = (msg, separator, emsg) else: parts = (emsg, separator, msg) new_msg = "%s%s%s" % parts new_args = (new_msg, ) + exc.args[1:] exc.message = new_msg exc.args = new_args return exc
[ "def", "update_exc", "(", "exc", ",", "msg", ",", "before", "=", "True", ",", "separator", "=", "\"\\n\"", ")", ":", "emsg", "=", "exc", ".", "message", "if", "before", ":", "parts", "=", "(", "msg", ",", "separator", ",", "emsg", ")", "else", ":",...
Adds additional text to an exception's error message. The new text will be added before the existing text by default; to append it after the original text, pass False to the `before` parameter. By default the old and new text will be separated by a newline. If you wish to use a different separator, pass that as the `separator` parameter.
[ "Adds", "additional", "text", "to", "an", "exception", "s", "error", "message", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L630-L649
17,895
pycontribs/pyrax
pyrax/utils.py
case_insensitive_update
def case_insensitive_update(dct1, dct2): """ Given two dicts, updates the first one with the second, but considers keys that are identical except for case to be the same. No return value; this function modified dct1 similar to the update() method. """ lowkeys = dict([(key.lower(), key) for key in dct1]) for key, val in dct2.items(): d1_key = lowkeys.get(key.lower(), key) dct1[d1_key] = val
python
def case_insensitive_update(dct1, dct2): lowkeys = dict([(key.lower(), key) for key in dct1]) for key, val in dct2.items(): d1_key = lowkeys.get(key.lower(), key) dct1[d1_key] = val
[ "def", "case_insensitive_update", "(", "dct1", ",", "dct2", ")", ":", "lowkeys", "=", "dict", "(", "[", "(", "key", ".", "lower", "(", ")", ",", "key", ")", "for", "key", "in", "dct1", "]", ")", "for", "key", ",", "val", "in", "dct2", ".", "items...
Given two dicts, updates the first one with the second, but considers keys that are identical except for case to be the same. No return value; this function modified dct1 similar to the update() method.
[ "Given", "two", "dicts", "updates", "the", "first", "one", "with", "the", "second", "but", "considers", "keys", "that", "are", "identical", "except", "for", "case", "to", "be", "the", "same", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L652-L662
17,896
pycontribs/pyrax
pyrax/utils.py
env
def env(*args, **kwargs): """ Returns the first environment variable set if none are non-empty, defaults to "" or keyword arg default """ for arg in args: value = os.environ.get(arg, None) if value: return value return kwargs.get("default", "")
python
def env(*args, **kwargs): for arg in args: value = os.environ.get(arg, None) if value: return value return kwargs.get("default", "")
[ "def", "env", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "for", "arg", "in", "args", ":", "value", "=", "os", ".", "environ", ".", "get", "(", "arg", ",", "None", ")", "if", "value", ":", "return", "value", "return", "kwargs", ".", "g...
Returns the first environment variable set if none are non-empty, defaults to "" or keyword arg default
[ "Returns", "the", "first", "environment", "variable", "set", "if", "none", "are", "non", "-", "empty", "defaults", "to", "or", "keyword", "arg", "default" ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L665-L674
17,897
pycontribs/pyrax
pyrax/utils.py
to_slug
def to_slug(value, incoming=None, errors="strict"): """Normalize string. Convert to lowercase, remove non-word characters, and convert spaces to hyphens. This function was copied from novaclient.openstack.strutils Inspired by Django's `slugify` filter. :param value: Text to slugify :param incoming: Text's current encoding :param errors: Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html :returns: slugified unicode representation of `value` :raises TypeError: If text is not an instance of str """ value = safe_decode(value, incoming, errors) # NOTE(aababilov): no need to use safe_(encode|decode) here: # encodings are always "ascii", error handling is always "ignore" # and types are always known (first: unicode; second: str) value = unicodedata.normalize("NFKD", value).encode( "ascii", "ignore").decode("ascii") value = SLUGIFY_STRIP_RE.sub("", value).strip().lower() return SLUGIFY_HYPHENATE_RE.sub("-", value)
python
def to_slug(value, incoming=None, errors="strict"): value = safe_decode(value, incoming, errors) # NOTE(aababilov): no need to use safe_(encode|decode) here: # encodings are always "ascii", error handling is always "ignore" # and types are always known (first: unicode; second: str) value = unicodedata.normalize("NFKD", value).encode( "ascii", "ignore").decode("ascii") value = SLUGIFY_STRIP_RE.sub("", value).strip().lower() return SLUGIFY_HYPHENATE_RE.sub("-", value)
[ "def", "to_slug", "(", "value", ",", "incoming", "=", "None", ",", "errors", "=", "\"strict\"", ")", ":", "value", "=", "safe_decode", "(", "value", ",", "incoming", ",", "errors", ")", "# NOTE(aababilov): no need to use safe_(encode|decode) here:", "# encodings are...
Normalize string. Convert to lowercase, remove non-word characters, and convert spaces to hyphens. This function was copied from novaclient.openstack.strutils Inspired by Django's `slugify` filter. :param value: Text to slugify :param incoming: Text's current encoding :param errors: Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html :returns: slugified unicode representation of `value` :raises TypeError: If text is not an instance of str
[ "Normalize", "string", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L756-L780
17,898
pycontribs/pyrax
pyrax/utils.py
_WaitThread.run
def run(self): """Starts the thread.""" resp = _wait_until(obj=self.obj, att=self.att, desired=self.desired, callback=None, interval=self.interval, attempts=self.attempts, verbose=False, verbose_atts=None) self.callback(resp)
python
def run(self): resp = _wait_until(obj=self.obj, att=self.att, desired=self.desired, callback=None, interval=self.interval, attempts=self.attempts, verbose=False, verbose_atts=None) self.callback(resp)
[ "def", "run", "(", "self", ")", ":", "resp", "=", "_wait_until", "(", "obj", "=", "self", ".", "obj", ",", "att", "=", "self", ".", "att", ",", "desired", "=", "self", ".", "desired", ",", "callback", "=", "None", ",", "interval", "=", "self", "....
Starts the thread.
[ "Starts", "the", "thread", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/utils.py#L364-L370
17,899
pycontribs/pyrax
pyrax/cloudblockstorage.py
assure_volume
def assure_volume(fnc): """ Converts a volumeID passed as the volume to a CloudBlockStorageVolume object. """ @wraps(fnc) def _wrapped(self, volume, *args, **kwargs): if not isinstance(volume, CloudBlockStorageVolume): # Must be the ID volume = self._manager.get(volume) return fnc(self, volume, *args, **kwargs) return _wrapped
python
def assure_volume(fnc): @wraps(fnc) def _wrapped(self, volume, *args, **kwargs): if not isinstance(volume, CloudBlockStorageVolume): # Must be the ID volume = self._manager.get(volume) return fnc(self, volume, *args, **kwargs) return _wrapped
[ "def", "assure_volume", "(", "fnc", ")", ":", "@", "wraps", "(", "fnc", ")", "def", "_wrapped", "(", "self", ",", "volume", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "not", "isinstance", "(", "volume", ",", "CloudBlockStorageVolume", ...
Converts a volumeID passed as the volume to a CloudBlockStorageVolume object.
[ "Converts", "a", "volumeID", "passed", "as", "the", "volume", "to", "a", "CloudBlockStorageVolume", "object", "." ]
9ddfd5064b3a292d7337906f3b2d5dce95b50b99
https://github.com/pycontribs/pyrax/blob/9ddfd5064b3a292d7337906f3b2d5dce95b50b99/pyrax/cloudblockstorage.py#L46-L56