code
stringlengths
31
1.05M
apis
list
extract_api
stringlengths
97
1.91M
# Copyright 2021 Dice Finding Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Calculates best-fit dice poses from an image and estimated bounding boxes, classes, and y-rotations of dice and dots.""" import numpy as np import tensorflow.compat.v2 as tf import cv2 from pycpd import AffineRegistration, RigidRegistration from collections import defaultdict import copy import DiceConfig from CoordsHelper import Pose from typing import Dict, List, Sequence from DiceProjection import get_local_dots_facing_camera_from_eye_space_pose from scipy.optimize import linear_sum_assignment from scipy.spatial.distance import cdist distance_scale_px = 8.0 #Distance scale for calculating non-linear loss functions, similar to distance functions used in robust nonlinear regression, like https://scipy-cookbook.readthedocs.io/items/robust_regression.html approx_distance_score_cutoff_fraction = 1.25 #Fraction of minimum Pose score (of approximate poses) that should be compared by full pose calculation rounded_rectangle_radius = 0.35 #Radius of rounded rectangle for each die's bounding box. Dots at the corners outside the rounded radius are excluded, at they will belong to other die. inlier_cutoff_px = 10 class PoseResult(object): """Represents the result of a pose fit, given in both pyrender and opencv coordinates. Also calculates comparison against found dot positions in the image.""" def __init__(self, pose : Pose, additional_data : Dict, in_pyrender_coords : bool, distance_scale : float = distance_scale_px): if in_pyrender_coords: self.pose_pyrender = pose self.pose_cv = pose.get_converted_between_cv_and_pyrender_coords() else: self.pose_cv = pose self.pose_pyrender = pose.get_converted_between_cv_and_pyrender_coords() self.additional_data = additional_data self.distance_scale = distance_scale self.comparison_points_cv = None self.comparison_camera_matrix = None self.comparison_distortion_coefficients = None self.projected_points = None self.comparison_projected_distances = None self.comparison_soft_l1_distances = None self.comparison_cauchy_distances = None self.comparison_arctan_distances = None self.assignment_score_function = None self.assignment_scores = None self.comparison_indices = None self.projected_indices = None self.matched_scores = None self.matched_scores_rms = None self.calculate_inliers_within_bounding_box = False @property def has_comparison(self): """If comparison has been calculated.""" return self.comparison_results is not None def calculate_comparison(self, dot_centers_cv : np.ndarray, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray): """Calculates comparison against the given found dot center points, using the given camera matrix and distortion coefficients to perform projection.""" self.comparison_points_cv = dot_centers_cv self.comparison_camera_matrix = camera_matrix self.comparison_distortion_coefficients = distortion_coefficients self._perform_comparison() def get_soft_l1_distance_scores(self, distances : np.ndarray): """Calculates soft-l1 distance scores for a set of distances.""" return self.distance_scale * 2.0 * (np.sqrt(1.0 + (distances/self.distance_scale)**2) - 1.0) def get_cauchy_distance_scores(self, distances : np.ndarray): """Calculates Cauchy distance scores for a set of distances.""" return self.distance_scale * np.log1p((distances/self.distance_scale)**2) def get_arctan_distance_scores(self, distances : np.ndarray): """Calculates arctan distance scores for a set of distances.""" return self.distance_scale * np.arctan((distances/self.distance_scale)**2) def _perform_comparison(self): """Performs comparison against the found dot center points, using the given camera matrix and distortion coefficients to perform projection.""" local_dots_visible_in_eye_space = get_local_dots_facing_camera_from_eye_space_pose(self.pose_pyrender) pose_points, pose_points_jacobian = cv2.projectPoints(local_dots_visible_in_eye_space, self.pose_cv.rotation_rodrigues, self.pose_cv.translation, self.comparison_camera_matrix, self.comparison_distortion_coefficients) self.projected_points = np.squeeze(pose_points, axis = 1) #For matching points: See https://stackoverflow.com/questions/41936760/how-to-find-a-unique-set-of-closest-pairs-of-points which suggests using https://en.wikipedia.org/wiki/Hungarian_algorithm on assignment problem. See https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.optimize.linear_sum_assignment.html self.comparison_projected_distances = cdist(self.comparison_points_cv, self.projected_points)#MxN matrix with M comparison_points and N projected points self.comparison_soft_l1_distances = self.get_soft_l1_distance_scores(self.comparison_projected_distances) self.comparison_cauchy_distances = self.get_cauchy_distance_scores(self.comparison_projected_distances) self.comparison_arctan_distances = self.get_arctan_distance_scores(self.comparison_projected_distances) self.assignment_score_function = lambda d : self.get_arctan_distance_scores(d) self.assignment_scores = self.assignment_score_function(self.comparison_projected_distances) self.comparison_indices, self.projected_indices = linear_sum_assignment(self.assignment_scores)#Returns (row_indices, column_indices) self.matched_scores = self.assignment_scores[self.comparison_indices, self.projected_indices] self.matched_scores_rms = np.sqrt(np.mean(np.square(self.matched_scores))) def calculate_inliers(self, dot_bounding_boxes_sizes_cv): """Calculates inliers of projected against found points, potentially including the found dot bounding box sizes in addition to their positions.""" projected_points_ordered = self.projected_points[self.projected_indices, :] found_points_ordered = self.comparison_points_cv[self.comparison_indices, :] differences = projected_points_ordered - found_points_ordered if self.calculate_inliers_within_bounding_box: found_bb_sizes_ordered = dot_bounding_boxes_sizes_cv[self.comparison_indices, :] difference_bb_fractions = differences / found_bb_sizes_ordered abs_bb_fractions = np.abs(difference_bb_fractions) max_bb_fraction = np.max(abs_bb_fractions, axis = -1) inliers = max_bb_fraction < 1.0 else: center_distances = np.linalg.norm(differences, axis = -1) inliers = center_distances < inlier_cutoff_px self.comparison_inlier_indices = tf.boolean_mask(self.comparison_indices, inliers) self.projected_inlier_indices = tf.boolean_mask(self.projected_indices, inliers) def _delete_tf(tensor, idx, axis=0): """Deletes from a tensor along an axis at the given index.""" n = tf.shape(tensor)[axis] t = tf.ones_like(idx, dtype=tf.bool) m = ~tf.scatter_nd(tf.expand_dims(idx, 1), t, [n]) return tf.boolean_mask(tensor, m, axis=axis) def _bounding_box_intersection(bounding_box_cv_1 : np.ndarray, bounding_box_cv_2 : np.ndarray): """Gets the intersection of two OpenCV-coordinate bounding boxes.""" x_min = np.max(bounding_box_cv_1[0], bounding_box_cv_2[0]) y_min = np.max(bounding_box_cv_1[1], bounding_box_cv_2[1]) x_max = np.min(bounding_box_cv_1[2], bounding_box_cv_2[2]) y_max = np.min(bounding_box_cv_1[3], bounding_box_cv_2[3]) intersection = np.array([x_min, y_min, x_max, y_max]) return intersection def _bounding_box_area(bounding_box_cv : np.ndarray, clip_negative = True): """Gets the area of a bounding box.""" width = bounding_box_cv[2] - bounding_box_cv[0] height = bounding_box_cv[3] - bounding_box_cv[1] if clip_negative: width = np.max(width, 0.0) height = np.max(height, 0.0) area = width * height return area def _intersection_over_union(bounding_box_cv_1 : np.ndarray, bounding_box_cv_2 : np.ndarray): """Calculates the intersection over union for two bounding boxes in OpenCV coordinates.""" intersection_bb = _bounding_box_intersection(bounding_box_cv_1, bounding_box_cv_2) area_intersection = _bounding_box_area(intersection_bb) area_1 = _bounding_box_area(bounding_box_cv_1) area_2 = _bounding_box_area(bounding_box_cv_2) iou = area_intersection / (area_1 + area_2 - area_intersection) return iou def _get_dot_centers(dot_bounding_boxes): """Gets the center of Tensor bounding boxes.""" axis_0 = tf.gather(dot_bounding_boxes, [0, 2], axis = -1) axis_1 = tf.gather(dot_bounding_boxes, [1, 3], axis = -1) axis_0_average = tf.reduce_mean(axis_0, axis = -1) axis_1_average = tf.reduce_mean(axis_1, axis = -1) centers = tf.stack([axis_0_average, axis_1_average], axis = -1) return centers def _get_dot_sizes(dot_bounding_boxes): """Gets the size of Tensor bounding boxes.""" axis_0 = dot_bounding_boxes[:, 2] - dot_bounding_boxes[:, 0] axis_1 = dot_bounding_boxes[:, 3] - dot_bounding_boxes[:, 1] sizes = tf.stack([axis_0, axis_1], axis = -1) return sizes def _get_die_local_up_forward_right_axes(die_class : int): """Gets the local up, forward and right axes defined for a given die class (top face).""" die_local_face_normals = DiceConfig.get_local_face_normals() die_local_forward_axis = DiceConfig.get_local_face_forward(die_class) die_local_up_axis = die_local_face_normals[:, die_class - 1] die_local_right_axis = np.cross(die_local_up_axis, die_local_forward_axis) return die_local_up_axis, die_local_forward_axis, die_local_right_axis def _get_approximate_die_pose(die_class : int, y_angle_deg : float, bounding_box_pose_result : PoseResult, x_axis_rotations_deg : Sequence[float], y_rot_offsets_deg : Sequence[float]) -> PoseResult: """Gets an approximate pose given the die class, rotation around vertical axes, and a rough pose result estimated from the bounding box. Checks the given set of rotation offsets rotations around x and y axes.""" bb_pnp_res, bb_rotation, bb_translation = bounding_box_pose_result.pose_cv if bb_pnp_res: from scipy.spatial.transform import Rotation bb_translation_pyrender_coords = bb_translation * np.array([1, -1, -1])[:,np.newaxis] angle_of_translation_deg = np.rad2deg(np.arctan2(-bb_translation_pyrender_coords[0], -bb_translation_pyrender_coords[2])) y_angle_deg_with_position_offset = y_angle_deg + angle_of_translation_deg pose_results = [] die_local_up_axis, die_local_forward_axis, die_local_right_axis = _get_die_local_up_forward_right_axes(die_class) for y_rot_offset_deg in y_rot_offsets_deg: y_angle_final = y_angle_deg_with_position_offset + y_rot_offset_deg angle_cos = np.cos(np.deg2rad(y_angle_final)) angle_sin = np.sin(np.deg2rad(y_angle_final)) die_local_to_scene_rotation = np.eye(3) die_local_to_scene_rotation[0, 0:3] = angle_cos * die_local_right_axis + angle_sin * die_local_forward_axis die_local_to_scene_rotation[1, 0:3] = die_local_up_axis die_local_to_scene_rotation[2, 0:3] = - angle_sin * die_local_right_axis + angle_cos * die_local_forward_axis for x_axis_rotation_deg in x_axis_rotations_deg: x_axis_rotation = Rotation.from_euler('x', x_axis_rotation_deg, degrees = True) x_axis_rotation_matrix = x_axis_rotation.as_matrix() die_combined_rotation = x_axis_rotation_matrix @ die_local_to_scene_rotation die_rotation_rodrigues, die_rotation_rodrigues_jacobian = cv2.Rodrigues(die_combined_rotation) #NB Return in 'pyrender' coordinates approx_pose = Pose(True, die_rotation_rodrigues, bb_translation_pyrender_coords) pose_result = PoseResult(approx_pose, {}, in_pyrender_coords = True) pose_result.y_angle = y_angle_final pose_result.y_angle_rel = y_angle_deg + y_rot_offset_deg pose_result.x_angle = x_axis_rotation_deg pose_results.append(pose_result) return pose_results else: raise NotImplementedError("Cannot get approximate die pose if bounding box PNP was not found.") def _match_points_with_point_cloud_registration(dot_centers_cv : np.ndarray, local_dots_to_project : np.ndarray, approximate_pose_result : PoseResult, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray, matching_style = 'rigid_registration'): """Matches projected (from approximate pose result) with found dot center points, using Coherent Point Drift Algorithm registration between the 2D sets of points. Returns lists of projected and found indices for matching points.""" #How to match (in 2D, in particular)? Affine transformations? etc? Match by (relative) dot-to-dot distances? #See, for example, https://en.wikipedia.org/wiki/Point_set_registration, http://greatpanic.com/pntmatch.html, or Affine Consistency Check of Features in KLT Tracker http://cecas.clemson.edu/~stb/klt/ #Implementations can be found in https://pypi.org/project/pycpd/, ... #Here we'll try to use RigidRegistration or AffineRegistration as an alternative to matching points directly by distance function #While this type of approach can perform fairly well with only a rough approximate pose, we find that a multi-stage matching and pose estimation approach is more consistent. local_dots_projected, local_dots_projected_jacobian = cv2.projectPoints(local_dots_to_project, approximate_pose_result.pose_cv.rotation_rodrigues, approximate_pose_result.pose_cv.translation, camera_matrix, distortion_coefficients) local_dots_projected = np.squeeze(local_dots_projected) registration_dictionary = {'X': dot_centers_cv, 'Y': local_dots_projected} if matching_style == 'affine_registration': registration = AffineRegistration(**registration_dictionary) else: registration = RigidRegistration(**registration_dictionary) registration.register() registration.transform_point_cloud() dot_centers_transformed = registration.TY match_cutoff_score = 0.9 #Registration.P has shape M, N where M is num projected, N num found (in dot_centers_cv) projected_indices_assigned, found_indices_assigned = linear_sum_assignment(1 - registration.P)#Returns (row_indices, column_indices) registration_match_score_mask = registration.P[projected_indices_assigned, found_indices_assigned] > match_cutoff_score projected_indices, found_indices = projected_indices_assigned[registration_match_score_mask], found_indices_assigned[registration_match_score_mask] additional_data = { 'dot_centers_transformed' : dot_centers_transformed, 'local_dots_projected' : local_dots_projected} return projected_indices, found_indices, additional_data def _get_die_pose_from_projected(bounding_box_cv : np.ndarray, dot_centers_cv : np.ndarray, local_dots_to_project : np.ndarray, approximate_pose_result : PoseResult, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray) -> PoseResult: """Determines a die's pose that matches projection of local dots to found dot_centers_cv to solve Perspective-n-Point problem. Uses an approximate pose result as initial guess.""" #Note that we need a candidate pose to start performing matching, since are knows are model points (in 3D) and image-space dot locations (in 2D). #Therefore, we must either project the model points onto the image to 2D (given a candidate pose), or reproject the dot locations back to 3D (again, given a candidate pose). #Since the former might yield some model-space dots incorrectly omitted (since they face away from camera in candidate pose), the 3D matching problem gives more flexibility. #However, without knowing the dot size (image, and model space) there's no way to determine each dot's z-distance from the camera, so we'd need a projective matching/registration algorithm, which would supercede doing something like PNP. if approximate_pose_result.comparison_indices is not None and approximate_pose_result.projected_indices is not None: projected_indices = approximate_pose_result.projected_indices found_indices = approximate_pose_result.comparison_indices additional_data = {} #Note that poorer matches here will be handled with Ransac/outlier exclusion below. else: projected_indices, found_indices, additional_data = _match_points_with_point_cloud_registration(dot_centers_cv, local_dots_to_project, approximate_pose_result, camera_matrix, distortion_coefficients) local_dots_for_pnp_masked = local_dots_to_project[projected_indices, :] dot_centers_cv_masked = dot_centers_cv[found_indices, :] extrinsic_rvec = approximate_pose_result.pose_cv.rotation_rodrigues.copy() extrinsic_tvec = approximate_pose_result.pose_cv.translation.copy() num_dots_min = len(found_indices) inlier_distance = inlier_cutoff_px perform_iterative = False #NB It seems SolvePNP may not work for < 4 points, even in Iterative/useExtrinsicGuess case it claims to handle, and correspondingly the RANSAC version needs at least one more point to be meaningful. if num_dots_min >= 5: pnp_flags = cv2.SOLVEPNP_ITERATIVE matched_pnp = cv2.solvePnPRansac(local_dots_for_pnp_masked, dot_centers_cv_masked, camera_matrix, distortion_coefficients, reprojectionError = inlier_distance, rvec = extrinsic_rvec, tvec = extrinsic_tvec, useExtrinsicGuess = True, flags = pnp_flags) pose_cv = Pose.create_from_cv_results(matched_pnp) if pose_cv: pose_result = PoseResult(pose_cv, additional_data, in_pyrender_coords = False) else: perform_iterative = True elif num_dots_min == 4: four_dot_pnp_flags = cv2.SOLVEPNP_AP3P four_dot_pnp = cv2.solvePnP(local_dots_for_pnp_masked, dot_centers_cv_masked, camera_matrix, distortion_coefficients, flags = four_dot_pnp_flags) four_dot_pnp_pose_cv = Pose.create_from_cv_results(four_dot_pnp) if four_dot_pnp_pose_cv: pose_result = PoseResult(four_dot_pnp_pose_cv, additional_data, in_pyrender_coords = False) pose_result.calculate_comparison(dot_centers_cv_masked, camera_matrix, distortion_coefficients) pose_result_distances = pose_result.comparison_projected_distances[pose_result.comparison_indices, pose_result.projected_indices] all_distances_inliers = np.all(pose_result_distances < inlier_distance) perform_iterative = not all_distances_inliers else: perform_iterative = True else: perform_iterative = True if perform_iterative: iterative_pose_result = _get_die_pose_iterative(bounding_box_cv, dot_centers_cv, approximate_pose_result, camera_matrix, distortion_coefficients, get_reprojection_error_sum_assignment_arctan) pose_result = iterative_pose_result return pose_result def _get_die_pose_from_visible_dots(bounding_box_cv : np.ndarray, dot_centers_cv : np.ndarray, approximate_pose_result : PoseResult, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray) -> PoseResult: """Gets the die's pose from projection of visible dots matched to found dot_centers_cv, given initial approximate pose result.""" local_dots_facing_pyrender = get_local_dots_facing_camera_from_eye_space_pose(approximate_pose_result.pose_pyrender) local_dots_to_project = local_dots_facing_pyrender return _get_die_pose_from_projected(bounding_box_cv, dot_centers_cv, local_dots_to_project, approximate_pose_result, camera_matrix, distortion_coefficients) def _get_local_dots_projected(trial_pose_cv : Pose, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray): """Gets the projected visible dot positions of a die given its pose and camera information.""" local_dots_facing_pyrender = get_local_dots_facing_camera_from_eye_space_pose(trial_pose_cv.get_converted_between_cv_and_pyrender_coords()) local_dots_to_project = local_dots_facing_pyrender local_dots_projected, local_dots_projected_jacobian = cv2.projectPoints(local_dots_to_project, trial_pose_cv.rotation_rodrigues, trial_pose_cv.translation, camera_matrix, distortion_coefficients) local_dots_projected = np.squeeze(local_dots_projected, axis = 1) return local_dots_projected def get_reprojection_error_sum_assignment_arctan(bounding_box_cv : np.ndarray, dot_centers_cv : np.ndarray, trial_pose_cv : Pose, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray): """Gets a reprojection error based on the sum of arctan distances between projected and found points, matched using linear sum assignment.""" local_dots_projected = _get_local_dots_projected(trial_pose_cv, camera_matrix, distortion_coefficients) comparison_projected_distances = cdist(dot_centers_cv, local_dots_projected)#MxN matrix with M comparison_points and N projected points distance_scale = distance_scale_px arctan_scores = distance_scale * np.arctan((comparison_projected_distances/distance_scale)**2) comparison_indices, projected_indices = linear_sum_assignment(arctan_scores)#Returns (row_indices, column_indices) matched_scores = arctan_scores[comparison_indices, projected_indices] matched_scores_rms = np.sqrt(np.mean(np.square(matched_scores))) projected_dots_distance_outside_dice_bb = np.maximum(np.maximum(0, local_dots_projected - bounding_box_cv[2:4]), np.maximum(0, bounding_box_cv[0:2] - local_dots_projected)) max_distance_outside_dice_bb = np.max(projected_dots_distance_outside_dice_bb) final_score = matched_scores_rms + max_distance_outside_dice_bb return final_score def _get_die_pose_iterative(bounding_box_cv : np.ndarray, dot_centers_cv : np.ndarray, approximate_pose_result : PoseResult, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray, reprojection_error_function) -> PoseResult: """Gets a die pose iteratively given a reprojection error function, initial pose estimate, camera information, bounding box and found dot positions dot_centers_cv.""" def get_reprojection_error(trial_pose_rodrigues_translation_cv): trial_pose_rodrigues_cv = trial_pose_rodrigues_translation_cv[0:3] trial_pose_translation_cv = trial_pose_rodrigues_translation_cv[3:] trial_pose_cv = Pose(True, trial_pose_rodrigues_cv, trial_pose_translation_cv) reproj_error = reprojection_error_function(bounding_box_cv, dot_centers_cv, trial_pose_cv, camera_matrix, distortion_coefficients) return reproj_error from scipy.optimize import minimize initial_guess = approximate_pose_result.pose_cv.as_numpy_array() minimization_results = minimize(get_reprojection_error, initial_guess, method = 'Nelder-Mead')#NB Nelder-Mead may not be the most efficient method, but a gradient-free method seems best to handle this particular cost function. minimized_pose = Pose.create_from_numpy_array(minimization_results.x) return PoseResult(minimized_pose, {}, in_pyrender_coords = False) def _convert_tensorflow_points_to_opencv(tensor, transpose = False): """Converts a tensor of points to an OpenCV-coordinate numpy array.""" tensor_points = tensor.numpy().astype(np.float32) if transpose: tensor_points = tensor_points.T for j in range(tensor_points.shape[1] // 2): i =j *2 tensor_points[:, [i, i+1]] = tensor_points[:, [i + 1, i]]#Since TF returns y (row) coordinates first, we need to switch x and y for use with OpenCV return tensor_points def _get_die_image_bounding_box_pose(bounding_box, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray) -> PoseResult: """Get an approximate pose (particularly for translation) by solving PNP problem of the corners of an image-space bounding box.""" box_size = tf.gather(bounding_box, [2, 3], axis = -1) - tf.gather(bounding_box, [0, 1], axis = -1) max_box_dimension = tf.math.reduce_max(tf.math.abs(box_size)) dice_local_bb_min_max_abs = tf.math.abs(DiceConfig.get_local_bounding_box_min_max()) dice_local_bb_extent = tf.math.reduce_max(dice_local_bb_min_max_abs) dice_local_bb_extent = tf.cast(dice_local_bb_extent, tf.float32) #NB Since distance between dots on face 2 is actually slightly greater than to neighbouring dots on other faces, we can't simply cluster based on dot-to-dot distance within each face. quad_scaling_factor = 1.2#Fitting a slightly larger quad than the dice extent will tend to give more accurate distance results when fitting a quad to the image bounding box. quad_with_dice_size = (np.array([[-1, -1, 0],[-1, 1, 0], [1, -1, 0], [1, 1, 0]]) * quad_scaling_factor * dice_local_bb_extent.numpy()).astype(np.float32) bounding_box_corners = tf.stack([tf.gather(bounding_box, [0, 1], axis = -1), tf.gather(bounding_box, [2, 1], axis = -1), tf.gather(bounding_box, [0, 3], axis = -1), tf.gather(bounding_box, [2, 3], axis = -1)], axis = -1) bounding_box_points = _convert_tensorflow_points_to_opencv(bounding_box_corners, transpose = True) quad_pnp_results = cv2.solvePnP(quad_with_dice_size, bounding_box_points, camera_matrix, distortion_coefficients) quad_pnp_pose_cv = Pose.create_from_cv_results(quad_pnp_results) quad_pnp_pose_results = PoseResult(quad_pnp_pose_cv, {}, in_pyrender_coords = False) return quad_pnp_pose_results def _get_die_pose(bounding_box, die_class, die_y_angle, dot_centers_cv : np.ndarray, bounding_box_pose_result : PoseResult, approximate_up_vector_pyrender : np.ndarray, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray) -> PoseResult: """Gets the die pose, given its bounding box, class, estimated rotation angle around vertical axis, found dot center points, approximate pose result and up vector, and camera information.""" die_class_np = die_class.numpy() die_y_angle_deg_np = np.rad2deg(die_y_angle.numpy()) #TODO Handle case of tilted camera (around its forward axis). Up vector could be approximated from plane estimate (if >= 3 dice are found). This would affect the order in which dice bounding box are processed (in order of which die are likely to be in front of others). x_rotation_from_up_vector_deg = np.rad2deg(np.arctan2(approximate_up_vector_pyrender[2], approximate_up_vector_pyrender[1])) potential_die_pose_approx_results = _get_approximate_die_pose(die_class_np, die_y_angle_deg_np, bounding_box_pose_result, x_axis_rotations_deg = np.array([0, 7, -7, 15, -15, 30, -30, 45, -45]) + x_rotation_from_up_vector_deg, y_rot_offsets_deg = [0, 10, -10, 20, -20, 45, -45, 60, -60]) #NB OpenCV coords are x right, y down, z forward. pyrender's are x right, y up, z backwards. Both are right-handed but a 180-degree rotation around the x-axis different. bounding_box_cv = bounding_box.numpy() bounding_box_cv = bounding_box_cv[[1,0,3,2]] bounding_box_cv_size = bounding_box_cv[2:] - bounding_box_cv[0:2] for potential_results in potential_die_pose_approx_results: potential_results.calculate_comparison(dot_centers_cv, camera_matrix, distortion_coefficients) max_num_correlations = max([len(r.matched_scores) for r in potential_die_pose_approx_results]) def get_distance_score_with_missing_residuals(pr : PoseResult, num_correlations): distance_scores = pr.matched_scores missing_residual_score = pr.assignment_score_function(np.max(bounding_box_cv_size) * 0.5) return np.sum(distance_scores) + (num_correlations - len(distance_scores)) * missing_residual_score approx_pose_results_per_projected_indices_set = defaultdict(list) for potential_results in potential_die_pose_approx_results: potential_results.distance_score_with_missing_residuals = get_distance_score_with_missing_residuals(potential_results, max_num_correlations) potential_results.projected_indices_set = frozenset(potential_results.projected_indices) approx_pose_results_per_projected_indices_set[potential_results.projected_indices_set].append(potential_results) best_approx_pose_result_per_projected_indices_set = { indices : min(corresponding_pose_results, key = lambda r : r.distance_score_with_missing_residuals) for (indices, corresponding_pose_results) in approx_pose_results_per_projected_indices_set.items() } best_approx_pose_result = min(best_approx_pose_result_per_projected_indices_set.values(), key = lambda r : r.distance_score_with_missing_residuals) distance_score_cutoff = best_approx_pose_result.distance_score_with_missing_residuals * approx_distance_score_cutoff_fraction visible_fit_pose_result_per_projected_indices_set = { indices : _get_die_pose_from_visible_dots(bounding_box_cv, dot_centers_cv, pose_result, camera_matrix, distortion_coefficients) for (indices, pose_result) in best_approx_pose_result_per_projected_indices_set.items() if pose_result.distance_score_with_missing_residuals < distance_score_cutoff} potential_visible_fit_pose_results = list(visible_fit_pose_result_per_projected_indices_set.values()) if(len(potential_visible_fit_pose_results)) > 0: for pr in potential_visible_fit_pose_results: pr.calculate_comparison(dot_centers_cv, camera_matrix, distortion_coefficients) max_num_correlations_vis = max([len(r.matched_scores) for r in potential_visible_fit_pose_results]) for pr in potential_visible_fit_pose_results: pr.assignment_inlier_cutoff = pr.assignment_score_function(inlier_cutoff_px) inlier_matched_scores = pr.matched_scores[pr.matched_scores < pr.assignment_inlier_cutoff] pr.inlier_matched_scores_rms = np.sqrt(np.mean(np.square(inlier_matched_scores))) pr.distance_score_with_missing_residuals = get_distance_score_with_missing_residuals(potential_results, max_num_correlations_vis) visible_fit_pose_results = min(potential_visible_fit_pose_results, key = lambda r : r.inlier_matched_scores_rms) visible_fit_pose_results.additional_data['approx_pose_result'] = best_approx_pose_result else: visible_fit_pose_results = copy.deepcopy(bounding_box_pose_result) visible_fit_pose_results.additional_data['approx_pose_result'] = best_approx_pose_result return visible_fit_pose_results def _get_normal_up_vector(points_roughly_planar : np.ndarray) -> np.ndarray: """ Estimates the normal upward-facing vector given a set of points roughly defining a horizontal plane points_roughly_planar is a 3xN matrix of points roughly defining a plane. The plane's upward-facing normal will be returned, or a vector most upwardly-pointing in the case of two points. """ normal_up = None if points_roughly_planar.shape[1] < 2: normal_up = np.array([0, 1, 0]) elif points_roughly_planar.shape[1] == 2: difference = points_roughly_planar[:, 1] - points_roughly_planar[:, 0] direction = difference / np.linalg.norm(difference) up = np.array([0, 1, 0]) plane_other_direction = np.cross(direction, up) normal = np.cross(direction, plane_other_direction) normal_up = normal * np.sign(normal[1]) else: mean = points_roughly_planar.mean(axis=1) point_differences = points_roughly_planar - mean[:,np.newaxis] covariance = np.cov(point_differences) svd = np.linalg.svd(covariance) normal = svd[0][:,-1] normal_up = normal * np.sign(normal[1]) return normal_up def _get_approximate_dice_up_vector(bounding_box_pose_results : List[PoseResult], in_pyrender_coords : bool) -> np.ndarray: """Gets an approximate up vector for the die, given approximate pose translations, assuming all die are lying flat on the same plane, thereby pointing upwards.""" if len(bounding_box_pose_results) > 0: if in_pyrender_coords: dice_translations = np.hstack([pr.pose_pyrender.translation for pr in bounding_box_pose_results]) else: dice_translations = np.hstack([pr.pose_cv.translation for pr in bounding_box_pose_results]) else: dice_translations = np.zeros((0, 0)) #3xN matrix of points at center of die approx_up = _get_normal_up_vector(dice_translations) return approx_up def get_dice_pose_results(bounding_boxes, classes, scores, y_rotation_angles, camera_matrix : np.ndarray, distortion_coefficients : np.ndarray, score_threshold : float = 0.5): """Estimates pose results for all die, given estimates for bounding box, die (top face) classes, scores and threshold, rotation angles around vertical axes, and camera information.""" scores_in_threshold = tf.math.greater(scores, score_threshold) classes_in_score = tf.boolean_mask(classes, scores_in_threshold) boxes_in_scores = tf.boolean_mask(bounding_boxes, scores_in_threshold) y_angles_in_scores = tf.boolean_mask(y_rotation_angles, scores_in_threshold) classes_are_dots = tf.equal(classes_in_score, 0) classes_are_dice = tf.logical_not(classes_are_dots) dice_bounding_boxes = tf.boolean_mask(boxes_in_scores, classes_are_dice) dice_y_angles = tf.boolean_mask(y_angles_in_scores, classes_are_dice) dice_classes = tf.boolean_mask(classes_in_score, classes_are_dice) dot_bounding_boxes = tf.boolean_mask(boxes_in_scores, classes_are_dots) dot_centers = _get_dot_centers(dot_bounding_boxes) dot_sizes = _get_dot_sizes(dot_bounding_boxes) #NB Largest box[2] is the box lower bound dice_bb_lower_y = dice_bounding_boxes[:,2] dice_indices = tf.argsort(dice_bb_lower_y, axis = -1, direction='DESCENDING') def get_area(bb): return tf.math.maximum(bb[:, 3] - bb[:, 1], 0) * tf.math.maximum(bb[:, 2] - bb[:, 0], 0) dice_indices_np = dice_indices.numpy() bounding_box_pose_results = [_get_die_image_bounding_box_pose(dice_bounding_boxes[index, :], camera_matrix, distortion_coefficients) for index in dice_indices_np] approximate_dice_up_vector_pyrender = _get_approximate_dice_up_vector(bounding_box_pose_results, in_pyrender_coords=True) pose_results = [] for index, bounding_box_pose_result in zip(dice_indices_np, bounding_box_pose_results): die_box = dice_bounding_boxes[index, :] die_y_angle = dice_y_angles[index] die_class = dice_classes[index] die_box_size = (-die_box[0:2] + die_box[2:4]) dot_centers_fraction_of_die_box = (dot_centers - die_box[0:2]) / die_box_size dot_centers_rounded_rectangle_distance = tf.norm(tf.math.maximum(tf.math.abs(dot_centers_fraction_of_die_box - 0.5) - 0.5 + rounded_rectangle_radius,0.0), axis = -1) - rounded_rectangle_radius dots_are_in_rounded_rectangle = dot_centers_rounded_rectangle_distance < 0 dot_bb_intersection_left = tf.math.maximum(dot_bounding_boxes[:, 1], die_box[1]) dot_bb_intersection_right = tf.math.minimum(dot_bounding_boxes[:, 3], die_box[3]) dot_bb_intersection_top = tf.math.maximum(dot_bounding_boxes[:, 0], die_box[0]) dot_bb_intersection_bottom = tf.math.minimum(dot_bounding_boxes[:, 2], die_box[2]) dot_bb_intersection = tf.stack([dot_bb_intersection_top, dot_bb_intersection_left, dot_bb_intersection_bottom, dot_bb_intersection_right], axis = 1) dot_bb_intersection_area = get_area(dot_bb_intersection) dot_bb_area = get_area(dot_bounding_boxes) dot_bb_intersection_over_area = dot_bb_intersection_area / dot_bb_area dots_have_sufficient_bb_intersection_over_area = tf.greater(dot_bb_intersection_over_area, 0.9) dots_are_in_box = tf.logical_and(dots_have_sufficient_bb_intersection_over_area, dots_are_in_rounded_rectangle) dot_centers_in_box = tf.boolean_mask(dot_centers, dots_are_in_box) dot_centers_cv = _convert_tensorflow_points_to_opencv(dot_centers_in_box) die_pose_result = _get_die_pose(die_box, die_class, die_y_angle, dot_centers_cv, bounding_box_pose_result, approximate_dice_up_vector_pyrender, camera_matrix, distortion_coefficients) die_pose_result.calculate_comparison(dot_centers_cv, camera_matrix, distortion_coefficients) die_pose_result.calculate_inliers(_convert_tensorflow_points_to_opencv(dot_sizes)) pose_results.append(die_pose_result) indices_in_box = tf.where(dots_are_in_box) inlier_indices_in_box = tf.gather(indices_in_box, die_pose_result.comparison_inlier_indices) dot_centers = _delete_tf(dot_centers, inlier_indices_in_box) dot_sizes = _delete_tf(dot_sizes, inlier_indices_in_box) dot_bounding_boxes = _delete_tf(dot_bounding_boxes, inlier_indices_in_box) return pose_results
[ "numpy.maximum", "numpy.arctan2", "numpy.abs", "numpy.sum", "cv2.solvePnP", "collections.defaultdict", "numpy.linalg.svd", "numpy.linalg.norm", "tensorflow.compat.v2.expand_dims", "DiceConfig.get_local_face_forward", "tensorflow.compat.v2.math.greater", "DiceProjection.get_local_dots_facing_camera_from_eye_space_pose", "scipy.optimize.minimize", "tensorflow.compat.v2.math.abs", "tensorflow.compat.v2.math.maximum", "pycpd.AffineRegistration", "DiceConfig.get_local_bounding_box_min_max", "tensorflow.compat.v2.shape", "cv2.solvePnPRansac", "numpy.max", "tensorflow.compat.v2.boolean_mask", "DiceConfig.get_local_face_normals", "CoordsHelper.Pose.create_from_cv_results", "CoordsHelper.Pose", "numpy.cov", "numpy.log1p", "scipy.spatial.distance.cdist", "tensorflow.compat.v2.math.minimum", "copy.deepcopy", "tensorflow.compat.v2.argsort", "tensorflow.compat.v2.where", "tensorflow.compat.v2.stack", "tensorflow.compat.v2.gather", "numpy.square", "numpy.cross", "cv2.projectPoints", "tensorflow.compat.v2.math.reduce_max", "tensorflow.compat.v2.cast", "numpy.hstack", "numpy.min", "cv2.Rodrigues", "numpy.squeeze", "numpy.arctan", "tensorflow.compat.v2.equal", "tensorflow.compat.v2.greater", "tensorflow.compat.v2.reduce_mean", "numpy.all", "scipy.optimize.linear_sum_assignment", "tensorflow.compat.v2.logical_and", "CoordsHelper.Pose.create_from_numpy_array", "pycpd.RigidRegistration", "numpy.deg2rad", "numpy.zeros", "tensorflow.compat.v2.logical_not", "numpy.array", "tensorflow.compat.v2.ones_like", "numpy.sign", "numpy.eye", "scipy.spatial.transform.Rotation.from_euler", "numpy.sqrt" ]
[((7780, 7812), 'tensorflow.compat.v2.ones_like', 'tf.ones_like', (['idx'], {'dtype': 'tf.bool'}), '(idx, dtype=tf.bool)\n', (7792, 7812), True, 'import tensorflow.compat.v2 as tf\n'), ((7879, 7916), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['tensor', 'm'], {'axis': 'axis'}), '(tensor, m, axis=axis)\n', (7894, 7916), True, 'import tensorflow.compat.v2 as tf\n'), ((8099, 8149), 'numpy.max', 'np.max', (['bounding_box_cv_1[0]', 'bounding_box_cv_2[0]'], {}), '(bounding_box_cv_1[0], bounding_box_cv_2[0])\n', (8105, 8149), True, 'import numpy as np\n'), ((8162, 8212), 'numpy.max', 'np.max', (['bounding_box_cv_1[1]', 'bounding_box_cv_2[1]'], {}), '(bounding_box_cv_1[1], bounding_box_cv_2[1])\n', (8168, 8212), True, 'import numpy as np\n'), ((8225, 8275), 'numpy.min', 'np.min', (['bounding_box_cv_1[2]', 'bounding_box_cv_2[2]'], {}), '(bounding_box_cv_1[2], bounding_box_cv_2[2])\n', (8231, 8275), True, 'import numpy as np\n'), ((8288, 8338), 'numpy.min', 'np.min', (['bounding_box_cv_1[3]', 'bounding_box_cv_2[3]'], {}), '(bounding_box_cv_1[3], bounding_box_cv_2[3])\n', (8294, 8338), True, 'import numpy as np\n'), ((8358, 8396), 'numpy.array', 'np.array', (['[x_min, y_min, x_max, y_max]'], {}), '([x_min, y_min, x_max, y_max])\n', (8366, 8396), True, 'import numpy as np\n'), ((9412, 9458), 'tensorflow.compat.v2.gather', 'tf.gather', (['dot_bounding_boxes', '[0, 2]'], {'axis': '(-1)'}), '(dot_bounding_boxes, [0, 2], axis=-1)\n', (9421, 9458), True, 'import tensorflow.compat.v2 as tf\n'), ((9474, 9520), 'tensorflow.compat.v2.gather', 'tf.gather', (['dot_bounding_boxes', '[1, 3]'], {'axis': '(-1)'}), '(dot_bounding_boxes, [1, 3], axis=-1)\n', (9483, 9520), True, 'import tensorflow.compat.v2 as tf\n'), ((9544, 9575), 'tensorflow.compat.v2.reduce_mean', 'tf.reduce_mean', (['axis_0'], {'axis': '(-1)'}), '(axis_0, axis=-1)\n', (9558, 9575), True, 'import tensorflow.compat.v2 as tf\n'), ((9599, 9630), 'tensorflow.compat.v2.reduce_mean', 'tf.reduce_mean', (['axis_1'], {'axis': '(-1)'}), '(axis_1, axis=-1)\n', (9613, 9630), True, 'import tensorflow.compat.v2 as tf\n'), ((9647, 9698), 'tensorflow.compat.v2.stack', 'tf.stack', (['[axis_0_average, axis_1_average]'], {'axis': '(-1)'}), '([axis_0_average, axis_1_average], axis=-1)\n', (9655, 9698), True, 'import tensorflow.compat.v2 as tf\n'), ((9954, 9989), 'tensorflow.compat.v2.stack', 'tf.stack', (['[axis_0, axis_1]'], {'axis': '(-1)'}), '([axis_0, axis_1], axis=-1)\n', (9962, 9989), True, 'import tensorflow.compat.v2 as tf\n'), ((10192, 10227), 'DiceConfig.get_local_face_normals', 'DiceConfig.get_local_face_normals', ([], {}), '()\n', (10225, 10227), False, 'import DiceConfig\n'), ((10257, 10301), 'DiceConfig.get_local_face_forward', 'DiceConfig.get_local_face_forward', (['die_class'], {}), '(die_class)\n', (10290, 10301), False, 'import DiceConfig\n'), ((10394, 10445), 'numpy.cross', 'np.cross', (['die_local_up_axis', 'die_local_forward_axis'], {}), '(die_local_up_axis, die_local_forward_axis)\n', (10402, 10445), True, 'import numpy as np\n'), ((14459, 14645), 'cv2.projectPoints', 'cv2.projectPoints', (['local_dots_to_project', 'approximate_pose_result.pose_cv.rotation_rodrigues', 'approximate_pose_result.pose_cv.translation', 'camera_matrix', 'distortion_coefficients'], {}), '(local_dots_to_project, approximate_pose_result.pose_cv.\n rotation_rodrigues, approximate_pose_result.pose_cv.translation,\n camera_matrix, distortion_coefficients)\n', (14476, 14645), False, 'import cv2\n'), ((14664, 14696), 'numpy.squeeze', 'np.squeeze', (['local_dots_projected'], {}), '(local_dots_projected)\n', (14674, 14696), True, 'import numpy as np\n'), ((15281, 15322), 'scipy.optimize.linear_sum_assignment', 'linear_sum_assignment', (['(1 - registration.P)'], {}), '(1 - registration.P)\n', (15302, 15322), False, 'from scipy.optimize import linear_sum_assignment\n'), ((20358, 20450), 'DiceProjection.get_local_dots_facing_camera_from_eye_space_pose', 'get_local_dots_facing_camera_from_eye_space_pose', (['approximate_pose_result.pose_pyrender'], {}), '(approximate_pose_result.\n pose_pyrender)\n', (20406, 20450), False, 'from DiceProjection import get_local_dots_facing_camera_from_eye_space_pose\n'), ((21139, 21284), 'cv2.projectPoints', 'cv2.projectPoints', (['local_dots_to_project', 'trial_pose_cv.rotation_rodrigues', 'trial_pose_cv.translation', 'camera_matrix', 'distortion_coefficients'], {}), '(local_dots_to_project, trial_pose_cv.rotation_rodrigues,\n trial_pose_cv.translation, camera_matrix, distortion_coefficients)\n', (21156, 21284), False, 'import cv2\n'), ((21308, 21348), 'numpy.squeeze', 'np.squeeze', (['local_dots_projected'], {'axis': '(1)'}), '(local_dots_projected, axis=1)\n', (21318, 21348), True, 'import numpy as np\n'), ((21872, 21915), 'scipy.spatial.distance.cdist', 'cdist', (['dot_centers_cv', 'local_dots_projected'], {}), '(dot_centers_cv, local_dots_projected)\n', (21877, 21915), False, 'from scipy.spatial.distance import cdist\n'), ((22166, 22202), 'scipy.optimize.linear_sum_assignment', 'linear_sum_assignment', (['arctan_scores'], {}), '(arctan_scores)\n', (22187, 22202), False, 'from scipy.optimize import linear_sum_assignment\n'), ((22597, 22644), 'numpy.max', 'np.max', (['projected_dots_distance_outside_dice_bb'], {}), '(projected_dots_distance_outside_dice_bb)\n', (22603, 22644), True, 'import numpy as np\n'), ((23755, 23824), 'scipy.optimize.minimize', 'minimize', (['get_reprojection_error', 'initial_guess'], {'method': '"""Nelder-Mead"""'}), "(get_reprojection_error, initial_guess, method='Nelder-Mead')\n", (23763, 23824), False, 'from scipy.optimize import minimize\n'), ((23980, 24032), 'CoordsHelper.Pose.create_from_numpy_array', 'Pose.create_from_numpy_array', (['minimization_results.x'], {}), '(minimization_results.x)\n', (24008, 24032), False, 'from CoordsHelper import Pose\n'), ((25159, 25204), 'tensorflow.compat.v2.math.reduce_max', 'tf.math.reduce_max', (['dice_local_bb_min_max_abs'], {}), '(dice_local_bb_min_max_abs)\n', (25177, 25204), True, 'import tensorflow.compat.v2 as tf\n'), ((25232, 25273), 'tensorflow.compat.v2.cast', 'tf.cast', (['dice_local_bb_extent', 'tf.float32'], {}), '(dice_local_bb_extent, tf.float32)\n', (25239, 25273), True, 'import tensorflow.compat.v2 as tf\n'), ((26151, 26249), 'cv2.solvePnP', 'cv2.solvePnP', (['quad_with_dice_size', 'bounding_box_points', 'camera_matrix', 'distortion_coefficients'], {}), '(quad_with_dice_size, bounding_box_points, camera_matrix,\n distortion_coefficients)\n', (26163, 26249), False, 'import cv2\n'), ((26269, 26314), 'CoordsHelper.Pose.create_from_cv_results', 'Pose.create_from_cv_results', (['quad_pnp_results'], {}), '(quad_pnp_results)\n', (26296, 26314), False, 'from CoordsHelper import Pose\n'), ((28663, 28680), 'collections.defaultdict', 'defaultdict', (['list'], {}), '(list)\n', (28674, 28680), False, 'from collections import defaultdict\n'), ((33696, 33736), 'tensorflow.compat.v2.math.greater', 'tf.math.greater', (['scores', 'score_threshold'], {}), '(scores, score_threshold)\n', (33711, 33736), True, 'import tensorflow.compat.v2 as tf\n'), ((33760, 33805), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['classes', 'scores_in_threshold'], {}), '(classes, scores_in_threshold)\n', (33775, 33805), True, 'import tensorflow.compat.v2 as tf\n'), ((33828, 33880), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['bounding_boxes', 'scores_in_threshold'], {}), '(bounding_boxes, scores_in_threshold)\n', (33843, 33880), True, 'import tensorflow.compat.v2 as tf\n'), ((33906, 33961), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['y_rotation_angles', 'scores_in_threshold'], {}), '(y_rotation_angles, scores_in_threshold)\n', (33921, 33961), True, 'import tensorflow.compat.v2 as tf\n'), ((33986, 34015), 'tensorflow.compat.v2.equal', 'tf.equal', (['classes_in_score', '(0)'], {}), '(classes_in_score, 0)\n', (33994, 34015), True, 'import tensorflow.compat.v2 as tf\n'), ((34039, 34071), 'tensorflow.compat.v2.logical_not', 'tf.logical_not', (['classes_are_dots'], {}), '(classes_are_dots)\n', (34053, 34071), True, 'import tensorflow.compat.v2 as tf\n'), ((34098, 34148), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['boxes_in_scores', 'classes_are_dice'], {}), '(boxes_in_scores, classes_are_dice)\n', (34113, 34148), True, 'import tensorflow.compat.v2 as tf\n'), ((34169, 34222), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['y_angles_in_scores', 'classes_are_dice'], {}), '(y_angles_in_scores, classes_are_dice)\n', (34184, 34222), True, 'import tensorflow.compat.v2 as tf\n'), ((34242, 34293), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['classes_in_score', 'classes_are_dice'], {}), '(classes_in_score, classes_are_dice)\n', (34257, 34293), True, 'import tensorflow.compat.v2 as tf\n'), ((34319, 34369), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['boxes_in_scores', 'classes_are_dots'], {}), '(boxes_in_scores, classes_are_dots)\n', (34334, 34369), True, 'import tensorflow.compat.v2 as tf\n'), ((34591, 34651), 'tensorflow.compat.v2.argsort', 'tf.argsort', (['dice_bb_lower_y'], {'axis': '(-1)', 'direction': '"""DESCENDING"""'}), "(dice_bb_lower_y, axis=-1, direction='DESCENDING')\n", (34601, 34651), True, 'import tensorflow.compat.v2 as tf\n'), ((4747, 4815), 'DiceProjection.get_local_dots_facing_camera_from_eye_space_pose', 'get_local_dots_facing_camera_from_eye_space_pose', (['self.pose_pyrender'], {}), '(self.pose_pyrender)\n', (4795, 4815), False, 'from DiceProjection import get_local_dots_facing_camera_from_eye_space_pose\n'), ((4860, 5051), 'cv2.projectPoints', 'cv2.projectPoints', (['local_dots_visible_in_eye_space', 'self.pose_cv.rotation_rodrigues', 'self.pose_cv.translation', 'self.comparison_camera_matrix', 'self.comparison_distortion_coefficients'], {}), '(local_dots_visible_in_eye_space, self.pose_cv.\n rotation_rodrigues, self.pose_cv.translation, self.\n comparison_camera_matrix, self.comparison_distortion_coefficients)\n', (4877, 5051), False, 'import cv2\n'), ((5074, 5105), 'numpy.squeeze', 'np.squeeze', (['pose_points'], {'axis': '(1)'}), '(pose_points, axis=1)\n', (5084, 5105), True, 'import numpy as np\n'), ((5485, 5540), 'scipy.spatial.distance.cdist', 'cdist', (['self.comparison_points_cv', 'self.projected_points'], {}), '(self.comparison_points_cv, self.projected_points)\n', (5490, 5540), False, 'from scipy.spatial.distance import cdist\n'), ((6193, 6238), 'scipy.optimize.linear_sum_assignment', 'linear_sum_assignment', (['self.assignment_scores'], {}), '(self.assignment_scores)\n', (6214, 6238), False, 'from scipy.optimize import linear_sum_assignment\n'), ((7498, 7547), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['self.comparison_indices', 'inliers'], {}), '(self.comparison_indices, inliers)\n', (7513, 7547), True, 'import tensorflow.compat.v2 as tf\n'), ((7588, 7636), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['self.projected_indices', 'inliers'], {}), '(self.projected_indices, inliers)\n', (7603, 7636), True, 'import tensorflow.compat.v2 as tf\n'), ((7749, 7765), 'tensorflow.compat.v2.shape', 'tf.shape', (['tensor'], {}), '(tensor)\n', (7757, 7765), True, 'import tensorflow.compat.v2 as tf\n'), ((8684, 8702), 'numpy.max', 'np.max', (['width', '(0.0)'], {}), '(width, 0.0)\n', (8690, 8702), True, 'import numpy as np\n'), ((8720, 8739), 'numpy.max', 'np.max', (['height', '(0.0)'], {}), '(height, 0.0)\n', (8726, 8739), True, 'import numpy as np\n'), ((14852, 14897), 'pycpd.AffineRegistration', 'AffineRegistration', ([], {}), '(**registration_dictionary)\n', (14870, 14897), False, 'from pycpd import AffineRegistration, RigidRegistration\n'), ((14931, 14975), 'pycpd.RigidRegistration', 'RigidRegistration', ([], {}), '(**registration_dictionary)\n', (14948, 14975), False, 'from pycpd import AffineRegistration, RigidRegistration\n'), ((18294, 18533), 'cv2.solvePnPRansac', 'cv2.solvePnPRansac', (['local_dots_for_pnp_masked', 'dot_centers_cv_masked', 'camera_matrix', 'distortion_coefficients'], {'reprojectionError': 'inlier_distance', 'rvec': 'extrinsic_rvec', 'tvec': 'extrinsic_tvec', 'useExtrinsicGuess': '(True)', 'flags': 'pnp_flags'}), '(local_dots_for_pnp_masked, dot_centers_cv_masked,\n camera_matrix, distortion_coefficients, reprojectionError=\n inlier_distance, rvec=extrinsic_rvec, tvec=extrinsic_tvec,\n useExtrinsicGuess=True, flags=pnp_flags)\n', (18312, 18533), False, 'import cv2\n'), ((18549, 18589), 'CoordsHelper.Pose.create_from_cv_results', 'Pose.create_from_cv_results', (['matched_pnp'], {}), '(matched_pnp)\n', (18576, 18589), False, 'from CoordsHelper import Pose\n'), ((22051, 22116), 'numpy.arctan', 'np.arctan', (['((comparison_projected_distances / distance_scale) ** 2)'], {}), '((comparison_projected_distances / distance_scale) ** 2)\n', (22060, 22116), True, 'import numpy as np\n'), ((22442, 22500), 'numpy.maximum', 'np.maximum', (['(0)', '(local_dots_projected - bounding_box_cv[2:4])'], {}), '(0, local_dots_projected - bounding_box_cv[2:4])\n', (22452, 22500), True, 'import numpy as np\n'), ((22502, 22560), 'numpy.maximum', 'np.maximum', (['(0)', '(bounding_box_cv[0:2] - local_dots_projected)'], {}), '(0, bounding_box_cv[0:2] - local_dots_projected)\n', (22512, 22560), True, 'import numpy as np\n'), ((23388, 23450), 'CoordsHelper.Pose', 'Pose', (['(True)', 'trial_pose_rodrigues_cv', 'trial_pose_translation_cv'], {}), '(True, trial_pose_rodrigues_cv, trial_pose_translation_cv)\n', (23392, 23450), False, 'from CoordsHelper import Pose\n'), ((24889, 24929), 'tensorflow.compat.v2.gather', 'tf.gather', (['bounding_box', '[2, 3]'], {'axis': '(-1)'}), '(bounding_box, [2, 3], axis=-1)\n', (24898, 24929), True, 'import tensorflow.compat.v2 as tf\n'), ((24934, 24974), 'tensorflow.compat.v2.gather', 'tf.gather', (['bounding_box', '[0, 1]'], {'axis': '(-1)'}), '(bounding_box, [0, 1], axis=-1)\n', (24943, 24974), True, 'import tensorflow.compat.v2 as tf\n'), ((25020, 25041), 'tensorflow.compat.v2.math.abs', 'tf.math.abs', (['box_size'], {}), '(box_size)\n', (25031, 25041), True, 'import tensorflow.compat.v2 as tf\n'), ((25087, 25130), 'DiceConfig.get_local_bounding_box_min_max', 'DiceConfig.get_local_bounding_box_min_max', ([], {}), '()\n', (25128, 25130), False, 'import DiceConfig\n'), ((27298, 27383), 'numpy.arctan2', 'np.arctan2', (['approximate_up_vector_pyrender[2]', 'approximate_up_vector_pyrender[1]'], {}), '(approximate_up_vector_pyrender[2], approximate_up_vector_pyrender[1]\n )\n', (27308, 27383), True, 'import numpy as np\n'), ((31165, 31204), 'copy.deepcopy', 'copy.deepcopy', (['bounding_box_pose_result'], {}), '(bounding_box_pose_result)\n', (31178, 31204), False, 'import copy\n'), ((31816, 31835), 'numpy.array', 'np.array', (['[0, 1, 0]'], {}), '([0, 1, 0])\n', (31824, 31835), True, 'import numpy as np\n'), ((33167, 33183), 'numpy.zeros', 'np.zeros', (['(0, 0)'], {}), '((0, 0))\n', (33175, 33183), True, 'import numpy as np\n'), ((35817, 35870), 'tensorflow.compat.v2.math.maximum', 'tf.math.maximum', (['dot_bounding_boxes[:, 1]', 'die_box[1]'], {}), '(dot_bounding_boxes[:, 1], die_box[1])\n', (35832, 35870), True, 'import tensorflow.compat.v2 as tf\n'), ((35907, 35960), 'tensorflow.compat.v2.math.minimum', 'tf.math.minimum', (['dot_bounding_boxes[:, 3]', 'die_box[3]'], {}), '(dot_bounding_boxes[:, 3], die_box[3])\n', (35922, 35960), True, 'import tensorflow.compat.v2 as tf\n'), ((35995, 36048), 'tensorflow.compat.v2.math.maximum', 'tf.math.maximum', (['dot_bounding_boxes[:, 0]', 'die_box[0]'], {}), '(dot_bounding_boxes[:, 0], die_box[0])\n', (36010, 36048), True, 'import tensorflow.compat.v2 as tf\n'), ((36086, 36139), 'tensorflow.compat.v2.math.minimum', 'tf.math.minimum', (['dot_bounding_boxes[:, 2]', 'die_box[2]'], {}), '(dot_bounding_boxes[:, 2], die_box[2])\n', (36101, 36139), True, 'import tensorflow.compat.v2 as tf\n'), ((36170, 36298), 'tensorflow.compat.v2.stack', 'tf.stack', (['[dot_bb_intersection_top, dot_bb_intersection_left,\n dot_bb_intersection_bottom, dot_bb_intersection_right]'], {'axis': '(1)'}), '([dot_bb_intersection_top, dot_bb_intersection_left,\n dot_bb_intersection_bottom, dot_bb_intersection_right], axis=1)\n', (36178, 36298), True, 'import tensorflow.compat.v2 as tf\n'), ((36549, 36595), 'tensorflow.compat.v2.greater', 'tf.greater', (['dot_bb_intersection_over_area', '(0.9)'], {}), '(dot_bb_intersection_over_area, 0.9)\n', (36559, 36595), True, 'import tensorflow.compat.v2 as tf\n'), ((36631, 36728), 'tensorflow.compat.v2.logical_and', 'tf.logical_and', (['dots_have_sufficient_bb_intersection_over_area', 'dots_are_in_rounded_rectangle'], {}), '(dots_have_sufficient_bb_intersection_over_area,\n dots_are_in_rounded_rectangle)\n', (36645, 36728), True, 'import tensorflow.compat.v2 as tf\n'), ((36755, 36800), 'tensorflow.compat.v2.boolean_mask', 'tf.boolean_mask', (['dot_centers', 'dots_are_in_box'], {}), '(dot_centers, dots_are_in_box)\n', (36770, 36800), True, 'import tensorflow.compat.v2 as tf\n'), ((37338, 37363), 'tensorflow.compat.v2.where', 'tf.where', (['dots_are_in_box'], {}), '(dots_are_in_box)\n', (37346, 37363), True, 'import tensorflow.compat.v2 as tf\n'), ((37396, 37464), 'tensorflow.compat.v2.gather', 'tf.gather', (['indices_in_box', 'die_pose_result.comparison_inlier_indices'], {}), '(indices_in_box, die_pose_result.comparison_inlier_indices)\n', (37405, 37464), True, 'import tensorflow.compat.v2 as tf\n'), ((4250, 4298), 'numpy.log1p', 'np.log1p', (['((distances / self.distance_scale) ** 2)'], {}), '((distances / self.distance_scale) ** 2)\n', (4258, 4298), True, 'import numpy as np\n'), ((4471, 4520), 'numpy.arctan', 'np.arctan', (['((distances / self.distance_scale) ** 2)'], {}), '((distances / self.distance_scale) ** 2)\n', (4480, 4520), True, 'import numpy as np\n'), ((7173, 7204), 'numpy.abs', 'np.abs', (['difference_bb_fractions'], {}), '(difference_bb_fractions)\n', (7179, 7204), True, 'import numpy as np\n'), ((7235, 7268), 'numpy.max', 'np.max', (['abs_bb_fractions'], {'axis': '(-1)'}), '(abs_bb_fractions, axis=-1)\n', (7241, 7268), True, 'import numpy as np\n'), ((7360, 7396), 'numpy.linalg.norm', 'np.linalg.norm', (['differences'], {'axis': '(-1)'}), '(differences, axis=-1)\n', (7374, 7396), True, 'import numpy as np\n'), ((7836, 7858), 'tensorflow.compat.v2.expand_dims', 'tf.expand_dims', (['idx', '(1)'], {}), '(idx, 1)\n', (7850, 7858), True, 'import tensorflow.compat.v2 as tf\n'), ((11233, 11320), 'numpy.arctan2', 'np.arctan2', (['(-bb_translation_pyrender_coords[0])', '(-bb_translation_pyrender_coords[2])'], {}), '(-bb_translation_pyrender_coords[0], -\n bb_translation_pyrender_coords[2])\n', (11243, 11320), True, 'import numpy as np\n'), ((11837, 11846), 'numpy.eye', 'np.eye', (['(3)'], {}), '(3)\n', (11843, 11846), True, 'import numpy as np\n'), ((18850, 18982), 'cv2.solvePnP', 'cv2.solvePnP', (['local_dots_for_pnp_masked', 'dot_centers_cv_masked', 'camera_matrix', 'distortion_coefficients'], {'flags': 'four_dot_pnp_flags'}), '(local_dots_for_pnp_masked, dot_centers_cv_masked,\n camera_matrix, distortion_coefficients, flags=four_dot_pnp_flags)\n', (18862, 18982), False, 'import cv2\n'), ((19012, 19053), 'CoordsHelper.Pose.create_from_cv_results', 'Pose.create_from_cv_results', (['four_dot_pnp'], {}), '(four_dot_pnp)\n', (19039, 19053), False, 'from CoordsHelper import Pose\n'), ((22356, 22381), 'numpy.square', 'np.square', (['matched_scores'], {}), '(matched_scores)\n', (22365, 22381), True, 'import numpy as np\n'), ((25837, 25877), 'tensorflow.compat.v2.gather', 'tf.gather', (['bounding_box', '[0, 1]'], {'axis': '(-1)'}), '(bounding_box, [0, 1], axis=-1)\n', (25846, 25877), True, 'import tensorflow.compat.v2 as tf\n'), ((25881, 25921), 'tensorflow.compat.v2.gather', 'tf.gather', (['bounding_box', '[2, 1]'], {'axis': '(-1)'}), '(bounding_box, [2, 1], axis=-1)\n', (25890, 25921), True, 'import tensorflow.compat.v2 as tf\n'), ((25925, 25965), 'tensorflow.compat.v2.gather', 'tf.gather', (['bounding_box', '[0, 3]'], {'axis': '(-1)'}), '(bounding_box, [0, 3], axis=-1)\n', (25934, 25965), True, 'import tensorflow.compat.v2 as tf\n'), ((25969, 26009), 'tensorflow.compat.v2.gather', 'tf.gather', (['bounding_box', '[2, 3]'], {'axis': '(-1)'}), '(bounding_box, [2, 3], axis=-1)\n', (25978, 26009), True, 'import tensorflow.compat.v2 as tf\n'), ((28518, 28541), 'numpy.sum', 'np.sum', (['distance_scores'], {}), '(distance_scores)\n', (28524, 28541), True, 'import numpy as np\n'), ((32034, 32053), 'numpy.array', 'np.array', (['[0, 1, 0]'], {}), '([0, 1, 0])\n', (32042, 32053), True, 'import numpy as np\n'), ((32086, 32109), 'numpy.cross', 'np.cross', (['direction', 'up'], {}), '(direction, up)\n', (32094, 32109), True, 'import numpy as np\n'), ((32127, 32169), 'numpy.cross', 'np.cross', (['direction', 'plane_other_direction'], {}), '(direction, plane_other_direction)\n', (32135, 32169), True, 'import numpy as np\n'), ((32370, 32395), 'numpy.cov', 'np.cov', (['point_differences'], {}), '(point_differences)\n', (32376, 32395), True, 'import numpy as np\n'), ((32410, 32435), 'numpy.linalg.svd', 'np.linalg.svd', (['covariance'], {}), '(covariance)\n', (32423, 32435), True, 'import numpy as np\n'), ((32933, 33010), 'numpy.hstack', 'np.hstack', (['[pr.pose_pyrender.translation for pr in bounding_box_pose_results]'], {}), '([pr.pose_pyrender.translation for pr in bounding_box_pose_results])\n', (32942, 33010), True, 'import numpy as np\n'), ((33057, 33128), 'numpy.hstack', 'np.hstack', (['[pr.pose_cv.translation for pr in bounding_box_pose_results]'], {}), '([pr.pose_cv.translation for pr in bounding_box_pose_results])\n', (33066, 33128), True, 'import numpy as np\n'), ((34692, 34731), 'tensorflow.compat.v2.math.maximum', 'tf.math.maximum', (['(bb[:, 3] - bb[:, 1])', '(0)'], {}), '(bb[:, 3] - bb[:, 1], 0)\n', (34707, 34731), True, 'import tensorflow.compat.v2 as tf\n'), ((34734, 34773), 'tensorflow.compat.v2.math.maximum', 'tf.math.maximum', (['(bb[:, 2] - bb[:, 0])', '(0)'], {}), '(bb[:, 2] - bb[:, 0], 0)\n', (34749, 34773), True, 'import tensorflow.compat.v2 as tf\n'), ((4017, 4070), 'numpy.sqrt', 'np.sqrt', (['(1.0 + (distances / self.distance_scale) ** 2)'], {}), '(1.0 + (distances / self.distance_scale) ** 2)\n', (4024, 4070), True, 'import numpy as np\n'), ((6429, 6459), 'numpy.square', 'np.square', (['self.matched_scores'], {}), '(self.matched_scores)\n', (6438, 6459), True, 'import numpy as np\n'), ((11151, 11172), 'numpy.array', 'np.array', (['[1, -1, -1]'], {}), '([1, -1, -1])\n', (11159, 11172), True, 'import numpy as np\n'), ((11710, 11735), 'numpy.deg2rad', 'np.deg2rad', (['y_angle_final'], {}), '(y_angle_final)\n', (11720, 11735), True, 'import numpy as np\n'), ((11768, 11793), 'numpy.deg2rad', 'np.deg2rad', (['y_angle_final'], {}), '(y_angle_final)\n', (11778, 11793), True, 'import numpy as np\n'), ((12254, 12313), 'scipy.spatial.transform.Rotation.from_euler', 'Rotation.from_euler', (['"""x"""', 'x_axis_rotation_deg'], {'degrees': '(True)'}), "('x', x_axis_rotation_deg, degrees=True)\n", (12273, 12313), False, 'from scipy.spatial.transform import Rotation\n'), ((12553, 12589), 'cv2.Rodrigues', 'cv2.Rodrigues', (['die_combined_rotation'], {}), '(die_combined_rotation)\n', (12566, 12589), False, 'import cv2\n'), ((12673, 12739), 'CoordsHelper.Pose', 'Pose', (['(True)', 'die_rotation_rodrigues', 'bb_translation_pyrender_coords'], {}), '(True, die_rotation_rodrigues, bb_translation_pyrender_coords)\n', (12677, 12739), False, 'from CoordsHelper import Pose\n'), ((19477, 19524), 'numpy.all', 'np.all', (['(pose_result_distances < inlier_distance)'], {}), '(pose_result_distances < inlier_distance)\n', (19483, 19524), True, 'import numpy as np\n'), ((27529, 27576), 'numpy.array', 'np.array', (['[0, 7, -7, 15, -15, 30, -30, 45, -45]'], {}), '([0, 7, -7, 15, -15, 30, -30, 45, -45])\n', (27537, 27576), True, 'import numpy as np\n'), ((28467, 28495), 'numpy.max', 'np.max', (['bounding_box_cv_size'], {}), '(bounding_box_cv_size)\n', (28473, 28495), True, 'import numpy as np\n'), ((31994, 32020), 'numpy.linalg.norm', 'np.linalg.norm', (['difference'], {}), '(difference)\n', (32008, 32020), True, 'import numpy as np\n'), ((32199, 32217), 'numpy.sign', 'np.sign', (['normal[1]'], {}), '(normal[1])\n', (32206, 32217), True, 'import numpy as np\n'), ((32495, 32513), 'numpy.sign', 'np.sign', (['normal[1]'], {}), '(normal[1])\n', (32502, 32513), True, 'import numpy as np\n'), ((25669, 25727), 'numpy.array', 'np.array', (['[[-1, -1, 0], [-1, 1, 0], [1, -1, 0], [1, 1, 0]]'], {}), '([[-1, -1, 0], [-1, 1, 0], [1, -1, 0], [1, 1, 0]])\n', (25677, 25727), True, 'import numpy as np\n'), ((30724, 30756), 'numpy.square', 'np.square', (['inlier_matched_scores'], {}), '(inlier_matched_scores)\n', (30733, 30756), True, 'import numpy as np\n'), ((35570, 35620), 'tensorflow.compat.v2.math.abs', 'tf.math.abs', (['(dot_centers_fraction_of_die_box - 0.5)'], {}), '(dot_centers_fraction_of_die_box - 0.5)\n', (35581, 35620), True, 'import tensorflow.compat.v2 as tf\n')]
import numpy as np from scipy import optimize def circle_fit(coords): """ Find the least squares circle fitting a set of 2D points ``(x,y)``. Parameters ---------- coords : (N, 2) ndarray Set of ``x`` and ``y`` coordinates. Returns ------- centre_i : (2,) The 2D coordinates of the centre of the circle. r_i : double The radius of the circle. References ---------- .. [1] http://www.scipy.org/Cookbook/Least_Squares_Circle """ def r_sq_of_circle(coords, centre): return np.mean(np.sum((coords - centre) ** 2, axis=1)) def residuals(p, x, y): x_c, y_c = p err = np.sqrt((x - x_c) ** 2 + (y - y_c) ** 2) return err - err.mean() c_est = np.mean(coords, axis=0) #r_sq = r_sq_of_circle(coords, c_est) centre_i, ier = optimize.leastsq(residuals, c_est, args=(coords[:, 0], coords[:, 1])) r_i = np.sqrt(r_sq_of_circle(coords, centre_i)) return centre_i, r_i
[ "numpy.mean", "numpy.sqrt", "numpy.sum", "scipy.optimize.leastsq" ]
[((762, 785), 'numpy.mean', 'np.mean', (['coords'], {'axis': '(0)'}), '(coords, axis=0)\n', (769, 785), True, 'import numpy as np\n'), ((849, 918), 'scipy.optimize.leastsq', 'optimize.leastsq', (['residuals', 'c_est'], {'args': '(coords[:, 0], coords[:, 1])'}), '(residuals, c_est, args=(coords[:, 0], coords[:, 1]))\n', (865, 918), False, 'from scipy import optimize\n'), ((676, 716), 'numpy.sqrt', 'np.sqrt', (['((x - x_c) ** 2 + (y - y_c) ** 2)'], {}), '((x - x_c) ** 2 + (y - y_c) ** 2)\n', (683, 716), True, 'import numpy as np\n'), ((572, 610), 'numpy.sum', 'np.sum', (['((coords - centre) ** 2)'], {'axis': '(1)'}), '((coords - centre) ** 2, axis=1)\n', (578, 610), True, 'import numpy as np\n')]
import numpy as np class KITTICategory(object): CLASSES = ['Car', 'Pedestrian', 'Cyclist'] CLASS_MEAN_SIZE = { 'Car': np.array([3.88311640418, 1.62856739989, 1.52563191462]), 'Pedestrian': np.array([0.84422524, 0.66068622, 1.76255119]), 'Cyclist': np.array([1.76282397, 0.59706367, 1.73698127]), } NUM_SIZE_CLUSTER = len(CLASSES) MEAN_SIZE_ARRAY = np.zeros((NUM_SIZE_CLUSTER, 3)) # size clustrs for i in range(NUM_SIZE_CLUSTER): MEAN_SIZE_ARRAY[i, :] = CLASS_MEAN_SIZE[CLASSES[i]]
[ "numpy.array", "numpy.zeros" ]
[((400, 431), 'numpy.zeros', 'np.zeros', (['(NUM_SIZE_CLUSTER, 3)'], {}), '((NUM_SIZE_CLUSTER, 3))\n', (408, 431), True, 'import numpy as np\n'), ((139, 194), 'numpy.array', 'np.array', (['[3.88311640418, 1.62856739989, 1.52563191462]'], {}), '([3.88311640418, 1.62856739989, 1.52563191462])\n', (147, 194), True, 'import numpy as np\n'), ((218, 264), 'numpy.array', 'np.array', (['[0.84422524, 0.66068622, 1.76255119]'], {}), '([0.84422524, 0.66068622, 1.76255119])\n', (226, 264), True, 'import numpy as np\n'), ((285, 331), 'numpy.array', 'np.array', (['[1.76282397, 0.59706367, 1.73698127]'], {}), '([1.76282397, 0.59706367, 1.73698127])\n', (293, 331), True, 'import numpy as np\n')]
import numpy as np x = np.ones((2,2)) print("Original array:") print(x) print("0 on the border and 1 inside in the array") x = np.pad(x, pad_width=1, mode='constant', constant_values=0) print(x)
[ "numpy.pad", "numpy.ones" ]
[((24, 39), 'numpy.ones', 'np.ones', (['(2, 2)'], {}), '((2, 2))\n', (31, 39), True, 'import numpy as np\n'), ((132, 190), 'numpy.pad', 'np.pad', (['x'], {'pad_width': '(1)', 'mode': '"""constant"""', 'constant_values': '(0)'}), "(x, pad_width=1, mode='constant', constant_values=0)\n", (138, 190), True, 'import numpy as np\n')]
import cv2 import numpy as np ''' This part is just to see how to capture video from Webcam and show it to you cap = cv2.VideoCapture(0) cap.set(3,640) #width cap.set(4,480) #height cap.set(10,100) #brightness while True: success, img = cap.read() cv2.imshow('Video',img) if cv2.waitKey(1) & 0xFF == ord('q'): break ''' img = cv2.imread("D:\Github\python\emilia_small.jpg") kernal = np.ones((5,5),np.uint8) imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) imgBlur = cv2.GaussianBlur(imgGray,(7,7),0) imgCanny = cv2.Canny(img,150,100) imgDilation = cv2.dilate(imgCanny,kernal,iterations=1) imgEroded = cv2.erode(imgDilation,kernal,iterations=1) ##resizing and cropping height, length, channels = img.shape imgResize = cv2.resize(img,(600,400)) #cv2.imshow('resized',imgResize) imgCropped = imgResize[:int(height/2),:int(length/2)] cv2.imshow('cropped',imgCropped) #cv2.imshow("gray",imgGray) #cv2.imshow("Blur",imgBlur) #cv2.imshow("Canny",imgCanny) #cv2.imshow("dilation",imgDilation) #cv2.imshow("Erode",imgEroded) cv2.waitKey(0)
[ "cv2.GaussianBlur", "cv2.Canny", "cv2.dilate", "cv2.cvtColor", "cv2.waitKey", "numpy.ones", "cv2.imread", "cv2.erode", "cv2.imshow", "cv2.resize" ]
[((354, 404), 'cv2.imread', 'cv2.imread', (['"""D:\\\\Github\\\\python\\\\emilia_small.jpg"""'], {}), "('D:\\\\Github\\\\python\\\\emilia_small.jpg')\n", (364, 404), False, 'import cv2\n'), ((411, 436), 'numpy.ones', 'np.ones', (['(5, 5)', 'np.uint8'], {}), '((5, 5), np.uint8)\n', (418, 436), True, 'import numpy as np\n'), ((446, 483), 'cv2.cvtColor', 'cv2.cvtColor', (['img', 'cv2.COLOR_BGR2GRAY'], {}), '(img, cv2.COLOR_BGR2GRAY)\n', (458, 483), False, 'import cv2\n'), ((493, 529), 'cv2.GaussianBlur', 'cv2.GaussianBlur', (['imgGray', '(7, 7)', '(0)'], {}), '(imgGray, (7, 7), 0)\n', (509, 529), False, 'import cv2\n'), ((538, 562), 'cv2.Canny', 'cv2.Canny', (['img', '(150)', '(100)'], {}), '(img, 150, 100)\n', (547, 562), False, 'import cv2\n'), ((575, 617), 'cv2.dilate', 'cv2.dilate', (['imgCanny', 'kernal'], {'iterations': '(1)'}), '(imgCanny, kernal, iterations=1)\n', (585, 617), False, 'import cv2\n'), ((628, 672), 'cv2.erode', 'cv2.erode', (['imgDilation', 'kernal'], {'iterations': '(1)'}), '(imgDilation, kernal, iterations=1)\n', (637, 672), False, 'import cv2\n'), ((745, 772), 'cv2.resize', 'cv2.resize', (['img', '(600, 400)'], {}), '(img, (600, 400))\n', (755, 772), False, 'import cv2\n'), ((858, 891), 'cv2.imshow', 'cv2.imshow', (['"""cropped"""', 'imgCropped'], {}), "('cropped', imgCropped)\n", (868, 891), False, 'import cv2\n'), ((1045, 1059), 'cv2.waitKey', 'cv2.waitKey', (['(0)'], {}), '(0)\n', (1056, 1059), False, 'import cv2\n')]
import numpy as np from .agent import Agent class RandMinQlearning(Agent): def __init__(self, env, thres, discount=0.9, learning_rate=0.01, epsilon=0.1): super().__init__(env, discount, learning_rate, epsilon) self.name = "RandMin" + str(thres) self.q = np.random.uniform(low=-1, high=1, size=(self.n_states, self.n_actions)) self.old_values = np.random.uniform( low=-1, high=1, size=(self.n_states, self.n_actions) ) self.thres = thres def choose_best_action(self, state): return np.argmax(self.old_values[state]) def calculate_diff(self): return self.env.get_result(self.q, self.discount) def update(self, state, action, r, ns): q_estimate = np.max(self.old_values[ns]) td_target = r + self.discount * q_estimate td_delta = td_target - self.q[state, action] self.q[state, action] += self.lr * td_delta if ( self.q[state, action] <= self.old_values[state, action] or np.random.rand() < self.thres ): self.old_values[state, action] = self.q[state, action]
[ "numpy.random.rand", "numpy.random.uniform", "numpy.max", "numpy.argmax" ]
[((285, 356), 'numpy.random.uniform', 'np.random.uniform', ([], {'low': '(-1)', 'high': '(1)', 'size': '(self.n_states, self.n_actions)'}), '(low=-1, high=1, size=(self.n_states, self.n_actions))\n', (302, 356), True, 'import numpy as np\n'), ((383, 454), 'numpy.random.uniform', 'np.random.uniform', ([], {'low': '(-1)', 'high': '(1)', 'size': '(self.n_states, self.n_actions)'}), '(low=-1, high=1, size=(self.n_states, self.n_actions))\n', (400, 454), True, 'import numpy as np\n'), ((561, 594), 'numpy.argmax', 'np.argmax', (['self.old_values[state]'], {}), '(self.old_values[state])\n', (570, 594), True, 'import numpy as np\n'), ((750, 777), 'numpy.max', 'np.max', (['self.old_values[ns]'], {}), '(self.old_values[ns])\n', (756, 777), True, 'import numpy as np\n'), ((1030, 1046), 'numpy.random.rand', 'np.random.rand', ([], {}), '()\n', (1044, 1046), True, 'import numpy as np\n')]
""" @file: get_circle_point.py @author: Tatsumi0000 @brief: ブロックサークルと交点サークルを囲むだけで各サークルの座標を取得. """ import cv2.cv2 as cv2 import numpy as np class GetCirclePoint: def __init__(self, window_name=None): """コンストラクタ """ self.CROSS_CIRCLE_POINTS = 16 # 交点サークルの個数 self.BLOCK_CIRCLE_POINTS = 8 # ブロックサークルの個数 self.POINTS_NUMBER = 2 # 座標の個数 self.cc_points = np.empty((self.CROSS_CIRCLE_POINTS, self.POINTS_NUMBER), dtype=int) # 交点サークル self.bc_points = np.empty((self.BLOCK_CIRCLE_POINTS, self.POINTS_NUMBER), dtype=int) # ブロックサークル self.ix = self.iy = 0 # 起点となる座標 # サークルを選択するモード.0だと.交点サークルを囲むモード.1だとブロックサークルを囲むモード.2以上だと終了モード self.circle_mode = 0 self.mode = False # Trueだと青い囲む枠が出てくる # 各サークルの座標がX,Y座標が入っている辞書型(BlockPointListの丸パクリ) self.named_points = { "c00": None, "c10": None, "c20": None, "c30": None, "b1": None, "b2": None, "b3": None, "c01": None, "c11": None, "c21": None, "c31": None, "b4": None, "b5": None, "c02": None, "c12": None, "c22": None, "c32": None, "b6": None, "b7": None, "b8": None, "c03": None, "c13": None, "c23": None, "c33": None, } self.window_name = window_name def add(self): """ 交点サークル,ブロックサークルの座標を辞書型に代入する. きちんとソートされていないとめちゃくちゃになるので,取り扱い注意. :return: """ # ブロックサークルの座標を代入. for i in range(self.BLOCK_CIRCLE_POINTS): key = 'b{0}'.format(i + 1) self.named_points[key] = [int(p) for p in self.bc_points[i]] # 交点サークルの座標を代入. cross_circle_count = 0 for i in range(4): for j in range(4): key = 'c{0}{1}'.format(j, i) self.named_points[key] = [int(p) for p in self.cc_points[cross_circle_count]] cross_circle_count += 1 @staticmethod def draw_points(img, x, y): """ 座標を描画する :param img: 画像 :type img: ndarray :param x: x座標 :type x: int :param y: y座標 :param y: int :return None: """ cv2.putText(img, '({0}, {1})'.format(x, y), (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255)) def draw_all_points(self, img, points, win_name=None): if win_name is None: win_name = self.window_name for i in range(points.shape[0]): # print("({0}, {1})" .format(cc_points[i, 0], cc_points[i, 1])) self.draw_points(img, points[i, 0], points[i, 1]) # print("{},{}".format(calc_cc_points[i, 0], calc_cc_points[i, 1])) img = cv2.circle(img, (points[i, 0], points[i, 1]), 5, (255, 255, 0), -1) cv2.imshow(win_name, img) @staticmethod def calc_points(points, column) -> np.ndarray: """ 分割するときは切り捨てにしている columnが8ならブロックサークルの分割,16なら交点サークルの分割をする.それ以外だとすべて0の座標を返す. :param points: :param column: :return: """ circle_points = np.zeros((column, 2), dtype=int) x_distance = points[1, 0] - points[0, 0] # xの距離 y_distance = points[2, 1] - points[0, 1] # yの距離 # print('前{0}'.format(circle_points)) if column == 8: # ブロックサークルのとき # 交点サークルは,1/3に分割 # x座標 circle_points[0, 0] = circle_points[3, 0] = circle_points[5, 0] = points[0, 0] circle_points[1, 0] = circle_points[6, 0] = x_distance * 1 // 2 + points[0, 0] circle_points[2, 0] = circle_points[4, 0] = circle_points[7, 0] = points[1, 0] # y座標 circle_points[0:4, 1] = points[0, 1] circle_points[3:5, 1] = y_distance * 1 // 2 + points[0, 1] circle_points[5:, 1] = points[3, 1] elif column == 16: # 交点サークルのとき # ブロックサークルは,半分に分割 # x座標 circle_points[0:13:4, 0] = points[0, 0] circle_points[1:14:4, 0] = x_distance * 1 // 3 + points[0, 0] circle_points[2:15:4, 0] = x_distance * 2 // 3 + points[0, 0] circle_points[3:16:4, 0] = points[3, 0] # y座標 circle_points[0:4, 1] = points[0, 1] circle_points[4:8, 1] = y_distance * 1 // 3 + points[0, 1] circle_points[8:12, 1] = y_distance * 2 // 3 + points[0, 1] circle_points[12:, 1] = points[3, 1] else: return circle_points # print("座標計算結果:{}".format(circle_points)) return circle_points @staticmethod def drag_and_drop_square(event, x, y, flags, param) -> None: """ 画像上でドラッグアンドドロップした範囲に対して四角形を描画する :param event: CV_EVENT_* の内の1つ :param x: 画像座標系におけるマウスポインタのX座標 :param y: 画像座標系におけるマウスポインタのY座標 :param flags: CV_EVENT_FLAG_* の論理和 :param param: コールバック関数に渡される,ユーザ定義パラメータ :return: None """ window_name, img, get_circle_point = param h = img.shape[0] # 画像の高さを取る w = img.shape[1] # 画像の幅を取る if event == cv2.EVENT_MOUSEMOVE and get_circle_point.circle_mode <= 1: # マウスが動いたときかつ囲むモードのとき img_copy = np.copy(img) h = img_copy.shape[0] # 画像の高さを取る w = img_copy.shape[1] # 画像の幅を取る # cv2.line(画像, (x1, y1), (x2, y2), (r, g, b)) cv2.line(img_copy, (x, 0), (x, h), (0, 0, 255)) # 縦の線 cv2.line(img_copy, (0, y), (w, y), (0, 0, 255)) # 横の線 if get_circle_point.mode: # ドラッグ・アンド・ドロップで範囲指定モードがTrueなら # cv2.rectangle(画像, 起点の(x, y), 終点の(x, y), 線の色(r, g, b), 線の太さ) cv2.rectangle(img_copy, (get_circle_point.ix, get_circle_point.iy), (x, y), (255, 0, 0), thickness=2) cv2.imshow(window_name, img_copy) if event == cv2.EVENT_LBUTTONDOWN and get_circle_point.circle_mode <= 1: # 左ボタンを押下したとき get_circle_point.ix, get_circle_point.iy = x, y print('起点の座標:({0}, {1})'.format(get_circle_point.ix, get_circle_point.iy)) get_circle_point.mode = True # ドラッグ・アンド・ドロップで範囲指定モードをON if event == cv2.EVENT_LBUTTONUP and get_circle_point.circle_mode <= 1: # マウスの左ボタンを上げたとき cv2.rectangle(img, (get_circle_point.ix, get_circle_point.iy), (x, y), (255, 0, 0), thickness=2) # 四角形を描画 get_circle_point.draw_points(img, get_circle_point.ix, get_circle_point.iy) # 始点の頂点 get_circle_point.draw_points(img, x, get_circle_point.iy) # 始点の横の頂点 get_circle_point.draw_points(img, get_circle_point.ix, y) # 終点の横の頂点 get_circle_point.draw_points(img, x, y) # 終点の頂点 # square_points = np.empty((4, 2)) # 0行目左上.1行目右上.2行目左下.3行目右下 ix, iy = get_circle_point.ix, get_circle_point.iy if w / 2 <= ix and h / 2 >= iy: # 起点の座標が第一象限のとき square_points = np.array([[x, iy], [ix, iy], [x, y], [ix, y]]) elif w / 2 >= ix and h / 2 >= iy: # 起点の座標が第二象限のとき # print('{}'.format('第二象限')) square_points = np.array([[ix, iy], [x, iy], [ix, y], [x, y]]) elif w / 2 >= ix and h / 2 <= iy: # 起点の座標が第三象限のとき square_points = np.array([[ix, y], [x, y], [ix, iy], [x, iy]]) else: # それ以外(起点の座標が第四象限のとき) square_points = np.array([[x, y], [ix, y], [x, iy], [ix, iy]]) if get_circle_point.circle_mode == 0: # 交点サークルを囲むモード get_circle_point.cc_points = get_circle_point.calc_points(points=square_points, column=16) get_circle_point.draw_all_points(img=img, points=get_circle_point.cc_points, win_name=window_name) elif get_circle_point.circle_mode == 1: # ブロックサークルを囲むモード get_circle_point.bc_points = get_circle_point.calc_points(points=square_points, column=8) get_circle_point.draw_all_points(img=img, points=get_circle_point.bc_points, win_name=window_name) # print('代入前:{0}'.format(get_circle_point.named_points)) get_circle_point.add() # print('代入後:{0}'.format(get_circle_point.named_points)) else: print('m9( ´,_ゝ`)プッ') get_circle_point.circle_mode += 1 get_circle_point.mode = False # ドラッグ・アンド・ドロップで範囲指定モードをOFF def run(self): img = './../img/clip_field.png' window_name = 'WindowDAYO' img = cv2.imread(img) get_circle_point = GetCirclePoint() cv2.namedWindow(window_name) cv2.setMouseCallback(window_name, get_circle_point.drag_and_drop_square, [window_name, img, get_circle_point]) cv2.imshow(window_name, img) cv2.moveWindow(window_name, 100, 100) # 左上にウィンドウを出す cv2.waitKey() print(get_circle_point.named_points) # 自分をmain関数だと思っている精神異常者 def main(): img = './../img/clip_field.png' window_name = 'WindowDAYO' img = cv2.imread(img) get_circle_point = GetCirclePoint() cv2.namedWindow(window_name) cv2.setMouseCallback(window_name, get_circle_point.drag_and_drop_square, [window_name, img, get_circle_point]) cv2.imshow(window_name, img) cv2.moveWindow(window_name, 100, 100) # 左上にウィンドウを出す cv2.waitKey() print(get_circle_point.named_points) cv2.destroyAllWindows() if __name__ == '__main__': main()
[ "cv2.cv2.namedWindow", "cv2.cv2.destroyAllWindows", "cv2.cv2.circle", "cv2.cv2.waitKey", "numpy.copy", "cv2.cv2.line", "numpy.empty", "cv2.cv2.rectangle", "cv2.cv2.setMouseCallback", "numpy.zeros", "cv2.cv2.moveWindow", "numpy.array", "cv2.cv2.imread", "cv2.cv2.imshow" ]
[((8904, 8919), 'cv2.cv2.imread', 'cv2.imread', (['img'], {}), '(img)\n', (8914, 8919), True, 'import cv2.cv2 as cv2\n'), ((8964, 8992), 'cv2.cv2.namedWindow', 'cv2.namedWindow', (['window_name'], {}), '(window_name)\n', (8979, 8992), True, 'import cv2.cv2 as cv2\n'), ((8997, 9112), 'cv2.cv2.setMouseCallback', 'cv2.setMouseCallback', (['window_name', 'get_circle_point.drag_and_drop_square', '[window_name, img, get_circle_point]'], {}), '(window_name, get_circle_point.drag_and_drop_square, [\n window_name, img, get_circle_point])\n', (9017, 9112), True, 'import cv2.cv2 as cv2\n'), ((9137, 9165), 'cv2.cv2.imshow', 'cv2.imshow', (['window_name', 'img'], {}), '(window_name, img)\n', (9147, 9165), True, 'import cv2.cv2 as cv2\n'), ((9170, 9207), 'cv2.cv2.moveWindow', 'cv2.moveWindow', (['window_name', '(100)', '(100)'], {}), '(window_name, 100, 100)\n', (9184, 9207), True, 'import cv2.cv2 as cv2\n'), ((9227, 9240), 'cv2.cv2.waitKey', 'cv2.waitKey', ([], {}), '()\n', (9238, 9240), True, 'import cv2.cv2 as cv2\n'), ((9286, 9309), 'cv2.cv2.destroyAllWindows', 'cv2.destroyAllWindows', ([], {}), '()\n', (9307, 9309), True, 'import cv2.cv2 as cv2\n'), ((406, 473), 'numpy.empty', 'np.empty', (['(self.CROSS_CIRCLE_POINTS, self.POINTS_NUMBER)'], {'dtype': 'int'}), '((self.CROSS_CIRCLE_POINTS, self.POINTS_NUMBER), dtype=int)\n', (414, 473), True, 'import numpy as np\n'), ((509, 576), 'numpy.empty', 'np.empty', (['(self.BLOCK_CIRCLE_POINTS, self.POINTS_NUMBER)'], {'dtype': 'int'}), '((self.BLOCK_CIRCLE_POINTS, self.POINTS_NUMBER), dtype=int)\n', (517, 576), True, 'import numpy as np\n'), ((3057, 3089), 'numpy.zeros', 'np.zeros', (['(column, 2)'], {'dtype': 'int'}), '((column, 2), dtype=int)\n', (3065, 3089), True, 'import numpy as np\n'), ((8379, 8394), 'cv2.cv2.imread', 'cv2.imread', (['img'], {}), '(img)\n', (8389, 8394), True, 'import cv2.cv2 as cv2\n'), ((8447, 8475), 'cv2.cv2.namedWindow', 'cv2.namedWindow', (['window_name'], {}), '(window_name)\n', (8462, 8475), True, 'import cv2.cv2 as cv2\n'), ((8484, 8599), 'cv2.cv2.setMouseCallback', 'cv2.setMouseCallback', (['window_name', 'get_circle_point.drag_and_drop_square', '[window_name, img, get_circle_point]'], {}), '(window_name, get_circle_point.drag_and_drop_square, [\n window_name, img, get_circle_point])\n', (8504, 8599), True, 'import cv2.cv2 as cv2\n'), ((8632, 8660), 'cv2.cv2.imshow', 'cv2.imshow', (['window_name', 'img'], {}), '(window_name, img)\n', (8642, 8660), True, 'import cv2.cv2 as cv2\n'), ((8669, 8706), 'cv2.cv2.moveWindow', 'cv2.moveWindow', (['window_name', '(100)', '(100)'], {}), '(window_name, 100, 100)\n', (8683, 8706), True, 'import cv2.cv2 as cv2\n'), ((8730, 8743), 'cv2.cv2.waitKey', 'cv2.waitKey', ([], {}), '()\n', (8741, 8743), True, 'import cv2.cv2 as cv2\n'), ((2679, 2746), 'cv2.cv2.circle', 'cv2.circle', (['img', '(points[i, 0], points[i, 1])', '(5)', '(255, 255, 0)', '(-1)'], {}), '(img, (points[i, 0], points[i, 1]), 5, (255, 255, 0), -1)\n', (2689, 2746), True, 'import cv2.cv2 as cv2\n'), ((2759, 2784), 'cv2.cv2.imshow', 'cv2.imshow', (['win_name', 'img'], {}), '(win_name, img)\n', (2769, 2784), True, 'import cv2.cv2 as cv2\n'), ((5148, 5160), 'numpy.copy', 'np.copy', (['img'], {}), '(img)\n', (5155, 5160), True, 'import numpy as np\n'), ((5322, 5369), 'cv2.cv2.line', 'cv2.line', (['img_copy', '(x, 0)', '(x, h)', '(0, 0, 255)'], {}), '(img_copy, (x, 0), (x, h), (0, 0, 255))\n', (5330, 5369), True, 'import cv2.cv2 as cv2\n'), ((5389, 5436), 'cv2.cv2.line', 'cv2.line', (['img_copy', '(0, y)', '(w, y)', '(0, 0, 255)'], {}), '(img_copy, (0, y), (w, y), (0, 0, 255))\n', (5397, 5436), True, 'import cv2.cv2 as cv2\n'), ((5722, 5755), 'cv2.cv2.imshow', 'cv2.imshow', (['window_name', 'img_copy'], {}), '(window_name, img_copy)\n', (5732, 5755), True, 'import cv2.cv2 as cv2\n'), ((6179, 6279), 'cv2.cv2.rectangle', 'cv2.rectangle', (['img', '(get_circle_point.ix, get_circle_point.iy)', '(x, y)', '(255, 0, 0)'], {'thickness': '(2)'}), '(img, (get_circle_point.ix, get_circle_point.iy), (x, y), (255,\n 0, 0), thickness=2)\n', (6192, 6279), True, 'import cv2.cv2 as cv2\n'), ((5608, 5713), 'cv2.cv2.rectangle', 'cv2.rectangle', (['img_copy', '(get_circle_point.ix, get_circle_point.iy)', '(x, y)', '(255, 0, 0)'], {'thickness': '(2)'}), '(img_copy, (get_circle_point.ix, get_circle_point.iy), (x, y),\n (255, 0, 0), thickness=2)\n', (5621, 5713), True, 'import cv2.cv2 as cv2\n'), ((6835, 6881), 'numpy.array', 'np.array', (['[[x, iy], [ix, iy], [x, y], [ix, y]]'], {}), '([[x, iy], [ix, iy], [x, y], [ix, y]])\n', (6843, 6881), True, 'import numpy as np\n'), ((7022, 7068), 'numpy.array', 'np.array', (['[[ix, iy], [x, iy], [ix, y], [x, y]]'], {}), '([[ix, iy], [x, iy], [ix, y], [x, y]])\n', (7030, 7068), True, 'import numpy as np\n'), ((7164, 7210), 'numpy.array', 'np.array', (['[[ix, y], [x, y], [ix, iy], [x, iy]]'], {}), '([[ix, y], [x, y], [ix, iy], [x, iy]])\n', (7172, 7210), True, 'import numpy as np\n'), ((7284, 7330), 'numpy.array', 'np.array', (['[[x, y], [ix, y], [x, iy], [ix, iy]]'], {}), '([[x, y], [ix, y], [x, iy], [ix, iy]])\n', (7292, 7330), True, 'import numpy as np\n')]
""" Lorenz system """ from scipy.integrate import odeint from scipy.stats import norm class lorenz_system_63: def __init__(self, rho = None, sigma = None, beta = None): self.rho = 28.0 self.sigma = 10.0 self.beta = 8.0 / 3.0 self.N = 3 self.x0 = norm.rvs(size = self.N).reshape((self.N,))*4. def f(self,state, t): x, y, z = state # unpack the state vector return self.sigma * (y - x), x * (self.rho - z) - y, x * y - self.beta * z # derivatives if __name__ == '__main__': from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt ls = lorenz_system() state0 = [1.0, 1.0, 1.0] t = np.arange(0.0, 40.0, 0.01) states = odeint(ls.f, state0, t) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot(states[:,0], states[:,1], states[:,2]) plt.show()
[ "matplotlib.pyplot.show", "scipy.stats.norm.rvs", "scipy.integrate.odeint", "matplotlib.pyplot.figure", "numpy.arange" ]
[((705, 731), 'numpy.arange', 'np.arange', (['(0.0)', '(40.0)', '(0.01)'], {}), '(0.0, 40.0, 0.01)\n', (714, 731), True, 'import numpy as np\n'), ((745, 768), 'scipy.integrate.odeint', 'odeint', (['ls.f', 'state0', 't'], {}), '(ls.f, state0, t)\n', (751, 768), False, 'from scipy.integrate import odeint\n'), ((780, 792), 'matplotlib.pyplot.figure', 'plt.figure', ([], {}), '()\n', (790, 792), True, 'import matplotlib.pyplot as plt\n'), ((882, 892), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (890, 892), True, 'import matplotlib.pyplot as plt\n'), ((293, 314), 'scipy.stats.norm.rvs', 'norm.rvs', ([], {'size': 'self.N'}), '(size=self.N)\n', (301, 314), False, 'from scipy.stats import norm\n')]
from keras.models import Sequential, Model from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.utils import np_utils import numpy as np from collections import Counter import types from PIL import Image def test_image(): size = (256,250) color = (210,150,160) img = Image.new("RGBA",size,color) return np.asarray(img)[:, :, :3] def to_cpp_tensor(readable_tensor, is_bias=False, strip_channels=False): tensor = readable_tensor if isinstance(readable_tensor, list): if is_bias: tensor = readable_tensor[1] else: tensor = readable_tensor[0] if strip_channels and len(tensor.shape) == 4 and tensor.shape[0] == 1: tensor = tensor[0] declaration = 'Numeric tensor' + str(tensor.shape) declaration = declaration.replace('(', '[') declaration = declaration.replace(', ', '][') declaration = declaration.replace(',', '') declaration = declaration.replace(')', ']') tstr = str(repr(tensor)) tstr = tstr.replace(')', '') tstr = tstr.replace('array(', '') tstr = tstr.replace('[', '{') tstr = tstr.replace(']', '}') tstr = tstr.replace(', dtype=float32', '') return '{} =\n {};'.format(declaration, tstr) def list_lambda(func, value): if isinstance(value, list): return [func(x) for x in value] else: return func(value) # Translates from Keras Default # (rows, cols, depth, nr_filters) # to # (nr_filters, depth, rows, cols) def to_readable_weight(tensor): def to_readable_arr(arr): arr = np.swapaxes(arr, 3, 0) arr = np.swapaxes(arr, 2, 1) arr = np.swapaxes(arr, 2, 3) return arr return list_lambda(to_readable_arr, tensor) # Translates from readable # (nr_filters, depth, rows, cols) # to # (rows, cols, depth, nr_filters) def to_keras_weight(tensor): def to_keras_arr(arr): arr = np.swapaxes(arr, 0, 3) arr = np.swapaxes(arr, 1, 2) arr = np.swapaxes(arr, 0, 1) return arr return list_lambda(to_keras_arr, tensor) ########################################### # Translates from readable # (nr_inputs, depth, rows, cols) # to # (nr_inputs, rows, cols, depth) def to_keras_tensor(tensor): def to_keras_arr(arr): arr = np.swapaxes(arr, 1, 2) arr = np.swapaxes(arr, 3, 2) return arr return list_lambda(to_keras_arr, tensor) # Translates from Keras # (nr_inputs, rows, cols, depth) # to # (nr_inputs, depth, rows, cols) def to_readable_tensor(tensor, batch=True): if batch: def to_readable_arr(arr): arr = np.swapaxes(arr, 3, 1) arr = np.swapaxes(arr, 2, 3) return arr else: # (rows, cols, depth) to (depth, rows, cols) def to_readable_arr(arr): arr = np.swapaxes(arr, 2, 0) return arr return list_lambda(to_readable_arr, tensor) readable_input = np.array( [[ [ [ 0., 1., 2., 3., 4. ], [ 5., 6., 7., 8., 9. ], [ 10., 11., 12., 13., 14. ], [ 15., 16., 17., 18., 19. ], [ 20., 21., 22., 23., 24. ] ], [ [ 0., 1., 2., 3., 4. ], [ 5., 6., 7., 8., 9. ], [ 10., 11., 12., 13., 14. ], [ 15., 16., 17., 18., 19. ], [ 20., 21., 22., 23., 24. ] ], [ [ 0., 1., 2., 3., 4. ], [ 5., 6., 7., 8., 9. ], [ 10., 11., 12., 13., 14. ], [ 15., 16., 17., 18., 19. ], [ 20., 21., 22., 23., 24. ] ], ]] ) test_weights = [np.array( [ [ [ [0., 0.], [0., 0.], [0., 0.] ], [ [1., 1.], [1., 1.], [1., 1.] ], [ [2., 2.], [2., 2.], [2., 2.] ], ], [ [ [3., 3.], [3., 3.], [3., 3.] ], [ [4., 4.], [4., 4.], [4., 4.] ], [ [5., 5.], [5., 5.], [5., 5.] ], ], [ [ [6., 6.], [6., 6.], [6., 6.] ], [ [7., 7.], [7., 7.], [7., 7.] ], [ [8., 8.], [8., 8.], [8., 8.] ], ] ] )] readable_test_weights = [np.array( [ [ # Kernel 1 [ [ 0., 1., 2. ], [ 3., 4., 5. ], [ 6., 7., 8. ] ], [ [ 0., 1., 2. ], [ 3., 4., 5. ], [ 6., 7., 8. ] ], [ [ 0., 1., 2. ], [ 3., 4., 5. ], [ 6., 7., 8. ] ], ], [ # Kernel 2 [ [ 0., 1., 2. ], [ 3., 4., 5. ], [ 6., 7., 8. ] ], [ [ 0., 1., 2. ], [ 3., 4., 5. ], [ 6., 7., 8. ] ], [ [ 0., 1., 2. ], [ 3., 4., 5. ], [ 6., 7., 8. ] ], ] ] )] # Channels last is DEFAULT # Input shape: (rows, cols, depth) # # Weights: [(rows, cols, depth, nr_filters)] model = Sequential() # weights = [np.ones((3, 3, 3, 1))] weights = test_weights weights.append(np.array([0, 1])) # weights = to_keras_tensor(readable_test_weights) print(to_cpp_tensor(readable_test_weights)) print(to_cpp_tensor(readable_input)) # print(to_keras_weight(readable_test_weights)) test_layer = Conv2D(2, (3, 3), input_shape=(5, 5, 3), weights=weights, use_bias=True, name='conv') # test_layer.set_weights(test_weights_2) # print(test_layer.get_weights()) # test_layer.set_weights(test_weights) # print(test_layer) model.add(test_layer) model.compile(optimizer='sgd', loss='mean_squared_error') # print(test_layer.get_weights()) print(to_cpp_tensor(test_layer.get_weights(), is_bias=True)) out = model.predict(to_keras_tensor(readable_input)) print(to_readable_tensor(out)) np.set_printoptions(threshold=np.nan) print(to_readable_tensor(test_image(), False)) print(test_image().shape) print(to_cpp_tensor(to_readable_tensor(test_image(), False)))
[ "PIL.Image.new", "numpy.set_printoptions", "numpy.asarray", "numpy.array", "keras.layers.Conv2D", "numpy.swapaxes", "keras.models.Sequential" ]
[((2812, 3309), 'numpy.array', 'np.array', (['[[[[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0, 9.0], [10.0, 11.0, 12.0,\n 13.0, 14.0], [15.0, 16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0, \n 24.0]], [[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0, 9.0], [10.0, \n 11.0, 12.0, 13.0, 14.0], [15.0, 16.0, 17.0, 18.0, 19.0], [20.0, 21.0, \n 22.0, 23.0, 24.0]], [[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0, \n 9.0], [10.0, 11.0, 12.0, 13.0, 14.0], [15.0, 16.0, 17.0, 18.0, 19.0], [\n 20.0, 21.0, 22.0, 23.0, 24.0]]]]'], {}), '([[[[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0, 9.0], [10.0, \n 11.0, 12.0, 13.0, 14.0], [15.0, 16.0, 17.0, 18.0, 19.0], [20.0, 21.0, \n 22.0, 23.0, 24.0]], [[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0, \n 9.0], [10.0, 11.0, 12.0, 13.0, 14.0], [15.0, 16.0, 17.0, 18.0, 19.0], [\n 20.0, 21.0, 22.0, 23.0, 24.0]], [[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, \n 7.0, 8.0, 9.0], [10.0, 11.0, 12.0, 13.0, 14.0], [15.0, 16.0, 17.0, 18.0,\n 19.0], [20.0, 21.0, 22.0, 23.0, 24.0]]]])\n', (2820, 3309), True, 'import numpy as np\n'), ((4596, 4608), 'keras.models.Sequential', 'Sequential', ([], {}), '()\n', (4606, 4608), False, 'from keras.models import Sequential, Model\n'), ((4895, 4984), 'keras.layers.Conv2D', 'Conv2D', (['(2)', '(3, 3)'], {'input_shape': '(5, 5, 3)', 'weights': 'weights', 'use_bias': '(True)', 'name': '"""conv"""'}), "(2, (3, 3), input_shape=(5, 5, 3), weights=weights, use_bias=True,\n name='conv')\n", (4901, 4984), False, 'from keras.layers import Conv2D, MaxPooling2D\n'), ((5381, 5418), 'numpy.set_printoptions', 'np.set_printoptions', ([], {'threshold': 'np.nan'}), '(threshold=np.nan)\n', (5400, 5418), True, 'import numpy as np\n'), ((433, 463), 'PIL.Image.new', 'Image.new', (['"""RGBA"""', 'size', 'color'], {}), "('RGBA', size, color)\n", (442, 463), False, 'from PIL import Image\n'), ((3410, 3788), 'numpy.array', 'np.array', (['[[[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]\n ], [[2.0, 2.0], [2.0, 2.0], [2.0, 2.0]]], [[[3.0, 3.0], [3.0, 3.0], [\n 3.0, 3.0]], [[4.0, 4.0], [4.0, 4.0], [4.0, 4.0]], [[5.0, 5.0], [5.0, \n 5.0], [5.0, 5.0]]], [[[6.0, 6.0], [6.0, 6.0], [6.0, 6.0]], [[7.0, 7.0],\n [7.0, 7.0], [7.0, 7.0]], [[8.0, 8.0], [8.0, 8.0], [8.0, 8.0]]]]'], {}), '([[[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[1.0, 1.0], [1.0, 1.0], [\n 1.0, 1.0]], [[2.0, 2.0], [2.0, 2.0], [2.0, 2.0]]], [[[3.0, 3.0], [3.0, \n 3.0], [3.0, 3.0]], [[4.0, 4.0], [4.0, 4.0], [4.0, 4.0]], [[5.0, 5.0], [\n 5.0, 5.0], [5.0, 5.0]]], [[[6.0, 6.0], [6.0, 6.0], [6.0, 6.0]], [[7.0, \n 7.0], [7.0, 7.0], [7.0, 7.0]], [[8.0, 8.0], [8.0, 8.0], [8.0, 8.0]]]])\n', (3418, 3788), True, 'import numpy as np\n'), ((3873, 4222), 'numpy.array', 'np.array', (['[[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[0.0, 1.0, 2.0], [\n 3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [\n 6.0, 7.0, 8.0]]], [[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[0.0, 1.0, 2.0],\n [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]]'], {}), '([[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[0.0, 1.0,\n 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[0.0, 1.0, 2.0], [3.0, 4.0, \n 5.0], [6.0, 7.0, 8.0]]], [[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0,\n 8.0]], [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[0.0, 1.0,\n 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]])\n', (3881, 4222), True, 'import numpy as np\n'), ((4683, 4699), 'numpy.array', 'np.array', (['[0, 1]'], {}), '([0, 1])\n', (4691, 4699), True, 'import numpy as np\n'), ((470, 485), 'numpy.asarray', 'np.asarray', (['img'], {}), '(img)\n', (480, 485), True, 'import numpy as np\n'), ((1597, 1619), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(3)', '(0)'], {}), '(arr, 3, 0)\n', (1608, 1619), True, 'import numpy as np\n'), ((1628, 1650), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(2)', '(1)'], {}), '(arr, 2, 1)\n', (1639, 1650), True, 'import numpy as np\n'), ((1659, 1681), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(2)', '(3)'], {}), '(arr, 2, 3)\n', (1670, 1681), True, 'import numpy as np\n'), ((1904, 1926), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(0)', '(3)'], {}), '(arr, 0, 3)\n', (1915, 1926), True, 'import numpy as np\n'), ((1935, 1957), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(1)', '(2)'], {}), '(arr, 1, 2)\n', (1946, 1957), True, 'import numpy as np\n'), ((1966, 1988), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(0)', '(1)'], {}), '(arr, 0, 1)\n', (1977, 1988), True, 'import numpy as np\n'), ((2253, 2275), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(1)', '(2)'], {}), '(arr, 1, 2)\n', (2264, 2275), True, 'import numpy as np\n'), ((2284, 2306), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(3)', '(2)'], {}), '(arr, 3, 2)\n', (2295, 2306), True, 'import numpy as np\n'), ((2552, 2574), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(3)', '(1)'], {}), '(arr, 3, 1)\n', (2563, 2574), True, 'import numpy as np\n'), ((2584, 2606), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(2)', '(3)'], {}), '(arr, 2, 3)\n', (2595, 2606), True, 'import numpy as np\n'), ((2712, 2734), 'numpy.swapaxes', 'np.swapaxes', (['arr', '(2)', '(0)'], {}), '(arr, 2, 0)\n', (2723, 2734), True, 'import numpy as np\n')]
# Fix paths for imports to work in unit tests ---------------- if __name__ == "__main__": from _fix_paths import fix_paths fix_paths() # ------------------------------------------------------------ # Load libraries --------------------------------------------- import numpy as np from ssa_sim_v2.policies.policy import Policy from ssa_sim_v2.simulator.action import Action from ssa_sim_v2.tools import dhl from ssa_sim_v2.simulator.action import Action, ActionSet from ssa_sim_v2.simulator.attribute import AttrSet from ssa_sim_v2.simulator.state import StateSet # ------------------------------------------------------------ class PolicyThompsonSamplingSI(Policy): """ State-independent (SI) Thompson sampling policy. :ivar list state_set: A list of states. :ivar list action_set: A list of actions. :ivar object rng: Random number generator. :ivar int seed: Seed for the random number generator. :ivar Policy.UDP udp: User-defined params. :ivar Policy.HTP htp: Hard-tunable params. :ivar Policy.STP stp: Soft-tunable params. :ivar Policy.IP ip: Inner params. """ class UDP(Policy.UDP): """ A class for storing user-defined params -- hard-coded overwrite on all other parameters. :ivar float min_bid: Minimal allowed bid. :ivar float max_bid: Maximal allowed bid. """ def __init__(self): Policy.UDP.__init__(self) # Min and max bid self.min_bid = None self.max_bid = None class STP(Policy.STP): """ A class for storing soft-tunable params -- tuned externally for a specific bidding entity (possibly based on a larger dataset than inner parameters). :ivar float mu_init: Initial belief for reward value. :ivar float sigma_init: Initial uncertainty for the belief. :ivar float sigma_measure: Measurement uncertainty. """ def __init__(self): Policy.STP.__init__(self) self.mu_init = 0.0 self.sigma_init = 1000.0 self.sigma_measure = 1.0 class IP(Policy.IP): """ A class for storing inner params -- all parameters trained within the policy (based on data for a given bidding entity). :ivar np.ndarray mu: Array of beliefs for the reward for every action. :ivar np.ndarray sigma: Array of uncertainties for the reward for every action. """ def __init__(self): Policy.IP.__init__(self) self.mu = None self.sigma = None def __init__(self, state_set, action_set, attr_set, seed=12345, save_history=False): """ :param StateSet state_set: State set. :param ActionSet action_set: Action set. :param AttrSet attr_set: Attribute set. :param int seed: Seed for the random number generator. :param bool save_history: Indicates if policy history should be saved in the history attribute. """ Policy.__init__(self, state_set, action_set, attr_set, seed, save_history) self.udp = self.UDP() self.htp = self.HTP() self.stp = self.STP() self.ip = self.IP() def initialize(self, params): """ Initializes the policy using a policy initializer (dependency injection pattern). The policy initializer may be used to test many policy parameters settings -- it is enough that the simulator provides appropriate policy initializers which set the params. :param PolicyInitializer policy_initializer: Policy initializer. """ Policy.initialize(self, params) # Apply bounds if defined if self.udp.min_bid is not None: action_set_temp = [] for action in self.action_set: if action.bid >= self.udp.min_bid: action_set_temp.append(action) self.action_set = action_set_temp if self.udp.max_bid is not None: action_set_temp = [] for action in self.action_set: if action.bid <= self.udp.max_bid: action_set_temp.append(action) self.action_set = action_set_temp # handy functions to discretize bids self.bid_amount_to_index = lambda x: round(x) self.index_to_bid_amount = lambda x: float(x) # Initialize beliefs self.ip.mu = np.array([self.stp.mu_init]*self.bid_amount_to_index(self.action_set.max_bid)) self.ip.sigma = np.array([self.stp.sigma_init]*self.bid_amount_to_index(self.action_set.max_bid)) def learn(self, state, data): """ A method that allows the policy to learn based on observations provided by the simulator. :param StateSet.State state: The state in the previous turn. :param Dict data: Dictionary with the following fields: * action -- Your original action used in the previous turn. * effective_action -- Actual action used by the simulator. The original action may need to be adjusted (base bid or modifiers clipped to bounds) to be valid. * reward -- Overall reward obtained in the previous turn. * info -- A dictionary with overall data for the policy: * auctions -- number of auctions, * clicks -- number of clicks, * conversions -- number of conversions, * click_probability -- click probability (clicks / auctions), * cvr -- conversion rate (conversions / clicks), * rpc -- revenue per click (revenue / clicks), * cpc -- cost per click (cost / clicks), * rpv -- revenue per conversion (revenue / conversions), * revenue -- revenue from all conversions, * cost -- cost for all clicks, * profit -- revenue - cost. * attr_info: A dict with data per segment, e.g. { "gender": {"M": info_for_gender_M, "F": info_for_gender_F, ...}, "age": {"18-24": info_for_age_18-24, "25-34": info_for_age_25-34, ...}, ... }, where info_for... has the same form as info but contains data only for a given segment. """ Policy.learn(self, state, data) if data["action"] is None: return #idx = self.action_set.index(data["effective_action"]) idx = round(data['effective_action'].bid) obs_mu = self._get_observation(data) self.ip.mu[idx] = ((1.0 / self.ip.sigma[idx] ** 2 * self.ip.mu[idx] + 1.0 / self.stp.sigma_measure ** 2 * obs_mu) / (1.0 / self.ip.sigma[idx] ** 2 + 1.0 / self.stp.sigma_measure ** 2)) self.ip.sigma[idx] = (self.ip.sigma[idx] * self.stp.sigma_measure) \ / np.sqrt(self.ip.sigma[idx] ** 2 + self.stp.sigma_measure ** 2) def act(self, state, data=None): """ Returns an action given state. :param State state: The current state. :param Union[pd.DataFrame, dict] data: Input data. :return: An action chosen by the policy. :rtype: Action """ Policy.act(self, state, data) try: randomized_mu = np.array([self.rng.normal(self.ip.mu[idx], self.ip.sigma[idx]) for idx in range(round(self.action_set.max_bid))]) except: print ('error !!!!!') print (self.action_set.max_bid) print (len(self.ip.mu)) print (len(self.ip.sigma)) print ('length of mu = {}, length of sigma = {}').format(len(self.ip.mu), len(self.ip.sigma)) action_index = dhl.randargmax(randomized_mu, rng=self.rng) base_bid_amount = self.index_to_bid_amount(action_index) action_inc = Action(base_bid_amount) # note: underspecified action (modifiers not defined at all) # action_inc = Action(base_bid_amount, {'gender': {'M': 1.1, 'F': 1.2}}) # underspecified modifiers action = self.action_set.validify_action(action_inc) # this function fills in unspecified modifiers self.history.update({"bid": action.bid}) # TODO: we should keep not just the base bid, but the entire bid for all attrs return action def _get_observation(self, data): return data["info"]["profit"] / data["info"]["auctions"] \ if data["info"]["auctions"] != 0 else 0.0 class PolicyThompsonSamplingPPASI(PolicyThompsonSamplingSI): """ State-independent (SI) Thompson sampling policy optimizing the profit per auction (PPA). """ def _get_observation(self, data): return data["info"]["profit"] / data["info"]["auctions"] \ if data["info"]["auctions"] != 0 else 0.0 class PolicyThompsonSamplingPSI(PolicyThompsonSamplingSI): """ State-independent (SI) Thompson sampling policy optimizing the total profit (P). """ def _get_observation(self, data): return data["info"]["profit"] def test_01_setup(): """ sample init init attr, state, action space :return: """ names = ['gender', 'age'] vals = {'gender': ['M', 'F', 'U'], 'age': ['0-19', '20-29', '30-39', '40-49', '50-59', '60-69', '70-*']} attr_set = AttrSet(names, vals) state_set = StateSet(['date', 'how'], ['discrete', 'discrete'], [['2018-01-01', '2018-01-02'], list(range(168))]) act_set = ActionSet(attr_set, max_bid=9.99, min_bid=0.01, max_mod=9.0, min_mod=0.1) return attr_set, state_set, act_set def test_one_policy_run(): # init attr, state, action space attr_set, state_set, act_set = test_01_setup() # get first state s = state_set.make_state({'date': '2018-01-01', 'how': 12}) # initialize policy pol = PolicyThompsonSamplingSI(state_set, act_set, attr_set, seed=9292) pol.initialize({"stp": {"cvr_default": 0.02, "rpv_default": 300.0}}) a = pol.act(s) print(a) if __name__ == "__main__": test_01_setup() test_one_policy_run()
[ "ssa_sim_v2.policies.policy.Policy.STP.__init__", "_fix_paths.fix_paths", "ssa_sim_v2.policies.policy.Policy.learn", "ssa_sim_v2.policies.policy.Policy.__init__", "ssa_sim_v2.simulator.attribute.AttrSet", "ssa_sim_v2.simulator.action.ActionSet", "ssa_sim_v2.policies.policy.Policy.UDP.__init__", "ssa_sim_v2.policies.policy.Policy.IP.__init__", "ssa_sim_v2.tools.dhl.randargmax", "ssa_sim_v2.policies.policy.Policy.initialize", "ssa_sim_v2.simulator.action.Action", "ssa_sim_v2.policies.policy.Policy.act", "numpy.sqrt" ]
[((137, 148), '_fix_paths.fix_paths', 'fix_paths', ([], {}), '()\n', (146, 148), False, 'from _fix_paths import fix_paths\n'), ((9549, 9569), 'ssa_sim_v2.simulator.attribute.AttrSet', 'AttrSet', (['names', 'vals'], {}), '(names, vals)\n', (9556, 9569), False, 'from ssa_sim_v2.simulator.attribute import AttrSet\n'), ((9729, 9802), 'ssa_sim_v2.simulator.action.ActionSet', 'ActionSet', (['attr_set'], {'max_bid': '(9.99)', 'min_bid': '(0.01)', 'max_mod': '(9.0)', 'min_mod': '(0.1)'}), '(attr_set, max_bid=9.99, min_bid=0.01, max_mod=9.0, min_mod=0.1)\n', (9738, 9802), False, 'from ssa_sim_v2.simulator.action import Action, ActionSet\n'), ((3077, 3151), 'ssa_sim_v2.policies.policy.Policy.__init__', 'Policy.__init__', (['self', 'state_set', 'action_set', 'attr_set', 'seed', 'save_history'], {}), '(self, state_set, action_set, attr_set, seed, save_history)\n', (3092, 3151), False, 'from ssa_sim_v2.policies.policy import Policy\n'), ((3699, 3730), 'ssa_sim_v2.policies.policy.Policy.initialize', 'Policy.initialize', (['self', 'params'], {}), '(self, params)\n', (3716, 3730), False, 'from ssa_sim_v2.policies.policy import Policy\n'), ((6487, 6518), 'ssa_sim_v2.policies.policy.Policy.learn', 'Policy.learn', (['self', 'state', 'data'], {}), '(self, state, data)\n', (6499, 6518), False, 'from ssa_sim_v2.policies.policy import Policy\n'), ((7426, 7455), 'ssa_sim_v2.policies.policy.Policy.act', 'Policy.act', (['self', 'state', 'data'], {}), '(self, state, data)\n', (7436, 7455), False, 'from ssa_sim_v2.policies.policy import Policy\n'), ((7944, 7987), 'ssa_sim_v2.tools.dhl.randargmax', 'dhl.randargmax', (['randomized_mu'], {'rng': 'self.rng'}), '(randomized_mu, rng=self.rng)\n', (7958, 7987), False, 'from ssa_sim_v2.tools import dhl\n'), ((8074, 8097), 'ssa_sim_v2.simulator.action.Action', 'Action', (['base_bid_amount'], {}), '(base_bid_amount)\n', (8080, 8097), False, 'from ssa_sim_v2.simulator.action import Action, ActionSet\n'), ((1430, 1455), 'ssa_sim_v2.policies.policy.Policy.UDP.__init__', 'Policy.UDP.__init__', (['self'], {}), '(self)\n', (1449, 1455), False, 'from ssa_sim_v2.policies.policy import Policy\n'), ((2009, 2034), 'ssa_sim_v2.policies.policy.Policy.STP.__init__', 'Policy.STP.__init__', (['self'], {}), '(self)\n', (2028, 2034), False, 'from ssa_sim_v2.policies.policy import Policy\n'), ((2549, 2573), 'ssa_sim_v2.policies.policy.Policy.IP.__init__', 'Policy.IP.__init__', (['self'], {}), '(self)\n', (2567, 2573), False, 'from ssa_sim_v2.policies.policy import Policy\n'), ((7074, 7136), 'numpy.sqrt', 'np.sqrt', (['(self.ip.sigma[idx] ** 2 + self.stp.sigma_measure ** 2)'], {}), '(self.ip.sigma[idx] ** 2 + self.stp.sigma_measure ** 2)\n', (7081, 7136), True, 'import numpy as np\n')]
#!/usr/bin/env python3 """A python script to perform watermark embedding/detection in the wavelet domain.""" # Copyright (C) 2020 by <NAME> # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import numpy as np from scipy.io import wavfile from scipy.signal import windows import pywt HOST_SIGNAL_FILE = "bass_half.wav" # 透かし埋め込み先のファイル WATERMARK_SIGNAL_FILE = "wmed_signal.wav" # 透かしを埋め込んだファイル PSEUDO_RAND_FILE = 'pseudo_rand.dat' # 疑似乱数列のファイル WATERMARK_ORIGINAL_FILE = 'watermark_ori.dat' # オリジナルの透かし信号 REP_CODE = True # 繰り返し埋め込みを使う FRAME_LENGTH = 2048 # フレーム長 CONTROL_STRENGTH = 1000 # 埋め込み強度 OVERLAP = 0.5 # フレーム分析のオーバーラップ率 (固定) NUM_REPS = 3 # 埋め込みの繰り返し数 WAVELET_BASIS = 'db4' WAVELET_LEVEL = 3 WAVELET_MODE = 'symmetric' THRESHOLD = 0.0 def fix(xs): """ A emuration of MATLAB 'fix' function. borrowed from https://ideone.com/YjJwOh """ # res = [np.floor(e) if e >= 0 else np.ceil(e) for e in xs] if xs >= 0: res = np.floor(xs) else: res = np.ceil(xs) return res def embed(): """ perform embedding. """ # ホスト信号 sr, host_signal = wavfile.read(HOST_SIGNAL_FILE) signal_len = len(host_signal) # フレームの移動量 frame_shift = int(FRAME_LENGTH * (1 - OVERLAP)) # 隣接フレームとのオーバーラップ長 overlap_length = int(FRAME_LENGTH * OVERLAP) # 埋め込みの総ビット数 embed_nbit = fix((signal_len - overlap_length) / frame_shift) if REP_CODE: # 実効的な埋め込み可能ビット数 effective_nbit = np.floor(embed_nbit / NUM_REPS) embed_nbit = effective_nbit * NUM_REPS else: effective_nbit = embed_nbit # 整数化 frame_shift = int(frame_shift) effective_nbit = int(effective_nbit) embed_nbit = int(embed_nbit) # オリジナルの透かし信号を作成(0と1のビット列) wmark_original = np.random.randint(2, size=int(effective_nbit)) # オリジナルの透かし信号を保存 with open(WATERMARK_ORIGINAL_FILE, 'w') as f: for d in wmark_original: f.write("%d\n" % d) # 透かし信号を拡張する if REP_CODE: wmark_extended = np.repeat(wmark_original, NUM_REPS) else: wmark_extended = wmark_original # 透かしの埋め込み強度 alpha = CONTROL_STRENGTH # 透かしが埋め込まれた信号(watermarked signal)を生成 in wavelet domain pointer = 0 count = 0 wmed_signal = np.zeros((frame_shift * embed_nbit)) # watermarked signal prev = np.zeros((FRAME_LENGTH)) for i in range(embed_nbit): frame = host_signal[pointer: pointer + FRAME_LENGTH] # Wavelet係数を計算 coeffs = pywt.wavedec(data=frame, wavelet=WAVELET_BASIS, level=WAVELET_LEVEL, mode=WAVELET_MODE) # 透かしの埋め込み強度を平均と同じオーダーに設定する(adaptive) # coef_size = int(np.log10(np.abs(np.mean(coeffs[0])))) + 1 # alpha = 10 ** coef_size # 透かしの埋め込み if wmark_extended[count] == 1: coeffs[0] = coeffs[0] - np.mean(coeffs[0]) + alpha else: coeffs[0] = coeffs[0] - np.mean(coeffs[0]) - alpha # 再構成 wmarked_frame = pywt.waverec(coeffs=coeffs, wavelet=WAVELET_BASIS, mode=WAVELET_MODE) # 窓をかける (Hann window) wmarked_frame = wmarked_frame * windows.hann(FRAME_LENGTH) wmed_signal[frame_shift * i: frame_shift * (i+1)] = \ np.concatenate((prev[frame_shift:FRAME_LENGTH] + wmarked_frame[0:overlap_length], wmarked_frame[overlap_length:frame_shift])) prev = wmarked_frame count = count + 1 pointer = pointer + frame_shift # ホスト信号の残りと結合 wmed_signal = np.concatenate( (wmed_signal, host_signal[len(wmed_signal): signal_len])) # 透かしが埋め込まれた信号をwavとして保存 wmed_signal = wmed_signal.astype(np.int16) # convert float into integer wavfile.write(WATERMARK_SIGNAL_FILE, sr, wmed_signal) def detect(): """ perform detecton. """ sr, host_signal = wavfile.read(HOST_SIGNAL_FILE) # 埋め込み済みの音声ファイルを開く _, eval_signal = wavfile.read(WATERMARK_SIGNAL_FILE) signal_len = len(eval_signal) # オリジナルの透かし信号をロード with open(WATERMARK_ORIGINAL_FILE, 'r') as f: wmark_original = f.readlines() wmark_original = np.array([float(w.rstrip()) for w in wmark_original]) # フレームの移動量 frame_shift = int(FRAME_LENGTH * (1 - OVERLAP)) # 埋め込みビット数 embed_nbit = fix((signal_len - FRAME_LENGTH * OVERLAP) / frame_shift) if REP_CODE: # 実質的な埋め込み可能ビット数 effective_nbit = np.floor(embed_nbit / NUM_REPS) embed_nbit = effective_nbit * NUM_REPS else: effective_nbit = embed_nbit frame_shift = int(frame_shift) effective_nbit = int(effective_nbit) embed_nbit = int(embed_nbit) # オリジナルの透かし信号をロード with open(WATERMARK_ORIGINAL_FILE, 'r') as f: wmark_original = f.readlines() wmark_original = np.array([int(w.rstrip()) for w in wmark_original]) # 透かし情報の検出 pointer = 0 detected_bit = np.zeros(embed_nbit) for i in range(embed_nbit): wmarked_frame = eval_signal[pointer: pointer + FRAME_LENGTH] # wavelet decomposition wmarked_coeffs = pywt.wavedec( data=wmarked_frame, wavelet=WAVELET_BASIS, level=WAVELET_LEVEL, mode=WAVELET_MODE) thres = np.sum(wmarked_coeffs[0]) if thres >= THRESHOLD: detected_bit[i] = 1 else: detected_bit[i] = 0 pointer = pointer + frame_shift if REP_CODE: count = 0 wmark_recovered = np.zeros(effective_nbit) for i in range(effective_nbit): # ビットを集計(平均値) ave = np.sum(detected_bit[count:count + NUM_REPS]) / NUM_REPS if ave >= 0.5: # 過半数 wmark_recovered[i] = 1 else: wmark_recovered[i] = 0 count = count + NUM_REPS else: wmark_recovered = detected_bit # ビット誤り率を表示 denom = np.int(np.sum(np.abs(wmark_recovered - wmark_original))) BER = np.sum(np.abs(wmark_recovered - wmark_original)) / \ effective_nbit * 100 print(f'BER = {BER} % ({denom} / {effective_nbit})') # SNRを表示 SNR = 10 * np.log10( np.sum(np.square(host_signal.astype(np.float32))) / np.sum(np.square(host_signal.astype(np.float32) - eval_signal.astype(np.float32)))) print(f'SNR = {SNR:.2f} dB') # bpsを表示 print('BPS = {:.2f} bps'.format(embed_nbit / (len(host_signal) / sr))) def main(): """Main routine. """ embed() detect() if __name__ in '__main__': main()
[ "numpy.sum", "numpy.ceil", "numpy.abs", "pywt.wavedec", "numpy.floor", "numpy.zeros", "scipy.io.wavfile.write", "scipy.io.wavfile.read", "pywt.waverec", "numpy.mean", "scipy.signal.windows.hann", "numpy.concatenate", "numpy.repeat" ]
[((1801, 1831), 'scipy.io.wavfile.read', 'wavfile.read', (['HOST_SIGNAL_FILE'], {}), '(HOST_SIGNAL_FILE)\n', (1813, 1831), False, 'from scipy.io import wavfile\n'), ((2944, 2978), 'numpy.zeros', 'np.zeros', (['(frame_shift * embed_nbit)'], {}), '(frame_shift * embed_nbit)\n', (2952, 2978), True, 'import numpy as np\n'), ((3014, 3036), 'numpy.zeros', 'np.zeros', (['FRAME_LENGTH'], {}), '(FRAME_LENGTH)\n', (3022, 3036), True, 'import numpy as np\n'), ((4465, 4518), 'scipy.io.wavfile.write', 'wavfile.write', (['WATERMARK_SIGNAL_FILE', 'sr', 'wmed_signal'], {}), '(WATERMARK_SIGNAL_FILE, sr, wmed_signal)\n', (4478, 4518), False, 'from scipy.io import wavfile\n'), ((4596, 4626), 'scipy.io.wavfile.read', 'wavfile.read', (['HOST_SIGNAL_FILE'], {}), '(HOST_SIGNAL_FILE)\n', (4608, 4626), False, 'from scipy.io import wavfile\n'), ((4672, 4707), 'scipy.io.wavfile.read', 'wavfile.read', (['WATERMARK_SIGNAL_FILE'], {}), '(WATERMARK_SIGNAL_FILE)\n', (4684, 4707), False, 'from scipy.io import wavfile\n'), ((5627, 5647), 'numpy.zeros', 'np.zeros', (['embed_nbit'], {}), '(embed_nbit)\n', (5635, 5647), True, 'import numpy as np\n'), ((1648, 1660), 'numpy.floor', 'np.floor', (['xs'], {}), '(xs)\n', (1656, 1660), True, 'import numpy as np\n'), ((1685, 1696), 'numpy.ceil', 'np.ceil', (['xs'], {}), '(xs)\n', (1692, 1696), True, 'import numpy as np\n'), ((2159, 2190), 'numpy.floor', 'np.floor', (['(embed_nbit / NUM_REPS)'], {}), '(embed_nbit / NUM_REPS)\n', (2167, 2190), True, 'import numpy as np\n'), ((2702, 2737), 'numpy.repeat', 'np.repeat', (['wmark_original', 'NUM_REPS'], {}), '(wmark_original, NUM_REPS)\n', (2711, 2737), True, 'import numpy as np\n'), ((3173, 3265), 'pywt.wavedec', 'pywt.wavedec', ([], {'data': 'frame', 'wavelet': 'WAVELET_BASIS', 'level': 'WAVELET_LEVEL', 'mode': 'WAVELET_MODE'}), '(data=frame, wavelet=WAVELET_BASIS, level=WAVELET_LEVEL, mode=\n WAVELET_MODE)\n', (3185, 3265), False, 'import pywt\n'), ((3678, 3747), 'pywt.waverec', 'pywt.waverec', ([], {'coeffs': 'coeffs', 'wavelet': 'WAVELET_BASIS', 'mode': 'WAVELET_MODE'}), '(coeffs=coeffs, wavelet=WAVELET_BASIS, mode=WAVELET_MODE)\n', (3690, 3747), False, 'import pywt\n'), ((3958, 4088), 'numpy.concatenate', 'np.concatenate', (['(prev[frame_shift:FRAME_LENGTH] + wmarked_frame[0:overlap_length],\n wmarked_frame[overlap_length:frame_shift])'], {}), '((prev[frame_shift:FRAME_LENGTH] + wmarked_frame[0:\n overlap_length], wmarked_frame[overlap_length:frame_shift]))\n', (3972, 4088), True, 'import numpy as np\n'), ((5155, 5186), 'numpy.floor', 'np.floor', (['(embed_nbit / NUM_REPS)'], {}), '(embed_nbit / NUM_REPS)\n', (5163, 5186), True, 'import numpy as np\n'), ((5807, 5906), 'pywt.wavedec', 'pywt.wavedec', ([], {'data': 'wmarked_frame', 'wavelet': 'WAVELET_BASIS', 'level': 'WAVELET_LEVEL', 'mode': 'WAVELET_MODE'}), '(data=wmarked_frame, wavelet=WAVELET_BASIS, level=WAVELET_LEVEL,\n mode=WAVELET_MODE)\n', (5819, 5906), False, 'import pywt\n'), ((5945, 5970), 'numpy.sum', 'np.sum', (['wmarked_coeffs[0]'], {}), '(wmarked_coeffs[0])\n', (5951, 5970), True, 'import numpy as np\n'), ((6184, 6208), 'numpy.zeros', 'np.zeros', (['effective_nbit'], {}), '(effective_nbit)\n', (6192, 6208), True, 'import numpy as np\n'), ((3856, 3882), 'scipy.signal.windows.hann', 'windows.hann', (['FRAME_LENGTH'], {}), '(FRAME_LENGTH)\n', (3868, 3882), False, 'from scipy.signal import windows\n'), ((6616, 6656), 'numpy.abs', 'np.abs', (['(wmark_recovered - wmark_original)'], {}), '(wmark_recovered - wmark_original)\n', (6622, 6656), True, 'import numpy as np\n'), ((6295, 6339), 'numpy.sum', 'np.sum', (['detected_bit[count:count + NUM_REPS]'], {}), '(detected_bit[count:count + NUM_REPS])\n', (6301, 6339), True, 'import numpy as np\n'), ((6676, 6716), 'numpy.abs', 'np.abs', (['(wmark_recovered - wmark_original)'], {}), '(wmark_recovered - wmark_original)\n', (6682, 6716), True, 'import numpy as np\n'), ((3535, 3553), 'numpy.mean', 'np.mean', (['coeffs[0]'], {}), '(coeffs[0])\n', (3542, 3553), True, 'import numpy as np\n'), ((3612, 3630), 'numpy.mean', 'np.mean', (['coeffs[0]'], {}), '(coeffs[0])\n', (3619, 3630), True, 'import numpy as np\n')]
import numpy as np import torch from torchvision import models import torch.nn as nn from nn_ood.data.cifar10 import Cifar10Data from nn_ood.posteriors import LocalEnsemble, SCOD, Ensemble, Naive, KFAC, Mahalanobis from nn_ood.distributions import CategoricalLogit import matplotlib.pyplot as plt import matplotlib.animation as animation from densenet import densenet121 # WHERE TO SAVE THE MODEL FILENAME = "model" ## HYPERPARAMS N_MODELS = 1 LEARNING_RATE = 0.001 SGD_MOMENTUM = 0.9 LR_DROP_FACTOR = 0.5 EPOCHS_PER_DROP = 2 BATCH_SIZE = 5 N_EPOCHS = 0 ## SET UP DATASETS dataset_class = Cifar10Data test_dataset_args = ['val', 'ood', 'svhn', 'tIN', 'lsun'] ## DEFINE VISUALIZATION FUNCTIONS def plt_image(ax, inp): inp = inp.numpy().transpose((1, 2, 0)) mean = np.array([0.4914, 0.4822, 0.4465]) std = np.array([0.2471, 0.2435, 0.2616]) inp = std * inp + mean inp = np.clip(inp, 0, 1) return ax.imshow(inp) def viz_dataset_sample(ax, dataset, idx=0, model=None, unc_model=None): input, target = dataset[idx] plt_image(ax, input) xlabel = 'Target: %d' % target if unc_model is not None: input = input.to(device) pred, unc = unc_model(input.unsqueeze(0)) pred = pred[0].detach().cpu().numpy() unc = unc.item() xlabel += '\nPred: %d\nUnc: %0.3f' % (np.argmax(pred), unc) elif model is not None: input = input.to(device) pred = model(input.unsqueeze(0))[0].detach().cpu().numpy() xlabel += '\nPred: %d' % np.argmax(pred) ax.set_xlabel(xlabel) def viz_datasets(idx=0, unc_model=None, model=None): num_plots = len(test_dataset_args) fig, axes = plt.subplots(1,num_plots, figsize=[5*num_plots, 5], dpi=100) for i, split in enumerate( test_dataset_args ): dataset = dataset_class(split) viz_dataset_sample(axes[i], dataset, idx=idx, unc_model=unc_model, model=model) ## USE CUDA IF POSSIBLE device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") classesToKeep = [0,1,2,3,4] class classSelector(nn.Module): def __init__(self, idx_list): super().__init__() self.idx_list = idx_list def forward(self,x): result = x[..., self.idx_list] return result ## MODEL SET UP og_req_grads = [] def make_model(): global og_req_grads base = densenet121(pretrained=True) selector = classSelector(classesToKeep) model = nn.Sequential(base, selector) og_req_grads = [p.requires_grad for p in model.parameters()] return model def freeze_model(model, freeze_frac=None): # freeze up the the last n_params = len(list(model.parameters())) if freeze_frac is None: freeze_frac = 1. print(freeze_frac) for i, p in enumerate(model.parameters()): if i < freeze_frac*n_params: p.requires_grad = False # make last layer trainable for i,m in enumerate(model.children()): if i > 0: break for p in m.classifier.parameters(): p.requires_grad = True def unfreeze_model(model): global og_req_grads # unfreeze everything for p,v in zip( model.parameters(), og_req_grads): p.requires_grad = v def disable_batchnorm(m): classname = m.__class__.__name__ if classname.find('BatchNorm') != -1: for n,p in m.named_parameters(): p.requires_grad_(False) def get_features_and_classifier(model): """works for Densenet with class selector only""" features = nn.Sequential( model[0].features, nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten() ) classifier = nn.Sequential( model[0].classifier, model[1] ) return features, classifier ## OPTIMIZATION dist_fam = CategoricalLogit().to(device) #criterion = nn.MSELoss() opt_class = torch.optim.SGD opt_kwargs = { 'lr': LEARNING_RATE, 'momentum': SGD_MOMENTUM } sched_class = torch.optim.lr_scheduler.StepLR sched_kwargs = { 'step_size': EPOCHS_PER_DROP, 'gamma': LR_DROP_FACTOR } # recipe for preparing uncertainty models prep_unc_models = { # 'local_ensemble': { # 'class': LocalEnsemble, # 'kwargs': { # 'batch_size': 32, # 'max_samples': 4, # 'num_eigs': 20, # 'device': 'gpu' # } # }, 'scod_SRFT_s184_n30_freeze_0.85': { 'class': SCOD, 'kwargs': { 'num_samples': 184, 'num_eigs': 30, 'device':'gpu', 'sketch_type': 'srft' }, 'freeze': 0.85 }, # 'scod_SRFT_s76_n12': { # 'class': SCOD, # 'kwargs': { # 'num_samples': 76, # 'num_eigs': 12, # 'device':'gpu', # 'sketch_type': 'srft' # }, # }, # 'scod_SRFT_s76_n12_freeze_0.5': { # 'class': SCOD, # 'kwargs': { # 'num_samples': 76, # 'num_eigs': 12, # 'device':'gpu', # 'sketch_type': 'srft' # }, # 'freeze': 0.5, # }, # 'scod_SRFT_s76_n12_freeze_0.25': { # 'class': SCOD, # 'kwargs': { # 'num_samples': 76, # 'num_eigs': 12, # 'device':'gpu', # 'sketch_type': 'srft' # }, # 'freeze': 0.25, # }, # 'scod_SRFT_s76_n12_freeze_0.75': { # 'class': SCOD, # 'kwargs': { # 'num_samples': 76, # 'num_eigs': 12, # 'device':'gpu', # 'sketch_type': 'srft' # }, # 'freeze': 0.75, # }, # 'scod_SRFT_s76_n12_freeze_0.85': { # 'class': SCOD, # 'kwargs': { # 'num_samples': 76, # 'num_eigs': 12, # 'device':'gpu', # 'sketch_type': 'srft' # }, # 'freeze': 0.85, # }, # 'scod_SRFT_s76_n12_freeze_1.0': { # 'class': SCOD, # 'kwargs': { # 'num_samples': 76, # 'num_eigs': 12, # 'device':'gpu', # 'sketch_type': 'srft' # }, # 'freeze': 1.0, # }, 'kfac': { 'class': KFAC, 'kwargs': { 'batch_size': 16, 'device':'gpu', 'input_shape': [3,224,224] }, }, # 'maha': { # 'class': Mahalanobis, # 'kwargs': { # 'device': 'gpu', # "features_and_classifier": get_features_and_classifier, # 'num_classes': 5, # } # } } # recipe for testing uncertainty models test_unc_models = { # 'local_ensemble': { # 'class': LocalEnsemble, # 'kwargs': { # 'num_eigs': 20, # 'device': 'gpu', # 'n_y_samp': 5, # }, # 'load_name': 'local_ensemble', # 'forward_kwargs': { # 'n_samples': 1 # } # }, # 'SCOD (T=76,k=12)': { # 'class': SCOD, # 'kwargs': { # 'num_eigs': 12, # 'device':'gpu', # }, # 'load_name': 'scod_SRFT_s76_n12', # 'forward_kwargs': { # 'n_eigs': 12, # } # }, # 'SCOD_freeze_0.25 (T=76,k=12)': { # 'class': SCOD, # 'kwargs': { # 'num_eigs': 12, # 'device':'gpu', # }, # 'freeze': 0.25, # 'load_name': 'scod_SRFT_s76_n12_freeze_0.25', # 'forward_kwargs': { # 'n_eigs': 12, # } # }, # 'SCOD_freeze_0.5 (T=76,k=12)': { # 'class': SCOD, # 'kwargs': { # 'num_eigs': 12, # 'device':'gpu', # }, # 'freeze': 0.5, # 'load_name': 'scod_SRFT_s76_n12_freeze_0.5', # 'forward_kwargs': { # 'n_eigs': 12, # } # }, # 'SCOD_freeze_0.75 (T=76,k=12)': { # 'class': SCOD, # 'kwargs': { # 'num_eigs': 12, # 'device':'gpu', # }, # 'freeze': 0.75, # 'load_name': 'scod_SRFT_s76_n12_freeze_0.75', # 'forward_kwargs': { # 'n_eigs': 12, # } # }, # 'SCOD_freeze_0.85 (T=76,k=12)': { # 'class': SCOD, # 'kwargs': { # 'num_eigs': 12, # 'device':'gpu', # }, # 'freeze': 0.85, # 'load_name': 'scod_SRFT_s76_n12_freeze_0.85', # 'forward_kwargs': { # 'n_eigs': 12, # } # }, # 'SCOD_freeze_1.0 (T=76,k=12)': { # 'class': SCOD, # 'kwargs': { # 'num_eigs': 12, # 'device':'gpu', # }, # 'freeze': 1.0, # 'load_name': 'scod_SRFT_s76_n12_freeze_1.0', # 'forward_kwargs': { # 'n_eigs': 12, # } # }, 'SCOD_freeze_0.85 (T=184,k=30)': { 'class': SCOD, 'kwargs': { 'num_eigs': 30, 'device':'gpu', }, 'freeze': 0.85, 'load_name': 'scod_SRFT_s184_n30_freeze_0.85', 'forward_kwargs': { 'n_eigs': 30, } }, 'kfac_n1e6_s5000': { 'class': KFAC, 'kwargs': { 'device':'gpu', 'input_shape':[3,224,224] }, 'load_name': 'kfac', 'forward_kwargs': { 'norm': 1e6, 'scale': 5000. } }, # 'naive': { # 'class': Naive, # 'kwargs': { # 'device':'gpu' # }, # 'load_name': None, # 'forward_kwargs': {} # }, # 'ensemble': { # 'class': Ensemble, # 'kwargs': { # 'device': 'gpu', # }, # 'load_name': None, # 'multi_model': True, # 'forward_kwargs': {} # } # 'maha': { # 'class': Mahalanobis, # 'kwargs': { # 'device': 'gpu', # "features_and_classifier": get_features_and_classifier, # 'num_classes': 5, # }, # 'load_name': 'maha', # 'forward_kwargs': {} # } } # OOD PERFORMANCE TESTS from nn_ood.utils.viz import summarize_ood_results, summarize_ood_results_by_error, plot_perf_vs_runtime splits_to_use = test_dataset_args err_thresh = 2. in_dist_splits = test_dataset_args[0:1] out_dist_splits = test_dataset_args[2:] keys_to_compare = [ 'SCOD (T=76,k=12)', 'SCOD_freeze_0.85 (T=184,k=30)', 'local_ensemble', 'kfac_n1e6_s5000', 'naive', 'maha', ] colors = [ 'xkcd:azure', 'xkcd:electric blue', 'xkcd:mango', 'xkcd:blood orange', 'xkcd:scarlet', 'xkcd:indigo' ] plots_to_generate = { 'auroc_vs_runtime.pdf': { 'summary_fn': summarize_ood_results, 'summary_fn_args': [ in_dist_splits, out_dist_splits ], 'summary_fn_kwargs': { 'keys_to_compare': keys_to_compare, }, 'plot_fn': plot_perf_vs_runtime, 'plot_fn_args': [], 'plot_fn_kwargs': { 'colors': colors, 'figsize': [4,2.5], 'dpi': 150, 'normalize_x': True, }, 'legend': { 'labels': [ 'SCOD', 'SCOD (LL)', 'Local Ensemble', 'KFAC', 'Naive', 'Maha' ] }, 'title': "CIFAR10", }, } freeze_keys = [ 'SCOD (T=76,k=12)', 'SCOD_freeze_0.25 (T=76,k=12)', 'SCOD_freeze_0.5 (T=76,k=12)', 'SCOD_freeze_0.75 (T=76,k=12)', 'SCOD_freeze_0.85 (T=76,k=12)', 'SCOD_freeze_1.0 (T=76,k=12)', 'SCOD_freeze_0.85 (T=184,k=30)', ] freeze_labels = [ "SCOD (T=76,k=12)", "SCOD (LL 0.75) (T=76,k=12)", "SCOD (LL 0.50) (T=76,k=12)", "SCOD (LL 0.25) (T=76,k=12)", "SCOD (LL 0.15) (T=76,k=12)", "SCOD (only linear) (T=76,k=12)", "SCOD (LL 0.15) (T=184,k=30)" ] import seaborn as sns cmap = sns.color_palette("crest", as_cmap=True) freeze_colors = [cmap(k/6) for k in range(6)] + ["xkcd:azure"] for split, label in zip(['ood', 'svhn', 'tIN', 'lsun'],['CIFAR class >= 5','SVHN','TinyImageNet', 'LSUN']): plot = { 'summary_fn': summarize_ood_results, 'summary_fn_args': [ in_dist_splits, [split], ], 'summary_fn_kwargs': { 'keys_to_compare': freeze_keys, }, 'plot_fn': plot_perf_vs_runtime, 'plot_fn_args': [], 'plot_fn_kwargs': { 'colors': freeze_colors, 'figsize': [6,3], 'dpi': 150, 'normalize_x': True, }, 'legend': { 'labels': freeze_labels }, 'title': "In: CIFAR class < 5" + " | Out: " + label, } plot_name = 'freeze_test_%s.pdf' % split plots_to_generate[plot_name] = plot
[ "torch.nn.AdaptiveAvgPool2d", "nn_ood.distributions.CategoricalLogit", "torch.nn.ReLU", "torch.nn.Sequential", "numpy.argmax", "numpy.clip", "numpy.array", "densenet.densenet121", "seaborn.color_palette", "torch.cuda.is_available", "matplotlib.pyplot.subplots", "torch.nn.Flatten" ]
[((11970, 12010), 'seaborn.color_palette', 'sns.color_palette', (['"""crest"""'], {'as_cmap': '(True)'}), "('crest', as_cmap=True)\n", (11987, 12010), True, 'import seaborn as sns\n'), ((783, 817), 'numpy.array', 'np.array', (['[0.4914, 0.4822, 0.4465]'], {}), '([0.4914, 0.4822, 0.4465])\n', (791, 817), True, 'import numpy as np\n'), ((828, 862), 'numpy.array', 'np.array', (['[0.2471, 0.2435, 0.2616]'], {}), '([0.2471, 0.2435, 0.2616])\n', (836, 862), True, 'import numpy as np\n'), ((900, 918), 'numpy.clip', 'np.clip', (['inp', '(0)', '(1)'], {}), '(inp, 0, 1)\n', (907, 918), True, 'import numpy as np\n'), ((1689, 1752), 'matplotlib.pyplot.subplots', 'plt.subplots', (['(1)', 'num_plots'], {'figsize': '[5 * num_plots, 5]', 'dpi': '(100)'}), '(1, num_plots, figsize=[5 * num_plots, 5], dpi=100)\n', (1701, 1752), True, 'import matplotlib.pyplot as plt\n'), ((2365, 2393), 'densenet.densenet121', 'densenet121', ([], {'pretrained': '(True)'}), '(pretrained=True)\n', (2376, 2393), False, 'from densenet import densenet121\n'), ((2450, 2479), 'torch.nn.Sequential', 'nn.Sequential', (['base', 'selector'], {}), '(base, selector)\n', (2463, 2479), True, 'import torch.nn as nn\n'), ((3706, 3750), 'torch.nn.Sequential', 'nn.Sequential', (['model[0].classifier', 'model[1]'], {}), '(model[0].classifier, model[1])\n', (3719, 3750), True, 'import torch.nn as nn\n'), ((1988, 2013), 'torch.cuda.is_available', 'torch.cuda.is_available', ([], {}), '()\n', (2011, 2013), False, 'import torch\n'), ((3618, 3627), 'torch.nn.ReLU', 'nn.ReLU', ([], {}), '()\n', (3625, 3627), True, 'import torch.nn as nn\n'), ((3637, 3660), 'torch.nn.AdaptiveAvgPool2d', 'nn.AdaptiveAvgPool2d', (['(1)'], {}), '(1)\n', (3657, 3660), True, 'import torch.nn as nn\n'), ((3670, 3682), 'torch.nn.Flatten', 'nn.Flatten', ([], {}), '()\n', (3680, 3682), True, 'import torch.nn as nn\n'), ((3846, 3864), 'nn_ood.distributions.CategoricalLogit', 'CategoricalLogit', ([], {}), '()\n', (3862, 3864), False, 'from nn_ood.distributions import CategoricalLogit\n'), ((1345, 1360), 'numpy.argmax', 'np.argmax', (['pred'], {}), '(pred)\n', (1354, 1360), True, 'import numpy as np\n'), ((1528, 1543), 'numpy.argmax', 'np.argmax', (['pred'], {}), '(pred)\n', (1537, 1543), True, 'import numpy as np\n')]
"""The interface defining class for fit modes.""" from abc import ABC, abstractmethod import numpy as np from iminuit import Minuit class AbstractFitPlugin(ABC): """Minuit wrapper to standardize usage with different likelihood function definitions and parameter transformations. """ def __init__( self, data_set, use_expected_counts=True, rng=None, has_limits=False, print_brs_sum_not_1=True, _precalculated_M=None, # Can be inherited in toy studies, ): self._data_set = data_set self._use_expected_counts = use_expected_counts self.rng = rng self._precalculated_M = _precalculated_M self._counts = {} fcn = self._create_likelihood() internal_starters = self.transform_to_internal(data_set.fit_start_brs) self.Minuit = Minuit(fcn, internal_starters) self.has_limits = has_limits if not self._enforces_brs_sum_to_1 and print_brs_sum_not_1: print( f"INFO: The chosen minimizer plugin {self.__class__.__name__} " "does not enforce the branching ratios to sum to 1. \n" " On top of being conceptually problematic, this will break " "if the signal cross section does not match with the expectation." ) def __repr__(self): return self.__class__.__name__ @property def errors(self): return np.array(self.covariance).diagonal() ** 0.5 @abstractmethod def _create_likelihood(self): pass @abstractmethod def transform_to_internal(self, values): pass @property @abstractmethod def values(self): pass @property @abstractmethod def parameters(self): pass @property @abstractmethod def covariance(self): pass @property @abstractmethod def _default_limits(self): pass @property def has_limits(self): inf = float("infinity") return self.Minuit.limits != [(-inf, inf)] * len(self.Minuit.limits) @has_limits.setter def has_limits(self, new_has_limits): if not isinstance(new_has_limits, bool): raise TypeError( f"Expected Bool. {type(new_has_limits)=}, {new_has_limits=}." ) if new_has_limits == self.has_limits: pass elif new_has_limits: self.Minuit.limits = self._default_limits else: inf = float("infinity") self.Minuit.limits = [(-inf, inf)] * len(self.Minuit.limits) @property @abstractmethod def _enforces_brs_sum_to_1(self) -> bool: """A True/False hook to allow some relaxing for sub-ideal likelihood descriptions.""" pass def _prepare_numpy_y_M(self): """Prepare the MC counts matrix and box counts as numpy arrays.""" data_set = self._data_set n_boxes_per_channel = { k: len(ch.box_names) for k, ch in data_set.get_channels().items() } n_boxes = sum(n_boxes_per_channel.values()) n_bkg = 1 y = np.empty(n_boxes) if self._precalculated_M is None: n_parameters = len(data_set.decay_names) M = np.empty((n_boxes, n_parameters + n_bkg)) i_stop = 0 for name, channel in data_set.get_channels().items(): i_start = i_stop i_stop = i_start + n_boxes_per_channel[name] # Fill y (in every toy) if self._use_expected_counts: self._counts[name] = channel.get_expected_counts() else: self._counts[name] = channel.get_toys(rng=self.rng) y[i_start:i_stop] = self._counts[name] # Fill M (if necessary) if self._precalculated_M is None: signal_factor = channel.signal_cs_default * channel.signal_scaler M[i_start:i_stop, :-n_bkg] = channel.mc_matrix[channel.decay_names] M[i_start:i_stop, :-n_bkg] *= signal_factor bkg_box_probabilities = ( channel.mc_matrix[channel.bkg_names] * channel.bkg_cs_default / channel.bkg_cs_default.sum() ).sum(axis=1) M[i_start:i_stop, -n_bkg] = bkg_box_probabilities M[i_start:i_stop, -n_bkg] *= channel.bkg_cs_default.sum() M[i_start:i_stop, :] *= channel.luminosity_ifb if self._precalculated_M is None: self._precalculated_M = M return y, self._precalculated_M, n_bkg
[ "numpy.empty", "numpy.array", "iminuit.Minuit" ]
[((865, 895), 'iminuit.Minuit', 'Minuit', (['fcn', 'internal_starters'], {}), '(fcn, internal_starters)\n', (871, 895), False, 'from iminuit import Minuit\n'), ((3147, 3164), 'numpy.empty', 'np.empty', (['n_boxes'], {}), '(n_boxes)\n', (3155, 3164), True, 'import numpy as np\n'), ((3277, 3318), 'numpy.empty', 'np.empty', (['(n_boxes, n_parameters + n_bkg)'], {}), '((n_boxes, n_parameters + n_bkg))\n', (3285, 3318), True, 'import numpy as np\n'), ((1469, 1494), 'numpy.array', 'np.array', (['self.covariance'], {}), '(self.covariance)\n', (1477, 1494), True, 'import numpy as np\n')]
from sklearn.datasets import fetch_20newsgroups import torchvision from sklearn.feature_extraction.text import TfidfVectorizer from os.path import join import numpy as np import pickle #TODO: Update mnist examples!!! def dataset_loader(dataset_path=None, dataset='mnist', seed=1): """ Loads a dataset and creates the appropriate train/test splits :param dataset_path: path to the datasets :param dataset: the dataset to be loaded :param seed: seed for creating train/test splits (when appropriate) :return: """ np.random.seed(seed) n_train = 5000 if dataset == 'mnist': train_data, train_labels, test_data, test_labels = load_mnist(dataset_path) train_data = train_data[:n_train, :] train_labels = train_labels[:n_train] elif dataset == '20ng': train_data, train_labels, test_data, test_labels = load_20ng_dataset_bow() train_data = train_data[:n_train, :] train_labels = train_labels[:n_train] elif dataset == '15scene': data, labels = load_15_scene_bow_features(dataset_path) # Get a split with 100 samples per class train_idx, test_idx = [], [] idx = np.random.permutation(data.shape[0]) data = data[idx] labels = labels[idx] for i in range(15): class_idx = np.where(labels == i)[0] train_idx.append(class_idx[:100]) test_idx.append(class_idx[100:]) train_idx = np.int64(np.concatenate(train_idx)) test_idx = np.int64(np.concatenate(test_idx)) # Get the actual split train_data = data[train_idx, :] train_labels = labels[train_idx] test_data = data[test_idx, :] test_labels = labels[test_idx] elif dataset == 'corel': data, labels = load_corel_bow_features(dataset_path) idx = np.random.permutation(data.shape[0]) data = data[idx] labels = labels[idx] # Get the actual split train_data = data[:4800, :] train_labels = labels[:4800] test_data = data[4800:, :] test_labels = labels[4800:] elif dataset == 'yale': data, labels = load_yale_dataset(dataset_path) # Get a split with 30 per sample train_idx, test_idx = [], [] idx = np.random.permutation(data.shape[0]) data = data[idx] labels = labels[idx] for i in range(38): class_idx = np.where(labels == i)[0] train_idx.append(class_idx[:30]) test_idx.append(class_idx[30:]) train_idx = np.int64(np.concatenate(train_idx)) test_idx = np.int64(np.concatenate(test_idx)) # Get the actual split train_data = data[train_idx, :] train_labels = labels[train_idx] test_data = data[test_idx, :] test_labels = labels[test_idx] elif dataset == 'kth': train_data, train_labels, test_data, test_labels = load_kth(dataset_path) idx = np.random.permutation(train_data.shape[0]) train_data, train_labels = train_data[idx], train_labels[idx] idx = np.random.permutation(test_data.shape[0]) test_data, test_labels = test_data[idx], test_labels[idx] else: print("Unknown dataset!") assert False return train_data, train_labels, test_data, test_labels def load_mnist(dataset_path): """ Loads the MNIST dataset :return: """ # Get the train split mnist = torchvision.datasets.MNIST(root=dataset_path, download=True, train=True) x_train, y_train = mnist.train_data.numpy(), mnist.train_labels.numpy() # Get the test split mnist = torchvision.datasets.MNIST(root=dataset_path, download=True, train=False) x_test, y_test = mnist.test_data.numpy(), mnist.test_labels.numpy() x_train = x_train.reshape((x_train.shape[0], -1)) / 255.0 x_test = x_test.reshape((x_test.shape[0], -1)) / 255.0 return np.float32(x_train), y_train, np.float32(x_test), y_test def load_20ng_dataset_bow(): """ Loads the 20NG dataset :return: """ newsgroups_train = fetch_20newsgroups(subset='train') newsgroups_test = fetch_20newsgroups(subset='test') # Convert data to tf-idf vectorizer = TfidfVectorizer(min_df=0.01, max_df=0.95) train_data = vectorizer.fit_transform(newsgroups_train.data) test_data = vectorizer.transform(newsgroups_test.data) train_data = train_data.todense() test_data = test_data.todense() train_labels = newsgroups_train.target test_labels = newsgroups_test.target return train_data, train_labels, test_data, test_labels def load_15_scene_bow_features(datasets_path): """ Loads the pre-extracted BoF features for the 15-scene dataset :return: """ datafile = join(datasets_path, 'scenes.pickle') with open(datafile, 'rb') as f: features = pickle.load(f, encoding='latin1') labels = pickle.load(f, encoding='latin1') labels = np.asarray(np.squeeze(labels), dtype='int') features = np.asarray(features, dtype='float32') return features, labels def load_corel_bow_features(datasets_path): """ Loads the pre-extracted BoF features for the Corel dataset :return: """ datafile = join(datasets_path, 'corel.pickle') with open(datafile, 'rb') as f: features = pickle.load(f, encoding='latin1') labels = pickle.load(f, encoding='latin1') labels = np.asarray(np.squeeze(labels), dtype='int') features = np.asarray(features, dtype='float32') return features, labels def load_kth(datasets_path): """ Loads the HoF/HoG features for the KTH dataset :return: """ datafile = join(datasets_path, 'kth.pickle') with open(datafile, 'rb') as f: train_data, train_labels = pickle.load(f, encoding='latin1'), pickle.load(f, encoding='latin1') test_data, test_labels = pickle.load(f, encoding='latin1'), pickle.load(f, encoding='latin1') return train_data, train_labels, test_data, test_labels def load_yale_dataset(datasets_path): """ Loads the ORL dataset """ datafile = join(datasets_path, 'yale.pickle') with open(datafile, 'rb') as f: features = pickle.load(f) labels = pickle.load(f) features = [x for x in features] features = np.asarray(features) / 255.0 labels = np.asarray(np.squeeze(labels), dtype='int') return features, labels
[ "numpy.random.seed", "sklearn.feature_extraction.text.TfidfVectorizer", "numpy.asarray", "numpy.float32", "pickle.load", "numpy.random.permutation", "numpy.where", "torchvision.datasets.MNIST", "sklearn.datasets.fetch_20newsgroups", "numpy.squeeze", "os.path.join", "numpy.concatenate" ]
[((546, 566), 'numpy.random.seed', 'np.random.seed', (['seed'], {}), '(seed)\n', (560, 566), True, 'import numpy as np\n'), ((3462, 3534), 'torchvision.datasets.MNIST', 'torchvision.datasets.MNIST', ([], {'root': 'dataset_path', 'download': '(True)', 'train': '(True)'}), '(root=dataset_path, download=True, train=True)\n', (3488, 3534), False, 'import torchvision\n'), ((3649, 3722), 'torchvision.datasets.MNIST', 'torchvision.datasets.MNIST', ([], {'root': 'dataset_path', 'download': '(True)', 'train': '(False)'}), '(root=dataset_path, download=True, train=False)\n', (3675, 3722), False, 'import torchvision\n'), ((4096, 4130), 'sklearn.datasets.fetch_20newsgroups', 'fetch_20newsgroups', ([], {'subset': '"""train"""'}), "(subset='train')\n", (4114, 4130), False, 'from sklearn.datasets import fetch_20newsgroups\n'), ((4153, 4186), 'sklearn.datasets.fetch_20newsgroups', 'fetch_20newsgroups', ([], {'subset': '"""test"""'}), "(subset='test')\n", (4171, 4186), False, 'from sklearn.datasets import fetch_20newsgroups\n'), ((4235, 4276), 'sklearn.feature_extraction.text.TfidfVectorizer', 'TfidfVectorizer', ([], {'min_df': '(0.01)', 'max_df': '(0.95)'}), '(min_df=0.01, max_df=0.95)\n', (4250, 4276), False, 'from sklearn.feature_extraction.text import TfidfVectorizer\n'), ((4779, 4815), 'os.path.join', 'join', (['datasets_path', '"""scenes.pickle"""'], {}), "(datasets_path, 'scenes.pickle')\n", (4783, 4815), False, 'from os.path import join\n'), ((5028, 5065), 'numpy.asarray', 'np.asarray', (['features'], {'dtype': '"""float32"""'}), "(features, dtype='float32')\n", (5038, 5065), True, 'import numpy as np\n'), ((5247, 5282), 'os.path.join', 'join', (['datasets_path', '"""corel.pickle"""'], {}), "(datasets_path, 'corel.pickle')\n", (5251, 5282), False, 'from os.path import join\n'), ((5496, 5533), 'numpy.asarray', 'np.asarray', (['features'], {'dtype': '"""float32"""'}), "(features, dtype='float32')\n", (5506, 5533), True, 'import numpy as np\n'), ((5689, 5722), 'os.path.join', 'join', (['datasets_path', '"""kth.pickle"""'], {}), "(datasets_path, 'kth.pickle')\n", (5693, 5722), False, 'from os.path import join\n'), ((6123, 6157), 'os.path.join', 'join', (['datasets_path', '"""yale.pickle"""'], {}), "(datasets_path, 'yale.pickle')\n", (6127, 6157), False, 'from os.path import join\n'), ((3929, 3948), 'numpy.float32', 'np.float32', (['x_train'], {}), '(x_train)\n', (3939, 3948), True, 'import numpy as np\n'), ((3959, 3977), 'numpy.float32', 'np.float32', (['x_test'], {}), '(x_test)\n', (3969, 3977), True, 'import numpy as np\n'), ((4871, 4904), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (4882, 4904), False, 'import pickle\n'), ((4922, 4955), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (4933, 4955), False, 'import pickle\n'), ((4980, 4998), 'numpy.squeeze', 'np.squeeze', (['labels'], {}), '(labels)\n', (4990, 4998), True, 'import numpy as np\n'), ((5339, 5372), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (5350, 5372), False, 'import pickle\n'), ((5390, 5423), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (5401, 5423), False, 'import pickle\n'), ((5448, 5466), 'numpy.squeeze', 'np.squeeze', (['labels'], {}), '(labels)\n', (5458, 5466), True, 'import numpy as np\n'), ((6214, 6228), 'pickle.load', 'pickle.load', (['f'], {}), '(f)\n', (6225, 6228), False, 'import pickle\n'), ((6246, 6260), 'pickle.load', 'pickle.load', (['f'], {}), '(f)\n', (6257, 6260), False, 'import pickle\n'), ((6314, 6334), 'numpy.asarray', 'np.asarray', (['features'], {}), '(features)\n', (6324, 6334), True, 'import numpy as np\n'), ((6367, 6385), 'numpy.squeeze', 'np.squeeze', (['labels'], {}), '(labels)\n', (6377, 6385), True, 'import numpy as np\n'), ((5794, 5827), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (5805, 5827), False, 'import pickle\n'), ((5829, 5862), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (5840, 5862), False, 'import pickle\n'), ((5896, 5929), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (5907, 5929), False, 'import pickle\n'), ((5931, 5964), 'pickle.load', 'pickle.load', (['f'], {'encoding': '"""latin1"""'}), "(f, encoding='latin1')\n", (5942, 5964), False, 'import pickle\n'), ((1186, 1222), 'numpy.random.permutation', 'np.random.permutation', (['data.shape[0]'], {}), '(data.shape[0])\n', (1207, 1222), True, 'import numpy as np\n'), ((1474, 1499), 'numpy.concatenate', 'np.concatenate', (['train_idx'], {}), '(train_idx)\n', (1488, 1499), True, 'import numpy as np\n'), ((1529, 1553), 'numpy.concatenate', 'np.concatenate', (['test_idx'], {}), '(test_idx)\n', (1543, 1553), True, 'import numpy as np\n'), ((1849, 1885), 'numpy.random.permutation', 'np.random.permutation', (['data.shape[0]'], {}), '(data.shape[0])\n', (1870, 1885), True, 'import numpy as np\n'), ((1329, 1350), 'numpy.where', 'np.where', (['(labels == i)'], {}), '(labels == i)\n', (1337, 1350), True, 'import numpy as np\n'), ((2292, 2328), 'numpy.random.permutation', 'np.random.permutation', (['data.shape[0]'], {}), '(data.shape[0])\n', (2313, 2328), True, 'import numpy as np\n'), ((2578, 2603), 'numpy.concatenate', 'np.concatenate', (['train_idx'], {}), '(train_idx)\n', (2592, 2603), True, 'import numpy as np\n'), ((2633, 2657), 'numpy.concatenate', 'np.concatenate', (['test_idx'], {}), '(test_idx)\n', (2647, 2657), True, 'import numpy as np\n'), ((2973, 3015), 'numpy.random.permutation', 'np.random.permutation', (['train_data.shape[0]'], {}), '(train_data.shape[0])\n', (2994, 3015), True, 'import numpy as np\n'), ((3100, 3141), 'numpy.random.permutation', 'np.random.permutation', (['test_data.shape[0]'], {}), '(test_data.shape[0])\n', (3121, 3141), True, 'import numpy as np\n'), ((2435, 2456), 'numpy.where', 'np.where', (['(labels == i)'], {}), '(labels == i)\n', (2443, 2456), True, 'import numpy as np\n')]
import numpy as np import pandas as pd def _to_binary(target): return (target > target.median()).astype(int) def generate_test_data(data_size): df = pd.DataFrame() np.random.seed(0) df["A"] = np.random.rand(data_size) df["B"] = np.random.rand(data_size) df["C"] = np.random.rand(data_size) df["D"] = np.random.rand(data_size) df["target"] = 0.2 * np.random.rand(data_size) + df["A"] * df["D"] + 2 * df["B"] df["binary_target"] = _to_binary(df["target"]) return df def generate_unstructured_test_data(data_size): df = generate_test_data(data_size) df.loc[np.random.rand(data_size) < 0.3, "A"] = None df["E"] = np.random.choice(["category1", "category2", "category3"], data_size) df["target"] = (df["E"] != "category2")*df["target"] df["binary_target"] = _to_binary(df["target"]) return df
[ "pandas.DataFrame", "numpy.random.rand", "numpy.random.seed", "numpy.random.choice" ]
[((161, 175), 'pandas.DataFrame', 'pd.DataFrame', ([], {}), '()\n', (173, 175), True, 'import pandas as pd\n'), ((181, 198), 'numpy.random.seed', 'np.random.seed', (['(0)'], {}), '(0)\n', (195, 198), True, 'import numpy as np\n'), ((213, 238), 'numpy.random.rand', 'np.random.rand', (['data_size'], {}), '(data_size)\n', (227, 238), True, 'import numpy as np\n'), ((253, 278), 'numpy.random.rand', 'np.random.rand', (['data_size'], {}), '(data_size)\n', (267, 278), True, 'import numpy as np\n'), ((293, 318), 'numpy.random.rand', 'np.random.rand', (['data_size'], {}), '(data_size)\n', (307, 318), True, 'import numpy as np\n'), ((333, 358), 'numpy.random.rand', 'np.random.rand', (['data_size'], {}), '(data_size)\n', (347, 358), True, 'import numpy as np\n'), ((669, 737), 'numpy.random.choice', 'np.random.choice', (["['category1', 'category2', 'category3']", 'data_size'], {}), "(['category1', 'category2', 'category3'], data_size)\n", (685, 737), True, 'import numpy as np\n'), ((385, 410), 'numpy.random.rand', 'np.random.rand', (['data_size'], {}), '(data_size)\n', (399, 410), True, 'import numpy as np\n'), ((610, 635), 'numpy.random.rand', 'np.random.rand', (['data_size'], {}), '(data_size)\n', (624, 635), True, 'import numpy as np\n')]
# -*- coding: utf-8 -*- """MedleyDB pitch Dataset Loader .. admonition:: Dataset Info :class: dropdown MedleyDB Pitch is a pitch-tracking subset of the MedleyDB dataset containing only f0-annotated, monophonic stems. MedleyDB is a dataset of annotated, royalty-free multitrack recordings. MedleyDB was curated primarily to support research on melody extraction, addressing important shortcomings of existing collections. For each song we provide melody f0 annotations as well as instrument activations for evaluating automatic instrument recognition. For more details, please visit: https://medleydb.weebly.com """ import csv import json import librosa import logging import numpy as np import os from mirdata import download_utils from mirdata import jams_utils from mirdata import core from mirdata import annotations BIBTEX = """@inproceedings{bittner2014medleydb, Author = {<NAME> and Salamon, Justin and Tierney, Mike and Mauch, Matthias and Cannam, Chris and <NAME>}, Booktitle = {International Society of Music Information Retrieval (ISMIR)}, Month = {October}, Title = {Medley{DB}: A Multitrack Dataset for Annotation-Intensive {MIR} Research}, Year = {2014} }""" DOWNLOAD_INFO = """ To download this dataset, visit: https://zenodo.org/record/2620624#.XKZc7hNKh24 and request access. Once downloaded, unzip the file MedleyDB-Pitch.zip and copy the result to: {} """ LICENSE_INFO = ( "Creative Commons Attribution Non-Commercial Share-Alike 4.0 (CC BY-NC-SA 4.0)." ) def _load_metadata(data_home): metadata_path = os.path.join(data_home, "medleydb_pitch_metadata.json") if not os.path.exists(metadata_path): logging.info("Metadata file {} not found.".format(metadata_path)) return None with open(metadata_path, "r") as fhandle: metadata = json.load(fhandle) metadata["data_home"] = data_home return metadata DATA = core.LargeData("medleydb_pitch_index.json", _load_metadata) class Track(core.Track): """medleydb_pitch Track class Args: track_id (str): track id of the track Attributes: artist (str): artist audio_path (str): path to the audio file genre (str): genre instrument (str): instrument of the track pitch_path (str): path to the pitch annotation file title (str): title track_id (str): track id Cached Properties: pitch (F0Data): human annotated pitch """ def __init__(self, track_id, data_home): if track_id not in DATA.index["tracks"]: raise ValueError( "{} is not a valid track ID in MedleyDB-Pitch".format(track_id) ) self.track_id = track_id self._data_home = data_home self._track_paths = DATA.index["tracks"][track_id] self.pitch_path = os.path.join(self._data_home, self._track_paths["pitch"][0]) metadata = DATA.metadata(data_home) if metadata is not None and track_id in metadata: self._track_metadata = metadata[track_id] else: self._track_metadata = { "instrument": None, "artist": None, "title": None, "genre": None, } self.audio_path = os.path.join(self._data_home, self._track_paths["audio"][0]) self.instrument = self._track_metadata["instrument"] self.artist = self._track_metadata["artist"] self.title = self._track_metadata["title"] self.genre = self._track_metadata["genre"] @core.cached_property def pitch(self): return load_pitch(self.pitch_path) @property def audio(self): """The track's audio Returns: * np.ndarray - audio signal * float - sample rate """ return load_audio(self.audio_path) def to_jams(self): """Get the track's data in jams format Returns: jams.JAMS: the track's data in jams format """ return jams_utils.jams_converter( audio_path=self.audio_path, f0_data=[(self.pitch, "annotated pitch")], metadata=self._track_metadata, ) def load_audio(audio_path): """Load a MedleyDB audio file. Args: audio_path (str): path to audio file Returns: * np.ndarray - the mono audio signal * float - The sample rate of the audio file """ if not os.path.exists(audio_path): raise IOError("audio_path {} does not exist".format(audio_path)) return librosa.load(audio_path, sr=None, mono=True) def load_pitch(pitch_path): """load a MedleyDB pitch annotation file Args: pitch_path (str): path to pitch annotation file Raises: IOError: if pitch_path doesn't exist Returns: F0Data: pitch annotation """ if not os.path.exists(pitch_path): raise IOError("pitch_path {} does not exist".format(pitch_path)) times = [] freqs = [] with open(pitch_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter=",") for line in reader: times.append(float(line[0])) freqs.append(float(line[1])) times = np.array(times) freqs = np.array(freqs) confidence = (freqs > 0).astype(float) pitch_data = annotations.F0Data(times, freqs, confidence) return pitch_data @core.docstring_inherit(core.Dataset) class Dataset(core.Dataset): """ The medleydb_pitch dataset """ def __init__(self, data_home=None): super().__init__( data_home, index=DATA.index, name="medleydb_pitch", track_object=Track, bibtex=BIBTEX, download_info=DOWNLOAD_INFO, license_info=LICENSE_INFO, ) @core.copy_docs(load_audio) def load_audio(self, *args, **kwargs): return load_audio(*args, **kwargs) @core.copy_docs(load_pitch) def load_pitch(self, *args, **kwargs): return load_pitch(*args, **kwargs)
[ "json.load", "mirdata.core.copy_docs", "csv.reader", "mirdata.annotations.F0Data", "mirdata.jams_utils.jams_converter", "os.path.exists", "mirdata.core.docstring_inherit", "mirdata.core.LargeData", "numpy.array", "librosa.load", "os.path.join" ]
[((1974, 2033), 'mirdata.core.LargeData', 'core.LargeData', (['"""medleydb_pitch_index.json"""', '_load_metadata'], {}), "('medleydb_pitch_index.json', _load_metadata)\n", (1988, 2033), False, 'from mirdata import core\n'), ((5465, 5501), 'mirdata.core.docstring_inherit', 'core.docstring_inherit', (['core.Dataset'], {}), '(core.Dataset)\n', (5487, 5501), False, 'from mirdata import core\n'), ((1628, 1683), 'os.path.join', 'os.path.join', (['data_home', '"""medleydb_pitch_metadata.json"""'], {}), "(data_home, 'medleydb_pitch_metadata.json')\n", (1640, 1683), False, 'import os\n'), ((4629, 4673), 'librosa.load', 'librosa.load', (['audio_path'], {'sr': 'None', 'mono': '(True)'}), '(audio_path, sr=None, mono=True)\n', (4641, 4673), False, 'import librosa\n'), ((5291, 5306), 'numpy.array', 'np.array', (['times'], {}), '(times)\n', (5299, 5306), True, 'import numpy as np\n'), ((5319, 5334), 'numpy.array', 'np.array', (['freqs'], {}), '(freqs)\n', (5327, 5334), True, 'import numpy as np\n'), ((5395, 5439), 'mirdata.annotations.F0Data', 'annotations.F0Data', (['times', 'freqs', 'confidence'], {}), '(times, freqs, confidence)\n', (5413, 5439), False, 'from mirdata import annotations\n'), ((5888, 5914), 'mirdata.core.copy_docs', 'core.copy_docs', (['load_audio'], {}), '(load_audio)\n', (5902, 5914), False, 'from mirdata import core\n'), ((6007, 6033), 'mirdata.core.copy_docs', 'core.copy_docs', (['load_pitch'], {}), '(load_pitch)\n', (6021, 6033), False, 'from mirdata import core\n'), ((1696, 1725), 'os.path.exists', 'os.path.exists', (['metadata_path'], {}), '(metadata_path)\n', (1710, 1725), False, 'import os\n'), ((1887, 1905), 'json.load', 'json.load', (['fhandle'], {}), '(fhandle)\n', (1896, 1905), False, 'import json\n'), ((2898, 2958), 'os.path.join', 'os.path.join', (['self._data_home', "self._track_paths['pitch'][0]"], {}), "(self._data_home, self._track_paths['pitch'][0])\n", (2910, 2958), False, 'import os\n'), ((3338, 3398), 'os.path.join', 'os.path.join', (['self._data_home', "self._track_paths['audio'][0]"], {}), "(self._data_home, self._track_paths['audio'][0])\n", (3350, 3398), False, 'import os\n'), ((4089, 4220), 'mirdata.jams_utils.jams_converter', 'jams_utils.jams_converter', ([], {'audio_path': 'self.audio_path', 'f0_data': "[(self.pitch, 'annotated pitch')]", 'metadata': 'self._track_metadata'}), "(audio_path=self.audio_path, f0_data=[(self.pitch,\n 'annotated pitch')], metadata=self._track_metadata)\n", (4114, 4220), False, 'from mirdata import jams_utils\n'), ((4516, 4542), 'os.path.exists', 'os.path.exists', (['audio_path'], {}), '(audio_path)\n', (4530, 4542), False, 'import os\n'), ((4941, 4967), 'os.path.exists', 'os.path.exists', (['pitch_path'], {}), '(pitch_path)\n', (4955, 4967), False, 'import os\n'), ((5133, 5167), 'csv.reader', 'csv.reader', (['fhandle'], {'delimiter': '""","""'}), "(fhandle, delimiter=',')\n", (5143, 5167), False, 'import csv\n')]
from __future__ import division import torch import numpy as np def parse_conv_block(m, weights, offset, initflag): """ Initialization of conv layers with batchnorm Args: m (Sequential): sequence of layers weights (numpy.ndarray): pretrained weights data offset (int): current position in the weights file initflag (bool): if True, the layers are not covered by the weights file. \ They are initialized using darknet-style initialization. Returns: offset (int): current position in the weights file weights (numpy.ndarray): pretrained weights data """ conv_model = m[0] bn_model = m[1] param_length = m[1].bias.numel() # numel()获取张量元素的个数 # batchnorm for pname in ['bias', 'weight', 'running_mean', 'running_var']: layerparam = getattr(bn_model, pname) if initflag: # yolo initialization - scale to one, bias to zero if pname == 'weight': weights = np.append(weights, np.ones(param_length)) else: weights = np.append(weights, np.zeros(param_length)) param = torch.from_numpy(weights[offset:offset + param_length]).view_as(layerparam) layerparam.data.copy_(param) offset += param_length param_length = conv_model.weight.numel() # conv if initflag: # yolo initialization n, c, k, _ = conv_model.weight.shape scale = np.sqrt(2 / (k * k * c)) weights = np.append(weights, scale * np.random.normal(size=param_length)) param = torch.from_numpy( weights[offset:offset + param_length]).view_as(conv_model.weight) conv_model.weight.data.copy_(param) offset += param_length return offset, weights def parse_yolo_block(m, weights, offset, initflag): """ YOLO Layer (one conv with bias) Initialization Args: m (Sequential): sequence of layers weights (numpy.ndarray): pretrained weights data offset (int): current position in the weights file initflag (bool): if True, the layers are not covered by the weights file. \ They are initialized using darknet-style initialization. Returns: offset (int): current position in the weights file weights (numpy.ndarray): pretrained weights data """ conv_model = m._modules['conv'] param_length = conv_model.bias.numel() if initflag: # yolo initialization - bias to zero weights = np.append(weights, np.zeros(param_length)) param = torch.from_numpy( weights[offset:offset + param_length]).view_as(conv_model.bias) conv_model.bias.data.copy_(param) offset += param_length param_length = conv_model.weight.numel() if initflag: # yolo initialization n, c, k, _ = conv_model.weight.shape scale = np.sqrt(2 / (k * k * c)) weights = np.append(weights, scale * np.random.normal(size=param_length)) param = torch.from_numpy( weights[offset:offset + param_length]).view_as(conv_model.weight) conv_model.weight.data.copy_(param) offset += param_length return offset, weights def parse_yolo_weights(model, weights_path): """ Parse YOLO (darknet) pre-trained weights data onto the pytorch model Args: model : pytorch model object weights_path (str): path to the YOLO (darknet) pre-trained weights file """ fp = open(weights_path, "rb") # skip the header header = np.fromfile(fp, dtype=np.int32, count=5) # not used # read weights weights = np.fromfile(fp, dtype=np.float32) fp.close() offset = 0 initflag = False #whole yolo weights : False, darknet weights : True for m in model.module_list: if m._get_name() == 'Sequential': # normal conv block offset, weights = parse_conv_block(m, weights, offset, initflag) elif m._get_name() == 'resblock': # residual block for modu in m._modules['module_list']: for blk in modu: offset, weights = parse_conv_block(blk, weights, offset, initflag) elif m._get_name() == 'YOLOLayer': # YOLO Layer (one conv with bias) Initialization offset, weights = parse_yolo_block(m, weights, offset, initflag) initflag = (offset >= len(weights)) # the end of the weights file. turn the flag on
[ "numpy.fromfile", "numpy.zeros", "numpy.ones", "numpy.random.normal", "numpy.sqrt", "torch.from_numpy" ]
[((3473, 3513), 'numpy.fromfile', 'np.fromfile', (['fp'], {'dtype': 'np.int32', 'count': '(5)'}), '(fp, dtype=np.int32, count=5)\n', (3484, 3513), True, 'import numpy as np\n'), ((3559, 3592), 'numpy.fromfile', 'np.fromfile', (['fp'], {'dtype': 'np.float32'}), '(fp, dtype=np.float32)\n', (3570, 3592), True, 'import numpy as np\n'), ((1445, 1469), 'numpy.sqrt', 'np.sqrt', (['(2 / (k * k * c))'], {}), '(2 / (k * k * c))\n', (1452, 1469), True, 'import numpy as np\n'), ((2833, 2857), 'numpy.sqrt', 'np.sqrt', (['(2 / (k * k * c))'], {}), '(2 / (k * k * c))\n', (2840, 2857), True, 'import numpy as np\n'), ((1565, 1620), 'torch.from_numpy', 'torch.from_numpy', (['weights[offset:offset + param_length]'], {}), '(weights[offset:offset + param_length])\n', (1581, 1620), False, 'import torch\n'), ((2494, 2516), 'numpy.zeros', 'np.zeros', (['param_length'], {}), '(param_length)\n', (2502, 2516), True, 'import numpy as np\n'), ((2531, 2586), 'torch.from_numpy', 'torch.from_numpy', (['weights[offset:offset + param_length]'], {}), '(weights[offset:offset + param_length])\n', (2547, 2586), False, 'import torch\n'), ((2954, 3009), 'torch.from_numpy', 'torch.from_numpy', (['weights[offset:offset + param_length]'], {}), '(weights[offset:offset + param_length])\n', (2970, 3009), False, 'import torch\n'), ((1143, 1198), 'torch.from_numpy', 'torch.from_numpy', (['weights[offset:offset + param_length]'], {}), '(weights[offset:offset + param_length])\n', (1159, 1198), False, 'import torch\n'), ((1515, 1550), 'numpy.random.normal', 'np.random.normal', ([], {'size': 'param_length'}), '(size=param_length)\n', (1531, 1550), True, 'import numpy as np\n'), ((2903, 2938), 'numpy.random.normal', 'np.random.normal', ([], {'size': 'param_length'}), '(size=param_length)\n', (2919, 2938), True, 'import numpy as np\n'), ((1016, 1037), 'numpy.ones', 'np.ones', (['param_length'], {}), '(param_length)\n', (1023, 1037), True, 'import numpy as np\n'), ((1102, 1124), 'numpy.zeros', 'np.zeros', (['param_length'], {}), '(param_length)\n', (1110, 1124), True, 'import numpy as np\n')]
import sklearn.datasets as dt import matplotlib.pyplot as plt import numpy as np seed = 1 # Create dataset """ x_data,y_data = dt.make_classification(n_samples=1000, n_features=2, n_repeated=0, class_sep=2, n_redundant=0, random_state=seed) """ x_data,y_data = dt.make_circles(n_samples=700, noise=0.2, factor=0.3) # Plot dataset my_scatter_plot = plt.scatter(x_data[:,0], x_data[:,1], c=y_data, vmin=min(y_data), vmax=max(y_data), s=35) plt.savefig("data.png") plt.show() # Format y_data y_data = np.array([[1,0] if y==0 else [0,1] for y in y_data]) # Save data into csv files np.savetxt("x_data.csv", x_data,delimiter=',',fmt='%f') np.savetxt("y_data.csv", y_data,delimiter=',',fmt='%f')
[ "sklearn.datasets.make_circles", "matplotlib.pyplot.show", "numpy.savetxt", "numpy.array", "matplotlib.pyplot.savefig" ]
[((458, 511), 'sklearn.datasets.make_circles', 'dt.make_circles', ([], {'n_samples': '(700)', 'noise': '(0.2)', 'factor': '(0.3)'}), '(n_samples=700, noise=0.2, factor=0.3)\n', (473, 511), True, 'import sklearn.datasets as dt\n'), ((866, 889), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""data.png"""'], {}), "('data.png')\n", (877, 889), True, 'import matplotlib.pyplot as plt\n'), ((890, 900), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (898, 900), True, 'import matplotlib.pyplot as plt\n'), ((929, 987), 'numpy.array', 'np.array', (['[([1, 0] if y == 0 else [0, 1]) for y in y_data]'], {}), '([([1, 0] if y == 0 else [0, 1]) for y in y_data])\n', (937, 987), True, 'import numpy as np\n'), ((1010, 1067), 'numpy.savetxt', 'np.savetxt', (['"""x_data.csv"""', 'x_data'], {'delimiter': '""","""', 'fmt': '"""%f"""'}), "('x_data.csv', x_data, delimiter=',', fmt='%f')\n", (1020, 1067), True, 'import numpy as np\n'), ((1066, 1123), 'numpy.savetxt', 'np.savetxt', (['"""y_data.csv"""', 'y_data'], {'delimiter': '""","""', 'fmt': '"""%f"""'}), "('y_data.csv', y_data, delimiter=',', fmt='%f')\n", (1076, 1123), True, 'import numpy as np\n')]
# A simple script that plots the time and the speedup # of the parallel OpenMP program as the number of available # cores increases. import matplotlib.pyplot as plt import sys import numpy as np import matplotlib matplotlib.use('Agg') t_64 = [] t_1024 = [] t_4096 = [] s_64 = [] s_1024 = [] s_4096 = [] fp = open(sys.argv[1]) line = fp.readline() while line: tokens = line.split() if tokens[2] == '64': t_64.append(float(tokens[6]) * 100) if tokens[2] == '1024': t_1024.append(float(tokens[6])) if tokens[2] == '4096': t_4096.append(float(tokens[6])) line = fp.readline() fp.close() print(t_64) print(t_1024) print(t_4096) for i in range(0, len(t_64)): s_64.append(t_64[0] / t_64[i]) s_1024.append(t_1024[0] / t_1024[i]) s_4096.append(t_4096[0] / t_4096[i]) print(s_64) print(s_1024) print(s_4096) fig, ax = plt.subplots() ax.grid(True) ax.set_xlabel("number of cores") ax.xaxis.set_ticks(np.arange(0, 5, 1)) ax.set_xticklabels(['1', '2', '4', '6', '8'], rotation=45) ax.set_xlim(-0.5, 4.5) ax.set_ylabel("time (ms)") plt.plot(t_64, label="Time", color="blue", marker='x') plt.title("Game of Life in 64×64 table") plt.savefig("time_64.png", bbox_inches="tight") fig, ax = plt.subplots() ax.grid(True) ax.set_xlabel("number of cores") ax.xaxis.set_ticks(np.arange(0, 5, 1)) ax.set_xticklabels(['1', '2', '4', '6', '8'], rotation=45) ax.set_xlim(-0.5, 4.5) ax.set_ylabel("time (s)") plt.plot(t_1024, label="Time", color="blue", marker='x') plt.title("Game of Life 1024×1024 table") plt.savefig("time_1024.png", bbox_inches="tight") fig, ax = plt.subplots() ax.grid(True) ax.set_xlabel("number of cores") ax.xaxis.set_ticks(np.arange(0, 5, 1)) ax.set_xticklabels(['1', '2', '4', '6', '8'], rotation=45) ax.set_xlim(-0.5, 4.5) ax.set_ylabel("time (s)") plt.plot(t_4096, label="Time", color="blue", marker='x') plt.title("Game of Life 4096×4096 table") plt.savefig("time_4096.png", bbox_inches="tight") fig, ax = plt.subplots() ax.grid(True) ax.set_xlabel("number of cores") ax.xaxis.set_ticks(np.arange(0, 5, 1)) ax.set_xticklabels(['1', '2', '4', '6', '8'], rotation=45) ax.set_xlim(-0.5, 4.5) ax.set_ylabel("speedup") plt.plot(s_64, label="Speedup", color="blue", marker='x') plt.title("Game of Life in 64×64 table") plt.savefig("speedup_64.png", bbox_inches="tight") fig, ax = plt.subplots() ax.grid(True) ax.set_xlabel("number of cores") ax.xaxis.set_ticks(np.arange(0, 5, 1)) ax.set_xticklabels(['1', '2', '4', '6', '8'], rotation=45) ax.set_xlim(-0.5, 4.5) ax.set_ylabel("speedup") plt.plot(s_1024, label="Speedup", color="blue", marker='x') plt.title("Game of Life 1024×1024 table") plt.savefig("speedup_1024.png", bbox_inches="tight") fig, ax = plt.subplots() ax.grid(True) ax.set_xlabel("number of cores") ax.xaxis.set_ticks(np.arange(0, 5, 1)) ax.set_xticklabels(['1', '2', '4', '6', '8'], rotation=45) ax.set_xlim(-0.5, 4.5) ax.set_ylabel("speedup") plt.plot(s_4096, label="Speedup", color="blue", marker='x') plt.title("Game of Life 4096×4096 table") plt.savefig("speedup_4096.png", bbox_inches="tight")
[ "matplotlib.pyplot.title", "matplotlib.pyplot.plot", "matplotlib.use", "numpy.arange", "matplotlib.pyplot.subplots", "matplotlib.pyplot.savefig" ]
[((214, 235), 'matplotlib.use', 'matplotlib.use', (['"""Agg"""'], {}), "('Agg')\n", (228, 235), False, 'import matplotlib\n'), ((873, 887), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (885, 887), True, 'import matplotlib.pyplot as plt\n'), ((1083, 1137), 'matplotlib.pyplot.plot', 'plt.plot', (['t_64'], {'label': '"""Time"""', 'color': '"""blue"""', 'marker': '"""x"""'}), "(t_64, label='Time', color='blue', marker='x')\n", (1091, 1137), True, 'import matplotlib.pyplot as plt\n'), ((1138, 1178), 'matplotlib.pyplot.title', 'plt.title', (['"""Game of Life in 64×64 table"""'], {}), "('Game of Life in 64×64 table')\n", (1147, 1178), True, 'import matplotlib.pyplot as plt\n'), ((1179, 1226), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""time_64.png"""'], {'bbox_inches': '"""tight"""'}), "('time_64.png', bbox_inches='tight')\n", (1190, 1226), True, 'import matplotlib.pyplot as plt\n'), ((1238, 1252), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (1250, 1252), True, 'import matplotlib.pyplot as plt\n'), ((1447, 1503), 'matplotlib.pyplot.plot', 'plt.plot', (['t_1024'], {'label': '"""Time"""', 'color': '"""blue"""', 'marker': '"""x"""'}), "(t_1024, label='Time', color='blue', marker='x')\n", (1455, 1503), True, 'import matplotlib.pyplot as plt\n'), ((1504, 1545), 'matplotlib.pyplot.title', 'plt.title', (['"""Game of Life 1024×1024 table"""'], {}), "('Game of Life 1024×1024 table')\n", (1513, 1545), True, 'import matplotlib.pyplot as plt\n'), ((1546, 1595), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""time_1024.png"""'], {'bbox_inches': '"""tight"""'}), "('time_1024.png', bbox_inches='tight')\n", (1557, 1595), True, 'import matplotlib.pyplot as plt\n'), ((1607, 1621), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (1619, 1621), True, 'import matplotlib.pyplot as plt\n'), ((1816, 1872), 'matplotlib.pyplot.plot', 'plt.plot', (['t_4096'], {'label': '"""Time"""', 'color': '"""blue"""', 'marker': '"""x"""'}), "(t_4096, label='Time', color='blue', marker='x')\n", (1824, 1872), True, 'import matplotlib.pyplot as plt\n'), ((1873, 1914), 'matplotlib.pyplot.title', 'plt.title', (['"""Game of Life 4096×4096 table"""'], {}), "('Game of Life 4096×4096 table')\n", (1882, 1914), True, 'import matplotlib.pyplot as plt\n'), ((1915, 1964), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""time_4096.png"""'], {'bbox_inches': '"""tight"""'}), "('time_4096.png', bbox_inches='tight')\n", (1926, 1964), True, 'import matplotlib.pyplot as plt\n'), ((1976, 1990), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (1988, 1990), True, 'import matplotlib.pyplot as plt\n'), ((2184, 2241), 'matplotlib.pyplot.plot', 'plt.plot', (['s_64'], {'label': '"""Speedup"""', 'color': '"""blue"""', 'marker': '"""x"""'}), "(s_64, label='Speedup', color='blue', marker='x')\n", (2192, 2241), True, 'import matplotlib.pyplot as plt\n'), ((2242, 2282), 'matplotlib.pyplot.title', 'plt.title', (['"""Game of Life in 64×64 table"""'], {}), "('Game of Life in 64×64 table')\n", (2251, 2282), True, 'import matplotlib.pyplot as plt\n'), ((2283, 2333), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""speedup_64.png"""'], {'bbox_inches': '"""tight"""'}), "('speedup_64.png', bbox_inches='tight')\n", (2294, 2333), True, 'import matplotlib.pyplot as plt\n'), ((2345, 2359), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (2357, 2359), True, 'import matplotlib.pyplot as plt\n'), ((2553, 2612), 'matplotlib.pyplot.plot', 'plt.plot', (['s_1024'], {'label': '"""Speedup"""', 'color': '"""blue"""', 'marker': '"""x"""'}), "(s_1024, label='Speedup', color='blue', marker='x')\n", (2561, 2612), True, 'import matplotlib.pyplot as plt\n'), ((2613, 2654), 'matplotlib.pyplot.title', 'plt.title', (['"""Game of Life 1024×1024 table"""'], {}), "('Game of Life 1024×1024 table')\n", (2622, 2654), True, 'import matplotlib.pyplot as plt\n'), ((2655, 2707), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""speedup_1024.png"""'], {'bbox_inches': '"""tight"""'}), "('speedup_1024.png', bbox_inches='tight')\n", (2666, 2707), True, 'import matplotlib.pyplot as plt\n'), ((2719, 2733), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (2731, 2733), True, 'import matplotlib.pyplot as plt\n'), ((2927, 2986), 'matplotlib.pyplot.plot', 'plt.plot', (['s_4096'], {'label': '"""Speedup"""', 'color': '"""blue"""', 'marker': '"""x"""'}), "(s_4096, label='Speedup', color='blue', marker='x')\n", (2935, 2986), True, 'import matplotlib.pyplot as plt\n'), ((2987, 3028), 'matplotlib.pyplot.title', 'plt.title', (['"""Game of Life 4096×4096 table"""'], {}), "('Game of Life 4096×4096 table')\n", (2996, 3028), True, 'import matplotlib.pyplot as plt\n'), ((3029, 3081), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""speedup_4096.png"""'], {'bbox_inches': '"""tight"""'}), "('speedup_4096.png', bbox_inches='tight')\n", (3040, 3081), True, 'import matplotlib.pyplot as plt\n'), ((954, 972), 'numpy.arange', 'np.arange', (['(0)', '(5)', '(1)'], {}), '(0, 5, 1)\n', (963, 972), True, 'import numpy as np\n'), ((1319, 1337), 'numpy.arange', 'np.arange', (['(0)', '(5)', '(1)'], {}), '(0, 5, 1)\n', (1328, 1337), True, 'import numpy as np\n'), ((1688, 1706), 'numpy.arange', 'np.arange', (['(0)', '(5)', '(1)'], {}), '(0, 5, 1)\n', (1697, 1706), True, 'import numpy as np\n'), ((2057, 2075), 'numpy.arange', 'np.arange', (['(0)', '(5)', '(1)'], {}), '(0, 5, 1)\n', (2066, 2075), True, 'import numpy as np\n'), ((2426, 2444), 'numpy.arange', 'np.arange', (['(0)', '(5)', '(1)'], {}), '(0, 5, 1)\n', (2435, 2444), True, 'import numpy as np\n'), ((2800, 2818), 'numpy.arange', 'np.arange', (['(0)', '(5)', '(1)'], {}), '(0, 5, 1)\n', (2809, 2818), True, 'import numpy as np\n')]
# -*- coding: utf-8 -*- import numpy as np def kalman_transit_covariance(S, A, R): """ :param S: Current covariance matrix :param A: Either transition matrix or jacobian matrix :param R: Current noise covariance matrix """ state_size = S.shape[0] assert S.shape == (state_size, state_size) assert A.shape == (state_size, state_size) assert R.shape == (state_size, state_size) new_S = np.dot(np.dot(A, S), A.T) + R return new_S def kalman_process_observation(mu, S, observation, C, Q): """ Performs processing of an observation coming from the model: z = C * x + noise :param mu: Current mean :param S: Current covariance matrix :param observation: Vector z :param C: Observation matrix :param Q: Noise covariance matrix (with zero mean) """ state_size = mu.shape[0] observation_size = observation.shape[0] assert S.shape == (state_size, state_size) assert observation_size == C.shape[0] assert observation_size == Q.shape[0] H = np.linalg.inv(np.dot(np.dot(C, S), C.T) + Q) K = np.dot(np.dot(S, C.T), H) new_mu = mu + np.dot(K, observation - np.dot(C, mu)) new_S = np.dot(np.eye(state_size) - np.dot(K, C), S) # Избавляемся от маленьких чисел. Из-за них могут быть мнимые числа в собственных значениях new_S[np.abs(new_S) < 1e-16] = 0 return new_mu, new_S
[ "numpy.dot", "numpy.abs", "numpy.eye" ]
[((1093, 1107), 'numpy.dot', 'np.dot', (['S', 'C.T'], {}), '(S, C.T)\n', (1099, 1107), True, 'import numpy as np\n'), ((433, 445), 'numpy.dot', 'np.dot', (['A', 'S'], {}), '(A, S)\n', (439, 445), True, 'import numpy as np\n'), ((1188, 1206), 'numpy.eye', 'np.eye', (['state_size'], {}), '(state_size)\n', (1194, 1206), True, 'import numpy as np\n'), ((1209, 1221), 'numpy.dot', 'np.dot', (['K', 'C'], {}), '(K, C)\n', (1215, 1221), True, 'import numpy as np\n'), ((1332, 1345), 'numpy.abs', 'np.abs', (['new_S'], {}), '(new_S)\n', (1338, 1345), True, 'import numpy as np\n'), ((1054, 1066), 'numpy.dot', 'np.dot', (['C', 'S'], {}), '(C, S)\n', (1060, 1066), True, 'import numpy as np\n'), ((1154, 1167), 'numpy.dot', 'np.dot', (['C', 'mu'], {}), '(C, mu)\n', (1160, 1167), True, 'import numpy as np\n')]
# Author: <NAME> import h5py import json import librosa import numpy as np import os import scipy import time from pathlib import Path from PIL import Image from torchvision.transforms import transforms from dataloaders.utils import WINDOWS, compute_spectrogram def run(json_path, hdf5_json_path, audio_path, image_path, audio_conf={}): with open(json_path, 'r') as f: data_and_dirs = json.load(f) data = data_and_dirs['data'] audio_base = data_and_dirs['audio_base_path'] image_base = data_and_dirs['image_base_path'] print('Loaded %d data from %s' % (len(data), json_path)) run_audio(data, audio_base, audio_path, audio_conf) run_image(data, image_base, image_path) Path(os.path.dirname(hdf5_json_path)).mkdir(parents=True, exist_ok=True) with open(hdf5_json_path, 'w') as f: d = {'audio_hdf5_path': audio_path, 'image_hdf5_path': image_path} json.dump(d, f) # Solution borrows from https://github.com/h5py/h5py/issues/745 def run_image(data, image_base, image_path): if os.path.exists(image_path): print('%s already exists. skip' % image_path) return print('Dumping image to HDF5 : %s' % image_path) n = len(data) Path(os.path.dirname(image_path)).mkdir(parents=True, exist_ok=True) f = h5py.File(image_path, 'w') dt = h5py.special_dtype(vlen=np.dtype('uint8')) dset_img = f.create_dataset('image', (n,), dtype=dt) start = time.time() for i, d in enumerate(data): with open('%s/%s' % (image_base, d['image']), 'rb') as f_img: binary_img = f_img.read() dset_img[i] = np.frombuffer(binary_img, dtype='uint8') if i % 100 == 0: t = time.time() - start print('processed %d / %d images (%.fs)' % (i, n, t)) def run_audio(data, audio_base, audio_path, audio_conf): if os.path.exists(audio_path): print('%s already exists. skip' % audio_path) return print('Dumping audio to HDF5 : %s' % audio_path) print(' audio_conf : %s' % audio_conf) audio_conf['num_mel_bins'] = audio_conf.get('num_mel_bins', 40) audio_conf['target_length'] = audio_conf.get('target_length', 2048) audio_conf['use_raw_length'] = audio_conf.get('use_raw_length', False) assert(not audio_conf['use_raw_length']) # dump audio n = len(data) Path(os.path.dirname(audio_path)).mkdir(parents=True, exist_ok=True) f = h5py.File(audio_path, 'w') dset_mel_shape = (n, audio_conf['num_mel_bins'], audio_conf['target_length']) dset_mel = f.create_dataset('melspec', dset_mel_shape, dtype='f') dset_len = f.create_dataset('melspec_len', (n,), dtype='i8') start = time.time() for i, d in enumerate(data): y, sr = librosa.load('%s/%s' % (audio_base, d['wav']), None) logspec, n_frames = compute_spectrogram(y, sr, audio_conf) dset_mel[i, :, :] = logspec dset_len[i] = n_frames if i % 100 == 0: t = time.time() - start print('processed %d / %d audios (%.fs)' % (i, n, t)) if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('inp_json_path', type=str, help='input JSON file') parser.add_argument('out_json_path', type=str, help='path to save output json') parser.add_argument('audio_h5_path', type=str, help='path to save audio HDF5') parser.add_argument('image_h5_path', type=str, help='path to save image HDF5') args = parser.parse_args() print(args) run(args.inp_json_path, args.out_json_path, args.audio_h5_path, args.image_h5_path)
[ "json.dump", "h5py.File", "json.load", "argparse.ArgumentParser", "numpy.frombuffer", "os.path.dirname", "numpy.dtype", "os.path.exists", "time.time", "dataloaders.utils.compute_spectrogram", "librosa.load" ]
[((1060, 1086), 'os.path.exists', 'os.path.exists', (['image_path'], {}), '(image_path)\n', (1074, 1086), False, 'import os\n'), ((1310, 1336), 'h5py.File', 'h5py.File', (['image_path', '"""w"""'], {}), "(image_path, 'w')\n", (1319, 1336), False, 'import h5py\n'), ((1463, 1474), 'time.time', 'time.time', ([], {}), '()\n', (1472, 1474), False, 'import time\n'), ((1872, 1898), 'os.path.exists', 'os.path.exists', (['audio_path'], {}), '(audio_path)\n', (1886, 1898), False, 'import os\n'), ((2448, 2474), 'h5py.File', 'h5py.File', (['audio_path', '"""w"""'], {}), "(audio_path, 'w')\n", (2457, 2474), False, 'import h5py\n'), ((2727, 2738), 'time.time', 'time.time', ([], {}), '()\n', (2736, 2738), False, 'import time\n'), ((3163, 3188), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (3186, 3188), False, 'import argparse\n'), ((400, 412), 'json.load', 'json.load', (['f'], {}), '(f)\n', (409, 412), False, 'import json\n'), ((926, 941), 'json.dump', 'json.dump', (['d', 'f'], {}), '(d, f)\n', (935, 941), False, 'import json\n'), ((1638, 1678), 'numpy.frombuffer', 'np.frombuffer', (['binary_img'], {'dtype': '"""uint8"""'}), "(binary_img, dtype='uint8')\n", (1651, 1678), True, 'import numpy as np\n'), ((2788, 2840), 'librosa.load', 'librosa.load', (["('%s/%s' % (audio_base, d['wav']))", 'None'], {}), "('%s/%s' % (audio_base, d['wav']), None)\n", (2800, 2840), False, 'import librosa\n'), ((2869, 2907), 'dataloaders.utils.compute_spectrogram', 'compute_spectrogram', (['y', 'sr', 'audio_conf'], {}), '(y, sr, audio_conf)\n', (2888, 2907), False, 'from dataloaders.utils import WINDOWS, compute_spectrogram\n'), ((1370, 1387), 'numpy.dtype', 'np.dtype', (['"""uint8"""'], {}), "('uint8')\n", (1378, 1387), True, 'import numpy as np\n'), ((734, 765), 'os.path.dirname', 'os.path.dirname', (['hdf5_json_path'], {}), '(hdf5_json_path)\n', (749, 765), False, 'import os\n'), ((1238, 1265), 'os.path.dirname', 'os.path.dirname', (['image_path'], {}), '(image_path)\n', (1253, 1265), False, 'import os\n'), ((1721, 1732), 'time.time', 'time.time', ([], {}), '()\n', (1730, 1732), False, 'import time\n'), ((2376, 2403), 'os.path.dirname', 'os.path.dirname', (['audio_path'], {}), '(audio_path)\n', (2391, 2403), False, 'import os\n'), ((3017, 3028), 'time.time', 'time.time', ([], {}), '()\n', (3026, 3028), False, 'import time\n')]
from __future__ import print_function import pandas as pd import numpy as np import os from collections import OrderedDict from pria_lifechem.function import * from prospective_screening_model_names import * from prospective_screening_metric_names import * def clean_excel(): dataframe = pd.read_excel('../../output/stage_2_predictions/Keck_LC4_backup.xlsx') dataframe = dataframe.drop(dataframe.index[[8779]]) dataframe.to_excel('../../output/stage_2_predictions/Keck_LC4_export.xlsx', index=None) def merge_prediction(): dataframe = pd.read_csv('../../dataset/fixed_dataset/pria_prospective.csv.gz') molecule_ids = dataframe['Molecule'].tolist() actual_labels = dataframe['Keck_Pria_AS_Retest'].tolist() inhibits = dataframe['Keck_Pria_Continuous'].tolist() complete_df = pd.DataFrame({'molecule': molecule_ids, 'label': actual_labels, 'inhibition': inhibits}) column_names = ['molecule', 'label', 'inhibition'] complete_df = complete_df[column_names] dir_ = '../../output/stage_2_predictions/Keck_Pria_AS_Retest' model_names = [] for model_name in model_name_mapping.keys(): file_path = '{}/{}.npz'.format(dir_, model_name) if not os.path.exists(file_path): continue print('model: {} exists'.format(model_name)) data = np.load(file_path) file_path, '\t', data.keys() y_pred = data['y_pred_on_test'][:, 0] if y_pred.ndim == 2: y_pred = y_pred[:, 0] model_name = model_name_mapping[model_name] model_names.append(model_name) complete_df[model_name] = y_pred print() model_names = sorted(model_names) column_names.extend(model_names) complete_df = complete_df[column_names] print(complete_df.shape) complete_df.to_csv('{}/complete_prediction.csv'.format(dir_), index=None) return def merge_prediction_old(): dataframe = pd.read_excel('../../output/stage_2_predictions/Keck_LC4_export.xlsx') molecule_name_list = dataframe['Molecule Name'].tolist() supplier_id = dataframe['Supplier ID'].tolist() failed_id = ['F0401-0050', 'F2964-1411', 'F2964-1523'] inhibits = dataframe[ 'PriA-SSB AS, normalized for plate and edge effects, correct plate map: % inhibition Alpha, normalized (%)'].tolist() neo_dataframe = pd.read_csv('../../output/stage_2_predictions/pria_lc4_retest_may18.csv') failed_molecule_names = neo_dataframe[neo_dataframe['Active'] == 0]['Row Labels'].tolist() failed_molecule_names += ['SMSSF-0044356', 'SMSSF-0030688'] positive_enumerate = filter(lambda x: x[1] >= 35 and supplier_id[x[0]] not in failed_id and molecule_name_list[x[0]] not in failed_molecule_names, enumerate(inhibits)) positive_idx = map(lambda x: x[0], positive_enumerate) actual_label = map(lambda x: 1 if x in positive_idx else 0, range(len(supplier_id))) actual_label = np.array(actual_label) complete_df = pd.DataFrame({'molecule name': molecule_name_list, 'molecule id': supplier_id, 'label': actual_label, 'inhibition': inhibits}) column_names = ['molecule name', 'molecule id', 'label', 'inhibition'] complete_df = complete_df[column_names] test_data_df = pd.read_csv('../../dataset/keck_lc4.csv.gz') test_data_df = test_data_df[['Molecule', 'SMILES', 'Fingerprints']] complete_df = complete_df.merge(test_data_df, how='left', left_on='molecule id', right_on='Molecule', sort=False) complete_df.to_csv('LC4_complete.csv', index=None) dir_ = '../../output/stage_2_predictions/Keck_Pria_AS_Retest' file_path = '{}/{}.npz'.format(dir_, 'vanilla_lstm_19') data = np.load(file_path) molecule_id = data['molecule_id'] model_names = [] special_models = ['irv', 'random_forest', 'dockscore', 'consensus', 'baseline'] for model_name in model_name_mapping.keys(): file_path = '{}/{}.npz'.format(dir_, model_name) if not os.path.exists(file_path): continue print('model: {} exists'.format(model_name)) data = np.load(file_path) if any(x in model_name for x in special_models): y_pred = data['y_pred_on_test'] else: y_pred = data['y_pred'] if y_pred.ndim == 2: y_pred = y_pred[:, 0] temp_df = pd.DataFrame({'molecule id': molecule_id, model_name_mapping[model_name]: y_pred}) model_names.append(model_name_mapping[model_name]) complete_df = complete_df.join(temp_df.set_index('molecule id'), on='molecule id') print() model_names = sorted(model_names) column_names.extend(model_names) complete_df = complete_df[column_names] print(complete_df.shape) complete_df.to_csv('{}/complete_prediction.csv'.format(dir_), index=None) def merge_rank(): dir_ = '../../output/stage_2_predictions/Keck_Pria_AS_Retest' complete_df = pd.read_csv('{}/complete_prediction.csv'.format(dir_)) model_names = complete_df.columns[3:] rank_df = complete_df[['molecule', 'label', 'inhibition']] for (idx, model_name) in enumerate(model_names): order = complete_df[model_name].rank(ascending=False, method='max').tolist() order = np.array(order) order = order.astype(np.int) rank_df[model_name] = order ensemble_model_names_pairs = OrderedDict() for ensemble_name, ensemble_model_names in ensemble_model_names_pairs.items(): ensemble_orders = [] for (idx, model_name) in enumerate(model_names): order = complete_df[model_name].rank(ascending=False, method='max').tolist() order = np.array(order) order = order.astype(np.int) if model_name in ensemble_model_names: ensemble_orders.append(order) ensemble_orders = np.vstack(ensemble_orders) ensemble_order = np.zeros((ensemble_orders.shape[1])) for i in range(ensemble_orders.shape[1]): ensemble_order[i] = np.min(ensemble_orders[:, i]) ensemble_order = ensemble_order.astype(int) temp_df = pd.DataFrame() temp_df[ensemble_name] = ensemble_order # Rank the simple ensemble order = temp_df[ensemble_name].rank(method='max').as_matrix() order = np.array(order) order = order.astype(int) rank_df[ensemble_name] = order rank_df.to_csv('{}/complete_rank.csv'.format(dir_), index=None) def merge_evaluation(): dir_ = '../../output/stage_2_predictions/Keck_Pria_AS_Retest' complete_df = pd.read_csv('{}/complete_prediction.csv'.format(dir_)) model_names = complete_df.columns[3:] metric_df = pd.DataFrame({'Model': model_names}) actual_oracle = complete_df['label'].as_matrix() actual_oracle = reshape_data_into_2_dim(actual_oracle) for (metric_name, metric_) in metric_name_mapping.iteritems(): metric_values = [] for model_name in model_names: pred = complete_df[model_name].as_matrix() pred = reshape_data_into_2_dim(pred) actual, pred = collectively_drop_nan(actual_oracle, pred) value = metric_['function'](actual, pred, **metric_['argument']) metric_values.append(value) print(metric_name, '\t', model_name, '\t', value) metric_df[metric_name] = metric_values print() print('saving to {}/complete_evaluation.csv'.format(dir_)) metric_df.to_csv('{}/complete_evaluation.csv'.format(dir_), index=None) def filter_model_name(model_name): model_name = model_name.replace('SingleClassification', 'STC') model_name = model_name.replace('SingleRegression', 'STR') model_name = model_name.replace('MultiClassification', 'MTC') model_name = model_name.replace('RandomForest', 'RF') model_name = model_name.replace('ConsensusDocking', 'ConDock') model_name = model_name.replace('Docking', 'Dock') return model_name if __name__ == '__main__': # clean_excel() merge_prediction() merge_rank() merge_evaluation()
[ "pandas.DataFrame", "numpy.load", "pandas.read_csv", "numpy.zeros", "os.path.exists", "pandas.read_excel", "numpy.min", "numpy.array", "collections.OrderedDict", "numpy.vstack" ]
[((295, 365), 'pandas.read_excel', 'pd.read_excel', (['"""../../output/stage_2_predictions/Keck_LC4_backup.xlsx"""'], {}), "('../../output/stage_2_predictions/Keck_LC4_backup.xlsx')\n", (308, 365), True, 'import pandas as pd\n'), ((556, 622), 'pandas.read_csv', 'pd.read_csv', (['"""../../dataset/fixed_dataset/pria_prospective.csv.gz"""'], {}), "('../../dataset/fixed_dataset/pria_prospective.csv.gz')\n", (567, 622), True, 'import pandas as pd\n'), ((812, 904), 'pandas.DataFrame', 'pd.DataFrame', (["{'molecule': molecule_ids, 'label': actual_labels, 'inhibition': inhibits}"], {}), "({'molecule': molecule_ids, 'label': actual_labels,\n 'inhibition': inhibits})\n", (824, 904), True, 'import pandas as pd\n'), ((1929, 1999), 'pandas.read_excel', 'pd.read_excel', (['"""../../output/stage_2_predictions/Keck_LC4_export.xlsx"""'], {}), "('../../output/stage_2_predictions/Keck_LC4_export.xlsx')\n", (1942, 1999), True, 'import pandas as pd\n'), ((2345, 2418), 'pandas.read_csv', 'pd.read_csv', (['"""../../output/stage_2_predictions/pria_lc4_retest_may18.csv"""'], {}), "('../../output/stage_2_predictions/pria_lc4_retest_may18.csv')\n", (2356, 2418), True, 'import pandas as pd\n'), ((2918, 2940), 'numpy.array', 'np.array', (['actual_label'], {}), '(actual_label)\n', (2926, 2940), True, 'import numpy as np\n'), ((2960, 3090), 'pandas.DataFrame', 'pd.DataFrame', (["{'molecule name': molecule_name_list, 'molecule id': supplier_id, 'label':\n actual_label, 'inhibition': inhibits}"], {}), "({'molecule name': molecule_name_list, 'molecule id':\n supplier_id, 'label': actual_label, 'inhibition': inhibits})\n", (2972, 3090), True, 'import pandas as pd\n'), ((3227, 3271), 'pandas.read_csv', 'pd.read_csv', (['"""../../dataset/keck_lc4.csv.gz"""'], {}), "('../../dataset/keck_lc4.csv.gz')\n", (3238, 3271), True, 'import pandas as pd\n'), ((3655, 3673), 'numpy.load', 'np.load', (['file_path'], {}), '(file_path)\n', (3662, 3673), True, 'import numpy as np\n'), ((5361, 5374), 'collections.OrderedDict', 'OrderedDict', ([], {}), '()\n', (5372, 5374), False, 'from collections import OrderedDict\n'), ((6673, 6709), 'pandas.DataFrame', 'pd.DataFrame', (["{'Model': model_names}"], {}), "({'Model': model_names})\n", (6685, 6709), True, 'import pandas as pd\n'), ((1327, 1345), 'numpy.load', 'np.load', (['file_path'], {}), '(file_path)\n', (1334, 1345), True, 'import numpy as np\n'), ((4056, 4074), 'numpy.load', 'np.load', (['file_path'], {}), '(file_path)\n', (4063, 4074), True, 'import numpy as np\n'), ((4309, 4395), 'pandas.DataFrame', 'pd.DataFrame', (["{'molecule id': molecule_id, model_name_mapping[model_name]: y_pred}"], {}), "({'molecule id': molecule_id, model_name_mapping[model_name]:\n y_pred})\n", (4321, 4395), True, 'import pandas as pd\n'), ((5238, 5253), 'numpy.array', 'np.array', (['order'], {}), '(order)\n', (5246, 5253), True, 'import numpy as np\n'), ((5834, 5860), 'numpy.vstack', 'np.vstack', (['ensemble_orders'], {}), '(ensemble_orders)\n', (5843, 5860), True, 'import numpy as np\n'), ((5886, 5920), 'numpy.zeros', 'np.zeros', (['ensemble_orders.shape[1]'], {}), '(ensemble_orders.shape[1])\n', (5894, 5920), True, 'import numpy as np\n'), ((6106, 6120), 'pandas.DataFrame', 'pd.DataFrame', ([], {}), '()\n', (6118, 6120), True, 'import pandas as pd\n'), ((6291, 6306), 'numpy.array', 'np.array', (['order'], {}), '(order)\n', (6299, 6306), True, 'import numpy as np\n'), ((1211, 1236), 'os.path.exists', 'os.path.exists', (['file_path'], {}), '(file_path)\n', (1225, 1236), False, 'import os\n'), ((3940, 3965), 'os.path.exists', 'os.path.exists', (['file_path'], {}), '(file_path)\n', (3954, 3965), False, 'import os\n'), ((5654, 5669), 'numpy.array', 'np.array', (['order'], {}), '(order)\n', (5662, 5669), True, 'import numpy as np\n'), ((6005, 6034), 'numpy.min', 'np.min', (['ensemble_orders[:, i]'], {}), '(ensemble_orders[:, i])\n', (6011, 6034), True, 'import numpy as np\n')]
# coding=utf-8 # pylint: disable-msg=E1101,W0612 from datetime import datetime, timedelta import operator from itertools import product, starmap from numpy import nan, inf import numpy as np import pandas as pd from pandas import (Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8) from pandas.tseries.index import Timestamp from pandas.tseries.tdi import Timedelta import pandas.core.nanops as nanops from pandas.compat import range, zip from pandas import compat from pandas.util.testing import assert_series_equal, assert_almost_equal import pandas.util.testing as tm from .common import TestData class TestSeriesOperators(TestData, tm.TestCase): _multiprocess_can_split_ = True def test_comparisons(self): left = np.random.randn(10) right = np.random.randn(10) left[:3] = np.nan result = nanops.nangt(left, right) with np.errstate(invalid='ignore'): expected = (left > right).astype('O') expected[:3] = np.nan assert_almost_equal(result, expected) s = Series(['a', 'b', 'c']) s2 = Series([False, True, False]) # it works! exp = Series([False, False, False]) tm.assert_series_equal(s == s2, exp) tm.assert_series_equal(s2 == s, exp) def test_op_method(self): def check(series, other, check_reverse=False): simple_ops = ['add', 'sub', 'mul', 'floordiv', 'truediv', 'pow'] if not compat.PY3: simple_ops.append('div') for opname in simple_ops: op = getattr(Series, opname) if op == 'div': alt = operator.truediv else: alt = getattr(operator, opname) result = op(series, other) expected = alt(series, other) tm.assert_almost_equal(result, expected) if check_reverse: rop = getattr(Series, "r" + opname) result = rop(series, other) expected = alt(other, series) tm.assert_almost_equal(result, expected) check(self.ts, self.ts * 2) check(self.ts, self.ts[::2]) check(self.ts, 5, check_reverse=True) check(tm.makeFloatSeries(), tm.makeFloatSeries(), check_reverse=True) def test_neg(self): assert_series_equal(-self.series, -1 * self.series) def test_invert(self): assert_series_equal(-(self.series < 0), ~(self.series < 0)) def test_div(self): with np.errstate(all='ignore'): # no longer do integer div for any ops, but deal with the 0's p = DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) result = p['first'] / p['second'] expected = Series( p['first'].values.astype(float) / p['second'].values, dtype='float64') expected.iloc[0:3] = np.inf assert_series_equal(result, expected) result = p['first'] / 0 expected = Series(np.inf, index=p.index, name='first') assert_series_equal(result, expected) p = p.astype('float64') result = p['first'] / p['second'] expected = Series(p['first'].values / p['second'].values) assert_series_equal(result, expected) p = DataFrame({'first': [3, 4, 5, 8], 'second': [1, 1, 1, 1]}) result = p['first'] / p['second'] assert_series_equal(result, p['first'].astype('float64'), check_names=False) self.assertTrue(result.name is None) self.assertFalse(np.array_equal(result, p['second'] / p['first'])) # inf signing s = Series([np.nan, 1., -1.]) result = s / 0 expected = Series([np.nan, np.inf, -np.inf]) assert_series_equal(result, expected) # float/integer issue # GH 7785 p = DataFrame({'first': (1, 0), 'second': (-0.01, -0.02)}) expected = Series([-0.01, -np.inf]) result = p['second'].div(p['first']) assert_series_equal(result, expected, check_names=False) result = p['second'] / p['first'] assert_series_equal(result, expected) # GH 9144 s = Series([-1, 0, 1]) result = 0 / s expected = Series([0.0, nan, 0.0]) assert_series_equal(result, expected) result = s / 0 expected = Series([-inf, nan, inf]) assert_series_equal(result, expected) result = s // 0 expected = Series([-inf, nan, inf]) assert_series_equal(result, expected) def test_operators(self): def _check_op(series, other, op, pos_only=False, check_dtype=True): left = np.abs(series) if pos_only else series right = np.abs(other) if pos_only else other cython_or_numpy = op(left, right) python = left.combine(right, op) tm.assert_series_equal(cython_or_numpy, python, check_dtype=check_dtype) def check(series, other): simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'mod'] for opname in simple_ops: _check_op(series, other, getattr(operator, opname)) _check_op(series, other, operator.pow, pos_only=True) _check_op(series, other, lambda x, y: operator.add(y, x)) _check_op(series, other, lambda x, y: operator.sub(y, x)) _check_op(series, other, lambda x, y: operator.truediv(y, x)) _check_op(series, other, lambda x, y: operator.floordiv(y, x)) _check_op(series, other, lambda x, y: operator.mul(y, x)) _check_op(series, other, lambda x, y: operator.pow(y, x), pos_only=True) _check_op(series, other, lambda x, y: operator.mod(y, x)) check(self.ts, self.ts * 2) check(self.ts, self.ts * 0) check(self.ts, self.ts[::2]) check(self.ts, 5) def check_comparators(series, other, check_dtype=True): _check_op(series, other, operator.gt, check_dtype=check_dtype) _check_op(series, other, operator.ge, check_dtype=check_dtype) _check_op(series, other, operator.eq, check_dtype=check_dtype) _check_op(series, other, operator.lt, check_dtype=check_dtype) _check_op(series, other, operator.le, check_dtype=check_dtype) check_comparators(self.ts, 5) check_comparators(self.ts, self.ts + 1, check_dtype=False) def test_operators_empty_int_corner(self): s1 = Series([], [], dtype=np.int32) s2 = Series({'x': 0.}) tm.assert_series_equal(s1 * s2, Series([np.nan], index=['x'])) def test_operators_timedelta64(self): # invalid ops self.assertRaises(Exception, self.objSeries.__add__, 1) self.assertRaises(Exception, self.objSeries.__add__, np.array(1, dtype=np.int64)) self.assertRaises(Exception, self.objSeries.__sub__, 1) self.assertRaises(Exception, self.objSeries.__sub__, np.array(1, dtype=np.int64)) # seriese ops v1 = date_range('2012-1-1', periods=3, freq='D') v2 = date_range('2012-1-2', periods=3, freq='D') rs = Series(v2) - Series(v1) xp = Series(1e9 * 3600 * 24, rs.index).astype('int64').astype('timedelta64[ns]') assert_series_equal(rs, xp) self.assertEqual(rs.dtype, 'timedelta64[ns]') df = DataFrame(dict(A=v1)) td = Series([timedelta(days=i) for i in range(3)]) self.assertEqual(td.dtype, 'timedelta64[ns]') # series on the rhs result = df['A'] - df['A'].shift() self.assertEqual(result.dtype, 'timedelta64[ns]') result = df['A'] + td self.assertEqual(result.dtype, 'M8[ns]') # scalar Timestamp on rhs maxa = df['A'].max() tm.assertIsInstance(maxa, Timestamp) resultb = df['A'] - df['A'].max() self.assertEqual(resultb.dtype, 'timedelta64[ns]') # timestamp on lhs result = resultb + df['A'] values = [Timestamp('20111230'), Timestamp('20120101'), Timestamp('20120103')] expected = Series(values, name='A') assert_series_equal(result, expected) # datetimes on rhs result = df['A'] - datetime(2001, 1, 1) expected = Series( [timedelta(days=4017 + i) for i in range(3)], name='A') assert_series_equal(result, expected) self.assertEqual(result.dtype, 'm8[ns]') d = datetime(2001, 1, 1, 3, 4) resulta = df['A'] - d self.assertEqual(resulta.dtype, 'm8[ns]') # roundtrip resultb = resulta + d assert_series_equal(df['A'], resultb) # timedeltas on rhs td = timedelta(days=1) resulta = df['A'] + td resultb = resulta - td assert_series_equal(resultb, df['A']) self.assertEqual(resultb.dtype, 'M8[ns]') # roundtrip td = timedelta(minutes=5, seconds=3) resulta = df['A'] + td resultb = resulta - td assert_series_equal(df['A'], resultb) self.assertEqual(resultb.dtype, 'M8[ns]') # inplace value = rs[2] + np.timedelta64(timedelta(minutes=5, seconds=1)) rs[2] += np.timedelta64(timedelta(minutes=5, seconds=1)) self.assertEqual(rs[2], value) def test_operator_series_comparison_zerorank(self): # GH 13006 result = np.float64(0) > pd.Series([1, 2, 3]) expected = 0.0 > pd.Series([1, 2, 3]) self.assert_series_equal(result, expected) result = pd.Series([1, 2, 3]) < np.float64(0) expected = pd.Series([1, 2, 3]) < 0.0 self.assert_series_equal(result, expected) result = np.array([0, 1, 2])[0] > pd.Series([0, 1, 2]) expected = 0.0 > pd.Series([1, 2, 3]) self.assert_series_equal(result, expected) def test_timedeltas_with_DateOffset(self): # GH 4532 # operate with pd.offsets s = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) result = s + pd.offsets.Second(5) result2 = pd.offsets.Second(5) + s expected = Series([Timestamp('20130101 9:01:05'), Timestamp( '20130101 9:02:05')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s - pd.offsets.Second(5) result2 = -pd.offsets.Second(5) + s expected = Series([Timestamp('20130101 9:00:55'), Timestamp( '20130101 9:01:55')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s + pd.offsets.Milli(5) result2 = pd.offsets.Milli(5) + s expected = Series([Timestamp('20130101 9:01:00.005'), Timestamp( '20130101 9:02:00.005')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s + pd.offsets.Minute(5) + pd.offsets.Milli(5) expected = Series([Timestamp('20130101 9:06:00.005'), Timestamp( '20130101 9:07:00.005')]) assert_series_equal(result, expected) # operate with np.timedelta64 correctly result = s + np.timedelta64(1, 's') result2 = np.timedelta64(1, 's') + s expected = Series([Timestamp('20130101 9:01:01'), Timestamp( '20130101 9:02:01')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) result = s + np.timedelta64(5, 'ms') result2 = np.timedelta64(5, 'ms') + s expected = Series([Timestamp('20130101 9:01:00.005'), Timestamp( '20130101 9:02:00.005')]) assert_series_equal(result, expected) assert_series_equal(result2, expected) # valid DateOffsets for do in ['Hour', 'Minute', 'Second', 'Day', 'Micro', 'Milli', 'Nano']: op = getattr(pd.offsets, do) s + op(5) op(5) + s def test_timedelta_series_ops(self): # GH11925 s = Series(timedelta_range('1 day', periods=3)) ts = Timestamp('2012-01-01') expected = Series(date_range('2012-01-02', periods=3)) assert_series_equal(ts + s, expected) assert_series_equal(s + ts, expected) expected2 = Series(date_range('2011-12-31', periods=3, freq='-1D')) assert_series_equal(ts - s, expected2) assert_series_equal(ts + (-s), expected2) def test_timedelta64_operations_with_DateOffset(self): # GH 10699 td = Series([timedelta(minutes=5, seconds=3)] * 3) result = td + pd.offsets.Minute(1) expected = Series([timedelta(minutes=6, seconds=3)] * 3) assert_series_equal(result, expected) result = td - pd.offsets.Minute(1) expected = Series([timedelta(minutes=4, seconds=3)] * 3) assert_series_equal(result, expected) result = td + Series([pd.offsets.Minute(1), pd.offsets.Second(3), pd.offsets.Hour(2)]) expected = Series([timedelta(minutes=6, seconds=3), timedelta( minutes=5, seconds=6), timedelta(hours=2, minutes=5, seconds=3)]) assert_series_equal(result, expected) result = td + pd.offsets.Minute(1) + pd.offsets.Second(12) expected = Series([timedelta(minutes=6, seconds=15)] * 3) assert_series_equal(result, expected) # valid DateOffsets for do in ['Hour', 'Minute', 'Second', 'Day', 'Micro', 'Milli', 'Nano']: op = getattr(pd.offsets, do) td + op(5) op(5) + td td - op(5) op(5) - td def test_timedelta64_operations_with_timedeltas(self): # td operate with td td1 = Series([timedelta(minutes=5, seconds=3)] * 3) td2 = timedelta(minutes=5, seconds=4) result = td1 - td2 expected = Series([timedelta(seconds=0)] * 3) - Series([timedelta( seconds=1)] * 3) self.assertEqual(result.dtype, 'm8[ns]') assert_series_equal(result, expected) result2 = td2 - td1 expected = (Series([timedelta(seconds=1)] * 3) - Series([timedelta( seconds=0)] * 3)) assert_series_equal(result2, expected) # roundtrip assert_series_equal(result + td2, td1) # Now again, using pd.to_timedelta, which should build # a Series or a scalar, depending on input. td1 = Series(pd.to_timedelta(['00:05:03'] * 3)) td2 = pd.to_timedelta('00:05:04') result = td1 - td2 expected = Series([timedelta(seconds=0)] * 3) - Series([timedelta( seconds=1)] * 3) self.assertEqual(result.dtype, 'm8[ns]') assert_series_equal(result, expected) result2 = td2 - td1 expected = (Series([timedelta(seconds=1)] * 3) - Series([timedelta( seconds=0)] * 3)) assert_series_equal(result2, expected) # roundtrip assert_series_equal(result + td2, td1) def test_timedelta64_operations_with_integers(self): # GH 4521 # divide/multiply by integers startdate = Series(date_range('2013-01-01', '2013-01-03')) enddate = Series(date_range('2013-03-01', '2013-03-03')) s1 = enddate - startdate s1[2] = np.nan s2 = Series([2, 3, 4]) expected = Series(s1.values.astype(np.int64) / s2, dtype='m8[ns]') expected[2] = np.nan result = s1 / s2 assert_series_equal(result, expected) s2 = Series([20, 30, 40]) expected = Series(s1.values.astype(np.int64) / s2, dtype='m8[ns]') expected[2] = np.nan result = s1 / s2 assert_series_equal(result, expected) result = s1 / 2 expected = Series(s1.values.astype(np.int64) / 2, dtype='m8[ns]') expected[2] = np.nan assert_series_equal(result, expected) s2 = Series([20, 30, 40]) expected = Series(s1.values.astype(np.int64) * s2, dtype='m8[ns]') expected[2] = np.nan result = s1 * s2 assert_series_equal(result, expected) for dtype in ['int32', 'int16', 'uint32', 'uint64', 'uint32', 'uint16', 'uint8']: s2 = Series([20, 30, 40], dtype=dtype) expected = Series( s1.values.astype(np.int64) * s2.astype(np.int64), dtype='m8[ns]') expected[2] = np.nan result = s1 * s2 assert_series_equal(result, expected) result = s1 * 2 expected = Series(s1.values.astype(np.int64) * 2, dtype='m8[ns]') expected[2] = np.nan assert_series_equal(result, expected) result = s1 * -1 expected = Series(s1.values.astype(np.int64) * -1, dtype='m8[ns]') expected[2] = np.nan assert_series_equal(result, expected) # invalid ops assert_series_equal(s1 / s2.astype(float), Series([Timedelta('2 days 22:48:00'), Timedelta( '1 days 23:12:00'), Timedelta('NaT')])) assert_series_equal(s1 / 2.0, Series([Timedelta('29 days 12:00:00'), Timedelta( '29 days 12:00:00'), Timedelta('NaT')])) for op in ['__add__', '__sub__']: sop = getattr(s1, op, None) if sop is not None: self.assertRaises(TypeError, sop, 1) self.assertRaises(TypeError, sop, s2.values) def test_timedelta64_conversions(self): startdate = Series(date_range('2013-01-01', '2013-01-03')) enddate = Series(date_range('2013-03-01', '2013-03-03')) s1 = enddate - startdate s1[2] = np.nan for m in [1, 3, 10]: for unit in ['D', 'h', 'm', 's', 'ms', 'us', 'ns']: # op expected = s1.apply(lambda x: x / np.timedelta64(m, unit)) result = s1 / np.timedelta64(m, unit) assert_series_equal(result, expected) if m == 1 and unit != 'ns': # astype result = s1.astype("timedelta64[{0}]".format(unit)) assert_series_equal(result, expected) # reverse op expected = s1.apply( lambda x: Timedelta(np.timedelta64(m, unit)) / x) result = np.timedelta64(m, unit) / s1 # astype s = Series(date_range('20130101', periods=3)) result = s.astype(object) self.assertIsInstance(result.iloc[0], datetime) self.assertTrue(result.dtype == np.object_) result = s1.astype(object) self.assertIsInstance(result.iloc[0], timedelta) self.assertTrue(result.dtype == np.object_) def test_timedelta64_equal_timedelta_supported_ops(self): ser = Series([Timestamp('20130301'), Timestamp('20130228 23:00:00'), Timestamp('20130228 22:00:00'), Timestamp( '20130228 21:00:00')]) intervals = 'D', 'h', 'm', 's', 'us' # TODO: unused # npy16_mappings = {'D': 24 * 60 * 60 * 1000000, # 'h': 60 * 60 * 1000000, # 'm': 60 * 1000000, # 's': 1000000, # 'us': 1} def timedelta64(*args): return sum(starmap(np.timedelta64, zip(args, intervals))) for op, d, h, m, s, us in product([operator.add, operator.sub], *([range(2)] * 5)): nptd = timedelta64(d, h, m, s, us) pytd = timedelta(days=d, hours=h, minutes=m, seconds=s, microseconds=us) lhs = op(ser, nptd) rhs = op(ser, pytd) try: assert_series_equal(lhs, rhs) except: raise AssertionError( "invalid comparsion [op->{0},d->{1},h->{2},m->{3}," "s->{4},us->{5}]\n{6}\n{7}\n".format(op, d, h, m, s, us, lhs, rhs)) def test_operators_datetimelike(self): def run_ops(ops, get_ser, test_ser): # check that we are getting a TypeError # with 'operate' (from core/ops.py) for the ops that are not # defined for op_str in ops: op = getattr(get_ser, op_str, None) with tm.assertRaisesRegexp(TypeError, 'operate'): op(test_ser) # ## timedelta64 ### td1 = Series([timedelta(minutes=5, seconds=3)] * 3) td1.iloc[2] = np.nan td2 = timedelta(minutes=5, seconds=4) ops = ['__mul__', '__floordiv__', '__pow__', '__rmul__', '__rfloordiv__', '__rpow__'] run_ops(ops, td1, td2) td1 + td2 td2 + td1 td1 - td2 td2 - td1 td1 / td2 td2 / td1 # ## datetime64 ### dt1 = Series([Timestamp('20111230'), Timestamp('20120101'), Timestamp('20120103')]) dt1.iloc[2] = np.nan dt2 = Series([Timestamp('20111231'), Timestamp('20120102'), Timestamp('20120104')]) ops = ['__add__', '__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__radd__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] run_ops(ops, dt1, dt2) dt1 - dt2 dt2 - dt1 # ## datetime64 with timetimedelta ### ops = ['__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] run_ops(ops, dt1, td1) dt1 + td1 td1 + dt1 dt1 - td1 # TODO: Decide if this ought to work. # td1 - dt1 # ## timetimedelta with datetime64 ### ops = ['__sub__', '__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] run_ops(ops, td1, dt1) td1 + dt1 dt1 + td1 # 8260, 10763 # datetime64 with tz ops = ['__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__', '__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__', '__rpow__'] tz = 'US/Eastern' dt1 = Series(date_range('2000-01-01 09:00:00', periods=5, tz=tz), name='foo') dt2 = dt1.copy() dt2.iloc[2] = np.nan td1 = Series(timedelta_range('1 days 1 min', periods=5, freq='H')) td2 = td1.copy() td2.iloc[1] = np.nan run_ops(ops, dt1, td1) result = dt1 + td1[0] exp = (dt1.dt.tz_localize(None) + td1[0]).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt2 + td2[0] exp = (dt2.dt.tz_localize(None) + td2[0]).dt.tz_localize(tz) assert_series_equal(result, exp) # odd numpy behavior with scalar timedeltas if not _np_version_under1p8: result = td1[0] + dt1 exp = (dt1.dt.tz_localize(None) + td1[0]).dt.tz_localize(tz) assert_series_equal(result, exp) result = td2[0] + dt2 exp = (dt2.dt.tz_localize(None) + td2[0]).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt1 - td1[0] exp = (dt1.dt.tz_localize(None) - td1[0]).dt.tz_localize(tz) assert_series_equal(result, exp) self.assertRaises(TypeError, lambda: td1[0] - dt1) result = dt2 - td2[0] exp = (dt2.dt.tz_localize(None) - td2[0]).dt.tz_localize(tz) assert_series_equal(result, exp) self.assertRaises(TypeError, lambda: td2[0] - dt2) result = dt1 + td1 exp = (dt1.dt.tz_localize(None) + td1).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt2 + td2 exp = (dt2.dt.tz_localize(None) + td2).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt1 - td1 exp = (dt1.dt.tz_localize(None) - td1).dt.tz_localize(tz) assert_series_equal(result, exp) result = dt2 - td2 exp = (dt2.dt.tz_localize(None) - td2).dt.tz_localize(tz) assert_series_equal(result, exp) self.assertRaises(TypeError, lambda: td1 - dt1) self.assertRaises(TypeError, lambda: td2 - dt2) def test_sub_single_tz(self): # GH12290 s1 = Series([pd.Timestamp('2016-02-10', tz='America/Sao_Paulo')]) s2 = Series([pd.Timestamp('2016-02-08', tz='America/Sao_Paulo')]) result = s1 - s2 expected = Series([Timedelta('2days')]) assert_series_equal(result, expected) result = s2 - s1 expected = Series([Timedelta('-2days')]) assert_series_equal(result, expected) def test_ops_nat(self): # GH 11349 timedelta_series = Series([NaT, Timedelta('1s')]) datetime_series = Series([NaT, Timestamp('19900315')]) nat_series_dtype_timedelta = Series( [NaT, NaT], dtype='timedelta64[ns]') nat_series_dtype_timestamp = Series([NaT, NaT], dtype='datetime64[ns]') single_nat_dtype_datetime = Series([NaT], dtype='datetime64[ns]') single_nat_dtype_timedelta = Series([NaT], dtype='timedelta64[ns]') # subtraction assert_series_equal(timedelta_series - NaT, nat_series_dtype_timedelta) assert_series_equal(-NaT + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(timedelta_series - single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(-single_nat_dtype_timedelta + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(datetime_series - NaT, nat_series_dtype_timestamp) assert_series_equal(-NaT + datetime_series, nat_series_dtype_timestamp) assert_series_equal(datetime_series - single_nat_dtype_datetime, nat_series_dtype_timedelta) with tm.assertRaises(TypeError): -single_nat_dtype_datetime + datetime_series assert_series_equal(datetime_series - single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(-single_nat_dtype_timedelta + datetime_series, nat_series_dtype_timestamp) # without a Series wrapping the NaT, it is ambiguous # whether it is a datetime64 or timedelta64 # defaults to interpreting it as timedelta64 assert_series_equal(nat_series_dtype_timestamp - NaT, nat_series_dtype_timestamp) assert_series_equal(-NaT + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timestamp - single_nat_dtype_datetime, nat_series_dtype_timedelta) with tm.assertRaises(TypeError): -single_nat_dtype_datetime + nat_series_dtype_timestamp assert_series_equal(nat_series_dtype_timestamp - single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(-single_nat_dtype_timedelta + nat_series_dtype_timestamp, nat_series_dtype_timestamp) with tm.assertRaises(TypeError): timedelta_series - single_nat_dtype_datetime # addition assert_series_equal(nat_series_dtype_timestamp + NaT, nat_series_dtype_timestamp) assert_series_equal(NaT + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timestamp + single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timedelta + NaT, nat_series_dtype_timedelta) assert_series_equal(NaT + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timedelta + single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(timedelta_series + NaT, nat_series_dtype_timedelta) assert_series_equal(NaT + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(timedelta_series + single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(single_nat_dtype_timedelta + timedelta_series, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timestamp + NaT, nat_series_dtype_timestamp) assert_series_equal(NaT + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timestamp + single_nat_dtype_timedelta, nat_series_dtype_timestamp) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timestamp, nat_series_dtype_timestamp) assert_series_equal(nat_series_dtype_timedelta + NaT, nat_series_dtype_timedelta) assert_series_equal(NaT + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timedelta + single_nat_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(single_nat_dtype_timedelta + nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(nat_series_dtype_timedelta + single_nat_dtype_datetime, nat_series_dtype_timestamp) assert_series_equal(single_nat_dtype_datetime + nat_series_dtype_timedelta, nat_series_dtype_timestamp) # multiplication assert_series_equal(nat_series_dtype_timedelta * 1.0, nat_series_dtype_timedelta) assert_series_equal(1.0 * nat_series_dtype_timedelta, nat_series_dtype_timedelta) assert_series_equal(timedelta_series * 1, timedelta_series) assert_series_equal(1 * timedelta_series, timedelta_series) assert_series_equal(timedelta_series * 1.5, Series([NaT, Timedelta('1.5s')])) assert_series_equal(1.5 * timedelta_series, Series([NaT, Timedelta('1.5s')])) assert_series_equal(timedelta_series * nan, nat_series_dtype_timedelta) assert_series_equal(nan * timedelta_series, nat_series_dtype_timedelta) with tm.assertRaises(TypeError): datetime_series * 1 with tm.assertRaises(TypeError): nat_series_dtype_timestamp * 1 with tm.assertRaises(TypeError): datetime_series * 1.0 with tm.assertRaises(TypeError): nat_series_dtype_timestamp * 1.0 # division assert_series_equal(timedelta_series / 2, Series([NaT, Timedelta('0.5s')])) assert_series_equal(timedelta_series / 2.0, Series([NaT, Timedelta('0.5s')])) assert_series_equal(timedelta_series / nan, nat_series_dtype_timedelta) with tm.assertRaises(TypeError): nat_series_dtype_timestamp / 1.0 with tm.assertRaises(TypeError): nat_series_dtype_timestamp / 1 def test_ops_datetimelike_align(self): # GH 7500 # datetimelike ops need to align dt = Series(date_range('2012-1-1', periods=3, freq='D')) dt.iloc[2] = np.nan dt2 = dt[::-1] expected = Series([timedelta(0), timedelta(0), pd.NaT]) # name is reset result = dt2 - dt assert_series_equal(result, expected) expected = Series(expected, name=0) result = (dt2.to_frame() - dt.to_frame())[0] assert_series_equal(result, expected) def test_object_comparisons(self): s = Series(['a', 'b', np.nan, 'c', 'a']) result = s == 'a' expected = Series([True, False, False, False, True]) assert_series_equal(result, expected) result = s < 'a' expected = Series([False, False, False, False, False]) assert_series_equal(result, expected) result = s != 'a' expected = -(s == 'a') assert_series_equal(result, expected) def test_comparison_tuples(self): # GH11339 # comparisons vs tuple s = Series([(1, 1), (1, 2)]) result = s == (1, 2) expected = Series([False, True]) assert_series_equal(result, expected) result = s != (1, 2) expected = Series([True, False]) assert_series_equal(result, expected) result = s == (0, 0) expected = Series([False, False]) assert_series_equal(result, expected) result = s != (0, 0) expected = Series([True, True]) assert_series_equal(result, expected) s = Series([(1, 1), (1, 1)]) result = s == (1, 1) expected = Series([True, True]) assert_series_equal(result, expected) result = s != (1, 1) expected = Series([False, False]) assert_series_equal(result, expected) s = Series([frozenset([1]), frozenset([1, 2])]) result = s == frozenset([1]) expected = Series([True, False]) assert_series_equal(result, expected) def test_comparison_operators_with_nas(self): s = Series(bdate_range('1/1/2000', periods=10), dtype=object) s[::2] = np.nan # test that comparisons work ops = ['lt', 'le', 'gt', 'ge', 'eq', 'ne'] for op in ops: val = s[5] f = getattr(operator, op) result = f(s, val) expected = f(s.dropna(), val).reindex(s.index) if op == 'ne': expected = expected.fillna(True).astype(bool) else: expected = expected.fillna(False).astype(bool) assert_series_equal(result, expected) # fffffffuuuuuuuuuuuu # result = f(val, s) # expected = f(val, s.dropna()).reindex(s.index) # assert_series_equal(result, expected) # boolean &, |, ^ should work with object arrays and propagate NAs ops = ['and_', 'or_', 'xor'] mask = s.isnull() for bool_op in ops: f = getattr(operator, bool_op) filled = s.fillna(s[0]) result = f(s < s[9], s > s[3]) expected = f(filled < filled[9], filled > filled[3]) expected[mask] = False assert_series_equal(result, expected) def test_comparison_object_numeric_nas(self): s = Series(np.random.randn(10), dtype=object) shifted = s.shift(2) ops = ['lt', 'le', 'gt', 'ge', 'eq', 'ne'] for op in ops: f = getattr(operator, op) result = f(s, shifted) expected = f(s.astype(float), shifted.astype(float)) assert_series_equal(result, expected) def test_comparison_invalid(self): # GH4968 # invalid date/int comparisons s = Series(range(5)) s2 = Series(date_range('20010101', periods=5)) for (x, y) in [(s, s2), (s2, s)]: self.assertRaises(TypeError, lambda: x == y) self.assertRaises(TypeError, lambda: x != y) self.assertRaises(TypeError, lambda: x >= y) self.assertRaises(TypeError, lambda: x > y) self.assertRaises(TypeError, lambda: x < y) self.assertRaises(TypeError, lambda: x <= y) def test_more_na_comparisons(self): for dtype in [None, object]: left = Series(['a', np.nan, 'c'], dtype=dtype) right = Series(['a', np.nan, 'd'], dtype=dtype) result = left == right expected = Series([True, False, False]) assert_series_equal(result, expected) result = left != right expected = Series([False, True, True]) assert_series_equal(result, expected) result = left == np.nan expected = Series([False, False, False]) assert_series_equal(result, expected) result = left != np.nan expected = Series([True, True, True]) assert_series_equal(result, expected) def test_nat_comparisons(self): data = [([pd.Timestamp('2011-01-01'), pd.NaT, pd.Timestamp('2011-01-03')], [pd.NaT, pd.NaT, pd.Timestamp('2011-01-03')]), ([pd.Timedelta('1 days'), pd.NaT, pd.Timedelta('3 days')], [pd.NaT, pd.NaT, pd.Timedelta('3 days')]), ([pd.Period('2011-01', freq='M'), pd.NaT, pd.Period('2011-03', freq='M')], [pd.NaT, pd.NaT, pd.Period('2011-03', freq='M')])] # add lhs / rhs switched data data = data + [(r, l) for l, r in data] for l, r in data: for dtype in [None, object]: left = Series(l, dtype=dtype) # Series, Index for right in [Series(r, dtype=dtype), Index(r, dtype=dtype)]: expected = Series([False, False, True]) assert_series_equal(left == right, expected) expected = Series([True, True, False]) assert_series_equal(left != right, expected) expected = Series([False, False, False]) assert_series_equal(left < right, expected) expected = Series([False, False, False]) assert_series_equal(left > right, expected) expected = Series([False, False, True]) assert_series_equal(left >= right, expected) expected = Series([False, False, True]) assert_series_equal(left <= right, expected) def test_nat_comparisons_scalar(self): data = [[pd.Timestamp('2011-01-01'), pd.NaT, pd.Timestamp('2011-01-03')], [pd.Timedelta('1 days'), pd.NaT, pd.Timedelta('3 days')], [pd.Period('2011-01', freq='M'), pd.NaT, pd.Period('2011-03', freq='M')]] for l in data: for dtype in [None, object]: left = Series(l, dtype=dtype) expected = Series([False, False, False]) assert_series_equal(left == pd.NaT, expected) assert_series_equal(pd.NaT == left, expected) expected = Series([True, True, True]) assert_series_equal(left != pd.NaT, expected) assert_series_equal(pd.NaT != left, expected) expected = Series([False, False, False]) assert_series_equal(left < pd.NaT, expected) assert_series_equal(pd.NaT > left, expected) assert_series_equal(left <= pd.NaT, expected) assert_series_equal(pd.NaT >= left, expected) assert_series_equal(left > pd.NaT, expected) assert_series_equal(pd.NaT < left, expected) assert_series_equal(left >= pd.NaT, expected) assert_series_equal(pd.NaT <= left, expected) def test_comparison_different_length(self): a = Series(['a', 'b', 'c']) b = Series(['b', 'a']) self.assertRaises(ValueError, a.__lt__, b) a = Series([1, 2]) b = Series([2, 3, 4]) self.assertRaises(ValueError, a.__eq__, b) def test_comparison_label_based(self): # GH 4947 # comparisons should be label based a = Series([True, False, True], list('bca')) b = Series([False, True, False], list('abc')) expected = Series([False, True, False], list('abc')) result = a & b assert_series_equal(result, expected) expected = Series([True, True, False], list('abc')) result = a | b assert_series_equal(result, expected) expected = Series([True, False, False], list('abc')) result = a ^ b assert_series_equal(result, expected) # rhs is bigger a = Series([True, False, True], list('bca')) b = Series([False, True, False, True], list('abcd')) expected = Series([False, True, False, False], list('abcd')) result = a & b assert_series_equal(result, expected) expected = Series([True, True, False, False], list('abcd')) result = a | b assert_series_equal(result, expected) # filling # vs empty result = a & Series([]) expected = Series([False, False, False], list('bca')) assert_series_equal(result, expected) result = a | Series([]) expected = Series([True, False, True], list('bca')) assert_series_equal(result, expected) # vs non-matching result = a & Series([1], ['z']) expected = Series([False, False, False, False], list('abcz')) assert_series_equal(result, expected) result = a | Series([1], ['z']) expected = Series([True, True, False, False], list('abcz')) assert_series_equal(result, expected) # identity # we would like s[s|e] == s to hold for any e, whether empty or not for e in [Series([]), Series([1], ['z']), Series(np.nan, b.index), Series(np.nan, a.index)]: result = a[a | e] assert_series_equal(result, a[a]) for e in [Series(['z'])]: if compat.PY3: with tm.assert_produces_warning(RuntimeWarning): result = a[a | e] else: result = a[a | e] assert_series_equal(result, a[a]) # vs scalars index = list('bca') t = Series([True, False, True]) for v in [True, 1, 2]: result = Series([True, False, True], index=index) | v expected = Series([True, True, True], index=index) assert_series_equal(result, expected) for v in [np.nan, 'foo']: self.assertRaises(TypeError, lambda: t | v) for v in [False, 0]: result = Series([True, False, True], index=index) | v expected = Series([True, False, True], index=index) assert_series_equal(result, expected) for v in [True, 1]: result = Series([True, False, True], index=index) & v expected = Series([True, False, True], index=index) assert_series_equal(result, expected) for v in [False, 0]: result = Series([True, False, True], index=index) & v expected = Series([False, False, False], index=index) assert_series_equal(result, expected) for v in [np.nan]: self.assertRaises(TypeError, lambda: t & v) def test_comparison_flex_basic(self): left = pd.Series(np.random.randn(10)) right = pd.Series(np.random.randn(10)) tm.assert_series_equal(left.eq(right), left == right) tm.assert_series_equal(left.ne(right), left != right) tm.assert_series_equal(left.le(right), left < right) tm.assert_series_equal(left.lt(right), left <= right) tm.assert_series_equal(left.gt(right), left > right) tm.assert_series_equal(left.ge(right), left >= right) # axis for axis in [0, None, 'index']: tm.assert_series_equal(left.eq(right, axis=axis), left == right) tm.assert_series_equal(left.ne(right, axis=axis), left != right) tm.assert_series_equal(left.le(right, axis=axis), left < right) tm.assert_series_equal(left.lt(right, axis=axis), left <= right) tm.assert_series_equal(left.gt(right, axis=axis), left > right) tm.assert_series_equal(left.ge(right, axis=axis), left >= right) # msg = 'No axis named 1 for object type' for op in ['eq', 'ne', 'le', 'le', 'gt', 'ge']: with tm.assertRaisesRegexp(ValueError, msg): getattr(left, op)(right, axis=1) def test_comparison_flex_alignment(self): left = Series([1, 3, 2], index=list('abc')) right = Series([2, 2, 2], index=list('bcd')) exp = pd.Series([False, False, True, False], index=list('abcd')) tm.assert_series_equal(left.eq(right), exp) exp = pd.Series([True, True, False, True], index=list('abcd')) tm.assert_series_equal(left.ne(right), exp) exp = pd.Series([False, False, True, False], index=list('abcd')) tm.assert_series_equal(left.le(right), exp) exp = pd.Series([False, False, False, False], index=list('abcd')) tm.assert_series_equal(left.lt(right), exp) exp = pd.Series([False, True, True, False], index=list('abcd')) tm.assert_series_equal(left.ge(right), exp) exp = pd.Series([False, True, False, False], index=list('abcd')) tm.assert_series_equal(left.gt(right), exp) def test_comparison_flex_alignment_fill(self): left = Series([1, 3, 2], index=list('abc')) right = Series([2, 2, 2], index=list('bcd')) exp = pd.Series([False, False, True, True], index=list('abcd')) tm.assert_series_equal(left.eq(right, fill_value=2), exp) exp = pd.Series([True, True, False, False], index=list('abcd')) tm.assert_series_equal(left.ne(right, fill_value=2), exp) exp = pd.Series([False, False, True, True], index=list('abcd')) tm.assert_series_equal(left.le(right, fill_value=0), exp) exp = pd.Series([False, False, False, True], index=list('abcd')) tm.assert_series_equal(left.lt(right, fill_value=0), exp) exp = pd.Series([True, True, True, False], index=list('abcd')) tm.assert_series_equal(left.ge(right, fill_value=0), exp) exp = pd.Series([True, True, False, False], index=list('abcd')) tm.assert_series_equal(left.gt(right, fill_value=0), exp) def test_operators_bitwise(self): # GH 9016: support bitwise op for integer types index = list('bca') s_tft = Series([True, False, True], index=index) s_fff = Series([False, False, False], index=index) s_tff = Series([True, False, False], index=index) s_empty = Series([]) # TODO: unused # s_0101 = Series([0, 1, 0, 1]) s_0123 = Series(range(4), dtype='int64') s_3333 = Series([3] * 4) s_4444 = Series([4] * 4) res = s_tft & s_empty expected = s_fff assert_series_equal(res, expected) res = s_tft | s_empty expected = s_tft assert_series_equal(res, expected) res = s_0123 & s_3333 expected = Series(range(4), dtype='int64') assert_series_equal(res, expected) res = s_0123 | s_4444 expected = Series(range(4, 8), dtype='int64') assert_series_equal(res, expected) s_a0b1c0 = Series([1], list('b')) res = s_tft & s_a0b1c0 expected = s_tff.reindex(list('abc')) assert_series_equal(res, expected) res = s_tft | s_a0b1c0 expected = s_tft.reindex(list('abc')) assert_series_equal(res, expected) n0 = 0 res = s_tft & n0 expected = s_fff assert_series_equal(res, expected) res = s_0123 & n0 expected = Series([0] * 4) assert_series_equal(res, expected) n1 = 1 res = s_tft & n1 expected = s_tft assert_series_equal(res, expected) res = s_0123 & n1 expected = Series([0, 1, 0, 1]) assert_series_equal(res, expected) s_1111 = Series([1] * 4, dtype='int8') res = s_0123 & s_1111 expected = Series([0, 1, 0, 1], dtype='int64') assert_series_equal(res, expected) res = s_0123.astype(np.int16) | s_1111.astype(np.int32) expected = Series([1, 1, 3, 3], dtype='int32') assert_series_equal(res, expected) self.assertRaises(TypeError, lambda: s_1111 & 'a') self.assertRaises(TypeError, lambda: s_1111 & ['a', 'b', 'c', 'd']) self.assertRaises(TypeError, lambda: s_0123 & np.NaN) self.assertRaises(TypeError, lambda: s_0123 & 3.14) self.assertRaises(TypeError, lambda: s_0123 & [0.1, 4, 3.14, 2]) # s_0123 will be all false now because of reindexing like s_tft if compat.PY3: # unable to sort incompatible object via .union. exp = Series([False] * 7, index=['b', 'c', 'a', 0, 1, 2, 3]) with tm.assert_produces_warning(RuntimeWarning): assert_series_equal(s_tft & s_0123, exp) else: exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) assert_series_equal(s_tft & s_0123, exp) # s_tft will be all false now because of reindexing like s_0123 if compat.PY3: # unable to sort incompatible object via .union. exp = Series([False] * 7, index=[0, 1, 2, 3, 'b', 'c', 'a']) with tm.assert_produces_warning(RuntimeWarning): assert_series_equal(s_0123 & s_tft, exp) else: exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) assert_series_equal(s_0123 & s_tft, exp) assert_series_equal(s_0123 & False, Series([False] * 4)) assert_series_equal(s_0123 ^ False, Series([False, True, True, True])) assert_series_equal(s_0123 & [False], Series([False] * 4)) assert_series_equal(s_0123 & (False), Series([False] * 4)) assert_series_equal(s_0123 & Series([False, np.NaN, False, False]), Series([False] * 4)) s_ftft = Series([False, True, False, True]) assert_series_equal(s_0123 & Series([0.1, 4, -3.14, 2]), s_ftft) s_abNd = Series(['a', 'b', np.NaN, 'd']) res = s_0123 & s_abNd expected = s_ftft assert_series_equal(res, expected) def test_scalar_na_cmp_corners(self): s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10]) def tester(a, b): return a & b self.assertRaises(TypeError, tester, s, datetime(2005, 1, 1)) s = Series([2, 3, 4, 5, 6, 7, 8, 9, datetime(2005, 1, 1)]) s[::2] = np.nan expected = Series(True, index=s.index) expected[::2] = False assert_series_equal(tester(s, list(s)), expected) d = DataFrame({'A': s}) # TODO: Fix this exception - needs to be fixed! (see GH5035) # (previously this was a TypeError because series returned # NotImplemented # this is an alignment issue; these are equivalent # https://github.com/pydata/pandas/issues/5284 self.assertRaises(ValueError, lambda: d.__and__(s, axis='columns')) self.assertRaises(ValueError, tester, s, d) # this is wrong as its not a boolean result # result = d.__and__(s,axis='index') def test_operators_corner(self): series = self.ts empty = Series([], index=Index([])) result = series + empty self.assertTrue(np.isnan(result).all()) result = empty + Series([], index=Index([])) self.assertEqual(len(result), 0) # TODO: this returned NotImplemented earlier, what to do? # deltas = Series([timedelta(1)] * 5, index=np.arange(5)) # sub_deltas = deltas[::2] # deltas5 = deltas * 5 # deltas = deltas + sub_deltas # float + int int_ts = self.ts.astype(int)[:-5] added = self.ts + int_ts expected = Series(self.ts.values[:-5] + int_ts.values, index=self.ts.index[:-5], name='ts') self.assert_series_equal(added[:-5], expected) def test_operators_reverse_object(self): # GH 56 arr = Series(np.random.randn(10), index=np.arange(10), dtype=object) def _check_op(arr, op): result = op(1., arr) expected = op(1., arr.astype(float)) assert_series_equal(result.astype(float), expected) _check_op(arr, operator.add) _check_op(arr, operator.sub) _check_op(arr, operator.mul) _check_op(arr, operator.truediv) _check_op(arr, operator.floordiv) def test_arith_ops_df_compat(self): # GH 1134 s1 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s2 = pd.Series([2, 2, 2], index=list('ABD'), name='x') exp = pd.Series([3.0, 4.0, np.nan, np.nan], index=list('ABCD'), name='x') tm.assert_series_equal(s1 + s2, exp) tm.assert_series_equal(s2 + s1, exp) exp = pd.DataFrame({'x': [3.0, 4.0, np.nan, np.nan]}, index=list('ABCD')) tm.assert_frame_equal(s1.to_frame() + s2.to_frame(), exp) tm.assert_frame_equal(s2.to_frame() + s1.to_frame(), exp) # different length s3 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s4 = pd.Series([2, 2, 2, 2], index=list('ABCD'), name='x') exp = pd.Series([3, 4, 5, np.nan], index=list('ABCD'), name='x') tm.assert_series_equal(s3 + s4, exp) tm.assert_series_equal(s4 + s3, exp) exp = pd.DataFrame({'x': [3, 4, 5, np.nan]}, index=list('ABCD')) tm.assert_frame_equal(s3.to_frame() + s4.to_frame(), exp) tm.assert_frame_equal(s4.to_frame() + s3.to_frame(), exp) def test_comp_ops_df_compat(self): # GH 1134 s1 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s2 = pd.Series([2, 2, 2], index=list('ABD'), name='x') s3 = pd.Series([1, 2, 3], index=list('ABC'), name='x') s4 = pd.Series([2, 2, 2, 2], index=list('ABCD'), name='x') for l, r in [(s1, s2), (s2, s1), (s3, s4), (s4, s3)]: msg = "Can only compare identically-labeled Series objects" with tm.assertRaisesRegexp(ValueError, msg): l == r with tm.assertRaisesRegexp(ValueError, msg): l != r with tm.assertRaisesRegexp(ValueError, msg): l < r msg = "Can only compare identically-labeled DataFrame objects" with tm.assertRaisesRegexp(ValueError, msg): l.to_frame() == r.to_frame() with tm.assertRaisesRegexp(ValueError, msg): l.to_frame() != r.to_frame() with tm.assertRaisesRegexp(ValueError, msg): l.to_frame() < r.to_frame() def test_bool_ops_df_compat(self): # GH 1134 s1 = pd.Series([True, False, True], index=list('ABC'), name='x') s2 = pd.Series([True, True, False], index=list('ABD'), name='x') exp = pd.Series([True, False, False, False], index=list('ABCD'), name='x') tm.assert_series_equal(s1 & s2, exp) tm.assert_series_equal(s2 & s1, exp) # True | np.nan => True exp = pd.Series([True, True, True, False], index=list('ABCD'), name='x') tm.assert_series_equal(s1 | s2, exp) # np.nan | True => np.nan, filled with False exp = pd.Series([True, True, False, False], index=list('ABCD'), name='x') tm.assert_series_equal(s2 | s1, exp) # DataFrame doesn't fill nan with False exp = pd.DataFrame({'x': [True, False, np.nan, np.nan]}, index=list('ABCD')) tm.assert_frame_equal(s1.to_frame() & s2.to_frame(), exp) tm.assert_frame_equal(s2.to_frame() & s1.to_frame(), exp) exp = pd.DataFrame({'x': [True, True, np.nan, np.nan]}, index=list('ABCD')) tm.assert_frame_equal(s1.to_frame() | s2.to_frame(), exp) tm.assert_frame_equal(s2.to_frame() | s1.to_frame(), exp) # different length s3 = pd.Series([True, False, True], index=list('ABC'), name='x') s4 = pd.Series([True, True, True, True], index=list('ABCD'), name='x') exp = pd.Series([True, False, True, False], index=list('ABCD'), name='x') tm.assert_series_equal(s3 & s4, exp) tm.assert_series_equal(s4 & s3, exp) # np.nan | True => np.nan, filled with False exp = pd.Series([True, True, True, False], index=list('ABCD'), name='x') tm.assert_series_equal(s3 | s4, exp) # True | np.nan => True exp = pd.Series([True, True, True, True], index=list('ABCD'), name='x') tm.assert_series_equal(s4 | s3, exp) exp = pd.DataFrame({'x': [True, False, True, np.nan]}, index=list('ABCD')) tm.assert_frame_equal(s3.to_frame() & s4.to_frame(), exp) tm.assert_frame_equal(s4.to_frame() & s3.to_frame(), exp) exp = pd.DataFrame({'x': [True, True, True, np.nan]}, index=list('ABCD')) tm.assert_frame_equal(s3.to_frame() | s4.to_frame(), exp) tm.assert_frame_equal(s4.to_frame() | s3.to_frame(), exp) def test_series_frame_radd_bug(self): # GH 353 vals = Series(tm.rands_array(5, 10)) result = 'foo_' + vals expected = vals.map(lambda x: 'foo_' + x) assert_series_equal(result, expected) frame = DataFrame({'vals': vals}) result = 'foo_' + frame expected = DataFrame({'vals': vals.map(lambda x: 'foo_' + x)}) tm.assert_frame_equal(result, expected) # really raise this time with tm.assertRaises(TypeError): datetime.now() + self.ts with tm.assertRaises(TypeError): self.ts + datetime.now() def test_series_radd_more(self): data = [[1, 2, 3], [1.1, 2.2, 3.3], [pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02'), pd.NaT], ['x', 'y', 1]] for d in data: for dtype in [None, object]: s = Series(d, dtype=dtype) with tm.assertRaises(TypeError): 'foo_' + s for dtype in [None, object]: res = 1 + pd.Series([1, 2, 3], dtype=dtype) exp = pd.Series([2, 3, 4], dtype=dtype) tm.assert_series_equal(res, exp) res = pd.Series([1, 2, 3], dtype=dtype) + 1 tm.assert_series_equal(res, exp) res = np.nan + pd.Series([1, 2, 3], dtype=dtype) exp = pd.Series([np.nan, np.nan, np.nan], dtype=dtype) tm.assert_series_equal(res, exp) res = pd.Series([1, 2, 3], dtype=dtype) + np.nan tm.assert_series_equal(res, exp) s = pd.Series([pd.Timedelta('1 days'), pd.Timedelta('2 days'), pd.Timedelta('3 days')], dtype=dtype) exp = pd.Series([pd.Timedelta('4 days'), pd.Timedelta('5 days'), pd.Timedelta('6 days')]) tm.assert_series_equal(pd.Timedelta('3 days') + s, exp) tm.assert_series_equal(s + pd.Timedelta('3 days'), exp) s = pd.Series(['x', np.nan, 'x']) tm.assert_series_equal('a' + s, pd.Series(['ax', np.nan, 'ax'])) tm.assert_series_equal(s + 'a', pd.Series(['xa', np.nan, 'xa'])) def test_frame_radd_more(self): data = [[1, 2, 3], [1.1, 2.2, 3.3], [pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02'), pd.NaT], ['x', 'y', 1]] for d in data: for dtype in [None, object]: s = DataFrame(d, dtype=dtype) with tm.assertRaises(TypeError): 'foo_' + s for dtype in [None, object]: res = 1 + pd.DataFrame([1, 2, 3], dtype=dtype) exp = pd.DataFrame([2, 3, 4], dtype=dtype) tm.assert_frame_equal(res, exp) res = pd.DataFrame([1, 2, 3], dtype=dtype) + 1 tm.assert_frame_equal(res, exp) res = np.nan + pd.DataFrame([1, 2, 3], dtype=dtype) exp = pd.DataFrame([np.nan, np.nan, np.nan], dtype=dtype) tm.assert_frame_equal(res, exp) res = pd.DataFrame([1, 2, 3], dtype=dtype) + np.nan tm.assert_frame_equal(res, exp) df = pd.DataFrame(['x', np.nan, 'x']) tm.assert_frame_equal('a' + df, pd.DataFrame(['ax', np.nan, 'ax'])) tm.assert_frame_equal(df + 'a', pd.DataFrame(['xa', np.nan, 'xa'])) def test_operators_frame(self): # rpow does not work with DataFrame df = DataFrame({'A': self.ts}) tm.assert_series_equal(self.ts + self.ts, self.ts + df['A'], check_names=False) tm.assert_series_equal(self.ts ** self.ts, self.ts ** df['A'], check_names=False) tm.assert_series_equal(self.ts < self.ts, self.ts < df['A'], check_names=False) tm.assert_series_equal(self.ts / self.ts, self.ts / df['A'], check_names=False) def test_operators_combine(self): def _check_fill(meth, op, a, b, fill_value=0): exp_index = a.index.union(b.index) a = a.reindex(exp_index) b = b.reindex(exp_index) amask = isnull(a) bmask = isnull(b) exp_values = [] for i in range(len(exp_index)): with np.errstate(all='ignore'): if amask[i]: if bmask[i]: exp_values.append(nan) continue exp_values.append(op(fill_value, b[i])) elif bmask[i]: if amask[i]: exp_values.append(nan) continue exp_values.append(op(a[i], fill_value)) else: exp_values.append(op(a[i], b[i])) result = meth(a, b, fill_value=fill_value) expected = Series(exp_values, exp_index) assert_series_equal(result, expected) a = Series([nan, 1., 2., 3., nan], index=np.arange(5)) b = Series([nan, 1, nan, 3, nan, 4.], index=np.arange(6)) pairings = [] for op in ['add', 'sub', 'mul', 'pow', 'truediv', 'floordiv']: fv = 0 lop = getattr(Series, op) lequiv = getattr(operator, op) rop = getattr(Series, 'r' + op) # bind op at definition time... requiv = lambda x, y, op=op: getattr(operator, op)(y, x) pairings.append((lop, lequiv, fv)) pairings.append((rop, requiv, fv)) if compat.PY3: pairings.append((Series.div, operator.truediv, 1)) pairings.append((Series.rdiv, lambda x, y: operator.truediv(y, x), 1)) else: pairings.append((Series.div, operator.div, 1)) pairings.append((Series.rdiv, lambda x, y: operator.div(y, x), 1)) for op, equiv_op, fv in pairings: result = op(a, b) exp = equiv_op(a, b) assert_series_equal(result, exp) _check_fill(op, equiv_op, a, b, fill_value=fv) # should accept axis=0 or axis='rows' op(a, b, axis=0) def test_ne(self): ts = Series([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float) expected = [True, True, False, True, True] self.assertTrue(tm.equalContents(ts.index != 5, expected)) self.assertTrue(tm.equalContents(~(ts.index == 5), expected)) def test_operators_na_handling(self): from decimal import Decimal from datetime import date s = Series([Decimal('1.3'), Decimal('2.3')], index=[date(2012, 1, 1), date(2012, 1, 2)]) result = s + s.shift(1) result2 = s.shift(1) + s self.assertTrue(isnull(result[0])) self.assertTrue(isnull(result2[0])) s = Series(['foo', 'bar', 'baz', np.nan]) result = 'prefix_' + s expected = Series(['prefix_foo', 'prefix_bar', 'prefix_baz', np.nan]) assert_series_equal(result, expected) result = s + '_suffix' expected = Series(['foo_suffix', 'bar_suffix', 'baz_suffix', np.nan]) assert_series_equal(result, expected) def test_divide_decimal(self): """ resolves issue #9787 """ from decimal import Decimal expected = Series([Decimal(5)]) s = Series([Decimal(10)]) s = s / Decimal(2) tm.assert_series_equal(expected, s) s = Series([Decimal(10)]) s = s // Decimal(2) tm.assert_series_equal(expected, s) def test_datetime64_with_index(self): # arithmetic integer ops with an index s = Series(np.random.randn(5)) expected = s - s.index.to_series() result = s - s.index assert_series_equal(result, expected) # GH 4629 # arithmetic datetime64 ops with an index s = Series(date_range('20130101', periods=5), index=date_range('20130101', periods=5)) expected = s - s.index.to_series() result = s - s.index assert_series_equal(result, expected) result = s - s.index.to_period() assert_series_equal(result, expected) df = DataFrame(np.random.randn(5, 2), index=date_range('20130101', periods=5)) df['date'] = Timestamp('20130102') df['expected'] = df['date'] - df.index.to_series() df['result'] = df['date'] - df.index assert_series_equal(df['result'], df['expected'], check_names=False) def test_dti_tz_convert_to_utc(self): base = pd.DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], tz='UTC') idx1 = base.tz_convert('Asia/Tokyo')[:2] idx2 = base.tz_convert('US/Eastern')[1:] res = Series([1, 2], index=idx1) + Series([1, 1], index=idx2) assert_series_equal(res, Series([np.nan, 3, np.nan], index=base))
[ "numpy.abs", "operator.add", "operator.pow", "numpy.isnan", "pandas.DatetimeIndex", "numpy.arange", "numpy.float64", "pandas.bdate_range", "pandas.DataFrame", "pandas.offsets.Minute", "numpy.random.randn", "pandas.tseries.index.Timestamp", "pandas.util.testing.rands_array", "datetime.timedelta", "pandas.Period", "operator.truediv", "pandas.Timedelta", "operator.div", "pandas.compat.range", "operator.mod", "datetime.datetime.now", "pandas.util.testing.assertIsInstance", "pandas.core.nanops.nangt", "pandas.date_range", "pandas.util.testing.assert_frame_equal", "pandas.timedelta_range", "pandas.util.testing.assertRaises", "pandas.tseries.tdi.Timedelta", "datetime.date", "datetime.datetime", "pandas.Index", "pandas.to_timedelta", "pandas.Series", "pandas.util.testing.assert_almost_equal", "pandas.util.testing.assert_series_equal", "operator.sub", "pandas.Timestamp", "pandas.compat.zip", "pandas.offsets.Hour", "decimal.Decimal", "pandas.util.testing.equalContents", "numpy.errstate", "pandas.util.testing.makeFloatSeries", "pandas.isnull", "operator.floordiv", "pandas.offsets.Second", "numpy.timedelta64", "numpy.array", "pandas.util.testing.assertRaisesRegexp", "pandas.offsets.Milli", "numpy.array_equal", "pandas.util.testing.assert_produces_warning", "operator.mul" ]
[((834, 853), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (849, 853), True, 'import numpy as np\n'), ((870, 889), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (885, 889), True, 'import numpy as np\n'), ((934, 959), 'pandas.core.nanops.nangt', 'nanops.nangt', (['left', 'right'], {}), '(left, right)\n', (946, 959), True, 'import pandas.core.nanops as nanops\n'), ((1093, 1130), 'pandas.util.testing.assert_almost_equal', 'assert_almost_equal', (['result', 'expected'], {}), '(result, expected)\n', (1112, 1130), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((1144, 1167), 'pandas.Series', 'Series', (["['a', 'b', 'c']"], {}), "(['a', 'b', 'c'])\n", (1150, 1167), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((1181, 1209), 'pandas.Series', 'Series', (['[False, True, False]'], {}), '([False, True, False])\n', (1187, 1209), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((1245, 1274), 'pandas.Series', 'Series', (['[False, False, False]'], {}), '([False, False, False])\n', (1251, 1274), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((1283, 1319), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s == s2)', 'exp'], {}), '(s == s2, exp)\n', (1305, 1319), True, 'import pandas.util.testing as tm\n'), ((1328, 1364), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s2 == s)', 'exp'], {}), '(s2 == s, exp)\n', (1350, 1364), True, 'import pandas.util.testing as tm\n'), ((2461, 2512), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-self.series)', '(-1 * self.series)'], {}), '(-self.series, -1 * self.series)\n', (2480, 2512), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((2549, 2608), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-(self.series < 0))', '(~(self.series < 0))'], {}), '(-(self.series < 0), ~(self.series < 0))\n', (2568, 2608), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((6874, 6904), 'pandas.Series', 'Series', (['[]', '[]'], {'dtype': 'np.int32'}), '([], [], dtype=np.int32)\n', (6880, 6904), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((6918, 6936), 'pandas.Series', 'Series', (["{'x': 0.0}"], {}), "({'x': 0.0})\n", (6924, 6936), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((7469, 7512), 'pandas.date_range', 'date_range', (['"""2012-1-1"""'], {'periods': '(3)', 'freq': '"""D"""'}), "('2012-1-1', periods=3, freq='D')\n", (7479, 7512), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((7526, 7569), 'pandas.date_range', 'date_range', (['"""2012-1-2"""'], {'periods': '(3)', 'freq': '"""D"""'}), "('2012-1-2', periods=3, freq='D')\n", (7536, 7569), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((7724, 7751), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['rs', 'xp'], {}), '(rs, xp)\n', (7743, 7751), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((8237, 8273), 'pandas.util.testing.assertIsInstance', 'tm.assertIsInstance', (['maxa', 'Timestamp'], {}), '(maxa, Timestamp)\n', (8256, 8273), True, 'import pandas.util.testing as tm\n'), ((8563, 8587), 'pandas.Series', 'Series', (['values'], {'name': '"""A"""'}), "(values, name='A')\n", (8569, 8587), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((8596, 8633), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (8615, 8633), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((8813, 8850), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (8832, 8850), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((8913, 8939), 'datetime.datetime', 'datetime', (['(2001)', '(1)', '(1)', '(3)', '(4)'], {}), '(2001, 1, 1, 3, 4)\n', (8921, 8939), False, 'from datetime import datetime, timedelta\n'), ((9079, 9116), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (["df['A']", 'resultb'], {}), "(df['A'], resultb)\n", (9098, 9116), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((9159, 9176), 'datetime.timedelta', 'timedelta', ([], {'days': '(1)'}), '(days=1)\n', (9168, 9176), False, 'from datetime import datetime, timedelta\n'), ((9247, 9284), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['resultb', "df['A']"], {}), "(resultb, df['A'])\n", (9266, 9284), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((9369, 9400), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(3)'}), '(minutes=5, seconds=3)\n', (9378, 9400), False, 'from datetime import datetime, timedelta\n'), ((9471, 9508), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (["df['A']", 'resultb'], {}), "(df['A'], resultb)\n", (9490, 9508), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((10667, 10704), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (10686, 10704), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((10713, 10751), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result2', 'expected'], {}), '(result2, expected)\n', (10732, 10751), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((10950, 10987), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (10969, 10987), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((10996, 11034), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result2', 'expected'], {}), '(result2, expected)\n', (11015, 11034), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((11238, 11275), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (11257, 11275), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((11284, 11322), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result2', 'expected'], {}), '(result2, expected)\n', (11303, 11322), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((11507, 11544), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (11526, 11544), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((11794, 11831), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (11813, 11831), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((11840, 11878), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result2', 'expected'], {}), '(result2, expected)\n', (11859, 11878), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((12090, 12127), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (12109, 12127), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((12136, 12174), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result2', 'expected'], {}), '(result2, expected)\n', (12155, 12174), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((12519, 12542), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""2012-01-01"""'], {}), "('2012-01-01')\n", (12528, 12542), False, 'from pandas.tseries.index import Timestamp\n'), ((12614, 12651), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(ts + s)', 'expected'], {}), '(ts + s, expected)\n', (12633, 12651), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((12660, 12697), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(s + ts)', 'expected'], {}), '(s + ts, expected)\n', (12679, 12697), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((12783, 12821), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(ts - s)', 'expected2'], {}), '(ts - s, expected2)\n', (12802, 12821), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((12830, 12869), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(ts + -s)', 'expected2'], {}), '(ts + -s, expected2)\n', (12849, 12869), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((13126, 13163), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (13145, 13163), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((13281, 13318), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (13300, 13318), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((13602, 13639), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (13621, 13639), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((13782, 13819), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (13801, 13819), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((14246, 14277), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(4)'}), '(minutes=5, seconds=4)\n', (14255, 14277), False, 'from datetime import datetime, timedelta\n'), ((14466, 14503), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (14485, 14503), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((14647, 14685), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result2', 'expected'], {}), '(result2, expected)\n', (14666, 14685), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((14715, 14753), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(result + td2)', 'td1'], {}), '(result + td2, td1)\n', (14734, 14753), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((14940, 14967), 'pandas.to_timedelta', 'pd.to_timedelta', (['"""00:05:04"""'], {}), "('00:05:04')\n", (14955, 14967), True, 'import pandas as pd\n'), ((15156, 15193), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (15175, 15193), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((15337, 15375), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result2', 'expected'], {}), '(result2, expected)\n', (15356, 15375), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((15405, 15443), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(result + td2)', 'td1'], {}), '(result + td2, td1)\n', (15424, 15443), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((15761, 15778), 'pandas.Series', 'Series', (['[2, 3, 4]'], {}), '([2, 3, 4])\n', (15767, 15778), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((15916, 15953), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (15935, 15953), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((15968, 15988), 'pandas.Series', 'Series', (['[20, 30, 40]'], {}), '([20, 30, 40])\n', (15974, 15988), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((16126, 16163), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (16145, 16163), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((16300, 16337), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (16319, 16337), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((16352, 16372), 'pandas.Series', 'Series', (['[20, 30, 40]'], {}), '([20, 30, 40])\n', (16358, 16372), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((16510, 16547), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (16529, 16547), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((17089, 17126), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (17108, 17126), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((17265, 17302), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (17284, 17302), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((21145, 21176), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(4)'}), '(minutes=5, seconds=4)\n', (21154, 21176), False, 'from datetime import datetime, timedelta\n'), ((23357, 23389), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (23376, 23389), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((23498, 23530), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (23517, 23530), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((24034, 24066), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (24053, 24066), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((24234, 24266), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (24253, 24266), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((24428, 24460), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (24447, 24460), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((24563, 24595), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (24582, 24595), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((24698, 24730), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (24717, 24730), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((24833, 24865), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (24852, 24865), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((25261, 25298), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (25280, 25298), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((25381, 25418), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (25400, 25418), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((25625, 25668), 'pandas.Series', 'Series', (['[NaT, NaT]'], {'dtype': '"""timedelta64[ns]"""'}), "([NaT, NaT], dtype='timedelta64[ns]')\n", (25631, 25668), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((25719, 25761), 'pandas.Series', 'Series', (['[NaT, NaT]'], {'dtype': '"""datetime64[ns]"""'}), "([NaT, NaT], dtype='datetime64[ns]')\n", (25725, 25761), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((25798, 25835), 'pandas.Series', 'Series', (['[NaT]'], {'dtype': '"""datetime64[ns]"""'}), "([NaT], dtype='datetime64[ns]')\n", (25804, 25835), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((25873, 25911), 'pandas.Series', 'Series', (['[NaT]'], {'dtype': '"""timedelta64[ns]"""'}), "([NaT], dtype='timedelta64[ns]')\n", (25879, 25911), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((25943, 26014), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(timedelta_series - NaT)', 'nat_series_dtype_timedelta'], {}), '(timedelta_series - NaT, nat_series_dtype_timedelta)\n', (25962, 26014), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26023, 26095), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-NaT + timedelta_series)', 'nat_series_dtype_timedelta'], {}), '(-NaT + timedelta_series, nat_series_dtype_timedelta)\n', (26042, 26095), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26133, 26231), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(timedelta_series - single_nat_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(timedelta_series - single_nat_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (26152, 26231), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26264, 26363), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-single_nat_dtype_timedelta + timedelta_series)', 'nat_series_dtype_timedelta'], {}), '(-single_nat_dtype_timedelta + timedelta_series,\n nat_series_dtype_timedelta)\n', (26283, 26363), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26397, 26467), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(datetime_series - NaT)', 'nat_series_dtype_timestamp'], {}), '(datetime_series - NaT, nat_series_dtype_timestamp)\n', (26416, 26467), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26476, 26547), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-NaT + datetime_series)', 'nat_series_dtype_timestamp'], {}), '(-NaT + datetime_series, nat_series_dtype_timestamp)\n', (26495, 26547), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26557, 26653), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(datetime_series - single_nat_dtype_datetime)', 'nat_series_dtype_timedelta'], {}), '(datetime_series - single_nat_dtype_datetime,\n nat_series_dtype_timedelta)\n', (26576, 26653), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26785, 26882), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(datetime_series - single_nat_dtype_timedelta)', 'nat_series_dtype_timestamp'], {}), '(datetime_series - single_nat_dtype_timedelta,\n nat_series_dtype_timestamp)\n', (26804, 26882), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26915, 27013), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-single_nat_dtype_timedelta + datetime_series)', 'nat_series_dtype_timestamp'], {}), '(-single_nat_dtype_timedelta + datetime_series,\n nat_series_dtype_timestamp)\n', (26934, 27013), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((27213, 27298), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timestamp - NaT)', 'nat_series_dtype_timestamp'], {}), '(nat_series_dtype_timestamp - NaT,\n nat_series_dtype_timestamp)\n', (27232, 27298), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((27331, 27417), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-NaT + nat_series_dtype_timestamp)', 'nat_series_dtype_timestamp'], {}), '(-NaT + nat_series_dtype_timestamp,\n nat_series_dtype_timestamp)\n', (27350, 27417), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((27451, 27558), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timestamp - single_nat_dtype_datetime)', 'nat_series_dtype_timedelta'], {}), '(nat_series_dtype_timestamp - single_nat_dtype_datetime,\n nat_series_dtype_timedelta)\n', (27470, 27558), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((27729, 27837), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timestamp - single_nat_dtype_timedelta)', 'nat_series_dtype_timestamp'], {}), '(nat_series_dtype_timestamp - single_nat_dtype_timedelta,\n nat_series_dtype_timestamp)\n', (27748, 27837), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((27898, 28007), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(-single_nat_dtype_timedelta + nat_series_dtype_timestamp)', 'nat_series_dtype_timestamp'], {}), '(-single_nat_dtype_timedelta +\n nat_series_dtype_timestamp, nat_series_dtype_timestamp)\n', (27917, 28007), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((28187, 28272), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timestamp + NaT)', 'nat_series_dtype_timestamp'], {}), '(nat_series_dtype_timestamp + NaT,\n nat_series_dtype_timestamp)\n', (28206, 28272), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((28305, 28390), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(NaT + nat_series_dtype_timestamp)', 'nat_series_dtype_timestamp'], {}), '(NaT + nat_series_dtype_timestamp,\n nat_series_dtype_timestamp)\n', (28324, 28390), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((28424, 28532), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timestamp + single_nat_dtype_timedelta)', 'nat_series_dtype_timestamp'], {}), '(nat_series_dtype_timestamp + single_nat_dtype_timedelta,\n nat_series_dtype_timestamp)\n', (28443, 28532), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((28593, 28701), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(single_nat_dtype_timedelta + nat_series_dtype_timestamp)', 'nat_series_dtype_timestamp'], {}), '(single_nat_dtype_timedelta + nat_series_dtype_timestamp,\n nat_series_dtype_timestamp)\n', (28612, 28701), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((28763, 28848), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timedelta + NaT)', 'nat_series_dtype_timedelta'], {}), '(nat_series_dtype_timedelta + NaT,\n nat_series_dtype_timedelta)\n', (28782, 28848), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((28881, 28966), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(NaT + nat_series_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(NaT + nat_series_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (28900, 28966), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29000, 29108), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timedelta + single_nat_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(nat_series_dtype_timedelta + single_nat_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (29019, 29108), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29169, 29277), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(single_nat_dtype_timedelta + nat_series_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(single_nat_dtype_timedelta + nat_series_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (29188, 29277), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29339, 29410), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(timedelta_series + NaT)', 'nat_series_dtype_timedelta'], {}), '(timedelta_series + NaT, nat_series_dtype_timedelta)\n', (29358, 29410), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29419, 29490), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(NaT + timedelta_series)', 'nat_series_dtype_timedelta'], {}), '(NaT + timedelta_series, nat_series_dtype_timedelta)\n', (29438, 29490), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29500, 29598), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(timedelta_series + single_nat_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(timedelta_series + single_nat_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (29519, 29598), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29631, 29729), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(single_nat_dtype_timedelta + timedelta_series)', 'nat_series_dtype_timedelta'], {}), '(single_nat_dtype_timedelta + timedelta_series,\n nat_series_dtype_timedelta)\n', (29650, 29729), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29763, 29848), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timestamp + NaT)', 'nat_series_dtype_timestamp'], {}), '(nat_series_dtype_timestamp + NaT,\n nat_series_dtype_timestamp)\n', (29782, 29848), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((29881, 29966), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(NaT + nat_series_dtype_timestamp)', 'nat_series_dtype_timestamp'], {}), '(NaT + nat_series_dtype_timestamp,\n nat_series_dtype_timestamp)\n', (29900, 29966), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((30000, 30108), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timestamp + single_nat_dtype_timedelta)', 'nat_series_dtype_timestamp'], {}), '(nat_series_dtype_timestamp + single_nat_dtype_timedelta,\n nat_series_dtype_timestamp)\n', (30019, 30108), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((30169, 30277), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(single_nat_dtype_timedelta + nat_series_dtype_timestamp)', 'nat_series_dtype_timestamp'], {}), '(single_nat_dtype_timedelta + nat_series_dtype_timestamp,\n nat_series_dtype_timestamp)\n', (30188, 30277), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((30339, 30424), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timedelta + NaT)', 'nat_series_dtype_timedelta'], {}), '(nat_series_dtype_timedelta + NaT,\n nat_series_dtype_timedelta)\n', (30358, 30424), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((30457, 30542), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(NaT + nat_series_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(NaT + nat_series_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (30476, 30542), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((30576, 30684), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timedelta + single_nat_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(nat_series_dtype_timedelta + single_nat_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (30595, 30684), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((30745, 30853), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(single_nat_dtype_timedelta + nat_series_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(single_nat_dtype_timedelta + nat_series_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (30764, 30853), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((30915, 31022), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timedelta + single_nat_dtype_datetime)', 'nat_series_dtype_timestamp'], {}), '(nat_series_dtype_timedelta + single_nat_dtype_datetime,\n nat_series_dtype_timestamp)\n', (30934, 31022), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((31083, 31190), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(single_nat_dtype_datetime + nat_series_dtype_timedelta)', 'nat_series_dtype_timestamp'], {}), '(single_nat_dtype_datetime + nat_series_dtype_timedelta,\n nat_series_dtype_timestamp)\n', (31102, 31190), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((31277, 31362), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nat_series_dtype_timedelta * 1.0)', 'nat_series_dtype_timedelta'], {}), '(nat_series_dtype_timedelta * 1.0,\n nat_series_dtype_timedelta)\n', (31296, 31362), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((31395, 31480), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(1.0 * nat_series_dtype_timedelta)', 'nat_series_dtype_timedelta'], {}), '(1.0 * nat_series_dtype_timedelta,\n nat_series_dtype_timedelta)\n', (31414, 31480), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((31514, 31573), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(timedelta_series * 1)', 'timedelta_series'], {}), '(timedelta_series * 1, timedelta_series)\n', (31533, 31573), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((31582, 31641), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(1 * timedelta_series)', 'timedelta_series'], {}), '(1 * timedelta_series, timedelta_series)\n', (31601, 31641), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((31880, 31951), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(timedelta_series * nan)', 'nat_series_dtype_timedelta'], {}), '(timedelta_series * nan, nat_series_dtype_timedelta)\n', (31899, 31951), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((31960, 32031), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(nan * timedelta_series)', 'nat_series_dtype_timedelta'], {}), '(nan * timedelta_series, nat_series_dtype_timedelta)\n', (31979, 32031), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((32605, 32676), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(timedelta_series / nan)', 'nat_series_dtype_timedelta'], {}), '(timedelta_series / nan, nat_series_dtype_timedelta)\n', (32624, 32676), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((33189, 33226), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (33208, 33226), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((33247, 33271), 'pandas.Series', 'Series', (['expected'], {'name': '(0)'}), '(expected, name=0)\n', (33253, 33271), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((33333, 33370), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (33352, 33370), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((33423, 33459), 'pandas.Series', 'Series', (["['a', 'b', np.nan, 'c', 'a']"], {}), "(['a', 'b', np.nan, 'c', 'a'])\n", (33429, 33459), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((33506, 33547), 'pandas.Series', 'Series', (['[True, False, False, False, True]'], {}), '([True, False, False, False, True])\n', (33512, 33547), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((33556, 33593), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (33575, 33593), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((33639, 33682), 'pandas.Series', 'Series', (['[False, False, False, False, False]'], {}), '([False, False, False, False, False])\n', (33645, 33682), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((33691, 33728), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (33710, 33728), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((33795, 33832), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (33814, 33832), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((33933, 33957), 'pandas.Series', 'Series', (['[(1, 1), (1, 2)]'], {}), '([(1, 1), (1, 2)])\n', (33939, 33957), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34007, 34028), 'pandas.Series', 'Series', (['[False, True]'], {}), '([False, True])\n', (34013, 34028), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34037, 34074), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (34056, 34074), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((34124, 34145), 'pandas.Series', 'Series', (['[True, False]'], {}), '([True, False])\n', (34130, 34145), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34154, 34191), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (34173, 34191), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((34241, 34263), 'pandas.Series', 'Series', (['[False, False]'], {}), '([False, False])\n', (34247, 34263), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34272, 34309), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (34291, 34309), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((34359, 34379), 'pandas.Series', 'Series', (['[True, True]'], {}), '([True, True])\n', (34365, 34379), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34388, 34425), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (34407, 34425), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((34439, 34463), 'pandas.Series', 'Series', (['[(1, 1), (1, 1)]'], {}), '([(1, 1), (1, 1)])\n', (34445, 34463), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34513, 34533), 'pandas.Series', 'Series', (['[True, True]'], {}), '([True, True])\n', (34519, 34533), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34542, 34579), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (34561, 34579), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((34629, 34651), 'pandas.Series', 'Series', (['[False, False]'], {}), '([False, False])\n', (34635, 34651), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34660, 34697), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (34679, 34697), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((34812, 34833), 'pandas.Series', 'Series', (['[True, False]'], {}), '([True, False])\n', (34818, 34833), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34842, 34879), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (34861, 34879), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40861, 40884), 'pandas.Series', 'Series', (["['a', 'b', 'c']"], {}), "(['a', 'b', 'c'])\n", (40867, 40884), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((40897, 40915), 'pandas.Series', 'Series', (["['b', 'a']"], {}), "(['b', 'a'])\n", (40903, 40915), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((40980, 40994), 'pandas.Series', 'Series', (['[1, 2]'], {}), '([1, 2])\n', (40986, 40994), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((41007, 41024), 'pandas.Series', 'Series', (['[2, 3, 4]'], {}), '([2, 3, 4])\n', (41013, 41024), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((41384, 41421), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (41403, 41421), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((41514, 41551), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (41533, 41551), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((41645, 41682), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (41664, 41682), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((41923, 41960), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (41942, 41960), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((42061, 42098), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (42080, 42098), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((42240, 42277), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (42259, 42277), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((42379, 42416), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (42398, 42416), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((42562, 42599), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (42581, 42599), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((42717, 42754), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (42736, 42754), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((43371, 43398), 'pandas.Series', 'Series', (['[True, False, True]'], {}), '([True, False, True])\n', (43377, 43398), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((47696, 47736), 'pandas.Series', 'Series', (['[True, False, True]'], {'index': 'index'}), '([True, False, True], index=index)\n', (47702, 47736), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((47753, 47795), 'pandas.Series', 'Series', (['[False, False, False]'], {'index': 'index'}), '([False, False, False], index=index)\n', (47759, 47795), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((47812, 47853), 'pandas.Series', 'Series', (['[True, False, False]'], {'index': 'index'}), '([True, False, False], index=index)\n', (47818, 47853), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((47872, 47882), 'pandas.Series', 'Series', (['[]'], {}), '([])\n', (47878, 47882), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((48014, 48029), 'pandas.Series', 'Series', (['([3] * 4)'], {}), '([3] * 4)\n', (48020, 48029), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((48047, 48062), 'pandas.Series', 'Series', (['([4] * 4)'], {}), '([4] * 4)\n', (48053, 48062), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((48127, 48161), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48146, 48161), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((48226, 48260), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48245, 48260), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((48351, 48385), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48370, 48385), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((48479, 48513), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48498, 48513), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((48643, 48677), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48662, 48677), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((48764, 48798), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48783, 48798), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((48873, 48907), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48892, 48907), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((48954, 48969), 'pandas.Series', 'Series', (['([0] * 4)'], {}), '([0] * 4)\n', (48960, 48969), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((48978, 49012), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (48997, 49012), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((49087, 49121), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (49106, 49121), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((49168, 49188), 'pandas.Series', 'Series', (['[0, 1, 0, 1]'], {}), '([0, 1, 0, 1])\n', (49174, 49188), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((49197, 49231), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (49216, 49231), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((49250, 49279), 'pandas.Series', 'Series', (['([1] * 4)'], {'dtype': '"""int8"""'}), "([1] * 4, dtype='int8')\n", (49256, 49279), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((49329, 49364), 'pandas.Series', 'Series', (['[0, 1, 0, 1]'], {'dtype': '"""int64"""'}), "([0, 1, 0, 1], dtype='int64')\n", (49335, 49364), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((49373, 49407), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (49392, 49407), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((49492, 49527), 'pandas.Series', 'Series', (['[1, 1, 3, 3]'], {'dtype': '"""int32"""'}), "([1, 1, 3, 3], dtype='int32')\n", (49498, 49527), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((49536, 49570), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (49555, 49570), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((51300, 51334), 'pandas.Series', 'Series', (['[False, True, False, True]'], {}), '([False, True, False, True])\n', (51306, 51334), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51426, 51457), 'pandas.Series', 'Series', (["['a', 'b', np.NaN, 'd']"], {}), "(['a', 'b', np.NaN, 'd'])\n", (51432, 51457), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51522, 51556), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['res', 'expected'], {}), '(res, expected)\n', (51541, 51556), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((51612, 51648), 'pandas.Series', 'Series', (['[2, 3, 4, 5, 6, 7, 8, 9, 10]'], {}), '([2, 3, 4, 5, 6, 7, 8, 9, 10])\n', (51618, 51648), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51884, 51911), 'pandas.Series', 'Series', (['(True)'], {'index': 's.index'}), '(True, index=s.index)\n', (51890, 51911), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((52013, 52032), 'pandas.DataFrame', 'DataFrame', (["{'A': s}"], {}), "({'A': s})\n", (52022, 52032), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((53175, 53260), 'pandas.Series', 'Series', (['(self.ts.values[:-5] + int_ts.values)'], {'index': 'self.ts.index[:-5]', 'name': '"""ts"""'}), "(self.ts.values[:-5] + int_ts.values, index=self.ts.index[:-5], name='ts'\n )\n", (53181, 53260), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((54150, 54186), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s1 + s2)', 'exp'], {}), '(s1 + s2, exp)\n', (54172, 54186), True, 'import pandas.util.testing as tm\n'), ((54195, 54231), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s2 + s1)', 'exp'], {}), '(s2 + s1, exp)\n', (54217, 54231), True, 'import pandas.util.testing as tm\n'), ((54738, 54774), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s3 + s4)', 'exp'], {}), '(s3 + s4, exp)\n', (54760, 54774), True, 'import pandas.util.testing as tm\n'), ((54783, 54819), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s4 + s3)', 'exp'], {}), '(s4 + s3, exp)\n', (54805, 54819), True, 'import pandas.util.testing as tm\n'), ((56448, 56484), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s1 & s2)', 'exp'], {}), '(s1 & s2, exp)\n', (56470, 56484), True, 'import pandas.util.testing as tm\n'), ((56493, 56529), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s2 & s1)', 'exp'], {}), '(s2 & s1, exp)\n', (56515, 56529), True, 'import pandas.util.testing as tm\n'), ((56676, 56712), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s1 | s2)', 'exp'], {}), '(s1 | s2, exp)\n', (56698, 56712), True, 'import pandas.util.testing as tm\n'), ((56880, 56916), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s2 | s1)', 'exp'], {}), '(s2 | s1, exp)\n', (56902, 56916), True, 'import pandas.util.testing as tm\n'), ((57749, 57785), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s3 & s4)', 'exp'], {}), '(s3 & s4, exp)\n', (57771, 57785), True, 'import pandas.util.testing as tm\n'), ((57794, 57830), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s4 & s3)', 'exp'], {}), '(s4 & s3, exp)\n', (57816, 57830), True, 'import pandas.util.testing as tm\n'), ((57998, 58034), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s3 | s4)', 'exp'], {}), '(s3 | s4, exp)\n', (58020, 58034), True, 'import pandas.util.testing as tm\n'), ((58179, 58215), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(s4 | s3)', 'exp'], {}), '(s4 | s3, exp)\n', (58201, 58215), True, 'import pandas.util.testing as tm\n'), ((58895, 58932), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (58914, 58932), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((58950, 58975), 'pandas.DataFrame', 'DataFrame', (["{'vals': vals}"], {}), "({'vals': vals})\n", (58959, 58975), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((59087, 59126), 'pandas.util.testing.assert_frame_equal', 'tm.assert_frame_equal', (['result', 'expected'], {}), '(result, expected)\n', (59108, 59126), True, 'import pandas.util.testing as tm\n'), ((60727, 60756), 'pandas.Series', 'pd.Series', (["['x', np.nan, 'x']"], {}), "(['x', np.nan, 'x'])\n", (60736, 60756), True, 'import pandas as pd\n'), ((61921, 61953), 'pandas.DataFrame', 'pd.DataFrame', (["['x', np.nan, 'x']"], {}), "(['x', np.nan, 'x'])\n", (61933, 61953), True, 'import pandas as pd\n'), ((62200, 62225), 'pandas.DataFrame', 'DataFrame', (["{'A': self.ts}"], {}), "({'A': self.ts})\n", (62209, 62225), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((62235, 62314), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(self.ts + self.ts)', "(self.ts + df['A'])"], {'check_names': '(False)'}), "(self.ts + self.ts, self.ts + df['A'], check_names=False)\n", (62257, 62314), True, 'import pandas.util.testing as tm\n'), ((62354, 62440), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(self.ts ** self.ts)', "(self.ts ** df['A'])"], {'check_names': '(False)'}), "(self.ts ** self.ts, self.ts ** df['A'], check_names=\n False)\n", (62376, 62440), True, 'import pandas.util.testing as tm\n'), ((62475, 62554), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(self.ts < self.ts)', "(self.ts < df['A'])"], {'check_names': '(False)'}), "(self.ts < self.ts, self.ts < df['A'], check_names=False)\n", (62497, 62554), True, 'import pandas.util.testing as tm\n'), ((62594, 62673), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['(self.ts / self.ts)', "(self.ts / df['A'])"], {'check_names': '(False)'}), "(self.ts / self.ts, self.ts / df['A'], check_names=False)\n", (62616, 62673), True, 'import pandas.util.testing as tm\n'), ((65043, 65096), 'pandas.Series', 'Series', (['[3, 4, 5, 6, 7]', '[3, 4, 5, 6, 7]'], {'dtype': 'float'}), '([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float)\n', (65049, 65096), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((65680, 65717), 'pandas.Series', 'Series', (["['foo', 'bar', 'baz', np.nan]"], {}), "(['foo', 'bar', 'baz', np.nan])\n", (65686, 65717), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((65768, 65826), 'pandas.Series', 'Series', (["['prefix_foo', 'prefix_bar', 'prefix_baz', np.nan]"], {}), "(['prefix_foo', 'prefix_bar', 'prefix_baz', np.nan])\n", (65774, 65826), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((65835, 65872), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (65854, 65872), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((65924, 65982), 'pandas.Series', 'Series', (["['foo_suffix', 'bar_suffix', 'baz_suffix', np.nan]"], {}), "(['foo_suffix', 'bar_suffix', 'baz_suffix', np.nan])\n", (65930, 65982), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((65991, 66028), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (66010, 66028), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((66250, 66285), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['expected', 's'], {}), '(expected, s)\n', (66272, 66285), True, 'import pandas.util.testing as tm\n'), ((66358, 66393), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['expected', 's'], {}), '(expected, s)\n', (66380, 66393), True, 'import pandas.util.testing as tm\n'), ((66604, 66641), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (66623, 66641), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((66905, 66942), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (66924, 66942), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((66993, 67030), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (67012, 67030), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((67163, 67184), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130102"""'], {}), "('20130102')\n", (67172, 67184), False, 'from pandas.tseries.index import Timestamp\n'), ((67297, 67365), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (["df['result']", "df['expected']"], {'check_names': '(False)'}), "(df['result'], df['expected'], check_names=False)\n", (67316, 67365), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((67424, 67494), 'pandas.DatetimeIndex', 'pd.DatetimeIndex', (["['2011-01-01', '2011-01-02', '2011-01-03']"], {'tz': '"""UTC"""'}), "(['2011-01-01', '2011-01-02', '2011-01-03'], tz='UTC')\n", (67440, 67494), True, 'import pandas as pd\n'), ((973, 1002), 'numpy.errstate', 'np.errstate', ([], {'invalid': '"""ignore"""'}), "(invalid='ignore')\n", (984, 1002), True, 'import numpy as np\n'), ((2364, 2384), 'pandas.util.testing.makeFloatSeries', 'tm.makeFloatSeries', ([], {}), '()\n', (2382, 2384), True, 'import pandas.util.testing as tm\n'), ((2386, 2406), 'pandas.util.testing.makeFloatSeries', 'tm.makeFloatSeries', ([], {}), '()\n', (2404, 2406), True, 'import pandas.util.testing as tm\n'), ((2647, 2672), 'numpy.errstate', 'np.errstate', ([], {'all': '"""ignore"""'}), "(all='ignore')\n", (2658, 2672), True, 'import numpy as np\n'), ((2764, 2822), 'pandas.DataFrame', 'DataFrame', (["{'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}"], {}), "({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})\n", (2773, 2822), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((3055, 3092), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (3074, 3092), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((3153, 3196), 'pandas.Series', 'Series', (['np.inf'], {'index': 'p.index', 'name': '"""first"""'}), "(np.inf, index=p.index, name='first')\n", (3159, 3196), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((3209, 3246), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (3228, 3246), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((3353, 3399), 'pandas.Series', 'Series', (["(p['first'].values / p['second'].values)"], {}), "(p['first'].values / p['second'].values)\n", (3359, 3399), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((3412, 3449), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (3431, 3449), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((3467, 3525), 'pandas.DataFrame', 'DataFrame', (["{'first': [3, 4, 5, 8], 'second': [1, 1, 1, 1]}"], {}), "({'first': [3, 4, 5, 8], 'second': [1, 1, 1, 1]})\n", (3476, 3525), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((3864, 3891), 'pandas.Series', 'Series', (['[np.nan, 1.0, -1.0]'], {}), '([np.nan, 1.0, -1.0])\n', (3870, 3891), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((3940, 3973), 'pandas.Series', 'Series', (['[np.nan, np.inf, -np.inf]'], {}), '([np.nan, np.inf, -np.inf])\n', (3946, 3973), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((3986, 4023), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (4005, 4023), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((4097, 4151), 'pandas.DataFrame', 'DataFrame', (["{'first': (1, 0), 'second': (-0.01, -0.02)}"], {}), "({'first': (1, 0), 'second': (-0.01, -0.02)})\n", (4106, 4151), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((4175, 4199), 'pandas.Series', 'Series', (['[-0.01, -np.inf]'], {}), '([-0.01, -np.inf])\n', (4181, 4199), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((4262, 4318), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {'check_names': '(False)'}), '(result, expected, check_names=False)\n', (4281, 4318), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((4378, 4415), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (4397, 4415), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((4455, 4473), 'pandas.Series', 'Series', (['[-1, 0, 1]'], {}), '([-1, 0, 1])\n', (4461, 4473), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((4525, 4548), 'pandas.Series', 'Series', (['[0.0, nan, 0.0]'], {}), '([0.0, nan, 0.0])\n', (4531, 4548), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((4561, 4598), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (4580, 4598), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((4650, 4674), 'pandas.Series', 'Series', (['[-inf, nan, inf]'], {}), '([-inf, nan, inf])\n', (4656, 4674), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((4687, 4724), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (4706, 4724), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((4777, 4801), 'pandas.Series', 'Series', (['[-inf, nan, inf]'], {}), '([-inf, nan, inf])\n', (4783, 4801), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((4814, 4851), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (4833, 4851), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((5200, 5272), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['cython_or_numpy', 'python'], {'check_dtype': 'check_dtype'}), '(cython_or_numpy, python, check_dtype=check_dtype)\n', (5222, 5272), True, 'import pandas.util.testing as tm\n'), ((6976, 7005), 'pandas.Series', 'Series', (['[np.nan]'], {'index': "['x']"}), "([np.nan], index=['x'])\n", (6982, 7005), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((7224, 7251), 'numpy.array', 'np.array', (['(1)'], {'dtype': 'np.int64'}), '(1, dtype=np.int64)\n', (7232, 7251), True, 'import numpy as np\n'), ((7404, 7431), 'numpy.array', 'np.array', (['(1)'], {'dtype': 'np.int64'}), '(1, dtype=np.int64)\n', (7412, 7431), True, 'import numpy as np\n'), ((7583, 7593), 'pandas.Series', 'Series', (['v2'], {}), '(v2)\n', (7589, 7593), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((7596, 7606), 'pandas.Series', 'Series', (['v1'], {}), '(v1)\n', (7602, 7606), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((8457, 8478), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20111230"""'], {}), "('20111230')\n", (8466, 8478), False, 'from pandas.tseries.index import Timestamp\n'), ((8480, 8501), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20120101"""'], {}), "('20120101')\n", (8489, 8501), False, 'from pandas.tseries.index import Timestamp\n'), ((8521, 8542), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20120103"""'], {}), "('20120103')\n", (8530, 8542), False, 'from pandas.tseries.index import Timestamp\n'), ((8689, 8709), 'datetime.datetime', 'datetime', (['(2001)', '(1)', '(1)'], {}), '(2001, 1, 1)\n', (8697, 8709), False, 'from datetime import datetime, timedelta\n'), ((9682, 9713), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(1)'}), '(minutes=5, seconds=1)\n', (9691, 9713), False, 'from datetime import datetime, timedelta\n'), ((9847, 9860), 'numpy.float64', 'np.float64', (['(0)'], {}), '(0)\n', (9857, 9860), True, 'import numpy as np\n'), ((9863, 9883), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {}), '([1, 2, 3])\n', (9872, 9883), True, 'import pandas as pd\n'), ((9909, 9929), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {}), '([1, 2, 3])\n', (9918, 9929), True, 'import pandas as pd\n'), ((9998, 10018), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {}), '([1, 2, 3])\n', (10007, 10018), True, 'import pandas as pd\n'), ((10021, 10034), 'numpy.float64', 'np.float64', (['(0)'], {}), '(0)\n', (10031, 10034), True, 'import numpy as np\n'), ((10054, 10074), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {}), '([1, 2, 3])\n', (10063, 10074), True, 'import pandas as pd\n'), ((10174, 10194), 'pandas.Series', 'pd.Series', (['[0, 1, 2]'], {}), '([0, 1, 2])\n', (10183, 10194), True, 'import pandas as pd\n'), ((10220, 10240), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {}), '([1, 2, 3])\n', (10229, 10240), True, 'import pandas as pd\n'), ((10492, 10512), 'pandas.offsets.Second', 'pd.offsets.Second', (['(5)'], {}), '(5)\n', (10509, 10512), True, 'import pandas as pd\n'), ((10531, 10551), 'pandas.offsets.Second', 'pd.offsets.Second', (['(5)'], {}), '(5)\n', (10548, 10551), True, 'import pandas as pd\n'), ((10774, 10794), 'pandas.offsets.Second', 'pd.offsets.Second', (['(5)'], {}), '(5)\n', (10791, 10794), True, 'import pandas as pd\n'), ((11057, 11076), 'pandas.offsets.Milli', 'pd.offsets.Milli', (['(5)'], {}), '(5)\n', (11073, 11076), True, 'import pandas as pd\n'), ((11095, 11114), 'pandas.offsets.Milli', 'pd.offsets.Milli', (['(5)'], {}), '(5)\n', (11111, 11114), True, 'import pandas as pd\n'), ((11368, 11387), 'pandas.offsets.Milli', 'pd.offsets.Milli', (['(5)'], {}), '(5)\n', (11384, 11387), True, 'import pandas as pd\n'), ((11615, 11637), 'numpy.timedelta64', 'np.timedelta64', (['(1)', '"""s"""'], {}), "(1, 's')\n", (11629, 11637), True, 'import numpy as np\n'), ((11656, 11678), 'numpy.timedelta64', 'np.timedelta64', (['(1)', '"""s"""'], {}), "(1, 's')\n", (11670, 11678), True, 'import numpy as np\n'), ((11901, 11924), 'numpy.timedelta64', 'np.timedelta64', (['(5)', '"""ms"""'], {}), "(5, 'ms')\n", (11915, 11924), True, 'import numpy as np\n'), ((11943, 11966), 'numpy.timedelta64', 'np.timedelta64', (['(5)', '"""ms"""'], {}), "(5, 'ms')\n", (11957, 11966), True, 'import numpy as np\n'), ((12469, 12504), 'pandas.timedelta_range', 'timedelta_range', (['"""1 day"""'], {'periods': '(3)'}), "('1 day', periods=3)\n", (12484, 12504), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((12569, 12604), 'pandas.date_range', 'date_range', (['"""2012-01-02"""'], {'periods': '(3)'}), "('2012-01-02', periods=3)\n", (12579, 12604), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((12726, 12773), 'pandas.date_range', 'date_range', (['"""2011-12-31"""'], {'periods': '(3)', 'freq': '"""-1D"""'}), "('2011-12-31', periods=3, freq='-1D')\n", (12736, 12773), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((13032, 13052), 'pandas.offsets.Minute', 'pd.offsets.Minute', (['(1)'], {}), '(1)\n', (13049, 13052), True, 'import pandas as pd\n'), ((13187, 13207), 'pandas.offsets.Minute', 'pd.offsets.Minute', (['(1)'], {}), '(1)\n', (13204, 13207), True, 'import pandas as pd\n'), ((13686, 13707), 'pandas.offsets.Second', 'pd.offsets.Second', (['(12)'], {}), '(12)\n', (13703, 13707), True, 'import pandas as pd\n'), ((14891, 14924), 'pandas.to_timedelta', 'pd.to_timedelta', (["(['00:05:03'] * 3)"], {}), "(['00:05:03'] * 3)\n", (14906, 14924), True, 'import pandas as pd\n'), ((15586, 15624), 'pandas.date_range', 'date_range', (['"""2013-01-01"""', '"""2013-01-03"""'], {}), "('2013-01-01', '2013-01-03')\n", (15596, 15624), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((15651, 15689), 'pandas.date_range', 'date_range', (['"""2013-03-01"""', '"""2013-03-03"""'], {}), "('2013-03-01', '2013-03-03')\n", (15661, 15689), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((16678, 16711), 'pandas.Series', 'Series', (['[20, 30, 40]'], {'dtype': 'dtype'}), '([20, 30, 40], dtype=dtype)\n', (16684, 16711), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((16915, 16952), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (16934, 16952), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((18016, 18054), 'pandas.date_range', 'date_range', (['"""2013-01-01"""', '"""2013-01-03"""'], {}), "('2013-01-01', '2013-01-03')\n", (18026, 18054), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((18081, 18119), 'pandas.date_range', 'date_range', (['"""2013-03-01"""', '"""2013-03-03"""'], {}), "('2013-03-01', '2013-03-03')\n", (18091, 18119), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((18910, 18943), 'pandas.date_range', 'date_range', (['"""20130101"""'], {'periods': '(3)'}), "('20130101', periods=3)\n", (18920, 18943), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((20095, 20160), 'datetime.timedelta', 'timedelta', ([], {'days': 'd', 'hours': 'h', 'minutes': 'm', 'seconds': 's', 'microseconds': 'us'}), '(days=d, hours=h, minutes=m, seconds=s, microseconds=us)\n', (20104, 20160), False, 'from datetime import datetime, timedelta\n'), ((22938, 22989), 'pandas.date_range', 'date_range', (['"""2000-01-01 09:00:00"""'], {'periods': '(5)', 'tz': 'tz'}), "('2000-01-01 09:00:00', periods=5, tz=tz)\n", (22948, 22989), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((23110, 23162), 'pandas.timedelta_range', 'timedelta_range', (['"""1 days 1 min"""'], {'periods': '(5)', 'freq': '"""H"""'}), "('1 days 1 min', periods=5, freq='H')\n", (23125, 23162), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((23740, 23772), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (23759, 23772), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((23893, 23925), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (23912, 23925), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((26691, 26717), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (26706, 26717), True, 'import pandas.util.testing as tm\n'), ((27624, 27650), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (27639, 27650), True, 'import pandas.util.testing as tm\n'), ((28074, 28100), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (28089, 28100), True, 'import pandas.util.testing as tm\n'), ((32046, 32072), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (32061, 32072), True, 'import pandas.util.testing as tm\n'), ((32119, 32145), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (32134, 32145), True, 'import pandas.util.testing as tm\n'), ((32203, 32229), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (32218, 32229), True, 'import pandas.util.testing as tm\n'), ((32278, 32304), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (32293, 32304), True, 'import pandas.util.testing as tm\n'), ((32690, 32716), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (32705, 32716), True, 'import pandas.util.testing as tm\n'), ((32776, 32802), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (32791, 32802), True, 'import pandas.util.testing as tm\n'), ((32970, 33013), 'pandas.date_range', 'date_range', (['"""2012-1-1"""'], {'periods': '(3)', 'freq': '"""D"""'}), "('2012-1-1', periods=3, freq='D')\n", (32980, 33013), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((34950, 34985), 'pandas.bdate_range', 'bdate_range', (['"""1/1/2000"""'], {'periods': '(10)'}), "('1/1/2000', periods=10)\n", (34961, 34985), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((35474, 35511), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (35493, 35511), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((36102, 36139), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (36121, 36139), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((36210, 36229), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (36225, 36229), True, 'import numpy as np\n'), ((36500, 36537), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (36519, 36537), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((36654, 36662), 'pandas.compat.range', 'range', (['(5)'], {}), '(5)\n', (36659, 36662), False, 'from pandas.compat import range, zip\n'), ((36684, 36717), 'pandas.date_range', 'date_range', (['"""20010101"""'], {'periods': '(5)'}), "('20010101', periods=5)\n", (36694, 36717), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((37199, 37238), 'pandas.Series', 'Series', (["['a', np.nan, 'c']"], {'dtype': 'dtype'}), "(['a', np.nan, 'c'], dtype=dtype)\n", (37205, 37238), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((37259, 37298), 'pandas.Series', 'Series', (["['a', np.nan, 'd']"], {'dtype': 'dtype'}), "(['a', np.nan, 'd'], dtype=dtype)\n", (37265, 37298), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((37358, 37386), 'pandas.Series', 'Series', (['[True, False, False]'], {}), '([True, False, False])\n', (37364, 37386), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((37399, 37436), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (37418, 37436), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((37496, 37523), 'pandas.Series', 'Series', (['[False, True, True]'], {}), '([False, True, True])\n', (37502, 37523), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((37536, 37573), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (37555, 37573), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((37634, 37663), 'pandas.Series', 'Series', (['[False, False, False]'], {}), '([False, False, False])\n', (37640, 37663), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((37676, 37713), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (37695, 37713), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((37774, 37800), 'pandas.Series', 'Series', (['[True, True, True]'], {}), '([True, True, True])\n', (37780, 37800), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((37813, 37850), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (37832, 37850), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((42159, 42169), 'pandas.Series', 'Series', (['[]'], {}), '([])\n', (42165, 42169), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((42300, 42310), 'pandas.Series', 'Series', (['[]'], {}), '([])\n', (42306, 42310), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((42465, 42483), 'pandas.Series', 'Series', (['[1]', "['z']"], {}), "([1], ['z'])\n", (42471, 42483), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((42622, 42640), 'pandas.Series', 'Series', (['[1]', "['z']"], {}), "([1], ['z'])\n", (42628, 42640), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((42869, 42879), 'pandas.Series', 'Series', (['[]'], {}), '([])\n', (42875, 42879), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((42881, 42899), 'pandas.Series', 'Series', (['[1]', "['z']"], {}), "([1], ['z'])\n", (42887, 42899), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((42919, 42942), 'pandas.Series', 'Series', (['np.nan', 'b.index'], {}), '(np.nan, b.index)\n', (42925, 42942), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((42944, 42967), 'pandas.Series', 'Series', (['np.nan', 'a.index'], {}), '(np.nan, a.index)\n', (42950, 42967), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((43012, 43045), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'a[a]'], {}), '(result, a[a])\n', (43031, 43045), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((43065, 43078), 'pandas.Series', 'Series', (["['z']"], {}), "(['z'])\n", (43071, 43078), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((43275, 43308), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'a[a]'], {}), '(result, a[a])\n', (43294, 43308), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((43520, 43559), 'pandas.Series', 'Series', (['[True, True, True]'], {'index': 'index'}), '([True, True, True], index=index)\n', (43526, 43559), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((43572, 43609), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (43591, 43609), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((43820, 43860), 'pandas.Series', 'Series', (['[True, False, True]'], {'index': 'index'}), '([True, False, True], index=index)\n', (43826, 43860), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((43873, 43910), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (43892, 43910), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((44029, 44069), 'pandas.Series', 'Series', (['[True, False, True]'], {'index': 'index'}), '([True, False, True], index=index)\n', (44035, 44069), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((44082, 44119), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (44101, 44119), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((44239, 44281), 'pandas.Series', 'Series', (['[False, False, False]'], {'index': 'index'}), '([False, False, False], index=index)\n', (44245, 44281), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((44294, 44331), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (44313, 44331), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((44483, 44502), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (44498, 44502), True, 'import numpy as np\n'), ((44530, 44549), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (44545, 44549), True, 'import numpy as np\n'), ((47972, 47980), 'pandas.compat.range', 'range', (['(4)'], {}), '(4)\n', (47977, 47980), False, 'from pandas.compat import range, zip\n'), ((48318, 48326), 'pandas.compat.range', 'range', (['(4)'], {}), '(4)\n', (48323, 48326), False, 'from pandas.compat import range, zip\n'), ((48443, 48454), 'pandas.compat.range', 'range', (['(4)', '(8)'], {}), '(4, 8)\n', (48448, 48454), False, 'from pandas.compat import range, zip\n'), ((50077, 50131), 'pandas.Series', 'Series', (['([False] * 7)'], {'index': "['b', 'c', 'a', 0, 1, 2, 3]"}), "([False] * 7, index=['b', 'c', 'a', 0, 1, 2, 3])\n", (50083, 50131), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((50282, 50336), 'pandas.Series', 'Series', (['([False] * 7)'], {'index': "[0, 1, 2, 3, 'a', 'b', 'c']"}), "([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])\n", (50288, 50336), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((50349, 50389), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(s_tft & s_0123)', 'exp'], {}), '(s_tft & s_0123, exp)\n', (50368, 50389), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((50565, 50619), 'pandas.Series', 'Series', (['([False] * 7)'], {'index': "[0, 1, 2, 3, 'b', 'c', 'a']"}), "([False] * 7, index=[0, 1, 2, 3, 'b', 'c', 'a'])\n", (50571, 50619), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((50770, 50824), 'pandas.Series', 'Series', (['([False] * 7)'], {'index': "[0, 1, 2, 3, 'a', 'b', 'c']"}), "([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])\n", (50776, 50824), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((50837, 50877), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(s_0123 & s_tft)', 'exp'], {}), '(s_0123 & s_tft, exp)\n', (50856, 50877), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((50923, 50942), 'pandas.Series', 'Series', (['([False] * 4)'], {}), '([False] * 4)\n', (50929, 50942), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((50988, 51021), 'pandas.Series', 'Series', (['[False, True, True, True]'], {}), '([False, True, True, True])\n', (50994, 51021), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51069, 51088), 'pandas.Series', 'Series', (['([False] * 4)'], {}), '([False] * 4)\n', (51075, 51088), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51136, 51155), 'pandas.Series', 'Series', (['([False] * 4)'], {}), '([False] * 4)\n', (51142, 51155), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51261, 51280), 'pandas.Series', 'Series', (['([False] * 4)'], {}), '([False] * 4)\n', (51267, 51280), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51750, 51770), 'datetime.datetime', 'datetime', (['(2005)', '(1)', '(1)'], {}), '(2005, 1, 1)\n', (51758, 51770), False, 'from datetime import datetime, timedelta\n'), ((53420, 53439), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (53435, 53439), True, 'import numpy as np\n'), ((58783, 58804), 'pandas.util.testing.rands_array', 'tm.rands_array', (['(5)', '(10)'], {}), '(5, 10)\n', (58797, 58804), True, 'import pandas.util.testing as tm\n'), ((59174, 59200), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (59189, 59200), True, 'import pandas.util.testing as tm\n'), ((59253, 59279), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (59268, 59279), True, 'import pandas.util.testing as tm\n'), ((59846, 59879), 'pandas.Series', 'pd.Series', (['[2, 3, 4]'], {'dtype': 'dtype'}), '([2, 3, 4], dtype=dtype)\n', (59855, 59879), True, 'import pandas as pd\n'), ((59892, 59924), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['res', 'exp'], {}), '(res, exp)\n', (59914, 59924), True, 'import pandas.util.testing as tm\n'), ((59993, 60025), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['res', 'exp'], {}), '(res, exp)\n', (60015, 60025), True, 'import pandas.util.testing as tm\n'), ((60106, 60154), 'pandas.Series', 'pd.Series', (['[np.nan, np.nan, np.nan]'], {'dtype': 'dtype'}), '([np.nan, np.nan, np.nan], dtype=dtype)\n', (60115, 60154), True, 'import pandas as pd\n'), ((60167, 60199), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['res', 'exp'], {}), '(res, exp)\n', (60189, 60199), True, 'import pandas.util.testing as tm\n'), ((60273, 60305), 'pandas.util.testing.assert_series_equal', 'tm.assert_series_equal', (['res', 'exp'], {}), '(res, exp)\n', (60295, 60305), True, 'import pandas.util.testing as tm\n'), ((60797, 60828), 'pandas.Series', 'pd.Series', (["['ax', np.nan, 'ax']"], {}), "(['ax', np.nan, 'ax'])\n", (60806, 60828), True, 'import pandas as pd\n'), ((60870, 60901), 'pandas.Series', 'pd.Series', (["['xa', np.nan, 'xa']"], {}), "(['xa', np.nan, 'xa'])\n", (60879, 60901), True, 'import pandas as pd\n'), ((61436, 61472), 'pandas.DataFrame', 'pd.DataFrame', (['[2, 3, 4]'], {'dtype': 'dtype'}), '([2, 3, 4], dtype=dtype)\n', (61448, 61472), True, 'import pandas as pd\n'), ((61485, 61516), 'pandas.util.testing.assert_frame_equal', 'tm.assert_frame_equal', (['res', 'exp'], {}), '(res, exp)\n', (61506, 61516), True, 'import pandas.util.testing as tm\n'), ((61588, 61619), 'pandas.util.testing.assert_frame_equal', 'tm.assert_frame_equal', (['res', 'exp'], {}), '(res, exp)\n', (61609, 61619), True, 'import pandas.util.testing as tm\n'), ((61703, 61754), 'pandas.DataFrame', 'pd.DataFrame', (['[np.nan, np.nan, np.nan]'], {'dtype': 'dtype'}), '([np.nan, np.nan, np.nan], dtype=dtype)\n', (61715, 61754), True, 'import pandas as pd\n'), ((61767, 61798), 'pandas.util.testing.assert_frame_equal', 'tm.assert_frame_equal', (['res', 'exp'], {}), '(res, exp)\n', (61788, 61798), True, 'import pandas.util.testing as tm\n'), ((61875, 61906), 'pandas.util.testing.assert_frame_equal', 'tm.assert_frame_equal', (['res', 'exp'], {}), '(res, exp)\n', (61896, 61906), True, 'import pandas.util.testing as tm\n'), ((61994, 62028), 'pandas.DataFrame', 'pd.DataFrame', (["['ax', np.nan, 'ax']"], {}), "(['ax', np.nan, 'ax'])\n", (62006, 62028), True, 'import pandas as pd\n'), ((62070, 62104), 'pandas.DataFrame', 'pd.DataFrame', (["['xa', np.nan, 'xa']"], {}), "(['xa', np.nan, 'xa'])\n", (62082, 62104), True, 'import pandas as pd\n'), ((62941, 62950), 'pandas.isnull', 'isnull', (['a'], {}), '(a)\n', (62947, 62950), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((62971, 62980), 'pandas.isnull', 'isnull', (['b'], {}), '(b)\n', (62977, 62980), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((63711, 63740), 'pandas.Series', 'Series', (['exp_values', 'exp_index'], {}), '(exp_values, exp_index)\n', (63717, 63740), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((63753, 63790), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (63772, 63790), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((64835, 64867), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'exp'], {}), '(result, exp)\n', (64854, 64867), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((65172, 65213), 'pandas.util.testing.equalContents', 'tm.equalContents', (['(ts.index != 5)', 'expected'], {}), '(ts.index != 5, expected)\n', (65188, 65213), True, 'import pandas.util.testing as tm\n'), ((65239, 65283), 'pandas.util.testing.equalContents', 'tm.equalContents', (['(~(ts.index == 5))', 'expected'], {}), '(~(ts.index == 5), expected)\n', (65255, 65283), True, 'import pandas.util.testing as tm\n'), ((65604, 65621), 'pandas.isnull', 'isnull', (['result[0]'], {}), '(result[0])\n', (65610, 65621), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((65647, 65665), 'pandas.isnull', 'isnull', (['result2[0]'], {}), '(result2[0])\n', (65653, 65665), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((66230, 66240), 'decimal.Decimal', 'Decimal', (['(2)'], {}), '(2)\n', (66237, 66240), False, 'from decimal import Decimal\n'), ((66338, 66348), 'decimal.Decimal', 'Decimal', (['(2)'], {}), '(2)\n', (66345, 66348), False, 'from decimal import Decimal\n'), ((66504, 66522), 'numpy.random.randn', 'np.random.randn', (['(5)'], {}), '(5)\n', (66519, 66522), True, 'import numpy as np\n'), ((66730, 66763), 'pandas.date_range', 'date_range', (['"""20130101"""'], {'periods': '(5)'}), "('20130101', periods=5)\n", (66740, 66763), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((67055, 67076), 'numpy.random.randn', 'np.random.randn', (['(5)', '(2)'], {}), '(5, 2)\n', (67070, 67076), True, 'import numpy as np\n'), ((67640, 67666), 'pandas.Series', 'Series', (['[1, 2]'], {'index': 'idx1'}), '([1, 2], index=idx1)\n', (67646, 67666), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((67669, 67695), 'pandas.Series', 'Series', (['[1, 1]'], {'index': 'idx2'}), '([1, 1], index=idx2)\n', (67675, 67695), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((67729, 67768), 'pandas.Series', 'Series', (['[np.nan, 3, np.nan]'], {'index': 'base'}), '([np.nan, 3, np.nan], index=base)\n', (67735, 67768), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((1940, 1980), 'pandas.util.testing.assert_almost_equal', 'tm.assert_almost_equal', (['result', 'expected'], {}), '(result, expected)\n', (1962, 1980), True, 'import pandas.util.testing as tm\n'), ((3771, 3819), 'numpy.array_equal', 'np.array_equal', (['result', "(p['second'] / p['first'])"], {}), "(result, p['second'] / p['first'])\n", (3785, 3819), True, 'import numpy as np\n'), ((5000, 5014), 'numpy.abs', 'np.abs', (['series'], {}), '(series)\n', (5006, 5014), True, 'import numpy as np\n'), ((5059, 5072), 'numpy.abs', 'np.abs', (['other'], {}), '(other)\n', (5065, 5072), True, 'import numpy as np\n'), ((7863, 7880), 'datetime.timedelta', 'timedelta', ([], {'days': 'i'}), '(days=i)\n', (7872, 7880), False, 'from datetime import datetime, timedelta\n'), ((8750, 8774), 'datetime.timedelta', 'timedelta', ([], {'days': '(4017 + i)'}), '(days=4017 + i)\n', (8759, 8774), False, 'from datetime import datetime, timedelta\n'), ((9617, 9648), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(1)'}), '(minutes=5, seconds=1)\n', (9626, 9648), False, 'from datetime import datetime, timedelta\n'), ((10149, 10168), 'numpy.array', 'np.array', (['[0, 1, 2]'], {}), '([0, 1, 2])\n', (10157, 10168), True, 'import numpy as np\n'), ((10413, 10439), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:01"""'], {}), "('20130101 9:01')\n", (10422, 10439), False, 'from pandas.tseries.index import Timestamp\n'), ((10441, 10467), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:02"""'], {}), "('20130101 9:02')\n", (10450, 10467), False, 'from pandas.tseries.index import Timestamp\n'), ((10583, 10612), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:01:05"""'], {}), "('20130101 9:01:05')\n", (10592, 10612), False, 'from pandas.tseries.index import Timestamp\n'), ((10614, 10643), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:02:05"""'], {}), "('20130101 9:02:05')\n", (10623, 10643), False, 'from pandas.tseries.index import Timestamp\n'), ((10814, 10834), 'pandas.offsets.Second', 'pd.offsets.Second', (['(5)'], {}), '(5)\n', (10831, 10834), True, 'import pandas as pd\n'), ((10866, 10895), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:00:55"""'], {}), "('20130101 9:00:55')\n", (10875, 10895), False, 'from pandas.tseries.index import Timestamp\n'), ((10897, 10926), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:01:55"""'], {}), "('20130101 9:01:55')\n", (10906, 10926), False, 'from pandas.tseries.index import Timestamp\n'), ((11146, 11179), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:01:00.005"""'], {}), "('20130101 9:01:00.005')\n", (11155, 11179), False, 'from pandas.tseries.index import Timestamp\n'), ((11181, 11214), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:02:00.005"""'], {}), "('20130101 9:02:00.005')\n", (11190, 11214), False, 'from pandas.tseries.index import Timestamp\n'), ((11345, 11365), 'pandas.offsets.Minute', 'pd.offsets.Minute', (['(5)'], {}), '(5)\n', (11362, 11365), True, 'import pandas as pd\n'), ((11415, 11448), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:06:00.005"""'], {}), "('20130101 9:06:00.005')\n", (11424, 11448), False, 'from pandas.tseries.index import Timestamp\n'), ((11450, 11483), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:07:00.005"""'], {}), "('20130101 9:07:00.005')\n", (11459, 11483), False, 'from pandas.tseries.index import Timestamp\n'), ((11710, 11739), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:01:01"""'], {}), "('20130101 9:01:01')\n", (11719, 11739), False, 'from pandas.tseries.index import Timestamp\n'), ((11741, 11770), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:02:01"""'], {}), "('20130101 9:02:01')\n", (11750, 11770), False, 'from pandas.tseries.index import Timestamp\n'), ((11998, 12031), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:01:00.005"""'], {}), "('20130101 9:01:00.005')\n", (12007, 12031), False, 'from pandas.tseries.index import Timestamp\n'), ((12033, 12066), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130101 9:02:00.005"""'], {}), "('20130101 9:02:00.005')\n", (12042, 12066), False, 'from pandas.tseries.index import Timestamp\n'), ((13472, 13503), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(6)', 'seconds': '(3)'}), '(minutes=6, seconds=3)\n', (13481, 13503), False, 'from datetime import datetime, timedelta\n'), ((13505, 13536), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(6)'}), '(minutes=5, seconds=6)\n', (13514, 13536), False, 'from datetime import datetime, timedelta\n'), ((13551, 13591), 'datetime.timedelta', 'timedelta', ([], {'hours': '(2)', 'minutes': '(5)', 'seconds': '(3)'}), '(hours=2, minutes=5, seconds=3)\n', (13560, 13591), False, 'from datetime import datetime, timedelta\n'), ((13663, 13683), 'pandas.offsets.Minute', 'pd.offsets.Minute', (['(1)'], {}), '(1)\n', (13680, 13683), True, 'import pandas as pd\n'), ((18439, 18476), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (18458, 18476), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((19317, 19338), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130301"""'], {}), "('20130301')\n", (19326, 19338), False, 'from pandas.tseries.index import Timestamp\n'), ((19340, 19370), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130228 23:00:00"""'], {}), "('20130228 23:00:00')\n", (19349, 19370), False, 'from pandas.tseries.index import Timestamp\n'), ((19394, 19424), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130228 22:00:00"""'], {}), "('20130228 22:00:00')\n", (19403, 19424), False, 'from pandas.tseries.index import Timestamp\n'), ((19426, 19456), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20130228 21:00:00"""'], {}), "('20130228 21:00:00')\n", (19435, 19456), False, 'from pandas.tseries.index import Timestamp\n'), ((20288, 20317), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['lhs', 'rhs'], {}), '(lhs, rhs)\n', (20307, 20317), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((21476, 21497), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20111230"""'], {}), "('20111230')\n", (21485, 21497), False, 'from pandas.tseries.index import Timestamp\n'), ((21499, 21520), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20120101"""'], {}), "('20120101')\n", (21508, 21520), False, 'from pandas.tseries.index import Timestamp\n'), ((21544, 21565), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20120103"""'], {}), "('20120103')\n", (21553, 21565), False, 'from pandas.tseries.index import Timestamp\n'), ((21619, 21640), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20111231"""'], {}), "('20111231')\n", (21628, 21640), False, 'from pandas.tseries.index import Timestamp\n'), ((21642, 21663), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20120102"""'], {}), "('20120102')\n", (21651, 21663), False, 'from pandas.tseries.index import Timestamp\n'), ((21687, 21708), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""20120104"""'], {}), "('20120104')\n", (21696, 21708), False, 'from pandas.tseries.index import Timestamp\n'), ((25053, 25103), 'pandas.Timestamp', 'pd.Timestamp', (['"""2016-02-10"""'], {'tz': '"""America/Sao_Paulo"""'}), "('2016-02-10', tz='America/Sao_Paulo')\n", (25065, 25103), True, 'import pandas as pd\n'), ((25127, 25177), 'pandas.Timestamp', 'pd.Timestamp', (['"""2016-02-08"""'], {'tz': '"""America/Sao_Paulo"""'}), "('2016-02-08', tz='America/Sao_Paulo')\n", (25139, 25177), True, 'import pandas as pd\n'), ((25232, 25250), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""2days"""'], {}), "('2days')\n", (25241, 25250), False, 'from pandas.tseries.tdi import Timedelta\n'), ((25351, 25370), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""-2days"""'], {}), "('-2days')\n", (25360, 25370), False, 'from pandas.tseries.tdi import Timedelta\n'), ((25507, 25522), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""1s"""'], {}), "('1s')\n", (25516, 25522), False, 'from pandas.tseries.tdi import Timedelta\n'), ((25564, 25585), 'pandas.tseries.index.Timestamp', 'Timestamp', (['"""19900315"""'], {}), "('19900315')\n", (25573, 25585), False, 'from pandas.tseries.index import Timestamp\n'), ((33094, 33106), 'datetime.timedelta', 'timedelta', (['(0)'], {}), '(0)\n', (33103, 33106), False, 'from datetime import datetime, timedelta\n'), ((33108, 33120), 'datetime.timedelta', 'timedelta', (['(0)'], {}), '(0)\n', (33117, 33120), False, 'from datetime import datetime, timedelta\n'), ((38563, 38585), 'pandas.Series', 'Series', (['l'], {'dtype': 'dtype'}), '(l, dtype=dtype)\n', (38569, 38585), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((39512, 39538), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-01"""'], {}), "('2011-01-01')\n", (39524, 39538), True, 'import pandas as pd\n'), ((39565, 39591), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-03"""'], {}), "('2011-01-03')\n", (39577, 39591), True, 'import pandas as pd\n'), ((39612, 39634), 'pandas.Timedelta', 'pd.Timedelta', (['"""1 days"""'], {}), "('1 days')\n", (39624, 39634), True, 'import pandas as pd\n'), ((39644, 39666), 'pandas.Timedelta', 'pd.Timedelta', (['"""3 days"""'], {}), "('3 days')\n", (39656, 39666), True, 'import pandas as pd\n'), ((39687, 39717), 'pandas.Period', 'pd.Period', (['"""2011-01"""'], {'freq': '"""M"""'}), "('2011-01', freq='M')\n", (39696, 39717), True, 'import pandas as pd\n'), ((39744, 39774), 'pandas.Period', 'pd.Period', (['"""2011-03"""'], {'freq': '"""M"""'}), "('2011-03', freq='M')\n", (39753, 39774), True, 'import pandas as pd\n'), ((39865, 39887), 'pandas.Series', 'Series', (['l'], {'dtype': 'dtype'}), '(l, dtype=dtype)\n', (39871, 39887), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((39916, 39945), 'pandas.Series', 'Series', (['[False, False, False]'], {}), '([False, False, False])\n', (39922, 39945), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((39962, 40007), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left == pd.NaT)', 'expected'], {}), '(left == pd.NaT, expected)\n', (39981, 40007), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40024, 40069), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(pd.NaT == left)', 'expected'], {}), '(pd.NaT == left, expected)\n', (40043, 40069), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40098, 40124), 'pandas.Series', 'Series', (['[True, True, True]'], {}), '([True, True, True])\n', (40104, 40124), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((40141, 40186), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left != pd.NaT)', 'expected'], {}), '(left != pd.NaT, expected)\n', (40160, 40186), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40203, 40248), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(pd.NaT != left)', 'expected'], {}), '(pd.NaT != left, expected)\n', (40222, 40248), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40277, 40306), 'pandas.Series', 'Series', (['[False, False, False]'], {}), '([False, False, False])\n', (40283, 40306), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((40323, 40367), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left < pd.NaT)', 'expected'], {}), '(left < pd.NaT, expected)\n', (40342, 40367), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40384, 40428), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(pd.NaT > left)', 'expected'], {}), '(pd.NaT > left, expected)\n', (40403, 40428), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40445, 40490), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left <= pd.NaT)', 'expected'], {}), '(left <= pd.NaT, expected)\n', (40464, 40490), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40507, 40552), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(pd.NaT >= left)', 'expected'], {}), '(pd.NaT >= left, expected)\n', (40526, 40552), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40570, 40614), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left > pd.NaT)', 'expected'], {}), '(left > pd.NaT, expected)\n', (40589, 40614), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40631, 40675), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(pd.NaT < left)', 'expected'], {}), '(pd.NaT < left, expected)\n', (40650, 40675), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40692, 40737), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left >= pd.NaT)', 'expected'], {}), '(left >= pd.NaT, expected)\n', (40711, 40737), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((40754, 40799), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(pd.NaT <= left)', 'expected'], {}), '(pd.NaT <= left, expected)\n', (40773, 40799), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((43452, 43492), 'pandas.Series', 'Series', (['[True, False, True]'], {'index': 'index'}), '([True, False, True], index=index)\n', (43458, 43492), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((43752, 43792), 'pandas.Series', 'Series', (['[True, False, True]'], {'index': 'index'}), '([True, False, True], index=index)\n', (43758, 43792), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((43961, 44001), 'pandas.Series', 'Series', (['[True, False, True]'], {'index': 'index'}), '([True, False, True], index=index)\n', (43967, 44001), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((44171, 44211), 'pandas.Series', 'Series', (['[True, False, True]'], {'index': 'index'}), '([True, False, True], index=index)\n', (44177, 44211), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((45570, 45608), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['ValueError', 'msg'], {}), '(ValueError, msg)\n', (45591, 45608), True, 'import pandas.util.testing as tm\n'), ((50149, 50191), 'pandas.util.testing.assert_produces_warning', 'tm.assert_produces_warning', (['RuntimeWarning'], {}), '(RuntimeWarning)\n', (50175, 50191), True, 'import pandas.util.testing as tm\n'), ((50209, 50249), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(s_tft & s_0123)', 'exp'], {}), '(s_tft & s_0123, exp)\n', (50228, 50249), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((50637, 50679), 'pandas.util.testing.assert_produces_warning', 'tm.assert_produces_warning', (['RuntimeWarning'], {}), '(RuntimeWarning)\n', (50663, 50679), True, 'import pandas.util.testing as tm\n'), ((50697, 50737), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(s_0123 & s_tft)', 'exp'], {}), '(s_0123 & s_tft, exp)\n', (50716, 50737), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((51194, 51231), 'pandas.Series', 'Series', (['[False, np.NaN, False, False]'], {}), '([False, np.NaN, False, False])\n', (51200, 51231), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51372, 51398), 'pandas.Series', 'Series', (['[0.1, 4, -3.14, 2]'], {}), '([0.1, 4, -3.14, 2])\n', (51378, 51398), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((51817, 51837), 'datetime.datetime', 'datetime', (['(2005)', '(1)', '(1)'], {}), '(2005, 1, 1)\n', (51825, 51837), False, 'from datetime import datetime, timedelta\n'), ((52633, 52642), 'pandas.Index', 'Index', (['[]'], {}), '([])\n', (52638, 52642), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((53447, 53460), 'numpy.arange', 'np.arange', (['(10)'], {}), '(10)\n', (53456, 53460), True, 'import numpy as np\n'), ((55521, 55559), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['ValueError', 'msg'], {}), '(ValueError, msg)\n', (55542, 55559), True, 'import pandas.util.testing as tm\n'), ((55602, 55640), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['ValueError', 'msg'], {}), '(ValueError, msg)\n', (55623, 55640), True, 'import pandas.util.testing as tm\n'), ((55683, 55721), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['ValueError', 'msg'], {}), '(ValueError, msg)\n', (55704, 55721), True, 'import pandas.util.testing as tm\n'), ((55838, 55876), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['ValueError', 'msg'], {}), '(ValueError, msg)\n', (55859, 55876), True, 'import pandas.util.testing as tm\n'), ((55941, 55979), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['ValueError', 'msg'], {}), '(ValueError, msg)\n', (55962, 55979), True, 'import pandas.util.testing as tm\n'), ((56044, 56082), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['ValueError', 'msg'], {}), '(ValueError, msg)\n', (56065, 56082), True, 'import pandas.util.testing as tm\n'), ((59214, 59228), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (59226, 59228), False, 'from datetime import datetime, timedelta\n'), ((59303, 59317), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (59315, 59317), False, 'from datetime import datetime, timedelta\n'), ((59433, 59459), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-01"""'], {}), "('2011-01-01')\n", (59445, 59459), True, 'import pandas as pd\n'), ((59461, 59487), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-02"""'], {}), "('2011-01-02')\n", (59473, 59487), True, 'import pandas as pd\n'), ((59631, 59653), 'pandas.Series', 'Series', (['d'], {'dtype': 'dtype'}), '(d, dtype=dtype)\n', (59637, 59653), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((59794, 59827), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (59803, 59827), True, 'import pandas as pd\n'), ((59943, 59976), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (59952, 59976), True, 'import pandas as pd\n'), ((60054, 60087), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (60063, 60087), True, 'import pandas as pd\n'), ((60218, 60251), 'pandas.Series', 'pd.Series', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (60227, 60251), True, 'import pandas as pd\n'), ((61017, 61043), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-01"""'], {}), "('2011-01-01')\n", (61029, 61043), True, 'import pandas as pd\n'), ((61045, 61071), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-02"""'], {}), "('2011-01-02')\n", (61057, 61071), True, 'import pandas as pd\n'), ((61215, 61240), 'pandas.DataFrame', 'DataFrame', (['d'], {'dtype': 'dtype'}), '(d, dtype=dtype)\n', (61224, 61240), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((61381, 61417), 'pandas.DataFrame', 'pd.DataFrame', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (61393, 61417), True, 'import pandas as pd\n'), ((61535, 61571), 'pandas.DataFrame', 'pd.DataFrame', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (61547, 61571), True, 'import pandas as pd\n'), ((61648, 61684), 'pandas.DataFrame', 'pd.DataFrame', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (61660, 61684), True, 'import pandas as pd\n'), ((61817, 61853), 'pandas.DataFrame', 'pd.DataFrame', (['[1, 2, 3]'], {'dtype': 'dtype'}), '([1, 2, 3], dtype=dtype)\n', (61829, 61853), True, 'import pandas as pd\n'), ((63841, 63853), 'numpy.arange', 'np.arange', (['(5)'], {}), '(5)\n', (63850, 63853), True, 'import numpy as np\n'), ((63907, 63919), 'numpy.arange', 'np.arange', (['(6)'], {}), '(6)\n', (63916, 63919), True, 'import numpy as np\n'), ((65418, 65432), 'decimal.Decimal', 'Decimal', (['"""1.3"""'], {}), "('1.3')\n", (65425, 65432), False, 'from decimal import Decimal\n'), ((65434, 65448), 'decimal.Decimal', 'Decimal', (['"""2.3"""'], {}), "('2.3')\n", (65441, 65448), False, 'from decimal import Decimal\n'), ((66166, 66176), 'decimal.Decimal', 'Decimal', (['(5)'], {}), '(5)\n', (66173, 66176), False, 'from decimal import Decimal\n'), ((66200, 66211), 'decimal.Decimal', 'Decimal', (['(10)'], {}), '(10)\n', (66207, 66211), False, 'from decimal import Decimal\n'), ((66307, 66318), 'decimal.Decimal', 'Decimal', (['(10)'], {}), '(10)\n', (66314, 66318), False, 'from decimal import Decimal\n'), ((66790, 66823), 'pandas.date_range', 'date_range', (['"""20130101"""'], {'periods': '(5)'}), "('20130101', periods=5)\n", (66800, 66823), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((67107, 67140), 'pandas.date_range', 'date_range', (['"""20130101"""'], {'periods': '(5)'}), "('20130101', periods=5)\n", (67117, 67140), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((2189, 2229), 'pandas.util.testing.assert_almost_equal', 'tm.assert_almost_equal', (['result', 'expected'], {}), '(result, expected)\n', (2211, 2229), True, 'import pandas.util.testing as tm\n'), ((5645, 5663), 'operator.add', 'operator.add', (['y', 'x'], {}), '(y, x)\n', (5657, 5663), False, 'import operator\n'), ((5715, 5733), 'operator.sub', 'operator.sub', (['y', 'x'], {}), '(y, x)\n', (5727, 5733), False, 'import operator\n'), ((5785, 5807), 'operator.truediv', 'operator.truediv', (['y', 'x'], {}), '(y, x)\n', (5801, 5807), False, 'import operator\n'), ((5859, 5882), 'operator.floordiv', 'operator.floordiv', (['y', 'x'], {}), '(y, x)\n', (5876, 5882), False, 'import operator\n'), ((5934, 5952), 'operator.mul', 'operator.mul', (['y', 'x'], {}), '(y, x)\n', (5946, 5952), False, 'import operator\n'), ((6004, 6022), 'operator.pow', 'operator.pow', (['y', 'x'], {}), '(y, x)\n', (6016, 6022), False, 'import operator\n'), ((6111, 6129), 'operator.mod', 'operator.mod', (['y', 'x'], {}), '(y, x)\n', (6123, 6129), False, 'import operator\n'), ((7890, 7898), 'pandas.compat.range', 'range', (['(3)'], {}), '(3)\n', (7895, 7898), False, 'from pandas.compat import range, zip\n'), ((8784, 8792), 'pandas.compat.range', 'range', (['(3)'], {}), '(3)\n', (8789, 8792), False, 'from pandas.compat import range, zip\n'), ((12972, 13003), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(3)'}), '(minutes=5, seconds=3)\n', (12981, 13003), False, 'from datetime import datetime, timedelta\n'), ((13080, 13111), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(6)', 'seconds': '(3)'}), '(minutes=6, seconds=3)\n', (13089, 13111), False, 'from datetime import datetime, timedelta\n'), ((13235, 13266), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(4)', 'seconds': '(3)'}), '(minutes=4, seconds=3)\n', (13244, 13266), False, 'from datetime import datetime, timedelta\n'), ((13350, 13370), 'pandas.offsets.Minute', 'pd.offsets.Minute', (['(1)'], {}), '(1)\n', (13367, 13370), True, 'import pandas as pd\n'), ((13372, 13392), 'pandas.offsets.Second', 'pd.offsets.Second', (['(3)'], {}), '(3)\n', (13389, 13392), True, 'import pandas as pd\n'), ((13424, 13442), 'pandas.offsets.Hour', 'pd.offsets.Hour', (['(2)'], {}), '(2)\n', (13439, 13442), True, 'import pandas as pd\n'), ((13735, 13767), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(6)', 'seconds': '(15)'}), '(minutes=6, seconds=15)\n', (13744, 13767), False, 'from datetime import datetime, timedelta\n'), ((14194, 14225), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(3)'}), '(minutes=5, seconds=3)\n', (14203, 14225), False, 'from datetime import datetime, timedelta\n'), ((17413, 17441), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""2 days 22:48:00"""'], {}), "('2 days 22:48:00')\n", (17422, 17441), False, 'from pandas.tseries.tdi import Timedelta\n'), ((17443, 17471), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""1 days 23:12:00"""'], {}), "('1 days 23:12:00')\n", (17452, 17471), False, 'from pandas.tseries.tdi import Timedelta\n'), ((17506, 17522), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""NaT"""'], {}), "('NaT')\n", (17515, 17522), False, 'from pandas.tseries.tdi import Timedelta\n'), ((17600, 17629), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""29 days 12:00:00"""'], {}), "('29 days 12:00:00')\n", (17609, 17629), False, 'from pandas.tseries.tdi import Timedelta\n'), ((17631, 17660), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""29 days 12:00:00"""'], {}), "('29 days 12:00:00')\n", (17640, 17660), False, 'from pandas.tseries.tdi import Timedelta\n'), ((17695, 17711), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""NaT"""'], {}), "('NaT')\n", (17704, 17711), False, 'from pandas.tseries.tdi import Timedelta\n'), ((18399, 18422), 'numpy.timedelta64', 'np.timedelta64', (['m', 'unit'], {}), '(m, unit)\n', (18413, 18422), True, 'import numpy as np\n'), ((18644, 18681), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['result', 'expected'], {}), '(result, expected)\n', (18663, 18681), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((18844, 18867), 'numpy.timedelta64', 'np.timedelta64', (['m', 'unit'], {}), '(m, unit)\n', (18858, 18867), True, 'import numpy as np\n'), ((19871, 19891), 'pandas.compat.zip', 'zip', (['args', 'intervals'], {}), '(args, intervals)\n', (19874, 19891), False, 'from pandas.compat import range, zip\n'), ((20934, 20977), 'pandas.util.testing.assertRaisesRegexp', 'tm.assertRaisesRegexp', (['TypeError', '"""operate"""'], {}), "(TypeError, 'operate')\n", (20955, 20977), True, 'import pandas.util.testing as tm\n'), ((21064, 21095), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)', 'seconds': '(3)'}), '(minutes=5, seconds=3)\n', (21073, 21095), False, 'from datetime import datetime, timedelta\n'), ((31736, 31753), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""1.5s"""'], {}), "('1.5s')\n", (31745, 31753), False, 'from pandas.tseries.tdi import Timedelta\n'), ((31850, 31867), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""1.5s"""'], {}), "('1.5s')\n", (31859, 31867), False, 'from pandas.tseries.tdi import Timedelta\n'), ((32462, 32479), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""0.5s"""'], {}), "('0.5s')\n", (32471, 32479), False, 'from pandas.tseries.tdi import Timedelta\n'), ((32576, 32593), 'pandas.tseries.tdi.Timedelta', 'Timedelta', (['"""0.5s"""'], {}), "('0.5s')\n", (32585, 32593), False, 'from pandas.tseries.tdi import Timedelta\n'), ((37906, 37932), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-01"""'], {}), "('2011-01-01')\n", (37918, 37932), True, 'import pandas as pd\n'), ((37960, 37986), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-03"""'], {}), "('2011-01-03')\n", (37972, 37986), True, 'import pandas as pd\n'), ((38023, 38049), 'pandas.Timestamp', 'pd.Timestamp', (['"""2011-01-03"""'], {}), "('2011-01-03')\n", (38035, 38049), True, 'import pandas as pd\n'), ((38072, 38094), 'pandas.Timedelta', 'pd.Timedelta', (['"""1 days"""'], {}), "('1 days')\n", (38084, 38094), True, 'import pandas as pd\n'), ((38122, 38144), 'pandas.Timedelta', 'pd.Timedelta', (['"""3 days"""'], {}), "('3 days')\n", (38134, 38144), True, 'import pandas as pd\n'), ((38181, 38203), 'pandas.Timedelta', 'pd.Timedelta', (['"""3 days"""'], {}), "('3 days')\n", (38193, 38203), True, 'import pandas as pd\n'), ((38226, 38256), 'pandas.Period', 'pd.Period', (['"""2011-01"""'], {'freq': '"""M"""'}), "('2011-01', freq='M')\n", (38235, 38256), True, 'import pandas as pd\n'), ((38284, 38314), 'pandas.Period', 'pd.Period', (['"""2011-03"""'], {'freq': '"""M"""'}), "('2011-03', freq='M')\n", (38293, 38314), True, 'import pandas as pd\n'), ((38351, 38381), 'pandas.Period', 'pd.Period', (['"""2011-03"""'], {'freq': '"""M"""'}), "('2011-03', freq='M')\n", (38360, 38381), True, 'import pandas as pd\n'), ((38649, 38671), 'pandas.Series', 'Series', (['r'], {'dtype': 'dtype'}), '(r, dtype=dtype)\n', (38655, 38671), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((38673, 38694), 'pandas.Index', 'Index', (['r'], {'dtype': 'dtype'}), '(r, dtype=dtype)\n', (38678, 38694), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((38728, 38756), 'pandas.Series', 'Series', (['[False, False, True]'], {}), '([False, False, True])\n', (38734, 38756), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((38777, 38821), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left == right)', 'expected'], {}), '(left == right, expected)\n', (38796, 38821), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((38854, 38881), 'pandas.Series', 'Series', (['[True, True, False]'], {}), '([True, True, False])\n', (38860, 38881), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((38902, 38946), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left != right)', 'expected'], {}), '(left != right, expected)\n', (38921, 38946), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((38979, 39008), 'pandas.Series', 'Series', (['[False, False, False]'], {}), '([False, False, False])\n', (38985, 39008), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((39029, 39072), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left < right)', 'expected'], {}), '(left < right, expected)\n', (39048, 39072), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((39105, 39134), 'pandas.Series', 'Series', (['[False, False, False]'], {}), '([False, False, False])\n', (39111, 39134), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((39155, 39198), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left > right)', 'expected'], {}), '(left > right, expected)\n', (39174, 39198), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((39231, 39259), 'pandas.Series', 'Series', (['[False, False, True]'], {}), '([False, False, True])\n', (39237, 39259), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((39280, 39324), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left >= right)', 'expected'], {}), '(left >= right, expected)\n', (39299, 39324), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((39357, 39385), 'pandas.Series', 'Series', (['[False, False, True]'], {}), '([False, False, True])\n', (39363, 39385), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((39406, 39450), 'pandas.util.testing.assert_series_equal', 'assert_series_equal', (['(left <= right)', 'expected'], {}), '(left <= right, expected)\n', (39425, 39450), False, 'from pandas.util.testing import assert_series_equal, assert_almost_equal\n'), ((43129, 43171), 'pandas.util.testing.assert_produces_warning', 'tm.assert_produces_warning', (['RuntimeWarning'], {}), '(RuntimeWarning)\n', (43155, 43171), True, 'import pandas.util.testing as tm\n'), ((52701, 52717), 'numpy.isnan', 'np.isnan', (['result'], {}), '(result)\n', (52709, 52717), True, 'import numpy as np\n'), ((52768, 52777), 'pandas.Index', 'Index', (['[]'], {}), '([])\n', (52773, 52777), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((59675, 59701), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (59690, 59701), True, 'import pandas.util.testing as tm\n'), ((60334, 60356), 'pandas.Timedelta', 'pd.Timedelta', (['"""1 days"""'], {}), "('1 days')\n", (60346, 60356), True, 'import pandas as pd\n'), ((60358, 60380), 'pandas.Timedelta', 'pd.Timedelta', (['"""2 days"""'], {}), "('2 days')\n", (60370, 60380), True, 'import pandas as pd\n'), ((60409, 60431), 'pandas.Timedelta', 'pd.Timedelta', (['"""3 days"""'], {}), "('3 days')\n", (60421, 60431), True, 'import pandas as pd\n'), ((60476, 60498), 'pandas.Timedelta', 'pd.Timedelta', (['"""4 days"""'], {}), "('4 days')\n", (60488, 60498), True, 'import pandas as pd\n'), ((60500, 60522), 'pandas.Timedelta', 'pd.Timedelta', (['"""5 days"""'], {}), "('5 days')\n", (60512, 60522), True, 'import pandas as pd\n'), ((60553, 60575), 'pandas.Timedelta', 'pd.Timedelta', (['"""6 days"""'], {}), "('6 days')\n", (60565, 60575), True, 'import pandas as pd\n'), ((60613, 60635), 'pandas.Timedelta', 'pd.Timedelta', (['"""3 days"""'], {}), "('3 days')\n", (60625, 60635), True, 'import pandas as pd\n'), ((60685, 60707), 'pandas.Timedelta', 'pd.Timedelta', (['"""3 days"""'], {}), "('3 days')\n", (60697, 60707), True, 'import pandas as pd\n'), ((61262, 61288), 'pandas.util.testing.assertRaises', 'tm.assertRaises', (['TypeError'], {}), '(TypeError)\n', (61277, 61288), True, 'import pandas.util.testing as tm\n'), ((63075, 63100), 'numpy.errstate', 'np.errstate', ([], {'all': '"""ignore"""'}), "(all='ignore')\n", (63086, 63100), True, 'import numpy as np\n'), ((65477, 65493), 'datetime.date', 'date', (['(2012)', '(1)', '(1)'], {}), '(2012, 1, 1)\n', (65481, 65493), False, 'from datetime import date\n'), ((65495, 65511), 'datetime.date', 'date', (['(2012)', '(1)', '(2)'], {}), '(2012, 1, 2)\n', (65499, 65511), False, 'from datetime import date\n'), ((7620, 7662), 'pandas.Series', 'Series', (['(1000000000.0 * 3600 * 24)', 'rs.index'], {}), '(1000000000.0 * 3600 * 24, rs.index)\n', (7626, 7662), False, 'from pandas import Index, Series, DataFrame, isnull, bdate_range, NaT, date_range, timedelta_range, _np_version_under1p8\n'), ((14332, 14352), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (14341, 14352), False, 'from datetime import datetime, timedelta\n'), ((14369, 14389), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(1)'}), '(seconds=1)\n', (14378, 14389), False, 'from datetime import datetime, timedelta\n'), ((14561, 14581), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(1)'}), '(seconds=1)\n', (14570, 14581), False, 'from datetime import datetime, timedelta\n'), ((14598, 14618), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (14607, 14618), False, 'from datetime import datetime, timedelta\n'), ((15022, 15042), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (15031, 15042), False, 'from datetime import datetime, timedelta\n'), ((15059, 15079), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(1)'}), '(seconds=1)\n', (15068, 15079), False, 'from datetime import datetime, timedelta\n'), ((15251, 15271), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(1)'}), '(seconds=1)\n', (15260, 15271), False, 'from datetime import datetime, timedelta\n'), ((15288, 15308), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (15297, 15308), False, 'from datetime import datetime, timedelta\n'), ((20012, 20020), 'pandas.compat.range', 'range', (['(2)'], {}), '(2)\n', (20017, 20020), False, 'from pandas.compat import range, zip\n'), ((64508, 64530), 'operator.truediv', 'operator.truediv', (['y', 'x'], {}), '(y, x)\n', (64524, 64530), False, 'import operator\n'), ((64693, 64711), 'operator.div', 'operator.div', (['y', 'x'], {}), '(y, x)\n', (64705, 64711), False, 'import operator\n'), ((18344, 18367), 'numpy.timedelta64', 'np.timedelta64', (['m', 'unit'], {}), '(m, unit)\n', (18358, 18367), True, 'import numpy as np\n'), ((18789, 18812), 'numpy.timedelta64', 'np.timedelta64', (['m', 'unit'], {}), '(m, unit)\n', (18803, 18812), True, 'import numpy as np\n')]
import argparse import multiprocessing import random import shutil from datetime import datetime from functools import partial from pathlib import Path import chainer import chainer.functions as F import chainer.links as L import cupy import numpy as np from chainer import iterators, optimizers, serializers from chainer.datasets import TransformDataset, get_cifar10 from chainer.training import StandardUpdater, Trainer, extensions import augmentation from metric_learning import MetricLearnClassifier from modified_evaluator import ModifiedEvaluator from modified_updater import ModifiedUpdater from resnet import ResNet50 def apply_augmentation(inputs, mean, std, angle=(-5, 5), scale=(1, 1.2), crop_size=None, train=True): img, label = inputs img = img.copy() img = img.transpose(1, 2, 0) if train: img, _ = augmentation.gamma_correction(img) img -= mean[None, None, :] img /= std[None, None, :] if train: img, _ = augmentation.random_rotate(img, angle=angle) if np.random.rand() < 0.5: img, _ = augmentation.mirror(img) if np.random.rand() < 0.5: img, _ = augmentation.flip(img) img, _ = augmentation.random_resize(img, scale=scale) if crop_size is not None: rnd1 = np.random.randint(img.shape[0] - crop_size) rnd2 = np.random.randint(img.shape[1] - crop_size) img = img[rnd1:rnd1 + crop_size, rnd2:rnd2 + crop_size, :] img = img.transpose(2, 0, 1) return img, label def main(): parser = argparse.ArgumentParser(description='training mnist') parser.add_argument('--gpu', '-g', default=-1, type=int, help='GPU ID (negative value indicates CPU)') parser.add_argument('--epoch', '-e', type=int, default=100, help='Number of sweeps over the dataset to train') parser.add_argument('--batchsize', '-b', type=int, default=8, help='Number of images in each mini-batch') parser.add_argument('--seed', '-s', type=int, default=0, help='Random seed') parser.add_argument('--report_trigger', '-rt', type=str, default='1e', help='Interval for reporting(Ex.100i, default:1e)') parser.add_argument('--save_trigger', '-st', type=str, default='1e', help='Interval for saving the model(Ex.100i, default:1e)') parser.add_argument('--load_model', '-lm', type=str, default=None, help='Path of the model object to load') parser.add_argument('--load_optimizer', '-lo', type=str, default=None, help='Path of the optimizer object to load') args = parser.parse_args() start_time = datetime.now() save_dir = Path('output/{}'.format(start_time.strftime('%Y%m%d_%H%M'))) random.seed(args.seed) np.random.seed(args.seed) cupy.random.seed(args.seed) model = MetricLearnClassifier(ResNet50(), 512, 10, method='arcface', final_margin=0.5, final_scale=64, target_epoch=100) if args.load_model is not None: serializers.load_npz(args.load_model, model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() optimizer = optimizers.Adam(alpha=1e-3, weight_decay_rate=5e-4, amsgrad=True) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(5e-4)) if args.load_optimizer is not None: serializers.load_npz(args.load_optimizer, optimizer) train_data, valid_data = get_cifar10(scale=255.) mean = np.mean([x for x, _ in train_data], axis=(0, 2, 3)) std = np.std([x for x, _ in train_data], axis=(0, 2, 3)) train_transform = partial(apply_augmentation, mean=mean, std=std, crop_size=28, train=True) valid_transform = partial(apply_augmentation, mean=mean, std=std, crop_size=28, train=True) train_data = TransformDataset(train_data, train_transform) valid_data = TransformDataset(valid_data, valid_transform) train_iter = iterators.SerialIterator(train_data, args.batchsize) valid_iter = iterators.SerialIterator(valid_data, args.batchsize, repeat=False, shuffle=False) updater = ModifiedUpdater(train_iter, optimizer, device=args.gpu) trainer = Trainer(updater, (args.epoch, 'epoch'), out=save_dir) report_trigger = (int(args.report_trigger[:-1]), 'iteration' if args.report_trigger[-1] == 'i' else 'epoch') trainer.extend(extensions.LogReport(trigger=report_trigger)) trainer.extend(ModifiedEvaluator(valid_iter, model, device=args.gpu), name='val', trigger=report_trigger) trainer.extend(extensions.PrintReport(['epoch', 'iteration', 'main/loss', 'main/accuracy', 'val/main/loss', 'val/main/accuracy', 'elapsed_time']), trigger=report_trigger) trainer.extend(extensions.PlotReport(['main/loss', 'val/main/loss'], x_key=report_trigger[1], marker='.', file_name='loss.png', trigger=report_trigger)) trainer.extend(extensions.PlotReport(['main/accuracy', 'val/main/accuracy'], x_key=report_trigger[1], marker='.', file_name='accuracy.png', trigger=report_trigger)) save_trigger = (int(args.save_trigger[:-1]), 'iteration' if args.save_trigger[-1] == 'i' else 'epoch') trainer.extend(extensions.snapshot_object(model, filename='model_{0}-{{.updater.{0}}}.npz' .format(save_trigger[1])), trigger=save_trigger) trainer.extend(extensions.snapshot_object(optimizer, filename='optimizer_{0}-{{.updater.{0}}}.npz' .format(save_trigger[1])), trigger=save_trigger) trainer.extend(extensions.ProgressBar()) trainer.extend(extensions.ExponentialShift('lr', 0.5), trigger=(30, 'epoch')) if save_dir.exists(): shutil.rmtree(save_dir) save_dir.mkdir() (save_dir / 'training_details').mkdir() # Write parameters text with open(save_dir / 'training_details/train_params.txt', 'w') as f: f.write('model: {}\n'.format(model.predictor.__class__.__name__)) f.write('n_epoch: {}\n'.format(args.epoch)) f.write('batch_size: {}\n'.format(args.batchsize)) f.write('n_data_train: {}\n'.format(len(train_data))) f.write('n_data_val: {}\n'.format(len(valid_data))) f.write('seed: {}\n'.format(args.seed)) trainer.run() if __name__ == '__main__': main()
[ "numpy.random.seed", "argparse.ArgumentParser", "modified_updater.ModifiedUpdater", "augmentation.flip", "numpy.mean", "numpy.random.randint", "chainer.iterators.SerialIterator", "augmentation.random_rotate", "shutil.rmtree", "resnet.ResNet50", "chainer.training.extensions.LogReport", "modified_evaluator.ModifiedEvaluator", "chainer.serializers.load_npz", "numpy.std", "cupy.random.seed", "chainer.training.extensions.ExponentialShift", "random.seed", "chainer.datasets.TransformDataset", "augmentation.random_resize", "datetime.datetime.now", "functools.partial", "chainer.optimizer.WeightDecay", "chainer.training.extensions.PrintReport", "chainer.training.extensions.PlotReport", "chainer.datasets.get_cifar10", "augmentation.gamma_correction", "augmentation.mirror", "chainer.optimizers.Adam", "chainer.training.Trainer", "chainer.training.extensions.ProgressBar", "numpy.random.rand", "chainer.cuda.get_device_from_id" ]
[((1560, 1613), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'description': '"""training mnist"""'}), "(description='training mnist')\n", (1583, 1613), False, 'import argparse\n'), ((2759, 2773), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (2771, 2773), False, 'from datetime import datetime\n'), ((2855, 2877), 'random.seed', 'random.seed', (['args.seed'], {}), '(args.seed)\n', (2866, 2877), False, 'import random\n'), ((2882, 2907), 'numpy.random.seed', 'np.random.seed', (['args.seed'], {}), '(args.seed)\n', (2896, 2907), True, 'import numpy as np\n'), ((2912, 2939), 'cupy.random.seed', 'cupy.random.seed', (['args.seed'], {}), '(args.seed)\n', (2928, 2939), False, 'import cupy\n'), ((3343, 3411), 'chainer.optimizers.Adam', 'optimizers.Adam', ([], {'alpha': '(0.001)', 'weight_decay_rate': '(0.0005)', 'amsgrad': '(True)'}), '(alpha=0.001, weight_decay_rate=0.0005, amsgrad=True)\n', (3358, 3411), False, 'from chainer import iterators, optimizers, serializers\n'), ((3627, 3651), 'chainer.datasets.get_cifar10', 'get_cifar10', ([], {'scale': '(255.0)'}), '(scale=255.0)\n', (3638, 3651), False, 'from chainer.datasets import TransformDataset, get_cifar10\n'), ((3662, 3713), 'numpy.mean', 'np.mean', (['[x for x, _ in train_data]'], {'axis': '(0, 2, 3)'}), '([x for x, _ in train_data], axis=(0, 2, 3))\n', (3669, 3713), True, 'import numpy as np\n'), ((3724, 3774), 'numpy.std', 'np.std', (['[x for x, _ in train_data]'], {'axis': '(0, 2, 3)'}), '([x for x, _ in train_data], axis=(0, 2, 3))\n', (3730, 3774), True, 'import numpy as np\n'), ((3798, 3871), 'functools.partial', 'partial', (['apply_augmentation'], {'mean': 'mean', 'std': 'std', 'crop_size': '(28)', 'train': '(True)'}), '(apply_augmentation, mean=mean, std=std, crop_size=28, train=True)\n', (3805, 3871), False, 'from functools import partial\n'), ((3894, 3967), 'functools.partial', 'partial', (['apply_augmentation'], {'mean': 'mean', 'std': 'std', 'crop_size': '(28)', 'train': '(True)'}), '(apply_augmentation, mean=mean, std=std, crop_size=28, train=True)\n', (3901, 3967), False, 'from functools import partial\n'), ((3986, 4031), 'chainer.datasets.TransformDataset', 'TransformDataset', (['train_data', 'train_transform'], {}), '(train_data, train_transform)\n', (4002, 4031), False, 'from chainer.datasets import TransformDataset, get_cifar10\n'), ((4049, 4094), 'chainer.datasets.TransformDataset', 'TransformDataset', (['valid_data', 'valid_transform'], {}), '(valid_data, valid_transform)\n', (4065, 4094), False, 'from chainer.datasets import TransformDataset, get_cifar10\n'), ((4113, 4165), 'chainer.iterators.SerialIterator', 'iterators.SerialIterator', (['train_data', 'args.batchsize'], {}), '(train_data, args.batchsize)\n', (4137, 4165), False, 'from chainer import iterators, optimizers, serializers\n'), ((4183, 4269), 'chainer.iterators.SerialIterator', 'iterators.SerialIterator', (['valid_data', 'args.batchsize'], {'repeat': '(False)', 'shuffle': '(False)'}), '(valid_data, args.batchsize, repeat=False, shuffle=\n False)\n', (4207, 4269), False, 'from chainer import iterators, optimizers, serializers\n'), ((4280, 4335), 'modified_updater.ModifiedUpdater', 'ModifiedUpdater', (['train_iter', 'optimizer'], {'device': 'args.gpu'}), '(train_iter, optimizer, device=args.gpu)\n', (4295, 4335), False, 'from modified_updater import ModifiedUpdater\n'), ((4350, 4403), 'chainer.training.Trainer', 'Trainer', (['updater', "(args.epoch, 'epoch')"], {'out': 'save_dir'}), "(updater, (args.epoch, 'epoch'), out=save_dir)\n", (4357, 4403), False, 'from chainer.training import StandardUpdater, Trainer, extensions\n'), ((865, 899), 'augmentation.gamma_correction', 'augmentation.gamma_correction', (['img'], {}), '(img)\n', (894, 899), False, 'import augmentation\n'), ((994, 1038), 'augmentation.random_rotate', 'augmentation.random_rotate', (['img'], {'angle': 'angle'}), '(img, angle=angle)\n', (1020, 1038), False, 'import augmentation\n'), ((1216, 1260), 'augmentation.random_resize', 'augmentation.random_resize', (['img'], {'scale': 'scale'}), '(img, scale=scale)\n', (1242, 1260), False, 'import augmentation\n'), ((1306, 1349), 'numpy.random.randint', 'np.random.randint', (['(img.shape[0] - crop_size)'], {}), '(img.shape[0] - crop_size)\n', (1323, 1349), True, 'import numpy as np\n'), ((1365, 1408), 'numpy.random.randint', 'np.random.randint', (['(img.shape[1] - crop_size)'], {}), '(img.shape[1] - crop_size)\n', (1382, 1408), True, 'import numpy as np\n'), ((2975, 2985), 'resnet.ResNet50', 'ResNet50', ([], {}), '()\n', (2983, 2985), False, 'from resnet import ResNet50\n'), ((3179, 3223), 'chainer.serializers.load_npz', 'serializers.load_npz', (['args.load_model', 'model'], {}), '(args.load_model, model)\n', (3199, 3223), False, 'from chainer import iterators, optimizers, serializers\n'), ((3459, 3496), 'chainer.optimizer.WeightDecay', 'chainer.optimizer.WeightDecay', (['(0.0005)'], {}), '(0.0005)\n', (3488, 3496), False, 'import chainer\n'), ((3544, 3596), 'chainer.serializers.load_npz', 'serializers.load_npz', (['args.load_optimizer', 'optimizer'], {}), '(args.load_optimizer, optimizer)\n', (3564, 3596), False, 'from chainer import iterators, optimizers, serializers\n'), ((4537, 4581), 'chainer.training.extensions.LogReport', 'extensions.LogReport', ([], {'trigger': 'report_trigger'}), '(trigger=report_trigger)\n', (4557, 4581), False, 'from chainer.training import StandardUpdater, Trainer, extensions\n'), ((4602, 4655), 'modified_evaluator.ModifiedEvaluator', 'ModifiedEvaluator', (['valid_iter', 'model'], {'device': 'args.gpu'}), '(valid_iter, model, device=args.gpu)\n', (4619, 4655), False, 'from modified_evaluator import ModifiedEvaluator\n'), ((4712, 4846), 'chainer.training.extensions.PrintReport', 'extensions.PrintReport', (["['epoch', 'iteration', 'main/loss', 'main/accuracy', 'val/main/loss',\n 'val/main/accuracy', 'elapsed_time']"], {}), "(['epoch', 'iteration', 'main/loss', 'main/accuracy',\n 'val/main/loss', 'val/main/accuracy', 'elapsed_time'])\n", (4734, 4846), False, 'from chainer.training import StandardUpdater, Trainer, extensions\n'), ((4930, 5071), 'chainer.training.extensions.PlotReport', 'extensions.PlotReport', (["['main/loss', 'val/main/loss']"], {'x_key': 'report_trigger[1]', 'marker': '"""."""', 'file_name': '"""loss.png"""', 'trigger': 'report_trigger'}), "(['main/loss', 'val/main/loss'], x_key=report_trigger[\n 1], marker='.', file_name='loss.png', trigger=report_trigger)\n", (4951, 5071), False, 'from chainer.training import StandardUpdater, Trainer, extensions\n'), ((5128, 5286), 'chainer.training.extensions.PlotReport', 'extensions.PlotReport', (["['main/accuracy', 'val/main/accuracy']"], {'x_key': 'report_trigger[1]', 'marker': '"""."""', 'file_name': '"""accuracy.png"""', 'trigger': 'report_trigger'}), "(['main/accuracy', 'val/main/accuracy'], x_key=\n report_trigger[1], marker='.', file_name='accuracy.png', trigger=\n report_trigger)\n", (5149, 5286), False, 'from chainer.training import StandardUpdater, Trainer, extensions\n'), ((5834, 5858), 'chainer.training.extensions.ProgressBar', 'extensions.ProgressBar', ([], {}), '()\n', (5856, 5858), False, 'from chainer.training import StandardUpdater, Trainer, extensions\n'), ((5879, 5917), 'chainer.training.extensions.ExponentialShift', 'extensions.ExponentialShift', (['"""lr"""', '(0.5)'], {}), "('lr', 0.5)\n", (5906, 5917), False, 'from chainer.training import StandardUpdater, Trainer, extensions\n'), ((5977, 6000), 'shutil.rmtree', 'shutil.rmtree', (['save_dir'], {}), '(save_dir)\n', (5990, 6000), False, 'import shutil\n'), ((1050, 1066), 'numpy.random.rand', 'np.random.rand', ([], {}), '()\n', (1064, 1066), True, 'import numpy as np\n'), ((1095, 1119), 'augmentation.mirror', 'augmentation.mirror', (['img'], {}), '(img)\n', (1114, 1119), False, 'import augmentation\n'), ((1131, 1147), 'numpy.random.rand', 'np.random.rand', ([], {}), '()\n', (1145, 1147), True, 'import numpy as np\n'), ((1176, 1198), 'augmentation.flip', 'augmentation.flip', (['img'], {}), '(img)\n', (1193, 1198), False, 'import augmentation\n'), ((3255, 3296), 'chainer.cuda.get_device_from_id', 'chainer.cuda.get_device_from_id', (['args.gpu'], {}), '(args.gpu)\n', (3286, 3296), False, 'import chainer\n')]
from dataclasses import dataclass import os import logging import json from functools import lru_cache import cv2 import numpy as np import app from util import cvimage as Image logger = logging.getLogger(__name__) net_file = app.cache_path / 'ark_material.onnx' index_file = app.cache_path / 'index_itemid_relation.json' model_timestamp = 0 @dataclass class DnnItemRecord: class_id: int item_id: str item_name: str item_type: str dnn_items_by_class : dict[int, DnnItemRecord] = {} dnn_items_by_item_id : dict[str, DnnItemRecord] = {} dnn_items_by_item_name : dict[str, DnnItemRecord] = {} @lru_cache(1) def load_net(): update_index_info() with open(net_file, 'rb') as f: data = f.read() net = cv2.dnn.readNetFromONNX(data) return net @lru_cache(1) def _update_index_info(): with open(index_file, 'r', encoding='utf-8') as f: data = json.load(f) global model_timestamp model_timestamp = data['time'] idx2id, id2idx, idx2name, idx2type = data['idx2id'], data['id2idx'], data['idx2name'], data['idx2type'] dnn_items_by_class.clear() dnn_items_by_item_id.clear() dnn_items_by_item_name.clear() for index, item_id in enumerate(idx2id): record = DnnItemRecord(index, item_id, idx2name[index], idx2type[index]) dnn_items_by_class[index] = record dnn_items_by_item_id[item_id] = record dnn_items_by_item_name[idx2name[index]] = record def update_index_info(): update_net() return _update_index_info() def retry_get(url, max_retry=5, timeout=3): import requests c = 0 ex = None while c < max_retry: try: return requests.get(url, timeout=timeout) except Exception as e: c += 1 ex = e raise ex def update_net(): local_cache_time = 0 import time os.makedirs(os.path.dirname(index_file), exist_ok=True) try: stat = os.stat(index_file) cache_mtime = stat.st_mtime with open(index_file, 'r', encoding='utf-8') as f: local_rel = json.load(f) model_gen_time = local_rel['time'] / 1000 now = time.time() logger.debug(f'{cache_mtime=} {now=} {model_gen_time=}') if cache_mtime > model_gen_time and now - cache_mtime < 60 * 60 * 8: return except: pass logger.info('检查物品识别模型更新') resp = retry_get('https://cdn.jsdelivr.net/gh/triwinds/arknights-ml@latest/inventory/index_itemid_relation.json') remote_relation = resp.json() if remote_relation['time'] > local_cache_time: from datetime import datetime logger.info(f'更新物品识别模型, 模型生成时间: {datetime.fromtimestamp(remote_relation["time"]/1000).strftime("%Y-%m-%d %H:%M:%S")}') with open(index_file, 'w', encoding='utf-8') as f: json.dump(remote_relation, f, ensure_ascii=False) resp = retry_get('https://cdn.jsdelivr.net/gh/triwinds/arknights-ml@latest/inventory/ark_material.onnx') with open(net_file, 'wb') as f: f.write(resp.content) _update_index_info.cache_clear() else: os.utime(index_file, None) def _update_mat_collection(collection, name, img): global itemmask if img.size != (48, 48): img = img.resize((48, 48), Image.BILINEAR) mat = np.array(img) mat[itemmask] = 0 collection[name] = mat resources_known_items = {} def load(): from . import resources from . import minireco resource_files = [(x[:-4], resources.resolve('items/' + x)) for x in resources.get_entries('items')[1] if x.endswith('.png')] global resources_itemmats, num_recognizer, itemmask, resources_known_items resources_itemmats = {} itemmask = np.asarray(resources.load_image('common/itemmask.png', '1')) for name, index in resource_files: img = resources.load_image(index, 'RGB') _update_mat_collection(resources_itemmats, name, img) model = resources.load_pickle('minireco/NotoSansCJKsc-DemiLight-nums.dat') reco = minireco.MiniRecognizer(model, minireco.compare_ccoeff) num_recognizer=reco for prefix in ['items', 'items/archive', 'items/not-loot']: _, files = resources.get_entries(prefix) for filename in files: itemname = filename[:-4] if filename.endswith('.png') else filename path = prefix + '/' + filename resources_known_items[itemname] = resources.resolve(path) update_extra_items() def update_extra_items(): import app new_mtime = os.path.getmtime(app.extra_items_path) if new_mtime <= update_extra_items.old_mtime: return from . import resources from glob import glob extra_files = [(os.path.basename(x)[:-4], resources.FileSystemIndex(x)) for x in glob(os.path.join( app.extra_items_path, '*.png'))] extra_known_items = {} extra_itemmats = {} for key, value in extra_files: for name, index in extra_files: img = resources.load_image(index, 'RGB') _update_mat_collection(extra_itemmats, name, img) extra_known_items[key] = value global itemmats itemmats = {} itemmats.update(resources_itemmats) itemmats.update(extra_itemmats) global all_known_items all_known_items = {} all_known_items.update(resources_known_items) all_known_items.update(extra_known_items) update_extra_items.old_mtime = new_mtime update_extra_items.old_mtime = 0 def add_item(image) -> str: import os import time import app date = time.strftime('%Y-%m-%d') index = add_item.last_index + 1 while True: name = '未知物品-%s-%d' % (date, index) filename = app.extra_items_path.joinpath(name + '.png') if not os.path.exists(filename): break index += 1 add_item.last_index = index image.save(filename) update_extra_items() return name add_item.last_index = 0 load()
[ "json.dump", "json.load", "os.path.join", "os.stat", "app.extra_items_path.joinpath", "os.path.basename", "os.path.dirname", "os.path.exists", "time.strftime", "cv2.dnn.readNetFromONNX", "time.time", "numpy.array", "os.path.getmtime", "requests.get", "datetime.datetime.fromtimestamp", "functools.lru_cache", "os.utime", "logging.getLogger" ]
[((190, 217), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (207, 217), False, 'import logging\n'), ((614, 626), 'functools.lru_cache', 'lru_cache', (['(1)'], {}), '(1)\n', (623, 626), False, 'from functools import lru_cache\n'), ((789, 801), 'functools.lru_cache', 'lru_cache', (['(1)'], {}), '(1)\n', (798, 801), False, 'from functools import lru_cache\n'), ((3309, 3322), 'numpy.array', 'np.array', (['img'], {}), '(img)\n', (3317, 3322), True, 'import numpy as np\n'), ((4527, 4565), 'os.path.getmtime', 'os.path.getmtime', (['app.extra_items_path'], {}), '(app.extra_items_path)\n', (4543, 4565), False, 'import os\n'), ((5537, 5562), 'time.strftime', 'time.strftime', (['"""%Y-%m-%d"""'], {}), "('%Y-%m-%d')\n", (5550, 5562), False, 'import time\n'), ((741, 770), 'cv2.dnn.readNetFromONNX', 'cv2.dnn.readNetFromONNX', (['data'], {}), '(data)\n', (764, 770), False, 'import cv2\n'), ((898, 910), 'json.load', 'json.load', (['f'], {}), '(f)\n', (907, 910), False, 'import json\n'), ((1868, 1895), 'os.path.dirname', 'os.path.dirname', (['index_file'], {}), '(index_file)\n', (1883, 1895), False, 'import os\n'), ((1936, 1955), 'os.stat', 'os.stat', (['index_file'], {}), '(index_file)\n', (1943, 1955), False, 'import os\n'), ((2156, 2167), 'time.time', 'time.time', ([], {}), '()\n', (2165, 2167), False, 'import time\n'), ((3119, 3145), 'os.utime', 'os.utime', (['index_file', 'None'], {}), '(index_file, None)\n', (3127, 3145), False, 'import os\n'), ((5678, 5722), 'app.extra_items_path.joinpath', 'app.extra_items_path.joinpath', (["(name + '.png')"], {}), "(name + '.png')\n", (5707, 5722), False, 'import app\n'), ((1674, 1708), 'requests.get', 'requests.get', (['url'], {'timeout': 'timeout'}), '(url, timeout=timeout)\n', (1686, 1708), False, 'import requests\n'), ((2075, 2087), 'json.load', 'json.load', (['f'], {}), '(f)\n', (2084, 2087), False, 'import json\n'), ((2823, 2872), 'json.dump', 'json.dump', (['remote_relation', 'f'], {'ensure_ascii': '(False)'}), '(remote_relation, f, ensure_ascii=False)\n', (2832, 2872), False, 'import json\n'), ((5738, 5762), 'os.path.exists', 'os.path.exists', (['filename'], {}), '(filename)\n', (5752, 5762), False, 'import os\n'), ((4706, 4725), 'os.path.basename', 'os.path.basename', (['x'], {}), '(x)\n', (4722, 4725), False, 'import os\n'), ((4776, 4819), 'os.path.join', 'os.path.join', (['app.extra_items_path', '"""*.png"""'], {}), "(app.extra_items_path, '*.png')\n", (4788, 4819), False, 'import os\n'), ((2694, 2748), 'datetime.datetime.fromtimestamp', 'datetime.fromtimestamp', (["(remote_relation['time'] / 1000)"], {}), "(remote_relation['time'] / 1000)\n", (2716, 2748), False, 'from datetime import datetime\n')]
import copy import logging import time import numpy as np import torch import wandb from torch import nn from .utils import transform_list_to_tensor from ....core.robustness.robust_aggregation import RobustAggregator, is_weight_param from ....utils.logging import logger def test( model, device, test_loader, criterion, mode="raw-task", dataset="cifar10", poison_type="fashion", ): class_correct = list(0.0 for i in range(10)) class_total = list(0.0 for i in range(10)) if dataset in ("mnist", "emnist"): target_class = 7 if mode == "raw-task": classes = [str(i) for i in range(10)] elif mode == "targetted-task": if poison_type == "ardis": classes = [str(i) for i in range(10)] else: classes = [ "T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot", ] elif dataset == "cifar10": classes = ( "plane", "car", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck", ) # target_class = 2 for greencar, 9 for southwest if poison_type in ("howto", "greencar-neo"): target_class = 2 else: target_class = 9 model.eval() test_loss = 0 correct = 0 backdoor_correct = 0 backdoor_tot = 0 final_acc = 0 task_acc = None with torch.no_grad(): for data, target in test_loader: data, target = data.to(device), target.to(device) output = model(data) _, predicted = torch.max(output, 1) c = (predicted == target).squeeze() # test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss test_loss += criterion(output, target).item() pred = output.argmax( dim=1, keepdim=True ) # get the index of the max log-probability correct += pred.eq(target.view_as(pred)).sum().item() # check backdoor accuracy if poison_type == "ardis": backdoor_index = torch.where(target == target_class) target_backdoor = torch.ones_like(target[backdoor_index]) predicted_backdoor = predicted[backdoor_index] backdoor_correct += (predicted_backdoor == target_backdoor).sum().item() backdoor_tot = backdoor_index[0].shape[0] # logger.info("Target: {}".format(target_backdoor)) # logger.info("Predicted: {}".format(predicted_backdoor)) # for image_index in range(test_batch_size): for image_index in range(len(target)): label = target[image_index] class_correct[label] += c[image_index].item() class_total[label] += 1 test_loss /= len(test_loader.dataset) if mode == "raw-task": for i in range(10): logger.info( "Accuracy of %5s : %.2f %%" % (classes[i], 100 * class_correct[i] / class_total[i]) ) if i == target_class: task_acc = 100 * class_correct[i] / class_total[i] logger.info( "\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n".format( test_loss, correct, len(test_loader.dataset), 100.0 * correct / len(test_loader.dataset), ) ) final_acc = 100.0 * correct / len(test_loader.dataset) elif mode == "targetted-task": if dataset in ("mnist", "emnist"): for i in range(10): logger.info( "Accuracy of %5s : %.2f %%" % (classes[i], 100 * class_correct[i] / class_total[i]) ) if poison_type == "ardis": # ensure 7 is being classified as 1 logger.info( "Backdoor Accuracy of %.2f : %.2f %%" % (target_class, 100 * backdoor_correct / backdoor_tot) ) final_acc = 100 * backdoor_correct / backdoor_tot else: # trouser acc final_acc = 100 * class_correct[1] / class_total[1] elif dataset == "cifar10": logger.info( "#### Targetted Accuracy of %5s : %.2f %%" % ( classes[target_class], 100 * class_correct[target_class] / class_total[target_class], ) ) final_acc = 100 * class_correct[target_class] / class_total[target_class] return final_acc, task_acc class FedAvgRobustAggregator(object): def __init__( self, train_global, test_global, all_train_data_num, train_data_local_dict, test_data_local_dict, train_data_local_num_dict, worker_num, device, model, targetted_task_test_loader, num_dps_poisoned_dataset, args, ): self.train_global = train_global self.test_global = test_global self.all_train_data_num = all_train_data_num self.train_data_local_dict = train_data_local_dict self.test_data_local_dict = test_data_local_dict self.train_data_local_num_dict = train_data_local_num_dict self.worker_num = worker_num self.device = device self.args = args self.model_dict = dict() self.sample_num_dict = dict() self.flag_client_model_uploaded_dict = dict() self.robust_aggregator = RobustAggregator(args) self.targetted_task_test_loader = targetted_task_test_loader self.num_dps_poisoned_dataset = num_dps_poisoned_dataset self.adversary_fl_rounds = [ i for i in range(1, args.comm_round + 1) if (i - 1) % args.attack_freq == 0 ] for idx in range(self.worker_num): self.flag_client_model_uploaded_dict[idx] = False self.model, _ = self.init_model(model) def init_model(self, model): model_params = model.state_dict() # logging.info(model) return model, model_params def get_global_model_params(self): return self.model.state_dict() def add_local_trained_result(self, index, model_params, sample_num): logging.info("add_model. index = %d" % index) self.model_dict[index] = model_params self.sample_num_dict[index] = sample_num self.flag_client_model_uploaded_dict[index] = True def check_whether_all_receive(self): for idx in range(self.worker_num): if not self.flag_client_model_uploaded_dict[idx]: return False for idx in range(self.worker_num): self.flag_client_model_uploaded_dict[idx] = False return True def aggregate(self): start_time = time.time() model_list = [] training_num = 0 for idx in range(self.worker_num): if self.args.is_mobile == 1: self.model_dict[idx] = transform_list_to_tensor(self.model_dict[idx]) # conduct the defense here: local_sample_number, local_model_params = ( self.sample_num_dict[idx], self.model_dict[idx], ) if self.robust_aggregator.defense_type in ("norm_diff_clipping", "weak_dp"): clipped_local_state_dict = self.robust_aggregator.norm_diff_clipping( local_model_params, self.model.state_dict() ) else: raise NotImplementedError("Non-supported Defense type ... ") model_list.append((local_sample_number, clipped_local_state_dict)) training_num += self.sample_num_dict[idx] logging.info("len of self.model_dict[idx] = " + str(len(self.model_dict))) # logging.info("################aggregate: %d" % len(model_list)) (num0, averaged_params) = model_list[0] for k in averaged_params.keys(): for i in range(0, len(model_list)): local_sample_number, local_model_params = model_list[i] w = local_sample_number / training_num local_layer_update = local_model_params[k] if self.robust_aggregator.defense_type == "weak_dp": if is_weight_param(k): local_layer_update = self.robust_aggregator.add_noise( local_layer_update, self.device ) if i == 0: averaged_params[k] = local_model_params[k] * w else: averaged_params[k] += local_model_params[k] * w # update the global model which is cached at the server side self.model.load_state_dict(averaged_params) end_time = time.time() logging.info("aggregate time cost: %d" % (end_time - start_time)) return averaged_params def client_sampling(self, round_idx, client_num_in_total, client_num_per_round): num_clients = min(client_num_per_round, client_num_in_total) np.random.seed( round_idx ) # make sure for each comparison, we are selecting the same clients each round if round_idx not in self.adversary_fl_rounds: client_indexes = np.random.choice( range(client_num_in_total), num_clients, replace=False ) else: client_indexes = np.array( [1] + list( np.random.choice( range(client_num_in_total), num_clients, replace=False ) ) ) # we gaurantee that the attacker will participate in a certain frequency logging.info("client_indexes = %s" % str(client_indexes)) return client_indexes def test_on_all_clients(self, round_idx): if ( round_idx % self.args.frequency_of_the_test == 0 or round_idx == self.args.comm_round - 1 ): logging.info( "################local_test_on_all_clients : {}".format(round_idx) ) train_num_samples = [] train_tot_corrects = [] train_losses = [] test_num_samples = [] test_tot_corrects = [] test_losses = [] for client_idx in range(self.args.client_num_in_total): # train data train_tot_correct, train_num_sample, train_loss = self._infer( self.train_data_local_dict[client_idx] ) train_tot_corrects.append(copy.deepcopy(train_tot_correct)) train_num_samples.append(copy.deepcopy(train_num_sample)) train_losses.append(copy.deepcopy(train_loss)) # test data test_tot_correct, test_num_sample, test_loss = self._infer( self.test_data_local_dict[client_idx] ) test_tot_corrects.append(copy.deepcopy(test_tot_correct)) test_num_samples.append(copy.deepcopy(test_num_sample)) test_losses.append(copy.deepcopy(test_loss)) # test on training dataset train_acc = sum(train_tot_corrects) / sum(train_num_samples) train_loss = sum(train_losses) / sum(train_num_samples) wandb.log({"Train/Acc": train_acc, "round": round_idx}) wandb.log({"Train/Loss": train_loss, "round": round_idx}) stats = {"training_acc": train_acc, "training_loss": train_loss} logging.info(stats) # test on test dataset test_acc = sum(test_tot_corrects) / sum(test_num_samples) test_loss = sum(test_losses) / sum(test_num_samples) wandb.log({"Test/Acc": test_acc, "round": round_idx}) wandb.log({"Test/Loss": test_loss, "round": round_idx}) stats = {"test_acc": test_acc, "test_loss": test_loss} logging.info(stats) def test_target_accuracy(self, round_idx): test( self.model, self.device, self.targetted_task_test_loader, criterion=nn.CrossEntropyLoss().to(self.device), mode="targetted-task", dataset=self.args.dataset, poison_type=self.args.poison_type, ) def _infer(self, test_data): self.model.eval() self.model.to(self.device) test_loss = test_acc = test_total = 0.0 criterion = nn.CrossEntropyLoss().to(self.device) with torch.no_grad(): for batch_idx, (x, target) in enumerate(test_data): x = x.to(self.device) target = target.to(self.device) pred = self.model(x) loss = criterion(pred, target) _, predicted = torch.max(pred, -1) correct = predicted.eq(target).sum() test_acc += correct.item() test_loss += loss.item() * target.size(0) test_total += target.size(0) return test_acc, test_total, test_loss
[ "wandb.log", "torch.ones_like", "copy.deepcopy", "numpy.random.seed", "torch.where", "torch.nn.CrossEntropyLoss", "time.time", "logging.info", "torch.max", "torch.no_grad" ]
[((1748, 1763), 'torch.no_grad', 'torch.no_grad', ([], {}), '()\n', (1761, 1763), False, 'import torch\n'), ((6728, 6773), 'logging.info', 'logging.info', (["('add_model. index = %d' % index)"], {}), "('add_model. index = %d' % index)\n", (6740, 6773), False, 'import logging\n'), ((7276, 7287), 'time.time', 'time.time', ([], {}), '()\n', (7285, 7287), False, 'import time\n'), ((9276, 9287), 'time.time', 'time.time', ([], {}), '()\n', (9285, 9287), False, 'import time\n'), ((9296, 9361), 'logging.info', 'logging.info', (["('aggregate time cost: %d' % (end_time - start_time))"], {}), "('aggregate time cost: %d' % (end_time - start_time))\n", (9308, 9361), False, 'import logging\n'), ((9556, 9581), 'numpy.random.seed', 'np.random.seed', (['round_idx'], {}), '(round_idx)\n', (9570, 9581), True, 'import numpy as np\n'), ((1928, 1948), 'torch.max', 'torch.max', (['output', '(1)'], {}), '(output, 1)\n', (1937, 1948), False, 'import torch\n'), ((11862, 11917), 'wandb.log', 'wandb.log', (["{'Train/Acc': train_acc, 'round': round_idx}"], {}), "({'Train/Acc': train_acc, 'round': round_idx})\n", (11871, 11917), False, 'import wandb\n'), ((11930, 11987), 'wandb.log', 'wandb.log', (["{'Train/Loss': train_loss, 'round': round_idx}"], {}), "({'Train/Loss': train_loss, 'round': round_idx})\n", (11939, 11987), False, 'import wandb\n'), ((12077, 12096), 'logging.info', 'logging.info', (['stats'], {}), '(stats)\n', (12089, 12096), False, 'import logging\n'), ((12280, 12333), 'wandb.log', 'wandb.log', (["{'Test/Acc': test_acc, 'round': round_idx}"], {}), "({'Test/Acc': test_acc, 'round': round_idx})\n", (12289, 12333), False, 'import wandb\n'), ((12346, 12401), 'wandb.log', 'wandb.log', (["{'Test/Loss': test_loss, 'round': round_idx}"], {}), "({'Test/Loss': test_loss, 'round': round_idx})\n", (12355, 12401), False, 'import wandb\n'), ((12481, 12500), 'logging.info', 'logging.info', (['stats'], {}), '(stats)\n', (12493, 12500), False, 'import logging\n'), ((13064, 13079), 'torch.no_grad', 'torch.no_grad', ([], {}), '()\n', (13077, 13079), False, 'import torch\n'), ((2459, 2494), 'torch.where', 'torch.where', (['(target == target_class)'], {}), '(target == target_class)\n', (2470, 2494), False, 'import torch\n'), ((2529, 2568), 'torch.ones_like', 'torch.ones_like', (['target[backdoor_index]'], {}), '(target[backdoor_index])\n', (2544, 2568), False, 'import torch\n'), ((13013, 13034), 'torch.nn.CrossEntropyLoss', 'nn.CrossEntropyLoss', ([], {}), '()\n', (13032, 13034), False, 'from torch import nn\n'), ((13346, 13365), 'torch.max', 'torch.max', (['pred', '(-1)'], {}), '(pred, -1)\n', (13355, 13365), False, 'import torch\n'), ((11110, 11142), 'copy.deepcopy', 'copy.deepcopy', (['train_tot_correct'], {}), '(train_tot_correct)\n', (11123, 11142), False, 'import copy\n'), ((11185, 11216), 'copy.deepcopy', 'copy.deepcopy', (['train_num_sample'], {}), '(train_num_sample)\n', (11198, 11216), False, 'import copy\n'), ((11254, 11279), 'copy.deepcopy', 'copy.deepcopy', (['train_loss'], {}), '(train_loss)\n', (11267, 11279), False, 'import copy\n'), ((11503, 11534), 'copy.deepcopy', 'copy.deepcopy', (['test_tot_correct'], {}), '(test_tot_correct)\n', (11516, 11534), False, 'import copy\n'), ((11576, 11606), 'copy.deepcopy', 'copy.deepcopy', (['test_num_sample'], {}), '(test_num_sample)\n', (11589, 11606), False, 'import copy\n'), ((11643, 11667), 'copy.deepcopy', 'copy.deepcopy', (['test_loss'], {}), '(test_loss)\n', (11656, 11667), False, 'import copy\n'), ((12679, 12700), 'torch.nn.CrossEntropyLoss', 'nn.CrossEntropyLoss', ([], {}), '()\n', (12698, 12700), False, 'from torch import nn\n')]
import numpy as np import h5py import pandas as pd from svhn_io import load_svhn from keras_uncertainty.utils import classifier_calibration_curve, classifier_calibration_error EPSILON = 1e-10 def load_hdf5_data(filename): inp = h5py.File(filename, "r") preds = inp["preds"][...] inp.close() return preds NUM_ENSEMBLES = 15 NUM_BINS=7 #IOD_FILE_PATTERN = "cnn_svhn-num_ens-{}-preds.hdf5" #OUTPUT_PATTERN = "svhn-calibration-sub-deepensembles_1_num-ens-{}_cnn_svhn.csv" IOD_FILE_PATTERN = "deepensembles-cnn_svhn-num_ens-{}-preds.hdf5" OUTPUT_PATTERN = "svhn-calibration-deepensembles-num-ens-{}_cnn_svhn.csv" if __name__ == "__main__": for num_ens in range(1, NUM_ENSEMBLES + 1): (_, __), (___, y_true) = load_svhn() y_true = y_true.flatten() y_probs = load_hdf5_data(IOD_FILE_PATTERN.format(num_ens)) y_confs = np.max(y_probs, axis=1) y_pred = np.argmax(y_probs, axis=1) curve_conf, curve_acc = classifier_calibration_curve(y_pred, y_true, y_confs, num_bins=NUM_BINS) error = classifier_calibration_error(y_pred, y_true, y_confs, num_bins=NUM_BINS) print("Processing calibration curve for {} ensembles. Error: {}".format(num_ens, error)) output_df = pd.DataFrame(data={"conf": curve_conf, "acc": curve_acc}) output_df.to_csv(OUTPUT_PATTERN.format(num_ens), sep=';', index=False)
[ "pandas.DataFrame", "h5py.File", "numpy.argmax", "keras_uncertainty.utils.classifier_calibration_curve", "numpy.max", "keras_uncertainty.utils.classifier_calibration_error", "svhn_io.load_svhn" ]
[((235, 259), 'h5py.File', 'h5py.File', (['filename', '"""r"""'], {}), "(filename, 'r')\n", (244, 259), False, 'import h5py\n'), ((741, 752), 'svhn_io.load_svhn', 'load_svhn', ([], {}), '()\n', (750, 752), False, 'from svhn_io import load_svhn\n'), ((873, 896), 'numpy.max', 'np.max', (['y_probs'], {'axis': '(1)'}), '(y_probs, axis=1)\n', (879, 896), True, 'import numpy as np\n'), ((914, 940), 'numpy.argmax', 'np.argmax', (['y_probs'], {'axis': '(1)'}), '(y_probs, axis=1)\n', (923, 940), True, 'import numpy as np\n'), ((974, 1046), 'keras_uncertainty.utils.classifier_calibration_curve', 'classifier_calibration_curve', (['y_pred', 'y_true', 'y_confs'], {'num_bins': 'NUM_BINS'}), '(y_pred, y_true, y_confs, num_bins=NUM_BINS)\n', (1002, 1046), False, 'from keras_uncertainty.utils import classifier_calibration_curve, classifier_calibration_error\n'), ((1063, 1135), 'keras_uncertainty.utils.classifier_calibration_error', 'classifier_calibration_error', (['y_pred', 'y_true', 'y_confs'], {'num_bins': 'NUM_BINS'}), '(y_pred, y_true, y_confs, num_bins=NUM_BINS)\n', (1091, 1135), False, 'from keras_uncertainty.utils import classifier_calibration_curve, classifier_calibration_error\n'), ((1255, 1312), 'pandas.DataFrame', 'pd.DataFrame', ([], {'data': "{'conf': curve_conf, 'acc': curve_acc}"}), "(data={'conf': curve_conf, 'acc': curve_acc})\n", (1267, 1312), True, 'import pandas as pd\n')]
import numpy as np import sys import gpflow import VFF from time import time from config import * dim = sys.argv[1] rep = sys.argv[2] print('vff: dimension {}, replicate {}'.format(dim, r)) # data data = np.load('data/data_dim{}_rep{}.npz'.format(dim, 0)) # full_gp def prodkern(dim): return gpflow.kernels.Prod([gpflow.kernels.Matern32(1, active_dims=[i], lengthscales=lengthscale) for i in range(dim)]) k = prodkern(dim) m = gpflow.gpr.GPR(data['Xtrain'], data['Ytrain'], kern=k) m.likelihood.variance = noise_var data = np.load('data/data_dim{}_rep{}.npz'.format(dim, r)) marg_lik = m.compute_log_likelihood().squeeze() mean_log_pred = np.mean(m.predict_density(data['Xtest'], data['Ytest'])) file = open("results/full.csv","a") file.write("{}, {}, {}, {}".format(dim, rep, marg_lik, mean_log_pred)) file.close() ########################## # kron results = pd.DataFrame() for dim in dimensions: a, b = -1.5 * np.ones(dim), 1.5 * np.ones(dim) k = prodkern(dim) for r in range(repeats): print('kron replicate ',r,'/',repeats) data = np.load('data/data_dim{}_rep{}.npz'.format(dim, r)) for M in num_freqs: if (2*M-1)**dim: a, b = -0.5 * np.ones(dim), 1.5 * np.ones(dim) m = VFF.vgp.VGP_kron(data['Xtrain'], data['Ytrain'], np.arange(M), a, b, kerns=prodkern(dim).kern_list, likelihood=gpflow.likelihoods.Gaussian(), use_two_krons=True) m.likelihood.variance = noise_var # only optimize q(u) m.kerns.fixed = True m.likelihood.fixed = True start = time() m.optimize() marg_lik = m.compute_log_likelihood().squeeze() mean_log_pred = np.mean(m.predict_density(data['Xtest'], data['Ytest'])) t = time() - start results = results.append(dict(dim=dim, rep=r, marg_lik=marg_lik, mean_log_pred=mean_log_pred, time=t, num_inducing=M), ignore_index=True) # do this inside the loop so we can get partial results if something crashes results.to_csv('results/kron.csv') ########################## # kron_opt results = pd.DataFrame() for dim in dimensions: a, b = -1.5 * np.ones(dim), 1.5 * np.ones(dim) k = prodkern(dim) for r in range(repeats): print('kron_opt replicate ',r,'/',repeats) data = np.load('data/data_dim{}_rep{}.npz'.format(dim, r)) for M in num_freqs: if (2*M-1)**dim: m = VFF.vgp.VGP_kron(data['Xtrain'], data['Ytrain'], np.arange(M), a, b, kerns=k.kern_list, likelihood=gpflow.likelihoods.Gaussian(), use_two_krons=True) m.likelihood.variance = noise_var # build kronecker GP model start = time() m.optimize() marg_lik = m.compute_log_likelihood().squeeze() mean_log_pred = np.mean(m.predict_density(data['Xtest'], data['Ytest'])) t = time() - start results = results.append(dict(dim=dim, rep=r, marg_lik=marg_lik, mean_log_pred=mean_log_pred, time=t, num_inducing=M), ignore_index=True) results.to_csv('results/kron_opt.csv') ########################## # Sparse results = pd.DataFrame() for dim in dimensions: for r in range(repeats): print('Sparse replicate ',r,'/',repeats) data = np.load('data/data_dim{}_rep{}.npz'.format(dim, r)) num_inducing = (2*num_freqs-1)**dim for M in num_inducing: if M < 500: # build sparse GP model Z = KMeans(n_clusters=M).fit(data['Xtrain']).cluster_centers_ m = gpflow.sgpr.SGPR(data['Xtrain'], data['Ytrain'], Z=Z, kern=prodkern(dim)) m.likelihood.variance = noise_var start = time() marg_lik = m.compute_log_likelihood().squeeze() mean_log_pred = np.mean(m.predict_density(data['Xtest'], data['Ytest'])) t = time() - start results = results.append(dict(dim=dim, rep=r, marg_lik=marg_lik, mean_log_pred=mean_log_pred, time=t, num_inducing=M), ignore_index=True) # do this inside the loop so we can get partial results if something crashes results.to_csv('results/sparse_kmeans.csv') ########################## # Sparse GP opt results = pd.DataFrame() for dim in dimensions: for r in range(repeats): print('sparse opt replicate ',r,'/',repeats) data = np.load('data/data_dim{}_rep{}.npz'.format(dim, r)) num_inducing = (2*num_freqs-1)**dim for M in num_inducing: if M < 500: # build sparse GP model Z = KMeans(n_clusters=M).fit(data['Xtrain']).cluster_centers_ m = gpflow.sgpr.SGPR(data['Xtrain'], data['Ytrain'], Z=Z, kern=prodkern(dim)) m.likelihood.variance = noise_var # only optimize Z m.kern.fixed = True m.likelihood.fixed = True start = time() m.optimize() marg_lik = m.compute_log_likelihood().squeeze() mean_log_pred = np.mean(m.predict_density(data['Xtest'], data['Ytest'])) t = time() - start results = results.append(dict(dim=dim, rep=r, marg_lik=marg_lik, mean_log_pred=mean_log_pred, time=t, num_inducing=M), ignore_index=True) # do this inside the loop so we can get partial results if something crashes results.to_csv('results/sparse_opt.csv') ########################## #
[ "gpflow.likelihoods.Gaussian", "numpy.ones", "time.time", "numpy.arange", "gpflow.gpr.GPR", "gpflow.kernels.Matern32" ]
[((469, 523), 'gpflow.gpr.GPR', 'gpflow.gpr.GPR', (["data['Xtrain']", "data['Ytrain']"], {'kern': 'k'}), "(data['Xtrain'], data['Ytrain'], kern=k)\n", (483, 523), False, 'import gpflow\n'), ((323, 392), 'gpflow.kernels.Matern32', 'gpflow.kernels.Matern32', (['(1)'], {'active_dims': '[i]', 'lengthscales': 'lengthscale'}), '(1, active_dims=[i], lengthscales=lengthscale)\n', (346, 392), False, 'import gpflow\n'), ((965, 977), 'numpy.ones', 'np.ones', (['dim'], {}), '(dim)\n', (972, 977), True, 'import numpy as np\n'), ((985, 997), 'numpy.ones', 'np.ones', (['dim'], {}), '(dim)\n', (992, 997), True, 'import numpy as np\n'), ((2532, 2544), 'numpy.ones', 'np.ones', (['dim'], {}), '(dim)\n', (2539, 2544), True, 'import numpy as np\n'), ((2552, 2564), 'numpy.ones', 'np.ones', (['dim'], {}), '(dim)\n', (2559, 2564), True, 'import numpy as np\n'), ((1769, 1775), 'time.time', 'time', ([], {}), '()\n', (1773, 1775), False, 'from time import time\n'), ((3189, 3195), 'time.time', 'time', ([], {}), '()\n', (3193, 3195), False, 'from time import time\n'), ((4377, 4383), 'time.time', 'time', ([], {}), '()\n', (4381, 4383), False, 'from time import time\n'), ((5759, 5765), 'time.time', 'time', ([], {}), '()\n', (5763, 5765), False, 'from time import time\n'), ((1353, 1365), 'numpy.arange', 'np.arange', (['M'], {}), '(M)\n', (1362, 1365), True, 'import numpy as np\n'), ((1978, 1984), 'time.time', 'time', ([], {}), '()\n', (1982, 1984), False, 'from time import time\n'), ((2860, 2872), 'numpy.arange', 'np.arange', (['M'], {}), '(M)\n', (2869, 2872), True, 'import numpy as np\n'), ((3398, 3404), 'time.time', 'time', ([], {}), '()\n', (3402, 3404), False, 'from time import time\n'), ((4557, 4563), 'time.time', 'time', ([], {}), '()\n', (4561, 4563), False, 'from time import time\n'), ((5968, 5974), 'time.time', 'time', ([], {}), '()\n', (5972, 5974), False, 'from time import time\n'), ((1251, 1263), 'numpy.ones', 'np.ones', (['dim'], {}), '(dim)\n', (1258, 1263), True, 'import numpy as np\n'), ((1271, 1283), 'numpy.ones', 'np.ones', (['dim'], {}), '(dim)\n', (1278, 1283), True, 'import numpy as np\n'), ((1489, 1518), 'gpflow.likelihoods.Gaussian', 'gpflow.likelihoods.Gaussian', ([], {}), '()\n', (1516, 1518), False, 'import gpflow\n'), ((2984, 3013), 'gpflow.likelihoods.Gaussian', 'gpflow.likelihoods.Gaussian', ([], {}), '()\n', (3011, 3013), False, 'import gpflow\n')]
import torch.utils.data as data import os import os.path from numpy.random import randint from ops.io import load_proposal_file from transforms import * from ops.utils import temporal_iou class SSNInstance: def __init__( self, start_frame, end_frame, video_frame_count, fps=1, label=None, best_iou=None, overlap_self=None, ): self.start_frame = start_frame self.end_frame = min(end_frame, video_frame_count) self._label = label self.fps = fps self.coverage = (end_frame - start_frame) / video_frame_count self.best_iou = best_iou self.overlap_self = overlap_self self.loc_reg = None self.size_reg = None def compute_regression_targets(self, gt_list, fg_thresh): if self.best_iou < fg_thresh: # background proposals do not need this return # find the groundtruth instance with the highest IOU ious = [ temporal_iou( (self.start_frame, self.end_frame), (gt.start_frame, gt.end_frame) ) for gt in gt_list ] best_gt_id = np.argmax(ious) best_gt = gt_list[best_gt_id] prop_center = (self.start_frame + self.end_frame) / 2 gt_center = (best_gt.start_frame + best_gt.end_frame) / 2 prop_size = self.end_frame - self.start_frame + 1 gt_size = best_gt.end_frame - best_gt.start_frame + 1 # get regression target: # (1). center shift propotional to the proposal duration # (2). logarithm of the groundtruth duration over proposal duraiton self.loc_reg = (gt_center - prop_center) / prop_size try: self.size_reg = math.log(gt_size / prop_size) except: print((gt_size, prop_size, self.start_frame, self.end_frame)) raise @property def start_time(self): return self.start_frame / self.fps @property def end_time(self): return self.end_frame / self.fps @property def label(self): return self._label if self._label is not None else -1 @property def regression_targets(self): return [self.loc_reg, self.size_reg] if self.loc_reg is not None else [0, 0] class SSNVideoRecord: def __init__(self, prop_record): self._data = prop_record frame_count = int(self._data[1]) # build instance record self.gt = [ SSNInstance( int(x[1]), int(x[2]), frame_count, label=int(x[0]), best_iou=1.0 ) for x in self._data[2] if int(x[2]) > int(x[1]) ] self.gt = list([x for x in self.gt if x.start_frame < frame_count]) self.proposals = [ SSNInstance( int(x[3]), int(x[4]), frame_count, label=int(x[0]), best_iou=float(x[1]), overlap_self=float(x[2]), ) for x in self._data[3] if int(x[4]) > int(x[3]) ] self.proposals = list( [x for x in self.proposals if x.start_frame < frame_count] ) @property def id(self): return self._data[0] @property def num_frames(self): return int(self._data[1]) def get_fg(self, fg_thresh, with_gt=True): fg = [p for p in self.proposals if p.best_iou > fg_thresh] if with_gt: fg.extend(self.gt) for x in fg: x.compute_regression_targets(self.gt, fg_thresh) return fg def get_negatives( self, incomplete_iou_thresh, bg_iou_thresh, bg_coverage_thresh=0.01, incomplete_overlap_thresh=0.7, ): tag = [0] * len(self.proposals) incomplete_props = [] background_props = [] for i in range(len(tag)): if ( self.proposals[i].best_iou < incomplete_iou_thresh and self.proposals[i].overlap_self > incomplete_overlap_thresh ): tag[i] = 1 # incomplete incomplete_props.append(self.proposals[i]) for i in range(len(tag)): if ( tag[i] == 0 and self.proposals[i].best_iou < bg_iou_thresh and self.proposals[i].coverage > bg_coverage_thresh ): background_props.append(self.proposals[i]) return incomplete_props, background_props class SSNDataSet(data.Dataset): def __init__( self, root_path, prop_file=None, body_seg=5, aug_seg=2, video_centric=True, new_length=1, modality="RGB", image_tmpl="img_{:05d}.jpg", transform=None, random_shift=True, test_mode=False, prop_per_video=8, fg_ratio=1, bg_ratio=1, incomplete_ratio=6, fg_iou_thresh=0.7, bg_iou_thresh=0.01, incomplete_iou_thresh=0.3, bg_coverage_thresh=0.02, incomplete_overlap_thresh=0.7, gt_as_fg=True, reg_stats=None, test_interval=6, verbose=True, exclude_empty=True, epoch_multiplier=1, ): self.root_path = root_path self.prop_file = prop_file self.verbose = verbose self.body_seg = body_seg self.aug_seg = aug_seg self.video_centric = video_centric self.exclude_empty = exclude_empty self.epoch_multiplier = epoch_multiplier self.new_length = new_length self.modality = modality self.image_tmpl = image_tmpl self.transform = transform self.random_shift = random_shift self.test_mode = test_mode self.test_interval = test_interval self.fg_iou_thresh = fg_iou_thresh self.incomplete_iou_thresh = incomplete_iou_thresh self.bg_iou_thresh = bg_iou_thresh self.bg_coverage_thresh = bg_coverage_thresh self.incomplete_overlap_thresh = incomplete_overlap_thresh self.starting_ratio = 0.5 self.ending_ratio = 0.5 self.gt_as_fg = gt_as_fg denum = fg_ratio + bg_ratio + incomplete_ratio self.fg_per_video = int(prop_per_video * (fg_ratio / denum)) self.bg_per_video = int(prop_per_video * (bg_ratio / denum)) self.incomplete_per_video = ( prop_per_video - self.fg_per_video - self.bg_per_video ) self._parse_prop_file(stats=reg_stats) def _load_image(self, directory, idx): if self.modality == "RGB" or self.modality == "RGBDiff": return [ Image.open( os.path.join(directory, self.image_tmpl.format(idx)) ).convert("RGB") ] elif self.modality == "Flow": x_img = Image.open( os.path.join(directory, self.image_tmpl.format("x", idx)) ).convert("L") y_img = Image.open( os.path.join(directory, self.image_tmpl.format("y", idx)) ).convert("L") return [x_img, y_img] def _parse_prop_file(self, stats=None): prop_info = load_proposal_file(self.prop_file) self.video_list = [SSNVideoRecord(p) for p in prop_info] if self.exclude_empty: self.video_list = list([x for x in self.video_list if len(x.gt) > 0]) self.video_dict = {v.id: v for v in self.video_list} # construct three pools: # 1. Foreground # 2. Background # 3. Incomplete self.fg_pool = [] self.bg_pool = [] self.incomp_pool = [] for v in self.video_list: self.fg_pool.extend( [(v.id, prop) for prop in v.get_fg(self.fg_iou_thresh, self.gt_as_fg)] ) incomp, bg = v.get_negatives( self.incomplete_iou_thresh, self.bg_iou_thresh, self.bg_coverage_thresh, self.incomplete_overlap_thresh, ) self.incomp_pool.extend([(v.id, prop) for prop in incomp]) self.bg_pool.extend([(v.id, prop) for prop in bg]) if stats is None: self._compute_regresssion_stats() else: self.stats = stats if self.verbose: print( ( """ SSNDataset: Proposal file {prop_file} parsed. There are {pnum} usable proposals from {vnum} videos. {fnum} foreground proposals {inum} incomplete_proposals {bnum} background_proposals Sampling config: FG/BG/INC: {fr}/{br}/{ir} Video Centric: {vc} Epoch size multiplier: {em} Regression Stats: Location: mean {stats[0][0]:.05f} std {stats[1][0]:.05f} Duration: mean {stats[0][1]:.05f} std {stats[1][1]:.05f} """.format( prop_file=self.prop_file, pnum=len(self.fg_pool) + len(self.bg_pool) + len(self.incomp_pool), fnum=len(self.fg_pool), inum=len(self.incomp_pool), bnum=len(self.bg_pool), fr=self.fg_per_video, br=self.bg_per_video, ir=self.incomplete_per_video, vnum=len(self.video_dict), vc=self.video_centric, stats=self.stats, em=self.epoch_multiplier, ) ) ) else: print( ( """ SSNDataset: Proposal file {prop_file} parsed. """.format( prop_file=self.prop_file ) ) ) def _video_centric_sampling(self, video): fg = video.get_fg(self.fg_iou_thresh, self.gt_as_fg) incomp, bg = video.get_negatives( self.incomplete_iou_thresh, self.bg_iou_thresh, self.bg_coverage_thresh, self.incomplete_overlap_thresh, ) def sample_video_proposals( proposal_type, video_id, video_pool, requested_num, dataset_pool ): if len(video_pool) == 0: # if there is nothing in the video pool, go fetch from the dataset pool return [ (dataset_pool[x], proposal_type) for x in np.random.choice( len(dataset_pool), requested_num, replace=False ) ] else: replicate = len(video_pool) < requested_num idx = np.random.choice( len(video_pool), requested_num, replace=replicate ) return [((video_id, video_pool[x]), proposal_type) for x in idx] out_props = [] out_props.extend( sample_video_proposals(0, video.id, fg, self.fg_per_video, self.fg_pool) ) # sample foreground out_props.extend( sample_video_proposals( 1, video.id, incomp, self.incomplete_per_video, self.incomp_pool ) ) # sample incomp. out_props.extend( sample_video_proposals(2, video.id, bg, self.bg_per_video, self.bg_pool) ) # sample background return out_props def _random_sampling(self): out_props = [] out_props.extend( [ (x, 0) for x in np.random.choice( self.fg_pool, self.fg_per_video, replace=False ) ] ) out_props.extend( [ (x, 1) for x in np.random.choice( self.incomp_pool, self.incomplete_per_video, replace=False ) ] ) out_props.extend( [ (x, 2) for x in np.random.choice( self.bg_pool, self.bg_per_video, replace=False ) ] ) return out_props def _sample_indices(self, valid_length, num_seg): """ :param record: VideoRecord :return: list """ average_duration = (valid_length + 1) // num_seg if average_duration > 0: # normal cases offsets = np.multiply(list(range(num_seg)), average_duration) + randint( average_duration, size=num_seg ) elif valid_length > num_seg: offsets = np.sort(randint(valid_length, size=num_seg)) else: offsets = np.zeros((num_seg,)) return offsets def _get_val_indices(self, valid_length, num_seg): if valid_length > num_seg: tick = valid_length / float(num_seg) offsets = np.array([int(tick / 2.0 + tick * x) for x in range(num_seg)]) else: offsets = np.zeros((num_seg,)) return offsets def _sample_ssn_indices(self, prop, frame_cnt): start_frame = prop.start_frame + 1 end_frame = prop.end_frame duration = end_frame - start_frame + 1 assert duration != 0, (prop.start_frame, prop.end_frame, prop.best_iou) valid_length = duration - self.new_length valid_starting = max(1, start_frame - int(duration * self.starting_ratio)) valid_ending = min( frame_cnt - self.new_length + 1, end_frame + int(duration * self.ending_ratio), ) valid_starting_length = start_frame - valid_starting - self.new_length + 1 valid_ending_length = valid_ending - end_frame - self.new_length + 1 starting_scale = (valid_starting_length + self.new_length - 1) / ( duration * self.starting_ratio ) ending_scale = (valid_ending_length + self.new_length - 1) / ( duration * self.ending_ratio ) # get starting starting_offsets = ( self._sample_indices(valid_starting_length, self.aug_seg) if self.random_shift else self._get_val_indices(valid_starting_length, self.aug_seg) ) + valid_starting course_offsets = ( self._sample_indices(valid_length, self.body_seg) if self.random_shift else self._get_val_indices(valid_length, self.body_seg) ) + start_frame ending_offsets = ( self._sample_indices(valid_ending_length, self.aug_seg) if self.random_shift else self._get_val_indices(valid_ending_length, self.aug_seg) ) + end_frame offsets = np.concatenate((starting_offsets, course_offsets, ending_offsets)) stage_split = [ self.aug_seg, self.aug_seg + self.body_seg, self.aug_seg * 2 + self.body_seg, ] return offsets, starting_scale, ending_scale, stage_split def _load_prop_data(self, prop): # read frame count frame_cnt = self.video_dict[prop[0][0]].num_frames # sample segment indices prop_indices, starting_scale, ending_scale, stage_split = self._sample_ssn_indices( prop[0][1], frame_cnt ) # turn prop into standard format # get label if prop[1] == 0: label = prop[0][1].label elif prop[1] == 1: label = prop[0][1].label # incomplete elif prop[1] == 2: label = 0 # background else: raise ValueError() frames = [] for idx, seg_ind in enumerate(prop_indices): p = int(seg_ind) for x in range(self.new_length): frames.extend(self._load_image(prop[0][0], min(frame_cnt, p + x))) # get regression target if prop[1] == 0: reg_targets = prop[0][1].regression_targets reg_targets = ( (reg_targets[0] - self.stats[0][0]) / self.stats[1][0], (reg_targets[1] - self.stats[0][1]) / self.stats[1][1], ) else: reg_targets = (0.0, 0.0) return ( frames, label, reg_targets, starting_scale, ending_scale, stage_split, prop[1], ) def _compute_regresssion_stats(self): if self.verbose: print("computing regression target normalizing constants") targets = [] for video in self.video_list: fg = video.get_fg(self.fg_iou_thresh, False) for p in fg: targets.append(list(p.regression_targets)) self.stats = np.array((np.mean(targets, axis=0), np.std(targets, axis=0))) def get_test_data(self, video, test_interval, gen_batchsize=4): props = video.proposals video_id = video.id frame_cnt = video.num_frames frame_ticks = ( np.arange(0, frame_cnt - self.new_length, test_interval, dtype=np.int) + 1 ) num_sampled_frames = len(frame_ticks) # avoid empty proposal list if len(props) == 0: props.append(SSNInstance(0, frame_cnt - 1, frame_cnt)) # process proposals to subsampled sequences rel_prop_list = [] proposal_tick_list = [] scaling_list = [] for proposal in props: rel_prop = proposal.start_frame / frame_cnt, proposal.end_frame / frame_cnt rel_duration = rel_prop[1] - rel_prop[0] rel_starting_duration = rel_duration * self.starting_ratio rel_ending_duration = rel_duration * self.ending_ratio rel_starting = rel_prop[0] - rel_starting_duration rel_ending = rel_prop[1] + rel_ending_duration real_rel_starting = max(0.0, rel_starting) real_rel_ending = min(1.0, rel_ending) starting_scaling = (rel_prop[0] - real_rel_starting) / rel_starting_duration ending_scaling = (real_rel_ending - rel_prop[1]) / rel_ending_duration proposal_ticks = ( int(real_rel_starting * num_sampled_frames), int(rel_prop[0] * num_sampled_frames), int(rel_prop[1] * num_sampled_frames), int(real_rel_ending * num_sampled_frames), ) rel_prop_list.append(rel_prop) proposal_tick_list.append(proposal_ticks) scaling_list.append((starting_scaling, ending_scaling)) # load frames # Since there are many frames for each video during testing, instead of returning the read frames, # we return a generator which gives the frames in small batches, this lower the memory burden # and runtime overhead. Usually setting batchsize=4 would fit most cases. def frame_gen(batchsize): frames = [] cnt = 0 for idx, seg_ind in enumerate(frame_ticks): p = int(seg_ind) for x in range(self.new_length): frames.extend(self._load_image(video_id, min(frame_cnt, p + x))) cnt += 1 if cnt % batchsize == 0: frames = self.transform(frames) yield frames frames = [] if len(frames): frames = self.transform(frames) yield frames return ( frame_gen(gen_batchsize), len(frame_ticks), torch.from_numpy(np.array(rel_prop_list)), torch.from_numpy(np.array(proposal_tick_list)), torch.from_numpy(np.array(scaling_list)), ) def get_training_data(self, index): if self.video_centric: video = self.video_list[index] props = self._video_centric_sampling(video) else: props = self._random_sampling() out_frames = [] out_prop_len = [] out_prop_scaling = [] out_prop_type = [] out_prop_labels = [] out_prop_reg_targets = [] out_stage_split = [] for idx, p in enumerate(props): prop_frames, prop_label, reg_targets, starting_scale, ending_scale, stage_split, prop_type = self._load_prop_data( p ) processed_frames = self.transform(prop_frames) out_frames.append(processed_frames) out_prop_len.append(self.body_seg + 2 * self.aug_seg) out_prop_scaling.append([starting_scale, ending_scale]) out_prop_labels.append(prop_label) out_prop_reg_targets.append(reg_targets) out_prop_type.append(prop_type) out_stage_split.append(stage_split) out_prop_len = torch.from_numpy(np.array(out_prop_len)) out_prop_scaling = torch.from_numpy( np.array(out_prop_scaling, dtype=np.float32) ) out_prop_labels = torch.from_numpy(np.array(out_prop_labels)) out_prop_reg_targets = torch.from_numpy( np.array(out_prop_reg_targets, dtype=np.float32) ) out_prop_type = torch.from_numpy(np.array(out_prop_type)) out_stage_split = torch.from_numpy(np.array(out_stage_split)) out_frames = torch.cat(out_frames) return ( out_frames, out_prop_len, out_prop_scaling, out_prop_type, out_prop_labels, out_prop_reg_targets, out_stage_split, ) def get_all_gt(self): gt_list = [] for video in self.video_list: vid = video.id gt_list.extend( [ [ vid, x.label - 1, x.start_frame / video.num_frames, x.end_frame / video.num_frames, ] for x in video.gt ] ) return gt_list def __getitem__(self, index): real_index = index % len(self.video_list) if self.test_mode: return self.get_test_data(self.video_list[real_index], self.test_interval) else: return self.get_training_data(real_index) def __len__(self): return len(self.video_list) * self.epoch_multiplier
[ "ops.io.load_proposal_file", "numpy.random.randint", "ops.utils.temporal_iou" ]
[((7277, 7311), 'ops.io.load_proposal_file', 'load_proposal_file', (['self.prop_file'], {}), '(self.prop_file)\n', (7295, 7311), False, 'from ops.io import load_proposal_file\n'), ((1017, 1102), 'ops.utils.temporal_iou', 'temporal_iou', (['(self.start_frame, self.end_frame)', '(gt.start_frame, gt.end_frame)'], {}), '((self.start_frame, self.end_frame), (gt.start_frame, gt.end_frame)\n )\n', (1029, 1102), False, 'from ops.utils import temporal_iou\n'), ((12799, 12838), 'numpy.random.randint', 'randint', (['average_duration'], {'size': 'num_seg'}), '(average_duration, size=num_seg)\n', (12806, 12838), False, 'from numpy.random import randint\n'), ((12936, 12971), 'numpy.random.randint', 'randint', (['valid_length'], {'size': 'num_seg'}), '(valid_length, size=num_seg)\n', (12943, 12971), False, 'from numpy.random import randint\n')]
"""Central data class and associated.""" # --- import -------------------------------------------------------------------------------------- import collections import operator import functools import warnings import numpy as np import h5py import scipy from scipy.interpolate import griddata, interp1d from .._group import Group from .. import collection as wt_collection from .. import exceptions as wt_exceptions from .. import kit as wt_kit from .. import units as wt_units from ._axis import Axis, identifier_to_operator from ._channel import Channel from ._constant import Constant from ._variable import Variable # --- define -------------------------------------------------------------------------------------- __all__ = ["Data"] # --- class --------------------------------------------------------------------------------------- class Data(Group): """Multidimensional dataset.""" class_name = "Data" def __init__(self, *args, **kwargs): self._axes = [] self._constants = [] Group.__init__(self, *args, **kwargs) # populate axes, constants from attrs string for identifier in self.attrs.get("axes", []): if hasattr(identifier, "decode"): identifier = identifier.decode() expression, units = identifier.split("{") units = units.replace("}", "").strip() if units == "None": units = None # Should not be needed for wt5 >= 1.0.3, kept for opening older wt5 files. for i in identifier_to_operator.keys(): expression = expression.replace(i, identifier_to_operator[i]) expression = expression.replace(" ", "") # remove all whitespace axis = Axis(self, expression, units) self._axes.append(axis) for identifier in self.attrs.get("constants", []): if hasattr(identifier, "decode"): identifier = identifier.decode() expression, units = identifier.split("{") units = units.replace("}", "").strip() if units == "None": units = None for i in identifier_to_operator.keys(): expression = expression.replace(i, identifier_to_operator[i]) expression = expression.replace(" ", "") # remove all whitespace const = Constant(self, expression, units) self._constants.append(const) self._current_axis_identities_in_natural_namespace = [] if self.file.mode is not None and self.file.mode != "r": self._on_constants_updated() self._on_axes_updated() # the following are populated if not already recorded self.channel_names self.source self.variable_names def __repr__(self) -> str: return "<WrightTools.Data '{0}' {1} at {2}>".format( self.natural_name, str(self.axis_names), "::".join([self.filepath, self.name]) ) @property def axes(self) -> tuple: return tuple(self._axes) @property def axis_expressions(self) -> tuple: """Axis expressions.""" return tuple(a.expression for a in self._axes) @property def axis_names(self) -> tuple: """Axis names.""" return tuple(a.natural_name for a in self._axes) @property def constants(self) -> tuple: return tuple(self._constants) @property def constant_expressions(self) -> tuple: """Axis expressions.""" return tuple(a.expression for a in self._constants) @property def constant_names(self) -> tuple: """Axis names.""" return tuple(a.natural_name for a in self._constants) @property def channel_names(self) -> tuple: """Channel names.""" if "channel_names" not in self.attrs.keys(): self.attrs["channel_names"] = np.array([], dtype="S") return tuple(s.decode() for s in self.attrs["channel_names"]) @channel_names.setter def channel_names(self, value): """Set channel names.""" self.attrs["channel_names"] = np.array(value, dtype="S") @property def channels(self) -> tuple: """Channels.""" return tuple(self[n] for n in self.channel_names) @property def datasets(self) -> tuple: """Datasets.""" return tuple(v for _, v in self.items() if isinstance(v, h5py.Dataset)) @property def kind(self): """Kind.""" if "kind" not in self.attrs.keys(): self.attrs["kind"] = "None" value = self.attrs["kind"] return value if not value == "None" else None @property def ndim(self) -> int: """Get number of dimensions.""" try: assert self._ndim is not None except (AssertionError, AttributeError): if len(self.variables) == 0: self._ndim = 0 else: self._ndim = self.variables[0].ndim finally: return self._ndim @property def shape(self) -> tuple: """Shape.""" try: assert self._shape is not None except (AssertionError, AttributeError): self._shape = wt_kit.joint_shape(*self.variables) finally: return self._shape @property def size(self) -> int: """Size.""" return functools.reduce(operator.mul, self.shape) @property def source(self): """Source.""" if "source" not in self.attrs.keys(): self.attrs["source"] = "None" value = self.attrs["source"] return value if not value == "None" else None @property def units(self) -> tuple: """All axis units.""" return tuple(a.units for a in self._axes) @property def constant_units(self) -> tuple: """All constant units.""" return tuple(a.units for a in self._constants) @property def variable_names(self) -> tuple: """Variable names.""" if "variable_names" not in self.attrs.keys(): self.attrs["variable_names"] = np.array([], dtype="S") return tuple(s.decode() for s in self.attrs["variable_names"]) @variable_names.setter def variable_names(self, value): """Set variable names.""" self.attrs["variable_names"] = np.array(value, dtype="S") @property def variables(self) -> tuple: """Variables.""" try: assert self._variables is not None except (AssertionError, AttributeError): self._variables = [self[n] for n in self.variable_names] finally: return tuple(self._variables) @property def _leaf(self): return "{0} {1}".format(self.natural_name, self.shape) def _on_axes_updated(self): """Method to run when axes are changed in any way. Propagates updated axes properly. """ # update attrs self.attrs["axes"] = np.array([a.identity.encode() for a in self._axes], dtype="S") # remove old attributes while len(self._current_axis_identities_in_natural_namespace) > 0: key = self._current_axis_identities_in_natural_namespace.pop(0) try: delattr(self, key) except AttributeError: pass # already gone # populate new attributes for a in self._axes: key = a.natural_name setattr(self, key, a) self._current_axis_identities_in_natural_namespace.append(key) def _on_constants_updated(self): """Method to run when constants are changed in any way. Propagates updated constants properly. """ # update attrs self.attrs["constants"] = np.array( [a.identity.encode() for a in self._constants], dtype="S" ) def _print_branch(self, prefix, depth, verbose): def print_leaves(prefix, lis, vline=True): for i, item in enumerate(lis): if vline: a = "│ " else: a = " " if i + 1 == len(lis): b = "└── " else: b = "├── " s = prefix + a + b + "{0}: {1}".format(i, item._leaf) print(s) if verbose: # axes print(prefix + "├── axes") print_leaves(prefix, self.axes) # constants print(prefix + "├── constants") print_leaves(prefix, self.constants) # variables print(prefix + "├── variables") print_leaves(prefix, self.variables) # channels print(prefix + "└── channels") print_leaves(prefix, self.channels, vline=False) else: # axes s = "axes: " s += ", ".join(["{0} ({1})".format(a.expression, a.units) for a in self.axes]) print(prefix + "├── " + s) # constants s = "constants: " s += ", ".join( ["{0} ({1} {2})".format(a.expression, a.value, a.units) for a in self.constants] ) print(prefix + "├── " + s) # channels s = "channels: " s += ", ".join(self.channel_names) print(prefix + "└── " + s) def bring_to_front(self, channel): """Bring a specific channel to the zero-indexed position in channels. All other channels get pushed back but remain in order. Parameters ---------- channel : int or str Channel index or name. """ channel_index = wt_kit.get_index(self.channel_names, channel) new = list(self.channel_names) new.insert(0, new.pop(channel_index)) self.channel_names = new def chop(self, *args, at={}, parent=None, verbose=True) -> wt_collection.Collection: """Divide the dataset into its lower-dimensionality components. Parameters ---------- axis : str or int (args) Axes of the returned data objects. Strings refer to the names of axes in this object, integers refer to their index. Provide multiple axes to return multidimensional data objects. at : dict (optional) Choice of position along an axis. Keys are axis names, values are lists ``[position, input units]``. If exact position does not exist, the closest valid position is used. parent : WrightTools Collection instance (optional) Collection to place the new "chop" collection within. Default is None (new parent). verbose : bool (optional) Toggle talkback. Default is True. Returns ------- WrightTools Collection Collection of chopped data objects. Examples -------- >>> data.axis_names ['d2', 'w1', 'w2'] Get all w1 wigners. >>> datas = data.chop('d2', 'w1') >>> len(datas) 51 Get 2D frequency at d2=0 fs. >>> datas = data.chop('w1', 'w2', at={'d2': [0, 'fs']}) >>> len(datas) 0 >>> datas[0].axis_names ['w1', 'w2'] >>> datas[0].d2[:] 0. See Also -------- collapse Collapse the dataset along one axis. split Split the dataset while maintaining its dimensionality. """ from ._axis import operators, operator_to_identifier # parse args args = list(args) for i, arg in enumerate(args): if isinstance(arg, int): args[i] = self._axes[arg].natural_name elif isinstance(arg, str): # same normalization that occurs in the natural_name @property arg = arg.strip() for op in operators: arg = arg.replace(op, operator_to_identifier[op]) args[i] = wt_kit.string2identifier(arg) # normalize the at keys to the natural name for k in [ak for ak in at.keys() if type(ak) == str]: for op in operators: if op in k: nk = k.replace(op, operator_to_identifier[op]) at[nk] = at[k] at.pop(k) k = nk # get output collection out = wt_collection.Collection(name="chop", parent=parent) # get output shape kept = args + [ak for ak in at.keys() if type(ak) == str] kept_axes = [self._axes[self.axis_names.index(a)] for a in kept] removed_axes = [a for a in self._axes if a not in kept_axes] removed_shape = wt_kit.joint_shape(*removed_axes) if removed_shape == (): removed_shape = (1,) * self.ndim removed_shape = list(removed_shape) for i in at.keys(): if type(i) == int: removed_shape[i] = 1 for ax in kept_axes: if ax.shape.count(1) == ax.ndim - 1: removed_shape[ax.shape.index(ax.size)] = 1 removed_shape = tuple(removed_shape) # iterate i = 0 for idx in np.ndindex(removed_shape): idx = np.array(idx, dtype=object) idx[np.array(removed_shape) == 1] = slice(None) for axis, point in at.items(): if type(axis) == int: idx[axis] = point continue point, units = point destination_units = self._axes[self.axis_names.index(axis)].units point = wt_units.converter(point, units, destination_units) axis_index = self.axis_names.index(axis) axis = self._axes[axis_index] idx_index = np.array(axis.shape) > 1 if np.sum(idx_index) > 1: raise wt_exceptions.MultidimensionalAxisError("chop", axis.natural_name) idx_index = list(idx_index).index(True) idx[idx_index] = np.argmin(np.abs(axis[tuple(idx)] - point)) data = out.create_data(name="chop%03i" % i) for v in self.variables: kwargs = {} kwargs["name"] = v.natural_name kwargs["values"] = v[idx] kwargs["units"] = v.units kwargs["label"] = v.label kwargs.update(v.attrs) data.create_variable(**kwargs) for c in self.channels: kwargs = {} kwargs["name"] = c.natural_name kwargs["values"] = c[idx] kwargs["units"] = c.units kwargs["label"] = c.label kwargs["signed"] = c.signed kwargs.update(c.attrs) data.create_channel(**kwargs) new_axes = [a.expression for a in kept_axes if a.expression not in at.keys()] new_axis_units = [a.units for a in kept_axes if a.expression not in at.keys()] data.transform(*new_axes) for const in self.constant_expressions: data.create_constant(const, verbose=False) for ax in self.axis_expressions: if ax not in new_axes: data.create_constant(ax, verbose=False) for j, units in enumerate(new_axis_units): data.axes[j].convert(units) i += 1 out.flush() # return if verbose: print("chopped data into %d piece(s)" % len(out), "in", new_axes) return out def gradient(self, axis, *, channel=0): """ Compute the gradient along one axis. New channels have names ``<channel name>_<axis name>_gradient``. Parameters ---------- axis : int or str The axis to differentiate along. If given as an integer, the axis in the underlying array is used, and unitary spacing is assumed. If given as a string, the axis must exist, and be a 1D array-aligned axis. (i.e. have a shape with a single value which is not ``1``) The axis to collapse along is inferred from the shape of the axis. channel : int or str The channel to differentiate. Default is the first channel. """ # get axis index -------------------------------------------------------------------------- if isinstance(axis, int): axis_index = axis elif isinstance(axis, str): index = self.axis_names.index(axis) axes = [i for i in range(self.ndim) if self.axes[index].shape[i] > 1] if len(axes) > 1: raise wt_exceptions.MultidimensionalAxisError(axis, "collapse") elif len(axes) == 0: raise wt_exceptions.ValueError( "Axis '{}' is a single point, cannot compute gradient".format(axis) ) axis_index = axes[0] else: raise wt_exceptions.TypeError("axis: expected {int, str}, got %s" % type(axis)) channel_index = wt_kit.get_index(self.channel_names, channel) channel = self.channel_names[channel_index] if self[channel].shape[axis_index] == 1: raise wt_exceptions.ValueError( "Channel '{}' has a single point along Axis '{}', cannot compute gradient".format( channel, axis ) ) rtype = np.result_type(self[channel].dtype, float) new = self.create_channel( "{}_{}_gradient".format(channel, axis), values=np.empty(self[channel].shape, dtype=rtype), ) channel = self[channel] if axis == axis_index: new[:] = np.gradient(channel[:], axis=axis_index) else: new[:] = np.gradient(channel[:], self[axis].points, axis=axis_index) def moment(self, axis, channel=0, moment=1, *, resultant=None): """Take the nth moment the dataset along one axis, adding lower rank channels. New channels have names ``<channel name>_<axis name>_moment_<moment num>``. Moment 0 is the integral of the slice. Moment 1 is the weighted average or "Center of Mass", normalized by the integral Moment 2 is the variance, the central moment about the center of mass, normalized by the integral Moments 3+ are central moments about the center of mass, normalized by the integral and by the standard deviation to the power of the moment. Moments, especially higher order moments, are susceptible to noise and baseline. It is recommended when used with real data to use :meth:`WrightTools.data.Channel.clip` in conjunction with moments to reduce effects of noise. Parameters ---------- axis : int or str The axis to take the moment along. If given as an integer, the axis with that index is used. If given as a string, the axis with that name is used. The axis must exist, and be a 1D array-aligned axis. (i.e. have a shape with a single value which is not ``1``) The collapsed axis must be monotonic to produce correct results. The axis to collapse along is inferred from the shape of the axis. channel : int or str The channel to take the moment. If given as an integer, the channel with that index is used. If given as a string, the channel with that name is used. The channel must have values along the axis (i.e. its shape must not be ``1`` in the dimension for which the axis is not ``1``) Default is 0, the first channel. moment : int or tuple of int The moments to take. One channel will be created for each number given. Default is 1, the center of mass. resultant : tuple of int The resultant shape after the moment operation. By default, it is intuited by the axis along which the moment is being taken. This default only works if that axis is 1D, so resultant is required if a multidimensional axis is passed as the first argument. The requirement of monotonicity applies on a per pixel basis. See Also -------- collapse Reduce dimensionality by some mathematical operation clip Set values above/below a threshold to a particular value WrightTools.kit.joint_shape Useful for setting `resultant` kwarg based off of axes not collapsed. """ # get axis index -------------------------------------------------------------------------- axis_index = None if resultant is not None: for i, (s, r) in enumerate(zip(wt_kit.joint_shape(*self.axes), resultant)): if s != r and r == 1 and axis_index is None: axis_index = i elif s == r: continue else: raise wt_exceptions.ValueError( f"Invalid resultant shape '{resultant}' for shape {wt_kit.joint_shape(*self.axes)}. " + "Consider using `wt.kit.joint_shape` to join non-collapsed axes." ) index = wt_kit.get_index(self.axis_names, axis) if axis_index is None: axes = [i for i in range(self.ndim) if self.axes[index].shape[i] > 1] if len(axes) > 1: raise wt_exceptions.MultidimensionalAxisError(axis, "moment") elif len(axes) == 0: raise wt_exceptions.ValueError( "Axis {} is a single point, cannot compute moment".format(axis) ) axis_index = axes[0] warnings.warn("moment", category=wt_exceptions.EntireDatasetInMemoryWarning) channel_index = wt_kit.get_index(self.channel_names, channel) channel = self.channel_names[channel_index] if self[channel].shape[axis_index] == 1: raise wt_exceptions.ValueError( "Channel '{}' has a single point along Axis '{}', cannot compute moment".format( channel, axis ) ) new_shape = list(self[channel].shape) new_shape[axis_index] = 1 channel = self[channel] axis_inp = axis axis = self.axes[index] x = axis[:] if np.any(np.isnan(x)): raise wt_exceptions.ValueError("Axis '{}' includes NaN".format(axis_inp)) y = np.nan_to_num(channel[:]) try: moments = tuple(moment) except TypeError: moments = (moment,) multiplier = 1 if 0 in moments: # May be possible to optimize, probably doesn't need the sum # only matters for integral, all others normalize by integral multiplier = np.sign( np.sum(np.diff(x, axis=axis_index), axis=axis_index, keepdims=True) ) for moment in moments: about = 0 norm = 1 if moment > 0: norm = np.trapz(y, x, axis=axis_index) norm = np.array(norm) norm.shape = new_shape if moment > 1: about = np.trapz(x * y, x, axis=axis_index) about = np.array(about) about.shape = new_shape about /= norm if moment > 2: sigma = np.trapz((x - about) ** 2 * y, x, axis=axis_index) sigma = np.array(sigma) sigma.shape = new_shape sigma /= norm sigma **= 0.5 norm *= sigma ** moment values = np.trapz((x - about) ** moment * y, x, axis=axis_index) values = np.array(values) values.shape = new_shape values /= norm if moment == 0: values *= multiplier self.create_channel( "{}_{}_{}_{}".format(channel.natural_name, axis_inp, "moment", moment), values=values, ) def collapse(self, axis, method="sum"): """Collapse the dataset along one axis, adding lower rank channels. New channels have names ``<channel name>_<axis name>_<method>``. Parameters ---------- axis : int or str The axis to collapse along. If given as an integer, the axis in the underlying array is used. If given as a string, the axis must exist, and be a 1D array-aligned axis. (i.e. have a shape with a single value which is not ``1``) The axis to collapse along is inferred from the shape of the axis. method : {'average', 'sum', 'max', 'min'} (optional) The method of collapsing the given axis. Method may also be list of methods corresponding to the channels of the object. Default is sum. NaNs are ignored. Can also be a list, allowing for different treatment for varied channels. In this case, None indicates that no change to that channel should occur. See Also -------- chop Divide the dataset into its lower-dimensionality components. split Split the dataset while maintaining its dimensionality. moment Take the moment along a particular axis """ if method in ("int", "integrate"): warnings.warn( "integrate method of collapse is deprecated, use moment(moment=0) instead", wt_exceptions.VisibleDeprecationWarning, ) for channel in self.channel_names: try: self.moment(axis, channel, moment=0) self.rename_channels( **{self.channel_names[-1]: f"{channel}_{axis}_{method}"}, verbose=False ) except wt_exceptions.ValueError: pass # may have some channels which fail, do so silently return # get axis index -------------------------------------------------------------------------- if isinstance(axis, int): axis_index = axis elif isinstance(axis, str): index = self.axis_names.index(axis) axes = [i for i in range(self.ndim) if self.axes[index].shape[i] > 1] if len(axes) > 1: raise wt_exceptions.MultidimensionalAxisError(axis, "collapse") elif len(axes) == 0: raise wt_exceptions.ValueError( "Axis {} is a single point, cannot collapse".format(axis) ) axis_index = axes[0] else: raise wt_exceptions.TypeError("axis: expected {int, str}, got %s" % type(axis)) new_shape = list(self.shape) new_shape[axis_index] = 1 func = { "sum": np.nansum, "max": np.nanmax, "maximum": np.nanmax, "min": np.nanmin, "minimum": np.nanmin, "ave": np.nanmean, "average": np.nanmean, "mean": np.nanmean, } # methods --------------------------------------------------------------------------------- if isinstance(method, str): methods = [method for _ in self.channels] if isinstance(method, list): if len(method) == len(self.channels): methods = method else: raise wt_exceptions.ValueError( "method argument must have same number of elements as there are channels" ) for m in methods: if m not in func.keys(): raise wt_exceptions.ValueError("method '{}' not recognized".format(m)) warnings.warn("collapse", category=wt_exceptions.EntireDatasetInMemoryWarning) # collapse -------------------------------------------------------------------------------- for method, channel in zip(methods, self.channel_names): if method is None: continue if self[channel].shape[axis_index] == 1: continue # Cannot collapse any further, don't clutter data object new_shape = list(self[channel].shape) new_shape[axis_index] = 1 rtype = self[channel].dtype if method in ["ave", "average", "mean"]: rtype = np.result_type(self[channel].dtype, float) new = self.create_channel( "{}_{}_{}".format(channel, axis, method), values=np.empty(new_shape, dtype=rtype), units=self[channel].units, ) new[:] = func[method](self[channel], axis=axis_index, keepdims=True) def convert(self, destination_units, *, convert_variables=False, verbose=True): """Convert all compatable axes and constants to given units. Parameters ---------- destination_units : str Destination units. convert_variables : boolean (optional) Toggle conversion of stored arrays. Default is False verbose : bool (optional) Toggle talkback. Default is True. See Also -------- Axis.convert Convert a single axis object to compatable units. Call on an axis object in data.axes. """ # apply to all compatible axes for axis in self.axes: if wt_units.is_valid_conversion(axis.units, destination_units): orig = axis.units axis.convert(destination_units, convert_variables=convert_variables) if verbose: print( "axis {} converted from {} to {}".format( axis.expression, orig, destination_units ) ) # apply to all compatible constants for constant in self.constants: if wt_units.is_valid_conversion(constant.units, destination_units): orig = constant.units constant.convert(destination_units, convert_variables=convert_variables) if verbose: print( "constant {} converted from {} to {}".format( constant.expression, orig, destination_units ) ) if convert_variables: for var in self.variables: if wt_units.is_valid_conversion(var.units, destination_units): orig = var.units var.convert(destination_units) if verbose: print( "variable {} converted from {} to {}".format( var.natural_name, orig, destination_units ) ) self._on_axes_updated() self._on_constants_updated() def create_channel( self, name, values=None, *, shape=None, units=None, dtype=None, **kwargs ) -> Channel: """Append a new channel. Parameters ---------- name : string Unique name for this channel. values : array (optional) Array. If None, an empty array equaling the data shape is created. Default is None. shape : tuple of int Shape to use. Must broadcast with the full shape. Only used if `values` is None. Default is the full shape of self. units : string (optional) Channel units. Default is None. dtype : numpy.dtype (optional) dtype to use for dataset, default is np.float64. Only used if `values` is None. kwargs : dict Additional keyword arguments passed to Channel instantiation. Returns ------- Channel Created channel. """ if name in self.channel_names: warnings.warn(name, wt_exceptions.ObjectExistsWarning) return self[name] elif name in self.variable_names: raise wt_exceptions.NameNotUniqueError(name) require_kwargs = {"chunks": True} if values is None: if shape is None: require_kwargs["shape"] = self.shape else: require_kwargs["shape"] = shape if dtype is None: require_kwargs["dtype"] = np.dtype(np.float64) else: require_kwargs["dtype"] = dtype if require_kwargs["dtype"].kind in "fcmM": require_kwargs["fillvalue"] = np.nan else: require_kwargs["fillvalue"] = 0 else: require_kwargs["data"] = values require_kwargs["shape"] = values.shape require_kwargs["dtype"] = values.dtype if np.prod(require_kwargs["shape"]) == 1: require_kwargs["chunks"] = None # create dataset dataset_id = self.require_dataset(name=name, **require_kwargs).id channel = Channel(self, dataset_id, units=units, **kwargs) # finish self.attrs["channel_names"] = np.append(self.attrs["channel_names"], name.encode()) return channel def create_variable( self, name, values=None, *, shape=None, units=None, dtype=None, **kwargs ) -> Variable: """Add new child variable. Parameters ---------- name : string Unique identifier. values : array-like (optional) Array to populate variable with. If None, an variable will be filled with NaN. Default is None. shape : tuple of int Shape to use. must broadcast with the full shape. Only used if `values` is None. Default is the full shape of self. units : string (optional) Variable units. Default is None. dtype : numpy.dtype (optional) dtype to use for dataset, default is np.float64. Only used if `values` is None. kwargs Additional kwargs to variable instantiation. Returns ------- WrightTools Variable New child variable. """ if name in self.variable_names: warnings.warn(name, wt_exceptions.ObjectExistsWarning) return self[name] elif name in self.channel_names: raise wt_exceptions.NameNotUniqueError(name) if values is None: if shape is None: shape = self.shape if dtype is None: dtype = np.dtype(np.float64) if dtype.kind in "fcmM": fillvalue = np.nan else: fillvalue = 0 else: shape = values.shape dtype = values.dtype fillvalue = None # create dataset id = self.require_dataset( name=name, data=values, shape=shape, dtype=dtype, fillvalue=fillvalue ).id variable = Variable(self, id, units=units, **kwargs) # finish self._variables = None self.attrs["variable_names"] = np.append(self.attrs["variable_names"], name.encode()) return variable def get_nadir(self, channel=0) -> tuple: """Get the coordinates, in units, of the minimum in a channel. Parameters ---------- channel : int or str (optional) Channel. Default is 0. Returns ------- generator of numbers Coordinates in units for each axis. """ # get channel if isinstance(channel, int): channel_index = channel elif isinstance(channel, str): channel_index = self.channel_names.index(channel) else: raise TypeError("channel: expected {int, str}, got %s" % type(channel)) channel = self.channels[channel_index] # get indicies idx = channel.argmin() # finish return tuple(a[idx] for a in self._axes) def get_zenith(self, channel=0) -> tuple: """Get the coordinates, in units, of the maximum in a channel. Parameters ---------- channel : int or str (optional) Channel. Default is 0. Returns ------- generator of numbers Coordinates in units for each axis. """ # get channel if isinstance(channel, int): channel_index = channel elif isinstance(channel, str): channel_index = self.channel_names.index(channel) else: raise TypeError("channel: expected {int, str}, got %s" % type(channel)) channel = self.channels[channel_index] # get indicies idx = channel.argmax() # finish return tuple(a[idx] for a in self._axes) def heal(self, channel=0, method="linear", fill_value=np.nan, verbose=True): """ Remove nans from channel using interpolation. Parameters ---------- channel : int or str (optional) Channel to heal. Default is 0. method : {'linear', 'nearest', 'cubic'} (optional) The interpolation method. Note that cubic interpolation is only possible for 1D and 2D data. See `griddata`__ for more information. Default is linear. fill_value : number-like (optional) The value written to pixels that cannot be filled by interpolation. Default is nan. verbose : bool (optional) Toggle talkback. Default is True. __ http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html .. note:: Healing may take several minutes for large datasets. Interpolation time goes as nearest, linear, then cubic. """ warnings.warn("heal", category=wt_exceptions.EntireDatasetInMemoryWarning) timer = wt_kit.Timer(verbose=False) with timer: # channel if isinstance(channel, int): channel_index = channel elif isinstance(channel, str): channel_index = self.channel_names.index(channel) else: raise TypeError("channel: expected {int, str}, got %s" % type(channel)) channel = self.channels[channel_index] values = self.channels[channel_index][:] points = [axis[:] for axis in self._axes] xi = tuple(np.meshgrid(*points, indexing="ij")) # 'undo' gridding arr = np.zeros((len(self._axes) + 1, values.size)) for i in range(len(self._axes)): arr[i] = xi[i].flatten() arr[-1] = values.flatten() # remove nans arr = arr[:, ~np.isnan(arr).any(axis=0)] # grid data wants tuples tup = tuple([arr[i] for i in range(len(arr) - 1)]) # grid data out = griddata(tup, arr[-1], xi, method=method, fill_value=fill_value) self.channels[channel_index][:] = out # print if verbose: print( "channel {0} healed in {1} seconds".format( channel.name, np.around(timer.interval, decimals=3) ) ) def level(self, channel, axis, npts, *, verbose=True): """Subtract the average value of npts at the edge of a given axis. Parameters ---------- channel : int or str Channel to level. axis : int Axis to level along. npts : int Number of points to average for each slice. Positive numbers take points at leading indicies and negative numbers take points at trailing indicies. verbose : bool (optional) Toggle talkback. Default is True. """ warnings.warn("level", category=wt_exceptions.EntireDatasetInMemoryWarning) channel_index = wt_kit.get_index(self.channel_names, channel) channel = self.channels[channel_index] # verify npts not zero npts = int(npts) if npts == 0: raise wt_exceptions.ValueError("npts must not be zero") # get subtrahend ss = [slice(None)] * self.ndim if npts > 0: ss[axis] = slice(0, npts, None) else: ss[axis] = slice(npts, None, None) subtrahend = np.nanmean(channel[ss], axis=axis) if self.ndim > 1: subtrahend = np.expand_dims(subtrahend, axis=axis) # level channel -= subtrahend # finish channel._null = 0 if verbose: print("channel {0} leveled along axis {1}".format(channel.natural_name, axis)) def map_variable( self, variable, points, input_units="same", *, name=None, parent=None, verbose=True ) -> "Data": """Map points of an axis to new points using linear interpolation. Out-of-bounds points are written nan. Parameters ---------- variable : string The variable to map onto. points : array-like or int If array, the new points. If int, new points will have the same limits, with int defining the number of evenly spaced points between. input_units : str (optional) The units of the new points. Default is same, which assumes the new points have the same units as the axis. name : string (optional) The name of the new data object. If None, generated from natural_name. Default is None. parent : WrightTools.Collection (optional) Parent of new data object. If None, data is made at root of a new temporary file. verbose : bool (optional) Toggle talkback. Default is True. Returns ------- WrightTools.Data New data object. """ # get variable index variable_index = wt_kit.get_index(self.variable_names, variable) variable = self.variables[variable_index] # get points if isinstance(points, int): points = np.linspace(variable.min(), variable.max(), points) points = np.array(points) # points dimensionality if points.ndim < variable.ndim: for i, d in enumerate(variable.shape): if d == 1: points = np.expand_dims(points, axis=i) # convert points if input_units == "same": pass else: points = wt_units.converter(points, input_units, variable.units) # construct new data object special = ["name", "axes", "constants", "channel_names", "variable_names"] kwargs = {k: v for k, v in self.attrs.items() if k not in special} if name is None: name = "{0}_{1}_mapped".format(self.natural_name, variable.natural_name) kwargs["name"] = name kwargs["parent"] = parent out = Data(**kwargs) # mapped variable values = points out.create_variable(values=values, **variable.attrs) # orthogonal variables for v in self.variables: if wt_kit.orthogonal(v.shape, variable.shape): out.create_variable(values=v[:], **v.attrs) out.transform(*self.axis_expressions) # interpolate if self.ndim == 1: def interpolate(dataset, points): function = scipy.interpolate.interp1d(variable[:], dataset[:], bounds_error=False) return function(points) else: pts = np.array([a.full.flatten() for a in self.axes]).T out_pts = np.array([a.full.flatten() for a in out.axes]).T def interpolate(dataset, points): values = dataset.full.flatten() function = scipy.interpolate.LinearNDInterpolator(pts, values, rescale=True) new = function(out_pts) new.shape = out.shape return new for v in self.variables: if v.natural_name not in out.variable_names: out.create_variable(values=interpolate(v, points), **v.attrs) out.variable_names = self.variable_names # enforce old order out._variables = None # force regeneration of variables @property for channel in self.channels: out.create_channel(values=interpolate(channel, points), **channel.attrs) # finish if verbose: print("data mapped from {0} to {1}".format(self.shape, out.shape)) return out def offset( self, points, offsets, along, offset_axis, units="same", offset_units="same", mode="valid", method="linear", verbose=True, ): """Offset one axis based on another axis' values. Useful for correcting instrumental artifacts such as zerotune. Parameters ---------- points : 1D array-like Points. offsets : 1D array-like Offsets. along : str or int Axis that points array lies along. offset_axis : str or int Axis to offset using offsets. units : str (optional) Units of points array. offset_units : str (optional) Units of offsets aray. mode : {'valid', 'full', 'old'} (optional) Define how far the new axis will extend. Points outside of valid interpolation range will be written nan. method : {'linear', 'nearest', 'cubic'} (optional) The interpolation method. Note that cubic interpolation is only possible for 1D and 2D data. See `griddata`__ for more information. Default is linear. verbose : bool (optional) Toggle talkback. Default is True. __ http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html >>> points # an array of w1 points >>> offsets # an array of d1 corrections >>> data.offset(points, offsets, 'w1', 'd1') """ raise NotImplementedError # axis ------------------------------------------------------------------------------------ if isinstance(along, int): axis_index = along elif isinstance(along, str): axis_index = self.axis_names.index(along) else: raise TypeError("along: expected {int, str}, got %s" % type(along)) axis = self._axes[axis_index] # values & points ------------------------------------------------------------------------- # get values, points, units if units == "same": input_units = axis.units else: input_units = units # check offsets is 1D or 0D if len(offsets.shape) == 1: pass else: raise RuntimeError("values must be 1D or 0D in offset!") # check if units is compatible, convert dictionary = getattr(wt_units, axis.units_kind) if input_units in dictionary.keys(): pass else: raise RuntimeError("units incompatible in offset!") points = wt_units.converter(points, input_units, axis.units) # create correction array function = interp1d(points, offsets, bounds_error=False) corrections = function(axis[:]) # remove nans finite_indicies = np.where(np.isfinite(corrections))[0] left_pad_width = finite_indicies[0] right_pad_width = len(corrections) - finite_indicies[-1] - 1 corrections = np.pad( corrections[np.isfinite(corrections)], (int(left_pad_width), int(right_pad_width)), mode="edge", ) # do correction --------------------------------------------------------------------------- # transpose so axis is last transpose_order = np.arange(len(self._axes)) transpose_order[axis_index] = len(self._axes) - 1 transpose_order[-1] = axis_index self.transpose(transpose_order, verbose=False) # get offset axis index if isinstance(offset_axis, int): offset_axis_index = offset_axis elif isinstance(offset_axis, str): offset_axis_index = self.axis_names.index(offset_axis) else: raise TypeError("offset_axis: expected {int, str}, got %s" % type(offset_axis)) # new points new_points = [a[:] for a in self._axes] old_offset_axis_points = self._axes[offset_axis_index][:] spacing = abs( (old_offset_axis_points.max() - old_offset_axis_points.min()) / float(len(old_offset_axis_points)) ) if mode == "old": new_offset_axis_points = old_offset_axis_points elif mode == "valid": _max = old_offset_axis_points.max() + corrections.min() _min = old_offset_axis_points.min() + corrections.max() n = int(abs(np.ceil((_max - _min) / spacing))) new_offset_axis_points = np.linspace(_min, _max, n) elif mode == "full": _max = old_offset_axis_points.max() + corrections.max() _min = old_offset_axis_points.min() + corrections.min() n = np.ceil((_max - _min) / spacing) new_offset_axis_points = np.linspace(_min, _max, n) new_points[offset_axis_index] = new_offset_axis_points new_xi = tuple(np.meshgrid(*new_points, indexing="ij")) xi = tuple(np.meshgrid(*[a[:] for a in self._axes], indexing="ij")) for channel in self.channels: # 'undo' gridding arr = np.zeros((len(self._axes) + 1, channel[:].size)) for i in range(len(self._axes)): arr[i] = xi[i].flatten() arr[-1] = channel[:].flatten() # do corrections corrections = list(corrections) corrections = corrections * int((len(arr[0]) / len(corrections))) arr[offset_axis_index] += corrections # grid data tup = tuple([arr[i] for i in range(len(arr) - 1)]) # note that rescale is crucial in this operation out = griddata(tup, arr[-1], new_xi, method=method, fill_value=np.nan, rescale=True) channel[:] = out self._axes[offset_axis_index][:] = new_offset_axis_points # transpose out self.transpose(transpose_order, verbose=False) def print_tree(self, *, verbose=True): """Print a ascii-formatted tree representation of the data contents.""" print("{0} ({1})".format(self.natural_name, self.filepath)) self._print_branch("", depth=0, verbose=verbose) def prune(self, keep_channels=True, *, verbose=True): """Remove unused variables and (optionally) channels from the Data object. Unused variables are those that are not included in either axes or constants. Unused channels are those not specified in keep_channels, or the first channel. Parameters ---------- keep_channels : boolean or int or str or tuple If False, removes all but the first channel. If int or str, removes all but that index/name channel. If tuple, removes all channels except those in the tuple by index or name. Default is True: do not delete channels verbose : boolean Toggle talkback. Default is True. """ for v in self.variables: for var in wt_kit.flatten_list([ax.variables for ax in self._axes + self._constants]): if v == var: break else: self.remove_variable(v.natural_name, implied=False, verbose=verbose) if keep_channels is not True: try: if isinstance(keep_channels, str): raise TypeError indexes = tuple(keep_channels) except TypeError: indexes = (keep_channels,) for i, ch in enumerate(self.channels): if i not in indexes and not ch.natural_name in indexes: self.remove_channel(ch.natural_name, verbose=verbose) def remove_channel(self, channel, *, verbose=True): """Remove channel from data. Parameters ---------- channel : int or str Channel index or name to remove. verbose : boolean (optional) Toggle talkback. Default is True. """ channel_index = wt_kit.get_index(self.channel_names, channel) new = list(self.channel_names) name = new.pop(channel_index) del self[name] self.channel_names = new if verbose: print("channel {0} removed".format(name)) def remove_variable(self, variable, *, implied=True, verbose=True): """Remove variable from data. Parameters ---------- variable : int or str Variable index or name to remove. implied : boolean (optional) Toggle deletion of other variables that start with the same name. Default is True. verbose : boolean (optional) Toggle talkback. Default is True. """ if isinstance(variable, int): variable = self.variable_names[variable] # find all of the implied variables removed = [] if implied: for n in self.variable_names: if n.startswith(variable): removed.append(n) else: removed = [variable] # check that axes will not be ruined for n in removed: for a in self._axes: if n in [v.natural_name for v in a.variables]: message = "{0} is contained in axis {1}".format(n, a.expression) raise RuntimeError(message) for c in self._constants: if n in [v.natural_name for v in c.variables]: warnings.warn( "Variable being removed used in a constant", wt_exceptions.WrightToolsWarning, ) # do removal for n in removed: variable_index = wt_kit.get_index(self.variable_names, n) new = list(self.variable_names) name = new.pop(variable_index) del self[name] self.variable_names = new self._variables = None # finish if verbose: print("{0} variable(s) removed:".format(len(removed))) for n in removed: print(" {0}".format(n)) def rename_channels(self, *, verbose=True, **kwargs): """Rename a set of channels. Parameters ---------- kwargs Keyword arguments of the form current:'new'. verbose : boolean (optional) Toggle talkback. Default is True """ # ensure that items will remain unique changed = kwargs.keys() for k, v in kwargs.items(): if v not in changed and v in self.keys(): raise wt_exceptions.NameNotUniqueError(v) # compile references to items that are changing new = {} for k, v in kwargs.items(): obj = self[k] index = self.channel_names.index(k) # rename new[v] = obj, index Group._instances.pop(obj.fullpath, None) obj.natural_name = str(v) # remove old references del self[k] # apply new references names = list(self.channel_names) for v, value in new.items(): obj, index = value self[v] = obj names[index] = v self.channel_names = names # finish if verbose: print("{0} channel(s) renamed:".format(len(kwargs))) for k, v in kwargs.items(): print(" {0} --> {1}".format(k, v)) def rename_variables(self, *, implied=True, verbose=True, **kwargs): """Rename a set of variables. Parameters ---------- kwargs Keyword arguments of the form current:'new'. implied : boolean (optional) Toggle inclusion of other variables that start with the same name. Default is True. verbose : boolean (optional) Toggle talkback. Default is True """ # find all of the implied variables kwargs = collections.OrderedDict(kwargs) if implied: new = collections.OrderedDict() for k, v in kwargs.items(): for n in self.variable_names: if n.startswith(k): new[n] = n.replace(k, v, 1) kwargs = new # ensure that items will remain unique changed = kwargs.keys() for k, v in kwargs.items(): if v not in changed and v in self.keys(): raise wt_exceptions.NameNotUniqueError(v) # compile references to items that are changing new = {} for k, v in kwargs.items(): obj = self[k] index = self.variable_names.index(k) # rename new[v] = obj, index Group._instances.pop(obj.fullpath, None) obj.natural_name = str(v) # remove old references del self[k] # apply new references names = list(self.variable_names) for v, value in new.items(): obj, index = value self[v] = obj names[index] = v self.variable_names = names units = self.units new = list(self.axis_expressions) for i, v in enumerate(kwargs.keys()): for j, n in enumerate(new): new[j] = n.replace(v, "{%i}" % i) for i, n in enumerate(new): new[i] = n.format(*kwargs.values()) self.transform(*new) for a, u in zip(self._axes, units): a.convert(u) units = self.constant_units new = list(self.constant_expressions) for i, v in enumerate(kwargs.keys()): for j, n in enumerate(new): new[j] = n.replace(v, "{%i}" % i) for i, n in enumerate(new): new[i] = n.format(*kwargs.values()) self.set_constants(*new) for c, u in zip(self._constants, units): c.convert(u) # finish if verbose: print("{0} variable(s) renamed:".format(len(kwargs))) for k, v in kwargs.items(): print(" {0} --> {1}".format(k, v)) def share_nans(self): """Share not-a-numbers between all channels. If any channel is nan at a given index, all channels will be nan at that index after this operation. Uses the share_nans method found in wt.kit. """ def f(_, s, channels): outs = wt_kit.share_nans(*[c[s] for c in channels]) for c, o in zip(channels, outs): c[s] = o self.channels[0].chunkwise(f, self.channels) def smooth(self, factors, channel=None, verbose=True) -> "Data": """Smooth a channel using an n-dimenional kaiser window. Note, all arrays are loaded into memory. For more info see `Kaiser_window`__ wikipedia entry. __ https://en.wikipedia.org/wiki/Kaiser_window Parameters ---------- factors : int or list of int The smoothing factor. You may provide a list of smoothing factors for each axis. channel : int or str or None (optional) The channel to smooth. If None, all channels will be smoothed. Default is None. verbose : bool (optional) Toggle talkback. Default is True. """ warnings.warn("smooth", category=wt_exceptions.EntireDatasetInMemoryWarning) # get factors ----------------------------------------------------------------------------- if isinstance(factors, list): pass else: dummy = np.zeros(len(self._axes)) dummy[::] = factors factors = list(dummy) # get channels ---------------------------------------------------------------------------- if channel is None: channels = self.channels else: if isinstance(channel, int): channel_index = channel elif isinstance(channel, str): channel_index = self.channel_names.index(channel) else: raise TypeError("channel: expected {int, str}, got %s" % type(channel)) channels = [self.channels[channel_index]] # smooth ---------------------------------------------------------------------------------- for channel in channels: values = channel[:] for axis_index in range(len(factors)): factor = factors[axis_index] # transpose so the axis of interest is last transpose_order = range(len(values.shape)) # replace axis_index with zero transpose_order = [ len(values.shape) - 1 if i == axis_index else i for i in transpose_order ] transpose_order[len(values.shape) - 1] = axis_index values = values.transpose(transpose_order) # get kaiser window beta = 5.0 w = np.kaiser(2 * factor + 1, beta) # for all slices... for index in np.ndindex(values[..., 0].shape): current_slice = values[index] temp_slice = np.pad(current_slice, int(factor), mode=str("edge")) values[index] = np.convolve(temp_slice, w / w.sum(), mode=str("valid")) # transpose out values = values.transpose(transpose_order) # return array to channel object channel[:] = values if verbose: print("smoothed data") def split( self, expression, positions, *, units=None, parent=None, verbose=True ) -> wt_collection.Collection: """ Split the data object along a given expression, in units. Parameters ---------- expression : int or str The expression to split along. If given as an integer, the axis at that index is used. positions : number-type or 1D array-type The position(s) to split at, in units. units : str (optional) The units of the given positions. Default is same, which assumes input units are identical to first variable units. parent : WrightTools.Collection (optional) The parent collection in which to place the 'split' collection. Default is a new Collection. verbose : bool (optional) Toggle talkback. Default is True. Returns ------- WrightTools.collection.Collection A Collection of data objects. The order of the objects is such that the axis points retain their original order. See Also -------- chop Divide the dataset into its lower-dimensionality components. collapse Collapse the dataset along one axis. """ # axis ------------------------------------------------------------------------------------ old_expr = self.axis_expressions old_units = self.units out = wt_collection.Collection(name="split", parent=parent) if isinstance(expression, int): if units is None: units = self._axes[expression].units expression = self._axes[expression].expression elif isinstance(expression, str): pass else: raise TypeError("expression: expected {int, str}, got %s" % type(expression)) self.transform(expression) if units: self.convert(units, verbose=False) try: positions = [-np.inf] + sorted(list(positions)) + [np.inf] except TypeError: positions = [-np.inf, positions, np.inf] values = self._axes[0].full masks = [(values >= lo) & (values < hi) for lo, hi in wt_kit.pairwise(positions)] omasks = [] cuts = [] for mask in masks: try: omasks.append(wt_kit.mask_reduce(mask)) cuts.append([i == 1 for i in omasks[-1].shape]) # Ensure at least one axis is kept if np.all(cuts[-1]): cuts[-1][0] = False except ValueError: omasks.append(None) cuts.append(None) for i in range(len(positions) - 1): out.create_data("split%03i" % i) for var in self.variables: for i, (imask, omask, cut) in enumerate(zip(masks, omasks, cuts)): if omask is None: # Zero length split continue omask = wt_kit.enforce_mask_shape(omask, var.shape) omask.shape = tuple([s for s, c in zip(omask.shape, cut) if not c]) out_arr = np.full(omask.shape, np.nan) imask = wt_kit.enforce_mask_shape(imask, var.shape) out_arr[omask] = var[:][imask] out[i].create_variable(values=out_arr, **var.attrs) for ch in self.channels: for i, (imask, omask, cut) in enumerate(zip(masks, omasks, cuts)): if omask is None: # Zero length split continue omask = wt_kit.enforce_mask_shape(omask, ch.shape) omask.shape = tuple([s for s, c in zip(omask.shape, cut) if not c]) out_arr = np.full(omask.shape, np.nan) imask = wt_kit.enforce_mask_shape(imask, ch.shape) out_arr[omask] = ch[:][imask] out[i].create_channel(values=out_arr, **ch.attrs) if verbose: for d in out.values(): try: d.transform(expression) except IndexError: continue print("split data into {0} pieces along <{1}>:".format(len(positions) - 1, expression)) for i, (lo, hi) in enumerate(wt_kit.pairwise(positions)): new_data = out[i] if new_data.shape == (): print(" {0} : None".format(i)) else: new_axis = new_data.axes[0] print( " {0} : {1:0.2f} to {2:0.2f} {3} {4}".format( i, lo, hi, self.axes[0].units, new_axis.shape ) ) for d in out.values(): try: d.transform(*old_expr) keep = [] keep_units = [] for ax, u in zip(d.axes, old_units): if ax.size > 1: keep.append(ax.expression) keep_units.append(u) else: d.create_constant(ax.expression, verbose=False) d.transform(*keep) for ax, u in zip(d.axes, keep_units): ax.convert(u) except IndexError: continue tempax = Axis(d, expression) if all( np.all( np.sum(~np.isnan(tempax.masked), axis=tuple(set(range(tempax.ndim)) - {j})) <= 1 ) for j in range(tempax.ndim) ): d.create_constant(expression, verbose=False) self.transform(*old_expr) for ax, u in zip(self.axes, old_units): ax.convert(u) return out def transform(self, *axes, verbose=True): """Transform the data. Parameters ---------- axes : strings Expressions for the new set of axes. verbose : boolean (optional) Toggle talkback. Default is True See Also -------- set_constants Similar method except for constants """ # TODO: ensure that transform does not break data # create new = [] newt = "newt" in self.axis_expressions current = {a.expression: a for a in self._axes} for expression in axes: axis = current.get(expression, Axis(self, expression)) new.append(axis) self._axes = new # units for a in self._axes: if a.units is None: a.convert(a.variables[0].units) # finish self.flush() self._on_axes_updated() nownewt = "newt" in self.axis_expressions if verbose and nownewt and not newt: print("Look she turned me into a newt") elif verbose and newt and not nownewt: print("I got better") def set_constants(self, *constants, verbose=True): """Set the constants associated with the data. Parameters ---------- constants : str Expressions for the new set of constants. verbose : boolean (optional) Toggle talkback. Default is True See Also -------- transform Similar method except for axes. create_constant Add an individual constant. remove_constant Remove an individual constant. """ # create new = [] current = {c.expression: c for c in self._constants} for expression in constants: constant = current.get(expression, Constant(self, expression)) new.append(constant) self._constants = new # units for c in self._constants: if c.units is None: c.convert(c.variables[0].units) # finish self.flush() self._on_constants_updated() def create_constant(self, expression, *, verbose=True): """Append a constant to the stored list. Parameters ---------- expression : str Expression for the new constant. verbose : boolean (optional) Toggle talkback. Default is True See Also -------- set_constants Remove and replace all constants. remove_constant Remove an individual constant. """ if expression in self.constant_expressions: wt_exceptions.ObjectExistsWarning.warn(expression) return self.constants[self.constant_expressions.index(expression)] constant = Constant(self, expression) if constant.units is None: constant.convert(constant.variables[0].units) self._constants.append(constant) self.flush() self._on_constants_updated() if verbose: print("Constant '{}' added".format(constant.expression)) return constant def remove_constant(self, constant, *, verbose=True): """Remove a constant from the stored list. Parameters ---------- constant : str or Constant or int Expression for the new constant. verbose : boolean (optional) Toggle talkback. Default is True See Also -------- set_constants Remove and replace all constants. create_constant Add an individual constant. """ if isinstance(constant, (str, int)): constant_index = wt_kit.get_index(self.constant_expressions, constant) elif isinstance(constant, Constant): constant_index = wt_kit.get_index(self.constants, constant) constant = self._constants[constant_index] self._constants.pop(constant_index) self.flush() self._on_constants_updated() if verbose: print("Constant '{}' removed".format(constant.expression)) def zoom(self, factor, order=1, verbose=True): """Zoom the data array using spline interpolation of the requested order. The number of points along each axis is increased by factor. See `scipy ndimage`__ for more info. __ http://docs.scipy.org/doc/scipy/reference/ generated/scipy.ndimage.interpolation.zoom.html Parameters ---------- factor : float The number of points along each axis will increase by this factor. order : int (optional) The order of the spline used to interpolate onto new points. verbose : bool (optional) Toggle talkback. Default is True. """ raise NotImplementedError import scipy.ndimage # axes for axis in self._axes: axis[:] = scipy.ndimage.interpolation.zoom(axis[:], factor, order=order) # channels for channel in self.channels: channel[:] = scipy.ndimage.interpolation.zoom(channel[:], factor, order=order) # return if verbose: print("data zoomed to new shape:", self.shape)
[ "numpy.kaiser", "numpy.sum", "numpy.nan_to_num", "numpy.empty", "numpy.isnan", "numpy.around", "scipy.interpolate.interp1d", "numpy.prod", "numpy.nanmean", "numpy.full", "numpy.meshgrid", "numpy.isfinite", "scipy.ndimage.interpolation.zoom", "numpy.linspace", "numpy.trapz", "numpy.result_type", "numpy.ceil", "scipy.interpolate.griddata", "scipy.interpolate.LinearNDInterpolator", "collections.OrderedDict", "numpy.all", "numpy.ndindex", "numpy.dtype", "numpy.expand_dims", "numpy.diff", "numpy.array", "functools.reduce", "warnings.warn", "numpy.gradient" ]
[((4139, 4165), 'numpy.array', 'np.array', (['value'], {'dtype': '"""S"""'}), "(value, dtype='S')\n", (4147, 4165), True, 'import numpy as np\n'), ((5409, 5451), 'functools.reduce', 'functools.reduce', (['operator.mul', 'self.shape'], {}), '(operator.mul, self.shape)\n', (5425, 5451), False, 'import functools\n'), ((6372, 6398), 'numpy.array', 'np.array', (['value'], {'dtype': '"""S"""'}), "(value, dtype='S')\n", (6380, 6398), True, 'import numpy as np\n'), ((13292, 13317), 'numpy.ndindex', 'np.ndindex', (['removed_shape'], {}), '(removed_shape)\n', (13302, 13317), True, 'import numpy as np\n'), ((17620, 17662), 'numpy.result_type', 'np.result_type', (['self[channel].dtype', 'float'], {}), '(self[channel].dtype, float)\n', (17634, 17662), True, 'import numpy as np\n'), ((22019, 22095), 'warnings.warn', 'warnings.warn', (['"""moment"""'], {'category': 'wt_exceptions.EntireDatasetInMemoryWarning'}), "('moment', category=wt_exceptions.EntireDatasetInMemoryWarning)\n", (22032, 22095), False, 'import warnings\n'), ((22796, 22821), 'numpy.nan_to_num', 'np.nan_to_num', (['channel[:]'], {}), '(channel[:])\n', (22809, 22821), True, 'import numpy as np\n'), ((28138, 28216), 'warnings.warn', 'warnings.warn', (['"""collapse"""'], {'category': 'wt_exceptions.EntireDatasetInMemoryWarning'}), "('collapse', category=wt_exceptions.EntireDatasetInMemoryWarning)\n", (28151, 28216), False, 'import warnings\n'), ((38329, 38403), 'warnings.warn', 'warnings.warn', (['"""heal"""'], {'category': 'wt_exceptions.EntireDatasetInMemoryWarning'}), "('heal', category=wt_exceptions.EntireDatasetInMemoryWarning)\n", (38342, 38403), False, 'import warnings\n'), ((40365, 40440), 'warnings.warn', 'warnings.warn', (['"""level"""'], {'category': 'wt_exceptions.EntireDatasetInMemoryWarning'}), "('level', category=wt_exceptions.EntireDatasetInMemoryWarning)\n", (40378, 40440), False, 'import warnings\n'), ((40915, 40949), 'numpy.nanmean', 'np.nanmean', (['channel[ss]'], {'axis': 'axis'}), '(channel[ss], axis=axis)\n', (40925, 40949), True, 'import numpy as np\n'), ((42750, 42766), 'numpy.array', 'np.array', (['points'], {}), '(points)\n', (42758, 42766), True, 'import numpy as np\n'), ((47908, 47953), 'scipy.interpolate.interp1d', 'interp1d', (['points', 'offsets'], {'bounds_error': '(False)'}), '(points, offsets, bounds_error=False)\n', (47916, 47953), False, 'from scipy.interpolate import griddata, interp1d\n'), ((57148, 57179), 'collections.OrderedDict', 'collections.OrderedDict', (['kwargs'], {}), '(kwargs)\n', (57171, 57179), False, 'import collections\n'), ((60504, 60580), 'warnings.warn', 'warnings.warn', (['"""smooth"""'], {'category': 'wt_exceptions.EntireDatasetInMemoryWarning'}), "('smooth', category=wt_exceptions.EntireDatasetInMemoryWarning)\n", (60517, 60580), False, 'import warnings\n'), ((3911, 3934), 'numpy.array', 'np.array', (['[]'], {'dtype': '"""S"""'}), "([], dtype='S')\n", (3919, 3934), True, 'import numpy as np\n'), ((6139, 6162), 'numpy.array', 'np.array', (['[]'], {'dtype': '"""S"""'}), "([], dtype='S')\n", (6147, 6162), True, 'import numpy as np\n'), ((13337, 13364), 'numpy.array', 'np.array', (['idx'], {'dtype': 'object'}), '(idx, dtype=object)\n', (13345, 13364), True, 'import numpy as np\n'), ((17908, 17948), 'numpy.gradient', 'np.gradient', (['channel[:]'], {'axis': 'axis_index'}), '(channel[:], axis=axis_index)\n', (17919, 17948), True, 'import numpy as np\n'), ((17984, 18043), 'numpy.gradient', 'np.gradient', (['channel[:]', 'self[axis].points'], {'axis': 'axis_index'}), '(channel[:], self[axis].points, axis=axis_index)\n', (17995, 18043), True, 'import numpy as np\n'), ((22684, 22695), 'numpy.isnan', 'np.isnan', (['x'], {}), '(x)\n', (22692, 22695), True, 'import numpy as np\n'), ((23993, 24048), 'numpy.trapz', 'np.trapz', (['((x - about) ** moment * y)', 'x'], {'axis': 'axis_index'}), '((x - about) ** moment * y, x, axis=axis_index)\n', (24001, 24048), True, 'import numpy as np\n'), ((24070, 24086), 'numpy.array', 'np.array', (['values'], {}), '(values)\n', (24078, 24086), True, 'import numpy as np\n'), ((25758, 25897), 'warnings.warn', 'warnings.warn', (['"""integrate method of collapse is deprecated, use moment(moment=0) instead"""', 'wt_exceptions.VisibleDeprecationWarning'], {}), "(\n 'integrate method of collapse is deprecated, use moment(moment=0) instead',\n wt_exceptions.VisibleDeprecationWarning)\n", (25771, 25897), False, 'import warnings\n'), ((32399, 32453), 'warnings.warn', 'warnings.warn', (['name', 'wt_exceptions.ObjectExistsWarning'], {}), '(name, wt_exceptions.ObjectExistsWarning)\n', (32412, 32453), False, 'import warnings\n'), ((33306, 33338), 'numpy.prod', 'np.prod', (["require_kwargs['shape']"], {}), "(require_kwargs['shape'])\n", (33313, 33338), True, 'import numpy as np\n'), ((34732, 34786), 'warnings.warn', 'warnings.warn', (['name', 'wt_exceptions.ObjectExistsWarning'], {}), '(name, wt_exceptions.ObjectExistsWarning)\n', (34745, 34786), False, 'import warnings\n'), ((39443, 39507), 'scipy.interpolate.griddata', 'griddata', (['tup', 'arr[-1]', 'xi'], {'method': 'method', 'fill_value': 'fill_value'}), '(tup, arr[-1], xi, method=method, fill_value=fill_value)\n', (39451, 39507), False, 'from scipy.interpolate import griddata, interp1d\n'), ((41001, 41038), 'numpy.expand_dims', 'np.expand_dims', (['subtrahend'], {'axis': 'axis'}), '(subtrahend, axis=axis)\n', (41015, 41038), True, 'import numpy as np\n'), ((50072, 50111), 'numpy.meshgrid', 'np.meshgrid', (['*new_points'], {'indexing': '"""ij"""'}), "(*new_points, indexing='ij')\n", (50083, 50111), True, 'import numpy as np\n'), ((50132, 50187), 'numpy.meshgrid', 'np.meshgrid', (['*[a[:] for a in self._axes]'], {'indexing': '"""ij"""'}), "(*[a[:] for a in self._axes], indexing='ij')\n", (50143, 50187), True, 'import numpy as np\n'), ((50820, 50898), 'scipy.interpolate.griddata', 'griddata', (['tup', 'arr[-1]', 'new_xi'], {'method': 'method', 'fill_value': 'np.nan', 'rescale': '(True)'}), '(tup, arr[-1], new_xi, method=method, fill_value=np.nan, rescale=True)\n', (50828, 50898), False, 'from scipy.interpolate import griddata, interp1d\n'), ((57218, 57243), 'collections.OrderedDict', 'collections.OrderedDict', ([], {}), '()\n', (57241, 57243), False, 'import collections\n'), ((73676, 73738), 'scipy.ndimage.interpolation.zoom', 'scipy.ndimage.interpolation.zoom', (['axis[:]', 'factor'], {'order': 'order'}), '(axis[:], factor, order=order)\n', (73708, 73738), False, 'import scipy\n'), ((73821, 73886), 'scipy.ndimage.interpolation.zoom', 'scipy.ndimage.interpolation.zoom', (['channel[:]', 'factor'], {'order': 'order'}), '(channel[:], factor, order=order)\n', (73853, 73886), False, 'import scipy\n'), ((17769, 17811), 'numpy.empty', 'np.empty', (['self[channel].shape'], {'dtype': 'rtype'}), '(self[channel].shape, dtype=rtype)\n', (17777, 17811), True, 'import numpy as np\n'), ((23383, 23414), 'numpy.trapz', 'np.trapz', (['y', 'x'], {'axis': 'axis_index'}), '(y, x, axis=axis_index)\n', (23391, 23414), True, 'import numpy as np\n'), ((23438, 23452), 'numpy.array', 'np.array', (['norm'], {}), '(norm)\n', (23446, 23452), True, 'import numpy as np\n'), ((23543, 23578), 'numpy.trapz', 'np.trapz', (['(x * y)', 'x'], {'axis': 'axis_index'}), '(x * y, x, axis=axis_index)\n', (23551, 23578), True, 'import numpy as np\n'), ((23603, 23618), 'numpy.array', 'np.array', (['about'], {}), '(about)\n', (23611, 23618), True, 'import numpy as np\n'), ((23740, 23790), 'numpy.trapz', 'np.trapz', (['((x - about) ** 2 * y)', 'x'], {'axis': 'axis_index'}), '((x - about) ** 2 * y, x, axis=axis_index)\n', (23748, 23790), True, 'import numpy as np\n'), ((23815, 23830), 'numpy.array', 'np.array', (['sigma'], {}), '(sigma)\n', (23823, 23830), True, 'import numpy as np\n'), ((28782, 28824), 'numpy.result_type', 'np.result_type', (['self[channel].dtype', 'float'], {}), '(self[channel].dtype, float)\n', (28796, 28824), True, 'import numpy as np\n'), ((32874, 32894), 'numpy.dtype', 'np.dtype', (['np.float64'], {}), '(np.float64)\n', (32882, 32894), True, 'import numpy as np\n'), ((35061, 35081), 'numpy.dtype', 'np.dtype', (['np.float64'], {}), '(np.float64)\n', (35069, 35081), True, 'import numpy as np\n'), ((38967, 39002), 'numpy.meshgrid', 'np.meshgrid', (['*points'], {'indexing': '"""ij"""'}), "(*points, indexing='ij')\n", (38978, 39002), True, 'import numpy as np\n'), ((44004, 44075), 'scipy.interpolate.interp1d', 'scipy.interpolate.interp1d', (['variable[:]', 'dataset[:]'], {'bounds_error': '(False)'}), '(variable[:], dataset[:], bounds_error=False)\n', (44030, 44075), False, 'import scipy\n'), ((44392, 44457), 'scipy.interpolate.LinearNDInterpolator', 'scipy.interpolate.LinearNDInterpolator', (['pts', 'values'], {'rescale': '(True)'}), '(pts, values, rescale=True)\n', (44430, 44457), False, 'import scipy\n'), ((48051, 48075), 'numpy.isfinite', 'np.isfinite', (['corrections'], {}), '(corrections)\n', (48062, 48075), True, 'import numpy as np\n'), ((48247, 48271), 'numpy.isfinite', 'np.isfinite', (['corrections'], {}), '(corrections)\n', (48258, 48271), True, 'import numpy as np\n'), ((49681, 49707), 'numpy.linspace', 'np.linspace', (['_min', '_max', 'n'], {}), '(_min, _max, n)\n', (49692, 49707), True, 'import numpy as np\n'), ((62176, 62207), 'numpy.kaiser', 'np.kaiser', (['(2 * factor + 1)', 'beta'], {}), '(2 * factor + 1, beta)\n', (62185, 62207), True, 'import numpy as np\n'), ((62273, 62305), 'numpy.ndindex', 'np.ndindex', (['values[..., 0].shape'], {}), '(values[..., 0].shape)\n', (62283, 62305), True, 'import numpy as np\n'), ((65326, 65342), 'numpy.all', 'np.all', (['cuts[-1]'], {}), '(cuts[-1])\n', (65332, 65342), True, 'import numpy as np\n'), ((65970, 65998), 'numpy.full', 'np.full', (['omask.shape', 'np.nan'], {}), '(omask.shape, np.nan)\n', (65977, 65998), True, 'import numpy as np\n'), ((66575, 66603), 'numpy.full', 'np.full', (['omask.shape', 'np.nan'], {}), '(omask.shape, np.nan)\n', (66582, 66603), True, 'import numpy as np\n'), ((13381, 13404), 'numpy.array', 'np.array', (['removed_shape'], {}), '(removed_shape)\n', (13389, 13404), True, 'import numpy as np\n'), ((13899, 13919), 'numpy.array', 'np.array', (['axis.shape'], {}), '(axis.shape)\n', (13907, 13919), True, 'import numpy as np\n'), ((13943, 13960), 'numpy.sum', 'np.sum', (['idx_index'], {}), '(idx_index)\n', (13949, 13960), True, 'import numpy as np\n'), ((23183, 23210), 'numpy.diff', 'np.diff', (['x'], {'axis': 'axis_index'}), '(x, axis=axis_index)\n', (23190, 23210), True, 'import numpy as np\n'), ((28946, 28978), 'numpy.empty', 'np.empty', (['new_shape'], {'dtype': 'rtype'}), '(new_shape, dtype=rtype)\n', (28954, 28978), True, 'import numpy as np\n'), ((39707, 39744), 'numpy.around', 'np.around', (['timer.interval'], {'decimals': '(3)'}), '(timer.interval, decimals=3)\n', (39716, 39744), True, 'import numpy as np\n'), ((42946, 42976), 'numpy.expand_dims', 'np.expand_dims', (['points'], {'axis': 'i'}), '(points, axis=i)\n', (42960, 42976), True, 'import numpy as np\n'), ((49889, 49921), 'numpy.ceil', 'np.ceil', (['((_max - _min) / spacing)'], {}), '((_max - _min) / spacing)\n', (49896, 49921), True, 'import numpy as np\n'), ((49959, 49985), 'numpy.linspace', 'np.linspace', (['_min', '_max', 'n'], {}), '(_min, _max, n)\n', (49970, 49985), True, 'import numpy as np\n'), ((54642, 54739), 'warnings.warn', 'warnings.warn', (['"""Variable being removed used in a constant"""', 'wt_exceptions.WrightToolsWarning'], {}), "('Variable being removed used in a constant', wt_exceptions.\n WrightToolsWarning)\n", (54655, 54739), False, 'import warnings\n'), ((49609, 49641), 'numpy.ceil', 'np.ceil', (['((_max - _min) / spacing)'], {}), '((_max - _min) / spacing)\n', (49616, 49641), True, 'import numpy as np\n'), ((39274, 39287), 'numpy.isnan', 'np.isnan', (['arr'], {}), '(arr)\n', (39282, 39287), True, 'import numpy as np\n'), ((68277, 68300), 'numpy.isnan', 'np.isnan', (['tempax.masked'], {}), '(tempax.masked)\n', (68285, 68300), True, 'import numpy as np\n')]
# Plot polynomial regression on 1d problem # Based on https://github.com/probml/pmtk3/blob/master/demos/linregPolyVsDegree.m import numpy as np import matplotlib.pyplot as plt from pyprobml_utils import save_fig from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.preprocessing import MinMaxScaler import sklearn.metrics from sklearn.metrics import mean_squared_error as mse def make_1dregression_data(n=21): np.random.seed(0) xtrain = np.linspace(0.0, 20, n) xtest = np.arange(0.0, 20, 0.1) sigma2 = 4 w = np.array([-1.5, 1/9.]) fun = lambda x: w[0]*x + w[1]*np.square(x) ytrain = fun(xtrain) + np.random.normal(0, 1, xtrain.shape) * \ np.sqrt(sigma2) ytest= fun(xtest) + np.random.normal(0, 1, xtest.shape) * \ np.sqrt(sigma2) return xtrain, ytrain, xtest, ytest xtrain, ytrain, xtest, ytest = make_1dregression_data(n=21) #Rescaling data scaler = MinMaxScaler(feature_range=(-1, 1)) Xtrain = scaler.fit_transform(xtrain.reshape(-1, 1)) Xtest = scaler.transform(xtest.reshape(-1, 1)) degs = np.arange(1, 21, 1) ndegs = np.max(degs) mse_train = np.empty(ndegs) mse_test = np.empty(ndegs) ytest_pred_stored = np.empty(ndegs, dtype=np.ndarray) ytrain_pred_stored = np.empty(ndegs, dtype=np.ndarray) for deg in degs: model = LinearRegression() poly_features = PolynomialFeatures(degree=deg, include_bias=False) Xtrain_poly = poly_features.fit_transform(Xtrain) model.fit(Xtrain_poly, ytrain) ytrain_pred = model.predict(Xtrain_poly) ytrain_pred_stored[deg-1] = ytrain_pred Xtest_poly = poly_features.transform(Xtest) ytest_pred = model.predict(Xtest_poly) mse_train[deg-1] = mse(ytrain_pred, ytrain) mse_test[deg-1] = mse(ytest_pred, ytest) ytest_pred_stored[deg-1] = ytest_pred # Plot MSE vs degree fig, ax = plt.subplots() mask = degs <= 15 ax.plot(degs[mask], mse_test[mask], color = 'r', marker = 'x',label='test') ax.plot(degs[mask], mse_train[mask], color='b', marker = 's', label='train') ax.legend(loc='upper right', shadow=True) plt.xlabel('degree') plt.ylabel('mse') save_fig('polyfitVsDegree.pdf') plt.show() # Plot fitted functions chosen_degs = [1, 2, 14, 20] for deg in chosen_degs: fig, ax = plt.subplots() ax.scatter(xtrain, ytrain) ax.plot(xtest, ytest_pred_stored[deg-1]) ax.set_ylim((-10, 15)) plt.title('degree {}'.format(deg)) save_fig('polyfitDegree{}.pdf'.format(deg)) plt.show() # Plot residuals #https://blog.minitab.com/blog/adventures-in-statistics-2/why-you-need-to-check-your-residual-plots-for-regression-analysis chosen_degs = [1, 2, 14, 20] for deg in chosen_degs: fig, ax = plt.subplots() ypred = ytrain_pred_stored[deg-1] residuals = ytrain - ypred ax.plot(ypred, residuals, 'o') ax.set_xlabel('predicted y') ax.set_ylabel('residual') plt.title('degree {}. Predictions on the training set'.format(deg)) save_fig('polyfitDegree{}Residuals.pdf'.format(deg)) plt.show() # Plot fit vs actual # https://blog.minitab.com/blog/adventures-in-statistics-2/regression-analysis-how-do-i-interpret-r-squared-and-assess-the-goodness-of-fit chosen_degs = [1, 2, 14, 20] for deg in chosen_degs: for train in [True, False]: if train: ytrue = ytrain ypred = ytrain_pred_stored[deg-1] dataset = 'Train' else: ytrue = ytest ypred = ytest_pred_stored[deg-1] dataset = 'Test' fig, ax = plt.subplots() ax.scatter(ytrue, ypred) ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3") ax.set_xlabel('true y') ax.set_ylabel('predicted y') r2 = sklearn.metrics.r2_score(ytrue, ypred) plt.title('degree {}. R2 on {} = {:0.3f}'.format(deg, dataset, r2)) save_fig('polyfitDegree{}FitVsActual{}.pdf'.format(deg, dataset)) plt.show()
[ "pyprobml_utils.save_fig", "matplotlib.pyplot.show", "numpy.random.seed", "numpy.empty", "sklearn.preprocessing.MinMaxScaler", "numpy.square", "sklearn.linear_model.LinearRegression", "sklearn.preprocessing.PolynomialFeatures", "numpy.max", "numpy.arange", "numpy.array", "numpy.linspace", "numpy.random.normal", "matplotlib.pyplot.ylabel", "matplotlib.pyplot.xlabel", "matplotlib.pyplot.subplots", "sklearn.metrics.mean_squared_error", "numpy.sqrt" ]
[((974, 1009), 'sklearn.preprocessing.MinMaxScaler', 'MinMaxScaler', ([], {'feature_range': '(-1, 1)'}), '(feature_range=(-1, 1))\n', (986, 1009), False, 'from sklearn.preprocessing import MinMaxScaler\n'), ((1119, 1138), 'numpy.arange', 'np.arange', (['(1)', '(21)', '(1)'], {}), '(1, 21, 1)\n', (1128, 1138), True, 'import numpy as np\n'), ((1147, 1159), 'numpy.max', 'np.max', (['degs'], {}), '(degs)\n', (1153, 1159), True, 'import numpy as np\n'), ((1172, 1187), 'numpy.empty', 'np.empty', (['ndegs'], {}), '(ndegs)\n', (1180, 1187), True, 'import numpy as np\n'), ((1199, 1214), 'numpy.empty', 'np.empty', (['ndegs'], {}), '(ndegs)\n', (1207, 1214), True, 'import numpy as np\n'), ((1235, 1268), 'numpy.empty', 'np.empty', (['ndegs'], {'dtype': 'np.ndarray'}), '(ndegs, dtype=np.ndarray)\n', (1243, 1268), True, 'import numpy as np\n'), ((1290, 1323), 'numpy.empty', 'np.empty', (['ndegs'], {'dtype': 'np.ndarray'}), '(ndegs, dtype=np.ndarray)\n', (1298, 1323), True, 'import numpy as np\n'), ((1884, 1898), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (1896, 1898), True, 'import matplotlib.pyplot as plt\n'), ((2112, 2132), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""degree"""'], {}), "('degree')\n", (2122, 2132), True, 'import matplotlib.pyplot as plt\n'), ((2133, 2150), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""mse"""'], {}), "('mse')\n", (2143, 2150), True, 'import matplotlib.pyplot as plt\n'), ((2151, 2182), 'pyprobml_utils.save_fig', 'save_fig', (['"""polyfitVsDegree.pdf"""'], {}), "('polyfitVsDegree.pdf')\n", (2159, 2182), False, 'from pyprobml_utils import save_fig\n'), ((2183, 2193), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2191, 2193), True, 'import matplotlib.pyplot as plt\n'), ((483, 500), 'numpy.random.seed', 'np.random.seed', (['(0)'], {}), '(0)\n', (497, 500), True, 'import numpy as np\n'), ((514, 537), 'numpy.linspace', 'np.linspace', (['(0.0)', '(20)', 'n'], {}), '(0.0, 20, n)\n', (525, 537), True, 'import numpy as np\n'), ((550, 573), 'numpy.arange', 'np.arange', (['(0.0)', '(20)', '(0.1)'], {}), '(0.0, 20, 0.1)\n', (559, 573), True, 'import numpy as np\n'), ((597, 622), 'numpy.array', 'np.array', (['[-1.5, 1 / 9.0]'], {}), '([-1.5, 1 / 9.0])\n', (605, 622), True, 'import numpy as np\n'), ((1353, 1371), 'sklearn.linear_model.LinearRegression', 'LinearRegression', ([], {}), '()\n', (1369, 1371), False, 'from sklearn.linear_model import LinearRegression\n'), ((1392, 1442), 'sklearn.preprocessing.PolynomialFeatures', 'PolynomialFeatures', ([], {'degree': 'deg', 'include_bias': '(False)'}), '(degree=deg, include_bias=False)\n', (1410, 1442), False, 'from sklearn.preprocessing import PolynomialFeatures\n'), ((1735, 1759), 'sklearn.metrics.mean_squared_error', 'mse', (['ytrain_pred', 'ytrain'], {}), '(ytrain_pred, ytrain)\n', (1738, 1759), True, 'from sklearn.metrics import mean_squared_error as mse\n'), ((1783, 1805), 'sklearn.metrics.mean_squared_error', 'mse', (['ytest_pred', 'ytest'], {}), '(ytest_pred, ytest)\n', (1786, 1805), True, 'from sklearn.metrics import mean_squared_error as mse\n'), ((2286, 2300), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (2298, 2300), True, 'import matplotlib.pyplot as plt\n'), ((2495, 2505), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2503, 2505), True, 'import matplotlib.pyplot as plt\n'), ((2719, 2733), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (2731, 2733), True, 'import matplotlib.pyplot as plt\n'), ((3035, 3045), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3043, 3045), True, 'import matplotlib.pyplot as plt\n'), ((3548, 3562), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (3560, 3562), True, 'import matplotlib.pyplot as plt\n'), ((3938, 3948), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3946, 3948), True, 'import matplotlib.pyplot as plt\n'), ((694, 730), 'numpy.random.normal', 'np.random.normal', (['(0)', '(1)', 'xtrain.shape'], {}), '(0, 1, xtrain.shape)\n', (710, 730), True, 'import numpy as np\n'), ((743, 758), 'numpy.sqrt', 'np.sqrt', (['sigma2'], {}), '(sigma2)\n', (750, 758), True, 'import numpy as np\n'), ((783, 818), 'numpy.random.normal', 'np.random.normal', (['(0)', '(1)', 'xtest.shape'], {}), '(0, 1, xtest.shape)\n', (799, 818), True, 'import numpy as np\n'), ((831, 846), 'numpy.sqrt', 'np.sqrt', (['sigma2'], {}), '(sigma2)\n', (838, 846), True, 'import numpy as np\n'), ((654, 666), 'numpy.square', 'np.square', (['x'], {}), '(x)\n', (663, 666), True, 'import numpy as np\n')]
import pandas as pd import numpy as np import pickle from keras.preprocessing.text import Tokenizer from keras.preprocessing import sequence from keras.models import Sequential from keras.layers.embeddings import Embedding from keras.layers.convolutional import Conv1D from keras.layers.convolutional import MaxPooling1D from keras.layers import LSTM from keras.layers import Flatten from keras.layers import Dense from keras.callbacks import EarlyStopping # Main function of cnn training def run_neural_network(): print(" == Enter into CNN training step ==") np.random.seed(0) x_train = pd.read_pickle("data/pickles/train_after_preprocess.pkl") x_train = np.array(x_train['tweet']) x_test = pd.read_pickle("data/pickles/test_after_preprocess.pkl") x_test = np.array(x_test['tweet']) y = np.array(int(2500000 / 2) * [0] + int(2500000 / 2) * [1]) print("Data loading finish!") # Tokenization tokenizer = Tokenizer(filters='') tokenizer.fit_on_texts(x_train) # Turn x_train into sequence form sequence_train = tokenizer.texts_to_sequences(x_train) # Turn x_test into sequence form sequence_test = tokenizer.texts_to_sequences(x_test) # Transform sequence_train into into a 2D Numpy array sequence_train = sequence.pad_sequences(sequence_train, maxlen = 30) # Transform sequence_test into into a 2D Numpy array sequence_test = sequence.pad_sequences(sequence_test, maxlen = 30) # Affect input dimension input_dim = len(tokenizer.word_index) + 1 input_length = sequence_train.shape[1] print("Tokenization finish!") # Shuffle training dataset new_index = np.arange(sequence_train.shape[0]) np.random.shuffle(new_index) sequence_train = sequence_train[new_index] y = y[new_index] print("Data shuffling finish!") earlyStopping = EarlyStopping(monitor = 'val_loss', patience = 2) ### Model 1 ### print("Build model1!") np.random.seed(1) model = Sequential() model.add(Embedding(input_dim, 50, input_length = input_length)) model.add(Conv1D(padding = "same", kernel_size = 3, filters = 32, activation = "relu")) model.add(MaxPooling1D(pool_size = 2)) model.add(Flatten()) model.add(Dense(250, activation = 'relu')) model.add(Dense(1, activation = 'sigmoid')) model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy']) print("Fit model1!") model.fit(sequence_train, y, validation_split = 0.1, epochs = 10, batch_size = 128, verbose = 1, shuffle = True, callbacks = [earlyStopping]) print("Generate prediction!") train_model1 = model.predict(sequence_train, batch_size = 128) pickle.dump(train_model1, open('data/xgboost/train_model1.txt', 'wb')) test_model1 = model.predict(sequence_test) pickle.dump(test_model1, open('data/xgboost/test_model1.txt', 'wb')) print("Model1 finished!") ### Model 2 ### print("Build model2!") np.random.seed(2) model = Sequential() model.add(Embedding(input_dim, 50, input_length = input_length)) model.add(LSTM(100, recurrent_dropout = 0.2, dropout = 0.2)) model.add(Dense(1, activation = 'sigmoid')) model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy']) print("Fit model2!") model.fit(sequence_train, y, validation_split = 0.1, epochs = 10, batch_size = 128, verbose = 1, shuffle = True, callbacks = [earlyStopping]) print("Generate prediction!") train_model2 = model.predict(sequence_train, batch_size = 128) pickle.dump(train_model2, open('data/xgboost/train_model2.txt', 'wb')) test_model2 = model.predict(sequence_test) pickle.dump(test_model2, open('data/xgboost/test_model2.txt', 'wb')) print("Model2 finished!") ### Model 3 ### print("Build model1!") np.random.seed(3) model = Sequential() model.add(Embedding(input_dim, 50, input_length = input_length)) model.add(Conv1D(padding = "same", kernel_size = 3, filters = 32, activation = "relu")) model.add(MaxPooling1D(pool_size = 2)) model.add(LSTM(100)) model.add(Dense(1, activation = 'sigmoid')) model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy']) print("Fit model3!") model.fit(sequence_train, y, validation_split = 0.1, epochs = 10, batch_size = 128, verbose = 1, shuffle = True, callbacks = [earlyStopping]) print("Generate prediction!") train_model3= model.predict(sequence_train, batch_size = 128) pickle.dump(train_model3, open('data/xgboost/train_model3.txt', 'wb')) test_model3 = model.predict(sequence_test) pickle.dump(test_model3, open('data/xgboost/test_model3.txt', 'wb')) print("Model3 finished!") if __name__ == "__main__": run_neural_network()
[ "keras.layers.embeddings.Embedding", "numpy.random.seed", "keras.preprocessing.sequence.pad_sequences", "keras.layers.LSTM", "keras.layers.Flatten", "keras.layers.convolutional.MaxPooling1D", "keras.preprocessing.text.Tokenizer", "numpy.array", "numpy.arange", "keras.callbacks.EarlyStopping", "keras.layers.convolutional.Conv1D", "pandas.read_pickle", "keras.models.Sequential", "keras.layers.Dense", "numpy.random.shuffle" ]
[((572, 589), 'numpy.random.seed', 'np.random.seed', (['(0)'], {}), '(0)\n', (586, 589), True, 'import numpy as np\n'), ((605, 662), 'pandas.read_pickle', 'pd.read_pickle', (['"""data/pickles/train_after_preprocess.pkl"""'], {}), "('data/pickles/train_after_preprocess.pkl')\n", (619, 662), True, 'import pandas as pd\n'), ((677, 703), 'numpy.array', 'np.array', (["x_train['tweet']"], {}), "(x_train['tweet'])\n", (685, 703), True, 'import numpy as np\n'), ((718, 774), 'pandas.read_pickle', 'pd.read_pickle', (['"""data/pickles/test_after_preprocess.pkl"""'], {}), "('data/pickles/test_after_preprocess.pkl')\n", (732, 774), True, 'import pandas as pd\n'), ((788, 813), 'numpy.array', 'np.array', (["x_test['tweet']"], {}), "(x_test['tweet'])\n", (796, 813), True, 'import numpy as np\n'), ((951, 972), 'keras.preprocessing.text.Tokenizer', 'Tokenizer', ([], {'filters': '""""""'}), "(filters='')\n", (960, 972), False, 'from keras.preprocessing.text import Tokenizer\n'), ((1281, 1330), 'keras.preprocessing.sequence.pad_sequences', 'sequence.pad_sequences', (['sequence_train'], {'maxlen': '(30)'}), '(sequence_train, maxlen=30)\n', (1303, 1330), False, 'from keras.preprocessing import sequence\n'), ((1410, 1458), 'keras.preprocessing.sequence.pad_sequences', 'sequence.pad_sequences', (['sequence_test'], {'maxlen': '(30)'}), '(sequence_test, maxlen=30)\n', (1432, 1458), False, 'from keras.preprocessing import sequence\n'), ((1662, 1696), 'numpy.arange', 'np.arange', (['sequence_train.shape[0]'], {}), '(sequence_train.shape[0])\n', (1671, 1696), True, 'import numpy as np\n'), ((1701, 1729), 'numpy.random.shuffle', 'np.random.shuffle', (['new_index'], {}), '(new_index)\n', (1718, 1729), True, 'import numpy as np\n'), ((1855, 1900), 'keras.callbacks.EarlyStopping', 'EarlyStopping', ([], {'monitor': '"""val_loss"""', 'patience': '(2)'}), "(monitor='val_loss', patience=2)\n", (1868, 1900), False, 'from keras.callbacks import EarlyStopping\n'), ((1958, 1975), 'numpy.random.seed', 'np.random.seed', (['(1)'], {}), '(1)\n', (1972, 1975), True, 'import numpy as np\n'), ((1988, 2000), 'keras.models.Sequential', 'Sequential', ([], {}), '()\n', (1998, 2000), False, 'from keras.models import Sequential\n'), ((2969, 2986), 'numpy.random.seed', 'np.random.seed', (['(2)'], {}), '(2)\n', (2983, 2986), True, 'import numpy as np\n'), ((2999, 3011), 'keras.models.Sequential', 'Sequential', ([], {}), '()\n', (3009, 3011), False, 'from keras.models import Sequential\n'), ((3838, 3855), 'numpy.random.seed', 'np.random.seed', (['(3)'], {}), '(3)\n', (3852, 3855), True, 'import numpy as np\n'), ((3868, 3880), 'keras.models.Sequential', 'Sequential', ([], {}), '()\n', (3878, 3880), False, 'from keras.models import Sequential\n'), ((2015, 2066), 'keras.layers.embeddings.Embedding', 'Embedding', (['input_dim', '(50)'], {'input_length': 'input_length'}), '(input_dim, 50, input_length=input_length)\n', (2024, 2066), False, 'from keras.layers.embeddings import Embedding\n'), ((2084, 2152), 'keras.layers.convolutional.Conv1D', 'Conv1D', ([], {'padding': '"""same"""', 'kernel_size': '(3)', 'filters': '(32)', 'activation': '"""relu"""'}), "(padding='same', kernel_size=3, filters=32, activation='relu')\n", (2090, 2152), False, 'from keras.layers.convolutional import Conv1D\n'), ((2176, 2201), 'keras.layers.convolutional.MaxPooling1D', 'MaxPooling1D', ([], {'pool_size': '(2)'}), '(pool_size=2)\n', (2188, 2201), False, 'from keras.layers.convolutional import MaxPooling1D\n'), ((2219, 2228), 'keras.layers.Flatten', 'Flatten', ([], {}), '()\n', (2226, 2228), False, 'from keras.layers import Flatten\n'), ((2244, 2273), 'keras.layers.Dense', 'Dense', (['(250)'], {'activation': '"""relu"""'}), "(250, activation='relu')\n", (2249, 2273), False, 'from keras.layers import Dense\n'), ((2291, 2321), 'keras.layers.Dense', 'Dense', (['(1)'], {'activation': '"""sigmoid"""'}), "(1, activation='sigmoid')\n", (2296, 2321), False, 'from keras.layers import Dense\n'), ((3026, 3077), 'keras.layers.embeddings.Embedding', 'Embedding', (['input_dim', '(50)'], {'input_length': 'input_length'}), '(input_dim, 50, input_length=input_length)\n', (3035, 3077), False, 'from keras.layers.embeddings import Embedding\n'), ((3095, 3140), 'keras.layers.LSTM', 'LSTM', (['(100)'], {'recurrent_dropout': '(0.2)', 'dropout': '(0.2)'}), '(100, recurrent_dropout=0.2, dropout=0.2)\n', (3099, 3140), False, 'from keras.layers import LSTM\n'), ((3160, 3190), 'keras.layers.Dense', 'Dense', (['(1)'], {'activation': '"""sigmoid"""'}), "(1, activation='sigmoid')\n", (3165, 3190), False, 'from keras.layers import Dense\n'), ((3895, 3946), 'keras.layers.embeddings.Embedding', 'Embedding', (['input_dim', '(50)'], {'input_length': 'input_length'}), '(input_dim, 50, input_length=input_length)\n', (3904, 3946), False, 'from keras.layers.embeddings import Embedding\n'), ((3964, 4032), 'keras.layers.convolutional.Conv1D', 'Conv1D', ([], {'padding': '"""same"""', 'kernel_size': '(3)', 'filters': '(32)', 'activation': '"""relu"""'}), "(padding='same', kernel_size=3, filters=32, activation='relu')\n", (3970, 4032), False, 'from keras.layers.convolutional import Conv1D\n'), ((4056, 4081), 'keras.layers.convolutional.MaxPooling1D', 'MaxPooling1D', ([], {'pool_size': '(2)'}), '(pool_size=2)\n', (4068, 4081), False, 'from keras.layers.convolutional import MaxPooling1D\n'), ((4099, 4108), 'keras.layers.LSTM', 'LSTM', (['(100)'], {}), '(100)\n', (4103, 4108), False, 'from keras.layers import LSTM\n'), ((4124, 4154), 'keras.layers.Dense', 'Dense', (['(1)'], {'activation': '"""sigmoid"""'}), "(1, activation='sigmoid')\n", (4129, 4154), False, 'from keras.layers import Dense\n')]
from numpy.linalg import norm from numpy import dot def cosine_sim(vec1, vec2): """Calculates the cosine similarity between two vectors Args: vec1 (list of float): A vector vec2 (list of float): A vector Returns: The cosine similarity between the two input vectors """ return dot(vec1, vec2) / (norm(vec1) * norm(vec2)) def select_salient_posts(post_vectors, post_weights, k=10, similarity_threshold=0.4): """ Selects the top k most salient posts in a collection of posts. To avoid redundancy, any post too similar to other-posts are disregarded. Each selected post will therefore be both highly salient and representative of unique semantics. Note: post_vectors and post_weights must be in the same order. The ith element of post_weights must reflect the ith element of post_vectors Args: post_vectors (list of (list of float)): Hybrid tfidf representation of the documents as a document-term matrix post_weights (list of float): Hybrid Tfidf weight for each document k (int): The number of posts to select as output similarity_threshold (float): The maximum cosine similiarity for a post to be selected """ sorted_keyed_vectors = [z for _, z in sorted(zip(post_weights, enumerate(post_vectors)), key=lambda i: i[0], reverse=True)] # z is (i,vi) sorted by weight i = 1 veclength = len(post_vectors) loop_condition = True significant_indices = [0] unsorted_indices = [sorted_keyed_vectors[0][0]] while loop_condition: is_similar = False for j in significant_indices: sim = cosine_sim(sorted_keyed_vectors[j][1], sorted_keyed_vectors[i][1]) if sim >= similarity_threshold: is_similar = True if not is_similar: significant_indices.append(i) unsorted_indices.append(sorted_keyed_vectors[i][0]) if (len(significant_indices) >= k) or (i >= veclength - 1): loop_condition = False i += 1 return unsorted_indices
[ "numpy.dot", "numpy.linalg.norm" ]
[((338, 353), 'numpy.dot', 'dot', (['vec1', 'vec2'], {}), '(vec1, vec2)\n', (341, 353), False, 'from numpy import dot\n'), ((357, 367), 'numpy.linalg.norm', 'norm', (['vec1'], {}), '(vec1)\n', (361, 367), False, 'from numpy.linalg import norm\n'), ((370, 380), 'numpy.linalg.norm', 'norm', (['vec2'], {}), '(vec2)\n', (374, 380), False, 'from numpy.linalg import norm\n')]
import cv2 import numpy as np from elements.yolo import OBJ_DETECTION Object_classes = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] Object_colors = list(np.random.rand(80,3)*255) Object_detector = OBJ_DETECTION('weights/yolov5s.pt', Object_classes) def gstreamer_pipeline( capture_width=1280, capture_height=720, display_width=1280, display_height=720, framerate=60, flip_method=0, ): return ( "nvarguscamerasrc ! " "video/x-raw(memory:NVMM), " "width=(int)%d, height=(int)%d, " "format=(string)NV12, framerate=(fraction)%d/1 ! " "nvvidconv flip-method=%d ! " "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! " "videoconvert ! " "video/x-raw, format=(string)BGR ! appsink" % ( capture_width, capture_height, framerate, flip_method, display_width, display_height, ) ) # To flip the image, modify the flip_method parameter (0 and 2 are the most common) print(gstreamer_pipeline(flip_method=0)) # cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER) cap = cv2.VideoCapture("1627775013.mp4") if cap.isOpened(): window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE) # Window while cv2.getWindowProperty("CSI Camera", 0) >= 0: ret, frame = cap.read() if ret and not(frame is None): # detection process objs = Object_detector.detect(frame) # plotting for obj in objs: # print(obj) label = obj['label'] score = obj['score'] [(xmin,ymin),(xmax,ymax)] = obj['bbox'] color = Object_colors[Object_classes.index(label)] frame = cv2.rectangle(frame, (xmin,ymin), (xmax,ymax), color, 2) frame = cv2.putText(frame, f'{label} ({str(score)})', (xmin,ymin), cv2.FONT_HERSHEY_SIMPLEX , 0.75, color, 1, cv2.LINE_AA) else: break cv2.imshow("CSI Camera", frame) keyCode = cv2.waitKey(30) if keyCode == ord('q'): break cap.release() cv2.destroyAllWindows() else: print("Unable to open camera")
[ "cv2.waitKey", "cv2.imshow", "cv2.VideoCapture", "elements.yolo.OBJ_DETECTION", "cv2.rectangle", "numpy.random.rand", "cv2.destroyAllWindows", "cv2.getWindowProperty", "cv2.namedWindow" ]
[((1145, 1196), 'elements.yolo.OBJ_DETECTION', 'OBJ_DETECTION', (['"""weights/yolov5s.pt"""', 'Object_classes'], {}), "('weights/yolov5s.pt', Object_classes)\n", (1158, 1196), False, 'from elements.yolo import OBJ_DETECTION\n'), ((2131, 2165), 'cv2.VideoCapture', 'cv2.VideoCapture', (['"""1627775013.mp4"""'], {}), "('1627775013.mp4')\n", (2147, 2165), False, 'import cv2\n'), ((2205, 2255), 'cv2.namedWindow', 'cv2.namedWindow', (['"""CSI Camera"""', 'cv2.WINDOW_AUTOSIZE'], {}), "('CSI Camera', cv2.WINDOW_AUTOSIZE)\n", (2220, 2255), False, 'import cv2\n'), ((3154, 3177), 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ([], {}), '()\n', (3175, 3177), False, 'import cv2\n'), ((1101, 1122), 'numpy.random.rand', 'np.random.rand', (['(80)', '(3)'], {}), '(80, 3)\n', (1115, 1122), True, 'import numpy as np\n'), ((2279, 2317), 'cv2.getWindowProperty', 'cv2.getWindowProperty', (['"""CSI Camera"""', '(0)'], {}), "('CSI Camera', 0)\n", (2300, 2317), False, 'import cv2\n'), ((3016, 3047), 'cv2.imshow', 'cv2.imshow', (['"""CSI Camera"""', 'frame'], {}), "('CSI Camera', frame)\n", (3026, 3047), False, 'import cv2\n'), ((3066, 3081), 'cv2.waitKey', 'cv2.waitKey', (['(30)'], {}), '(30)\n', (3077, 3081), False, 'import cv2\n'), ((2779, 2837), 'cv2.rectangle', 'cv2.rectangle', (['frame', '(xmin, ymin)', '(xmax, ymax)', 'color', '(2)'], {}), '(frame, (xmin, ymin), (xmax, ymax), color, 2)\n', (2792, 2837), False, 'import cv2\n')]
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Fri Jan 26 12:25:25 2018 Toy datasets. @author: jlsuarezdiaz """ import numpy as np import pandas as pd from six.moves import xrange from sklearn.preprocessing import LabelEncoder import seaborn as sns import matplotlib.pyplot as plt from sklearn.datasets import( load_iris, load_digits) def toy_plot(X,y): f, ax = plt.subplots(figsize=(12,9)) plt.axis('equal') plt.scatter(X[:,0],X[:,1],c=y,cmap="rainbow",label=y) #cmap = plt.get_cmap('rainbow') #cc = np.unique(y) #cn = len(cc) #for i,c in enumerate(cc): # print(i,c) # ind = np.where(y == c)[0] # print(ind) # XX = X[ind] # print(cmap(i/(cn+1))) # ax.scatter(XX[:,0],XX[:,1],c=cmap(i/(cn+1)),label=c) #plt.legend() plt.show() return plt def circular_toy_dataset(rads = [1,2], samples = [200,200], noise = [0.2,0.2], seed = None): if seed is not None: np.random.seed(seed) n = sum(samples) d = 2 X = np.empty([n,d]) y = np.empty([n]) le = LabelEncoder() le.fit(rads) acum = 0 for j,s in enumerate(samples): for i in xrange(s): ns1 = noise[j]*np.random.randn() ns2 = noise[j]*np.random.randn() x1 = (rads[j]+ns1)*np.cos(2*np.pi*i/s) x2 = (rads[j]+ns2)*np.sin(2*np.pi*i/s) X[acum+i,:] = [x1,x2] y[acum+i] = rads[j] acum += s y = le.transform(y) return X,y def hiperplane_toy_dataset(ws = [[1,1],[1,-1]],bs = [[0,0],[0,0]],nsamples=800,xrange=[-1,1],yrange=[-1,1], noise = 0.1,seed = None): if seed is not None: np.random.seed(seed) n=nsamples d=2 X = np.random.rand(n,d) y = np.zeros([n]) yy = np.empty([n,len(ws)]) X[:,0] = (xrange[1]-xrange[0])*X[:,0]+xrange[0] X[:,1] = (yrange[1]-yrange[0])*X[:,1]+yrange[0] for j, (w, b) in enumerate(zip(ws,bs)): w = np.matrix(w) b = np.matrix(b) ns = noise*np.random.randn(n,2) yy[:,j] = np.sign(((X+ns)-b).dot(w.T)).reshape([n]) yy[yy==-1]=0 yy = yy.astype(int) for i in range(n): for j, u in enumerate(yy[i,:]): y[i] += (u << j) return X,y def iris2d_toy_dataset(dims=[0,2]): data=load_iris() # IRIS X=data['data'] X=X[:,dims] y=data['target'] return X,y def balls_toy_dataset(centers = [[-2,-2],[0,0],[2,2],[2,-2],[-2,2]],rads = [1.4,1.4,1.4,1.4,1.4],samples=[200,200,200,200,200],noise = [0.3,0.3,0.3,0.3,0.3],seed=None): if seed is not None: np.random.seed(seed) n = sum(samples) d=2 X=np.empty([n,d]) y=np.empty([n]) acum=0 for j, s in enumerate(samples): rs = rads[j]*np.random.rand(s) angs = 2*np.pi*np.random.rand(s) ns = noise[j]*np.random.rand(s) c = np.array(centers[j]) for i in xrange(s): X[acum+i,:] = c +ns[i] + rs[i]*np.array([np.cos(angs[i]),np.sin(angs[i])]) y[acum+i]=j acum += s return X,y def simetria_hor(A): nrow, ncol= A.shape A = np.abs(A-A[:,::-1]) # Diferencia con la imagen simétrica return np.mean(A) # Media de las diferencias (grado de simetría) def simetria_ver(A): nrow, ncol= A.shape A = np.abs(A-A[::-1,:]) # Diferencia con la imagen simétrica return np.mean(A) # Media de las diferencias (grado de simetría) def digits_toy_dataset(dims=[0,2],numbers=[0,1,2,3,4,5,6,7,8,9]): data=load_digits() XX = data['data'] y = data['target'] nn,dd = XX.shape XX = XX.reshape([nn,8,8]) X = np.empty([nn,3]) for i in xrange(nn): X[i,0] = simetria_hor(XX[i,:,:]) X[i,1] = simetria_ver(XX[i,:,:]) X[i,2] = np.mean(XX[i,:]) selected = np.where(np.isin(y,numbers))[0] return X[selected,:][:,dims],y[selected] def single_toy_dataset(samples=8, classes = 3, seed=None): X = np.empty([samples,2]) y = np.empty([samples]) for i in xrange(samples): c = np.random.randint(0,classes) x = np.random.rand(1,2) X[i,:]=x y[i]=c return X,y
[ "sklearn.datasets.load_iris", "sklearn.datasets.load_digits", "numpy.isin", "numpy.abs", "numpy.random.seed", "numpy.empty", "numpy.mean", "numpy.random.randint", "numpy.sin", "numpy.random.randn", "sklearn.preprocessing.LabelEncoder", "matplotlib.pyplot.subplots", "matplotlib.pyplot.show", "numpy.cos", "numpy.matrix", "matplotlib.pyplot.scatter", "numpy.zeros", "matplotlib.pyplot.axis", "six.moves.xrange", "numpy.array", "numpy.random.rand" ]
[((393, 422), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {'figsize': '(12, 9)'}), '(figsize=(12, 9))\n', (405, 422), True, 'import matplotlib.pyplot as plt\n'), ((431, 448), 'matplotlib.pyplot.axis', 'plt.axis', (['"""equal"""'], {}), "('equal')\n", (439, 448), True, 'import matplotlib.pyplot as plt\n'), ((453, 512), 'matplotlib.pyplot.scatter', 'plt.scatter', (['X[:, 0]', 'X[:, 1]'], {'c': 'y', 'cmap': '"""rainbow"""', 'label': 'y'}), "(X[:, 0], X[:, 1], c=y, cmap='rainbow', label=y)\n", (464, 512), True, 'import matplotlib.pyplot as plt\n'), ((835, 845), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (843, 845), True, 'import matplotlib.pyplot as plt\n'), ((1057, 1073), 'numpy.empty', 'np.empty', (['[n, d]'], {}), '([n, d])\n', (1065, 1073), True, 'import numpy as np\n'), ((1081, 1094), 'numpy.empty', 'np.empty', (['[n]'], {}), '([n])\n', (1089, 1094), True, 'import numpy as np\n'), ((1104, 1118), 'sklearn.preprocessing.LabelEncoder', 'LabelEncoder', ([], {}), '()\n', (1116, 1118), False, 'from sklearn.preprocessing import LabelEncoder\n'), ((1792, 1812), 'numpy.random.rand', 'np.random.rand', (['n', 'd'], {}), '(n, d)\n', (1806, 1812), True, 'import numpy as np\n'), ((1820, 1833), 'numpy.zeros', 'np.zeros', (['[n]'], {}), '([n])\n', (1828, 1833), True, 'import numpy as np\n'), ((2394, 2405), 'sklearn.datasets.load_iris', 'load_iris', ([], {}), '()\n', (2403, 2405), False, 'from sklearn.datasets import load_iris, load_digits\n'), ((2758, 2774), 'numpy.empty', 'np.empty', (['[n, d]'], {}), '([n, d])\n', (2766, 2774), True, 'import numpy as np\n'), ((2780, 2793), 'numpy.empty', 'np.empty', (['[n]'], {}), '([n])\n', (2788, 2793), True, 'import numpy as np\n'), ((3248, 3270), 'numpy.abs', 'np.abs', (['(A - A[:, ::-1])'], {}), '(A - A[:, ::-1])\n', (3254, 3270), True, 'import numpy as np\n'), ((3316, 3326), 'numpy.mean', 'np.mean', (['A'], {}), '(A)\n', (3323, 3326), True, 'import numpy as np\n'), ((3445, 3467), 'numpy.abs', 'np.abs', (['(A - A[::-1, :])'], {}), '(A - A[::-1, :])\n', (3451, 3467), True, 'import numpy as np\n'), ((3513, 3523), 'numpy.mean', 'np.mean', (['A'], {}), '(A)\n', (3520, 3523), True, 'import numpy as np\n'), ((3664, 3677), 'sklearn.datasets.load_digits', 'load_digits', ([], {}), '()\n', (3675, 3677), False, 'from sklearn.datasets import load_iris, load_digits\n'), ((3783, 3800), 'numpy.empty', 'np.empty', (['[nn, 3]'], {}), '([nn, 3])\n', (3791, 3800), True, 'import numpy as np\n'), ((3813, 3823), 'six.moves.xrange', 'xrange', (['nn'], {}), '(nn)\n', (3819, 3823), False, 'from six.moves import xrange\n'), ((4112, 4134), 'numpy.empty', 'np.empty', (['[samples, 2]'], {}), '([samples, 2])\n', (4120, 4134), True, 'import numpy as np\n'), ((4142, 4161), 'numpy.empty', 'np.empty', (['[samples]'], {}), '([samples])\n', (4150, 4161), True, 'import numpy as np\n'), ((4175, 4190), 'six.moves.xrange', 'xrange', (['samples'], {}), '(samples)\n', (4181, 4190), False, 'from six.moves import xrange\n'), ((988, 1008), 'numpy.random.seed', 'np.random.seed', (['seed'], {}), '(seed)\n', (1002, 1008), True, 'import numpy as np\n'), ((1206, 1215), 'six.moves.xrange', 'xrange', (['s'], {}), '(s)\n', (1212, 1215), False, 'from six.moves import xrange\n'), ((1731, 1751), 'numpy.random.seed', 'np.random.seed', (['seed'], {}), '(seed)\n', (1745, 1751), True, 'import numpy as np\n'), ((2035, 2047), 'numpy.matrix', 'np.matrix', (['w'], {}), '(w)\n', (2044, 2047), True, 'import numpy as np\n'), ((2060, 2072), 'numpy.matrix', 'np.matrix', (['b'], {}), '(b)\n', (2069, 2072), True, 'import numpy as np\n'), ((2688, 2708), 'numpy.random.seed', 'np.random.seed', (['seed'], {}), '(seed)\n', (2702, 2708), True, 'import numpy as np\n'), ((2978, 2998), 'numpy.array', 'np.array', (['centers[j]'], {}), '(centers[j])\n', (2986, 2998), True, 'import numpy as np\n'), ((3025, 3034), 'six.moves.xrange', 'xrange', (['s'], {}), '(s)\n', (3031, 3034), False, 'from six.moves import xrange\n'), ((3924, 3941), 'numpy.mean', 'np.mean', (['XX[i, :]'], {}), '(XX[i, :])\n', (3931, 3941), True, 'import numpy as np\n'), ((4204, 4233), 'numpy.random.randint', 'np.random.randint', (['(0)', 'classes'], {}), '(0, classes)\n', (4221, 4233), True, 'import numpy as np\n'), ((4245, 4265), 'numpy.random.rand', 'np.random.rand', (['(1)', '(2)'], {}), '(1, 2)\n', (4259, 4265), True, 'import numpy as np\n'), ((2092, 2113), 'numpy.random.randn', 'np.random.randn', (['n', '(2)'], {}), '(n, 2)\n', (2107, 2113), True, 'import numpy as np\n'), ((2867, 2884), 'numpy.random.rand', 'np.random.rand', (['s'], {}), '(s)\n', (2881, 2884), True, 'import numpy as np\n'), ((2908, 2925), 'numpy.random.rand', 'np.random.rand', (['s'], {}), '(s)\n', (2922, 2925), True, 'import numpy as np\n'), ((2948, 2965), 'numpy.random.rand', 'np.random.rand', (['s'], {}), '(s)\n', (2962, 2965), True, 'import numpy as np\n'), ((3970, 3989), 'numpy.isin', 'np.isin', (['y', 'numbers'], {}), '(y, numbers)\n', (3977, 3989), True, 'import numpy as np\n'), ((1244, 1261), 'numpy.random.randn', 'np.random.randn', ([], {}), '()\n', (1259, 1261), True, 'import numpy as np\n'), ((1289, 1306), 'numpy.random.randn', 'np.random.randn', ([], {}), '()\n', (1304, 1306), True, 'import numpy as np\n'), ((1338, 1363), 'numpy.cos', 'np.cos', (['(2 * np.pi * i / s)'], {}), '(2 * np.pi * i / s)\n', (1344, 1363), True, 'import numpy as np\n'), ((1389, 1414), 'numpy.sin', 'np.sin', (['(2 * np.pi * i / s)'], {}), '(2 * np.pi * i / s)\n', (1395, 1414), True, 'import numpy as np\n'), ((3089, 3104), 'numpy.cos', 'np.cos', (['angs[i]'], {}), '(angs[i])\n', (3095, 3104), True, 'import numpy as np\n'), ((3105, 3120), 'numpy.sin', 'np.sin', (['angs[i]'], {}), '(angs[i])\n', (3111, 3120), True, 'import numpy as np\n')]
#!/usr/bin/env python3 """Entropy and information theory related calculations. **Author: <NAME>** """ ######################## Imports ######################## import numpy as np import stp ######################## Helper functions ######################## def _eps_filter(x): """ Checks if the value is within machine-epsilon of zero and maps it to zero if it is the case. Useful for removing negative values in entropies that should otherwise be zero. Args: x (float): value to be checked. Returns: (float): x if the value is not within machine epsilon of zero, 0 otherwise. """ return x if not np.isclose(x, 0, atol=9*10E-15) else 0 ######################## Entropy calculations ######################## def entropy(p): """ Calculates the Shannon entropy for a marginal distribution. Args: p (np.ndarray): the marginal distribution. Returns: (float): the entropy of p """ # Since zeros do not contribute to the Shannon entropy by definition, we # ignore them to avoid any errors/warnings. p = p[p != 0] H = -np.dot(p, np.log(p)) # Filter against machine epsilon return _eps_filter(H) def delta_entropy(R, p): """ Calculates the discrete time change in entropy using the entropy of p evolved with R, minus the entropy of p. Args: R (np.ndarray): the transition matrix. p (np.ndarray): the marginal distribution. Returns: (float): the change in entropy """ return entropy(step(R, p)) - entropy(p) def relative_entropy(p, q): """ Calculates the Kullback-Leibler divergence, which is nonnegative and vanishes if and only if the distributions coincide. Args: p, q (np.ndarray): the probability distributions. Returns: (float): the relative entropy. """ if p.shape[0] != q.shape[0]: print('Dimensions of vectors are not equal. Cannot find relative entropy.') sys.exit() # Any values where p is zero are defined to be zero and hence do not # contribute to the relative entropy # By masking q as well we automatically skip the values that were supposed # to vanish with p avoiding any misalignment issues # Note that by masking q only where p is zero doesn't remove # any mismatching meaning it will still be infinite (as it should be) # in the case where q has a zero that p does not. p_filtered = p[p != 0] log_ratio = np.log(p_filtered / q[p != 0]) return np.dot(p_filtered, log_ratio) def entropy_production(matrix, p, discrete=True): """ Calculates the entropy production for either discrete or continuous time. Args: matrix (np.ndarray): the stochastic matrix, either a discrete time transition matrix or a continuous time rate matrix. p (np.ndarray): the marginal distribution Kwargs: discrete (bool): True if we are calculating the discrete time entropy production (nats), False if we are calculating it in continuous time (nats/time). Returns: (float/np.inf): the entropy production """ log_product = matrix * np.log( matrix / matrix.T ) # The entropy term only exists in the case of discrete time # it vanishes when we calculate the continuous time EP, # by multiplying by the boolean we include it only when # necessary EP = np.dot(log_product.sum(axis=0), p) - (entropy(p) * discrete) \ - np.dot(stp.step(matrix, p), np.log(p)) return EP def entropy_flow(R, p): """ Calculates the discrete time entropy flow. This has not been generalized to handle the continuous time entropy flow yet. Args: R (np.ndarray): the discrete time transition matrix p (np.ndarray): the marginal distribution Returns: (float): the entropy flow """ # Vectorized calculation log_product = R * np.log( R / R.T ) p_step = step(R, p) EF = -np.dot(log_product.sum(axis=0), p) + entropy(p_step) \ + np.dot(p_step, np.log(p)) return EF ######################## Entropy rates ######################## def entropy_rate(R): """ Calculates the asymptotic entropy rate for the provided transition matrix. If the matrix is time-inhomogeneous then we return a function that generates the entropy_rate as a function of n by calculating the systems limiting distribution for each n. Args: R (np.ndarray/function): the transition matrix. Returns: (float/function): the entropy velocity. """ if callable(R): return lambda n : entropy_rate(R(n)) pst = stp.get_stationary_distribution(R, discrete=True) RProduct = (R * np.log(R)).sum(axis=0) return -np.dot(pst, RProduct) ######################## Information Space Objects ######################## class InfoSpace: """ Information space. Holds collections of paths that traverse states in a state space as a matrix, and the probability of each of those paths. Provides functionality on this path space such as providing path entropies. Attributes: paths: the matrix of paths. probabilities: a list of probabilities each path. num_paths: the number of paths considered. path_length: the length of the paths considered. probabilities: a matrix where the (i,j)th element is the probability of observing the first j states of the ith path. entropies: a list of path entropies for each path total_probability: the sum of the probabilities of each path. """ def __init__(self, paths, p_matrix): """ Initializes the InfoSpace object. Args: paths (np.ndarray): a matrix of paths where the (i,j)th element corresponds to the jth symbol of the ith path. p_matrix (np.ndarray): a matrix of probabilities where the (i,j)th element corresponds to the probability of observing the ith path for the first j+1 (zero-indexing) symbols. """ self._paths = np.array(paths) # Matrix of probabilities corresponding to the probability for the path # at each moment. self._p_matrix = np.array(p_matrix) if self._p_matrix.size != 0: # The information space is not empty self._probabilities = self._p_matrix[:, -1] else: # There is zero probability here. self._probabilities = 0 #------------- Properties -------------# @property def paths(self): return self._paths @property def num_paths(self): return self.paths.shape[0] @property def path_length(self): return self.paths.shape[1] @property def probabilities(self): return self._probabilities @property def entropies(self): """ Returns a list of path entropies for each corresponding path probability. """ try: return self._entropies except AttributeError: # It's never been calculated before self._entropies = -np.log(self.probabilities) return self._entropies @property def total_probability(self): try: return self.probabilities.sum() except AttributeError: # Space is empty return 0 #------------- Static methods -------------# @staticmethod def shorten(infospace, path_length, return_index=False): """ Takes an Information Space and shortens it. Since unique paths of length n, may be degenerate when truncated to paths of length m < n, we need to check for degeneracies and filter them out in both paths and probabilities. Args: infospace (InfoSpace): the information space to shorten. path_length (int): the path length the information space should be shortened to. Kwargs: return_index (bool): returns the indices of the non-degenerate paths for the given path length using the original matrix. Useful for filtering other quantities of interest that may not be attached to this object. Returns: (InfoSpace): the shortened InfoSpace. """ if path_length < 1: raise ValueError(f'Invalid path length: {path_length}. Path length must be an integer greater than 0.') elif path_length > infospace.path_length: raise ValueError(f'Cannot shorten an InformationSpace from length: {infospace.path_length} -> {path_length}.') if infospace.paths.size == 0: # This is an empty information space return infospace if not return_index else (infospace, []) # Truncate the path matrix paths = infospace.paths[:, :path_length] # Return index will provide the path indices of the non-degenerate paths _, indices = np.unique(paths, axis=0, return_index=True) # Sort the indices indices = sorted(indices) # Filter out the paths. Not taken from np.unique to ensure the correct # ordering. paths = paths[indices, :] # Truncate the probability matrix p_matrix = infospace._p_matrix[:, :path_length] # Filter the probabilities matrix p_matrix = p_matrix[indices, :] infospace = InfoSpace(paths, p_matrix) return infospace if not return_index else infospace, indices class PartitionedInfoSpace(InfoSpace): """ Partitioned Information Space. Constructs a typical set on an information space to partition it into a typical information space and an atypical one. Holds path probabilities, typical paths, atypical paths, atypical path probabilities and more. This object will use a provided (often sampled) path space to partition the space into a collection of typical and atypical paths depending on the dynamics provided. Will also track other quantities of interest such as the upper and lower bounds on the path probabilities required for the paths to be considered typical. Attributes: paths: the matrix of paths. probabilities: a list of probabilities each path. num_paths: the number of paths considered. path_length: the length of the paths considered. probabilities: a matrix where the (i,j)th element is the probability of observing the first j states of the ith path. entropies: a list of path entropies for each path. entropy_rates: a list of the entropy rates for each various path length. This will be the center of the epsilon-neighborhood for path entropies to qualify paths as typical for. epsilon: the widths of the neighborhood used for paths to be considered typical for each path length. upper/lower: the upper/lower bounds as measured in nats. This means that a path is typical if and only if its path entropy rate is within these bounds. typicalities: a matrix where the (i,j)th element is a boolean determining whether the ith path is typical after j+1 steps. ts: the typical set. ats: the atypical set. """ def __init__(self, entropy_rates, epsilon, paths=None, p_matrix=None, typical_space=None, atypical_space=None): """ Generates the PartitionedInfoSpace. Args: entropy_rates (np.ndarray): a list of the entropy rates for each various path length. This will be the center of the epsilon-neighborhood for path entropies to qualify paths as typical for. epsilon (np.ndarray): the widths of the neighborhood used for paths to be considered typical for each path length. Kwargs: paths (np.ndarray/None): the entire sampled path space, the union of the typical and atypical spaces. If not provided these spaces will be merged to generate it. p_matrix (np.ndarray/None): the entire matrix of probabilities for each path and each path length. If not provided, this will be generated by merging the p_matrix of the typical and atypical spaces. typical_space (InfoSpace/None): the typical set on this space. If None, partitions the provided path space. atypical_space (InfoSpace): the atypical set on this space. If None, partitions the provided path space. """ # Bool if the space simply needs to be partitioned must_partition = (paths is None) or (p_matrix is None) # Bool if the space simply needs to be merged since it's already been # partitioned into a typical and atypical space must_union = (typical_space is None) or (atypical_space is None) if must_partition and must_union: # We need either the paths AND the p_matrix or the tupical/atypical # spaces to partition/union the spaces respectively. raise TypeError('In sufficient information provided to partition/union the Information Space. We need either paths with their probabilities or the already partitioned spaces.') if must_partition: # Partition the paths and probability matrix into a typical and # atypical space # Need to generate the upper/lower bounds for the partitioning # of the spaces self._lower = entropy_rates - epsilon self._upper = entropy_rates + epsilon ts_paths = []; ts_p_matrix = [] ats_paths = []; ats_p_matrix = [] for path, path_index in enumerate(paths): path_prob = p_matrix[path_index] # The path entropy rate for direct comparison with the # upper/lower bounds path_entropy_rate = -np.log(path_prob[-1]) / path_length is_typical = ( (self.lower[-1] <= path_entropy_rate) and (path_entropy_rate <= self._upper) ) if is_typical: ts_paths.append(path) ts_p_matrix.append(path_prob) else: ats_paths.append(path) ats_p_matrix.append(path_prob) typical_space = InfoSpace(ts_paths, ts_p_matrix) atypical_space = InfoSpace(ats_paths, ats_p_matrix) elif must_union: # Union the path data ts_empty = (typical_space.paths.size == 0) ats_empty = (atypical_space.paths.size == 0) if not ts_empty and not ats_empty: # Both are nonempty paths = np.vstack( (typical_space.paths, atypical_space.paths) ) p_matrix = np.vstack( (typical_space._p_matrix, atypical_space._p_matrix) ) elif ts_empty: # Only the typical_space is empty paths = atypical_space.paths p_matrix = atypical_space._p_matrix else: # Only the atypical_space is empty paths = typical_space.paths p_matrix = typical_space._p_matrix ### Storing properties ### self._paths = paths self._p_matrix = p_matrix self._probabilities = self._p_matrix[:, -1] self._entropy_rates = entropy_rates # Generalize the epsilon to a path_length dependent epsilon for # potential generalizations in child classes. if isinstance(epsilon, list): epsilon = np.array(epsilon) if not isinstance(epsilon, np.ndarray): # We were only provided a float epsilon = np.full(self.path_length, epsilon) self._epsilon = epsilon self._ts = typical_space self._ats = atypical_space #------------- Properties -------------# @property def entropy_rates(self): return self._entropy_rates @property def epsilon(self): return self._epsilon @property def upper(self): try: return self._upper except AttributeError: # It's never been calculated before self._upper = self.entropy_rates + self.epsilon return self._upper @property def lower(self): try: return self._lower except AttributeError: # It's never been calculated before. self._lower = self.entropy_rates - self.epsilon return self._lower @property def typicalities(self): """ Returns the matrix of typicalities. """ try: return self._typicalities except AttributeError: # It's never been calculated before typicalities = [] ns = np.arange(1, self.path_length + 1) path_entropy_rates = -np.log(self._p_matrix) / ns self._typicalities = ( (self.lower <= path_entropy_rates) & (path_entropy_rates <= self.upper) ) return self._typicalities @property def ats(self): return self._ats @property def ts(self): return self._ts #------------- Static methods -------------# @staticmethod def shorten(pinfospace, path_length, return_index=False): """ Takes a PartitionedInformationSpace and shortens it. Since unique paths of length n, may be degenerate when truncated to paths of length m < n, we need to check for degeneracies and filter them out in both paths and probabilities. Args: pinfospace (PartitionedInfoSpace): the partitioned information space to shorten. path_length (int): the path length the information space should be shortened to. Kwargs: return_index (bool): returns the indices of the non-degenerate paths for the given path length using the original matrix. Useful for filtering other quantities of interest that may not be attached to this object. Returns: (PartitionedInfoSpace): the shortened PartitionedInfoSpace. """ # Hold the current information space to access properties old_pinfospace = pinfospace # Call parent method # Paths and p_matrix will be handled here along with any other # properties shared with parent. Sorted indices of non-degenerate # paths will be calculated here too. pinfospace, indices = InfoSpace.shorten(old_pinfospace, path_length, return_index=True) # Finish the rest of this object's specific properties # Truncate the entropy_rates entropy_rates = old_pinfospace.entropy_rates[:path_length] # Truncate the epsilon epsilon = old_pinfospace.epsilon[:path_length] # Truncate the typicalities matrix # Necessary to re-partition the space. # Filter out the typicalities matrix typicalities = old_pinfospace.typicalities[indices, :path_length] ### Partitioning ### ts_paths, ts_p_matrix = [], [] ats_paths, ats_p_matrix = [], [] paths = pinfospace.paths p_matrix = pinfospace._p_matrix for path_index, is_typical in enumerate(typicalities[:, -1]): path = paths[path_index] probs = p_matrix[path_index] if is_typical: ts_paths.append(path) ts_p_matrix.append(probs) else: ats_paths.append(path) ats_p_matrix.append(probs) # The partitioned spaces ts = InfoSpace(ts_paths, ts_p_matrix) ats = InfoSpace(ats_paths, ats_p_matrix) pinfospace = PartitionedInfoSpace(entropy_rates=entropy_rates, epsilon=epsilon, paths=paths, p_matrix=p_matrix, typical_space=ts, atypical_space=ats) # Save the pre-generated property pinfospace._typicalities = typicalities return pinfospace if not return_index else pinfospace, indices @staticmethod def partition_space(R, p, paths, epsilon=0.5, return_p=False): """ Partitions a path space using the dynamics provided. Args: R (np.ndarray/function): the transition matrix, time-dependent if provided as a function. p (np.ndarray): the initial marginal distribution. paths (np.ndarray): the portion of the path space to use. Kwargs: epsilon (float/np.ndarray): the radius/radii of the epsilon neighborhood to consider paths to be typical within. return_p (bool): False, return only the PartitionedInfoSpace, True returns both the PartitionedInfoSpace and a list of the marginal vs time. Returns: (ParitionedInfoSpace/2-tuple): the PartitionedInfoSpace (PIS) or the PIS and a list of the marginal versus observation step if return_p is True. """ #------------- Data preparation -------------# # Convert the transition matrix to add time-dependence as a constant # matrix if a constant matrix was provided if not callable(R): # Not being saved as an attribute since this is not easily # recoverable by being saved to a file. # Emphasize saving properties that can be saved/loaded. oldR = R R = lambda n : oldR num_paths, path_length = paths.shape p_matrix = np.zeros(paths.shape) # Initialize the marginal distribution data for x, path in enumerate(paths): # Just equal to the initial marginal p_matrix[x, 0] = p[path[0]] # Used for the bounds entropy_rates = np.array([ entropy_rate(R(i)) for i in range(path_length) ]) # The marginal versus time if return_p: p_vs_time = [p] #------------- Data gathering -------------# # bar = gui.ProgressBar(path_length * num_paths, width=300, title='Gathering data...') ### Quantities versus time ### for current_path_length in range(2, path_length + 1): # The data index i = current_path_length - 1 # Since the marginals are zero-indexed as are the paths step_index = current_path_length - 2 currentR = R(current_path_length - 1) # Propagate the marginal one step and save it separately # for quantities like the temporal coarse graining term pstep = stp.step(currentR, p) ### Path probability calculations ### for x, path in enumerate(paths): current_state = path[step_index] jump_state = path[step_index + 1] # Forward calculations # Recursive calculation to save time last_joint = p_matrix[x, i - 1] jump_prob = currentR[jump_state, current_state] p_matrix[x, i] = last_joint * jump_prob # If updated in each iteration, slows down the simulation # drastically # bar.update(amount=num_paths) if return_p: p_vs_time.append(pstep) # Finished data gathering for this iteration, propagate marginal # forward in time p = pstep # bar.finish() ### Partitioning ### upper = entropy_rates + epsilon lower = entropy_rates - epsilon ts_paths, ts_p_matrix = [], [] ats_paths, ats_p_matrix = [], [] path_entropy_rates = -np.log(p_matrix[:, -1]) / path_length # Identify the paths that are typical and atypical for path_index, path_entropy_rate in enumerate(path_entropy_rates): # Can't create typicality matrix since partitioning it will # break the ordering # Determines whether this path is ultimately typical is_typical = ( (lower[-1] <= path_entropy_rate) and (path_entropy_rate <= upper[-1]) ) probs = p_matrix[path_index] if is_typical: ts_paths.append(path) ts_p_matrix.append(probs) else: ats_paths.append(path) ats_p_matrix.append(probs) # The partitioned spaces ts = InfoSpace(ts_paths, ts_p_matrix) ats = InfoSpace(ats_paths, ats_p_matrix) pinfospace = PartitionedInfoSpace( entropy_rates=entropy_rates, epsilon=epsilon, paths=paths, p_matrix=p_matrix, typical_space=ts, atypical_space=ats ) # Set pre-calculated properties pinfospace._upper = upper pinfospace._lower = lower return (pinfospace, p_vs_time) if return_p else pinfospace ######################## Entry ######################## def main(): print('info.py') ### Testing ### p = stp.rand_p(3) R = stp.self_assembly_transition_matrix() paths = stp.complete_path_space(3, 4) pinfospace = PartitionedInfoSpace.partition_space(R, p, paths) print( f'pinfospace.total_probability: {pinfospace.total_probability}' ) print(pinfospace.ats.num_paths) if __name__ == '__main__': main()
[ "numpy.full", "stp.rand_p", "numpy.log", "stp.self_assembly_transition_matrix", "numpy.zeros", "stp.get_stationary_distribution", "numpy.isclose", "stp.complete_path_space", "numpy.array", "numpy.arange", "numpy.vstack", "numpy.dot", "stp.step", "numpy.unique" ]
[((2633, 2663), 'numpy.log', 'np.log', (['(p_filtered / q[p != 0])'], {}), '(p_filtered / q[p != 0])\n', (2639, 2663), True, 'import numpy as np\n'), ((2676, 2705), 'numpy.dot', 'np.dot', (['p_filtered', 'log_ratio'], {}), '(p_filtered, log_ratio)\n', (2682, 2705), True, 'import numpy as np\n'), ((4922, 4971), 'stp.get_stationary_distribution', 'stp.get_stationary_distribution', (['R'], {'discrete': '(True)'}), '(R, discrete=True)\n', (4953, 4971), False, 'import stp\n'), ((25621, 25634), 'stp.rand_p', 'stp.rand_p', (['(3)'], {}), '(3)\n', (25631, 25634), False, 'import stp\n'), ((25643, 25680), 'stp.self_assembly_transition_matrix', 'stp.self_assembly_transition_matrix', ([], {}), '()\n', (25678, 25680), False, 'import stp\n'), ((25693, 25722), 'stp.complete_path_space', 'stp.complete_path_space', (['(3)', '(4)'], {}), '(3, 4)\n', (25716, 25722), False, 'import stp\n'), ((3348, 3373), 'numpy.log', 'np.log', (['(matrix / matrix.T)'], {}), '(matrix / matrix.T)\n', (3354, 3373), True, 'import numpy as np\n'), ((4164, 4179), 'numpy.log', 'np.log', (['(R / R.T)'], {}), '(R / R.T)\n', (4170, 4179), True, 'import numpy as np\n'), ((5028, 5049), 'numpy.dot', 'np.dot', (['pst', 'RProduct'], {}), '(pst, RProduct)\n', (5034, 5049), True, 'import numpy as np\n'), ((6408, 6423), 'numpy.array', 'np.array', (['paths'], {}), '(paths)\n', (6416, 6423), True, 'import numpy as np\n'), ((6568, 6586), 'numpy.array', 'np.array', (['p_matrix'], {}), '(p_matrix)\n', (6576, 6586), True, 'import numpy as np\n'), ((9348, 9391), 'numpy.unique', 'np.unique', (['paths'], {'axis': '(0)', 'return_index': '(True)'}), '(paths, axis=0, return_index=True)\n', (9357, 9391), True, 'import numpy as np\n'), ((22071, 22092), 'numpy.zeros', 'np.zeros', (['paths.shape'], {}), '(paths.shape)\n', (22079, 22092), True, 'import numpy as np\n'), ((687, 719), 'numpy.isclose', 'np.isclose', (['x', '(0)'], {'atol': '(9 * 1e-14)'}), '(x, 0, atol=9 * 1e-14)\n', (697, 719), True, 'import numpy as np\n'), ((1192, 1201), 'numpy.log', 'np.log', (['p'], {}), '(p)\n', (1198, 1201), True, 'import numpy as np\n'), ((3677, 3696), 'stp.step', 'stp.step', (['matrix', 'p'], {}), '(matrix, p)\n', (3685, 3696), False, 'import stp\n'), ((3698, 3707), 'numpy.log', 'np.log', (['p'], {}), '(p)\n', (3704, 3707), True, 'import numpy as np\n'), ((4296, 4305), 'numpy.log', 'np.log', (['p'], {}), '(p)\n', (4302, 4305), True, 'import numpy as np\n'), ((16041, 16058), 'numpy.array', 'np.array', (['epsilon'], {}), '(epsilon)\n', (16049, 16058), True, 'import numpy as np\n'), ((16173, 16207), 'numpy.full', 'np.full', (['self.path_length', 'epsilon'], {}), '(self.path_length, epsilon)\n', (16180, 16207), True, 'import numpy as np\n'), ((23154, 23175), 'stp.step', 'stp.step', (['currentR', 'p'], {}), '(currentR, p)\n', (23162, 23175), False, 'import stp\n'), ((4992, 5001), 'numpy.log', 'np.log', (['R'], {}), '(R)\n', (4998, 5001), True, 'import numpy as np\n'), ((17286, 17320), 'numpy.arange', 'np.arange', (['(1)', '(self.path_length + 1)'], {}), '(1, self.path_length + 1)\n', (17295, 17320), True, 'import numpy as np\n'), ((24207, 24230), 'numpy.log', 'np.log', (['p_matrix[:, -1]'], {}), '(p_matrix[:, -1])\n', (24213, 24230), True, 'import numpy as np\n'), ((7494, 7520), 'numpy.log', 'np.log', (['self.probabilities'], {}), '(self.probabilities)\n', (7500, 7520), True, 'import numpy as np\n'), ((15131, 15185), 'numpy.vstack', 'np.vstack', (['(typical_space.paths, atypical_space.paths)'], {}), '((typical_space.paths, atypical_space.paths))\n', (15140, 15185), True, 'import numpy as np\n'), ((15215, 15277), 'numpy.vstack', 'np.vstack', (['(typical_space._p_matrix, atypical_space._p_matrix)'], {}), '((typical_space._p_matrix, atypical_space._p_matrix))\n', (15224, 15277), True, 'import numpy as np\n'), ((14274, 14295), 'numpy.log', 'np.log', (['path_prob[-1]'], {}), '(path_prob[-1])\n', (14280, 14295), True, 'import numpy as np\n'), ((17356, 17378), 'numpy.log', 'np.log', (['self._p_matrix'], {}), '(self._p_matrix)\n', (17362, 17378), True, 'import numpy as np\n')]
import sys sys.path.append('/usr/users/oliverren/meng/check-worthy') from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense from keras import Sequential from src.data import debates import numpy as np MAX_NUM_WORDS = 1000 # data_set[i] is the ith crossvalidation split, data_set[i][0] says which debate is the test debate # data_set[i][1] are the sentences in the test set # data_set[i][2] are the sentences in the training set data_sets = debates.get_for_crossvalidation() texts = [sentence.text for sentence in data_sets[0][1]] texts.extend([sentence.text for sentence in data_sets[0][2]]) MAX_SEQUENCE_LENGTH = max([len(sentence.split()) for sentence in texts]) # print(MAX_SEQUENCE_LENGTH) # the embedding is already pretrained, so whenever we go to a different dataset, we should reset the embedding layer # so that the embedding layer uses the words in the vocab of the dataset being tested tokenizer = Tokenizer(num_words= MAX_NUM_WORDS) tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(texts) # print(sequences) # print(texts[0]) # print(tokenizer.word_index) word_index = tokenizer.word_index # print(word_index) data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) # Create Embedding layer embeddings_index = {} f = open('/usr/users/oliverren/meng/check-worthy/data/glove/glove.6B.50d.txt') count = 0 for line in f: values = line.split() if count == 0: # print(values) count += 1 word = values[0] coefs = np.asarray(values[1:], dtype='float32') embeddings_index[word] = coefs f.close() EMBEDDING_DIM = 50 # + 1 because indexes are positive integers embedding_matrix = np.zeros((len(word_index) + 1, EMBEDDING_DIM)) for word, i in word_index.items(): embedding_vector = embeddings_index.get(word) if embedding_vector is not None: # words not in embedding index will be all-zeros embedding_matrix[i] = embedding_vector embedding_layer = Embedding(len(word_index) + 1, EMBEDDING_DIM, weights = [embedding_matrix], input_length = MAX_SEQUENCE_LENGTH, trainable = False) # bi-directional LSTM_OUTPOUT_DIM = 200 HIDDEN_LAYER_DIM = 200 BATCH_SIZE = 32 x_train = [sentence.text for sentence in data_sets[0][2]] y_train = [sentence.label for sentence in data_sets[0][2]] x_test = [sentence.text for sentence in data_sets[0][1]] y_test = [sentence.label for sentence in data_sets[0][1]] x_train = tokenizer.texts_to_sequences(x_train) x_train = pad_sequences(x_train, maxlen=MAX_SEQUENCE_LENGTH) x_test = tokenizer.texts_to_sequences(x_test) x_test = pad_sequences(x_test, maxlen=MAX_SEQUENCE_LENGTH) model = Sequential() model.add(embedding_layer) model.add(Bidirectional(LSTM(LSTM_OUTPOUT_DIM))) model.add(Dense(1,activation='sigmoid')) model.compile(loss = 'binary_crossentropy', optimizer='adam',metrics = ['accuracy']) print(model.summary()) model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=200, validation_data=[x_test, y_test]) model_2 = Sequential() model_2.add(embedding_layer) model_2.add(Bidirectional(LSTM(LSTM_OUTPOUT_DIM))) model_2.add(Dense(HIDDEN_LAYER_DIM*4,activation='relu')) model_2.add(Dropout(0.5)) model_2.add(Dense(HIDDEN_LAYER_DIM,activation='relu')) model_2.add(Dropout(0.5)) model_2.add(Dense(1,activation='sigmoid')) model_2.compile(loss = 'binary_crossentropy', optimizer='adam',metrics = ['accuracy']) print(model_2.summary()) model_2.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=200, validation_data=[x_test, y_test]) from sklearn.metrics import (average_precision_score, precision_score, recall_score, roc_auc_score) def f1(y_true, y_pred): precision = precision_score(y_true, y_pred) recall = recall_score(y_true, y_pred) return 2 * ((precision * recall) / (precision + recall)) def accuracy(y_true, y_pred): num_correct = len([1 for true, pred in zip(y_true, y_pred) if true == pred]) return num_correct/len(y_true) print('model 1') print('f1') print(f1(y_test, model.predict_classes(x_test).reshape(-1))) print('accuracy') print(accuracy(y_test, model.predict_classes(x_test).reshape(-1))) print('model 2') print('f1') print(f1(y_test, model_2.predict_classes(x_test).reshape(-1))) print('accuracy') print(accuracy(y_test, model_2.predict_classes(x_test).reshape(-1)))
[ "sys.path.append", "keras.preprocessing.sequence.pad_sequences", "keras.Sequential", "numpy.asarray", "keras.layers.Dropout", "keras.layers.LSTM", "sklearn.metrics.recall_score", "keras.preprocessing.text.Tokenizer", "src.data.debates.get_for_crossvalidation", "keras.layers.Dense", "sklearn.metrics.precision_score" ]
[((11, 68), 'sys.path.append', 'sys.path.append', (['"""/usr/users/oliverren/meng/check-worthy"""'], {}), "('/usr/users/oliverren/meng/check-worthy')\n", (26, 68), False, 'import sys\n'), ((561, 594), 'src.data.debates.get_for_crossvalidation', 'debates.get_for_crossvalidation', ([], {}), '()\n', (592, 594), False, 'from src.data import debates\n'), ((1033, 1067), 'keras.preprocessing.text.Tokenizer', 'Tokenizer', ([], {'num_words': 'MAX_NUM_WORDS'}), '(num_words=MAX_NUM_WORDS)\n', (1042, 1067), False, 'from keras.preprocessing.text import Tokenizer\n'), ((1276, 1328), 'keras.preprocessing.sequence.pad_sequences', 'pad_sequences', (['sequences'], {'maxlen': 'MAX_SEQUENCE_LENGTH'}), '(sequences, maxlen=MAX_SEQUENCE_LENGTH)\n', (1289, 1328), False, 'from keras.preprocessing.sequence import pad_sequences\n'), ((2679, 2729), 'keras.preprocessing.sequence.pad_sequences', 'pad_sequences', (['x_train'], {'maxlen': 'MAX_SEQUENCE_LENGTH'}), '(x_train, maxlen=MAX_SEQUENCE_LENGTH)\n', (2692, 2729), False, 'from keras.preprocessing.sequence import pad_sequences\n'), ((2785, 2834), 'keras.preprocessing.sequence.pad_sequences', 'pad_sequences', (['x_test'], {'maxlen': 'MAX_SEQUENCE_LENGTH'}), '(x_test, maxlen=MAX_SEQUENCE_LENGTH)\n', (2798, 2834), False, 'from keras.preprocessing.sequence import pad_sequences\n'), ((2845, 2857), 'keras.Sequential', 'Sequential', ([], {}), '()\n', (2855, 2857), False, 'from keras import Sequential\n'), ((3212, 3224), 'keras.Sequential', 'Sequential', ([], {}), '()\n', (3222, 3224), False, 'from keras import Sequential\n'), ((1602, 1641), 'numpy.asarray', 'np.asarray', (['values[1:]'], {'dtype': '"""float32"""'}), "(values[1:], dtype='float32')\n", (1612, 1641), True, 'import numpy as np\n'), ((2944, 2974), 'keras.layers.Dense', 'Dense', (['(1)'], {'activation': '"""sigmoid"""'}), "(1, activation='sigmoid')\n", (2949, 2974), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n'), ((3317, 3363), 'keras.layers.Dense', 'Dense', (['(HIDDEN_LAYER_DIM * 4)'], {'activation': '"""relu"""'}), "(HIDDEN_LAYER_DIM * 4, activation='relu')\n", (3322, 3363), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n'), ((3374, 3386), 'keras.layers.Dropout', 'Dropout', (['(0.5)'], {}), '(0.5)\n', (3381, 3386), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n'), ((3400, 3442), 'keras.layers.Dense', 'Dense', (['HIDDEN_LAYER_DIM'], {'activation': '"""relu"""'}), "(HIDDEN_LAYER_DIM, activation='relu')\n", (3405, 3442), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n'), ((3455, 3467), 'keras.layers.Dropout', 'Dropout', (['(0.5)'], {}), '(0.5)\n', (3462, 3467), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n'), ((3481, 3511), 'keras.layers.Dense', 'Dense', (['(1)'], {'activation': '"""sigmoid"""'}), "(1, activation='sigmoid')\n", (3486, 3511), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n'), ((3916, 3947), 'sklearn.metrics.precision_score', 'precision_score', (['y_true', 'y_pred'], {}), '(y_true, y_pred)\n', (3931, 3947), False, 'from sklearn.metrics import average_precision_score, precision_score, recall_score, roc_auc_score\n'), ((3961, 3989), 'sklearn.metrics.recall_score', 'recall_score', (['y_true', 'y_pred'], {}), '(y_true, y_pred)\n', (3973, 3989), False, 'from sklearn.metrics import average_precision_score, precision_score, recall_score, roc_auc_score\n'), ((2909, 2931), 'keras.layers.LSTM', 'LSTM', (['LSTM_OUTPOUT_DIM'], {}), '(LSTM_OUTPOUT_DIM)\n', (2913, 2931), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n'), ((3280, 3302), 'keras.layers.LSTM', 'LSTM', (['LSTM_OUTPOUT_DIM'], {}), '(LSTM_OUTPOUT_DIM)\n', (3284, 3302), False, 'from keras.layers import Embedding, LSTM, Bidirectional, Dropout, Dense\n')]
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import os import copy import math from pathlib import Path import warnings from typing import Callable, Tuple, Union, List import decord from einops.layers.torch import Rearrange import matplotlib.pyplot as plt import numpy as np from numpy.random import randint import torch from torch.utils.data import Dataset, Subset, DataLoader from torchvision.transforms import Compose from .references import transforms_video as transforms from .references.functional_video import denormalize from ..common.misc import Config from ..common.gpu import num_devices, db_num_workers Trans = Callable[[object, dict], Tuple[object, dict]] DEFAULT_MEAN = (0.43216, 0.394666, 0.37645) DEFAULT_STD = (0.22803, 0.22145, 0.216989) class VideoRecord(object): """ This class is used for parsing split-files where each row contains a path and a label: Ex: ``` path/to/my/clip_1 3 path/to/another/clip_2 32 ``` """ def __init__(self, data: List[str]): """ Initialized a VideoRecord Ex. data = ["path/to/video.mp4", 2, "cooking"] Args: row: a list where first element is the path and second element is the label, and the third element (optional) is the label name """ assert len(data) >= 2 and len(data) <= 3 assert isinstance(data[0], str) assert isinstance(int(data[1]), int) if len(data) == 3: assert isinstance(data[2], str) self._data = data self._num_frames = None @property def path(self) -> str: return self._data[0] @property def num_frames(self) -> int: if self._num_frames is None: self._num_frames = int( len([x for x in Path(self._data[0]).glob("img_*")]) - 1 ) return self._num_frames @property def label(self) -> int: return int(self._data[1]) @property def label_name(self) -> str: return None if len(self._data) <= 2 else self._data[2] def get_transforms(train: bool = True, tfms_config: Config = None) -> Trans: """ Get default transformations to apply depending on whether we're applying it to the training or the validation set. If no tfms configurations are passed in, use the defaults. Args: train: whether or not this is for training tfms_config: Config object with tranforms-related configs Returns: A list of transforms to apply """ if tfms_config is None: tfms_config = get_default_tfms_config(train=train) # 1. resize tfms = [ transforms.ToTensorVideo(), transforms.ResizeVideo( tfms_config.im_scale, tfms_config.resize_keep_ratio ), ] # 2. crop if tfms_config.random_crop: if tfms_config.random_crop_scales: crop = transforms.RandomResizedCropVideo( tfms_config.input_size, tfms_config.random_crop_scales ) else: crop = transforms.RandomCropVideo(tfms_config.input_size) else: crop = transforms.CenterCropVideo(tfms_config.input_size) tfms.append(crop) # 3. flip tfms.append(transforms.RandomHorizontalFlipVideo(tfms_config.flip_ratio)) # 4. normalize tfms.append(transforms.NormalizeVideo(tfms_config.mean, tfms_config.std)) return Compose(tfms) def get_default_tfms_config(train: bool) -> Config: """ Args: train: whether or not this is for training Settings: input_size (int or tuple): Model input image size. im_scale (int or tuple): Resize target size. resize_keep_ratio (bool): If True, keep the original ratio when resizing. mean (tuple): Normalization mean. if train: std (tuple): Normalization std. flip_ratio (float): Horizontal flip ratio. random_crop (bool): If False, do center-crop. random_crop_scales (tuple): Range of size of the origin size random cropped. """ flip_ratio = 0.5 if train else 0.0 random_crop = True if train else False random_crop_scales = (0.6, 1.0) if train else None return Config( dict( input_size=112, im_scale=128, resize_keep_ratio=True, mean=DEFAULT_MEAN, std=DEFAULT_STD, flip_ratio=flip_ratio, random_crop=random_crop, random_crop_scales=random_crop_scales, ) ) class VideoDataset: """ A video recognition dataset. """ def __init__( self, root: str, seed: int = None, train_pct: float = 0.75, num_samples: int = 1, sample_length: int = 8, sample_step: int = 1, temporal_jitter: bool = True, temporal_jitter_step: int = 2, random_shift: bool = True, batch_size: int = 8, video_ext: str = "mp4", warning: bool = False, train_split_file: str = None, test_split_file: str = None, train_transforms: Trans = get_transforms(train=True), test_transforms: Trans = get_transforms(train=False), ) -> None: """ initialize dataset Arg: root: Videos directory. seed: random seed train_pct: percentage of dataset to use for training num_samples: Number of clips to sample from each video. sample_length: Number of consecutive frames to sample from a video (i.e. clip length). sample_step: Sampling step. temporal_jitter: Randomly skip frames when sampling each frames. temporal_jitter_step: temporal jitter in frames random_shift: Random temporal shift when sample a clip. video_ext: Video file extension. warning: On or off warning. train_split_file: Annotation file containing video filenames and labels. test_split_file: Annotation file containing video filenames and labels. train_transforms: transforms for training test_transforms: transforms for testing """ assert sample_step > 0 assert num_samples > 0 if temporal_jitter: assert temporal_jitter_step > 0 if train_split_file: assert Path(train_split_file).exists() assert ( test_split_file is not None and Path(test_split_file).exists() ) if test_split_file: assert Path(test_split_file).exists() assert ( train_split_file is not None and Path(train_split_file).exists() ) self.root = root self.seed = seed self.num_samples = num_samples self.sample_length = sample_length self.sample_step = sample_step self.presample_length = sample_length * sample_step self.temporal_jitter_step = temporal_jitter_step self.train_transforms = train_transforms self.test_transforms = test_transforms self.random_shift = random_shift self.temporal_jitter = temporal_jitter self.batch_size = batch_size self.video_ext = video_ext self.warning = warning # create training and validation datasets self.train_ds, self.test_ds = ( self.split_with_file( train_split_file=train_split_file, test_split_file=test_split_file, ) if train_split_file else self.split_by_folder(train_pct=train_pct) ) # initialize dataloaders self.init_data_loaders() def split_by_folder( self, train_pct: float = 0.8 ) -> Tuple[Dataset, Dataset]: """ Split this dataset into a training and testing set based on the folders that the videos are in. ``` /data +-- action_class_1 | +-- video_01.mp4 | +-- video_02.mp4 | +-- ... +-- action_class_2 | +-- video_11.mp4 | +-- video_12.mp4 | +-- ... +-- ... ``` Args: train_pct: the ratio of images to use for training vs testing Return A training and testing dataset in that order """ self.video_records = [] # get all dirs in root (and make sure they are dirs) dirs = [] for entry in os.listdir(self.root): if os.path.isdir(os.path.join(self.root, entry)): dirs.append(os.path.join(self.root, entry)) # add each video in each dir as a video record label = 0 self.classes = [] for action in dirs: action = os.path.basename(os.path.normpath(action)) self.video_records.extend( [ VideoRecord( [ os.path.join(self.root, action, vid.split(".")[0]), label, action, ] ) for vid in os.listdir(os.path.join(self.root, action)) ] ) label += 1 self.classes.append(action) # random split test_num = math.floor(len(self) * (1 - train_pct)) if self.seed: torch.manual_seed(self.seed) # set indices indices = torch.randperm(len(self)).tolist() train_range = indices[test_num:] test_range = indices[:test_num] return self.split_train_test(train_range, test_range) def split_with_file( self, train_split_file: Union[Path, str], test_split_file: Union[Path, str], ) -> Tuple[Dataset, Dataset]: """ Split this dataset into a training and testing set using a split file. Each line in the split file must use the form: ``` path/to/jumping/video_name_1 3 path/to/swimming/video_name_2 5 path/to/another/jumping/video_name_3 3 ``` Args: split_files: a tuple of 2 files Return: A training and testing dataset in that order """ self.video_records = [] # add train records self.video_records.extend( [ VideoRecord(row.strip().split(" ")) for row in open(train_split_file) ] ) train_len = len(self.video_records) # add validation records self.video_records.extend( [ VideoRecord(row.strip().split(" ")) for row in open(test_split_file) ] ) # create indices indices = torch.arange(0, len(self.video_records)) train_range = indices[:train_len] test_range = indices[train_len:] return self.split_train_test(train_range, test_range) def split_train_test( self, train_range: torch.Tensor, test_range: torch.Tensor, ) -> Tuple[Dataset, Dataset]: """ Split this dataset into a training and testing set Args: train_range: range of indices for training set test_range: range of indices for testing set Return A training and testing dataset in that order """ # create train subset train = copy.deepcopy(Subset(self, train_range)) train.dataset.transforms = self.train_transforms train.dataset.sample_step = ( self.temporal_jitter_step if self.temporal_jitter else self.sample_step ) train.dataset.presample_length = self.sample_length * self.sample_step # create test subset test = copy.deepcopy(Subset(self, test_range)) test.dataset.transforms = self.test_transforms test.dataset.random_shift = False test.dataset.temporal_jitter = False return train, test def init_data_loaders(self) -> None: """ Create training and validation data loaders. """ devices = num_devices() self.train_dl = DataLoader( self.train_ds, batch_size=self.batch_size * devices, shuffle=True, num_workers=db_num_workers(), pin_memory=True, ) self.test_dl = DataLoader( self.test_ds, batch_size=self.batch_size * devices, shuffle=False, num_workers=db_num_workers(), pin_memory=True, ) def __len__(self) -> int: return len(self.video_records) def _sample_indices(self, record: VideoRecord) -> List[int]: """ Create a list of frame-wise offsets into a video record. Depending on whether or not 'random shift' is used, perform a uniform sample or a random sample. Args: record (VideoRecord): A video record. Return: list: Segment offsets (start indices) """ if record.num_frames > self.presample_length: if self.random_shift: # Random sample offsets = np.sort( randint( record.num_frames - self.presample_length + 1, size=self.num_samples, ) ) else: # Uniform sample distance = ( record.num_frames - self.presample_length + 1 ) / self.num_samples offsets = np.array( [ int(distance / 2.0 + distance * x) for x in range(self.num_samples) ] ) else: if self.warning: warnings.warn( f"num_samples and/or sample_length > num_frames in {record.path}" ) offsets = np.zeros((self.num_samples,), dtype=int) return offsets def _get_frames( self, video_reader: decord.VideoReader, offset: int, ) -> List[np.ndarray]: """ Get frames at sample length. Args: video_reader: the decord tool for parsing videos offset: where to start the reader from Returns Frames at sample length in a List """ clip = list() # decord.seek() seems to have a bug. use seek_accurate(). video_reader.seek_accurate(offset) # first frame clip.append(video_reader.next().asnumpy()) # remaining frames try: for i in range(self.sample_length - 1): step = ( randint(self.sample_step + 1) if self.temporal_jitter else self.sample_step ) if step == 0 and self.temporal_jitter: clip.append(clip[-1].copy()) else: if step > 1: video_reader.skip_frames(step - 1) cur_frame = video_reader.next().asnumpy() clip.append(cur_frame) except StopIteration: # pass when video has ended pass # if clip needs more frames, simply duplicate the last frame in the clip. while len(clip) < self.sample_length: clip.append(clip[-1].copy()) return clip def __getitem__(self, idx: int) -> Tuple[torch.tensor, int]: """ Return: (clips (torch.tensor), label (int)) """ record = self.video_records[idx] video_reader = decord.VideoReader( "{}.{}".format( os.path.join(self.root, record.path), self.video_ext ), # TODO try to add `ctx=decord.ndarray.gpu(0) or .cuda(0)` ) record._num_frames = len(video_reader) offsets = self._sample_indices(record) clips = np.array([self._get_frames(video_reader, o) for o in offsets]) if self.num_samples == 1: return ( # [T, H, W, C] -> [C, T, H, W] self.transforms(torch.from_numpy(clips[0])), record.label, ) else: return ( # [S, T, H, W, C] -> [S, C, T, H, W] torch.stack( [self.transforms(torch.from_numpy(c)) for c in clips] ), record.label, ) def _show_batch( self, images: List[torch.tensor], labels: List[int], sample_length: int, mean: Tuple[int, int, int] = DEFAULT_MEAN, std: Tuple[int, int, int] = DEFAULT_STD, ) -> None: """ Display a batch of images. Args: images: List of sample (clip) tensors labels: List of labels sample_length: Number of frames to show for each sample mean: Normalization mean std: Normalization std-dev """ batch_size = len(images) plt.tight_layout() fig, axs = plt.subplots( batch_size, sample_length, figsize=(4 * sample_length, 3 * batch_size), ) for i, ax in enumerate(axs): if batch_size == 1: clip = images[0] else: clip = images[i] clip = Rearrange("c t h w -> t c h w")(clip) if not isinstance(ax, np.ndarray): ax = [ax] for j, a in enumerate(ax): a.axis("off") a.imshow( np.moveaxis(denormalize(clip[j], mean, std).numpy(), 0, -1) ) # display label/label_name on the first image if j == 0: a.text( x=3, y=15, s=f"{labels[i]}", fontsize=20, bbox=dict(facecolor="white", alpha=0.80), ) def show_batch(self, train_or_test: str = "train", rows: int = 2) -> None: """Plot first few samples in the datasets""" if train_or_test == "train": batch = [self.train_ds[i] for i in range(rows)] elif train_or_test == "test": batch = [self.test_ds[i] for i in range(rows)] else: raise ValueError("Unknown data type {}".format(which_data)) images = [im[0] for im in batch] labels = [im[1] for im in batch] self._show_batch(images, labels, self.sample_length)
[ "torch.utils.data.Subset", "os.path.join", "torch.manual_seed", "numpy.zeros", "matplotlib.pyplot.subplots", "pathlib.Path", "torchvision.transforms.Compose", "numpy.random.randint", "os.path.normpath", "warnings.warn", "einops.layers.torch.Rearrange", "matplotlib.pyplot.tight_layout", "os.listdir", "torch.from_numpy" ]
[((3444, 3457), 'torchvision.transforms.Compose', 'Compose', (['tfms'], {}), '(tfms)\n', (3451, 3457), False, 'from torchvision.transforms import Compose\n'), ((8514, 8535), 'os.listdir', 'os.listdir', (['self.root'], {}), '(self.root)\n', (8524, 8535), False, 'import os\n'), ((17194, 17212), 'matplotlib.pyplot.tight_layout', 'plt.tight_layout', ([], {}), '()\n', (17210, 17212), True, 'import matplotlib.pyplot as plt\n'), ((17232, 17320), 'matplotlib.pyplot.subplots', 'plt.subplots', (['batch_size', 'sample_length'], {'figsize': '(4 * sample_length, 3 * batch_size)'}), '(batch_size, sample_length, figsize=(4 * sample_length, 3 *\n batch_size))\n', (17244, 17320), True, 'import matplotlib.pyplot as plt\n'), ((9453, 9481), 'torch.manual_seed', 'torch.manual_seed', (['self.seed'], {}), '(self.seed)\n', (9470, 9481), False, 'import torch\n'), ((11483, 11508), 'torch.utils.data.Subset', 'Subset', (['self', 'train_range'], {}), '(self, train_range)\n', (11489, 11508), False, 'from torch.utils.data import Dataset, Subset, DataLoader\n'), ((11861, 11885), 'torch.utils.data.Subset', 'Subset', (['self', 'test_range'], {}), '(self, test_range)\n', (11867, 11885), False, 'from torch.utils.data import Dataset, Subset, DataLoader\n'), ((14042, 14082), 'numpy.zeros', 'np.zeros', (['(self.num_samples,)'], {'dtype': 'int'}), '((self.num_samples,), dtype=int)\n', (14050, 14082), True, 'import numpy as np\n'), ((8566, 8596), 'os.path.join', 'os.path.join', (['self.root', 'entry'], {}), '(self.root, entry)\n', (8578, 8596), False, 'import os\n'), ((8825, 8849), 'os.path.normpath', 'os.path.normpath', (['action'], {}), '(action)\n', (8841, 8849), False, 'import os\n'), ((13901, 13986), 'warnings.warn', 'warnings.warn', (['f"""num_samples and/or sample_length > num_frames in {record.path}"""'], {}), "(f'num_samples and/or sample_length > num_frames in {record.path}'\n )\n", (13914, 13986), False, 'import warnings\n'), ((15823, 15859), 'os.path.join', 'os.path.join', (['self.root', 'record.path'], {}), '(self.root, record.path)\n', (15835, 15859), False, 'import os\n'), ((17537, 17568), 'einops.layers.torch.Rearrange', 'Rearrange', (['"""c t h w -> t c h w"""'], {}), "('c t h w -> t c h w')\n", (17546, 17568), False, 'from einops.layers.torch import Rearrange\n'), ((6377, 6399), 'pathlib.Path', 'Path', (['train_split_file'], {}), '(train_split_file)\n', (6381, 6399), False, 'from pathlib import Path\n'), ((6571, 6592), 'pathlib.Path', 'Path', (['test_split_file'], {}), '(test_split_file)\n', (6575, 6592), False, 'from pathlib import Path\n'), ((8627, 8657), 'os.path.join', 'os.path.join', (['self.root', 'entry'], {}), '(self.root, entry)\n', (8639, 8657), False, 'import os\n'), ((13278, 13355), 'numpy.random.randint', 'randint', (['(record.num_frames - self.presample_length + 1)'], {'size': 'self.num_samples'}), '(record.num_frames - self.presample_length + 1, size=self.num_samples)\n', (13285, 13355), False, 'from numpy.random import randint\n'), ((14804, 14833), 'numpy.random.randint', 'randint', (['(self.sample_step + 1)'], {}), '(self.sample_step + 1)\n', (14811, 14833), False, 'from numpy.random import randint\n'), ((16280, 16306), 'torch.from_numpy', 'torch.from_numpy', (['clips[0]'], {}), '(clips[0])\n', (16296, 16306), False, 'import torch\n'), ((6478, 6499), 'pathlib.Path', 'Path', (['test_split_file'], {}), '(test_split_file)\n', (6482, 6499), False, 'from pathlib import Path\n'), ((6688, 6710), 'pathlib.Path', 'Path', (['train_split_file'], {}), '(train_split_file)\n', (6692, 6710), False, 'from pathlib import Path\n'), ((9208, 9239), 'os.path.join', 'os.path.join', (['self.root', 'action'], {}), '(self.root, action)\n', (9220, 9239), False, 'import os\n'), ((16508, 16527), 'torch.from_numpy', 'torch.from_numpy', (['c'], {}), '(c)\n', (16524, 16527), False, 'import torch\n'), ((1841, 1860), 'pathlib.Path', 'Path', (['self._data[0]'], {}), '(self._data[0])\n', (1845, 1860), False, 'from pathlib import Path\n')]
from __future__ import print_function import os import cv2 import pickle import argparse import numpy as np import pandas as pd import xml.dom.minidom import matplotlib.pyplot as plt from PIL import Image,ImageDraw root_dir = "./" #root_dir = "/home/ksuresh/fpn.pytorch-master/data/uavdt/data/VOCdevkit2007/UAV2017/" #ImgPath = root_dir+'JPEGImages_split_4_overlap_0/' AnnoPath = root_dir+'Annotations/' #_10_classes_xml/' scale = [] area = [] aspect_ratio = [] def scale_and_aspect(img_idx): # imgfile = ImgPath + img_idx+'.jpg' xmlfile = AnnoPath + img_idx + '.xml' #imgfile = '0000097_09823_d_0000051_det.jpg' #xmlfile = '0000097_09823_d_0000051.xml' # img = Image.open(imgfile) DomTree = xml.dom.minidom.parse(xmlfile) annotation = DomTree.documentElement filenamelist = annotation.getElementsByTagName('filename') filename = filenamelist[0].childNodes[0].data objectlist = annotation.getElementsByTagName('object') #bboxes.append(len(objectlist)) for objects in objectlist: namelist = objects.getElementsByTagName('name') objectname = namelist[0].childNodes[0].data bndbox = objects.getElementsByTagName('bndbox') for box in bndbox: #if objectname == 'pedestrian': x1_list = box.getElementsByTagName('xmin') x1 = int(x1_list[0].childNodes[0].data) y1_list = box.getElementsByTagName('ymin') y1 = int(y1_list[0].childNodes[0].data) x2_list = box.getElementsByTagName('xmax') x2 = int(x2_list[0].childNodes[0].data) y2_list = box.getElementsByTagName('ymax') y2 = int(y2_list[0].childNodes[0].data) #scale.append(get_scale([x1,y1,x2,y2])) if y2 ==y1: y2+=1 print("fault...") if x2 ==x1: x2+=1 print("faulty") aspect_ratio.append(get_aspect_ratio([x1,y1,x2,y2])) area.append((x2-x1)*(y2-y1)) return aspect_ratio, area#, scale def get_scale(bbox): pass def get_aspect_ratio(bbox): return((float(bbox[2])-bbox[0])/(float(bbox[3])-bbox[1])) def plot(aspect_ratio, area, scale): import matplotlib.pyplot as plt plt.hist(np.array(aspect_ratio), bins = 75, range = (0,5)) # plt.xlabel("number of points") # plt.ylabel("aspect ratio") plt.show() plt.hist(area, bins = 75, range = (0,25000)) plt.show() if __name__ == '__main__': for img_idx in os.listdir(AnnoPath): img_idx = img_idx.split(".")[0] #9999966_00000_d_0000093_2': weird #args = parser.parse_args() asr, area= scale_and_aspect(img_idx) # with open("./aspect_ratio.txt", "w") as f: # print(asr, file = f) # print(area) plot(asr, area, [])
[ "numpy.array", "matplotlib.pyplot.show", "os.listdir", "matplotlib.pyplot.hist" ]
[((2469, 2479), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2477, 2479), True, 'import matplotlib.pyplot as plt\n'), ((2485, 2526), 'matplotlib.pyplot.hist', 'plt.hist', (['area'], {'bins': '(75)', 'range': '(0, 25000)'}), '(area, bins=75, range=(0, 25000))\n', (2493, 2526), True, 'import matplotlib.pyplot as plt\n'), ((2535, 2545), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2543, 2545), True, 'import matplotlib.pyplot as plt\n'), ((2596, 2616), 'os.listdir', 'os.listdir', (['AnnoPath'], {}), '(AnnoPath)\n', (2606, 2616), False, 'import os\n'), ((2342, 2364), 'numpy.array', 'np.array', (['aspect_ratio'], {}), '(aspect_ratio)\n', (2350, 2364), True, 'import numpy as np\n')]
import serial import time import matplotlib.pyplot as plt import numpy as np from scipy import signal from scipy.fft import fft, ifft, fftfreq from math import pi from scipy.signal import butter, lfilter def avg(list): return sum(list)/len(list) def butter_bandpass(lowcut, highcut, fs, order=5): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='band') return b, a def butter_bandpass_filter(data, lowcut, highcut, fs, order=5): b, a = butter_bandpass(lowcut, highcut, fs, order=order) y = lfilter(b, a, data) return y def do(): # make sure the 'COM#' is set according the Windows Device Manager ser = serial.Serial('/dev/cu.usbmodem101', 9800, timeout=1) time.sleep(2) timestamp = list() #get the time to figure out the frequency at which for gets data data = [] counter = 0 #counts the data appended without error in order to make the time vector num_samples = 200 for i in range(num_samples): line = ser.readline() # read a byte string if line: timestamp.append(time.time()) string = line.decode() # convert the byte string to a unicode string #try except to catch value error, counter for # of data appended try: num = int(string) # convert the unicode string to an int data.append(num*(3.3/682)) # add int to data converted to voltage (mV) list counter += 1; except ValueError: print('value Error \n') pass ser.close() #analyse recording frequency recording_intervals = list() counter = 0 #counter to not calculate interval in the first recoding for i in range(0, len(timestamp)): if(counter == 0): counter += 1 else: if counter>0: recording_intervals.append(timestamp[i] - timestamp[i-1]) avg_period = avg(recording_intervals) avg_freq = 1/avg_period #freq is 1/sampling_period fs = avg_freq t = np.linspace(0, avg_period, num_samples, endpoint=False) sample = data #plotting voltage reading vs time plt.figure(figsize=(18,4)) plt.plot(t, sample) #t_sample gives error plt.title('Potentiometer Reading vs. Time') plt.xlabel('Time (ms)') plt.ylabel('Signal Amplitude (mV)') plt.grid() plt.show() #plotting fourier transform of the filtered signal (convolved with the butterworth filter) convolution = butter_bandpass_filter(sample, 8, 13, fs, order=5) plt.figure(figsize=(18,4)) plt.plot(t, convolution, alpha=0.4) #he quitado t plt.title('Filtered signal vs. Time') plt.xlabel('Time (ms)') plt.ylabel('Signal Amplitude (mV)') plt.grid() plt.show() #plotting power of the signal using the welch method f, power_signal = signal.welch(convolution, fs) plt.semilogy(f, power_signal) plt.xlabel('frequency [Hz]') plt.ylabel('PSD [V**2/Hz]') plt.grid() plt.show() #only need to filter the power fot the 8-13Hz freq band out_freq_pow = list() for i in range(8,13): out_freq_pow.append(power_signal[i]) return out_freq_pow #Curve analysis vs time performed in GUI b = do() print('Results: \n') print(b) #references: #https://www.eecis.udel.edu/~boncelet/ipynb/Filtering.html #https://pythonforundergradengineers.com/python-arduino-potentiometer.html #https://www.daanmichiels.com/blog/2017/10/filtering-eeg-signals-using-scipy/ #https://www.tutorialspoint.com/python/python_gui_programming.htm #https://stackoverflow.com/questions/12093594/how-to-implement-band-pass-butterworth-filter-with-scipy-signal-butter
[ "serial.Serial", "matplotlib.pyplot.title", "matplotlib.pyplot.show", "scipy.signal.welch", "matplotlib.pyplot.plot", "scipy.signal.lfilter", "time.sleep", "time.time", "matplotlib.pyplot.figure", "numpy.linspace", "matplotlib.pyplot.ylabel", "matplotlib.pyplot.semilogy", "matplotlib.pyplot.xlabel", "matplotlib.pyplot.grid", "scipy.signal.butter" ]
[((381, 421), 'scipy.signal.butter', 'butter', (['order', '[low, high]'], {'btype': '"""band"""'}), "(order, [low, high], btype='band')\n", (387, 421), False, 'from scipy.signal import butter, lfilter\n'), ((572, 591), 'scipy.signal.lfilter', 'lfilter', (['b', 'a', 'data'], {}), '(b, a, data)\n', (579, 591), False, 'from scipy.signal import butter, lfilter\n'), ((698, 751), 'serial.Serial', 'serial.Serial', (['"""/dev/cu.usbmodem101"""', '(9800)'], {'timeout': '(1)'}), "('/dev/cu.usbmodem101', 9800, timeout=1)\n", (711, 751), False, 'import serial\n'), ((756, 769), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (766, 769), False, 'import time\n'), ((2100, 2155), 'numpy.linspace', 'np.linspace', (['(0)', 'avg_period', 'num_samples'], {'endpoint': '(False)'}), '(0, avg_period, num_samples, endpoint=False)\n', (2111, 2155), True, 'import numpy as np\n'), ((2221, 2248), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(18, 4)'}), '(figsize=(18, 4))\n', (2231, 2248), True, 'import matplotlib.pyplot as plt\n'), ((2252, 2271), 'matplotlib.pyplot.plot', 'plt.plot', (['t', 'sample'], {}), '(t, sample)\n', (2260, 2271), True, 'import matplotlib.pyplot as plt\n'), ((2298, 2341), 'matplotlib.pyplot.title', 'plt.title', (['"""Potentiometer Reading vs. Time"""'], {}), "('Potentiometer Reading vs. Time')\n", (2307, 2341), True, 'import matplotlib.pyplot as plt\n'), ((2347, 2370), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Time (ms)"""'], {}), "('Time (ms)')\n", (2357, 2370), True, 'import matplotlib.pyplot as plt\n'), ((2375, 2410), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Signal Amplitude (mV)"""'], {}), "('Signal Amplitude (mV)')\n", (2385, 2410), True, 'import matplotlib.pyplot as plt\n'), ((2415, 2425), 'matplotlib.pyplot.grid', 'plt.grid', ([], {}), '()\n', (2423, 2425), True, 'import matplotlib.pyplot as plt\n'), ((2430, 2440), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2438, 2440), True, 'import matplotlib.pyplot as plt\n'), ((2614, 2641), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(18, 4)'}), '(figsize=(18, 4))\n', (2624, 2641), True, 'import matplotlib.pyplot as plt\n'), ((2645, 2680), 'matplotlib.pyplot.plot', 'plt.plot', (['t', 'convolution'], {'alpha': '(0.4)'}), '(t, convolution, alpha=0.4)\n', (2653, 2680), True, 'import matplotlib.pyplot as plt\n'), ((2699, 2736), 'matplotlib.pyplot.title', 'plt.title', (['"""Filtered signal vs. Time"""'], {}), "('Filtered signal vs. Time')\n", (2708, 2736), True, 'import matplotlib.pyplot as plt\n'), ((2742, 2765), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Time (ms)"""'], {}), "('Time (ms)')\n", (2752, 2765), True, 'import matplotlib.pyplot as plt\n'), ((2770, 2805), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Signal Amplitude (mV)"""'], {}), "('Signal Amplitude (mV)')\n", (2780, 2805), True, 'import matplotlib.pyplot as plt\n'), ((2810, 2820), 'matplotlib.pyplot.grid', 'plt.grid', ([], {}), '()\n', (2818, 2820), True, 'import matplotlib.pyplot as plt\n'), ((2825, 2835), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2833, 2835), True, 'import matplotlib.pyplot as plt\n'), ((2920, 2949), 'scipy.signal.welch', 'signal.welch', (['convolution', 'fs'], {}), '(convolution, fs)\n', (2932, 2949), False, 'from scipy import signal\n'), ((2954, 2983), 'matplotlib.pyplot.semilogy', 'plt.semilogy', (['f', 'power_signal'], {}), '(f, power_signal)\n', (2966, 2983), True, 'import matplotlib.pyplot as plt\n'), ((2988, 3016), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""frequency [Hz]"""'], {}), "('frequency [Hz]')\n", (2998, 3016), True, 'import matplotlib.pyplot as plt\n'), ((3021, 3048), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""PSD [V**2/Hz]"""'], {}), "('PSD [V**2/Hz]')\n", (3031, 3048), True, 'import matplotlib.pyplot as plt\n'), ((3053, 3063), 'matplotlib.pyplot.grid', 'plt.grid', ([], {}), '()\n', (3061, 3063), True, 'import matplotlib.pyplot as plt\n'), ((3068, 3078), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3076, 3078), True, 'import matplotlib.pyplot as plt\n'), ((1124, 1135), 'time.time', 'time.time', ([], {}), '()\n', (1133, 1135), False, 'import time\n')]
from datetime import datetime import click import numpy as np import pandas import pyarrow import pyarrow.parquet as pq import yaml from cloudpathlib import AnyPath, CloudPath from deepdiff import DeepDiff from simple_term_menu import TerminalMenu from tqdm import tqdm import tempfile TYPE_MAPPINGS = {"numeric": "bytes", "text": "string", "boolean": "bool"} BLOCK_COLUMN = "_block_number" BLOCK_COLUMN_TYPE = pyarrow.uint32() def get_select_all_exclusive( database_string, subgraph_table_schema, table_name, start_partition, end_partition, ): return pandas.read_sql( sql=f"""SELECT 'SELECT ' || STRING_AGG('"' || column_name || '"', ', ') || ', lower(block_range) as {BLOCK_COLUMN} FROM {subgraph_table_schema}.{table_name} WHERE lower(block_range) >= %(start_partition)s AND lower(block_range) < %(end_partition)s ORDER BY lower(block_range) asc' FROM information_schema.columns WHERE table_name = %(table_name)s AND table_schema = %(subgraph_table_schema)s AND column_name NOT IN ('vid', 'block_range') """, params={ "start_partition": start_partition, "end_partition": end_partition, "table_name": table_name, "subgraph_table_schema": subgraph_table_schema, }, con=database_string, ).iloc[0][0] def get_column_types(database_string, subgraph_table_schema, table_name): df = pandas.read_sql( sql=f"""SELECT column_name, data_type FROM information_schema.columns WHERE table_name = %(table_name)s AND table_schema = %(subgraph_table_schema)s """, params={ "table_name": table_name, "subgraph_table_schema": subgraph_table_schema, }, con=database_string, ) return {row["column_name"]: row["data_type"] for row in df.to_dict("records")} def get_subgraph_table_schemas(database_string): schema_data = pandas.read_sql( """ SELECT ds.subgraph AS subgraph_deployment, ds.name AS subgraph_table_schema, sv.id, s.name as label, sd.earliest_ethereum_block_number::int as earliest_block, sd.latest_ethereum_block_number::int as latest_block FROM deployment_schemas ds LEFT JOIN subgraphs.subgraph_version sv ON (ds.subgraph = sv.deployment) LEFT JOIN subgraphs.subgraph s ON (s.current_version = sv.id) LEFT JOIN subgraphs.subgraph_deployment sd ON (sd.deployment = ds.subgraph) WHERE ds.active AND s.current_version is not NULL """, con=database_string, ).to_dict("records") return {subgraph["label"]: subgraph for subgraph in schema_data} def get_subgraph_table_schema(subgraph, database_string): schemas = get_subgraph_table_schemas(database_string) return schemas[subgraph]["subgraph_table_schema"] def get_subgraph_deployment(subgraph, database_string): schemas = get_subgraph_table_schemas(database_string) return schemas[subgraph]["subgraph_deployment"] def get_subgraph_block_range(subgraph, database_string): schemas = get_subgraph_table_schemas(database_string) return schemas[subgraph]["earliest_block"], schemas[subgraph]["latest_block"] def convert_columns(df, database_types, table_config): update_types = {BLOCK_COLUMN: BLOCK_COLUMN_TYPE} new_columns = {} for column, mappings in table_config.get("column_mappings", {}).items(): for new_column_name, new_column_config in mappings.items(): scale_factor = new_column_config.get("downscale") if scale_factor: new_column = df[column] // scale_factor else: new_column = df[column] if new_column_config.get("max_value"): max_value = new_column_config["max_value"] validity_column = new_column_config["validity_column"] default = new_column_config["default"] new_columns[new_column_name] = np.where( new_column <= max_value, new_column, default ) new_columns[validity_column] = np.where( new_column <= max_value, True, False ) update_types[validity_column] = "boolean" else: new_columns[new_column_name] = new_column update_types[new_column_name] = new_column_config["type"] for column_name in df.columns: if column_name != BLOCK_COLUMN: database_type = database_types[column_name] if database_type in TYPE_MAPPINGS: update_types[column_name] = TYPE_MAPPINGS[database_type] if database_type == "numeric": df[column_name] = df[column_name].map( lambda x: int(x).to_bytes(32, byteorder="big") ) df = df.rename_axis(None) df = df.assign(**new_columns) table = pyarrow.Table.from_pandas(df, preserve_index=False) schema = table.schema types = { "uint32": pyarrow.uint32(), "uint64": pyarrow.uint64(), "bytes": pyarrow.binary(), "bool": pyarrow.bool_(), "boolean": pyarrow.bool_(), "string": pyarrow.string(), "Numeric38": pyarrow.decimal128(precision=38), } for column_name, new_type in update_types.items(): field_index = schema.get_field_index(column_name) field = schema.field(field_index) new_field = field.with_type(types[new_type]) schema = schema.set(field_index, new_field) table = table.cast(schema, safe=False) return table def get_partition_iterator(min_partition, max_partition, partition_sizes): for partition_size in sorted(partition_sizes, reverse=True): start_partition_allowed = (min_partition // partition_size) * partition_size end_partition_allowed = (max_partition // partition_size) * partition_size last_max_partition = None for start_partition in range( start_partition_allowed, end_partition_allowed, partition_size ): last_max_partition = start_partition + partition_size yield partition_size, start_partition, start_partition + partition_size if last_max_partition is not None: min_partition = last_max_partition def get_partition_file_location( table_dir, partition_size, start_partition, end_partition ): return table_dir.joinpath( f"partition_size={partition_size}", f"start_partition={start_partition}", f"end_partition={end_partition}", "data.parquet", ) def filter_existing_partitions(table_dir, partitions): # Iterate in reverse until one exists, assume all previous exist # We must iterate forwards for processing so return in the correct order. new_partitions = [] for partition in sorted(partitions, reverse=True, key=lambda x: x[1]): if get_partition_file_location(table_dir, *partition).exists(): return sorted(new_partitions, reverse=True) else: new_partitions.append(partition) return new_partitions def extract_from_config(subgraph_config, database_string, output_location): config = yaml.safe_load(AnyPath(subgraph_config).open("r")) return extract(config, database_string, output_location) def write_config(config, root_output_location): config_output_location = root_output_location.joinpath("config.yaml") if config_output_location.exists(): existing_config = yaml.safe_load(config_output_location.open("r")) config_difference = DeepDiff(existing_config, config) if config_difference: raise Exception( f"Config provided does not match the previously seen version at {config_output_location}" ) else: config_output_location.parent.mkdir(parents=True, exist_ok=True) with config_output_location.open("w") as f_out: yaml.dump(config, f_out) def extract(config, database_string, output_location): """Connects to your database and pulls all data from all subgraphs""" subgraph = config["subgraph"] subgraph_deployment = get_subgraph_deployment(subgraph, database_string) subgraph_table_schema = get_subgraph_table_schema(subgraph, database_string) earliest_block, latest_block = get_subgraph_block_range(subgraph, database_string) root_output_location = AnyPath(output_location).joinpath( config["name"], config["version"] ) write_config(config, root_output_location) for table_name, table_config in tqdm( config["tables"].items(), leave=False, desc="Tables" ): table_dir = root_output_location.joinpath( "data", f"subgraph={subgraph_deployment}", f"table={table_name}" ) partition_range = get_partition_iterator( earliest_block, latest_block, table_config["partition_sizes"] ) database_types = get_column_types( database_string, subgraph_table_schema, table_name ) unexported_partitions = filter_existing_partitions(table_dir, partition_range) for partition_size, start_partition, end_partition in tqdm( unexported_partitions, leave=False, desc="Paritions" ): filepath = get_partition_file_location( table_dir, partition_size, start_partition, end_partition ) df = pandas.read_sql( sql=get_select_all_exclusive( database_string, subgraph_table_schema, table_name, start_partition=start_partition, end_partition=end_partition, ), con=database_string, coerce_float=False, ) typed_df = convert_columns(df, database_types, table_config) # Pyarrow can't take a file object so we have to write to a temp file # and upload directly filepath.parent.mkdir(parents=True, exist_ok=True) if isinstance(filepath, CloudPath): with tempfile.TemporaryDirectory() as temp_dir: pq_file_location = AnyPath(temp_dir).joinpath("data.parquet") pq.write_table(typed_df, pq_file_location) filepath.upload_from(pq_file_location) else: pq.write_table(typed_df, filepath) with root_output_location.joinpath("latest.yaml").open("w") as f_out: yaml.dump( { "subgraph": subgraph, "subgraph_deployment": subgraph_deployment, "updated": datetime.now(), "earliest_block": earliest_block, "latest_block": latest_block, }, f_out, ) @click.command() @click.option( "--subgraph-config", help="The config file specifying the data to extract", required=True, ) @click.option( "--database-string", default="postgresql://graph-node:let-me-in@localhost:5432/graph-node", help="The database string for connections, defaults to a local graph-node", ) @click.option( "--output-location", default="data", help="The base output location, whether local or cloud", ) def main(subgraph_config, database_string, output_location): """ Connects to your database and pulls all data from the tables specified in the config file """ extract_from_config(subgraph_config, database_string, output_location) def get_tables_in_schema(database_string, subgraph_table_schema): """ " Returns a list of all tables in the schema which have a block_range column. This corresponds to tables which contain entities that we can extract """ all_tables = pandas.read_sql( f""" SELECT distinct table_name FROM information_schema.columns WHERE table_schema = '{subgraph_table_schema}' AND column_name = 'block_range' ORDER BY table_name """, con=database_string, )["table_name"].tolist() return all_tables @click.command() @click.option( "--config-location", help="The output file location for this config", required=True ) @click.option( "--database-string", default="postgresql://graph-node:let-me-in@localhost:5432/graph-node", help="The database string for connections, defaults to a local graph-node", ) def config_generator(config_location, database_string): # Minimise the width any particular column can use in the preview pandas.set_option("display.max_colwidth", 8) # Let pandas figure out the width of the terminal pandas.set_option("display.width", None) config = {"name": AnyPath(config_location).stem, "version": "0.0.1"} subgraph_table_schemas = get_subgraph_table_schemas(database_string) def preview_schema_data(label): schema = subgraph_table_schemas[label] table_spacer = "\n - " table_list = get_tables_in_schema( database_string, schema["subgraph_table_schema"] ) table_list_formatted = table_spacer + table_spacer.join(table_list) # Make nicer return f""" Subgraph: {schema["subgraph_deployment"]} Tables ({len(table_list)}): {table_list_formatted} """ options = list(subgraph_table_schemas.keys()) terminal_menu = TerminalMenu( options, title="Please select the subgraph you want to extract", preview_command=preview_schema_data, preview_size=0.75, ) menu_entry_index = terminal_menu.show() schema_data = subgraph_table_schemas[options[menu_entry_index]] subgraph_table_schema = schema_data["subgraph_table_schema"] config["subgraph"] = schema_data["label"] tables = get_tables_in_schema(database_string, subgraph_table_schema) def preview_table_data(table): subset = pandas.read_sql( f"select * from {subgraph_table_schema}.{table} limit 10", con=database_string, ) return str(subset.head()) terminal_menu = TerminalMenu( tables, title="Please select the tables you want to extract", preview_command=preview_table_data, preview_size=0.75, multi_select=True, ) table_entry_index = terminal_menu.show() selected_tables = [tables[index] for index in table_entry_index] config["tables"] = {} for table in selected_tables: table_config = {} column_types = get_column_types(database_string, subgraph_table_schema, table) # These sizes are just a sensible default for gnosis chain # With a block duration of about 5 seconds these correspond to (very roughly): # 1024 blocks = 1 1/2 hours # 1024*16 blocks = 1 day # 1024*128 blocks = 1 week # 1024*512 blocks = 1 month table_config["partition_sizes"] = [1024 * 512, 1024 * 128, 1024 * 16, 1024] numeric_columns = sorted( [ column for column, data_type in column_types.items() if data_type == "numeric" ] ) if len(numeric_columns) > 0: terminal_menu = TerminalMenu( numeric_columns, title=f"These columns in table {table} are numeric and will be exported as bytes unless they are mapped, which should be mapped to another type?", multi_select=True, ) mapped_indices = terminal_menu.show() selected_columns = [numeric_columns[index] for index in mapped_indices] if len(selected_columns) > 0: table_config["column_mappings"] = {} for column in selected_columns: table_config["column_mappings"][column] = { f"{column}_uint64": { "type": "uint64", "max_value": 0xFFFFFFFFFFFFFFFF, "default": 0, "validity_column": f"{column}_uint64_valid", } } config["tables"][table] = table_config with open(config_location, "w") as f_out: yaml.dump(config, f_out)
[ "yaml.dump", "click.option", "pyarrow.Table.from_pandas", "cloudpathlib.AnyPath", "pandas.read_sql", "pandas.set_option", "tempfile.TemporaryDirectory", "click.command", "pyarrow.uint64", "pyarrow.binary", "pyarrow.bool_", "datetime.datetime.now", "simple_term_menu.TerminalMenu", "tqdm.tqdm", "pyarrow.uint32", "pyarrow.parquet.write_table", "pyarrow.decimal128", "numpy.where", "deepdiff.DeepDiff", "pyarrow.string" ]
[((414, 430), 'pyarrow.uint32', 'pyarrow.uint32', ([], {}), '()\n', (428, 430), False, 'import pyarrow\n'), ((10988, 11003), 'click.command', 'click.command', ([], {}), '()\n', (11001, 11003), False, 'import click\n'), ((11005, 11113), 'click.option', 'click.option', (['"""--subgraph-config"""'], {'help': '"""The config file specifying the data to extract"""', 'required': '(True)'}), "('--subgraph-config', help=\n 'The config file specifying the data to extract', required=True)\n", (11017, 11113), False, 'import click\n'), ((11125, 11315), 'click.option', 'click.option', (['"""--database-string"""'], {'default': '"""postgresql://graph-node:let-me-in@localhost:5432/graph-node"""', 'help': '"""The database string for connections, defaults to a local graph-node"""'}), "('--database-string', default=\n 'postgresql://graph-node:let-me-in@localhost:5432/graph-node', help=\n 'The database string for connections, defaults to a local graph-node')\n", (11137, 11315), False, 'import click\n'), ((11322, 11433), 'click.option', 'click.option', (['"""--output-location"""'], {'default': '"""data"""', 'help': '"""The base output location, whether local or cloud"""'}), "('--output-location', default='data', help=\n 'The base output location, whether local or cloud')\n", (11334, 11433), False, 'import click\n'), ((12248, 12263), 'click.command', 'click.command', ([], {}), '()\n', (12261, 12263), False, 'import click\n'), ((12265, 12367), 'click.option', 'click.option', (['"""--config-location"""'], {'help': '"""The output file location for this config"""', 'required': '(True)'}), "('--config-location', help=\n 'The output file location for this config', required=True)\n", (12277, 12367), False, 'import click\n'), ((12370, 12560), 'click.option', 'click.option', (['"""--database-string"""'], {'default': '"""postgresql://graph-node:let-me-in@localhost:5432/graph-node"""', 'help': '"""The database string for connections, defaults to a local graph-node"""'}), "('--database-string', default=\n 'postgresql://graph-node:let-me-in@localhost:5432/graph-node', help=\n 'The database string for connections, defaults to a local graph-node')\n", (12382, 12560), False, 'import click\n'), ((1585, 1924), 'pandas.read_sql', 'pandas.read_sql', ([], {'sql': 'f"""SELECT column_name, data_type\n FROM information_schema.columns\n WHERE table_name = %(table_name)s\n AND table_schema = %(subgraph_table_schema)s\n """', 'params': "{'table_name': table_name, 'subgraph_table_schema': subgraph_table_schema}", 'con': 'database_string'}), '(sql=\n f"""SELECT column_name, data_type\n FROM information_schema.columns\n WHERE table_name = %(table_name)s\n AND table_schema = %(subgraph_table_schema)s\n """\n , params={\'table_name\': table_name, \'subgraph_table_schema\':\n subgraph_table_schema}, con=database_string)\n', (1600, 1924), False, 'import pandas\n'), ((5058, 5109), 'pyarrow.Table.from_pandas', 'pyarrow.Table.from_pandas', (['df'], {'preserve_index': '(False)'}), '(df, preserve_index=False)\n', (5083, 5109), False, 'import pyarrow\n'), ((12696, 12740), 'pandas.set_option', 'pandas.set_option', (['"""display.max_colwidth"""', '(8)'], {}), "('display.max_colwidth', 8)\n", (12713, 12740), False, 'import pandas\n'), ((12799, 12839), 'pandas.set_option', 'pandas.set_option', (['"""display.width"""', 'None'], {}), "('display.width', None)\n", (12816, 12839), False, 'import pandas\n'), ((13502, 13645), 'simple_term_menu.TerminalMenu', 'TerminalMenu', (['options'], {'title': '"""Please select the subgraph you want to extract"""', 'preview_command': 'preview_schema_data', 'preview_size': '(0.75)'}), "(options, title=\n 'Please select the subgraph you want to extract', preview_command=\n preview_schema_data, preview_size=0.75)\n", (13514, 13645), False, 'from simple_term_menu import TerminalMenu\n'), ((14212, 14364), 'simple_term_menu.TerminalMenu', 'TerminalMenu', (['tables'], {'title': '"""Please select the tables you want to extract"""', 'preview_command': 'preview_table_data', 'preview_size': '(0.75)', 'multi_select': '(True)'}), "(tables, title='Please select the tables you want to extract',\n preview_command=preview_table_data, preview_size=0.75, multi_select=True)\n", (14224, 14364), False, 'from simple_term_menu import TerminalMenu\n'), ((5168, 5184), 'pyarrow.uint32', 'pyarrow.uint32', ([], {}), '()\n', (5182, 5184), False, 'import pyarrow\n'), ((5204, 5220), 'pyarrow.uint64', 'pyarrow.uint64', ([], {}), '()\n', (5218, 5220), False, 'import pyarrow\n'), ((5239, 5255), 'pyarrow.binary', 'pyarrow.binary', ([], {}), '()\n', (5253, 5255), False, 'import pyarrow\n'), ((5273, 5288), 'pyarrow.bool_', 'pyarrow.bool_', ([], {}), '()\n', (5286, 5288), False, 'import pyarrow\n'), ((5309, 5324), 'pyarrow.bool_', 'pyarrow.bool_', ([], {}), '()\n', (5322, 5324), False, 'import pyarrow\n'), ((5344, 5360), 'pyarrow.string', 'pyarrow.string', ([], {}), '()\n', (5358, 5360), False, 'import pyarrow\n'), ((5383, 5415), 'pyarrow.decimal128', 'pyarrow.decimal128', ([], {'precision': '(38)'}), '(precision=38)\n', (5401, 5415), False, 'import pyarrow\n'), ((7731, 7764), 'deepdiff.DeepDiff', 'DeepDiff', (['existing_config', 'config'], {}), '(existing_config, config)\n', (7739, 7764), False, 'from deepdiff import DeepDiff\n'), ((9338, 9396), 'tqdm.tqdm', 'tqdm', (['unexported_partitions'], {'leave': '(False)', 'desc': '"""Paritions"""'}), "(unexported_partitions, leave=False, desc='Paritions')\n", (9342, 9396), False, 'from tqdm import tqdm\n'), ((14026, 14125), 'pandas.read_sql', 'pandas.read_sql', (['f"""select * from {subgraph_table_schema}.{table} limit 10"""'], {'con': 'database_string'}), "(f'select * from {subgraph_table_schema}.{table} limit 10',\n con=database_string)\n", (14041, 14125), False, 'import pandas\n'), ((16357, 16381), 'yaml.dump', 'yaml.dump', (['config', 'f_out'], {}), '(config, f_out)\n', (16366, 16381), False, 'import yaml\n'), ((2129, 2714), 'pandas.read_sql', 'pandas.read_sql', (['"""\n SELECT\n ds.subgraph AS subgraph_deployment,\n ds.name AS subgraph_table_schema,\n sv.id,\n s.name as label,\n sd.earliest_ethereum_block_number::int as earliest_block,\n sd.latest_ethereum_block_number::int as latest_block\nFROM\n deployment_schemas ds\n LEFT JOIN subgraphs.subgraph_version sv ON (ds.subgraph = sv.deployment)\n LEFT JOIN subgraphs.subgraph s ON (s.current_version = sv.id)\n LEFT JOIN subgraphs.subgraph_deployment sd ON (sd.deployment = ds.subgraph)\nWHERE\n ds.active AND s.current_version is not NULL\n """'], {'con': 'database_string'}), '(\n """\n SELECT\n ds.subgraph AS subgraph_deployment,\n ds.name AS subgraph_table_schema,\n sv.id,\n s.name as label,\n sd.earliest_ethereum_block_number::int as earliest_block,\n sd.latest_ethereum_block_number::int as latest_block\nFROM\n deployment_schemas ds\n LEFT JOIN subgraphs.subgraph_version sv ON (ds.subgraph = sv.deployment)\n LEFT JOIN subgraphs.subgraph s ON (s.current_version = sv.id)\n LEFT JOIN subgraphs.subgraph_deployment sd ON (sd.deployment = ds.subgraph)\nWHERE\n ds.active AND s.current_version is not NULL\n """\n , con=database_string)\n', (2144, 2714), False, 'import pandas\n'), ((8095, 8119), 'yaml.dump', 'yaml.dump', (['config', 'f_out'], {}), '(config, f_out)\n', (8104, 8119), False, 'import yaml\n'), ((8559, 8583), 'cloudpathlib.AnyPath', 'AnyPath', (['output_location'], {}), '(output_location)\n', (8566, 8583), False, 'from cloudpathlib import AnyPath, CloudPath\n'), ((12863, 12887), 'cloudpathlib.AnyPath', 'AnyPath', (['config_location'], {}), '(config_location)\n', (12870, 12887), False, 'from cloudpathlib import AnyPath, CloudPath\n'), ((15340, 15545), 'simple_term_menu.TerminalMenu', 'TerminalMenu', (['numeric_columns'], {'title': 'f"""These columns in table {table} are numeric and will be exported as bytes unless they are mapped, which should be mapped to another type?"""', 'multi_select': '(True)'}), "(numeric_columns, title=\n f'These columns in table {table} are numeric and will be exported as bytes unless they are mapped, which should be mapped to another type?'\n , multi_select=True)\n", (15352, 15545), False, 'from simple_term_menu import TerminalMenu\n'), ((582, 1416), 'pandas.read_sql', 'pandas.read_sql', ([], {'sql': 'f"""SELECT \n \'SELECT \' || STRING_AGG(\'"\' || column_name || \'"\', \', \') || \', lower(block_range) as {BLOCK_COLUMN} \n FROM {subgraph_table_schema}.{table_name}\n WHERE\n lower(block_range) >= %(start_partition)s\n AND lower(block_range) < %(end_partition)s\n ORDER BY lower(block_range) asc\'\n FROM information_schema.columns\n WHERE table_name = %(table_name)s\n AND table_schema = %(subgraph_table_schema)s\n AND column_name NOT IN (\'vid\', \'block_range\')\n """', 'params': "{'start_partition': start_partition, 'end_partition': end_partition,\n 'table_name': table_name, 'subgraph_table_schema': subgraph_table_schema}", 'con': 'database_string'}), '(sql=\n f"""SELECT \n \'SELECT \' || STRING_AGG(\'"\' || column_name || \'"\', \', \') || \', lower(block_range) as {BLOCK_COLUMN} \n FROM {subgraph_table_schema}.{table_name}\n WHERE\n lower(block_range) >= %(start_partition)s\n AND lower(block_range) < %(end_partition)s\n ORDER BY lower(block_range) asc\'\n FROM information_schema.columns\n WHERE table_name = %(table_name)s\n AND table_schema = %(subgraph_table_schema)s\n AND column_name NOT IN (\'vid\', \'block_range\')\n """\n , params={\'start_partition\': start_partition, \'end_partition\':\n end_partition, \'table_name\': table_name, \'subgraph_table_schema\':\n subgraph_table_schema}, con=database_string)\n', (597, 1416), False, 'import pandas\n'), ((4119, 4173), 'numpy.where', 'np.where', (['(new_column <= max_value)', 'new_column', 'default'], {}), '(new_column <= max_value, new_column, default)\n', (4127, 4173), True, 'import numpy as np\n'), ((4259, 4305), 'numpy.where', 'np.where', (['(new_column <= max_value)', '(True)', '(False)'], {}), '(new_column <= max_value, True, False)\n', (4267, 4305), True, 'import numpy as np\n'), ((7367, 7391), 'cloudpathlib.AnyPath', 'AnyPath', (['subgraph_config'], {}), '(subgraph_config)\n', (7374, 7391), False, 'from cloudpathlib import AnyPath, CloudPath\n'), ((10562, 10596), 'pyarrow.parquet.write_table', 'pq.write_table', (['typed_df', 'filepath'], {}), '(typed_df, filepath)\n', (10576, 10596), True, 'import pyarrow.parquet as pq\n'), ((10829, 10843), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (10841, 10843), False, 'from datetime import datetime\n'), ((11951, 12186), 'pandas.read_sql', 'pandas.read_sql', (['f"""\n SELECT distinct table_name FROM information_schema.columns \n WHERE table_schema = \'{subgraph_table_schema}\'\n AND column_name = \'block_range\'\n ORDER BY table_name\n """'], {'con': 'database_string'}), '(\n f"""\n SELECT distinct table_name FROM information_schema.columns \n WHERE table_schema = \'{subgraph_table_schema}\'\n AND column_name = \'block_range\'\n ORDER BY table_name\n """\n , con=database_string)\n', (11966, 12186), False, 'import pandas\n'), ((10281, 10310), 'tempfile.TemporaryDirectory', 'tempfile.TemporaryDirectory', ([], {}), '()\n', (10308, 10310), False, 'import tempfile\n'), ((10426, 10468), 'pyarrow.parquet.write_table', 'pq.write_table', (['typed_df', 'pq_file_location'], {}), '(typed_df, pq_file_location)\n', (10440, 10468), True, 'import pyarrow.parquet as pq\n'), ((10363, 10380), 'cloudpathlib.AnyPath', 'AnyPath', (['temp_dir'], {}), '(temp_dir)\n', (10370, 10380), False, 'from cloudpathlib import AnyPath, CloudPath\n')]
from collections import defaultdict from tqdm import tqdm import numpy as np import gym import wandb import trueskill import torch from torch import nn from torch.nn.utils import rnn from ray import rllib from pdb import set_trace as TT import ray.rllib.agents.ppo.ppo as ppo import ray.rllib.agents.ppo.appo as appo import ray.rllib.agents.impala.impala as impala from ray.rllib.agents.callbacks import DefaultCallbacks from ray.tune.integration.wandb import WandbLoggerCallback from ray.rllib.utils.spaces.flexdict import FlexDict from ray.rllib.models.torch.recurrent_net import RecurrentNetwork from neural_mmo.forge.blade.io.action.static import Action, Fixed from neural_mmo.forge.blade.io.stimulus.static import Stimulus from neural_mmo.forge.blade.lib import overlay from neural_mmo.forge.blade.systems import ai from neural_mmo.forge.ethyr.torch import policy from neural_mmo.forge.ethyr.torch.policy import attention from neural_mmo.forge.trinity import Env from neural_mmo.forge.trinity.dataframe import DataType from neural_mmo.forge.trinity.overlay import Overlay, OverlayRegistry ############################################################################### ### Pytorch model + IO. The pip package contains some submodules class Input(nn.Module): def __init__(self, config, embeddings, attributes): '''Network responsible for processing observations Args: config : A configuration object embeddings : An attribute embedding module attributes : An attribute attention module ''' super().__init__() self.embeddings = nn.ModuleDict() self.attributes = nn.ModuleDict() for _, entity in Stimulus: continuous = len([e for e in entity if e[1].CONTINUOUS]) discrete = len([e for e in entity if e[1].DISCRETE]) self.attributes[entity.__name__] = nn.Linear( (continuous+discrete)*config.HIDDEN, config.HIDDEN) self.embeddings[entity.__name__] = embeddings( continuous=continuous, discrete=4096, config=config) #Hackey obs scaling self.tileWeight = torch.Tensor([1.0, 0.0, 0.02, 0.02, 0.0, 0.0, 0.0, 0.0]) self.entWeight = torch.Tensor([1.0, 0.0, 0.0, 0.05, 0.00, 0.02, 0.02, 0.1, 0.01, 0.1, 0.1, 0.1, 0.3, 0.0, 0,0,0,0,0,0,0,0]) # I added this to make lights observable hopefully def forward(self, inp): '''Produces tensor representations from an IO object Args: inp: An IO object specifying observations Returns: entityLookup: A fixed size representation of each entity ''' #Pack entities of each attribute set entityLookup = {} device = inp['Tile']['Continuous'].device inp['Tile']['Continuous'] *= self.tileWeight.to(device) inp['Entity']['Continuous'] *= self.entWeight.to(device) entityLookup['N'] = inp['Entity'].pop('N') for name, entities in inp.items(): #Construct: Batch, ents, nattrs, hidden embeddings = self.embeddings[name](entities) B, N, _, _ = embeddings.shape embeddings = embeddings.view(B, N, -1) #Construct: Batch, ents, hidden entityLookup[name] = self.attributes[name](embeddings) return entityLookup class Output(nn.Module): def __init__(self, config): '''Network responsible for selecting actions Args: config: A Config object ''' super().__init__() self.config = config self.h = config.HIDDEN self.net = DiscreteAction(self.config, self.h, self.h) self.arg = nn.Embedding(Action.n, self.h) def names(self, nameMap, args): '''Lookup argument indices from name mapping''' return np.array([nameMap.get(e) for e in args]) def forward(self, obs, lookup): '''Populates an IO object with actions in-place Args: obs : An IO object specifying observations lookup : A fixed size representation of each entity ''' rets = defaultdict(dict) for atn in Action.edges: for arg in atn.edges: lens = None if arg.argType == Fixed: batch = obs.shape[0] idxs = [e.idx for e in arg.edges] cands = self.arg.weight[idxs] cands = cands.repeat(batch, 1, 1) else: cands = lookup['Entity'] lens = lookup['N'] logits = self.net(obs, cands, lens) rets[atn][arg] = logits return rets class DiscreteAction(nn.Module): '''Head for making a discrete selection from a variable number of candidate actions''' def __init__(self, config, xdim, h): super().__init__() self.net = attention.DotReluBlock(h) def forward(self, stim, args, lens): x = self.net(stim, args) if lens is not None: mask = torch.arange(x.shape[-1]).to(x.device).expand_as(x) x[mask >= lens] = 0 return x class Base(nn.Module): def __init__(self, config): '''Base class for baseline policies Args: config: A Configuration object ''' super().__init__() self.embed = config.EMBED self.config = config self.output = Output(config) self.input = Input(config, embeddings=policy.MixedDTypeInput, attributes=policy.SelfAttention) self.valueF = nn.Linear(config.HIDDEN, 1) def hidden(self, obs, state=None, lens=None): '''Abstract method for hidden state processing, recurrent or otherwise, applied between the input and output modules Args: obs: An observation dictionary, provided by forward() state: The previous hidden state, only provided for recurrent nets lens: Trajectory segment lengths used to unflatten batched obs ''' raise NotImplementedError('Implement this method in a subclass') def forward(self, obs, state=None, lens=None): '''Applies builtin IO and value function with user-defined hidden state subnetwork processing. Arguments are supplied by RLlib ''' entityLookup = self.input(obs) hidden, state = self.hidden(entityLookup, state, lens) self.value = self.valueF(hidden).squeeze(1) actions = self.output(hidden, entityLookup) return actions, state class Encoder(Base): def __init__(self, config): '''Simple baseline model with flat subnetworks''' super().__init__(config) h = config.HIDDEN self.ent = nn.Linear(2*h, h) self.conv = nn.Conv2d(h, h, 3) self.pool = nn.MaxPool2d(2) self.fc = nn.Linear(h*6*6, h) self.proj = nn.Linear(2*h, h) self.attend = policy.SelfAttention(self.embed, h) def hidden(self, obs, state=None, lens=None): #Attentional agent embedding agentEmb = obs['Entity'] selfEmb = agentEmb[:, 0:1].expand_as(agentEmb) agents = torch.cat((selfEmb, agentEmb), dim=-1) agents = self.ent(agents) agents, _ = self.attend(agents) #agents = self.ent(selfEmb) #Convolutional tile embedding tiles = obs['Tile'] self.attn = torch.norm(tiles, p=2, dim=-1) w = self.config.WINDOW batch = tiles.size(0) hidden = tiles.size(2) #Dims correct? tiles = tiles.reshape(batch, w, w, hidden).permute(0, 3, 1, 2) tiles = self.conv(tiles) tiles = self.pool(tiles) tiles = tiles.reshape(batch, -1) tiles = self.fc(tiles) hidden = torch.cat((agents, tiles), dim=-1) hidden = self.proj(hidden) return hidden, state class Recurrent(Encoder): def __init__(self, config): '''Recurrent baseline model''' super().__init__(config) self.lstm = policy.BatchFirstLSTM( input_size=config.HIDDEN, hidden_size=config.HIDDEN) #Note: seemingly redundant transposes are required to convert between #Pytorch (seq_len, batch, hidden) <-> RLlib (batch, seq_len, hidden) def hidden(self, obs, state, lens): #Attentional input preprocessor and batching lens = lens.cpu() if type(lens) == torch.Tensor else lens hidden, _ = super().hidden(obs) config = self.config h, c = state TB = hidden.size(0) #Padded batch of size (seq x batch) B = len(lens) #Sequence fragment time length T = TB // B #Trajectory batch size H = config.HIDDEN #Hidden state size #Pack (batch x seq, hidden) -> (batch, seq, hidden) hidden = rnn.pack_padded_sequence( input=hidden.view(B, T, H), lengths=lens, enforce_sorted=False, batch_first=True) #Main recurrent network oldHidden = hidden hidden, state = self.lstm(hidden, state) newHidden = hidden #Unpack (batch, seq, hidden) -> (batch x seq, hidden) hidden, _ = rnn.pad_packed_sequence( sequence=hidden, batch_first=True, total_length=T) return hidden.reshape(TB, H), state ############################################################################### ### RLlib Policy, Evaluator, Trainer class RLlibPolicy(RecurrentNetwork, nn.Module): '''Wrapper class for using our baseline models with RLlib''' def __init__(self, *args, **kwargs): self.config = kwargs.pop('config') super().__init__(*args, **kwargs) nn.Module.__init__(self) self.space = actionSpace(self.config).spaces self.model = Recurrent(self.config) #Initial hidden state for RLlib Trainer def get_initial_state(self): return [self.model.valueF.weight.new(1, self.config.HIDDEN).zero_(), self.model.valueF.weight.new(1, self.config.HIDDEN).zero_()] def forward(self, input_dict, state, seq_lens): logitDict, state = self.model(input_dict['obs'], state, seq_lens) logits = [] #Flatten structured logits for RLlib for atnKey, atn in sorted(self.space.items()): for argKey, arg in sorted(atn.spaces.items()): logits.append(logitDict[atnKey][argKey]) return torch.cat(logits, dim=1), state def value_function(self): return self.model.value def attention(self): return self.model.attn ############################################################################### ### RLlib Wrappers: Env, Overlays class RLlibEnv(Env, rllib.MultiAgentEnv): def __init__(self, config): self.config = config['config'] super().__init__(self.config) def reward(self, ent): config = self.config ACHIEVEMENT = config.REWARD_ACHIEVEMENT SCALE = config.ACHIEVEMENT_SCALE COOPERATIVE = config.COOPERATIVE individual = 0 if ent.entID in self.realm.players else -1 team = 0 if ACHIEVEMENT: individual += SCALE*ent.achievements.update(self.realm, ent, dry=True) if COOPERATIVE: nDead = len([p for p in self.dead.values() if p.population == ent.pop]) team = -nDead / config.TEAM_SIZE if COOPERATIVE and ACHIEVEMENT: pre, post = [], [] for p in self.realm.players.corporeal.values(): if p.population == ent.pop: pre.append(p.achievements.score(aggregate=False)) post.append(p.achievements.update( self.realm, ent, aggregate=False, dry=True)) pre = np.array(pre).max(0) post = np.array(post).max(0) team += SCALE*(post - pre).sum() ent.achievements.update(self.realm, ent) alpha = config.TEAM_SPIRIT return alpha*team + (1.0-alpha)*individual def step(self, decisions, preprocess=None, omitDead=False): preprocess = {entID for entID in decisions} obs, rewards, dones, infos = super().step(decisions, preprocess, omitDead) config = self.config dones['__all__'] = False test = config.EVALUATE or config.RENDER if config.EVALUATE: horizon = config.EVALUATION_HORIZON else: horizon = config.TRAIN_HORIZON population = len(self.realm.players) == 0 hit_horizon = self.realm.tick >= horizon if not config.RENDER and (hit_horizon or population): dones['__all__'] = True return obs, rewards, dones, infos def observationSpace(config): obs = FlexDict(defaultdict(FlexDict)) for entity in sorted(Stimulus.values()): nRows = entity.N(config) nContinuous = 0 nDiscrete = 0 for _, attr in entity: if attr.DISCRETE: nDiscrete += 1 if attr.CONTINUOUS: nContinuous += 1 obs[entity.__name__]['Continuous'] = gym.spaces.Box( low=-2**20, high=2**20, shape=(nRows, nContinuous), dtype=DataType.CONTINUOUS) obs[entity.__name__]['Discrete'] = gym.spaces.Box( low=0, high=4096, shape=(nRows, nDiscrete), dtype=DataType.DISCRETE) obs['Entity']['N'] = gym.spaces.Box( low=0, high=config.N_AGENT_OBS, shape=(1,), dtype=DataType.DISCRETE) return obs def actionSpace(config): atns = FlexDict(defaultdict(FlexDict)) for atn in sorted(Action.edges): for arg in sorted(atn.edges): n = arg.N(config) atns[atn][arg] = gym.spaces.Discrete(n) return atns class RLlibOverlayRegistry(OverlayRegistry): '''Host class for RLlib Map overlays''' def __init__(self, realm): super().__init__(realm.config, realm) self.overlays['values'] = Values self.overlays['attention'] = Attention self.overlays['tileValues'] = TileValues self.overlays['entityValues'] = EntityValues class RLlibOverlay(Overlay): '''RLlib Map overlay wrapper''' def __init__(self, config, realm, trainer, model): super().__init__(config, realm) self.trainer = trainer self.model = model class Attention(RLlibOverlay): def register(self, obs): '''Computes local attentional maps with respect to each agent''' tiles = self.realm.realm.map.tiles players = self.realm.realm.players attentions = defaultdict(list) for idx, playerID in enumerate(obs): if playerID not in players: continue player = players[playerID] r, c = player.pos rad = self.config.NSTIM obTiles = self.realm.realm.map.tiles[r-rad:r+rad+1, c-rad:c+rad+1].ravel() for tile, a in zip(obTiles, self.model.attention()[idx]): attentions[tile].append(float(a)) sz = self.config.TERRAIN_SIZE data = np.zeros((sz, sz)) for r, tList in enumerate(tiles): for c, tile in enumerate(tList): if tile not in attentions: continue data[r, c] = np.mean(attentions[tile]) colorized = overlay.twoTone(data) self.realm.register(colorized) class Values(RLlibOverlay): def update(self, obs): '''Computes a local value function by painting tiles as agents walk over them. This is fast and does not require additional network forward passes''' players = self.realm.realm.players for idx, playerID in enumerate(obs): if playerID not in players: continue r, c = players[playerID].base.pos self.values[r, c] = float(self.model.value_function()[idx]) def register(self, obs): colorized = overlay.twoTone(self.values[:, :]) self.realm.register(colorized) def zeroOb(ob, key): for k in ob[key]: ob[key][k] *= 0 class GlobalValues(RLlibOverlay): '''Abstract base for global value functions''' def init(self, zeroKey): if self.trainer is None: return print('Computing value map...') model = self.trainer.get_policy('policy_0').model obs, ents = self.realm.dense() values = 0 * self.values #Compute actions to populate model value function BATCH_SIZE = 128 batch = {} final = list(obs.keys())[-1] for agentID in tqdm(obs): ob = obs[agentID] batch[agentID] = ob zeroOb(ob, zeroKey) if len(batch) == BATCH_SIZE or agentID == final: self.trainer.compute_actions(batch, state={}, policy_id='policy_0') for idx, agentID in enumerate(batch): r, c = ents[agentID].base.pos values[r, c] = float(self.model.value_function()[idx]) batch = {} print('Value map computed') self.colorized = overlay.twoTone(values) def register(self, obs): print('Computing Global Values. This requires one NN pass per tile') self.init() self.realm.register(self.colorized) class TileValues(GlobalValues): def init(self, zeroKey='Entity'): '''Compute a global value function map excluding other agents. This requires a forward pass for every tile and will be slow on large maps''' super().init(zeroKey) class EntityValues(GlobalValues): def init(self, zeroKey='Tile'): '''Compute a global value function map excluding tiles. This requires a forward pass for every tile and will be slow on large maps''' super().init(zeroKey) class RLlibTrainer(ppo.PPOTrainer): def __init__(self, config, env=None, logger_creator=None): super().__init__(config, env, logger_creator) self.env_config = config['env_config']['config'] #1/sqrt(2)=76% win chance within beta, 95% win chance vs 3*beta=100 SR trueskill.setup(mu=1000, sigma=2*100/3, beta=100/3, tau=2/3, draw_probability=0) self.ratings = [{agent.__name__: trueskill.Rating(mu=1000, sigma=2*100/3)} for agent in self.env_config.EVAL_AGENTS] self.reset_scripted() def reset_scripted(self): for rating_dict in self.ratings: for agent, rating in rating_dict.items(): if agent == 'Combat': rating_dict[agent] = trueskill.Rating(mu=1500, sigma=1) def post_mean(self, stats): for key, vals in stats.items(): if type(vals) == list: stats[key] = np.mean(vals) def train(self): stats = super().train() self.post_mean(stats['custom_metrics']) return stats def evaluate(self): stat_dict = super().evaluate() stats = stat_dict['evaluation']['custom_metrics'] ranks = {agent.__name__: -1 for agent in self.env_config.EVAL_AGENTS} for key in list(stats.keys()): if key.startswith('Rank_'): stat = stats[key] del stats[key] agent = key[5:] ranks[agent] = stat ranks = list(ranks.values()) nEnvs = len(ranks[0]) #Once RLlib adds better custom metric support, #there should be a cleaner way to divide episodes into blocks for i in range(nEnvs): env_ranks = [e[i] for e in ranks] self.ratings = trueskill.rate(self.ratings, env_ranks) self.reset_scripted() for rating in self.ratings: key = 'SR_{}'.format(list(rating.keys())[0]) val = list(rating.values())[0] stats[key] = val.mu return stat_dict ############################################################################### ### Logging class RLlibLogCallbacks(DefaultCallbacks): def on_episode_end(self, *, worker, base_env, policies, episode, **kwargs): assert len(base_env.envs) == 1, 'One env per worker' env = base_env.envs[0] logs = env.terminal() for key, vals in logs['Stats'].items(): episode.custom_metrics[key] = np.mean(vals) if not env.config.EVALUATE: return agents = defaultdict(list) stats = logs['Stats'] policy_ids = stats['PolicyID'] scores = stats['Achievement'] invMap = {agent.policyID: agent for agent in env.config.AGENTS} for policyID, score in zip(policy_ids, scores): policy = invMap[policyID] agents[policy].append(score) for agent in agents: agents[agent] = np.mean(agents[agent]) policies = list(agents.keys()) scores = list(agents.values()) idxs = np.argsort(-np.array(scores)) for rank, idx in enumerate(idxs): key = 'Rank_{}'.format(policies[idx].__name__) episode.custom_metrics[key] = rank
[ "trueskill.setup", "torch.nn.Embedding", "gym.spaces.Discrete", "torch.cat", "collections.defaultdict", "neural_mmo.forge.ethyr.torch.policy.attention.DotReluBlock", "torch.nn.ModuleDict", "numpy.mean", "torch.nn.utils.rnn.pad_packed_sequence", "torch.arange", "neural_mmo.forge.blade.io.stimulus.static.Stimulus.values", "neural_mmo.forge.ethyr.torch.policy.SelfAttention", "torch.Tensor", "torch.nn.Linear", "trueskill.Rating", "trueskill.rate", "tqdm.tqdm", "torch.norm", "torch.nn.Conv2d", "torch.nn.Module.__init__", "torch.nn.MaxPool2d", "numpy.zeros", "numpy.array", "gym.spaces.Box", "neural_mmo.forge.ethyr.torch.policy.BatchFirstLSTM", "neural_mmo.forge.blade.lib.overlay.twoTone" ]
[((13444, 13532), 'gym.spaces.Box', 'gym.spaces.Box', ([], {'low': '(0)', 'high': 'config.N_AGENT_OBS', 'shape': '(1,)', 'dtype': 'DataType.DISCRETE'}), '(low=0, high=config.N_AGENT_OBS, shape=(1,), dtype=DataType.\n DISCRETE)\n', (13458, 13532), False, 'import gym\n'), ((1606, 1621), 'torch.nn.ModuleDict', 'nn.ModuleDict', ([], {}), '()\n', (1619, 1621), False, 'from torch import nn\n'), ((1646, 1661), 'torch.nn.ModuleDict', 'nn.ModuleDict', ([], {}), '()\n', (1659, 1661), False, 'from torch import nn\n'), ((2123, 2179), 'torch.Tensor', 'torch.Tensor', (['[1.0, 0.0, 0.02, 0.02, 0.0, 0.0, 0.0, 0.0]'], {}), '([1.0, 0.0, 0.02, 0.02, 0.0, 0.0, 0.0, 0.0])\n', (2135, 2179), False, 'import torch\n'), ((2204, 2321), 'torch.Tensor', 'torch.Tensor', (['[1.0, 0.0, 0.0, 0.05, 0.0, 0.02, 0.02, 0.1, 0.01, 0.1, 0.1, 0.1, 0.3, 0.0, \n 0, 0, 0, 0, 0, 0, 0, 0]'], {}), '([1.0, 0.0, 0.0, 0.05, 0.0, 0.02, 0.02, 0.1, 0.01, 0.1, 0.1, \n 0.1, 0.3, 0.0, 0, 0, 0, 0, 0, 0, 0, 0])\n', (2216, 2321), False, 'import torch\n'), ((3709, 3739), 'torch.nn.Embedding', 'nn.Embedding', (['Action.n', 'self.h'], {}), '(Action.n, self.h)\n', (3721, 3739), False, 'from torch import nn\n'), ((4299, 4316), 'collections.defaultdict', 'defaultdict', (['dict'], {}), '(dict)\n', (4310, 4316), False, 'from collections import defaultdict\n'), ((5033, 5058), 'neural_mmo.forge.ethyr.torch.policy.attention.DotReluBlock', 'attention.DotReluBlock', (['h'], {}), '(h)\n', (5055, 5058), False, 'from neural_mmo.forge.ethyr.torch.policy import attention\n'), ((5700, 5727), 'torch.nn.Linear', 'nn.Linear', (['config.HIDDEN', '(1)'], {}), '(config.HIDDEN, 1)\n', (5709, 5727), False, 'from torch import nn\n'), ((6833, 6852), 'torch.nn.Linear', 'nn.Linear', (['(2 * h)', 'h'], {}), '(2 * h, h)\n', (6842, 6852), False, 'from torch import nn\n'), ((6871, 6889), 'torch.nn.Conv2d', 'nn.Conv2d', (['h', 'h', '(3)'], {}), '(h, h, 3)\n', (6880, 6889), False, 'from torch import nn\n'), ((6910, 6925), 'torch.nn.MaxPool2d', 'nn.MaxPool2d', (['(2)'], {}), '(2)\n', (6922, 6925), False, 'from torch import nn\n'), ((6946, 6969), 'torch.nn.Linear', 'nn.Linear', (['(h * 6 * 6)', 'h'], {}), '(h * 6 * 6, h)\n', (6955, 6969), False, 'from torch import nn\n'), ((6987, 7006), 'torch.nn.Linear', 'nn.Linear', (['(2 * h)', 'h'], {}), '(2 * h, h)\n', (6996, 7006), False, 'from torch import nn\n'), ((7025, 7060), 'neural_mmo.forge.ethyr.torch.policy.SelfAttention', 'policy.SelfAttention', (['self.embed', 'h'], {}), '(self.embed, h)\n', (7045, 7060), False, 'from neural_mmo.forge.ethyr.torch import policy\n'), ((7251, 7289), 'torch.cat', 'torch.cat', (['(selfEmb, agentEmb)'], {'dim': '(-1)'}), '((selfEmb, agentEmb), dim=-1)\n', (7260, 7289), False, 'import torch\n'), ((7482, 7512), 'torch.norm', 'torch.norm', (['tiles'], {'p': '(2)', 'dim': '(-1)'}), '(tiles, p=2, dim=-1)\n', (7492, 7512), False, 'import torch\n'), ((7847, 7881), 'torch.cat', 'torch.cat', (['(agents, tiles)'], {'dim': '(-1)'}), '((agents, tiles), dim=-1)\n', (7856, 7881), False, 'import torch\n'), ((8088, 8162), 'neural_mmo.forge.ethyr.torch.policy.BatchFirstLSTM', 'policy.BatchFirstLSTM', ([], {'input_size': 'config.HIDDEN', 'hidden_size': 'config.HIDDEN'}), '(input_size=config.HIDDEN, hidden_size=config.HIDDEN)\n', (8109, 8162), False, 'from neural_mmo.forge.ethyr.torch import policy\n'), ((9295, 9369), 'torch.nn.utils.rnn.pad_packed_sequence', 'rnn.pad_packed_sequence', ([], {'sequence': 'hidden', 'batch_first': '(True)', 'total_length': 'T'}), '(sequence=hidden, batch_first=True, total_length=T)\n', (9318, 9369), False, 'from torch.nn.utils import rnn\n'), ((9846, 9870), 'torch.nn.Module.__init__', 'nn.Module.__init__', (['self'], {}), '(self)\n', (9864, 9870), False, 'from torch import nn\n'), ((12811, 12832), 'collections.defaultdict', 'defaultdict', (['FlexDict'], {}), '(FlexDict)\n', (12822, 12832), False, 'from collections import defaultdict\n'), ((12858, 12875), 'neural_mmo.forge.blade.io.stimulus.static.Stimulus.values', 'Stimulus.values', ([], {}), '()\n', (12873, 12875), False, 'from neural_mmo.forge.blade.io.stimulus.static import Stimulus\n'), ((13145, 13246), 'gym.spaces.Box', 'gym.spaces.Box', ([], {'low': '(-2 ** 20)', 'high': '(2 ** 20)', 'shape': '(nRows, nContinuous)', 'dtype': 'DataType.CONTINUOUS'}), '(low=-2 ** 20, high=2 ** 20, shape=(nRows, nContinuous),\n dtype=DataType.CONTINUOUS)\n', (13159, 13246), False, 'import gym\n'), ((13308, 13396), 'gym.spaces.Box', 'gym.spaces.Box', ([], {'low': '(0)', 'high': '(4096)', 'shape': '(nRows, nDiscrete)', 'dtype': 'DataType.DISCRETE'}), '(low=0, high=4096, shape=(nRows, nDiscrete), dtype=DataType.\n DISCRETE)\n', (13322, 13396), False, 'import gym\n'), ((13606, 13627), 'collections.defaultdict', 'defaultdict', (['FlexDict'], {}), '(FlexDict)\n', (13617, 13627), False, 'from collections import defaultdict\n'), ((14616, 14633), 'collections.defaultdict', 'defaultdict', (['list'], {}), '(list)\n', (14627, 14633), False, 'from collections import defaultdict\n'), ((15090, 15108), 'numpy.zeros', 'np.zeros', (['(sz, sz)'], {}), '((sz, sz))\n', (15098, 15108), True, 'import numpy as np\n'), ((15324, 15345), 'neural_mmo.forge.blade.lib.overlay.twoTone', 'overlay.twoTone', (['data'], {}), '(data)\n', (15339, 15345), False, 'from neural_mmo.forge.blade.lib import overlay\n'), ((15907, 15941), 'neural_mmo.forge.blade.lib.overlay.twoTone', 'overlay.twoTone', (['self.values[:, :]'], {}), '(self.values[:, :])\n', (15922, 15941), False, 'from neural_mmo.forge.blade.lib import overlay\n'), ((16527, 16536), 'tqdm.tqdm', 'tqdm', (['obs'], {}), '(obs)\n', (16531, 16536), False, 'from tqdm import tqdm\n'), ((17027, 17050), 'neural_mmo.forge.blade.lib.overlay.twoTone', 'overlay.twoTone', (['values'], {}), '(values)\n', (17042, 17050), False, 'from neural_mmo.forge.blade.lib import overlay\n'), ((18002, 18094), 'trueskill.setup', 'trueskill.setup', ([], {'mu': '(1000)', 'sigma': '(2 * 100 / 3)', 'beta': '(100 / 3)', 'tau': '(2 / 3)', 'draw_probability': '(0)'}), '(mu=1000, sigma=2 * 100 / 3, beta=100 / 3, tau=2 / 3,\n draw_probability=0)\n', (18017, 18094), False, 'import trueskill\n'), ((20175, 20192), 'collections.defaultdict', 'defaultdict', (['list'], {}), '(list)\n', (20186, 20192), False, 'from collections import defaultdict\n'), ((1870, 1935), 'torch.nn.Linear', 'nn.Linear', (['((continuous + discrete) * config.HIDDEN)', 'config.HIDDEN'], {}), '((continuous + discrete) * config.HIDDEN, config.HIDDEN)\n', (1879, 1935), False, 'from torch import nn\n'), ((10555, 10579), 'torch.cat', 'torch.cat', (['logits'], {'dim': '(1)'}), '(logits, dim=1)\n', (10564, 10579), False, 'import torch\n'), ((13767, 13789), 'gym.spaces.Discrete', 'gym.spaces.Discrete', (['n'], {}), '(n)\n', (13786, 13789), False, 'import gym\n'), ((19412, 19451), 'trueskill.rate', 'trueskill.rate', (['self.ratings', 'env_ranks'], {}), '(self.ratings, env_ranks)\n', (19426, 19451), False, 'import trueskill\n'), ((20093, 20106), 'numpy.mean', 'np.mean', (['vals'], {}), '(vals)\n', (20100, 20106), True, 'import numpy as np\n'), ((20562, 20584), 'numpy.mean', 'np.mean', (['agents[agent]'], {}), '(agents[agent])\n', (20569, 20584), True, 'import numpy as np\n'), ((15279, 15304), 'numpy.mean', 'np.mean', (['attentions[tile]'], {}), '(attentions[tile])\n', (15286, 15304), True, 'import numpy as np\n'), ((18123, 18167), 'trueskill.Rating', 'trueskill.Rating', ([], {'mu': '(1000)', 'sigma': '(2 * 100 / 3)'}), '(mu=1000, sigma=2 * 100 / 3)\n', (18139, 18167), False, 'import trueskill\n'), ((18603, 18616), 'numpy.mean', 'np.mean', (['vals'], {}), '(vals)\n', (18610, 18616), True, 'import numpy as np\n'), ((20692, 20708), 'numpy.array', 'np.array', (['scores'], {}), '(scores)\n', (20700, 20708), True, 'import numpy as np\n'), ((11860, 11873), 'numpy.array', 'np.array', (['pre'], {}), '(pre)\n', (11868, 11873), True, 'import numpy as np\n'), ((11898, 11912), 'numpy.array', 'np.array', (['post'], {}), '(post)\n', (11906, 11912), True, 'import numpy as np\n'), ((18438, 18472), 'trueskill.Rating', 'trueskill.Rating', ([], {'mu': '(1500)', 'sigma': '(1)'}), '(mu=1500, sigma=1)\n', (18454, 18472), False, 'import trueskill\n'), ((5175, 5200), 'torch.arange', 'torch.arange', (['x.shape[-1]'], {}), '(x.shape[-1])\n', (5187, 5200), False, 'import torch\n')]
#!/usr/bin/python #-*- coding:utf-8 -*- import sys import struct import numpy as np import tensorflow as tf def lrn_f32(): para_int = [] para_float = [] # init the input data and parameters batch = int(np.random.randint(1, high=4, size=1)) in_size_x = int(np.random.randint(16, high=32, size=1)) in_size_y = int(np.random.randint(16, high=32, size=1)) in_channel = int(np.random.randint(15, high=32, size=1)) depth_radius = int(np.random.randint(1, high=6, size=1)) bias = float(np.random.uniform(1, high=5, size=1)) alpha = float(np.random.uniform(1e-5, high=1e-3, size=1)) beta = float(np.random.uniform(0.5, high=1, size=1)) zero_point = int(np.random.randint(0, high=1, size=1)) std = int(np.random.randint(1, high=2, size=1)) src_in = np.random.normal(zero_point, std, (batch, in_size_y, in_size_x, in_channel)) src_in = src_in.astype(np.float32) out_calcu = tf.nn.local_response_normalization(src_in, depth_radius, bias, alpha, beta) with tf.Session() as sess: src_out = sess.run(out_calcu) src_in_nhwc = src_in out_nhwc = src_out src_in_nchw = np.transpose(src_in, [0, 3, 1, 2]) out_nchw = np.transpose(src_out, [0, 3, 1, 2]) src_in_1 = src_in_nchw.flatten() src_out_1 = out_nchw.flatten() total_size = (len(src_in_1) + len(src_out_1)) + 8 para_int.append(total_size) para_int.append(batch) para_int.append(in_channel) para_int.append(in_size_y) para_int.append(in_size_x) para_int.append(depth_radius) para_float.append(bias) para_float.append(alpha) para_float.append(beta) print(para_int) print(para_float) with open("lrn_data_f32.bin", "wb") as fp: data = struct.pack(('%di' % len(para_int)), *para_int) fp.write(data) data = struct.pack(('%df' % len(para_float)), *para_float) fp.write(data) data = struct.pack(('%df' % len(src_in_1)), *src_in_1) fp.write(data) data = struct.pack(('%df' % len(src_out_1)), *src_out_1) fp.write(data) fp.close() return 0 if __name__ == '__main__': lrn_f32() print("end")
[ "numpy.random.uniform", "numpy.transpose", "tensorflow.nn.local_response_normalization", "tensorflow.Session", "numpy.random.randint", "numpy.random.normal" ]
[((819, 895), 'numpy.random.normal', 'np.random.normal', (['zero_point', 'std', '(batch, in_size_y, in_size_x, in_channel)'], {}), '(zero_point, std, (batch, in_size_y, in_size_x, in_channel))\n', (835, 895), True, 'import numpy as np\n'), ((953, 1028), 'tensorflow.nn.local_response_normalization', 'tf.nn.local_response_normalization', (['src_in', 'depth_radius', 'bias', 'alpha', 'beta'], {}), '(src_in, depth_radius, bias, alpha, beta)\n', (987, 1028), True, 'import tensorflow as tf\n'), ((1175, 1209), 'numpy.transpose', 'np.transpose', (['src_in', '[0, 3, 1, 2]'], {}), '(src_in, [0, 3, 1, 2])\n', (1187, 1209), True, 'import numpy as np\n'), ((1225, 1260), 'numpy.transpose', 'np.transpose', (['src_out', '[0, 3, 1, 2]'], {}), '(src_out, [0, 3, 1, 2])\n', (1237, 1260), True, 'import numpy as np\n'), ((226, 262), 'numpy.random.randint', 'np.random.randint', (['(1)'], {'high': '(4)', 'size': '(1)'}), '(1, high=4, size=1)\n', (243, 262), True, 'import numpy as np\n'), ((285, 323), 'numpy.random.randint', 'np.random.randint', (['(16)'], {'high': '(32)', 'size': '(1)'}), '(16, high=32, size=1)\n', (302, 323), True, 'import numpy as np\n'), ((346, 384), 'numpy.random.randint', 'np.random.randint', (['(16)'], {'high': '(32)', 'size': '(1)'}), '(16, high=32, size=1)\n', (363, 384), True, 'import numpy as np\n'), ((407, 445), 'numpy.random.randint', 'np.random.randint', (['(15)'], {'high': '(32)', 'size': '(1)'}), '(15, high=32, size=1)\n', (424, 445), True, 'import numpy as np\n'), ((471, 507), 'numpy.random.randint', 'np.random.randint', (['(1)'], {'high': '(6)', 'size': '(1)'}), '(1, high=6, size=1)\n', (488, 507), True, 'import numpy as np\n'), ((526, 562), 'numpy.random.uniform', 'np.random.uniform', (['(1)'], {'high': '(5)', 'size': '(1)'}), '(1, high=5, size=1)\n', (543, 562), True, 'import numpy as np\n'), ((582, 626), 'numpy.random.uniform', 'np.random.uniform', (['(1e-05)'], {'high': '(0.001)', 'size': '(1)'}), '(1e-05, high=0.001, size=1)\n', (599, 626), True, 'import numpy as np\n'), ((643, 681), 'numpy.random.uniform', 'np.random.uniform', (['(0.5)'], {'high': '(1)', 'size': '(1)'}), '(0.5, high=1, size=1)\n', (660, 681), True, 'import numpy as np\n'), ((705, 741), 'numpy.random.randint', 'np.random.randint', (['(0)'], {'high': '(1)', 'size': '(1)'}), '(0, high=1, size=1)\n', (722, 741), True, 'import numpy as np\n'), ((764, 800), 'numpy.random.randint', 'np.random.randint', (['(1)'], {'high': '(2)', 'size': '(1)'}), '(1, high=2, size=1)\n', (781, 800), True, 'import numpy as np\n'), ((1044, 1056), 'tensorflow.Session', 'tf.Session', ([], {}), '()\n', (1054, 1056), True, 'import tensorflow as tf\n')]
from sklearn.model_selection import cross_val_score from sklearn.model_selection import train_test_split import pandas as pd import numpy as np import matplotlib.pyplot as plt from lightgbm import LGBMRegressor pd.options.display.max_columns = 50 pd.options.display.width = 1000 training_data = pd.read_csv('training_data/covid19_measure_assessment_dataset.csv') X = training_data[training_data.columns[0:-1]] y = training_data[training_data.columns[-1]] X.drop(columns=['Obligatory medical tests not related to covid-19'], inplace=True) print(y.describe(), X.day_zero.describe()) X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=True, random_state=0, test_size=0.2) max_depth = -1 num_leaves = 150 learning_rate = 0.01 boosting_type, subsample, subsample_freq = 'rf', 0.8, 2 class_weight = None objective = 'rmse' colsample_bytree = 0.8 colsample_bynode = 0.8 min_child_samples = 180 max_bin = 256 Regressor = LGBMRegressor(n_estimators=100000, boosting_type=boosting_type, learning_rate=learning_rate, random_state=0, subsample=subsample, subsample_freq=subsample_freq, colsample_bytree=colsample_bytree, colsample_bynode=colsample_bynode, min_child_samples=min_child_samples, max_depth=max_depth, num_leaves=num_leaves, class_weight=class_weight, max_bin=max_bin, importance_type='split', objective=objective) Regressor.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)], early_stopping_rounds=1000, ) best_iteration = Regressor.best_iteration_ print(Regressor.get_params()) Regressor = LGBMRegressor(n_estimators=best_iteration, boosting_type=boosting_type, learning_rate=learning_rate, random_state=0, class_weight=class_weight, min_child_samples=min_child_samples, colsample_bynode=colsample_bynode, colsample_bytree=colsample_bytree, subsample=subsample, subsample_freq=subsample_freq, max_bin=max_bin, max_depth=max_depth, num_leaves=num_leaves, top_k=1000, tree_learner='voting', importance_type='split', objective=objective) scores = cross_val_score(Regressor, X, y, cv=5, scoring='neg_mean_squared_error') print('Cross Validation Results: {} +- Std.Dev. {} '.format(scores.mean(), scores.std())) Regressor.fit(X, y) print("Iterations used: ", Regressor.n_estimators) for c, i in zip(X.columns, Regressor.feature_importances_): print(c, i) fig, ax = plt.subplots(1, 1, figsize=(10, 5), facecolor=(0.9, 0.9, 0.9)) ax.set_title('Model predictions per solely activated measure') ax.set_facecolor((0.8, 0.8, 0.8)) ax.set_xlabel('Days since day zero') ax.set_ylabel('Exponent % increase since day zero') days = X.day_zero.max() period = np.arange(X.day_zero.min(), days, 1) def prediction(measure_intensity=0, days=days, temp=X.avg_temp.mean(), population=X.population.mean(), built=X.built_area.mean(), density=X.pop_density.mean(), measure_index=None, clf=Regressor, train_data=X): days = np.arange(X.day_zero.min(), days, 1) array = np.zeros((days.size, train_data.columns.size), 'float64') array[:, 0], array[:, 1], array[:, 2], array[:, 3], array[:, 4] = days, temp, population, built, density if measure_index: array[:, 4+measure_index] = 1 else: array[:, 5:] = measure_intensity array = pd.DataFrame(array, columns=X.columns) predictions = clf.predict(array, num_iteration=best_iteration, pred_contrib=False) predictions = predictions.cumsum() return predictions intensity_0, intensity_0_1, intensity_1 = prediction(0), prediction(0.1), prediction(1) measure_assessment = [prediction(days, measure_index=i) for i in range(1, 35)] ax.plot(period, intensity_0, c=(0.1, 0.1, 0.1), lw=6, alpha=0.9, label='All measures: Intensity 0') ax.plot(period, intensity_0_1, c=(0.3, 0.3, 0.3), lw=6, alpha=0.9, label='All measures: Intensity 0.1') ax.plot(period, intensity_1, c=(0.5, 0.5, 0.5), lw=6, alpha=0.9, label='All measures: Intensity 1') for name, measure in zip(X.columns[5:], measure_assessment): rc = np.random.random(3,) ax.plot(period, measure, c=rc, ls='--', lw=2, label=name) ax.fill_between(period, measure-scores.mean()*(period-period[0]+1), measure+scores.mean()*(period-period[0]+1), color=(*rc, 0.2)) ax.set_xlim(left=0, right=days) ax.set_ylim(bottom=0, top=intensity_0.max()) ax.yaxis.set_ticklabels(['{}%'.format(int(x*100)) for x in ax.get_yticks()], rotation=90) ax.grid(axis='both', linestyle=':', color=(1, 1, 1, 0.3)) ax.legend(bbox_to_anchor=(1, 1.01), fontsize=7.5, labelspacing=0.6, fancybox=True, title='Legend') plt.subplots_adjust(wspace=0, hspace=0, right=0.7, left=0.05) Regressor.booster_.save_model('Model/covid19_measure_assessment_model.txt', num_iteration=best_iteration) results = pd.DataFrame(((round(((measure[-1] - intensity_0[-1]) / intensity_0[-1])*100, 1)) for measure in measure_assessment), index=X.columns[5:], columns=['Spread % decrease in {} days'.format(int(days))]) results['RMSE'] = '+- {}%'.format(abs(round((days*scores.mean()/intensity_0[-1])*100, 1))) results['Feature importance (splits)'] = Regressor.feature_importances_[5:] results['No. of non-zero values (dataset)'] = X[X > 0].count()[5:] results.sort_values('Spread % decrease in {} days'.format(int(days)), inplace=True) results[results.columns[0]] = results[results.columns[0]].apply(lambda x: '{}%'.format(x)) print(results) plt.show()
[ "pandas.DataFrame", "matplotlib.pyplot.show", "pandas.read_csv", "sklearn.model_selection.train_test_split", "sklearn.model_selection.cross_val_score", "numpy.zeros", "numpy.random.random", "matplotlib.pyplot.subplots_adjust", "lightgbm.LGBMRegressor", "matplotlib.pyplot.subplots" ]
[((296, 363), 'pandas.read_csv', 'pd.read_csv', (['"""training_data/covid19_measure_assessment_dataset.csv"""'], {}), "('training_data/covid19_measure_assessment_dataset.csv')\n", (307, 363), True, 'import pandas as pd\n'), ((622, 689), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'shuffle': '(True)', 'random_state': '(0)', 'test_size': '(0.2)'}), '(X, y, shuffle=True, random_state=0, test_size=0.2)\n', (638, 689), False, 'from sklearn.model_selection import train_test_split\n'), ((937, 1358), 'lightgbm.LGBMRegressor', 'LGBMRegressor', ([], {'n_estimators': '(100000)', 'boosting_type': 'boosting_type', 'learning_rate': 'learning_rate', 'random_state': '(0)', 'subsample': 'subsample', 'subsample_freq': 'subsample_freq', 'colsample_bytree': 'colsample_bytree', 'colsample_bynode': 'colsample_bynode', 'min_child_samples': 'min_child_samples', 'max_depth': 'max_depth', 'num_leaves': 'num_leaves', 'class_weight': 'class_weight', 'max_bin': 'max_bin', 'importance_type': '"""split"""', 'objective': 'objective'}), "(n_estimators=100000, boosting_type=boosting_type,\n learning_rate=learning_rate, random_state=0, subsample=subsample,\n subsample_freq=subsample_freq, colsample_bytree=colsample_bytree,\n colsample_bynode=colsample_bynode, min_child_samples=min_child_samples,\n max_depth=max_depth, num_leaves=num_leaves, class_weight=class_weight,\n max_bin=max_bin, importance_type='split', objective=objective)\n", (950, 1358), False, 'from lightgbm import LGBMRegressor\n'), ((1657, 2127), 'lightgbm.LGBMRegressor', 'LGBMRegressor', ([], {'n_estimators': 'best_iteration', 'boosting_type': 'boosting_type', 'learning_rate': 'learning_rate', 'random_state': '(0)', 'class_weight': 'class_weight', 'min_child_samples': 'min_child_samples', 'colsample_bynode': 'colsample_bynode', 'colsample_bytree': 'colsample_bytree', 'subsample': 'subsample', 'subsample_freq': 'subsample_freq', 'max_bin': 'max_bin', 'max_depth': 'max_depth', 'num_leaves': 'num_leaves', 'top_k': '(1000)', 'tree_learner': '"""voting"""', 'importance_type': '"""split"""', 'objective': 'objective'}), "(n_estimators=best_iteration, boosting_type=boosting_type,\n learning_rate=learning_rate, random_state=0, class_weight=class_weight,\n min_child_samples=min_child_samples, colsample_bynode=colsample_bynode,\n colsample_bytree=colsample_bytree, subsample=subsample, subsample_freq=\n subsample_freq, max_bin=max_bin, max_depth=max_depth, num_leaves=\n num_leaves, top_k=1000, tree_learner='voting', importance_type='split',\n objective=objective)\n", (1670, 2127), False, 'from lightgbm import LGBMRegressor\n'), ((2243, 2315), 'sklearn.model_selection.cross_val_score', 'cross_val_score', (['Regressor', 'X', 'y'], {'cv': '(5)', 'scoring': '"""neg_mean_squared_error"""'}), "(Regressor, X, y, cv=5, scoring='neg_mean_squared_error')\n", (2258, 2315), False, 'from sklearn.model_selection import cross_val_score\n'), ((2594, 2656), 'matplotlib.pyplot.subplots', 'plt.subplots', (['(1)', '(1)'], {'figsize': '(10, 5)', 'facecolor': '(0.9, 0.9, 0.9)'}), '(1, 1, figsize=(10, 5), facecolor=(0.9, 0.9, 0.9))\n', (2606, 2656), True, 'import matplotlib.pyplot as plt\n'), ((4798, 4859), 'matplotlib.pyplot.subplots_adjust', 'plt.subplots_adjust', ([], {'wspace': '(0)', 'hspace': '(0)', 'right': '(0.7)', 'left': '(0.05)'}), '(wspace=0, hspace=0, right=0.7, left=0.05)\n', (4817, 4859), True, 'import matplotlib.pyplot as plt\n'), ((5676, 5686), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (5684, 5686), True, 'import matplotlib.pyplot as plt\n'), ((3203, 3260), 'numpy.zeros', 'np.zeros', (['(days.size, train_data.columns.size)', '"""float64"""'], {}), "((days.size, train_data.columns.size), 'float64')\n", (3211, 3260), True, 'import numpy as np\n'), ((3498, 3536), 'pandas.DataFrame', 'pd.DataFrame', (['array'], {'columns': 'X.columns'}), '(array, columns=X.columns)\n', (3510, 3536), True, 'import pandas as pd\n'), ((4234, 4253), 'numpy.random.random', 'np.random.random', (['(3)'], {}), '(3)\n', (4250, 4253), True, 'import numpy as np\n')]
#!/usr/bin/env python3 import h5py import numpy as np import pandas as pd import sys import os mtd_prefix = """ { "data_type": "matrix", "value_type": "double", """ mtd_suffix = """ "format": "csv", "header": false, "sep": "," } """ if len(sys.argv) < 3: print("usage: " + sys.argv[0] + " <in-file> <dataset> [NCHW] [nomean]") sys.exit() convert_to_nchw = False if "NCHW" in sys.argv: convert_to_nchw = True remove_mean = False if "nomean" in sys.argv: remove_mean = True input_file = sys.argv[1] dataset = sys.argv[2] out_file = input_file[:-3] + "_" + dataset if dataset != "label": if convert_to_nchw: out_file += "_NCHW" else: out_file += "NHWC" if remove_mean: out_file += "_nomean" out_file += ".csv" fid = h5py.File(input_file,'r') print('Datasets in \''+input_file+'\':') print(fid.keys()) print('Loading ' + dataset + '...') ds = np.array(fid[dataset]) print(ds.shape) if remove_mean: ds_mean = np.mean(ds, axis=0) ds -= ds_mean if ds.ndim > 2: if convert_to_nchw: print("converting to NCHW") ds = np.moveaxis(ds, -1, 1) ds = ds.reshape(ds.shape[0], ds.shape[1] * ds.shape[2] * ds.shape[3]) print("reshaped to " + str(ds.shape)) else: print("ndim: " + str(ds.ndim)) df = pd.DataFrame(ds) df.to_csv(out_file, index=False, header=False) meta= str(ds.shape[0]) + "," + str(ds.shape[1]) + ",1,f32,\n" mtd = mtd_prefix + " \"rows\": " + str(ds.shape[0]) + ",\n \"cols\": " + str(ds.shape[1]) + "," + mtd_suffix with open(out_file + ".meta", 'w') as f: f.write(meta) with open(out_file + ".mtd", 'w') as f: f.write(mtd)
[ "pandas.DataFrame", "h5py.File", "numpy.moveaxis", "numpy.mean", "numpy.array", "sys.exit" ]
[((810, 836), 'h5py.File', 'h5py.File', (['input_file', '"""r"""'], {}), "(input_file, 'r')\n", (819, 836), False, 'import h5py\n'), ((937, 959), 'numpy.array', 'np.array', (['fid[dataset]'], {}), '(fid[dataset])\n', (945, 959), True, 'import numpy as np\n'), ((1328, 1344), 'pandas.DataFrame', 'pd.DataFrame', (['ds'], {}), '(ds)\n', (1340, 1344), True, 'import pandas as pd\n'), ((365, 375), 'sys.exit', 'sys.exit', ([], {}), '()\n', (373, 375), False, 'import sys\n'), ((1006, 1025), 'numpy.mean', 'np.mean', (['ds'], {'axis': '(0)'}), '(ds, axis=0)\n', (1013, 1025), True, 'import numpy as np\n'), ((1138, 1160), 'numpy.moveaxis', 'np.moveaxis', (['ds', '(-1)', '(1)'], {}), '(ds, -1, 1)\n', (1149, 1160), True, 'import numpy as np\n')]
import inspect from typing import Union, Any, Callable, Iterable, Tuple, Sequence import torch import numpy as np def bifurcate(x: Iterable, lhs: Callable[[Any], bool]) -> Tuple[list, list]: """ Split an iterable into two lists depending on a condition. :param x: An iterable. :param lhs: A function that takes an element of x; when this returns True, the element is added to the left output, when this returns False, the element is added to the right output. :return: Two lists. """ l, r = [], [] for el in x: if lhs(el): l.append(el) else: r.append(el) return l, r def zpad(x: Any, n: int) -> str: return str(x).rjust(n, "0") def split_flat(tens: torch.Tensor, dim: int, clone: bool = False): if clone: return [tens.select(dim, i).clone() for i in range(tens.shape[dim])] else: assert not tens.requires_grad return [tens.select(dim, i) for i in range(tens.shape[dim])] def identity(x: Any) -> Any: return x def is_slow_grad(tens: torch.Tensor) -> bool: if tens.requires_grad: avoid_funs = {'CopyBackwards', 'SelectBackward'} next_fun = tens.grad_fn.next_functions[0][0] if (tens.grad_fn.__class__.__name__ in avoid_funs) or (next_fun.__class__.__name__ in avoid_funs): return True return False def ragged_cat(tensors: Sequence[torch.Tensor], ragged_dim: int, cat_dim: int = 0) -> torch.Tensor: max_dim_len = max(tensor.shape[ragged_dim] for tensor in tensors) out = [] num_dims = len(tensors[0].shape) for tensor in tensors: this_tens_dim_len = tensor.shape[ragged_dim] shape = list(tensor.shape) assert len(shape) == num_dims shape[ragged_dim] = max_dim_len padded = torch.empty(shape) padded[:] = float('nan') idx = tuple(slice(0, this_tens_dim_len) if i == ragged_dim else slice(None) for i in range(num_dims)) padded[idx] = tensor out.append(padded) return torch.cat(out, cat_dim) def true1d_idx(arr: Union[np.ndarray, torch.Tensor]) -> np.ndarray: if isinstance(arr, torch.Tensor): arr = arr.detach().numpy() arr = arr.astype('bool') if len(arr.shape) > 1: raise ValueError("Expected 1d array.") return np.where(arr)[0] def infer_forward_kwargs(nn: Union[torch.nn.Module, Callable], method_name: 'str' = 'forward') -> Sequence[str]: if hasattr(nn, '_forward_kwargs'): return nn._forward_kwargs method = getattr(nn, method_name, None) try: params = [kwarg for kwarg in inspect.signature(method).parameters if kwarg not in {'self', 'kwargs'}] except TypeError as e: if e.args[0].endswith('None is not a callable object'): params = [] else: raise e if not params: if method_name == '__call__': raise TypeError( f"Unable to infer arguments for {nn}. Make sure the `forward` method uses named keyword-arguments." ) return infer_forward_kwargs(nn, method_name='__call__') if 'args' in params: raise TypeError( f"Unable to infer arguments for {nn}. Make sure it does not use `*args, **kwargs`" ) return params
[ "numpy.where", "torch.empty", "torch.cat", "inspect.signature" ]
[((2036, 2059), 'torch.cat', 'torch.cat', (['out', 'cat_dim'], {}), '(out, cat_dim)\n', (2045, 2059), False, 'import torch\n'), ((1807, 1825), 'torch.empty', 'torch.empty', (['shape'], {}), '(shape)\n', (1818, 1825), False, 'import torch\n'), ((2317, 2330), 'numpy.where', 'np.where', (['arr'], {}), '(arr)\n', (2325, 2330), True, 'import numpy as np\n'), ((2612, 2637), 'inspect.signature', 'inspect.signature', (['method'], {}), '(method)\n', (2629, 2637), False, 'import inspect\n')]
import copy import os import numpy as np from hexrd.config.root import RootConfig from hexrd.config.material import MaterialConfig from hexrd.config.instrument import Instrument as InstrumentConfig from hexrd.ui.create_hedm_instrument import create_hedm_instrument from hexrd.ui.hexrd_config import HexrdConfig from hexrd.ui.utils import is_omega_imageseries def create_indexing_config(): # Creates a hexrd.config class from the indexing configuration # Make a copy to modify indexing_config = copy.deepcopy(HexrdConfig().indexing_config) available_materials = list(HexrdConfig().materials.keys()) selected_material = indexing_config.get('_selected_material') if selected_material not in available_materials: raise Exception(f'Selected material {selected_material} not available') material = HexrdConfig().material(selected_material) omaps = indexing_config['find_orientations']['orientation_maps'] omaps['active_hkls'] = list(range(len(material.planeData.getHKLs()))) # Set the active material on the config tmp = indexing_config.setdefault('material', {}) tmp['active'] = material.name # Create the root config from the indexing config dict config = RootConfig(indexing_config) # Create and set instrument config iconfig = InstrumentConfig(config) iconfig._hedm = create_hedm_instrument() config.instrument = iconfig # Create and set material config mconfig = MaterialConfig(config) mconfig.materials = HexrdConfig().materials config.material = mconfig # Set this so the config won't over-write our tThWidth config.set('material:tth_width', np.degrees(material.planeData.tThWidth)) # Use unaggregated images if possible ims_dict = HexrdConfig().unagg_images if ims_dict is None: # This probably means the image series was never aggregated. # Try using the imageseries dict. ims_dict = HexrdConfig().imageseries_dict if any(not is_omega_imageseries(ims) for ims in ims_dict.values()): # Add an early error that is easier to understand... raise Exception('Omegas not found!') config.image_series = ims_dict validate_config(config) return config def validate_config(config): # Perform any modifications to make sure this is a valid config try: config.working_dir except IOError: # This working directory does not exist. Set it to the cwd. print(f'Warning: {config.get("working_dir")} does not exist.', f'Changing working directory to {os.getcwd()}') config.set('working_dir', os.getcwd()) # Make sure future configs use the new working dir as well... HexrdConfig().indexing_config['working_dir'] = os.getcwd()
[ "hexrd.ui.create_hedm_instrument.create_hedm_instrument", "hexrd.config.material.MaterialConfig", "numpy.degrees", "os.getcwd", "hexrd.ui.utils.is_omega_imageseries", "hexrd.ui.hexrd_config.HexrdConfig", "hexrd.config.root.RootConfig", "hexrd.config.instrument.Instrument" ]
[((1227, 1254), 'hexrd.config.root.RootConfig', 'RootConfig', (['indexing_config'], {}), '(indexing_config)\n', (1237, 1254), False, 'from hexrd.config.root import RootConfig\n'), ((1309, 1333), 'hexrd.config.instrument.Instrument', 'InstrumentConfig', (['config'], {}), '(config)\n', (1325, 1333), True, 'from hexrd.config.instrument import Instrument as InstrumentConfig\n'), ((1354, 1378), 'hexrd.ui.create_hedm_instrument.create_hedm_instrument', 'create_hedm_instrument', ([], {}), '()\n', (1376, 1378), False, 'from hexrd.ui.create_hedm_instrument import create_hedm_instrument\n'), ((1463, 1485), 'hexrd.config.material.MaterialConfig', 'MaterialConfig', (['config'], {}), '(config)\n', (1477, 1485), False, 'from hexrd.config.material import MaterialConfig\n'), ((1510, 1523), 'hexrd.ui.hexrd_config.HexrdConfig', 'HexrdConfig', ([], {}), '()\n', (1521, 1523), False, 'from hexrd.ui.hexrd_config import HexrdConfig\n'), ((1661, 1700), 'numpy.degrees', 'np.degrees', (['material.planeData.tThWidth'], {}), '(material.planeData.tThWidth)\n', (1671, 1700), True, 'import numpy as np\n'), ((1760, 1773), 'hexrd.ui.hexrd_config.HexrdConfig', 'HexrdConfig', ([], {}), '()\n', (1771, 1773), False, 'from hexrd.ui.hexrd_config import HexrdConfig\n'), ((526, 539), 'hexrd.ui.hexrd_config.HexrdConfig', 'HexrdConfig', ([], {}), '()\n', (537, 539), False, 'from hexrd.ui.hexrd_config import HexrdConfig\n'), ((836, 849), 'hexrd.ui.hexrd_config.HexrdConfig', 'HexrdConfig', ([], {}), '()\n', (847, 849), False, 'from hexrd.ui.hexrd_config import HexrdConfig\n'), ((1942, 1955), 'hexrd.ui.hexrd_config.HexrdConfig', 'HexrdConfig', ([], {}), '()\n', (1953, 1955), False, 'from hexrd.ui.hexrd_config import HexrdConfig\n'), ((2765, 2776), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (2774, 2776), False, 'import os\n'), ((1989, 2014), 'hexrd.ui.utils.is_omega_imageseries', 'is_omega_imageseries', (['ims'], {}), '(ims)\n', (2009, 2014), False, 'from hexrd.ui.utils import is_omega_imageseries\n'), ((2626, 2637), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (2635, 2637), False, 'import os\n'), ((588, 601), 'hexrd.ui.hexrd_config.HexrdConfig', 'HexrdConfig', ([], {}), '()\n', (599, 601), False, 'from hexrd.ui.hexrd_config import HexrdConfig\n'), ((2718, 2731), 'hexrd.ui.hexrd_config.HexrdConfig', 'HexrdConfig', ([], {}), '()\n', (2729, 2731), False, 'from hexrd.ui.hexrd_config import HexrdConfig\n'), ((2577, 2588), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (2586, 2588), False, 'import os\n')]
from multiprocessing import Pool import pandas as pd from functools import partial import numpy as np from tqdm import tqdm def inductive_pooling(df, embeddings, G, workers, gamma=1000, dict_node=None, average_embedding=True): if average_embedding: avg_emb = embeddings.mean().values else: avg_emb = None #if __name__ == '__main__': with Pool(workers) as p: r = p.map(partial(inductive_pooling_chunk, embeddings=embeddings, G=G, average_embedding=avg_emb), np.array_split(df, workers)) return r def inductive_pooling_chunk(df, embeddings, G, gamma=1000, average_embedding=None): #Create a container for the new embeddings new_embeddings = dict() for transaction, transaction_row in tqdm(df.iterrows(), total=df.shape[0]): cardholder = transaction_row.CARD_PAN_ID merchant = transaction_row.TERM_MIDUID mutual = False if G.has_node(cardholder) & G.has_node(merchant): mutual_neighbors = list(set(G.neighbors(cardholder)).intersection(set(G.neighbors(merchant)))) mutual_neighbors.sort() if (len(mutual_neighbors) > 0): mutual = True # Use dataframe with TX_ID on index (to speed up retrieval of transaction rows) embeddings_mutual_neighbors = embeddings.loc[mutual_neighbors] # most recent transaction most_recent_embedding_mutual_neighbor = embeddings_mutual_neighbors.iloc[-1] new_embeddings[transaction] = most_recent_embedding_mutual_neighbor if G.has_node(cardholder) & (not mutual): cardholder_neighbors = list(G.neighbors(cardholder)) pooled_embedding = get_pooled_embedding(cardholder_neighbors, embeddings, gamma) new_embeddings[transaction] = pooled_embedding elif G.has_node(merchant) & (not mutual): merchant_neighbors = list(G.neighbors(merchant)) pooled_embedding = get_pooled_embedding(merchant_neighbors, embeddings, gamma) new_embeddings[transaction] = pooled_embedding elif (not mutual): new_embeddings[transaction] = average_embedding return new_embeddings def get_pooled_embedding(neighbors, embeddings, gamma): embeddings_to_pool = embeddings.loc[neighbors] most_recent_embeddings_to_pool = embeddings_to_pool.iloc[-min(gamma, embeddings_to_pool.shape[0]):] pooled_embedding = pd.DataFrame(most_recent_embeddings_to_pool.mean()).transpose().values[0] return pooled_embedding
[ "numpy.array_split", "functools.partial", "multiprocessing.Pool" ]
[((384, 397), 'multiprocessing.Pool', 'Pool', (['workers'], {}), '(workers)\n', (388, 397), False, 'from multiprocessing import Pool\n'), ((422, 513), 'functools.partial', 'partial', (['inductive_pooling_chunk'], {'embeddings': 'embeddings', 'G': 'G', 'average_embedding': 'avg_emb'}), '(inductive_pooling_chunk, embeddings=embeddings, G=G,\n average_embedding=avg_emb)\n', (429, 513), False, 'from functools import partial\n'), ((511, 538), 'numpy.array_split', 'np.array_split', (['df', 'workers'], {}), '(df, workers)\n', (525, 538), True, 'import numpy as np\n')]
import matplotlib.pyplot as plt import numpy as np from numpy import pi import pandas as pd from scripts.volatility_tree import build_volatility_tree from scripts.profiler import profiler i = complex(0, 1) # option parameters T = 1 H_original = 90 # limit K_original = 100.0 # strike r_premia = 10 # annual interest rate # Bates model parameters V0 = 0.01 # initial volatility kappa = 2 # heston parameter, mean reversion theta = 0.01 # heston parameter, long-run variance sigma = omega = 0.2 # heston parameter, volatility of variance. # Omega is used in variance tree, sigma - everywhere else rho = 0.5 # heston parameter #correlation # method parameters N = 10 # number_of_time_steps M = 2**15 # number of points in price grid dx = 1e-3 omega_plus = 1 omega_minus = -1 r = np.log(r_premia/100 + 1) omega = sigma # time-space domain construction x_space = np.linspace(-M * dx / 2, M * dx / 2, num=M, endpoint=False) u_space = np.linspace(-pi / dx, pi / dx, num=M, endpoint=False) du = u_space[1] - u_space[0] first_step_of_return = np.array([elem + V0*rho/sigma for elem in x_space]) original_prices_array = H_original * np.exp(first_step_of_return) delta_t = T/N # making volatilily tree markov_chain = build_volatility_tree(T, V0, kappa, theta, omega, N) V = markov_chain[0] pu_f = markov_chain[1] pd_f = markov_chain[2] f_up = markov_chain[3] f_down = markov_chain[4] rho_hat = np.sqrt(1 - rho**2) q = 1.0/delta_t + r factor = (q*delta_t)**(-1) def G(S, K): """the payoff function of put option. Nothing to do with barrier""" return max(K-S, 0) F_n_plus_1 = np.zeros((len(x_space), len(V[N])), dtype=complex) F_n = np.zeros((len(x_space), len(V[N])), dtype=complex) for j in range(len(x_space)): for k in range(len(V[N])): F_n_plus_1[j, k] = np.array(G(H_original * np.exp(x_space[j]), K_original)) # the global cycle starts here. It iterates over the volatility tree we just constructed, and goes backwards in time # starting from n-1 position # print("Main cycle entered") # when the variance is less than that, is is reasonable to assume it to be zero, which leads to simpler calculations treshold = 1e-6 discount_factor = np.exp(r*delta_t) def psi(xi, gamma=0, sigma=sigma): return (sigma**2/2) * np.power(xi, 2) - 1j*gamma*xi def make_rad_fft(f_x): sign_change_k = np.array([(-1)**k for k in range(0, M)]) sign_change_l = np.array([(-1)**l for l in range(0, M)]) # учитываем порядок хранения sign_change_l = np.fft.fftshift(sign_change_l) f = sign_change_k * f_x f_hat = dx * sign_change_l * np.fft.fft(f) # избегаем особенностей хранения результатов fft, нам они не нужны. return f_hat def make_rad_ifft(f_hat_xi): M = len(f_hat_xi) sign_change_k = np.array([(-1)**k for k in range(0, M)]) sign_change_l = np.array([(-1)**l for l in range(0, M)]) f = (1/dx) * sign_change_k * np.fft.ifft(sign_change_l * f_hat_xi) return f def make_phi_minus(gamma=0, sigma=sigma): def integrand_minus(upsilon_array): """ принимает и возвращает массив длиной в степень двойки, исходя из логики дальнейшего использования """ value = np.log(1 + psi(upsilon_array + 1j * omega_plus, gamma=gamma, sigma=sigma) / q) / (upsilon_array + 1j * omega_plus) ** 2 return value def F_minus_capital(): m_indicator = np.where(x_space >= 0, 1, 0) trimmed_x_space = m_indicator * x_space # чтобы при "сильно отрицательных" x не росла экспонента integral = make_rad_ifft(integrand_minus(u_space)) exponent = np.exp(-trimmed_x_space * omega_plus) return m_indicator * exponent * integral fm = F_minus_capital() F_m_hat = make_rad_fft(fm) def make_phi_minus_array(xi_array): first_term = - 1j * xi_array * (fm[M // 2]) second_term = - xi_array * xi_array * F_m_hat return np.exp(first_term + second_term) mex_symbol_minus = make_phi_minus_array(u_space) return mex_symbol_minus def make_phi_plus(gamma=0, sigma=sigma): def integrand_plus(upsilon_array): """ принимает и возвращает массив длиной в степень двойки, исходя из логики дальнейшего использования """ value = np.log(1 + psi(upsilon_array + 1j * omega_minus, gamma=gamma, sigma=sigma) / q) / (upsilon_array + 1j * omega_minus) ** 2 return value def F_plus_capital(): p_indicator = np.where(x_space <= 0, 1, 0) trimmed_x_space = p_indicator * x_space # чтобы при "сильно отрицательных" x не росла экспонента integral = make_rad_ifft(integrand_plus(u_space)) exponent = np.exp(-trimmed_x_space * omega_plus) return p_indicator * exponent * integral fp = F_plus_capital() F_p_hat = make_rad_fft(fp) def make_phi_plus_array(xi_array): first_term = 1j * xi_array * fp[M // 2] second_term = - xi_array * xi_array * F_p_hat return np.exp(first_term + second_term) mex_symbol_plus = make_phi_plus_array(u_space) return mex_symbol_plus for n in range(len(V[N]) - 2, -1, -1): print(str(n) + " of " + str(len(V[N]) - 2)) with profiler(): for k in range(n+1): # to calculate the binomial expectation one should use Antonino's matrices f_up and f_down # the meaning of the containing integers are as follows - after (n,k) you will be in # either (n+1, k + f_up) or (n+1, k - f_down). We use k_u and k_d shorthands, respectively k_u = k + int(f_up[n][k]) k_d = k + int(f_down[n][k]) # initial condition of a step f_n_plus_1_k_u = np.array([F_n_plus_1[j][k_u] for j in range(len(x_space))]) f_n_plus_1_k_d = np.array([F_n_plus_1[j][k_d] for j in range(len(x_space))]) H_N_k = - (rho / sigma) * V[n, k] # modified barrier local_domain = np.array([x_space[j] + H_N_k for j in range(len(x_space))]) if V[n, k] >= treshold: # set up variance-dependent parameters for a given step sigma_local = rho_hat * np.sqrt(V[n, k]) gamma = r - 0.5 * V[n, k] - rho / sigma * kappa * (theta - V[n, k]) # also local phi_plus_array = make_phi_minus(gamma=gamma, sigma=sigma_local) phi_minus_array = make_phi_plus(gamma=gamma, sigma=sigma_local) indicator = np.where(local_domain >= H_N_k, 1, 0) # factorization calculation f_n_k_u = factor * \ make_rad_ifft(phi_minus_array * make_rad_fft( indicator * make_rad_ifft(phi_plus_array * make_rad_fft(f_n_plus_1_k_u)))) f_n_k_d = factor * \ make_rad_ifft(phi_minus_array * make_rad_fft( indicator * make_rad_ifft(phi_plus_array * make_rad_fft(f_n_plus_1_k_d)))) elif V[n, k] < treshold: f_n_plus_1_k_u = [F_n_plus_1[j][k_u] for j in range(len(x_space))] f_n_k_u = discount_factor * f_n_plus_1_k_u f_n_plus_1_k_d = [F_n_plus_1[j][k_d] for j in range(len(x_space))] f_n_k_d = discount_factor * f_n_plus_1_k_d f_n_k = f_n_k_u * pu_f[n, k] + f_n_k_d * pd_f[n, k] for j in range(len(f_n_k)): # here we try some cutdown magic. The procedure without it returns great bubbles to the right # from the strike. And the more L the greater this bubble grows. # what we are going to do there is to try to cut off all the values on prices greater than, say, # 4 times bigger then the strike # we use S>4K and, therefore, y > ln(4K/H) + (pho/sigma)*V inequality to do this if local_domain[j] < np.log(3.5*K_original/H_original + (rho/sigma) * V[n][k]): F_n[j][k] = f_n_k[j] else: F_n[j][k] = complex(0) # plt.plot(original_prices_array, f_n_plus_1_k_u) # plt.show() # for j in range(len(y)): # tree_to_csv_file(y[j], "../output/routine/price_slices/Y" + str(original_prices_array[j]) + ".csv") # for j in range(len(F)): # tree_to_csv_file(F[j], "../output/routine/answers/F" + str(original_prices_array[j]) + ".csv") answer_total = open("../output/routine/answer_cumul.csv", "w") answers_list = np.array([F_n[j][0] for j in range(len(x_space))]) for elem in list(zip(original_prices_array, answers_list)): answer_total.write(str(elem[0]) + ',') answer_total.write(str(elem[1].real) + ',') # answer_total.write(str(elem[1].imag) + ',') answer_total.write('\n') # for j in range(len(F)): # tree_to_csv_file(F[j], "../output/routine/answers/F" + str(original_prices_array[j]) + ".csv") plt.plot(original_prices_array[(original_prices_array>75) & (original_prices_array<200)], answers_list[(original_prices_array>75) & (original_prices_array<200)]) plt.savefig("../output/figure.png") plt.show() plt.close()
[ "numpy.fft.ifft", "scripts.profiler.profiler", "matplotlib.pyplot.show", "numpy.log", "matplotlib.pyplot.plot", "scripts.volatility_tree.build_volatility_tree", "matplotlib.pyplot.close", "numpy.fft.fft", "numpy.power", "numpy.fft.fftshift", "numpy.array", "numpy.exp", "numpy.linspace", "numpy.where", "matplotlib.pyplot.savefig", "numpy.sqrt" ]
[((819, 845), 'numpy.log', 'np.log', (['(r_premia / 100 + 1)'], {}), '(r_premia / 100 + 1)\n', (825, 845), True, 'import numpy as np\n'), ((906, 965), 'numpy.linspace', 'np.linspace', (['(-M * dx / 2)', '(M * dx / 2)'], {'num': 'M', 'endpoint': '(False)'}), '(-M * dx / 2, M * dx / 2, num=M, endpoint=False)\n', (917, 965), True, 'import numpy as np\n'), ((977, 1030), 'numpy.linspace', 'np.linspace', (['(-pi / dx)', '(pi / dx)'], {'num': 'M', 'endpoint': '(False)'}), '(-pi / dx, pi / dx, num=M, endpoint=False)\n', (988, 1030), True, 'import numpy as np\n'), ((1087, 1144), 'numpy.array', 'np.array', (['[(elem + V0 * rho / sigma) for elem in x_space]'], {}), '([(elem + V0 * rho / sigma) for elem in x_space])\n', (1095, 1144), True, 'import numpy as np\n'), ((1267, 1319), 'scripts.volatility_tree.build_volatility_tree', 'build_volatility_tree', (['T', 'V0', 'kappa', 'theta', 'omega', 'N'], {}), '(T, V0, kappa, theta, omega, N)\n', (1288, 1319), False, 'from scripts.volatility_tree import build_volatility_tree\n'), ((1452, 1473), 'numpy.sqrt', 'np.sqrt', (['(1 - rho ** 2)'], {}), '(1 - rho ** 2)\n', (1459, 1473), True, 'import numpy as np\n'), ((2246, 2265), 'numpy.exp', 'np.exp', (['(r * delta_t)'], {}), '(r * delta_t)\n', (2252, 2265), True, 'import numpy as np\n'), ((9294, 9473), 'matplotlib.pyplot.plot', 'plt.plot', (['original_prices_array[(original_prices_array > 75) & (original_prices_array <\n 200)]', 'answers_list[(original_prices_array > 75) & (original_prices_array < 200)]'], {}), '(original_prices_array[(original_prices_array > 75) & (\n original_prices_array < 200)], answers_list[(original_prices_array > 75\n ) & (original_prices_array < 200)])\n', (9302, 9473), True, 'import matplotlib.pyplot as plt\n'), ((9467, 9502), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""../output/figure.png"""'], {}), "('../output/figure.png')\n", (9478, 9502), True, 'import matplotlib.pyplot as plt\n'), ((9504, 9514), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (9512, 9514), True, 'import matplotlib.pyplot as plt\n'), ((9516, 9527), 'matplotlib.pyplot.close', 'plt.close', ([], {}), '()\n', (9525, 9527), True, 'import matplotlib.pyplot as plt\n'), ((1177, 1205), 'numpy.exp', 'np.exp', (['first_step_of_return'], {}), '(first_step_of_return)\n', (1183, 1205), True, 'import numpy as np\n'), ((2570, 2600), 'numpy.fft.fftshift', 'np.fft.fftshift', (['sign_change_l'], {}), '(sign_change_l)\n', (2585, 2600), True, 'import numpy as np\n'), ((2666, 2679), 'numpy.fft.fft', 'np.fft.fft', (['f'], {}), '(f)\n', (2676, 2679), True, 'import numpy as np\n'), ((2994, 3031), 'numpy.fft.ifft', 'np.fft.ifft', (['(sign_change_l * f_hat_xi)'], {}), '(sign_change_l * f_hat_xi)\n', (3005, 3031), True, 'import numpy as np\n'), ((3481, 3509), 'numpy.where', 'np.where', (['(x_space >= 0)', '(1)', '(0)'], {}), '(x_space >= 0, 1, 0)\n', (3489, 3509), True, 'import numpy as np\n'), ((3697, 3734), 'numpy.exp', 'np.exp', (['(-trimmed_x_space * omega_plus)'], {}), '(-trimmed_x_space * omega_plus)\n', (3703, 3734), True, 'import numpy as np\n'), ((4014, 4046), 'numpy.exp', 'np.exp', (['(first_term + second_term)'], {}), '(first_term + second_term)\n', (4020, 4046), True, 'import numpy as np\n'), ((4566, 4594), 'numpy.where', 'np.where', (['(x_space <= 0)', '(1)', '(0)'], {}), '(x_space <= 0, 1, 0)\n', (4574, 4594), True, 'import numpy as np\n'), ((4781, 4818), 'numpy.exp', 'np.exp', (['(-trimmed_x_space * omega_plus)'], {}), '(-trimmed_x_space * omega_plus)\n', (4787, 4818), True, 'import numpy as np\n'), ((5094, 5126), 'numpy.exp', 'np.exp', (['(first_term + second_term)'], {}), '(first_term + second_term)\n', (5100, 5126), True, 'import numpy as np\n'), ((5310, 5320), 'scripts.profiler.profiler', 'profiler', ([], {}), '()\n', (5318, 5320), False, 'from scripts.profiler import profiler\n'), ((2331, 2346), 'numpy.power', 'np.power', (['xi', '(2)'], {}), '(xi, 2)\n', (2339, 2346), True, 'import numpy as np\n'), ((6586, 6623), 'numpy.where', 'np.where', (['(local_domain >= H_N_k)', '(1)', '(0)'], {}), '(local_domain >= H_N_k, 1, 0)\n', (6594, 6623), True, 'import numpy as np\n'), ((1876, 1894), 'numpy.exp', 'np.exp', (['x_space[j]'], {}), '(x_space[j])\n', (1882, 1894), True, 'import numpy as np\n'), ((6273, 6289), 'numpy.sqrt', 'np.sqrt', (['V[n, k]'], {}), '(V[n, k])\n', (6280, 6289), True, 'import numpy as np\n'), ((8270, 8331), 'numpy.log', 'np.log', (['(3.5 * K_original / H_original + rho / sigma * V[n][k])'], {}), '(3.5 * K_original / H_original + rho / sigma * V[n][k])\n', (8276, 8331), True, 'import numpy as np\n')]
import json import os import random from pathlib import Path from typing import Optional, List, Dict import cherrypy import numpy as np import psutil import yaml from app.emotions import predict_topk_emotions, EMOTIONS, get_fonts from app.features import extract_audio_features from app.keywords import predict_keywords METHOD_NAME_EMOTIONS = 'music-emotions' METHOD_NAME_FONTS = 'music-fonts' METHOD_NAME_KEYWORDS = 'music-keywords' ERROR_MESSAGE_NO_FILE_WITH_FEATURES = 'Invalid audio features path `%s`: no such file' ERROR_MESSAGE_INVALID_FEATURES = 'Failed to extract features from audio (is audio length less than 60 seconds?)' ERROR_MESSAGE_UNKNOWN_EMOTION = 'Unknown emotion `%s`. Expected emotions: [%s]' ERROR_MESSAGE_MODEL_FAIL_EMOTIONS = 'Failed to predict emotions: something wrong with model' ERROR_MESSAGE_MODEL_FAIL_KEYWORDS = 'Failed to predict keywords: something wrong with model' TypeAudio = cherrypy._cpreqbody.Part process = psutil.Process(os.getpid()) # for monitoring and debugging purposes config = yaml.safe_load(open('config.yml')) class ApiServerController(object): @cherrypy.expose(METHOD_NAME_EMOTIONS) def music_emotions(self, audio: TypeAudio): """ :param audio: audio file with music song """ features = get_audio_features(audio) if len(features) == 0: return result_error(ERROR_MESSAGE_INVALID_FEATURES) emotions = predict_topk_emotions(np.array(features), k=3) if len(emotions) == 0: return result_error(ERROR_MESSAGE_MODEL_FAIL_EMOTIONS) return result_emotions(emotions) @cherrypy.expose(METHOD_NAME_FONTS) def music_fonts(self, audio: Optional[TypeAudio] = None, emotion: Optional[str] = None): """ :param audio: audio file with music song :param emotion: emotion, selected by user """ if emotion is not None and emotion not in EMOTIONS: return result_error(ERROR_MESSAGE_UNKNOWN_EMOTION % (emotion, ', '.join(EMOTIONS))) if emotion is not None: emotions = [emotion] else: features = get_audio_features(audio) if len(features) == 0: return result_error(ERROR_MESSAGE_INVALID_FEATURES) emotions = predict_topk_emotions(np.array(features), k=3) emotion_fonts = {} for emotion in emotions: emotion_fonts[emotion] = get_fonts(emotion) return result_emotion_fonts(emotion_fonts) @cherrypy.expose(METHOD_NAME_KEYWORDS) def music_keywords(self, audio: TypeAudio): """ :param audio: audio file with music song """ features = get_audio_features(audio) if len(features) == 0: return result_error(ERROR_MESSAGE_INVALID_FEATURES) keywords = predict_keywords(np.array(features), k=10) if len(keywords) == 0: return result_error(ERROR_MESSAGE_MODEL_FAIL_KEYWORDS) return result_keywords(keywords) def get_audio_features(audio: TypeAudio) -> List[np.ndarray]: """ :param audio: audio file with music song :return: list of features for each full minute """ audio_file_name_prefix = random.randrange(1048576) tmp_dir = config['app']['tmp_dir'] audio_file_path = Path(os.path.join(tmp_dir, f'{audio_file_name_prefix}-{audio.filename}')) audio_file_path.parent.mkdir(exist_ok=True, parents=True) audio_file_path.write_bytes(audio.file.read()) features = extract_audio_features(audio_file_path) os.remove(audio_file_path) return features def result_error(error_message: str) -> str: """ :param: error_message: error message to return """ return json.dumps({ 'result': { 'error': error_message } }) def result_emotions(emotions: List[str]) -> str: """ :param: emotions: list of emotions to return, e.g. ['comfortable', 'happy', 'wary'] """ return json.dumps({ 'result': { 'emotions': emotions } }) def result_emotion_fonts(emotion_fonts: Dict[str, List[str]]) -> str: """ :param: emotions: fonts grouped by emotion, e.g. { 'comfortable': ['LexendExa', 'Suravaram', 'Philosopher'], 'happy': ['LilitaOne', 'Acme'] } """ return json.dumps({ 'result': [ {'emotion': emotion, 'fonts': fonts} for emotion, fonts in emotion_fonts.items() ] }) def result_keywords(keywords: List[str]) -> str: """ :param: keywords: list of keywords to return, e.g. ['porn', 'guitar', 'obama'] """ return json.dumps({ 'result': { 'keywords': keywords } }) if __name__ == '__main__': cherrypy.tree.mount(ApiServerController(), '/demo') cherrypy.config.update({ 'server.socket_port': config['app']['port'], 'server.socket_host': config['app']['host'], 'server.thread_pool': config['app']['thread_pool'], 'log.access_file': 'access1.log', 'log.error_file': 'error1.log', 'log.screen': True, 'tools.response_headers.on': True, 'tools.encode.encoding': 'utf-8', 'tools.response_headers.headers': [('Content-Type', 'text/html;encoding=utf-8')], }) try: cherrypy.engine.start() cherrypy.engine.block() except KeyboardInterrupt: cherrypy.engine.stop()
[ "os.remove", "cherrypy.expose", "os.getpid", "cherrypy.engine.start", "json.dumps", "cherrypy.config.update", "cherrypy.engine.block", "random.randrange", "numpy.array", "app.features.extract_audio_features", "app.emotions.get_fonts", "os.path.join", "cherrypy.engine.stop" ]
[((968, 979), 'os.getpid', 'os.getpid', ([], {}), '()\n', (977, 979), False, 'import os\n'), ((1110, 1147), 'cherrypy.expose', 'cherrypy.expose', (['METHOD_NAME_EMOTIONS'], {}), '(METHOD_NAME_EMOTIONS)\n', (1125, 1147), False, 'import cherrypy\n'), ((1622, 1656), 'cherrypy.expose', 'cherrypy.expose', (['METHOD_NAME_FONTS'], {}), '(METHOD_NAME_FONTS)\n', (1637, 1656), False, 'import cherrypy\n'), ((2505, 2542), 'cherrypy.expose', 'cherrypy.expose', (['METHOD_NAME_KEYWORDS'], {}), '(METHOD_NAME_KEYWORDS)\n', (2520, 2542), False, 'import cherrypy\n'), ((3212, 3237), 'random.randrange', 'random.randrange', (['(1048576)'], {}), '(1048576)\n', (3228, 3237), False, 'import random\n'), ((3503, 3542), 'app.features.extract_audio_features', 'extract_audio_features', (['audio_file_path'], {}), '(audio_file_path)\n', (3525, 3542), False, 'from app.features import extract_audio_features\n'), ((3547, 3573), 'os.remove', 'os.remove', (['audio_file_path'], {}), '(audio_file_path)\n', (3556, 3573), False, 'import os\n'), ((3719, 3767), 'json.dumps', 'json.dumps', (["{'result': {'error': error_message}}"], {}), "({'result': {'error': error_message}})\n", (3729, 3767), False, 'import json\n'), ((3970, 4016), 'json.dumps', 'json.dumps', (["{'result': {'emotions': emotions}}"], {}), "({'result': {'emotions': emotions}})\n", (3980, 4016), False, 'import json\n'), ((4626, 4672), 'json.dumps', 'json.dumps', (["{'result': {'keywords': keywords}}"], {}), "({'result': {'keywords': keywords}})\n", (4636, 4672), False, 'import json\n'), ((4799, 5226), 'cherrypy.config.update', 'cherrypy.config.update', (["{'server.socket_port': config['app']['port'], 'server.socket_host': config[\n 'app']['host'], 'server.thread_pool': config['app']['thread_pool'],\n 'log.access_file': 'access1.log', 'log.error_file': 'error1.log',\n 'log.screen': True, 'tools.response_headers.on': True,\n 'tools.encode.encoding': 'utf-8', 'tools.response_headers.headers': [(\n 'Content-Type', 'text/html;encoding=utf-8')]}"], {}), "({'server.socket_port': config['app']['port'],\n 'server.socket_host': config['app']['host'], 'server.thread_pool':\n config['app']['thread_pool'], 'log.access_file': 'access1.log',\n 'log.error_file': 'error1.log', 'log.screen': True,\n 'tools.response_headers.on': True, 'tools.encode.encoding': 'utf-8',\n 'tools.response_headers.headers': [('Content-Type',\n 'text/html;encoding=utf-8')]})\n", (4821, 5226), False, 'import cherrypy\n'), ((3305, 3372), 'os.path.join', 'os.path.join', (['tmp_dir', 'f"""{audio_file_name_prefix}-{audio.filename}"""'], {}), "(tmp_dir, f'{audio_file_name_prefix}-{audio.filename}')\n", (3317, 3372), False, 'import os\n'), ((5300, 5323), 'cherrypy.engine.start', 'cherrypy.engine.start', ([], {}), '()\n', (5321, 5323), False, 'import cherrypy\n'), ((5332, 5355), 'cherrypy.engine.block', 'cherrypy.engine.block', ([], {}), '()\n', (5353, 5355), False, 'import cherrypy\n'), ((1451, 1469), 'numpy.array', 'np.array', (['features'], {}), '(features)\n', (1459, 1469), True, 'import numpy as np\n'), ((2429, 2447), 'app.emotions.get_fonts', 'get_fonts', (['emotion'], {}), '(emotion)\n', (2438, 2447), False, 'from app.emotions import predict_topk_emotions, EMOTIONS, get_fonts\n'), ((2841, 2859), 'numpy.array', 'np.array', (['features'], {}), '(features)\n', (2849, 2859), True, 'import numpy as np\n'), ((5394, 5416), 'cherrypy.engine.stop', 'cherrypy.engine.stop', ([], {}), '()\n', (5414, 5416), False, 'import cherrypy\n'), ((2306, 2324), 'numpy.array', 'np.array', (['features'], {}), '(features)\n', (2314, 2324), True, 'import numpy as np\n')]
import matplotlib.pyplot as plt import pylab import numpy as N import scipy.io as sio import math from pyfmi import load_fmu fmu_loc = '/home/shashank/Documents/Gap Year Work/TAMU_ROVm/ROVm/Resources/FMU/' fmu_sm_name = 'SimplifiedBlueROV2.fmu' fmu_fm_name = 'InputBasedBlueROV2.fmu' fmu_full_sm_name = fmu_loc + fmu_sm_name fmu_full_fm_name = fmu_loc + fmu_fm_name smmodel = load_fmu(fmu_full_sm_name) fmmodel = load_fmu(fmu_full_fm_name) # logInfo = {1:[1,4],2:[4,7],3:[5,4],4:[6,10],5:[15,17],6:[16,17]} logInfo = {1:[16,1],} for i in range(1,len(logInfo)+1): for j in range(1,logInfo[i][1]+1): logNumber = [logInfo[i][0], 10] exp_log_loc = '/home/shashank/Documents/Gap Year Work/TAMU_ROVm/ROVm/Resources/PARSED_DATA_SPLIT/LOG' + str(logNumber[0]) + '/' exp_log_handle = 'IN_OUT_LOG' + str(logNumber[0]) + '_PARSED_' + str(logNumber[1]) + '.mat' exp_log_name = exp_log_loc + exp_log_handle exp = sio.loadmat(exp_log_name) parsed_exp = exp['inout_cell_mat_parsed'] t = parsed_exp[:,0] in_ch1 = parsed_exp[:,1] in_ch2 = parsed_exp[:,2] in_ch3 = parsed_exp[:,3] in_ch4 = parsed_exp[:,4] in_ch5 = parsed_exp[:,5] in_ch6 = parsed_exp[:,6] u_traj = N.transpose(N.vstack((t,in_ch1, in_ch2, in_ch3, in_ch4, in_ch5, in_ch6))) t_end_index = int(math.floor(parsed_exp.shape[0]-1)) t_data = parsed_exp[0:t_end_index,0] t_end = math.floor(t_data[t_data.shape[0]-1]) v_x_data = parsed_exp[0:t_end_index,7] v_y_data = parsed_exp[0:t_end_index,9] v_z_data = parsed_exp[0:t_end_index,8] try: input_object = (['u[1]','u[2]','u[3]','u[4]','u[5]','u[6]'], u_traj) smmodel.set('rovBody.mu_d',500) res_sm = smmodel.simulate(final_time = t_end, input=input_object) v_x_sm = res_sm['absoluteVelocity.v[1]'] v_y_sm = res_sm['absoluteVelocity.v[2]'] v_z_sm = res_sm['absoluteVelocity.v[3]'] t_sm = res_sm['time'] res_fm = fmmodel.simulate(final_time = t_end, input=input_object) v_x_fm = res_fm['absoluteVelocity.v[1]'] v_y_fm = res_fm['absoluteVelocity.v[2]'] v_z_fm = res_fm['absoluteVelocity.v[3]'] t_fm = res_fm['time'] plt.figure(1) plt.figure(figsize=(19.2,10.8), dpi=100) plt.subplot(3,1,1) plt.plot(t_sm, v_x_sm, t_fm, v_x_fm) plt.legend(('Simplified', 'Full')) plt.title("Model Comparison | Testing the Simplified and Full Model on Data Set: " + str(logNumber[0]) + "." + str(logNumber[1])) plt.ylabel("X-Axis (m/s)") plt.grid(True) plt.subplot(3,1,2) plt.plot(t_sm, v_y_sm, t_fm, v_y_fm) plt.legend(('Simplified', 'Full')) plt.ylabel("Y-Axis (m/s)") plt.grid(True) plt.subplot(3,1,3) plt.plot(t_sm, v_z_sm, t_fm, v_z_fm) plt.legend(('Simplified', 'Full')) plt.ylabel("Z-Axis (m/s)") plt.xlabel("Time (s)") plt.grid(True) pylab.savefig("Comp_"+str(logNumber[0]) + '_' + str(logNumber[1]) + '.png', bbox_inches = 'tight') except: print("Error in simulating Log " + str(logNumber[0]) + "." + str(logNumber[1]))
[ "matplotlib.pyplot.subplot", "matplotlib.pyplot.plot", "scipy.io.loadmat", "matplotlib.pyplot.legend", "math.floor", "matplotlib.pyplot.figure", "matplotlib.pyplot.ylabel", "pyfmi.load_fmu", "matplotlib.pyplot.grid", "numpy.vstack", "matplotlib.pyplot.xlabel" ]
[((389, 415), 'pyfmi.load_fmu', 'load_fmu', (['fmu_full_sm_name'], {}), '(fmu_full_sm_name)\n', (397, 415), False, 'from pyfmi import load_fmu\n'), ((427, 453), 'pyfmi.load_fmu', 'load_fmu', (['fmu_full_fm_name'], {}), '(fmu_full_fm_name)\n', (435, 453), False, 'from pyfmi import load_fmu\n'), ((936, 961), 'scipy.io.loadmat', 'sio.loadmat', (['exp_log_name'], {}), '(exp_log_name)\n', (947, 961), True, 'import scipy.io as sio\n'), ((1395, 1434), 'math.floor', 'math.floor', (['t_data[t_data.shape[0] - 1]'], {}), '(t_data[t_data.shape[0] - 1])\n', (1405, 1434), False, 'import math\n'), ((1224, 1285), 'numpy.vstack', 'N.vstack', (['(t, in_ch1, in_ch2, in_ch3, in_ch4, in_ch5, in_ch6)'], {}), '((t, in_ch1, in_ch2, in_ch3, in_ch4, in_ch5, in_ch6))\n', (1232, 1285), True, 'import numpy as N\n'), ((1309, 1344), 'math.floor', 'math.floor', (['(parsed_exp.shape[0] - 1)'], {}), '(parsed_exp.shape[0] - 1)\n', (1319, 1344), False, 'import math\n'), ((2148, 2161), 'matplotlib.pyplot.figure', 'plt.figure', (['(1)'], {}), '(1)\n', (2158, 2161), True, 'import matplotlib.pyplot as plt\n'), ((2166, 2207), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(19.2, 10.8)', 'dpi': '(100)'}), '(figsize=(19.2, 10.8), dpi=100)\n', (2176, 2207), True, 'import matplotlib.pyplot as plt\n'), ((2211, 2231), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(3)', '(1)', '(1)'], {}), '(3, 1, 1)\n', (2222, 2231), True, 'import matplotlib.pyplot as plt\n'), ((2234, 2270), 'matplotlib.pyplot.plot', 'plt.plot', (['t_sm', 'v_x_sm', 't_fm', 'v_x_fm'], {}), '(t_sm, v_x_sm, t_fm, v_x_fm)\n', (2242, 2270), True, 'import matplotlib.pyplot as plt\n'), ((2275, 2309), 'matplotlib.pyplot.legend', 'plt.legend', (["('Simplified', 'Full')"], {}), "(('Simplified', 'Full'))\n", (2285, 2309), True, 'import matplotlib.pyplot as plt\n'), ((2448, 2474), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""X-Axis (m/s)"""'], {}), "('X-Axis (m/s)')\n", (2458, 2474), True, 'import matplotlib.pyplot as plt\n'), ((2479, 2493), 'matplotlib.pyplot.grid', 'plt.grid', (['(True)'], {}), '(True)\n', (2487, 2493), True, 'import matplotlib.pyplot as plt\n'), ((2500, 2520), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(3)', '(1)', '(2)'], {}), '(3, 1, 2)\n', (2511, 2520), True, 'import matplotlib.pyplot as plt\n'), ((2523, 2559), 'matplotlib.pyplot.plot', 'plt.plot', (['t_sm', 'v_y_sm', 't_fm', 'v_y_fm'], {}), '(t_sm, v_y_sm, t_fm, v_y_fm)\n', (2531, 2559), True, 'import matplotlib.pyplot as plt\n'), ((2564, 2598), 'matplotlib.pyplot.legend', 'plt.legend', (["('Simplified', 'Full')"], {}), "(('Simplified', 'Full'))\n", (2574, 2598), True, 'import matplotlib.pyplot as plt\n'), ((2603, 2629), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Y-Axis (m/s)"""'], {}), "('Y-Axis (m/s)')\n", (2613, 2629), True, 'import matplotlib.pyplot as plt\n'), ((2634, 2648), 'matplotlib.pyplot.grid', 'plt.grid', (['(True)'], {}), '(True)\n', (2642, 2648), True, 'import matplotlib.pyplot as plt\n'), ((2655, 2675), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(3)', '(1)', '(3)'], {}), '(3, 1, 3)\n', (2666, 2675), True, 'import matplotlib.pyplot as plt\n'), ((2678, 2714), 'matplotlib.pyplot.plot', 'plt.plot', (['t_sm', 'v_z_sm', 't_fm', 'v_z_fm'], {}), '(t_sm, v_z_sm, t_fm, v_z_fm)\n', (2686, 2714), True, 'import matplotlib.pyplot as plt\n'), ((2719, 2753), 'matplotlib.pyplot.legend', 'plt.legend', (["('Simplified', 'Full')"], {}), "(('Simplified', 'Full'))\n", (2729, 2753), True, 'import matplotlib.pyplot as plt\n'), ((2758, 2784), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Z-Axis (m/s)"""'], {}), "('Z-Axis (m/s)')\n", (2768, 2784), True, 'import matplotlib.pyplot as plt\n'), ((2789, 2811), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Time (s)"""'], {}), "('Time (s)')\n", (2799, 2811), True, 'import matplotlib.pyplot as plt\n'), ((2816, 2830), 'matplotlib.pyplot.grid', 'plt.grid', (['(True)'], {}), '(True)\n', (2824, 2830), True, 'import matplotlib.pyplot as plt\n')]
import sys import os import sklearn from sklearn.decomposition import TruncatedSVD # give this a different alias so that it does not conflict with SPACY from sklearn.externals import joblib as sklearn_joblib import data_io, params, SIF_embedding from SIF_embedding import get_weighted_average # helper for word2vec format from data_io_w2v import load_w2v_word_map import numpy as np from past.builtins import xrange # this is a modified version of getWeight() from data_io.py # the main difference is that there is an option here to look up word keys as # different casings since the default def getWeightAlternate(words, word2weight, attempt_other_cases = True, report_oov = True): weight4ind = {} word_oov_set = set() word_remapping_set = set() for word, ind in words.items(): word_key = word if attempt_other_cases: if word_key not in word2weight: word_lower = word.lower() word_capital = word.capitalize() word_upper = word.upper() # let's try these in order if word_lower in word2weight: word_key = word_lower elif word_capital in word2weight: word_key = word_capital elif word_upper in word2weight: word_key = word_upper if word_key in word2weight: weight4ind[ind] = word2weight[word_key] if word != word_key: word_remapping_set.add(word) else: weight4ind[ind] = 1.0 word_oov_set.add(word) if report_oov: print('Total words remapped : {}'.format(len(word_remapping_set))) print('Total word-weight OOV : {}'.format(len(word_oov_set))) #word_oov_sorted = sorted(list(word_oov_set)) #print('Out of vocabulary with respect to word weighting:') #print(word_oov_sorted) return weight4ind def get_weighted_average_alternate(We, x, w): """ Compute the weighted average vectors :param We: We[i,:] is the vector for word i :param x: x[i, :] are the indices of the words in sentence i :param w: w[i, :] are the weights for the words in sentence i :return: emb[i, :] are the weighted average vector for sentence i """ n_samples = x.shape[0] emb = np.zeros((n_samples, We.shape[1])) for i in xrange(n_samples): denom = np.count_nonzero(w[i,:]) if denom <= 0.0: print('WHOA! Sample [{0}] attempted to compute a denominator of : [{1}]'.format(i, denom)) else: emb[i,:] = w[i,:].dot(We[x[i,:],:]) / denom return emb # This class serves as a means of fitting an SIF model and then being able to transform other sentence vectors later # This also allows save/loading model components via scikit-learn's joblib implementation class SIFModel(object): def __init__(self): self.trained = False self.svd = None self.word_map = None self.params = params self.sentence_count = -1 self.lowercase_tokens = False self.embeddings_filepath = None self.embeddings_format = None def transform(self, We, sentences): x, m = data_io.sentences2idx(sentences, self.word_map) # x is the array of word indices, m is the binary mask indicating whether there is a word in that location w = data_io.seq2weight(x, m, self.weight4ind) # get word weights weighted_emb = get_weighted_average(We, x, w) # now use the model we've already loaded return self.remove_pc(weighted_emb) def compute_pc(self, X): # this is what happens in compute_pc() in src/SIF_embedding.py self.svd = TruncatedSVD(n_components=self.params.rmpc, n_iter=7, random_state=0) self.svd.fit(X) def remove_pc(self, X): pc = self.svd.components_ if self.params.rmpc == 1: XX = X - X.dot(pc.transpose()) * pc else: XX = X - X.dot(pc.transpose()).dot(pc) return XX def fit(self, sentences, We, lowercase_tokens, embeddings_format, embeddings_filepath, params, word_map, weight4ind): # store these off for pickling or extra transforms self.word_map = word_map self.weight4ind = weight4ind self.params = params self.lowercase_tokens = lowercase_tokens self.embeddings_format = embeddings_format self.embeddings_filepath = embeddings_filepath self.sentence_count = len(sentences) x, m = data_io.sentences2idx(sentences, self.word_map) # x is the array of word indices, m is the binary mask indicating whether there is a word in that location w = data_io.seq2weight(x, m, self.weight4ind) # get word weights # now let's do some of what happens in src/SIF_embedding.py # but also keep some pieces along the way #weighted_emb = get_weighted_average(We, x, w) weighted_emb = get_weighted_average_alternate(We, x, w) self.compute_pc(weighted_emb) self.trained = True return self.remove_pc(weighted_emb) @staticmethod def embedding_loading_helper(embeddings_filepath, embeddings_format): words = None We = None if embeddings_format == 'GLOVE': print('Loading embeddings as GLOVE') (words, We) = data_io.load_glove_word_map(embeddings_filepath) elif embeddings_format == 'WORD2VEC_BIN': (words, We) = load_w2v_word_map(embeddings_filepath, binary = True) elif embeddings_format == 'WORD2VEC_TXT': (words, We) = load_w2v_word_map(embeddings_filepath, binary = False) else: print('Unknown embeddings format : {}'.format(embeddings_format)) return words, We
[ "past.builtins.xrange", "SIF_embedding.get_weighted_average", "numpy.count_nonzero", "sklearn.decomposition.TruncatedSVD", "data_io.load_glove_word_map", "data_io_w2v.load_w2v_word_map", "data_io.seq2weight", "numpy.zeros", "data_io.sentences2idx" ]
[((2442, 2476), 'numpy.zeros', 'np.zeros', (['(n_samples, We.shape[1])'], {}), '((n_samples, We.shape[1]))\n', (2450, 2476), True, 'import numpy as np\n'), ((2490, 2507), 'past.builtins.xrange', 'xrange', (['n_samples'], {}), '(n_samples)\n', (2496, 2507), False, 'from past.builtins import xrange\n'), ((2525, 2550), 'numpy.count_nonzero', 'np.count_nonzero', (['w[i, :]'], {}), '(w[i, :])\n', (2541, 2550), True, 'import numpy as np\n'), ((3345, 3392), 'data_io.sentences2idx', 'data_io.sentences2idx', (['sentences', 'self.word_map'], {}), '(sentences, self.word_map)\n', (3366, 3392), False, 'import data_io, params, SIF_embedding\n'), ((3512, 3553), 'data_io.seq2weight', 'data_io.seq2weight', (['x', 'm', 'self.weight4ind'], {}), '(x, m, self.weight4ind)\n', (3530, 3553), False, 'import data_io, params, SIF_embedding\n'), ((3596, 3626), 'SIF_embedding.get_weighted_average', 'get_weighted_average', (['We', 'x', 'w'], {}), '(We, x, w)\n', (3616, 3626), False, 'from SIF_embedding import get_weighted_average\n'), ((3848, 3917), 'sklearn.decomposition.TruncatedSVD', 'TruncatedSVD', ([], {'n_components': 'self.params.rmpc', 'n_iter': '(7)', 'random_state': '(0)'}), '(n_components=self.params.rmpc, n_iter=7, random_state=0)\n', (3860, 3917), False, 'from sklearn.decomposition import TruncatedSVD\n'), ((4731, 4778), 'data_io.sentences2idx', 'data_io.sentences2idx', (['sentences', 'self.word_map'], {}), '(sentences, self.word_map)\n', (4752, 4778), False, 'import data_io, params, SIF_embedding\n'), ((4898, 4939), 'data_io.seq2weight', 'data_io.seq2weight', (['x', 'm', 'self.weight4ind'], {}), '(x, m, self.weight4ind)\n', (4916, 4939), False, 'import data_io, params, SIF_embedding\n'), ((5598, 5646), 'data_io.load_glove_word_map', 'data_io.load_glove_word_map', (['embeddings_filepath'], {}), '(embeddings_filepath)\n', (5625, 5646), False, 'import data_io, params, SIF_embedding\n'), ((5723, 5774), 'data_io_w2v.load_w2v_word_map', 'load_w2v_word_map', (['embeddings_filepath'], {'binary': '(True)'}), '(embeddings_filepath, binary=True)\n', (5740, 5774), False, 'from data_io_w2v import load_w2v_word_map\n'), ((5853, 5905), 'data_io_w2v.load_w2v_word_map', 'load_w2v_word_map', (['embeddings_filepath'], {'binary': '(False)'}), '(embeddings_filepath, binary=False)\n', (5870, 5905), False, 'from data_io_w2v import load_w2v_word_map\n')]
# coding=utf8 # This code is adapted from the https://github.com/tensorflow/models/tree/master/official/r1/resnet. # ========================================================================================== # NAVER’s modifications are Copyright 2020 NAVER corp. All rights reserved. # ========================================================================================== # Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import os import tensorflow as tf from official.utils.export import export from utils import data_util from functions import data_config import numpy as np from tqdm import tqdm def export_test(bin_export_path, flags_obj, ir_eval): ds = tf.data.Dataset.list_files(flags_obj.data_dir + '/' + flags_obj.val_regex) ds = ds.interleave(tf.data.TFRecordDataset, cycle_length=10) def parse_tfr(example_proto): feature_def = {'image/class/label': tf.FixedLenFeature([], dtype=tf.int64, default_value=-1), 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='')} features = tf.io.parse_single_example(serialized=example_proto, features=feature_def) return features['image/encoded'], features['image/class/label'] ds = ds.map(parse_tfr) ds = ds.batch(flags_obj.val_batch_size) iterator = ds.make_one_shot_iterator() images, labels = iterator.get_next() dconf = data_config.get_config(flags_obj.dataset_name) num_val_images = dconf.num_images['validation'] if flags_obj.zeroshot_eval or ir_eval: feature_dim = flags_obj.embedding_size if flags_obj.embedding_size > 0 else flags_obj.num_features np_features = np.zeros((num_val_images, feature_dim), dtype=np.float32) np_labels = np.zeros(num_val_images, dtype=np.int64) np_i = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) tf.saved_model.load(sess=sess, export_dir=bin_export_path, tags={"serve"}) for _ in tqdm(range(int(num_val_images / flags_obj.val_batch_size) + 1)): try: np_image, np_label = sess.run([images, labels]) np_predict = sess.run('embedding_tensor:0', feed_dict={'input_tensor:0': np_image}) np_features[np_i:np_i + np_predict.shape[0], :] = np_predict np_labels[np_i:np_i + np_label.shape[0]] = np_label np_i += np_predict.shape[0] except tf.errors.OutOfRangeError: break assert np_i == num_val_images from sklearn.preprocessing import normalize x = normalize(np_features) np_sim = x.dot(x.T) np.fill_diagonal(np_sim, -10) # removing similarity for query. num_correct = 0 for i in range(num_val_images): cur_label = np_labels[i] rank1_label = np_labels[np.argmax(np_sim[i, :])] if rank1_label == cur_label: num_correct += 1 recall_at_1 = num_correct / num_val_images metric = recall_at_1 else: np_i = 0 correct_cnt = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) tf.saved_model.load(sess=sess, export_dir=bin_export_path, tags={"serve"}) for _ in tqdm(range(int(num_val_images / flags_obj.val_batch_size) + 1)): try: np_image, np_label = sess.run([images, labels]) np_predict = sess.run('ArgMax:0', feed_dict={'input_tensor:0': np_image}) np_i += np_predict.shape[0] correct_cnt += np.sum(np_predict == np_label) except tf.errors.OutOfRangeError: break assert np_i == num_val_images metric = correct_cnt / np_i return metric def image_bytes_serving_input_fn(image_shape, decoder_name, dtype=tf.float32, pptype='imagenet'): """Serving input fn for raw jpeg images.""" def _preprocess_image(image_bytes): """Preprocess a single raw image.""" # Bounding box around the whole image. bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=dtype, shape=[1, 1, 4]) _, _, num_channels = image_shape tf.logging.info("!!!!!!!!!! Preprocessing type for exporting pb: {} and decoder type: {}".format(pptype, decoder_name)) image = data_util.preprocess_image( image_buffer=image_bytes, is_training=False, bbox=bbox, num_channels=num_channels, dtype=dtype, use_random_crop=False, decoder_name=decoder_name, dct_method='INTEGER_ACCURATE', preprocessing_type=pptype) return image image_bytes_list = tf.placeholder( shape=[None], dtype=tf.string, name='input_tensor') images = tf.map_fn( _preprocess_image, image_bytes_list, back_prop=False, dtype=dtype) return tf.estimator.export.TensorServingInputReceiver( images, {'image_bytes': image_bytes_list}) def export_pb(flags_core, flags_obj, shape, classifier, ir_eval=False): export_dtype = flags_core.get_tf_dtype(flags_obj) if not flags_obj.data_format: raise ValueError('The `data_format` must be specified: channels_first or channels_last ') bin_export_path = os.path.join(flags_obj.export_dir, flags_obj.data_format, 'binary_input') bin_input_receiver_fn = functools.partial(image_bytes_serving_input_fn, shape, flags_obj.export_decoder_type, dtype=export_dtype, pptype=flags_obj.preprocessing_type) pp_export_path = os.path.join(flags_obj.export_dir, flags_obj.data_format, 'preprocessed_input') pp_input_receiver_fn = export.build_tensor_serving_input_receiver_fn( shape, batch_size=None, dtype=export_dtype) result_bin_export_path = classifier.export_savedmodel(bin_export_path, bin_input_receiver_fn) classifier.export_savedmodel(pp_export_path, pp_input_receiver_fn) if flags_obj.export_decoder_type == 'jpeg': metric = export_test(result_bin_export_path, flags_obj, ir_eval) msg = 'IMPOTANT! Evaluation metric of exported saved_model.pb is {}'.format(metric) tf.logging.info(msg) with tf.gfile.Open(result_bin_export_path.decode("utf-8") + '/model_performance.txt', 'w') as fp: fp.write(msg)
[ "numpy.sum", "tensorflow.logging.info", "numpy.argmax", "tensorflow.local_variables_initializer", "os.path.join", "tensorflow.estimator.export.TensorServingInputReceiver", "tensorflow.placeholder", "tensorflow.map_fn", "functools.partial", "numpy.fill_diagonal", "tensorflow.global_variables_initializer", "tensorflow.Session", "tensorflow.constant", "utils.data_util.preprocess_image", "sklearn.preprocessing.normalize", "tensorflow.saved_model.load", "tensorflow.io.parse_single_example", "numpy.zeros", "tensorflow.data.Dataset.list_files", "tensorflow.FixedLenFeature", "functions.data_config.get_config", "official.utils.export.export.build_tensor_serving_input_receiver_fn" ]
[((1436, 1510), 'tensorflow.data.Dataset.list_files', 'tf.data.Dataset.list_files', (["(flags_obj.data_dir + '/' + flags_obj.val_regex)"], {}), "(flags_obj.data_dir + '/' + flags_obj.val_regex)\n", (1462, 1510), True, 'import tensorflow as tf\n'), ((2116, 2162), 'functions.data_config.get_config', 'data_config.get_config', (['flags_obj.dataset_name'], {}), '(flags_obj.dataset_name)\n', (2138, 2162), False, 'from functions import data_config\n'), ((5267, 5333), 'tensorflow.placeholder', 'tf.placeholder', ([], {'shape': '[None]', 'dtype': 'tf.string', 'name': '"""input_tensor"""'}), "(shape=[None], dtype=tf.string, name='input_tensor')\n", (5281, 5333), True, 'import tensorflow as tf\n'), ((5350, 5426), 'tensorflow.map_fn', 'tf.map_fn', (['_preprocess_image', 'image_bytes_list'], {'back_prop': '(False)', 'dtype': 'dtype'}), '(_preprocess_image, image_bytes_list, back_prop=False, dtype=dtype)\n', (5359, 5426), True, 'import tensorflow as tf\n'), ((5441, 5534), 'tensorflow.estimator.export.TensorServingInputReceiver', 'tf.estimator.export.TensorServingInputReceiver', (['images', "{'image_bytes': image_bytes_list}"], {}), "(images, {'image_bytes':\n image_bytes_list})\n", (5487, 5534), True, 'import tensorflow as tf\n'), ((5810, 5883), 'os.path.join', 'os.path.join', (['flags_obj.export_dir', 'flags_obj.data_format', '"""binary_input"""'], {}), "(flags_obj.export_dir, flags_obj.data_format, 'binary_input')\n", (5822, 5883), False, 'import os\n'), ((5910, 6062), 'functools.partial', 'functools.partial', (['image_bytes_serving_input_fn', 'shape', 'flags_obj.export_decoder_type'], {'dtype': 'export_dtype', 'pptype': 'flags_obj.preprocessing_type'}), '(image_bytes_serving_input_fn, shape, flags_obj.\n export_decoder_type, dtype=export_dtype, pptype=flags_obj.\n preprocessing_type)\n', (5927, 6062), False, 'import functools\n'), ((6117, 6196), 'os.path.join', 'os.path.join', (['flags_obj.export_dir', 'flags_obj.data_format', '"""preprocessed_input"""'], {}), "(flags_obj.export_dir, flags_obj.data_format, 'preprocessed_input')\n", (6129, 6196), False, 'import os\n'), ((6222, 6316), 'official.utils.export.export.build_tensor_serving_input_receiver_fn', 'export.build_tensor_serving_input_receiver_fn', (['shape'], {'batch_size': 'None', 'dtype': 'export_dtype'}), '(shape, batch_size=None, dtype\n =export_dtype)\n', (6267, 6316), False, 'from official.utils.export import export\n'), ((1815, 1889), 'tensorflow.io.parse_single_example', 'tf.io.parse_single_example', ([], {'serialized': 'example_proto', 'features': 'feature_def'}), '(serialized=example_proto, features=feature_def)\n', (1841, 1889), True, 'import tensorflow as tf\n'), ((2375, 2432), 'numpy.zeros', 'np.zeros', (['(num_val_images, feature_dim)'], {'dtype': 'np.float32'}), '((num_val_images, feature_dim), dtype=np.float32)\n', (2383, 2432), True, 'import numpy as np\n'), ((2449, 2489), 'numpy.zeros', 'np.zeros', (['num_val_images'], {'dtype': 'np.int64'}), '(num_val_images, dtype=np.int64)\n', (2457, 2489), True, 'import numpy as np\n'), ((3315, 3337), 'sklearn.preprocessing.normalize', 'normalize', (['np_features'], {}), '(np_features)\n', (3324, 3337), False, 'from sklearn.preprocessing import normalize\n'), ((3366, 3395), 'numpy.fill_diagonal', 'np.fill_diagonal', (['np_sim', '(-10)'], {}), '(np_sim, -10)\n', (3382, 3395), True, 'import numpy as np\n'), ((4741, 4804), 'tensorflow.constant', 'tf.constant', (['[0.0, 0.0, 1.0, 1.0]'], {'dtype': 'dtype', 'shape': '[1, 1, 4]'}), '([0.0, 0.0, 1.0, 1.0], dtype=dtype, shape=[1, 1, 4])\n', (4752, 4804), True, 'import tensorflow as tf\n'), ((4978, 5221), 'utils.data_util.preprocess_image', 'data_util.preprocess_image', ([], {'image_buffer': 'image_bytes', 'is_training': '(False)', 'bbox': 'bbox', 'num_channels': 'num_channels', 'dtype': 'dtype', 'use_random_crop': '(False)', 'decoder_name': 'decoder_name', 'dct_method': '"""INTEGER_ACCURATE"""', 'preprocessing_type': 'pptype'}), "(image_buffer=image_bytes, is_training=False,\n bbox=bbox, num_channels=num_channels, dtype=dtype, use_random_crop=\n False, decoder_name=decoder_name, dct_method='INTEGER_ACCURATE',\n preprocessing_type=pptype)\n", (5004, 5221), False, 'from utils import data_util\n'), ((6691, 6711), 'tensorflow.logging.info', 'tf.logging.info', (['msg'], {}), '(msg)\n', (6706, 6711), True, 'import tensorflow as tf\n'), ((1647, 1703), 'tensorflow.FixedLenFeature', 'tf.FixedLenFeature', (['[]'], {'dtype': 'tf.int64', 'default_value': '(-1)'}), '([], dtype=tf.int64, default_value=-1)\n', (1665, 1703), True, 'import tensorflow as tf\n'), ((1741, 1798), 'tensorflow.FixedLenFeature', 'tf.FixedLenFeature', (['[]'], {'dtype': 'tf.string', 'default_value': '""""""'}), "([], dtype=tf.string, default_value='')\n", (1759, 1798), True, 'import tensorflow as tf\n'), ((2512, 2524), 'tensorflow.Session', 'tf.Session', ([], {}), '()\n', (2522, 2524), True, 'import tensorflow as tf\n'), ((2639, 2713), 'tensorflow.saved_model.load', 'tf.saved_model.load', ([], {'sess': 'sess', 'export_dir': 'bin_export_path', 'tags': "{'serve'}"}), "(sess=sess, export_dir=bin_export_path, tags={'serve'})\n", (2658, 2713), True, 'import tensorflow as tf\n'), ((3754, 3766), 'tensorflow.Session', 'tf.Session', ([], {}), '()\n', (3764, 3766), True, 'import tensorflow as tf\n'), ((3881, 3955), 'tensorflow.saved_model.load', 'tf.saved_model.load', ([], {'sess': 'sess', 'export_dir': 'bin_export_path', 'tags': "{'serve'}"}), "(sess=sess, export_dir=bin_export_path, tags={'serve'})\n", (3900, 3955), True, 'import tensorflow as tf\n'), ((2549, 2582), 'tensorflow.global_variables_initializer', 'tf.global_variables_initializer', ([], {}), '()\n', (2580, 2582), True, 'import tensorflow as tf\n'), ((2599, 2631), 'tensorflow.local_variables_initializer', 'tf.local_variables_initializer', ([], {}), '()\n', (2629, 2631), True, 'import tensorflow as tf\n'), ((3547, 3570), 'numpy.argmax', 'np.argmax', (['np_sim[i, :]'], {}), '(np_sim[i, :])\n', (3556, 3570), True, 'import numpy as np\n'), ((3791, 3824), 'tensorflow.global_variables_initializer', 'tf.global_variables_initializer', ([], {}), '()\n', (3822, 3824), True, 'import tensorflow as tf\n'), ((3841, 3873), 'tensorflow.local_variables_initializer', 'tf.local_variables_initializer', ([], {}), '()\n', (3871, 3873), True, 'import tensorflow as tf\n'), ((4286, 4316), 'numpy.sum', 'np.sum', (['(np_predict == np_label)'], {}), '(np_predict == np_label)\n', (4292, 4316), True, 'import numpy as np\n')]
import cv2 as cv import numpy as np # https://docs.opencv.org/4.2.0/d7/dfc/group__highgui.html def white_balance(img): result = cv.cvtColor(img, cv.COLOR_BGR2LAB) avg_a = np.average(result[:, :, 1]) avg_b = np.average(result[:, :, 2]) result[:, :, 1] = result[:, :, 1] - ((avg_a - 128) * (result[:, :, 0] / 255.0) * 1.1) result[:, :, 2] = result[:, :, 2] - ((avg_b - 128) * (result[:, :, 0] / 255.0) * 1.1) result = cv.cvtColor(result, cv.COLOR_LAB2BGR) return result cv.namedWindow("webcam") cv.moveWindow('webcam', 0, 0) cv.namedWindow("l") cv.moveWindow("l", 0, 300) cv.namedWindow("a") cv.moveWindow("a", 340, 300) cv.namedWindow("b") cv.moveWindow("b", 680, 300) image = cv.imread("sample.png") image = cv.resize(image, (320, 240)) cv.imshow('webcam', image) # gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) # b, g, r = cv.split(image) # cv.imshow('b', b) # cv.imshow('g', g) # cv.imshow('r', r) conv = cv.cvtColor(image, cv.COLOR_BGR2LAB) l, a, b = cv.split(conv) # l[:] = 100 a[:] += 10 # green - red b[:] -= 25 # blue - yellow # print(b[0][0]) # print(type(b)) # numpy.ndarray # print(b.shape) # print(b.size) # print(b.dtype) cv.imshow('l', l) cv.imshow('a', a) cv.imshow('b', b) result = cv.merge((l, a, b)) result = cv.cvtColor(result, cv.COLOR_LAB2BGR) cv.imshow('result', result) auto_balanced = white_balance(image) cv.imshow('auto', auto_balanced) # cv.setWindowProperty('webcam', cv.WND_PROP_AUTOSIZE, cv.WINDOW_FULLSCREEN) cv.waitKey(0) # cv.destroyWindow('SnapshotTest') cv.destroyAllWindows() vc.release()
[ "numpy.average", "cv2.cvtColor", "cv2.waitKey", "cv2.destroyAllWindows", "cv2.merge", "cv2.imread", "cv2.namedWindow", "cv2.split", "cv2.moveWindow", "cv2.imshow", "cv2.resize" ]
[((499, 523), 'cv2.namedWindow', 'cv.namedWindow', (['"""webcam"""'], {}), "('webcam')\n", (513, 523), True, 'import cv2 as cv\n'), ((524, 553), 'cv2.moveWindow', 'cv.moveWindow', (['"""webcam"""', '(0)', '(0)'], {}), "('webcam', 0, 0)\n", (537, 553), True, 'import cv2 as cv\n'), ((555, 574), 'cv2.namedWindow', 'cv.namedWindow', (['"""l"""'], {}), "('l')\n", (569, 574), True, 'import cv2 as cv\n'), ((575, 601), 'cv2.moveWindow', 'cv.moveWindow', (['"""l"""', '(0)', '(300)'], {}), "('l', 0, 300)\n", (588, 601), True, 'import cv2 as cv\n'), ((603, 622), 'cv2.namedWindow', 'cv.namedWindow', (['"""a"""'], {}), "('a')\n", (617, 622), True, 'import cv2 as cv\n'), ((623, 651), 'cv2.moveWindow', 'cv.moveWindow', (['"""a"""', '(340)', '(300)'], {}), "('a', 340, 300)\n", (636, 651), True, 'import cv2 as cv\n'), ((653, 672), 'cv2.namedWindow', 'cv.namedWindow', (['"""b"""'], {}), "('b')\n", (667, 672), True, 'import cv2 as cv\n'), ((673, 701), 'cv2.moveWindow', 'cv.moveWindow', (['"""b"""', '(680)', '(300)'], {}), "('b', 680, 300)\n", (686, 701), True, 'import cv2 as cv\n'), ((711, 734), 'cv2.imread', 'cv.imread', (['"""sample.png"""'], {}), "('sample.png')\n", (720, 734), True, 'import cv2 as cv\n'), ((743, 771), 'cv2.resize', 'cv.resize', (['image', '(320, 240)'], {}), '(image, (320, 240))\n', (752, 771), True, 'import cv2 as cv\n'), ((772, 798), 'cv2.imshow', 'cv.imshow', (['"""webcam"""', 'image'], {}), "('webcam', image)\n", (781, 798), True, 'import cv2 as cv\n'), ((944, 980), 'cv2.cvtColor', 'cv.cvtColor', (['image', 'cv.COLOR_BGR2LAB'], {}), '(image, cv.COLOR_BGR2LAB)\n', (955, 980), True, 'import cv2 as cv\n'), ((991, 1005), 'cv2.split', 'cv.split', (['conv'], {}), '(conv)\n', (999, 1005), True, 'import cv2 as cv\n'), ((1175, 1192), 'cv2.imshow', 'cv.imshow', (['"""l"""', 'l'], {}), "('l', l)\n", (1184, 1192), True, 'import cv2 as cv\n'), ((1193, 1210), 'cv2.imshow', 'cv.imshow', (['"""a"""', 'a'], {}), "('a', a)\n", (1202, 1210), True, 'import cv2 as cv\n'), ((1211, 1228), 'cv2.imshow', 'cv.imshow', (['"""b"""', 'b'], {}), "('b', b)\n", (1220, 1228), True, 'import cv2 as cv\n'), ((1239, 1258), 'cv2.merge', 'cv.merge', (['(l, a, b)'], {}), '((l, a, b))\n', (1247, 1258), True, 'import cv2 as cv\n'), ((1268, 1305), 'cv2.cvtColor', 'cv.cvtColor', (['result', 'cv.COLOR_LAB2BGR'], {}), '(result, cv.COLOR_LAB2BGR)\n', (1279, 1305), True, 'import cv2 as cv\n'), ((1306, 1333), 'cv2.imshow', 'cv.imshow', (['"""result"""', 'result'], {}), "('result', result)\n", (1315, 1333), True, 'import cv2 as cv\n'), ((1372, 1404), 'cv2.imshow', 'cv.imshow', (['"""auto"""', 'auto_balanced'], {}), "('auto', auto_balanced)\n", (1381, 1404), True, 'import cv2 as cv\n'), ((1483, 1496), 'cv2.waitKey', 'cv.waitKey', (['(0)'], {}), '(0)\n', (1493, 1496), True, 'import cv2 as cv\n'), ((1541, 1563), 'cv2.destroyAllWindows', 'cv.destroyAllWindows', ([], {}), '()\n', (1561, 1563), True, 'import cv2 as cv\n'), ((134, 168), 'cv2.cvtColor', 'cv.cvtColor', (['img', 'cv.COLOR_BGR2LAB'], {}), '(img, cv.COLOR_BGR2LAB)\n', (145, 168), True, 'import cv2 as cv\n'), ((181, 208), 'numpy.average', 'np.average', (['result[:, :, 1]'], {}), '(result[:, :, 1])\n', (191, 208), True, 'import numpy as np\n'), ((221, 248), 'numpy.average', 'np.average', (['result[:, :, 2]'], {}), '(result[:, :, 2])\n', (231, 248), True, 'import numpy as np\n'), ((442, 479), 'cv2.cvtColor', 'cv.cvtColor', (['result', 'cv.COLOR_LAB2BGR'], {}), '(result, cv.COLOR_LAB2BGR)\n', (453, 479), True, 'import cv2 as cv\n')]
import numpy as np import math from geofractal import * #------------------------------------------------------- # Fractal dimension #------------------------------------------------------- df = 1.8 #------------------------------------------------------- # Fractal prefactor #------------------------------------------------------- k0 = 0.5*(0.3-np.sqrt(3.0))*(df-1.0)+np.sqrt(3.0) #------------------------------------------------------- # Model of correlation function #------------------------------------------------------- #cormodel= 'EXPNL' #cormodel= 'GAUSS' cormodel= 'FLDIM' #------------------------------------------------------- # call geofractal.py #------------------------------------------------------- Nmin = 1.e0 Nmax = 1.e10 N = 250 PN = np.exp(np.linspace(math.log(Nmin),math.log(Nmax),N)) G = np.zeros(N) for i in range(N): G[i] = geofractal(PN[i],df,k0,cormodel) #------------------------------------------------------- # output the results #------------------------------------------------------- filename='gratio.out' with open(filename,'w') as f: f.write('# df = %13.6e \n'%df) f.write('# k0 = %13.6e \n'%k0) f.write('# model = %11s \n'%cormodel) f.write('# %11s %13s\n'%('PN','G/NpiR0^2')) for i in range(N): f.write('%13.6e %13.6e\n'%(PN[i],G[i]))
[ "math.log", "numpy.zeros", "numpy.sqrt" ]
[((829, 840), 'numpy.zeros', 'np.zeros', (['N'], {}), '(N)\n', (837, 840), True, 'import numpy as np\n'), ((375, 387), 'numpy.sqrt', 'np.sqrt', (['(3.0)'], {}), '(3.0)\n', (382, 387), True, 'import numpy as np\n'), ((788, 802), 'math.log', 'math.log', (['Nmin'], {}), '(Nmin)\n', (796, 802), False, 'import math\n'), ((803, 817), 'math.log', 'math.log', (['Nmax'], {}), '(Nmax)\n', (811, 817), False, 'import math\n'), ((352, 364), 'numpy.sqrt', 'np.sqrt', (['(3.0)'], {}), '(3.0)\n', (359, 364), True, 'import numpy as np\n')]
""" A Maximum-Entropy model for backbone torsion angles. Reference: Rowicka and Otwinowski 2004 """ import numpy from csb.statistics.pdf import BaseDensity class MaxentModel(BaseDensity): """ Fourier expansion of a biangular log-probability density """ def __init__(self, n, beta=1.): """ @param n: order of the fourier expansion @type n: int @param beta: inverse temperature @type beta: float """ super(MaxentModel, self).__init__() self._n = int(n) self._cc = numpy.zeros((self._n, self._n)) self._ss = numpy.zeros((self._n, self._n)) self._cs = numpy.zeros((self._n, self._n)) self._sc = numpy.zeros((self._n, self._n)) self._beta = float(beta) @property def beta(self): """ Inverse temperature @rtype: float """ return self._beta @property def n(self): """ Order of the fourier expansion @rtype: int """ return self._n def load_old(self, aa, f_name): """ Load set of expansion coefficients from isd. @param aa: Amino acid type @param f_name: File containing ramachandran definition """ import os params, _energies = eval(open(os.path.expanduser(f_name)).read()) params = params[self._n - 1] for k, l, x, f, g in params[aa]: if f == 'cos' and g == 'cos': self._cc[k, l] = -x elif f == 'cos' and g == 'sin': self._cs[k, l] = -x elif f == 'sin' and g == 'cos': self._sc[k, l] = -x elif f == 'sin' and g == 'sin': self._ss[k, l] = -x def load(self, aa, f_name): """ Load set of expansion coefficients from isd+. @param aa: Amino acid type @param f_name: File containing ramachandran definition """ import os from numpy import reshape, array from csb.io import load f_name = os.path.expanduser(f_name) params, _energies = load(f_name) params = params[self._n] a, b, c, d = params[aa] a, b, c, d = reshape(array(a), (self._n, self._n)).astype('d'), \ reshape(array(b), (self._n, self._n)).astype('d'), \ reshape(array(c), (self._n, self._n)).astype('d'), \ reshape(array(d), (self._n, self._n)).astype('d') # Not a typo, I accidently swichted cos*sin and sin*cos self._cc, self._cs, self._sc, self._ss = -a, -c, -b, -d def _periodicities(self): return numpy.arange(self._n) def log_prob(self, x, y): """ Return the energy at positions (x,y). @param x: x-coordinates for evaluation @type x: array-like @param y: y-coordinates for evaluation @type y: array-like """ return -self.energy(x, y) def set(self, coef): """ Set the fourier expansion coefficients and calculations the new partation function. @param coef: expansion coefficents @type coef: array like, with shape (4,n,n) """ self._cc[:, :], self._ss[:, :], self._cs[:, :], self._sc[:, :] = \ numpy.reshape(coef, (4, self._n, self._n)) self.normalize() def get(self): """ Return current expansion coefficients. """ return numpy.array([self._cc, self._ss, self._cs, self._sc]) def energy(self, x, y=None): """ Return the energy at positions (x,y). @param x: x-coordinates for evaluation @type x: array-like @param y: y-coordinates for evaluation @type y: array-like """ from numpy import sin, cos, dot, multiply k = self._periodicities() cx, sx = cos(multiply.outer(k, x)), sin(multiply.outer(k, x)) if y is not None: cy, sy = cos(multiply.outer(k, y)), sin(multiply.outer(k, y)) else: cy, sy = cx, sx return dot(dot(cx.T, self._cc), cy) + \ dot(dot(cx.T, self._cs), sy) + \ dot(dot(sx.T, self._sc), cy) + \ dot(dot(sx.T, self._ss), sy) def sample_weights(self): """ Create a random set of expansion coefficients. """ from numpy import add from numpy.random import standard_normal k = self._periodicities() k = add.outer(k ** 2, k ** 2) self.set([standard_normal(k.shape) for i in range(4)]) self.normalize(True) def prob(self, x, y): """ Return the probability of the configurations x cross y. """ from csb.numeric import exp return exp(-self.beta * self(x, y)) def z(self): """ Calculate the partion function . """ from scipy.integrate import dblquad from numpy import pi return dblquad(self.prob, 0., 2 * pi, lambda x: 0., lambda x: 2 * pi) def log_z(self, n=500, integration='simpson'): """ Calculate the log partion function. """ from numpy import pi, linspace, max from csb.numeric import log, exp if integration == 'simpson': from csb.numeric import simpson_2d x = linspace(0., 2 * pi, 2 * n + 1) dx = x[1] - x[0] f = -self.beta * self.energy(x) f_max = max(f) f -= f_max I = simpson_2d(exp(f)) return log(I) + f_max + 2 * log(dx) elif integration == 'trapezoidal': from csb.numeric import trapezoidal_2d x = linspace(0., 2 * pi, n) dx = x[1] - x[0] f = -self.beta * self.energy(x) f_max = max(f) f -= f_max I = trapezoidal_2d(exp(f)) return log(I) + f_max + 2 * log(dx) else: raise NotImplementedError( 'Choose from trapezoidal and simpson-rule Integration') def entropy(self, n=500): """ Calculate the entropy of the model. @param n: number of integration points for numerical integration @type n: integer """ from csb.numeric import trapezoidal_2d from numpy import pi, linspace, max from csb.numeric import log, exp x = linspace(0., 2 * pi, n) dx = x[1] - x[0] f = -self.beta * self.energy(x) f_max = max(f) log_z = log(trapezoidal_2d(exp(f - f_max))) + f_max + 2 * log(dx) average_energy = trapezoidal_2d(f * exp(f - f_max))\ * exp(f_max + 2 * log(dx) - log_z) return -average_energy + log_z def calculate_statistics(self, data): """ Calculate the sufficient statistics for the data. """ from numpy import cos, sin, dot, multiply k = self._periodicities() cx = cos(multiply.outer(k, data[:, 0])) sx = sin(multiply.outer(k, data[:, 0])) cy = cos(multiply.outer(k, data[:, 1])) sy = sin(multiply.outer(k, data[:, 1])) return dot(cx, cy.T), dot(sx, sy.T), dot(cx, sy.T), dot(sx, cy.T) def normalize(self, normalize_full=True): """ Remove parameter, which do not have any influence on the model and compute the partition function. @param normalize_full: compute partition function @type normalize_full: boolean """ self._cc[0, 0] = 0. self._ss[:, 0] = 0. self._ss[0, :] = 0. self._cs[:, 0] = 0. self._sc[0, :] = 0. if normalize_full: self._cc[0, 0] = self.log_z() class MaxentPosterior(object): """ Object to hold and calculate the posterior (log)probability given an exponential family model and corresponding data. """ def __init__(self, model, data): """ @param model: MaxentModel @param data: two dimensonal data """ self._model = model self._data = numpy.array(data) self._stats = self.model.calculate_statistics(self._data) self._log_likelihoods = [] @property def model(self): return self._model @model.setter def model(self, value): self._model = value self._stats = self.model.calculate_statistics(self._data) @property def data(self): return self._data @data.setter def data(self, value): self._data = numpy.array(value) self._stats = self.model.calculate_statistics(value) @property def stats(self): return self._stats def __call__(self, weights=None, n=100): """ Returns the log posterior likelihood @param weights: optional expansion coefficients of the model, if none are specified those of the model are used @param n: number of integration point for calculating the partition function """ from numpy import sum if weights is not None: self.model.set(weights) a = sum(self._stats[0] * self.model._cc) b = sum(self._stats[1] * self.model._ss) c = sum(self._stats[2] * self.model._cs) d = sum(self._stats[3] * self.model._sc) log_z = self.data.shape[0] * self.model.log_z(n=n) log_likelihood = -self.model.beta * (a + b + c + d) - log_z self._log_likelihoods.append(log_likelihood) return log_likelihood
[ "numpy.sum", "csb.numeric.log", "numpy.zeros", "numpy.max", "numpy.multiply.outer", "numpy.arange", "numpy.reshape", "numpy.array", "scipy.integrate.dblquad", "numpy.linspace", "numpy.dot", "numpy.random.standard_normal", "numpy.add.outer", "os.path.expanduser", "csb.io.load", "csb.numeric.exp" ]
[((566, 597), 'numpy.zeros', 'numpy.zeros', (['(self._n, self._n)'], {}), '((self._n, self._n))\n', (577, 597), False, 'import numpy\n'), ((617, 648), 'numpy.zeros', 'numpy.zeros', (['(self._n, self._n)'], {}), '((self._n, self._n))\n', (628, 648), False, 'import numpy\n'), ((668, 699), 'numpy.zeros', 'numpy.zeros', (['(self._n, self._n)'], {}), '((self._n, self._n))\n', (679, 699), False, 'import numpy\n'), ((719, 750), 'numpy.zeros', 'numpy.zeros', (['(self._n, self._n)'], {}), '((self._n, self._n))\n', (730, 750), False, 'import numpy\n'), ((2083, 2109), 'os.path.expanduser', 'os.path.expanduser', (['f_name'], {}), '(f_name)\n', (2101, 2109), False, 'import os\n'), ((2138, 2150), 'csb.io.load', 'load', (['f_name'], {}), '(f_name)\n', (2142, 2150), False, 'from csb.io import load\n'), ((2675, 2696), 'numpy.arange', 'numpy.arange', (['self._n'], {}), '(self._n)\n', (2687, 2696), False, 'import numpy\n'), ((3329, 3371), 'numpy.reshape', 'numpy.reshape', (['coef', '(4, self._n, self._n)'], {}), '(coef, (4, self._n, self._n))\n', (3342, 3371), False, 'import numpy\n'), ((3503, 3556), 'numpy.array', 'numpy.array', (['[self._cc, self._ss, self._cs, self._sc]'], {}), '([self._cc, self._ss, self._cs, self._sc])\n', (3514, 3556), False, 'import numpy\n'), ((4536, 4561), 'numpy.add.outer', 'add.outer', (['(k ** 2)', '(k ** 2)'], {}), '(k ** 2, k ** 2)\n', (4545, 4561), False, 'from numpy import add\n'), ((5024, 5088), 'scipy.integrate.dblquad', 'dblquad', (['self.prob', '(0.0)', '(2 * pi)', '(lambda x: 0.0)', '(lambda x: 2 * pi)'], {}), '(self.prob, 0.0, 2 * pi, lambda x: 0.0, lambda x: 2 * pi)\n', (5031, 5088), False, 'from scipy.integrate import dblquad\n'), ((6452, 6476), 'numpy.linspace', 'linspace', (['(0.0)', '(2 * pi)', 'n'], {}), '(0.0, 2 * pi, n)\n', (6460, 6476), False, 'from numpy import pi, linspace, max\n'), ((6558, 6564), 'numpy.max', 'max', (['f'], {}), '(f)\n', (6561, 6564), False, 'from numpy import pi, linspace, max\n'), ((8141, 8158), 'numpy.array', 'numpy.array', (['data'], {}), '(data)\n', (8152, 8158), False, 'import numpy\n'), ((8600, 8618), 'numpy.array', 'numpy.array', (['value'], {}), '(value)\n', (8611, 8618), False, 'import numpy\n'), ((9201, 9237), 'numpy.sum', 'sum', (['(self._stats[0] * self.model._cc)'], {}), '(self._stats[0] * self.model._cc)\n', (9204, 9237), False, 'from numpy import sum\n'), ((9250, 9286), 'numpy.sum', 'sum', (['(self._stats[1] * self.model._ss)'], {}), '(self._stats[1] * self.model._ss)\n', (9253, 9286), False, 'from numpy import sum\n'), ((9299, 9335), 'numpy.sum', 'sum', (['(self._stats[2] * self.model._cs)'], {}), '(self._stats[2] * self.model._cs)\n', (9302, 9335), False, 'from numpy import sum\n'), ((9348, 9384), 'numpy.sum', 'sum', (['(self._stats[3] * self.model._sc)'], {}), '(self._stats[3] * self.model._sc)\n', (9351, 9384), False, 'from numpy import sum\n'), ((5393, 5425), 'numpy.linspace', 'linspace', (['(0.0)', '(2 * pi)', '(2 * n + 1)'], {}), '(0.0, 2 * pi, 2 * n + 1)\n', (5401, 5425), False, 'from numpy import pi, linspace, max\n'), ((5519, 5525), 'numpy.max', 'max', (['f'], {}), '(f)\n', (5522, 5525), False, 'from numpy import pi, linspace, max\n'), ((7028, 7057), 'numpy.multiply.outer', 'multiply.outer', (['k', 'data[:, 0]'], {}), '(k, data[:, 0])\n', (7042, 7057), False, 'from numpy import cos, sin, dot, multiply\n'), ((7076, 7105), 'numpy.multiply.outer', 'multiply.outer', (['k', 'data[:, 0]'], {}), '(k, data[:, 0])\n', (7090, 7105), False, 'from numpy import cos, sin, dot, multiply\n'), ((7124, 7153), 'numpy.multiply.outer', 'multiply.outer', (['k', 'data[:, 1]'], {}), '(k, data[:, 1])\n', (7138, 7153), False, 'from numpy import cos, sin, dot, multiply\n'), ((7172, 7201), 'numpy.multiply.outer', 'multiply.outer', (['k', 'data[:, 1]'], {}), '(k, data[:, 1])\n', (7186, 7201), False, 'from numpy import cos, sin, dot, multiply\n'), ((7219, 7232), 'numpy.dot', 'dot', (['cx', 'cy.T'], {}), '(cx, cy.T)\n', (7222, 7232), False, 'from numpy import cos, sin, dot, multiply\n'), ((7234, 7247), 'numpy.dot', 'dot', (['sx', 'sy.T'], {}), '(sx, sy.T)\n', (7237, 7247), False, 'from numpy import cos, sin, dot, multiply\n'), ((7249, 7262), 'numpy.dot', 'dot', (['cx', 'sy.T'], {}), '(cx, sy.T)\n', (7252, 7262), False, 'from numpy import cos, sin, dot, multiply\n'), ((7264, 7277), 'numpy.dot', 'dot', (['sx', 'cy.T'], {}), '(sx, cy.T)\n', (7267, 7277), False, 'from numpy import cos, sin, dot, multiply\n'), ((3920, 3940), 'numpy.multiply.outer', 'multiply.outer', (['k', 'x'], {}), '(k, x)\n', (3934, 3940), False, 'from numpy import cos, sin, dot, multiply\n'), ((3947, 3967), 'numpy.multiply.outer', 'multiply.outer', (['k', 'x'], {}), '(k, x)\n', (3961, 3967), False, 'from numpy import cos, sin, dot, multiply\n'), ((4275, 4294), 'numpy.dot', 'dot', (['sx.T', 'self._ss'], {}), '(sx.T, self._ss)\n', (4278, 4294), False, 'from numpy import cos, sin, dot, multiply\n'), ((4580, 4604), 'numpy.random.standard_normal', 'standard_normal', (['k.shape'], {}), '(k.shape)\n', (4595, 4604), False, 'from numpy.random import standard_normal\n'), ((5577, 5583), 'csb.numeric.exp', 'exp', (['f'], {}), '(f)\n', (5580, 5583), False, 'from csb.numeric import log, exp\n'), ((5745, 5769), 'numpy.linspace', 'linspace', (['(0.0)', '(2 * pi)', 'n'], {}), '(0.0, 2 * pi, n)\n', (5753, 5769), False, 'from numpy import pi, linspace, max\n'), ((5863, 5869), 'numpy.max', 'max', (['f'], {}), '(f)\n', (5866, 5869), False, 'from numpy import pi, linspace, max\n'), ((6632, 6639), 'csb.numeric.log', 'log', (['dx'], {}), '(dx)\n', (6635, 6639), False, 'from csb.numeric import log, exp\n'), ((4020, 4040), 'numpy.multiply.outer', 'multiply.outer', (['k', 'y'], {}), '(k, y)\n', (4034, 4040), False, 'from numpy import cos, sin, dot, multiply\n'), ((4047, 4067), 'numpy.multiply.outer', 'multiply.outer', (['k', 'y'], {}), '(k, y)\n', (4061, 4067), False, 'from numpy import cos, sin, dot, multiply\n'), ((4227, 4246), 'numpy.dot', 'dot', (['sx.T', 'self._sc'], {}), '(sx.T, self._sc)\n', (4230, 4246), False, 'from numpy import cos, sin, dot, multiply\n'), ((5604, 5610), 'csb.numeric.log', 'log', (['I'], {}), '(I)\n', (5607, 5610), False, 'from csb.numeric import log, exp\n'), ((5625, 5632), 'csb.numeric.log', 'log', (['dx'], {}), '(dx)\n', (5628, 5632), False, 'from csb.numeric import log, exp\n'), ((5924, 5930), 'csb.numeric.exp', 'exp', (['f'], {}), '(f)\n', (5927, 5930), False, 'from csb.numeric import log, exp\n'), ((6684, 6698), 'csb.numeric.exp', 'exp', (['(f - f_max)'], {}), '(f - f_max)\n', (6687, 6698), False, 'from csb.numeric import log, exp\n'), ((1330, 1356), 'os.path.expanduser', 'os.path.expanduser', (['f_name'], {}), '(f_name)\n', (1348, 1356), False, 'import os\n'), ((2246, 2254), 'numpy.array', 'array', (['a'], {}), '(a)\n', (2251, 2254), False, 'from numpy import reshape, array\n'), ((2317, 2325), 'numpy.array', 'array', (['b'], {}), '(b)\n', (2322, 2325), False, 'from numpy import reshape, array\n'), ((2388, 2396), 'numpy.array', 'array', (['c'], {}), '(c)\n', (2393, 2396), False, 'from numpy import reshape, array\n'), ((2459, 2467), 'numpy.array', 'array', (['d'], {}), '(d)\n', (2464, 2467), False, 'from numpy import reshape, array\n'), ((4131, 4150), 'numpy.dot', 'dot', (['cx.T', 'self._cc'], {}), '(cx.T, self._cc)\n', (4134, 4150), False, 'from numpy import cos, sin, dot, multiply\n'), ((4179, 4198), 'numpy.dot', 'dot', (['cx.T', 'self._cs'], {}), '(cx.T, self._cs)\n', (4182, 4198), False, 'from numpy import cos, sin, dot, multiply\n'), ((5951, 5957), 'csb.numeric.log', 'log', (['I'], {}), '(I)\n', (5954, 5957), False, 'from csb.numeric import log, exp\n'), ((5972, 5979), 'csb.numeric.log', 'log', (['dx'], {}), '(dx)\n', (5975, 5979), False, 'from csb.numeric import log, exp\n'), ((6601, 6615), 'csb.numeric.exp', 'exp', (['(f - f_max)'], {}), '(f - f_max)\n', (6604, 6615), False, 'from csb.numeric import log, exp\n'), ((6744, 6751), 'csb.numeric.log', 'log', (['dx'], {}), '(dx)\n', (6747, 6751), False, 'from csb.numeric import log, exp\n')]
#!/usr/bin/env python """ Calculates a lookup table with optimal switching times for an isolated matrix-type DAB three-phase rectifier. This file calculates a 3D lookup table of relative switching times for an IMDAB3R, which are optimized for minimal conduction losses. In discontinuous conduction mode (DCM) analytical equations for the optimal operating conditions are used and numerical optimization is used in continuous conduction mode (CCM). """ import sys import argparse import time import numpy as np from scipy.optimize import fmin_slsqp import hw_functions as hw from csv_io import export_csv __author__ = "<NAME>" __copyright__ = "Copyright 2018, ETH Zurich" __credits__ = ["<NAME>"] __license__ = "MIT" __version__ = "1.0" __maintainer__ = "<NAME>" __email__ = "<EMAIL>" __status__ = "Production" def solver_to_sw_times(x): d1 = x[0] # space used by the numeric solver d2 = x[1] d_dc = x[2] shift = x[3] shift = np.clip(shift, -0.25, 0.25) # -0.25 ... 0.25 d_dc = np.clip(d_dc, 0, 0.5) # duty cycle of dc-side transformer voltage, 0 ... 0.5 t = [0.5-d1, 0.5-d2, 0, 0] t[2] = 0.5 - (d1/2 + shift + d_dc/2) t[3] = -(d1/2 + shift - d_dc/2) return t def solver_to_sw_times_jac(x, u=None): # jacobian of solver_to_sw_times which maps d1, d2, d_dc and shift to sw_times J1 = np.array( [[ -1, 0, 0, 0], # derivative of s[0] w.r.t to x[0]...x[3] [ 0, -1, 0, 0], [-1/2, 0, -1/2, -1], [-1/2, 0, 1/2, -1]]) return J1 def sw_times_to_solver(s): # transform from switching times to solver coordinate system d1 = 0.5 - s[0] d2 = 0.5 - s[1] shift = -0.5 * (s[2] + s[3] - 0.5 + d1) shift = np.clip(shift,-0.25,0.25) d_dc = 2 * (s[3] + d1 / 2 + shift) d_dc = np.clip(d_dc, 0, 0.5) x = np.zeros(4) x[0] = d1 x[1] = d2 x[2] = d_dc x[3] = shift return x # helper functions for DCM to create switching time vectors for given duty cycles def align_fe(d_1, d_2, d_dc): t_3 = 0.5 - d_1 t = [0.5 - d_1, 0.5 - d_2, t_3, d_dc - 0.5 + t_3] return t def align_re(d_1, d_2, d_dc): t = np.array([0.5 - d_1, 0.5 - d_2, 0.5 - d_dc, 0]) return t # check that obtained switching times achieved the required output current def check_solution(u, t, i_dc_ref): _, _, _, i_dc, q = hw.dab_io_currents(u, t) i_dc_err = (i_dc - i_dc_ref) q_err = q # if possible: normalize if (i_dc_err > 1e-6): i_dc_err = i_dc_err / i_dc_ref q_err = q_err / i_dc_ref ret = 0 if (np.abs(i_dc_err) > 1e-3): print('invalid solution, i_dc_err: ', i_dc_err) ret = ret + 100 if (np.abs(q_err) > 1e-3): print('invalid solution, q_err: ', q_err) ret = ret + 10 return ret # in max output current DCM, there is a u_pn for which both d_1 and d_dc become 0.5 for a given ac voltage (aka grid # voltage angle wt) and achieve no reactive power at the mains input (q=0) # this is the boundary case where the solution switches from aligned rising edges (u_pn lower than equivalent AC # voltage) to falling edge aligned def dcm_max_u_pn_boundary(u): u_ab = u[0] # in sector 1 u_bc = u[1] u_pn = 2 * (u_ab**2 + u_ab*u_bc + u_bc**2) / (2*u_ab + u_bc) return u_pn # calculates max dc output current possible with DCM for given voltages u def dcm_i_dc_max(u): u_ab = u[0] # in sector 1 u_bc = u[1] u_pn = u[2] if u_pn < dcm_max_u_pn_boundary(u): if u_bc < 1e-4: # wt = 0, calculate d_2 required to draw the same charge from phase a and phase b (to achieve q=0) uac = u_ab + u_bc # by definition of the three-phase voltages d_dc = 0.5 d_1 = d_dc * u_pn / uac # this is 1/2 - t1 # this case is simple: only during 0 < t < d_1 we are connected to the mains and the transformer # current rises linearly (due to d_dc = 0.5 the dc voltage must be on all the time) # therefore we just have to make d_2 so long that the area of the current triangle is split in half: d_2 = np.sqrt(0.5) * d_1 t = align_re(d_1, d_2, d_dc) else: # analytic solution found by mathematica, works fine from 0 < wt <= 30deg det = (u_bc**2)*(u_ab + 2*u_bc)*(u_ab + u_bc - u_pn)*(u_pn**2)*(2*(u_ab**2 + u_ab*u_bc + u_bc**2) - (2*u_ab + u_bc)*u_pn) t1 = (u_ab*(u_ab+u_bc-u_pn)*(2*(u_ab**2 + u_ab*u_bc + u_bc**2) - (2*u_ab + u_bc)*u_pn) + np.sqrt(det)) / (4*u_ab*(u_ab+u_bc)*(u_ab**2+u_ab*u_bc+u_bc**2) - 2*(u_ab-u_bc)*(2*u_ab**2+3*u_ab*u_bc+2*u_bc**2)*u_pn) x = (u_pn/(1-2*t1) - u_ab) / u_bc # that's d_2 / d_1, this ensures volt-sec balance t2 = 0.5 - x*(0.5-t1) t = [t1, t2, 0, 0] else: # analytic solution found by mathematica det =(u_ab**2 - u_bc**2)*(u_ab - u_pn)*u_pn*(2*(u_ab**2 + u_ab*u_bc + u_bc**2) - (2*u_ab + u_bc)*u_pn) t2 = (-(u_ab**2)*u_bc+(u_bc**3)-np.sqrt(det))/(2*(u_bc**2)*(-u_ab+u_bc) + 2*(2*(u_ab**2)+u_bc**2)*u_pn - 2*(2*u_ab+u_bc)*(u_pn**2)) t4 = -1/2 + (u_ab/2 + u_bc*(1/2-t2)) / u_pn # ensure volt sec balance t = [0, t2, 0, t4] _, _, _, i_dc, _ = hw.dab_io_currents(u, t) return [t, i_dc] # check if DCM can be used to the achieve an output current of i_dc_ref at operating point u def check_dcm(u, i_dc_ref, do_print=False): # calc max output current for discontinuous conduction mode (DCM) for the given voltages t_opt, i_dc_dcm_max = dcm_i_dc_max(u) if do_print: print('i_dc_dcm_max: ', i_dc_dcm_max) # check if this output current should be realized by DCM if i_dc_ref > i_dc_dcm_max: return None if do_print: print('using DCM solution') # the requested current is achievable by TCM, so we use this solution as it is the one with the lowest rms # transformer current k = np.sqrt(i_dc_ref / i_dc_dcm_max) # scaling factor for the three duty cycles of u_p and u_s # extract duty cycles from switching times calculated for max output current d_1 = 0.5 - t_opt[0] d_2 = 0.5 - t_opt[1] d_dc = 0.5 - t_opt[2] + t_opt[3] is_re_aligned = (t_opt[3] == 0) # If t_4 (t[3]) is 0 the rising edges of pri and sec voltage are aligned # apply scaling factor and re-create switching times d_1 = d_1 * k d_2 = d_2 * k d_dc = d_dc * k if is_re_aligned: t_opt = align_re(d_1, d_2, d_dc) else: t_opt = align_fe(d_1, d_2, d_dc) return t_opt # derive optimal switching times in CCM for given voltages u with an output current i_dc_ref def calc_ccm(u, i_dc_ref, i_dc_nom, t0, do_print=False): # objective function def obj(x): s = solver_to_sw_times(x) # note: Imperfections result from the fact that we can only consider an finite amount of harmonics. To avoid # problems with the numeric solver we select a rather high n here as the computational burden is low. i_rms_sqr, _, _ = hw.rms_current_harm(u, s, n=200) f = (i_rms_sqr / (i_dc_nom ** 2)) return f # gradient of the objective def gradient(x): t = solver_to_sw_times(x) J = solver_to_sw_times_jac(x) di_dt = hw.rms_current_grad(u, t, n=200) res = np.dot(di_dt, J) / (i_dc_nom ** 2) return res # equality constraint functions: demanded dc output current (ie active power) and reactive power def eqcons(x): t = solver_to_sw_times(x) _, _, _, i_dc, q = hw.dab_io_currents(u, t) i_dc_err = (i_dc - i_dc_ref) / i_dc_nom q_err = q / i_dc_nom return [i_dc_err, q_err] # inequality constraints: ensure ZVS def ieqcons(x): s = solver_to_sw_times(x) i, _ = hw.switched_current(u, s) return np.array(i) / i_dc_nom # pos values are ZVS, which is what the inequality constraints ensure x0 = sw_times_to_solver(t0) b = [(0, 0.5), (0, 0.5), (0, 0.5), (-0.24, 0.24)] # bounds if do_print: iprint = 1 else: iprint = 0 # call the solver opt_x, fx, its, i_mode, s_mode = fmin_slsqp(obj, x0, f_eqcons=eqcons, fprime=gradient, f_ieqcons=ieqcons, bounds=b, full_output=True, iter=1000, iprint=iprint) opt_s = solver_to_sw_times(opt_x) if do_print or i_mode != 0: print('opt terminated in {0:} iterations with {1:}: {2:} '.format(its, i_mode, s_mode)) eqc = eqcons(opt_x) ieqc = ieqcons(opt_x) if (np.max(np.abs((eqc))) > 1e-3) or (np.min(ieqc) < -1e-6): i_mode = 100 print('Constraint violation detected: eq cons={0:} ieq cons = {1:}'.format(eqc, ieqc)) return [opt_s, i_mode] # i_mode is zero on success or positive otherwise # Maximum output current in Triangular Current Mode (TCM) of a conventional dc/dc DAB according to # KRISMER AND KOLAR: CLOSED FORM SOLUTION FOR MINIMUM CONDUCTION LOSS MODULATION OF DAB CONVERTERS # in IEEE Transactions on Power Electronics, vol. 27, no. 1, pp. 174-188, Jan. 2012 # https://doi.org/10.1109/TPEL.2011.2157976 # Note: TCM in a conventional DAB is like DCM in the IMDAB3R def krismer_i_dc_tcm_max(u): # check that we are either at wt=0 or wt=30deg, otherwise we can't operate like a conventional DAB assert ((np.abs(u[0] - u[1]) < 1e-6) or (np.abs(u[1]) <= 1e-6)) # abbreviation, assuming mains voltage in sector 1 u_ac = u[0] + u[1] u_pn = u[2] # corner case: with 0 output voltage we cannot operate in TCM (there is no way to control the current with the # secondary side voltage) if u_pn < 1e-6: return 0 # normalized quantities in Krimer's notation v_ref = u_ac v_A = np.min([u_ac, u_pn]) / v_ref v_B = np.max([u_ac, u_pn]) / v_ref # calc max power for which we use triangular current mode (ZCS) p_tcm_max = np.pi / 2 * v_A ** 2 * (v_B - v_A) / v_B # rescale back to the dc output current in our notation i_dc_tcm_max = p_tcm_max * v_ref**2 / (2 * np.pi * u_pn) return i_dc_tcm_max # calc optimal switching times according to # KRISMER AND KOLAR: CLOSED FORM SOLUTION FOR MINIMUM CONDUCTION LOSS MODULATION OF DAB CONVERTERS # in IEEE Transactions on Power Electronics, vol. 27, no. 1, pp. 174-188, Jan. 2012 # https://doi.org/10.1109/TPEL.2011.2157976 def krismer(u, i_dc_ref): # abbreviation, assuming mains voltage in sector 1 u_ac = u[0] + u[1] u_pn = u[2] # check that we are at wt=30deg, otherwise we can't operate like a conventional DAB # Note: For wt=0 the transformer current looks the same, however, we need to determine an additional # duty cycle d_2 (switching time t_2) for phase b. Even though this does not change the shape of the current (as # u_bc is zero) changing d_2 will result in different reactive power and a function solver is required) assert (np.abs(u[0] - u[1]) < 1e-6) # normalized quantities according to Krimer's notation v_ref = u_ac v_A = np.min([u_ac, u_pn]) / v_ref v_B = np.max([u_ac, u_pn]) / v_ref p = u_pn * i_dc_ref * 2 * np.pi / (v_ref**2) # calc max power for which we use triangular current mode (discontinuous conduction mode) p_tcm_max = np.pi/2 * v_A**2 * (v_B-v_A) / v_B if (p <= p_tcm_max) and (v_A != v_B): if v_A < 0.001: # we have no output voltage hence output power is 0 for any output current so we cannot use krismers eq. # however this is trivial, we always operate at max phase shift and create the required transformer current # amplitude d_a = 0.5 # get as much current to secondary as we can phi = 0.25 # i.e. 90deg d_b = 0.5 - np.sqrt(0.25 - 2*i_dc_ref) assert (d_b <= 0.5) # if this fails we demanded too much current # print('I0, d_b: ', d_b) else: # standard case as considered by krismer phi = np.pi * np.sqrt((v_B - v_A) * p/np.pi / (2 * v_A**2 * v_B)) d_a = phi / np.pi * v_B / (v_B - v_A) d_b = phi / np.pi * v_A / (v_B - v_A) phi = phi / (2*np.pi) # we use 0..1 for 0..2pi # print('TCM, phi: ',phi) else: # try OTM, equations are copy/paste from the paper mentioned above e1 = -(2*v_A**2 + v_B**2)/(v_A**2 + v_B**2) e2 = (v_A**3*v_B + p/np.pi * (v_A**2 + v_B**2)) / (v_A**3 * v_B + v_A * v_B**3) e3 = ((8 * v_A**7 * v_B**5) - (64 * (p/np.pi)**3 * (v_A**2 + v_B**2)**3) - (p/np.pi * v_A**4 * v_B**2 * (4*v_A**2 + v_B**2) * (4*v_A**2 + 13*v_B**2)) + (16 * (p/np.pi)**2 * v_A * (v_A**2 + v_B**2)**2 * (4*v_A**2*v_B + v_B**3)) ) e4 = ((8 * v_A**9 * v_B**3) - ( 8 * (p/np.pi)**3 * (8*v_A**2 - v_B**2) * (v_A**2 + v_B**2)**2 ) - (12 * p/np.pi * v_A**6 * v_B**2 * (4*v_A**2 + v_B**2) ) + ( 3 * (p/np.pi)**2 * v_A**3 * v_B * (4*v_A**2 + v_B**2) * (8*v_A**2 + 5*v_B**2) ) + ((3*p/np.pi)**1.5 * v_A * v_B**2 * np.sqrt(e3)) ) e5 = ((2 * v_A**6 * v_B**2 + (2 * p/np.pi * (4*v_A**2 + v_B**2) * (p/np.pi * (v_A**2 + v_B**2) - v_A**3 * v_B) )) / (3 * v_A * v_B * (v_A**2 + v_B**2) * e4**(1/3.0)) ) e6 = ((4 * (v_A**3*v_B**2 + 2*v_A**5) + 4 * p/np.pi * (v_A**2*v_B + v_B**3)) / (v_A * (v_A**2 + v_B**2)**2)) e7 = ( (e4**(1/3.0) / (6 * v_A**3 * v_B + 6 * v_A * v_B**3) ) + (e1**2 / 4) - (2*e2 / 3) + e5) e8 = 0.25 * ( (-e1**3 - e6)/np.sqrt(e7) + 3*e1**2 - 8*e2 - 4*e7 ) d_a = 0.5 d_b = 0.25 * (2*np.sqrt(e7) - 2*np.sqrt(e8) - e1) if (d_b <= 0.5): # print('OTM, d_b: ', d_b) # unlike krismer's our phi is 0..1 for 0..360deg, he uses 0..2pi phi = 0.5 * (0.5 - np.sqrt(d_b*(1-d_b) - p/(np.pi*v_A*v_B) )) # print('OTM, phi: ', phi) else: # OTM did not yield a valid solution, so use phase shift modulation d_a = 0.5 d_b = 0.5 phi = 0.5 * (0.5 - np.sqrt(0.25 - p/(np.pi*v_A*v_B))) # print('CPM, phi: ', phi) # now transform the duty cycles and phase shifts back to our switching times if u_pn < u_ac: t_opt = [0.5 - d_b, 0.5 - d_b, 0, 0] # by def. u1 and u2 switch at the same time t_opt[3] = -0.5 * (2 * phi - t_opt[0] - d_a + 0.5) t_opt[2] = -d_a + 0.5 + t_opt[3] else : t_opt = [0.5 - d_a, 0.5 - d_a, 0, 0] # by def. u1 and u2 switch at the same time t_opt[3] = -0.5 * (2 * phi - t_opt[0] - d_b + 0.5) t_opt[2] = -d_b + 0.5 + t_opt[3] return t_opt # # i_dc_ref def calc_t_opt(u, i_dc_ref, i_dc_nom, t0, do_print=True): """Calculate optimal (min. rms current) switching times t_opt for given operating conditions :param u: AC and DC voltages :param i_dc_ref: requested dc output current, f*L = 1 is assumed :param i_dc_nom: normalization for i_dc_ref (to improve convergence of numerical solver) :param t0: initial conditions for numerical solver :param do_print: set to true for debug output :return: [t_opt, mode]: t_opt - array with rel switching times, mode: error code (0 = success) """ if i_dc_ref < 0.001: # 0 output current required -> trivial solution is to produce no transformer voltages t_opt = [0.5, 0.5, 0.5, 0] return [t_opt, 0] if u[2] <= 0.01: # no ouput voltage, trivial solution: use max duty cycle (0.5) and phase shift (0.25) for secondary # and the same duty cycles d_1 and d_2 on the primary, which will lead to mains input currents # with 0 amplitude d_1 = 0.5 - np.sqrt(0.25 - 2 * i_dc_ref) # select duty cycle to create correct transformer current d_2 = d_1 # switch both u_ab and u_bc at the same time, this leads to 0 power transfer between them d_dc = 0.5 shift = 0.25 t_opt = solver_to_sw_times([d_1, d_2, d_dc, shift]) return [t_opt, 0] if np.abs(u[0] - u[1]) < 1e-6: # u_ab and u_bc are equal, i.e. we can use the analytic solution for a conventional DAB t_opt = krismer(u, i_dc_ref) return [t_opt, 0] # if possible, try to use DCM t_opt = check_dcm(u, i_dc_ref, do_print) if t_opt is not None: return [t_opt, 0] # i_dc_ref is too high for DCM, so use the numeric optimizer for CCM return calc_ccm(u, i_dc_ref, i_dc_nom, t0, do_print) def calc_table(resolution, i_dc_max, u_pn_max, lut_fn, log_fn=None): """ Calculate 3D lookup table (LUT) @params: resolution - Required : Number of sampling points in each dimension (int) i_dc_max - Required : Highest normalized dc current in final LUT (float) u_pn_max - Required : Highest normalized output voltage in final LUT (float) lut_fn - Required : File name were LUT will be stored log_fn - Optional : Log file name, stdout if this is None """ grid_res = [resolution, resolution, resolution] if log_fn is not None: log_file = open(log_fn, mode='w') else: log_file = sys.stderr i_dc_range = np.linspace(0, i_dc_max, num=grid_res[0]) u_pn_range = np.linspace(0, u_pn_max, num=grid_res[1]) u_bc_range = np.linspace(0, 0.5, num=grid_res[2]) opt_mode = np.zeros(grid_res) # optimizer return code (error code, 0 means success) grid_res.append(4) sw_times = np.zeros(grid_res) n_not_solved = 0 log_file.write('resolution: {}\n'.format(resolution)) log_file.write('i_dc_max: {}\n'.format(i_dc_max)) log_file.write('u_pn_max: {}\n'.format(u_pn_max)) time.clock() total_pts = len(i_dc_range) * len(u_pn_range) * len(u_bc_range) pts_done = 0 # sweep the 3D grid, u_bc must be the inner most loop for convergence reasons for (k1, i_dc) in enumerate(i_dc_range): log_file.write('---------------------\n') for (k2, u_pn) in enumerate(u_pn_range): log_file.write('--------\n') log_file.write('k1={0:} k2={1:}\n'.format(k1,k2)) last_t_opt = [] # traverse starting with u2=05 for which we operate like a conventional DAB were we have a closed # analytic solution. This is then used as starting point for the next point for (k3, u_bc) in reversed(list(enumerate(u_bc_range))): u_ac = 1 # this is our normalization ref voltage u_ab = u_ac - u_bc u = [u_ab, u_bc, u_pn] log_file.write('u={0:} i_dc={1:.7f}\n'.format(u, i_dc)) t_opt, m = calc_t_opt(u, i_dc, i_dc, last_t_opt, do_print=False) if m == 0: # double check the validity of the obtained solution m = check_solution(u, t_opt, i_dc) opt_mode[k1, k2, k3] = m sw_times[k1, k2, k3, 0:4] = t_opt if m != 0: n_not_solved += 1 log_file.write('^ not solved\n') # mark point in table so the user can investigate the problem else : last_t_opt = t_opt # keep a copy of our initial conditions # show a progress bar in the terminal pts_done = pts_done + 1 suffix = 'elapsed: {}s'.format(int(time.clock())) print_progress(pts_done, total_pts, prefix='Progress', suffix=suffix, decimals=1, bar_length=80) log_file.write('\nnumber of points not solved: {}\n'.format(n_not_solved)) if log_fn is not None: log_file.close() sys.stderr.write('\nnumber of points not solved: {}\n'.format(n_not_solved)) # write LUT data to file export_csv(lut_fn, grid_res, i_dc_range, u_pn_range, u_bc_range, sw_times) # Snippet taken from: https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a # Print iterations progress def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100): """ Call in a loop to create terminal progress bar @params: iteration - Required : current iteration (Int) total - Required : total iterations (Int) prefix - Optional : prefix string (Str) suffix - Optional : suffix string (Str) decimals - Optional : positive number of decimals in percent complete (Int) bar_length - Optional : character length of bar (Int) """ str_format = "{0:." + str(decimals) + "f}" percents = str_format.format(100 * (iteration / float(total))) filled_length = int(round(bar_length * iteration / float(total))) bar = '█' * filled_length + '-' * (bar_length - filled_length) # send output to stderr instead of stdout as stdout is can be used as log file #sys.stderr.write('\x1b[2K') # should clear the last display line but does not work for some reason sys.stderr.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)), if iteration == total: sys.stdout.write('\n') sys.stdout.flush() if __name__ == "__main__": parser = argparse.ArgumentParser(description='LUT calculation for IMDAB3R converters') parser.add_argument('-o', '--output', type=str, help='output file name', default='recent.csv') parser.add_argument('-l', '--log', type=str, help='log file name, goes to stdout if no file is given') parser.add_argument('-n', type=int, help='LUT resolution (no of sampling points per dimension).', default=30) parser.add_argument('-i-dc', type=float, help='Max. normalized output current', default=0.07) parser.add_argument('-u-pn', type=float, help='Max. normalized output voltage w.r.t. primary', default=1.33) args = parser.parse_args() resolution = int(args.n) i_dc_max = args.i_dc u_pn_max = args.u_pn lut_fn = args.output log_fn = args.log if i_dc_max > 0.12: print('i_dc values above 0.12 are not feasible, limiting range of LUT') i_dc_max = 0.12 calc_table(resolution, i_dc_max, u_pn_max, lut_fn, log_fn)
[ "sys.stdout.write", "numpy.abs", "csv_io.export_csv", "argparse.ArgumentParser", "hw_functions.rms_current_grad", "numpy.clip", "hw_functions.dab_io_currents", "sys.stdout.flush", "scipy.optimize.fmin_slsqp", "time.clock", "numpy.max", "numpy.linspace", "hw_functions.rms_current_harm", "numpy.min", "numpy.dot", "hw_functions.switched_current", "numpy.zeros", "numpy.array", "sys.stderr.write", "numpy.sqrt" ]
[((955, 982), 'numpy.clip', 'np.clip', (['shift', '(-0.25)', '(0.25)'], {}), '(shift, -0.25, 0.25)\n', (962, 982), True, 'import numpy as np\n'), ((1012, 1033), 'numpy.clip', 'np.clip', (['d_dc', '(0)', '(0.5)'], {}), '(d_dc, 0, 0.5)\n', (1019, 1033), True, 'import numpy as np\n'), ((1345, 1438), 'numpy.array', 'np.array', (['[[-1, 0, 0, 0], [0, -1, 0, 0], [-1 / 2, 0, -1 / 2, -1], [-1 / 2, 0, 1 / 2, -1]]'], {}), '([[-1, 0, 0, 0], [0, -1, 0, 0], [-1 / 2, 0, -1 / 2, -1], [-1 / 2, 0,\n 1 / 2, -1]])\n', (1353, 1438), True, 'import numpy as np\n'), ((1727, 1754), 'numpy.clip', 'np.clip', (['shift', '(-0.25)', '(0.25)'], {}), '(shift, -0.25, 0.25)\n', (1734, 1754), True, 'import numpy as np\n'), ((1803, 1824), 'numpy.clip', 'np.clip', (['d_dc', '(0)', '(0.5)'], {}), '(d_dc, 0, 0.5)\n', (1810, 1824), True, 'import numpy as np\n'), ((1834, 1845), 'numpy.zeros', 'np.zeros', (['(4)'], {}), '(4)\n', (1842, 1845), True, 'import numpy as np\n'), ((2161, 2208), 'numpy.array', 'np.array', (['[0.5 - d_1, 0.5 - d_2, 0.5 - d_dc, 0]'], {}), '([0.5 - d_1, 0.5 - d_2, 0.5 - d_dc, 0])\n', (2169, 2208), True, 'import numpy as np\n'), ((2358, 2382), 'hw_functions.dab_io_currents', 'hw.dab_io_currents', (['u', 't'], {}), '(u, t)\n', (2376, 2382), True, 'import hw_functions as hw\n'), ((5429, 5453), 'hw_functions.dab_io_currents', 'hw.dab_io_currents', (['u', 't'], {}), '(u, t)\n', (5447, 5453), True, 'import hw_functions as hw\n'), ((6125, 6157), 'numpy.sqrt', 'np.sqrt', (['(i_dc_ref / i_dc_dcm_max)'], {}), '(i_dc_ref / i_dc_dcm_max)\n', (6132, 6157), True, 'import numpy as np\n'), ((8343, 8473), 'scipy.optimize.fmin_slsqp', 'fmin_slsqp', (['obj', 'x0'], {'f_eqcons': 'eqcons', 'fprime': 'gradient', 'f_ieqcons': 'ieqcons', 'bounds': 'b', 'full_output': '(True)', 'iter': '(1000)', 'iprint': 'iprint'}), '(obj, x0, f_eqcons=eqcons, fprime=gradient, f_ieqcons=ieqcons,\n bounds=b, full_output=True, iter=1000, iprint=iprint)\n', (8353, 8473), False, 'from scipy.optimize import fmin_slsqp\n'), ((17484, 17525), 'numpy.linspace', 'np.linspace', (['(0)', 'i_dc_max'], {'num': 'grid_res[0]'}), '(0, i_dc_max, num=grid_res[0])\n', (17495, 17525), True, 'import numpy as np\n'), ((17543, 17584), 'numpy.linspace', 'np.linspace', (['(0)', 'u_pn_max'], {'num': 'grid_res[1]'}), '(0, u_pn_max, num=grid_res[1])\n', (17554, 17584), True, 'import numpy as np\n'), ((17602, 17638), 'numpy.linspace', 'np.linspace', (['(0)', '(0.5)'], {'num': 'grid_res[2]'}), '(0, 0.5, num=grid_res[2])\n', (17613, 17638), True, 'import numpy as np\n'), ((17655, 17673), 'numpy.zeros', 'np.zeros', (['grid_res'], {}), '(grid_res)\n', (17663, 17673), True, 'import numpy as np\n'), ((17768, 17786), 'numpy.zeros', 'np.zeros', (['grid_res'], {}), '(grid_res)\n', (17776, 17786), True, 'import numpy as np\n'), ((17980, 17992), 'time.clock', 'time.clock', ([], {}), '()\n', (17990, 17992), False, 'import time\n'), ((20075, 20149), 'csv_io.export_csv', 'export_csv', (['lut_fn', 'grid_res', 'i_dc_range', 'u_pn_range', 'u_bc_range', 'sw_times'], {}), '(lut_fn, grid_res, i_dc_range, u_pn_range, u_bc_range, sw_times)\n', (20085, 20149), False, 'from csv_io import export_csv\n'), ((21397, 21415), 'sys.stdout.flush', 'sys.stdout.flush', ([], {}), '()\n', (21413, 21415), False, 'import sys\n'), ((21459, 21536), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'description': '"""LUT calculation for IMDAB3R converters"""'}), "(description='LUT calculation for IMDAB3R converters')\n", (21482, 21536), False, 'import argparse\n'), ((2577, 2593), 'numpy.abs', 'np.abs', (['i_dc_err'], {}), '(i_dc_err)\n', (2583, 2593), True, 'import numpy as np\n'), ((2691, 2704), 'numpy.abs', 'np.abs', (['q_err'], {}), '(q_err)\n', (2697, 2704), True, 'import numpy as np\n'), ((7221, 7253), 'hw_functions.rms_current_harm', 'hw.rms_current_harm', (['u', 's'], {'n': '(200)'}), '(u, s, n=200)\n', (7240, 7253), True, 'import hw_functions as hw\n'), ((7455, 7487), 'hw_functions.rms_current_grad', 'hw.rms_current_grad', (['u', 't'], {'n': '(200)'}), '(u, t, n=200)\n', (7474, 7487), True, 'import hw_functions as hw\n'), ((7739, 7763), 'hw_functions.dab_io_currents', 'hw.dab_io_currents', (['u', 't'], {}), '(u, t)\n', (7757, 7763), True, 'import hw_functions as hw\n'), ((7985, 8010), 'hw_functions.switched_current', 'hw.switched_current', (['u', 's'], {}), '(u, s)\n', (8004, 8010), True, 'import hw_functions as hw\n'), ((9988, 10008), 'numpy.min', 'np.min', (['[u_ac, u_pn]'], {}), '([u_ac, u_pn])\n', (9994, 10008), True, 'import numpy as np\n'), ((10027, 10047), 'numpy.max', 'np.max', (['[u_ac, u_pn]'], {}), '([u_ac, u_pn])\n', (10033, 10047), True, 'import numpy as np\n'), ((11151, 11170), 'numpy.abs', 'np.abs', (['(u[0] - u[1])'], {}), '(u[0] - u[1])\n', (11157, 11170), True, 'import numpy as np\n'), ((11266, 11286), 'numpy.min', 'np.min', (['[u_ac, u_pn]'], {}), '([u_ac, u_pn])\n', (11272, 11286), True, 'import numpy as np\n'), ((11305, 11325), 'numpy.max', 'np.max', (['[u_ac, u_pn]'], {}), '([u_ac, u_pn])\n', (11311, 11325), True, 'import numpy as np\n'), ((16324, 16343), 'numpy.abs', 'np.abs', (['(u[0] - u[1])'], {}), '(u[0] - u[1])\n', (16330, 16343), True, 'import numpy as np\n'), ((21256, 21332), 'sys.stderr.write', 'sys.stderr.write', (["('\\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix))"], {}), "('\\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix))\n", (21272, 21332), False, 'import sys\n'), ((21370, 21392), 'sys.stdout.write', 'sys.stdout.write', (['"""\n"""'], {}), "('\\n')\n", (21386, 21392), False, 'import sys\n'), ((7502, 7518), 'numpy.dot', 'np.dot', (['di_dt', 'J'], {}), '(di_dt, J)\n', (7508, 7518), True, 'import numpy as np\n'), ((8026, 8037), 'numpy.array', 'np.array', (['i'], {}), '(i)\n', (8034, 8037), True, 'import numpy as np\n'), ((8825, 8837), 'numpy.min', 'np.min', (['ieqc'], {}), '(ieqc)\n', (8831, 8837), True, 'import numpy as np\n'), ((9578, 9597), 'numpy.abs', 'np.abs', (['(u[0] - u[1])'], {}), '(u[0] - u[1])\n', (9584, 9597), True, 'import numpy as np\n'), ((9610, 9622), 'numpy.abs', 'np.abs', (['u[1]'], {}), '(u[1])\n', (9616, 9622), True, 'import numpy as np\n'), ((15994, 16022), 'numpy.sqrt', 'np.sqrt', (['(0.25 - 2 * i_dc_ref)'], {}), '(0.25 - 2 * i_dc_ref)\n', (16001, 16022), True, 'import numpy as np\n'), ((4128, 4140), 'numpy.sqrt', 'np.sqrt', (['(0.5)'], {}), '(0.5)\n', (4135, 4140), True, 'import numpy as np\n'), ((5145, 5157), 'numpy.sqrt', 'np.sqrt', (['det'], {}), '(det)\n', (5152, 5157), True, 'import numpy as np\n'), ((8798, 8809), 'numpy.abs', 'np.abs', (['eqc'], {}), '(eqc)\n', (8804, 8809), True, 'import numpy as np\n'), ((11987, 12015), 'numpy.sqrt', 'np.sqrt', (['(0.25 - 2 * i_dc_ref)'], {}), '(0.25 - 2 * i_dc_ref)\n', (11994, 12015), True, 'import numpy as np\n'), ((12222, 12277), 'numpy.sqrt', 'np.sqrt', (['((v_B - v_A) * p / np.pi / (2 * v_A ** 2 * v_B))'], {}), '((v_B - v_A) * p / np.pi / (2 * v_A ** 2 * v_B))\n', (12229, 12277), True, 'import numpy as np\n'), ((13330, 13341), 'numpy.sqrt', 'np.sqrt', (['e3'], {}), '(e3)\n', (13337, 13341), True, 'import numpy as np\n'), ((4617, 4629), 'numpy.sqrt', 'np.sqrt', (['det'], {}), '(det)\n', (4624, 4629), True, 'import numpy as np\n'), ((14120, 14170), 'numpy.sqrt', 'np.sqrt', (['(d_b * (1 - d_b) - p / (np.pi * v_A * v_B))'], {}), '(d_b * (1 - d_b) - p / (np.pi * v_A * v_B))\n', (14127, 14170), True, 'import numpy as np\n'), ((14371, 14410), 'numpy.sqrt', 'np.sqrt', (['(0.25 - p / (np.pi * v_A * v_B))'], {}), '(0.25 - p / (np.pi * v_A * v_B))\n', (14378, 14410), True, 'import numpy as np\n'), ((13913, 13924), 'numpy.sqrt', 'np.sqrt', (['e7'], {}), '(e7)\n', (13920, 13924), True, 'import numpy as np\n'), ((13929, 13940), 'numpy.sqrt', 'np.sqrt', (['e8'], {}), '(e8)\n', (13936, 13940), True, 'import numpy as np\n'), ((19697, 19709), 'time.clock', 'time.clock', ([], {}), '()\n', (19707, 19709), False, 'import time\n'), ((13832, 13843), 'numpy.sqrt', 'np.sqrt', (['e7'], {}), '(e7)\n', (13839, 13843), True, 'import numpy as np\n')]
import unittest import numpy from chainer import cuda from chainer import testing from chainer.testing import attr from chainer import utils class TestWalkerAlias(unittest.TestCase): def setUp(self): self.ps = numpy.array([5, 3, 4, 1, 2], dtype=numpy.int32) self.sampler = utils.WalkerAlias(self.ps) def check_sample(self): counts = numpy.zeros(len(self.ps), numpy.float32) for _ in range(1000): vs = self.sampler.sample((4, 3)) numpy.add.at(counts, cuda.to_cpu(vs), 1) counts /= (1000 * 12) counts *= sum(self.ps) testing.assert_allclose(self.ps, counts, atol=0.1, rtol=0.1) def test_sample_cpu(self): self.check_sample() @attr.gpu def test_sample_gpu(self): self.sampler.to_gpu() self.assertTrue(self.sampler.use_gpu) self.check_sample() @attr.gpu def test_to_cpu(self): self.sampler.to_gpu() self.sampler.to_cpu() self.assertFalse(self.sampler.use_gpu) self.check_sample() testing.run_module(__name__, __file__)
[ "chainer.testing.assert_allclose", "chainer.utils.WalkerAlias", "chainer.cuda.to_cpu", "numpy.array", "chainer.testing.run_module" ]
[((1059, 1097), 'chainer.testing.run_module', 'testing.run_module', (['__name__', '__file__'], {}), '(__name__, __file__)\n', (1077, 1097), False, 'from chainer import testing\n'), ((227, 274), 'numpy.array', 'numpy.array', (['[5, 3, 4, 1, 2]'], {'dtype': 'numpy.int32'}), '([5, 3, 4, 1, 2], dtype=numpy.int32)\n', (238, 274), False, 'import numpy\n'), ((298, 324), 'chainer.utils.WalkerAlias', 'utils.WalkerAlias', (['self.ps'], {}), '(self.ps)\n', (315, 324), False, 'from chainer import utils\n'), ((609, 669), 'chainer.testing.assert_allclose', 'testing.assert_allclose', (['self.ps', 'counts'], {'atol': '(0.1)', 'rtol': '(0.1)'}), '(self.ps, counts, atol=0.1, rtol=0.1)\n', (632, 669), False, 'from chainer import testing\n'), ((520, 535), 'chainer.cuda.to_cpu', 'cuda.to_cpu', (['vs'], {}), '(vs)\n', (531, 535), False, 'from chainer import cuda\n')]
# -*- coding: utf-8 -*- import os, jinja2 import numpy as np import scipy.optimize from ..util import functions as f from ..util import tools, constants # see README for terminology, terminolology, lol class Vertex(): """ point with an index that's used in block and face definition and can output in OpenFOAM format """ def __init__(self, point): self.point = np.array(point) self.mesh_index = None # will be changed in Mesh.prepare_data() def __repr__(self): s = constants.vector_format(self.point) if self.mesh_index is not None: s += " // {}".format(self.mesh_index) return s class Edge(): def __init__(self, index_1, index_2, points): """ an edge is defined by two vertices and points in between; a single point edge is treated as 'arc', more points are treated as 'spline'. passed indexes refer to position in Block.edges[] list; Mesh.prepare_data() will assign actual Vertex objects. """ # indexes in block.edges[] list self.block_index_1 = index_1 self.block_index_2 = index_2 # these will refer to actual Vertex objects after Mesh.prepare_data() self.vertex_1 = None self.vertex_2 = None self.type, self.points = self.get_type(points) @staticmethod def get_type(points): """ returns edge type and a list of points: 'None' for a straight line, 'arc' for a circular arc, 'spline' for a spline """ if points is None: return None, None # if multiple points are given check that they are of correct length points = np.array(points) shape = np.shape(points) if len(shape) == 1: t = 'arc' else: assert len(shape) == 2 for p in points: assert len(p) == 3 t = 'spline' return t, points @property def point_list(self): if self.type == 'arc': return constants.vector_format(self.points) else: return "(" + \ " ".join([constants.vector_format(p) for p in self.points]) + \ ")" @property def is_valid(self): # 'all' spline edges are 'valid' if self.type == 'spline': return True # wedge geometries produce coincident # edges and vertices; drop those if f.norm(self.vertex_1.point - self.vertex_2.point) < constants.tol: return False # if case vertex1, vertex2 and point in between # are collinear, blockMesh will find an arc with # infinite radius and crash. # so, check for collinearity; if the three points # are actually collinear, this edge is redundant and can be # silently dropped OA = self.vertex_1.point OB = self.vertex_2.point OC = self.points # if point C is on the same line as A and B: # OC = OA + k*(OB-OA) AB = OB - OA AC = OC - OA k = f.norm(AC)/f.norm(AB) d = f.norm((OA+AC) - (OA + k*AB)) return d > constants.tol def get_length(self): # TODO: test def curve_length(points): l = 0 for i in range(len(points)-1): l += f.norm(points[i+1] - points[i]) return l if self.type == 'arc': edge_points = np.array([ self.vertex_1.point, self.points, self.vertex_2.point ]) return curve_length(edge_points) elif self.type == 'spline': edge_points = np.concatenate(( [self.vertex_1.point], self.points, [self.vertex_2.point]), axis=0) return curve_length(edge_points) else: raise AttributeError(f"Unknown edge type: {self.type}") def __repr__(self): return "{} {} {} {}".format( self.type, self.vertex_1.mesh_index, self.vertex_2.mesh_index, self.point_list )
[ "numpy.shape", "numpy.array", "numpy.concatenate" ]
[((383, 398), 'numpy.array', 'np.array', (['point'], {}), '(point)\n', (391, 398), True, 'import numpy as np\n'), ((1708, 1724), 'numpy.array', 'np.array', (['points'], {}), '(points)\n', (1716, 1724), True, 'import numpy as np\n'), ((1741, 1757), 'numpy.shape', 'np.shape', (['points'], {}), '(points)\n', (1749, 1757), True, 'import numpy as np\n'), ((3522, 3587), 'numpy.array', 'np.array', (['[self.vertex_1.point, self.points, self.vertex_2.point]'], {}), '([self.vertex_1.point, self.points, self.vertex_2.point])\n', (3530, 3587), True, 'import numpy as np\n'), ((3758, 3845), 'numpy.concatenate', 'np.concatenate', (['([self.vertex_1.point], self.points, [self.vertex_2.point])'], {'axis': '(0)'}), '(([self.vertex_1.point], self.points, [self.vertex_2.point]),\n axis=0)\n', (3772, 3845), True, 'import numpy as np\n')]
import numpy as np from matplotlib import image as mimage from time import time class Timer(object): """A simple timer context-manager, taken from https://blog.usejournal.com/how-to-create-your-own-timing-context-manager-in-python-a0e944b48cf8 """ def __init__(self, description): self.description = description def __enter__(self): self.start = time() def __exit__(self, type, value, traceback): self.end = time() print("{desc}: {time}s".format( desc=self.description, time=(self.end - self.start))) def rgb_to_gray(rgb): """Convert color image to grayscale. Parameters ---------- rgb : ndarray Three dimensional array, last dimension being at least 3 in size. Returns ------- gray: ndarray Grayscale image. """ if len(rgb.shape) < 3: return rgb.squeeze() return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140]) def imread(filename, dtype='float64', force_grayscale=False): """Read a file from disk. Parameters ---------- filename : str Filename on disk. dtype : str, optional Data-type of returned array, by default 'float64' force_grayscale : bool, optional If true, a grayscale image is returned only works if input is rgb, by default False Returns ------- im: ndarray Loaded image. """ im = mimage.imread(filename) if force_grayscale: im = rgb_to_gray(im) im = im.astype(dtype) if dtype == 'float32' or dtype == 'float64': im /= np.max(im) return im def read_txt_matrix(txtf, header=False): """ Reads an matrix encoded in ASCII into memory as numpy matrix. """ return np.asarray([map(float, line.strip().split()) for iline, line in enumerate(open(txtf, 'r').readlines()) if (iline > 0 or not header)])
[ "numpy.dot", "matplotlib.image.imread", "numpy.max", "time.time" ]
[((906, 950), 'numpy.dot', 'np.dot', (['rgb[..., :3]', '[0.2989, 0.587, 0.114]'], {}), '(rgb[..., :3], [0.2989, 0.587, 0.114])\n', (912, 950), True, 'import numpy as np\n'), ((1416, 1439), 'matplotlib.image.imread', 'mimage.imread', (['filename'], {}), '(filename)\n', (1429, 1439), True, 'from matplotlib import image as mimage\n'), ((387, 393), 'time.time', 'time', ([], {}), '()\n', (391, 393), False, 'from time import time\n'), ((462, 468), 'time.time', 'time', ([], {}), '()\n', (466, 468), False, 'from time import time\n'), ((1582, 1592), 'numpy.max', 'np.max', (['im'], {}), '(im)\n', (1588, 1592), True, 'import numpy as np\n')]
import numpy as np import pandas as pd from scipy import interpolate from .Constants import * from .AtomicData import * from .Conversions import * ########################## # Taken from: https://stackoverflow.com/questions/779495/python-access-data-in-package-subdirectory # This imports the file 'PREM500.csv' within the DarkCapPy package so the user doesn't have to. import os this_dir, this_filename = os.path.split(__file__) # this_dir, this_filename = os.path.split(__file__) ########################## # Earth radius and mass ########################## Planet_Path = os.path.join(this_dir, "PREM500_Mod.csv") VelocityDist_Path = os.path.join(this_dir, "EarthDMVelDist.csv") Planet_Radius = 6.371e8 # cm Planet_Mass = 5.972e27 # grams Planet_Life = yr2s(4.5e9) # 4.5 Gyr -> sec ########################## # Sun radius and mass ########################## # Planet_Path = os.path.join(this_dir, "struct_b16_agss09.csv") # Vel_Dist_Path = os.path.join(this_dir, "SunDMVelDist.csv") # Planet_Radius = 69.551e9 # cm # Planet_Mass = 1.989e33 # g # Planet_Life = yr2s(4.5e9) # 4.5 Gyr -> sec # Variables to be used in DarkPhoton.py # 1). radius_List # 2). deltaR_List # 3). escVel2_List # 4). element_List ######################################################## # Data Input # ######################################################## ########################## # Read in Planet Data ########################## Planet_File = pd.read_csv(Planet_Path, delim_whitespace=True, header = 8) Vel_Dist_File = pd.read_csv(Vel_Dist_Path) radius_List = Planet_File['Radius'] * Planet_Radius enclosedMass_List = Planet_File['Mass'] * Planet_Mass element_List = np.asarray(Planet_File.columns[6:-1]) assert len(radius_List) == len(enclosedMass_List), 'Lengths of radius list and enclosed mass list do not match' ########################## # Shell Thickness ########################## def deltaR_Func(radiusList): # Input is a list if radiii values, # output is a list of deltaR values # DeltaR = Next radius - current radius # We define the variable 'radiusListm1' in order to give a deltaRList which has the same length as radiusList radiusListm1 = radiusList[0:len(radiusList)-1] s = [0] # Temporary variable used to obtain deltaRList. Stores radiusList offset by one index. for i in radiusListm1: s.append(i) deltaRList = radiusList[0:len(radiusList)] - s[0:len(s)] return deltaRList ########################## # Shell Mass ########################## def shellMass_Func(totalMassList): shellMass_List = [] bigMass = 0 smallMass = 0 for i in range(0,len(totalMassList)): if i == 0: shellMass_List.append(0) else: bigMass = totalMassList[i] smallMass = totalMassList[i-1] shellMass_List.append(bigMass-smallMass) return shellMass_List ########################## # Shell Density ########################## def shellDensity_Func(shellMass, shellRadius, deltaR): shellDensity = [] for i in range(0,len(shellMass)): shellDensity.append(shellMass[i]/(4*np.pi*shellRadius[i]**2 * deltaR[i])) # Kludge for radius = 0 shellDensity[0] = shellDensity[1] return shellDensity ########################## # Number Density of each element ########################## def numDensity_Func(element): numDensityList = [] for i in range(0,len(shellDensity_List)): mf = Planet_File[str(element)][i] numDensityList.append(mf * g2GeV(shellDensity_List[i]) / amu2GeV(atomicNumbers[element])) return numDensityList ########################## # Escape Velocity ########################## def escVel_Func(index, enclosedMassList, radiusList, deltaRList): G_Newton = 6.674e-11 * 100**3 * (1000)**-1 # in cm^3/(g s^2) c = 3e10 # in cm/s factor = 2.*G_Newton/c**2 # prefactor constant = max(enclosedMassList) / max(radiusList) assert len(enclosedMassList) == len(radiusList), 'Lengths of Enclosed mass list and radius list do not match' assert len(radiusList) == len(deltaRList), 'Lengths of radius list and delta R list do not match' if (index == 0): tempSum = 0 elif (index != 0): tempSum = 0 for i in range(index, len(radiusList)): summand = enclosedMassList[i] * deltaRList[i] / (radiusList[i])**2 tempSum += summand return (factor * (tempSum + constant)) ########################## # Generate all lists ########################## deltaR_List = deltaR_Func(radius_List) shellMass_List = shellMass_Func(enclosedMass_List) shellDensity_List = shellDensity_Func(shellMass_List, radius_List, deltaR_List) assert len(radius_List) == len(deltaR_List) assert len(radius_List) == len(shellMass_List) assert len(radius_List) == len(shellDensity_List) escVel2_List = [] #| Construct an array of escape velocities for i in range(0,len(radius_List)): #| escVel2_List.append(escVel_Func(i, enclosedMass_List, radius_List, deltaR_List)) #| escVel2_List[0] = escVel2_List[1] # Set the i=0 and i=1 escape velocities equal ########################## # DM Velocity Distrubution ########################## velocity_Range_List = Vel_Dist_File['Velocity_Range'] # A list of velocities between 0 and V_gal planet_velocity_List = Vel_Dist_File['VelocityDist_Planet_Frame'] # The DM velocity distrubution in the planet frame ######################## # Interpolate the Velocity Distribution ######################## velRange = velocity_Range_List fCrossVect = planet_velocity_List fCrossInterp = interpolate.interp1d(velRange, fCrossVect, kind ='linear') ########################## # Interpolate # These are intentionally commented out, we don't actually use them in DarkPhoton.py # I'm not sure why I made these but they are here if they are usefull ########################## # Earth_enclosedMassInterp = interpolate.interp1d(radius_List, enclosedMass_List, kind='linear') # Earth_escVel2Interp = interpolate.interp1d(radius_List, escVel2_List, kind='linear') # Earth_densityInterp = interpolate.interp1d(radius_List,Earth_density_List,kind='linear')
[ "pandas.read_csv", "numpy.asarray", "scipy.interpolate.interp1d", "os.path.split", "os.path.join" ]
[((409, 432), 'os.path.split', 'os.path.split', (['__file__'], {}), '(__file__)\n', (422, 432), False, 'import os\n'), ((580, 621), 'os.path.join', 'os.path.join', (['this_dir', '"""PREM500_Mod.csv"""'], {}), "(this_dir, 'PREM500_Mod.csv')\n", (592, 621), False, 'import os\n'), ((642, 686), 'os.path.join', 'os.path.join', (['this_dir', '"""EarthDMVelDist.csv"""'], {}), "(this_dir, 'EarthDMVelDist.csv')\n", (654, 686), False, 'import os\n'), ((1504, 1561), 'pandas.read_csv', 'pd.read_csv', (['Planet_Path'], {'delim_whitespace': '(True)', 'header': '(8)'}), '(Planet_Path, delim_whitespace=True, header=8)\n', (1515, 1561), True, 'import pandas as pd\n'), ((1581, 1607), 'pandas.read_csv', 'pd.read_csv', (['Vel_Dist_Path'], {}), '(Vel_Dist_Path)\n', (1592, 1607), True, 'import pandas as pd\n'), ((1731, 1768), 'numpy.asarray', 'np.asarray', (['Planet_File.columns[6:-1]'], {}), '(Planet_File.columns[6:-1])\n', (1741, 1768), True, 'import numpy as np\n'), ((5675, 5732), 'scipy.interpolate.interp1d', 'interpolate.interp1d', (['velRange', 'fCrossVect'], {'kind': '"""linear"""'}), "(velRange, fCrossVect, kind='linear')\n", (5695, 5732), False, 'from scipy import interpolate\n')]
#!/usr/bin/env python # <examples/doc_nistgauss2.py> import matplotlib.pyplot as plt import numpy as np from lmfit.models import ExponentialModel, GaussianModel dat = np.loadtxt('NIST_Gauss2.dat') x = dat[:, 1] y = dat[:, 0] exp_mod = ExponentialModel(prefix='exp_') gauss1 = GaussianModel(prefix='g1_') gauss2 = GaussianModel(prefix='g2_') def index_of(arrval, value): """return index of array *at or below* value """ if value < min(arrval): return 0 return max(np.where(arrval <= value)[0]) ix1 = index_of(x, 75) ix2 = index_of(x, 135) ix3 = index_of(x, 175) pars1 = exp_mod.guess(y[:ix1], x=x[:ix1]) pars2 = gauss1.guess(y[ix1:ix2], x=x[ix1:ix2]) pars3 = gauss2.guess(y[ix2:ix3], x=x[ix2:ix3]) pars = pars1 + pars2 + pars3 mod = gauss1 + gauss2 + exp_mod out = mod.fit(y, pars, x=x) print(out.fit_report(min_correl=0.5)) plt.plot(x, y, 'b') plt.plot(x, out.init_fit, 'k--') plt.plot(x, out.best_fit, 'r-') # plt.savefig('../doc/_images/models_nistgauss2.png') plt.show() # <end examples/doc_nistgauss2.py>
[ "matplotlib.pyplot.show", "matplotlib.pyplot.plot", "numpy.where", "numpy.loadtxt", "lmfit.models.GaussianModel", "lmfit.models.ExponentialModel" ]
[((170, 199), 'numpy.loadtxt', 'np.loadtxt', (['"""NIST_Gauss2.dat"""'], {}), "('NIST_Gauss2.dat')\n", (180, 199), True, 'import numpy as np\n'), ((239, 270), 'lmfit.models.ExponentialModel', 'ExponentialModel', ([], {'prefix': '"""exp_"""'}), "(prefix='exp_')\n", (255, 270), False, 'from lmfit.models import ExponentialModel, GaussianModel\n'), ((280, 307), 'lmfit.models.GaussianModel', 'GaussianModel', ([], {'prefix': '"""g1_"""'}), "(prefix='g1_')\n", (293, 307), False, 'from lmfit.models import ExponentialModel, GaussianModel\n'), ((317, 344), 'lmfit.models.GaussianModel', 'GaussianModel', ([], {'prefix': '"""g2_"""'}), "(prefix='g2_')\n", (330, 344), False, 'from lmfit.models import ExponentialModel, GaussianModel\n'), ((857, 876), 'matplotlib.pyplot.plot', 'plt.plot', (['x', 'y', '"""b"""'], {}), "(x, y, 'b')\n", (865, 876), True, 'import matplotlib.pyplot as plt\n'), ((877, 909), 'matplotlib.pyplot.plot', 'plt.plot', (['x', 'out.init_fit', '"""k--"""'], {}), "(x, out.init_fit, 'k--')\n", (885, 909), True, 'import matplotlib.pyplot as plt\n'), ((910, 941), 'matplotlib.pyplot.plot', 'plt.plot', (['x', 'out.best_fit', '"""r-"""'], {}), "(x, out.best_fit, 'r-')\n", (918, 941), True, 'import matplotlib.pyplot as plt\n'), ((996, 1006), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1004, 1006), True, 'import matplotlib.pyplot as plt\n'), ((489, 514), 'numpy.where', 'np.where', (['(arrval <= value)'], {}), '(arrval <= value)\n', (497, 514), True, 'import numpy as np\n')]
import librosa import librosa.display import matplotlib.pyplot as plt import numpy as np import torch from tqdm import tqdm from trainer.base_trainer import BaseTrainer from util.utils import compute_SDR plt.switch_backend('agg') class Trainer(BaseTrainer): def __init__(self, config, resume: bool, model, loss_function, optimizer, train_dataloader, validation_dataloader): super(Trainer, self).__init__(config, resume, model, loss_function, optimizer) self.train_dataloader = train_dataloader self.validation_dataloader = validation_dataloader def _train_epoch(self, epoch): loss_total = 0.0 short_loss_total = 0.0 middle_loss_total = 0.0 long_loss_total = 0.0 for mixture, target, reference, _ in tqdm(self.train_dataloader, desc="Training"): mixture = mixture.to(self.device).unsqueeze(1) target = target.to(self.device).unsqueeze(1) reference = reference.to(self.device).unsqueeze(1) self.optimizer.zero_grad() short_scale_enhanced, middle_scale_enhanced, long_scale_enhanced, _ = self.model(mixture, reference) loss, (short_loss, middle_loss, long_loss) = self.loss_function(target, short_scale_enhanced, middle_scale_enhanced, long_scale_enhanced) loss.backward() torch.nn.utils.clip_grad_norm_(self.model.parameters(), 5) self.optimizer.step() loss_total += loss.item() short_loss_total += short_loss.item() middle_loss_total += middle_loss.item() long_loss_total += long_loss.item() # if i == 0: # self.writer.add_figure(f"Train_Tensor/Mixture", self.image_grad(mixture_mag.cpu()), epoch) # self.writer.add_figure(f"Train_Tensor/Target", self.image_grad(target_mag.cpu()), epoch) # self.writer.add_figure(f"Train_Tensor/Enhanced", self.image_grad(enhanced_mag.detach().cpu()), epoch) # self.writer.add_figure(f"Train_Tensor/Ref", self.image_grad(reference.cpu()), epoch) self.writer.add_scalar(f"Train/Loss", loss_total / len(self.train_dataloader), epoch) self.writer.add_scalar(f"Train/Short Loss", short_loss_total / len(self.train_dataloader), epoch) self.writer.add_scalar(f"Train/Middle Loss", middle_loss_total / len(self.train_dataloader), epoch) self.writer.add_scalar(f"Train/Long Loss", long_loss_total / len(self.train_dataloader), epoch) @torch.no_grad() def _validation_epoch(self, epoch): visualize_audio_limit = self.validation_custom_config["visualize_audio_limit"] visualize_waveform_limit = self.validation_custom_config["visualize_waveform_limit"] visualize_spectrogram_limit = self.validation_custom_config["visualize_spectrogram_limit"] n_samples = self.validation_custom_config["n_samples"] weights = self.validation_custom_config["weights"] sr = self.validation_custom_config["sr"] get_metrics_ave = lambda metrics: np.sum(metrics) / len(metrics) sdr_c_m = [] # Clean and mixture sdr_c_e = [] # Clean and enhanced for i, (mixture, target, reference, target_filename) in tqdm(enumerate(self.validation_dataloader)): assert len(target_filename) == 1, "The batch size of validation dataloader must be 1." name = target_filename[0] mixture = mixture.to(self.device) reference = reference.to(self.device) mixture_chunks = list(torch.split(mixture, n_samples, dim=-1)) last_chunk = mixture_chunks[-1] if last_chunk.size(-1) != n_samples: mixture_chunks[-1] = torch.cat(( mixture_chunks[-1], torch.zeros(1, n_samples - last_chunk.size(-1)).to(self.device) ), dim=1) enhanced_chunks = [] for mixture_chunk in mixture_chunks: short_scale, middle_scale, long_scale, _ = self.model(mixture_chunk, reference).detach().cpu() enhanced_chunks.append(short_scale * weights[0] + middle_scale * weights[1] + long_scale * weights[2]) enhanced = torch.cat(enhanced_chunks, dim=1) # [F, T] enhanced = enhanced[:, :mixture.shape[1]] mixture = mixture.reshape(-1).cpu().numpy() enhanced = enhanced.reshape(-1).cpu().numpy() target = target.reshape(-1).cpu().numpy() reference = reference.reshape(-1).cpu().numpy() # Visualize audio if i <= visualize_audio_limit: self.writer.add_audio(f"Speech/{name}_Mixture", mixture, epoch, sample_rate=sr) self.writer.add_audio(f"Speech/{name}_Enhanced", enhanced, epoch, sample_rate=sr) self.writer.add_audio(f"Speech/{name}_Target", target, epoch, sample_rate=sr) self.writer.add_audio(f"Speech/{name}_Reference", reference, epoch, sample_rate=sr) # Visualize waveform if i <= visualize_waveform_limit: fig, ax = plt.subplots(3, 1) for j, y in enumerate([mixture, enhanced, target]): ax[j].set_title("mean: {:.3f}, std: {:.3f}, max: {:.3f}, min: {:.3f}".format( np.mean(y), np.std(y), np.max(y), np.min(y) )) librosa.display.waveplot(y, sr=sr, ax=ax[j]) plt.tight_layout() self.writer.add_figure(f"Waveform/{name}", fig, epoch) # Visualize spectrogram mixture_mag, _ = librosa.magphase(librosa.stft(mixture, n_fft=320, hop_length=160)) enhanced_mag, _ = librosa.magphase(librosa.stft(enhanced, n_fft=320, hop_length=160)) target_mag, _ = librosa.magphase(librosa.stft(target, n_fft=320, hop_length=160)) if i <= visualize_spectrogram_limit: fig, axes = plt.subplots(3, 1, figsize=(6, 6)) for k, mag in enumerate([ mixture_mag, enhanced_mag, target_mag, ]): axes[k].set_title(f"mean: {np.mean(mag):.3f}, " f"std: {np.std(mag):.3f}, " f"max: {np.max(mag):.3f}, " f"min: {np.min(mag):.3f}") librosa.display.specshow(librosa.amplitude_to_db(mag), cmap="magma", y_axis="linear", ax=axes[k], sr=sr) plt.tight_layout() self.writer.add_figure(f"Spectrogram/{name}", fig, epoch) # Metrics c_m = compute_SDR(target, mixture) c_e = compute_SDR(target, enhanced) sdr_c_m.append(c_m) sdr_c_e.append(c_e) print(f"Value: {c_e - c_m} \n" f"Mean: {get_metrics_ave(sdr_c_e) - get_metrics_ave(sdr_c_m)}") self.writer.add_scalars(f"Metrics/SDR", { "target and mixture": get_metrics_ave(sdr_c_m), "target and enhanced": get_metrics_ave(sdr_c_e) }, epoch) score = get_metrics_ave(sdr_c_e) return score
[ "matplotlib.pyplot.switch_backend", "matplotlib.pyplot.tight_layout", "tqdm.tqdm", "numpy.sum", "librosa.display.waveplot", "numpy.std", "torch.split", "torch.cat", "numpy.max", "numpy.mean", "numpy.min", "librosa.amplitude_to_db", "util.utils.compute_SDR", "torch.no_grad", "matplotlib.pyplot.subplots", "librosa.stft" ]
[((218, 243), 'matplotlib.pyplot.switch_backend', 'plt.switch_backend', (['"""agg"""'], {}), "('agg')\n", (236, 243), True, 'import matplotlib.pyplot as plt\n'), ((2571, 2586), 'torch.no_grad', 'torch.no_grad', ([], {}), '()\n', (2584, 2586), False, 'import torch\n'), ((804, 848), 'tqdm.tqdm', 'tqdm', (['self.train_dataloader'], {'desc': '"""Training"""'}), "(self.train_dataloader, desc='Training')\n", (808, 848), False, 'from tqdm import tqdm\n'), ((4320, 4353), 'torch.cat', 'torch.cat', (['enhanced_chunks'], {'dim': '(1)'}), '(enhanced_chunks, dim=1)\n', (4329, 4353), False, 'import torch\n'), ((6969, 6997), 'util.utils.compute_SDR', 'compute_SDR', (['target', 'mixture'], {}), '(target, mixture)\n', (6980, 6997), False, 'from util.utils import compute_SDR\n'), ((7017, 7046), 'util.utils.compute_SDR', 'compute_SDR', (['target', 'enhanced'], {}), '(target, enhanced)\n', (7028, 7046), False, 'from util.utils import compute_SDR\n'), ((3129, 3144), 'numpy.sum', 'np.sum', (['metrics'], {}), '(metrics)\n', (3135, 3144), True, 'import numpy as np\n'), ((3637, 3676), 'torch.split', 'torch.split', (['mixture', 'n_samples'], {'dim': '(-1)'}), '(mixture, n_samples, dim=-1)\n', (3648, 3676), False, 'import torch\n'), ((5232, 5250), 'matplotlib.pyplot.subplots', 'plt.subplots', (['(3)', '(1)'], {}), '(3, 1)\n', (5244, 5250), True, 'import matplotlib.pyplot as plt\n'), ((5670, 5688), 'matplotlib.pyplot.tight_layout', 'plt.tight_layout', ([], {}), '()\n', (5686, 5688), True, 'import matplotlib.pyplot as plt\n'), ((5847, 5895), 'librosa.stft', 'librosa.stft', (['mixture'], {'n_fft': '(320)', 'hop_length': '(160)'}), '(mixture, n_fft=320, hop_length=160)\n', (5859, 5895), False, 'import librosa\n'), ((5945, 5994), 'librosa.stft', 'librosa.stft', (['enhanced'], {'n_fft': '(320)', 'hop_length': '(160)'}), '(enhanced, n_fft=320, hop_length=160)\n', (5957, 5994), False, 'import librosa\n'), ((6042, 6089), 'librosa.stft', 'librosa.stft', (['target'], {'n_fft': '(320)', 'hop_length': '(160)'}), '(target, n_fft=320, hop_length=160)\n', (6054, 6089), False, 'import librosa\n'), ((6172, 6206), 'matplotlib.pyplot.subplots', 'plt.subplots', (['(3)', '(1)'], {'figsize': '(6, 6)'}), '(3, 1, figsize=(6, 6))\n', (6184, 6206), True, 'import matplotlib.pyplot as plt\n'), ((6831, 6849), 'matplotlib.pyplot.tight_layout', 'plt.tight_layout', ([], {}), '()\n', (6847, 6849), True, 'import matplotlib.pyplot as plt\n'), ((5608, 5652), 'librosa.display.waveplot', 'librosa.display.waveplot', (['y'], {'sr': 'sr', 'ax': 'ax[j]'}), '(y, sr=sr, ax=ax[j])\n', (5632, 5652), False, 'import librosa\n'), ((6688, 6716), 'librosa.amplitude_to_db', 'librosa.amplitude_to_db', (['mag'], {}), '(mag)\n', (6711, 6716), False, 'import librosa\n'), ((5444, 5454), 'numpy.mean', 'np.mean', (['y'], {}), '(y)\n', (5451, 5454), True, 'import numpy as np\n'), ((5481, 5490), 'numpy.std', 'np.std', (['y'], {}), '(y)\n', (5487, 5490), True, 'import numpy as np\n'), ((5517, 5526), 'numpy.max', 'np.max', (['y'], {}), '(y)\n', (5523, 5526), True, 'import numpy as np\n'), ((5553, 5562), 'numpy.min', 'np.min', (['y'], {}), '(y)\n', (5559, 5562), True, 'import numpy as np\n'), ((6421, 6433), 'numpy.mean', 'np.mean', (['mag'], {}), '(mag)\n', (6428, 6433), True, 'import numpy as np\n'), ((6489, 6500), 'numpy.std', 'np.std', (['mag'], {}), '(mag)\n', (6495, 6500), True, 'import numpy as np\n'), ((6556, 6567), 'numpy.max', 'np.max', (['mag'], {}), '(mag)\n', (6562, 6567), True, 'import numpy as np\n'), ((6623, 6634), 'numpy.min', 'np.min', (['mag'], {}), '(mag)\n', (6629, 6634), True, 'import numpy as np\n')]
''' Copyright 2015 by <NAME> This file is part of Statistical Parameter Estimation Tool (SPOTPY). :author: <NAME> This example implements the Rosenbrock function into SPOT. ''' from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import numpy as np import spotpy class spot_setup(object): slow = 1000 def __init__(self): self.params = [spotpy.parameter.List('x',[1,2,3,4,6,7,8,9,0]), #Give possible x values as a List spotpy.parameter.List('y',[0,1,2,5,7,8,9,0,1])] #Give possible y values as a List self.database = file('MyOwnDatabase.txt','w') def parameters(self): return spotpy.parameter.generate(self.params) def simulation(self,vector): x=np.array(vector) for i in xrange(self.slow): s = np.sin(i) simulations= [sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)] return simulations def evaluation(self): observations=[0] return observations def objectivefunction(self,simulation,evaluation): objectivefunction=-spotpy.objectivefunctions.rmse(evaluation,simulation) return objectivefunction def save(self, objectivefunctions, parameter, simulations): line=str(objectivefunctions)+','+str(parameter).strip('[]')+','+str(simulations).strip('[]')+'\n' self.database.write(line) spot_setup=spot_setup() 'Leave out dbformat and dbname and spotpy will return results in spot_setup.save function' sampler=spotpy.algorithms.mc(spot_setup) sampler.sample(10) #Choose equaly or less repetitions as you have parameters in your List spot_setup.database.close() # Close the created txt file
[ "spotpy.parameter.List", "spotpy.parameter.generate", "numpy.sin", "numpy.array", "spotpy.algorithms.mc", "spotpy.objectivefunctions.rmse" ]
[((1669, 1701), 'spotpy.algorithms.mc', 'spotpy.algorithms.mc', (['spot_setup'], {}), '(spot_setup)\n', (1689, 1701), False, 'import spotpy\n'), ((778, 816), 'spotpy.parameter.generate', 'spotpy.parameter.generate', (['self.params'], {}), '(self.params)\n', (803, 816), False, 'import spotpy\n'), ((869, 885), 'numpy.array', 'np.array', (['vector'], {}), '(vector)\n', (877, 885), True, 'import numpy as np\n'), ((462, 517), 'spotpy.parameter.List', 'spotpy.parameter.List', (['"""x"""', '[1, 2, 3, 4, 6, 7, 8, 9, 0]'], {}), "('x', [1, 2, 3, 4, 6, 7, 8, 9, 0])\n", (483, 517), False, 'import spotpy\n'), ((567, 622), 'spotpy.parameter.List', 'spotpy.parameter.List', (['"""y"""', '[0, 1, 2, 5, 7, 8, 9, 0, 1]'], {}), "('y', [0, 1, 2, 5, 7, 8, 9, 0, 1])\n", (588, 622), False, 'import spotpy\n'), ((938, 947), 'numpy.sin', 'np.sin', (['i'], {}), '(i)\n', (944, 947), True, 'import numpy as np\n'), ((1231, 1285), 'spotpy.objectivefunctions.rmse', 'spotpy.objectivefunctions.rmse', (['evaluation', 'simulation'], {}), '(evaluation, simulation)\n', (1261, 1285), False, 'import spotpy\n')]
import Nn import numpy as np import tensorflow as tf import tensorflow_probability as tfp from utils.tf2_utils import show_graph, get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy from Algorithms.tf2algos.base.on_policy import On_Policy class PPO(On_Policy): ''' Proximal Policy Optimization, https://arxiv.org/abs/1707.06347 Emergence of Locomotion Behaviours in Rich Environments, http://arxiv.org/abs/1707.02286, DPPO ''' def __init__(self, s_dim, visual_sources, visual_resolution, a_dim_or_list, is_continuous, policy_epoch=4, value_epoch=4, beta=1.0e-3, lr=5.0e-4, lambda_=0.95, epsilon=0.2, value_epsilon=0.2, share_net=True, actor_lr=3e-4, critic_lr=1e-3, kl_reverse=False, kl_target=0.02, kl_target_cutoff=2, kl_target_earlystop=4, kl_beta=[0.7, 1.3], kl_alpha=1.5, kl_coef=1.0, hidden_units={ 'share': { 'continuous': { 'share': [32, 32], 'mu': [32, 32], 'v': [32, 32] }, 'discrete': { 'share': [32, 32], 'logits': [32, 32], 'v': [32, 32] } }, 'actor_continuous': [32, 32], 'actor_discrete': [32, 32], 'critic': [32, 32] }, **kwargs): super().__init__( s_dim=s_dim, visual_sources=visual_sources, visual_resolution=visual_resolution, a_dim_or_list=a_dim_or_list, is_continuous=is_continuous, **kwargs) self.beta = beta self.policy_epoch = policy_epoch self.value_epoch = value_epoch self.lambda_ = lambda_ self.epsilon = epsilon self.value_epsilon = value_epsilon self.share_net = share_net self.kl_reverse = kl_reverse self.kl_target = kl_target self.kl_alpha = kl_alpha self.kl_coef = tf.constant(kl_coef, dtype=tf.float32) self.kl_cutoff = kl_target * kl_target_cutoff self.kl_stop = kl_target * kl_target_earlystop self.kl_low = kl_target * kl_beta[0] self.kl_high = kl_target * kl_beta[-1] if self.is_continuous: self.log_std = tf.Variable(initial_value=-0.5 * np.ones(self.a_counts, dtype=np.float32), trainable=True) if self.share_net: # self.TensorSpecs = get_TensorSpecs([self.s_dim], self.visual_dim, [self.a_counts], [1], [1], [1]) if self.is_continuous: self.net = Nn.a_c_v_continuous(self.rnn_net.hdim, self.a_counts, hidden_units['share']['continuous']) self.net_tv = self.net.trainable_variables + [self.log_std] + self.other_tv else: self.net = Nn.a_c_v_discrete(self.rnn_net.hdim, self.a_counts, hidden_units['share']['discrete']) self.net_tv = self.net.trainable_variables + self.other_tv self.lr = self.init_lr(lr) self.optimizer = self.init_optimizer(self.lr) self.model_recorder(dict( model=self.net, optimizer=self.optimizer )) else: # self.actor_TensorSpecs = get_TensorSpecs([self.s_dim], self.visual_dim, [self.a_counts], [1], [1]) # self.critic_TensorSpecs = get_TensorSpecs([self.s_dim], self.visual_dim, [1]) if self.is_continuous: self.actor_net = Nn.actor_mu(self.rnn_net.hdim, self.a_counts, hidden_units['actor_continuous']) self.actor_net_tv = self.actor_net.trainable_variables+ [self.log_std] else: self.actor_net = Nn.actor_discrete(self.rnn_net.hdim, self.a_counts, hidden_units['actor_discrete']) self.actor_net_tv = self.actor_net.trainable_variables self.critic_net = Nn.critic_v(self.rnn_net.hdim, hidden_units['critic']) self.critic_tv = self.critic_net.trainable_variables + self.other_tv self.actor_lr, self.critic_lr = map(self.init_lr, [actor_lr, critic_lr]) self.optimizer_actor, self.optimizer_critic = map(self.init_optimizer, [self.actor_lr, self.critic_lr]) self.model_recorder(dict( actor=self.actor_net, critic=self.critic_net, optimizer_actor=self.optimizer_actor, optimizer_critic=self.optimizer_critic )) self.initialize_data_buffer( data_name_list=['s', 'visual_s', 'a', 'r', 's_', 'visual_s_', 'done', 'value', 'log_prob']) def show_logo(self): self.recorder.logger.info('''    xxxxxxxx       xxxxxxxx         xxxxx           xx  xx         xx  xx        xxx xxx          x   xxx        x   xxx       xx   xx          x   xxx        x   xxx       xx   xxx         xxxxxx         xxxxxx       xxx   xxx         x              x             xx   xxx         x              x             xx   xx          x              x             xx  xxx        xxxxx          xxxxx            xxxxx   ''') def choose_action(self, s, visual_s, evaluation=False): a, value, log_prob, self.cell_state = self._get_action(s, visual_s, self.cell_state) a = a.numpy() self._value = np.squeeze(value.numpy()) self._log_prob = np.squeeze(log_prob.numpy()) + 1e-10 return a @tf.function def _get_action(self, s, visual_s, cell_state): with tf.device(self.device): feat, cell_state = self.get_feature(s, visual_s, cell_state=cell_state, record_cs=True, train=False) if self.is_continuous: if self.share_net: mu, value = self.net(feat) else: mu = self.actor_net(feat) value = self.critic_net(feat) sample_op, _ = gaussian_clip_rsample(mu, self.log_std) log_prob = gaussian_likelihood_sum(sample_op, mu, self.log_std) else: if self.share_net: logits, value = self.net(feat) else: logits = self.actor_net(feat) value = self.critic_net(feat) norm_dist = tfp.distributions.Categorical(logits) sample_op = norm_dist.sample() log_prob = norm_dist.log_prob(sample_op) return sample_op, value, log_prob, cell_state def store_data(self, s, visual_s, a, r, s_, visual_s_, done): assert isinstance(a, np.ndarray), "store_data need action type is np.ndarray" assert isinstance(r, np.ndarray), "store_data need reward type is np.ndarray" assert isinstance(done, np.ndarray), "store_data need done type is np.ndarray" self.data.add(s, visual_s, a, r, s_, visual_s_, done, self._value, self._log_prob) @tf.function def _get_value(self, feat): with tf.device(self.device): if self.share_net: _, value = self.net(feat) else: value = self.critic_net(feat) return value def calculate_statistics(self): feat, self.cell_state = self.get_feature(self.data.last_s(), self.data.last_visual_s(), cell_state=self.cell_state, record_cs=True, train=False) init_value = np.squeeze(self._get_value(feat).numpy()) self.data.cal_dc_r(self.gamma, init_value) self.data.cal_td_error(self.gamma, init_value) self.data.cal_gae_adv(self.lambda_, self.gamma) # @show_graph(name='ppo_net') def learn(self, **kwargs): self.episode = kwargs['episode'] def _train(data, crsty_loss, cell_state): early_step = 0 if self.share_net: for i in range(self.policy_epoch): actor_loss, critic_loss, entropy, kl = self.train_share( data, self.kl_coef, crsty_loss, cell_state ) if kl > self.kl_stop: early_step = i break else: for i in range(self.policy_epoch): s, visual_s, a, dc_r, old_log_prob, advantage, old_value = data actor_loss, entropy, kl = self.train_actor( (s, visual_s, a, old_log_prob, advantage), self.kl_coef, cell_state ) if kl > self.kl_stop: early_step = i break for _ in range(self.value_epoch): critic_loss = self.train_critic( (s, visual_s, dc_r, old_value), crsty_loss, cell_state ) # https://github.com/joschu/modular_rl/blob/6970cde3da265cf2a98537250fea5e0c0d9a7639/modular_rl/ppo.py#L93 if kl > self.kl_high: self.kl_coef *= self.kl_alpha elif kl < self.kl_low: self.kl_coef /= self.kl_alpha summaries = dict([ ['LOSS/actor_loss', actor_loss], ['LOSS/critic_loss', critic_loss], ['Statistics/kl', kl], ['Statistics/kl_coef', self.kl_coef], ['Statistics/early_step', early_step], ['Statistics/entropy', entropy] ]) return summaries if self.share_net: summary_dict = dict([['LEARNING_RATE/lr', self.lr(self.episode)]]) else: summary_dict =dict([ ['LEARNING_RATE/actor_lr', self.actor_lr(self.episode)], ['LEARNING_RATE/critic_lr', self.critic_lr(self.episode)] ]) self._learn(function_dict={ 'calculate_statistics': self.calculate_statistics, 'train_function': _train, 'train_data_list': ['s', 'visual_s', 'a', 'discounted_reward', 'log_prob', 'gae_adv', 'value'], 'summary_dict': summary_dict }) @tf.function(experimental_relax_shapes=True) def train_share(self, memories, kl_coef, crsty_loss, cell_state): s, visual_s, a, dc_r, old_log_prob, advantage, old_value = memories with tf.device(self.device): with tf.GradientTape() as tape: feat = self.get_feature(s, visual_s, cell_state=cell_state) if self.is_continuous: mu, value = self.net(feat) new_log_prob = gaussian_likelihood_sum(a, mu, self.log_std) entropy = gaussian_entropy(self.log_std) else: logits, value = self.net(feat) logp_all = tf.nn.log_softmax(logits) new_log_prob = tf.reduce_sum(a * logp_all, axis=1, keepdims=True) entropy = -tf.reduce_mean(tf.reduce_sum(tf.exp(logp_all) * logp_all, axis=1, keepdims=True)) ratio = tf.exp(new_log_prob - old_log_prob) # https://github.com/joschu/modular_rl/blob/6970cde3da265cf2a98537250fea5e0c0d9a7639/modular_rl/ppo.py#L40 if self.kl_reverse: kl = tf.reduce_mean(new_log_prob - old_log_prob) else: kl = tf.reduce_mean(old_log_prob - new_log_prob) # a sample estimate for KL-divergence, easy to compute surrogate = ratio * advantage # https://github.com/llSourcell/OpenAI_Five_vs_Dota2_Explained/blob/c5def7e57aa70785c2394ea2eeb3e5f66ad59a53/train.py#L154 value_clip = old_value + tf.clip_by_value(value - old_value, -self.value_epsilon, self.value_epsilon) td_error = dc_r - value td_error_clip = dc_r - value_clip td_square = tf.maximum(tf.square(td_error), tf.square(td_error_clip)) pi_loss = -tf.reduce_mean( tf.minimum( surrogate, tf.clip_by_value(ratio, 1.0 - self.epsilon, 1.0 + self.epsilon) * advantage )) kl_loss = kl_coef * kl extra_loss = 1000.0 * tf.square(tf.maximum(0., kl - self.kl_cutoff)) actor_loss = pi_loss + kl_loss + extra_loss value_loss = 0.5 * tf.reduce_mean(td_square) loss = actor_loss + 1.0 * value_loss - self.beta * entropy + crsty_loss loss_grads = tape.gradient(loss, self.net_tv) self.optimizer.apply_gradients( zip(loss_grads, self.net_tv) ) self.global_step.assign_add(1) return actor_loss, value_loss, entropy, kl @tf.function(experimental_relax_shapes=True) def train_actor(self, memories, kl_coef, cell_state): s, visual_s, a, old_log_prob, advantage = memories with tf.device(self.device): feat = self.get_feature(s, visual_s, cell_state=cell_state) with tf.GradientTape() as tape: if self.is_continuous: mu = self.actor_net(feat) new_log_prob = gaussian_likelihood_sum(a, mu, self.log_std) entropy = gaussian_entropy(self.log_std) else: logits = self.actor_net(feat) logp_all = tf.nn.log_softmax(logits) new_log_prob = tf.reduce_sum(a * logp_all, axis=1, keepdims=True) entropy = -tf.reduce_mean(tf.reduce_sum(tf.exp(logp_all) * logp_all, axis=1, keepdims=True)) ratio = tf.exp(new_log_prob - old_log_prob) kl = tf.reduce_mean(old_log_prob - new_log_prob) surrogate = ratio * advantage min_adv = tf.where(advantage > 0, (1 + self.epsilon) * advantage, (1 - self.epsilon) * advantage) pi_loss = -(tf.reduce_mean(tf.minimum(surrogate, min_adv)) + self.beta * entropy) kl_loss = kl_coef * kl extra_loss = 1000.0 * tf.square(tf.maximum(0., kl - self.kl_cutoff)) actor_loss = pi_loss + kl_loss + extra_loss actor_grads = tape.gradient(actor_loss, self.actor_net_tv) self.optimizer_actor.apply_gradients( zip(actor_grads, self.actor_net_tv) ) self.global_step.assign_add(1) return actor_loss, entropy, kl @tf.function(experimental_relax_shapes=True) def train_critic(self, memories, crsty_loss, cell_state): s, visual_s, dc_r, old_value = memories with tf.device(self.device): with tf.GradientTape() as tape: feat = self.get_feature(s, visual_s, cell_state=cell_state) value = self.critic_net(feat) value_clip = old_value + tf.clip_by_value(value-old_value, -self.value_epsilon, self.value_epsilon) td_error = dc_r - value td_error_clip = dc_r - value_clip td_square = tf.maximum(tf.square(td_error), tf.square(td_error_clip)) value_loss = 0.5 * tf.reduce_mean(td_square) + crsty_loss critic_grads = tape.gradient(value_loss, self.critic_tv) self.optimizer_critic.apply_gradients( zip(critic_grads, self.critic_tv) ) return value_loss
[ "Nn.critic_v", "tensorflow.reduce_sum", "tensorflow.clip_by_value", "tensorflow.maximum", "numpy.ones", "Nn.actor_mu", "tensorflow.nn.log_softmax", "tensorflow_probability.distributions.Categorical", "tensorflow.minimum", "tensorflow.exp", "utils.tf2_utils.gaussian_entropy", "tensorflow.reduce_mean", "tensorflow.constant", "Nn.a_c_v_discrete", "tensorflow.where", "utils.tf2_utils.gaussian_likelihood_sum", "tensorflow.device", "tensorflow.square", "tensorflow.function", "Nn.actor_discrete", "Nn.a_c_v_continuous", "utils.tf2_utils.gaussian_clip_rsample", "tensorflow.GradientTape" ]
[((10804, 10847), 'tensorflow.function', 'tf.function', ([], {'experimental_relax_shapes': '(True)'}), '(experimental_relax_shapes=True)\n', (10815, 10847), True, 'import tensorflow as tf\n'), ((13473, 13516), 'tensorflow.function', 'tf.function', ([], {'experimental_relax_shapes': '(True)'}), '(experimental_relax_shapes=True)\n', (13484, 13516), True, 'import tensorflow as tf\n'), ((15189, 15232), 'tensorflow.function', 'tf.function', ([], {'experimental_relax_shapes': '(True)'}), '(experimental_relax_shapes=True)\n', (15200, 15232), True, 'import tensorflow as tf\n'), ((2524, 2562), 'tensorflow.constant', 'tf.constant', (['kl_coef'], {'dtype': 'tf.float32'}), '(kl_coef, dtype=tf.float32)\n', (2535, 2562), True, 'import tensorflow as tf\n'), ((4423, 4477), 'Nn.critic_v', 'Nn.critic_v', (['self.rnn_net.hdim', "hidden_units['critic']"], {}), "(self.rnn_net.hdim, hidden_units['critic'])\n", (4434, 4477), False, 'import Nn\n'), ((6032, 6054), 'tensorflow.device', 'tf.device', (['self.device'], {}), '(self.device)\n', (6041, 6054), True, 'import tensorflow as tf\n'), ((7485, 7507), 'tensorflow.device', 'tf.device', (['self.device'], {}), '(self.device)\n', (7494, 7507), True, 'import tensorflow as tf\n'), ((11007, 11029), 'tensorflow.device', 'tf.device', (['self.device'], {}), '(self.device)\n', (11016, 11029), True, 'import tensorflow as tf\n'), ((13647, 13669), 'tensorflow.device', 'tf.device', (['self.device'], {}), '(self.device)\n', (13656, 13669), True, 'import tensorflow as tf\n'), ((15356, 15378), 'tensorflow.device', 'tf.device', (['self.device'], {}), '(self.device)\n', (15365, 15378), True, 'import tensorflow as tf\n'), ((3116, 3211), 'Nn.a_c_v_continuous', 'Nn.a_c_v_continuous', (['self.rnn_net.hdim', 'self.a_counts', "hidden_units['share']['continuous']"], {}), "(self.rnn_net.hdim, self.a_counts, hidden_units['share']\n ['continuous'])\n", (3135, 3211), False, 'import Nn\n'), ((3344, 3435), 'Nn.a_c_v_discrete', 'Nn.a_c_v_discrete', (['self.rnn_net.hdim', 'self.a_counts', "hidden_units['share']['discrete']"], {}), "(self.rnn_net.hdim, self.a_counts, hidden_units['share'][\n 'discrete'])\n", (3361, 3435), False, 'import Nn\n'), ((4020, 4099), 'Nn.actor_mu', 'Nn.actor_mu', (['self.rnn_net.hdim', 'self.a_counts', "hidden_units['actor_continuous']"], {}), "(self.rnn_net.hdim, self.a_counts, hidden_units['actor_continuous'])\n", (4031, 4099), False, 'import Nn\n'), ((4238, 4326), 'Nn.actor_discrete', 'Nn.actor_discrete', (['self.rnn_net.hdim', 'self.a_counts', "hidden_units['actor_discrete']"], {}), "(self.rnn_net.hdim, self.a_counts, hidden_units[\n 'actor_discrete'])\n", (4255, 4326), False, 'import Nn\n'), ((6435, 6474), 'utils.tf2_utils.gaussian_clip_rsample', 'gaussian_clip_rsample', (['mu', 'self.log_std'], {}), '(mu, self.log_std)\n', (6456, 6474), False, 'from utils.tf2_utils import show_graph, get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy\n'), ((6502, 6554), 'utils.tf2_utils.gaussian_likelihood_sum', 'gaussian_likelihood_sum', (['sample_op', 'mu', 'self.log_std'], {}), '(sample_op, mu, self.log_std)\n', (6525, 6554), False, 'from utils.tf2_utils import show_graph, get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy\n'), ((6809, 6846), 'tensorflow_probability.distributions.Categorical', 'tfp.distributions.Categorical', (['logits'], {}), '(logits)\n', (6838, 6846), True, 'import tensorflow_probability as tfp\n'), ((11048, 11065), 'tensorflow.GradientTape', 'tf.GradientTape', ([], {}), '()\n', (11063, 11065), True, 'import tensorflow as tf\n'), ((11731, 11766), 'tensorflow.exp', 'tf.exp', (['(new_log_prob - old_log_prob)'], {}), '(new_log_prob - old_log_prob)\n', (11737, 11766), True, 'import tensorflow as tf\n'), ((13760, 13777), 'tensorflow.GradientTape', 'tf.GradientTape', ([], {}), '()\n', (13775, 13777), True, 'import tensorflow as tf\n'), ((14365, 14400), 'tensorflow.exp', 'tf.exp', (['(new_log_prob - old_log_prob)'], {}), '(new_log_prob - old_log_prob)\n', (14371, 14400), True, 'import tensorflow as tf\n'), ((14422, 14465), 'tensorflow.reduce_mean', 'tf.reduce_mean', (['(old_log_prob - new_log_prob)'], {}), '(old_log_prob - new_log_prob)\n', (14436, 14465), True, 'import tensorflow as tf\n'), ((14538, 14629), 'tensorflow.where', 'tf.where', (['(advantage > 0)', '((1 + self.epsilon) * advantage)', '((1 - self.epsilon) * advantage)'], {}), '(advantage > 0, (1 + self.epsilon) * advantage, (1 - self.epsilon) *\n advantage)\n', (14546, 14629), True, 'import tensorflow as tf\n'), ((15397, 15414), 'tensorflow.GradientTape', 'tf.GradientTape', ([], {}), '()\n', (15412, 15414), True, 'import tensorflow as tf\n'), ((11272, 11316), 'utils.tf2_utils.gaussian_likelihood_sum', 'gaussian_likelihood_sum', (['a', 'mu', 'self.log_std'], {}), '(a, mu, self.log_std)\n', (11295, 11316), False, 'from utils.tf2_utils import show_graph, get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy\n'), ((11347, 11377), 'utils.tf2_utils.gaussian_entropy', 'gaussian_entropy', (['self.log_std'], {}), '(self.log_std)\n', (11363, 11377), False, 'from utils.tf2_utils import show_graph, get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy\n'), ((11482, 11507), 'tensorflow.nn.log_softmax', 'tf.nn.log_softmax', (['logits'], {}), '(logits)\n', (11499, 11507), True, 'import tensorflow as tf\n'), ((11543, 11593), 'tensorflow.reduce_sum', 'tf.reduce_sum', (['(a * logp_all)'], {'axis': '(1)', 'keepdims': '(True)'}), '(a * logp_all, axis=1, keepdims=True)\n', (11556, 11593), True, 'import tensorflow as tf\n'), ((11952, 11995), 'tensorflow.reduce_mean', 'tf.reduce_mean', (['(new_log_prob - old_log_prob)'], {}), '(new_log_prob - old_log_prob)\n', (11966, 11995), True, 'import tensorflow as tf\n'), ((12043, 12086), 'tensorflow.reduce_mean', 'tf.reduce_mean', (['(old_log_prob - new_log_prob)'], {}), '(old_log_prob - new_log_prob)\n', (12057, 12086), True, 'import tensorflow as tf\n'), ((12388, 12464), 'tensorflow.clip_by_value', 'tf.clip_by_value', (['(value - old_value)', '(-self.value_epsilon)', 'self.value_epsilon'], {}), '(value - old_value, -self.value_epsilon, self.value_epsilon)\n', (12404, 12464), True, 'import tensorflow as tf\n'), ((12594, 12613), 'tensorflow.square', 'tf.square', (['td_error'], {}), '(td_error)\n', (12603, 12613), True, 'import tensorflow as tf\n'), ((12615, 12639), 'tensorflow.square', 'tf.square', (['td_error_clip'], {}), '(td_error_clip)\n', (12624, 12639), True, 'import tensorflow as tf\n'), ((13094, 13119), 'tensorflow.reduce_mean', 'tf.reduce_mean', (['td_square'], {}), '(td_square)\n', (13108, 13119), True, 'import tensorflow as tf\n'), ((13907, 13951), 'utils.tf2_utils.gaussian_likelihood_sum', 'gaussian_likelihood_sum', (['a', 'mu', 'self.log_std'], {}), '(a, mu, self.log_std)\n', (13930, 13951), False, 'from utils.tf2_utils import show_graph, get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy\n'), ((13982, 14012), 'utils.tf2_utils.gaussian_entropy', 'gaussian_entropy', (['self.log_std'], {}), '(self.log_std)\n', (13998, 14012), False, 'from utils.tf2_utils import show_graph, get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy\n'), ((14116, 14141), 'tensorflow.nn.log_softmax', 'tf.nn.log_softmax', (['logits'], {}), '(logits)\n', (14133, 14141), True, 'import tensorflow as tf\n'), ((14177, 14227), 'tensorflow.reduce_sum', 'tf.reduce_sum', (['(a * logp_all)'], {'axis': '(1)', 'keepdims': '(True)'}), '(a * logp_all, axis=1, keepdims=True)\n', (14190, 14227), True, 'import tensorflow as tf\n'), ((15588, 15664), 'tensorflow.clip_by_value', 'tf.clip_by_value', (['(value - old_value)', '(-self.value_epsilon)', 'self.value_epsilon'], {}), '(value - old_value, -self.value_epsilon, self.value_epsilon)\n', (15604, 15664), True, 'import tensorflow as tf\n'), ((15792, 15811), 'tensorflow.square', 'tf.square', (['td_error'], {}), '(td_error)\n', (15801, 15811), True, 'import tensorflow as tf\n'), ((15813, 15837), 'tensorflow.square', 'tf.square', (['td_error_clip'], {}), '(td_error_clip)\n', (15822, 15837), True, 'import tensorflow as tf\n'), ((2857, 2897), 'numpy.ones', 'np.ones', (['self.a_counts'], {'dtype': 'np.float32'}), '(self.a_counts, dtype=np.float32)\n', (2864, 2897), True, 'import numpy as np\n'), ((12962, 12998), 'tensorflow.maximum', 'tf.maximum', (['(0.0)', '(kl - self.kl_cutoff)'], {}), '(0.0, kl - self.kl_cutoff)\n', (12972, 12998), True, 'import tensorflow as tf\n'), ((14812, 14848), 'tensorflow.maximum', 'tf.maximum', (['(0.0)', '(kl - self.kl_cutoff)'], {}), '(0.0, kl - self.kl_cutoff)\n', (14822, 14848), True, 'import tensorflow as tf\n'), ((15875, 15900), 'tensorflow.reduce_mean', 'tf.reduce_mean', (['td_square'], {}), '(td_square)\n', (15889, 15900), True, 'import tensorflow as tf\n'), ((14669, 14699), 'tensorflow.minimum', 'tf.minimum', (['surrogate', 'min_adv'], {}), '(surrogate, min_adv)\n', (14679, 14699), True, 'import tensorflow as tf\n'), ((12776, 12839), 'tensorflow.clip_by_value', 'tf.clip_by_value', (['ratio', '(1.0 - self.epsilon)', '(1.0 + self.epsilon)'], {}), '(ratio, 1.0 - self.epsilon, 1.0 + self.epsilon)\n', (12792, 12839), True, 'import tensorflow as tf\n'), ((11654, 11670), 'tensorflow.exp', 'tf.exp', (['logp_all'], {}), '(logp_all)\n', (11660, 11670), True, 'import tensorflow as tf\n'), ((14288, 14304), 'tensorflow.exp', 'tf.exp', (['logp_all'], {}), '(logp_all)\n', (14294, 14304), True, 'import tensorflow as tf\n')]
import sys import os import numpy as np from pprint import pprint from datetime import datetime from datetime import timedelta import mysql.connector import math import matplotlib.pyplot as plt from scipy import stats #database connection cnx = mysql.connector.connect(user='root', password='<PASSWORD>', host='localhost', database='black_carbon') cursor = cnx.cursor() cursor.execute(('''SELECT mc.meas_mean_mass_conc, mc.meas_rel_err, mc.UNIX_UTC_6h_midtime, mc.cluster_number, measCO.CO_ppbv FROM whi_gc_and_sp2_6h_mass_concs mc JOIN whi_co_data measCO on mc.CO_meas_id = measCO.id WHERE mc.RH_threshold = 90 and measCO.CO_ppbv < 250''') ) data = cursor.fetchall() cnx.close() plot_data_Cont = [] plot_data_SPac = [] plot_data_NPac = [] plot_data_WPac = [] for row in data: meas_rBC_v = row[0]/1.29 #density of air is 1.29kg/m3 at 0C and 1atm meas_rel_err_v = row[1] date = datetime.utcfromtimestamp(row[2]) cluster_number = row[3] meas_CO_v = row[4] if date.month == 4: color = 'g' if date.month == 5: color = 'b' if date.month == 6: color = 'orange' if date.month == 7: color = 'red' if date.month == 8: color = 'm' line = [meas_rBC_v,meas_rel_err_v,meas_CO_v,color] if cluster_number in [4]: #Spac = 6,8,9 Npac = 1,3,5,10, Cont = 4 LRT = 2,7 plot_data_Cont.append(line) if cluster_number in [6,8,9]: #Spac = 6,8,9 Npac = 1,3,5,10, Cont = 4 LRT = 2,7 plot_data_SPac.append(line) if cluster_number in [1,3,5,10]: #Spac = 6,8,9 Npac = 1,3,5,10, Cont = 4 LRT = 2,7 plot_data_NPac.append(line) if cluster_number in [2,7]: #Spac = 6,8,9 Npac = 1,3,5,10, Cont = 4 LRT = 2,7 plot_data_WPac.append(line) Cont_bc = [row[0] for row in plot_data_Cont] Cont_bc_err = [row[1]*row[0] for row in plot_data_Cont] Cont_co = [row[2] for row in plot_data_Cont] Cont_month = [row[3] for row in plot_data_Cont] varx = np.array(Cont_co) vary = np.array(Cont_bc) mask = ~np.isnan(varx) & ~np.isnan(vary) Cont_slope, Cont_intercept, Cont_r_value, Cont_p_value, Cont_std_err = stats.linregress(varx[mask], vary[mask]) Cont_line = Cont_slope*varx+Cont_intercept SPacbc = [row[0] for row in plot_data_SPac] SPacbc_err = [row[1]*row[0] for row in plot_data_SPac] SPacco = [row[2] for row in plot_data_SPac] SPacmonth = [row[3] for row in plot_data_SPac] varx = np.array(SPacco) vary = np.array(SPacbc) mask = ~np.isnan(varx) & ~np.isnan(vary) SPacslope, SPacintercept, SPacr_value, SPacp_value, SPacstd_err = stats.linregress(varx[mask], vary[mask]) SPacline = SPacslope*varx+SPacintercept NPacbc = [row[0] for row in plot_data_NPac] NPacbc_err = [row[1]*row[0] for row in plot_data_NPac] NPacco = [row[2] for row in plot_data_NPac] NPacmonth = [row[3] for row in plot_data_NPac] varx = np.array(NPacco) vary = np.array(NPacbc) mask = ~np.isnan(varx) & ~np.isnan(vary) NPacslope, NPacintercept, NPacr_value, NPacp_value, NPacstd_err = stats.linregress(varx[mask], vary[mask]) NPacline = NPacslope*varx+NPacintercept WPacbc = [row[0] for row in plot_data_WPac] WPacbc_err = [row[1]*row[0] for row in plot_data_WPac] WPacco = [row[2] for row in plot_data_WPac] WPacmonth = [row[3] for row in plot_data_WPac] varx = np.array(WPacco) vary = np.array(WPacbc) mask = ~np.isnan(varx) & ~np.isnan(vary) WPacslope, WPacintercept, WPacr_value, WPacp_value, WPacstd_err = stats.linregress(varx[mask], vary[mask]) WPacline = WPacslope*varx+WPacintercept fig = plt.figure(figsize=(14,6)) ax1 = plt.subplot2grid((1,4), (0,3), colspan=1) ax2 = plt.subplot2grid((1,4), (0,0), colspan=1) ax3 = plt.subplot2grid((1,4), (0,1), colspan=1) ax4 = plt.subplot2grid((1,4), (0,2), colspan=1) CO_upper = 175 CO_lower = 75 BC_upper = 325 info_x_pos = 0.05 info_y_pos = 0.8 label_x_pos = 0.05 label_y_pos = 0.92 #ax1.scatter(Cont_co,Cont_bc,c=Cont_month, marker = 'o',s=40) ax1.scatter(Cont_co,Cont_bc,c='r', marker = '>',s=40) ax1.errorbar(Cont_co,Cont_bc,yerr = Cont_bc_err,fmt = None,zorder=0) ax1.plot(Cont_co,Cont_line,color='k') ax1.text(info_x_pos, info_y_pos+0.05 ,'r-square: ' + str(round(Cont_r_value**2,3)),transform=ax1.transAxes, color='grey') ax1.text(info_x_pos, info_y_pos,'slope: ' + str(round(Cont_slope,3)),transform=ax1.transAxes, color='grey') ax1.text(label_x_pos, label_y_pos,'Northern Canada',transform=ax1.transAxes, color='k') #ax1.set_ylabel('rBC ng/kg') ax1.set_xlabel('CO ppbv') ax1.set_xlim(CO_lower,CO_upper) ax1.set_ylim(0,BC_upper) ax1.yaxis.tick_right() ax2.scatter(NPacco,NPacbc,c='b', marker = '<',s=40) ax2.errorbar(NPacco,NPacbc,yerr = NPacbc_err,fmt = None,zorder=0) ax2.plot(NPacco,NPacline,color='k') ax2.text(info_x_pos, (info_y_pos+0.05),'r-square: ' + str(round(NPacr_value**2,3)),transform=ax2.transAxes, color='grey') ax2.text(info_x_pos, info_y_pos,'slope: ' + str(round(NPacslope,3)),transform=ax2.transAxes, color='grey') ax2.text(label_x_pos,label_y_pos,'Northern Pacific',transform=ax2.transAxes, color='k') ax2.set_ylabel('rBC ng/kg') ax2.set_xlabel('CO ppbv') ax2.set_xlim(CO_lower,CO_upper) ax2.set_ylim(0,BC_upper) ax3.scatter(SPacco,SPacbc,c='g', marker = 'o',s=40) ax3.errorbar(SPacco,SPacbc,yerr = SPacbc_err,fmt = None,zorder=0) ax3.plot(SPacco,SPacline,color='k') ax3.text(info_x_pos, info_y_pos+0.05,'r-square: ' + str(round(SPacr_value**2,3)),transform=ax3.transAxes, color='grey') ax3.text(info_x_pos, info_y_pos,'slope: ' + str(round(SPacslope,3)),transform=ax3.transAxes, color='grey') ax3.text(label_x_pos, label_y_pos,'Southern Pacific',transform=ax3.transAxes, color='k') #ax3.set_ylabel('rBC ng/kg') ax3.set_xlabel('CO ppbv') ax3.set_xlim(CO_lower,CO_upper) ax3.set_ylim(0,BC_upper) ax3.set_yticklabels([]) ax4.scatter(WPacco,WPacbc,c='orange', marker = 's',s=40) ax4.errorbar(WPacco,WPacbc,yerr = WPacbc_err,fmt = None,zorder=0) ax4.plot(WPacco,WPacline,color='k') ax4.text(info_x_pos, info_y_pos+0.05,'r-square: ' + str(round(WPacr_value**2,3)),transform=ax4.transAxes, color='grey') ax4.text(info_x_pos, info_y_pos,'slope: ' + str(round(WPacslope,3)),transform=ax4.transAxes, color='grey') ax4.text(label_x_pos, label_y_pos,'Western Pacific/Asia',transform=ax4.transAxes, color='k') #ax4.set_ylabel('rBC ng/kg') ax4.set_xlabel('CO ppbv') ax4.set_xlim(CO_lower,CO_upper) ax4.set_ylim(0,BC_upper) ax4.set_yticklabels([]) plt.subplots_adjust(wspace=0.0) os.chdir('C:/Users/<NAME>/Documents/Data/WHI long term record/CO data/') plt.savefig('BC vs CO - all clusters.png', bbox_inches='tight') plt.show()
[ "matplotlib.pyplot.show", "matplotlib.pyplot.subplot2grid", "numpy.isnan", "datetime.datetime.utcfromtimestamp", "matplotlib.pyplot.figure", "numpy.array", "scipy.stats.linregress", "matplotlib.pyplot.subplots_adjust", "os.chdir", "matplotlib.pyplot.savefig" ]
[((1869, 1886), 'numpy.array', 'np.array', (['Cont_co'], {}), '(Cont_co)\n', (1877, 1886), True, 'import numpy as np\n'), ((1894, 1911), 'numpy.array', 'np.array', (['Cont_bc'], {}), '(Cont_bc)\n', (1902, 1911), True, 'import numpy as np\n'), ((2024, 2064), 'scipy.stats.linregress', 'stats.linregress', (['varx[mask]', 'vary[mask]'], {}), '(varx[mask], vary[mask])\n', (2040, 2064), False, 'from scipy import stats\n'), ((2306, 2322), 'numpy.array', 'np.array', (['SPacco'], {}), '(SPacco)\n', (2314, 2322), True, 'import numpy as np\n'), ((2330, 2346), 'numpy.array', 'np.array', (['SPacbc'], {}), '(SPacbc)\n', (2338, 2346), True, 'import numpy as np\n'), ((2454, 2494), 'scipy.stats.linregress', 'stats.linregress', (['varx[mask]', 'vary[mask]'], {}), '(varx[mask], vary[mask])\n', (2470, 2494), False, 'from scipy import stats\n'), ((2733, 2749), 'numpy.array', 'np.array', (['NPacco'], {}), '(NPacco)\n', (2741, 2749), True, 'import numpy as np\n'), ((2757, 2773), 'numpy.array', 'np.array', (['NPacbc'], {}), '(NPacbc)\n', (2765, 2773), True, 'import numpy as np\n'), ((2881, 2921), 'scipy.stats.linregress', 'stats.linregress', (['varx[mask]', 'vary[mask]'], {}), '(varx[mask], vary[mask])\n', (2897, 2921), False, 'from scipy import stats\n'), ((3160, 3176), 'numpy.array', 'np.array', (['WPacco'], {}), '(WPacco)\n', (3168, 3176), True, 'import numpy as np\n'), ((3184, 3200), 'numpy.array', 'np.array', (['WPacbc'], {}), '(WPacbc)\n', (3192, 3200), True, 'import numpy as np\n'), ((3308, 3348), 'scipy.stats.linregress', 'stats.linregress', (['varx[mask]', 'vary[mask]'], {}), '(varx[mask], vary[mask])\n', (3324, 3348), False, 'from scipy import stats\n'), ((3399, 3426), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(14, 6)'}), '(figsize=(14, 6))\n', (3409, 3426), True, 'import matplotlib.pyplot as plt\n'), ((3443, 3486), 'matplotlib.pyplot.subplot2grid', 'plt.subplot2grid', (['(1, 4)', '(0, 3)'], {'colspan': '(1)'}), '((1, 4), (0, 3), colspan=1)\n', (3459, 3486), True, 'import matplotlib.pyplot as plt\n'), ((3492, 3535), 'matplotlib.pyplot.subplot2grid', 'plt.subplot2grid', (['(1, 4)', '(0, 0)'], {'colspan': '(1)'}), '((1, 4), (0, 0), colspan=1)\n', (3508, 3535), True, 'import matplotlib.pyplot as plt\n'), ((3541, 3584), 'matplotlib.pyplot.subplot2grid', 'plt.subplot2grid', (['(1, 4)', '(0, 1)'], {'colspan': '(1)'}), '((1, 4), (0, 1), colspan=1)\n', (3557, 3584), True, 'import matplotlib.pyplot as plt\n'), ((3590, 3633), 'matplotlib.pyplot.subplot2grid', 'plt.subplot2grid', (['(1, 4)', '(0, 2)'], {'colspan': '(1)'}), '((1, 4), (0, 2), colspan=1)\n', (3606, 3633), True, 'import matplotlib.pyplot as plt\n'), ((6241, 6272), 'matplotlib.pyplot.subplots_adjust', 'plt.subplots_adjust', ([], {'wspace': '(0.0)'}), '(wspace=0.0)\n', (6260, 6272), True, 'import matplotlib.pyplot as plt\n'), ((6276, 6348), 'os.chdir', 'os.chdir', (['"""C:/Users/<NAME>/Documents/Data/WHI long term record/CO data/"""'], {}), "('C:/Users/<NAME>/Documents/Data/WHI long term record/CO data/')\n", (6284, 6348), False, 'import os\n'), ((6349, 6412), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""BC vs CO - all clusters.png"""'], {'bbox_inches': '"""tight"""'}), "('BC vs CO - all clusters.png', bbox_inches='tight')\n", (6360, 6412), True, 'import matplotlib.pyplot as plt\n'), ((6416, 6426), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (6424, 6426), True, 'import matplotlib.pyplot as plt\n'), ((894, 927), 'datetime.datetime.utcfromtimestamp', 'datetime.utcfromtimestamp', (['row[2]'], {}), '(row[2])\n', (919, 927), False, 'from datetime import datetime\n'), ((1920, 1934), 'numpy.isnan', 'np.isnan', (['varx'], {}), '(varx)\n', (1928, 1934), True, 'import numpy as np\n'), ((1938, 1952), 'numpy.isnan', 'np.isnan', (['vary'], {}), '(vary)\n', (1946, 1952), True, 'import numpy as np\n'), ((2355, 2369), 'numpy.isnan', 'np.isnan', (['varx'], {}), '(varx)\n', (2363, 2369), True, 'import numpy as np\n'), ((2373, 2387), 'numpy.isnan', 'np.isnan', (['vary'], {}), '(vary)\n', (2381, 2387), True, 'import numpy as np\n'), ((2782, 2796), 'numpy.isnan', 'np.isnan', (['varx'], {}), '(varx)\n', (2790, 2796), True, 'import numpy as np\n'), ((2800, 2814), 'numpy.isnan', 'np.isnan', (['vary'], {}), '(vary)\n', (2808, 2814), True, 'import numpy as np\n'), ((3209, 3223), 'numpy.isnan', 'np.isnan', (['varx'], {}), '(varx)\n', (3217, 3223), True, 'import numpy as np\n'), ((3227, 3241), 'numpy.isnan', 'np.isnan', (['vary'], {}), '(vary)\n', (3235, 3241), True, 'import numpy as np\n')]
# Lint as: python3 # Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Test keras Model training.""" from absl import flags import numpy as np from pyiree.tf.support import tf_test_utils from pyiree.tf.support import tf_utils from sklearn.preprocessing import PolynomialFeatures import tensorflow as tf FLAGS = flags.FLAGS flags.DEFINE_string( "optimizer_name", "sgd", "optimizer name: sgd, rmsprop, nadam, adamax, adam, adagrad, adadelta") _DEGREE = 3 # polynomial degree of input feature for regression test _FEATURE_SIZE = _DEGREE + 1 # input feature size _BATCH_SIZE = 8 # batch size has to be dynamic TODO(b/142948097) _INPUT_DATA_SHAPE = [_BATCH_SIZE, _FEATURE_SIZE] _OUTPUT_DATA_SHAPE = [_BATCH_SIZE, 1] class ModelTrain(tf.Module): """A module for model training.""" @staticmethod def CreateModule(input_dim=_FEATURE_SIZE, output_dim=1): """Creates a module for regression model training. Args: input_dim: input dimensionality output_dim: output dimensionality Returns: model for linear regression """ tf_utils.set_random_seed() # build a single layer model inputs = tf.keras.layers.Input((input_dim)) outputs = tf.keras.layers.Dense(output_dim)(inputs) model = tf.keras.Model(inputs, outputs) return ModelTrain(model) def __init__(self, model): self.model = model self.loss = tf.keras.losses.MeanSquaredError() self.optimizer = tf.keras.optimizers.get(FLAGS.optimizer_name) @tf.function(input_signature=[ tf.TensorSpec(_INPUT_DATA_SHAPE, tf.float32), tf.TensorSpec(_OUTPUT_DATA_SHAPE, tf.float32) ]) def train_step(self, inputs, targets): with tf.GradientTape() as tape: predictions = self.model(inputs, training=True) loss_value = self.loss(predictions, targets) gradients = tape.gradient(loss_value, self.model.trainable_variables) self.optimizer.apply_gradients( zip(gradients, self.model.trainable_variables)) return loss_value class ModelTrainTest(tf_test_utils.TracedModuleTestCase): def generate_regression_data(self, size=8): x = np.arange(size) - size // 2 y = 1.0 * x**3 + 1.0 * x**2 + 1.0 * x + np.random.randn(size) * size return x, y def test_model_train(self): # Generate input and output data for regression problem. inputs, targets = self.generate_regression_data() # Normalize data. inputs = inputs / max(inputs) targets = targets / max(targets) # Generate polynomial features. inputs = np.expand_dims(inputs, axis=1) polynomial = PolynomialFeatures(_DEGREE) # returns: [1, a, b, a^2, ab, b^2] inputs = polynomial.fit_transform(inputs) targets = np.expand_dims(targets, axis=1) def train_step(module): # Run one iteration of training step. module.train_step(inputs, targets) self.compare_backends(train_step, self._modules) if __name__ == "__main__": if hasattr(tf, "enable_v2_behavior"): tf.enable_v2_behavior() tf_test_utils.compile_tf_module(ModelTrain.CreateModule, exported_names=["train_step"]) tf.test.main()
[ "tensorflow.test.main", "pyiree.tf.support.tf_test_utils.compile_tf_module", "tensorflow.keras.losses.MeanSquaredError", "pyiree.tf.support.tf_utils.set_random_seed", "tensorflow.keras.layers.Dense", "tensorflow.enable_v2_behavior", "numpy.random.randn", "tensorflow.keras.optimizers.get", "numpy.expand_dims", "absl.flags.DEFINE_string", "tensorflow.keras.Model", "sklearn.preprocessing.PolynomialFeatures", "numpy.arange", "tensorflow.keras.layers.Input", "tensorflow.TensorSpec", "tensorflow.GradientTape" ]
[((852, 972), 'absl.flags.DEFINE_string', 'flags.DEFINE_string', (['"""optimizer_name"""', '"""sgd"""', '"""optimizer name: sgd, rmsprop, nadam, adamax, adam, adagrad, adadelta"""'], {}), "('optimizer_name', 'sgd',\n 'optimizer name: sgd, rmsprop, nadam, adamax, adam, adagrad, adadelta')\n", (871, 972), False, 'from absl import flags\n'), ((3518, 3610), 'pyiree.tf.support.tf_test_utils.compile_tf_module', 'tf_test_utils.compile_tf_module', (['ModelTrain.CreateModule'], {'exported_names': "['train_step']"}), "(ModelTrain.CreateModule, exported_names=[\n 'train_step'])\n", (3549, 3610), False, 'from pyiree.tf.support import tf_test_utils\n'), ((3642, 3656), 'tensorflow.test.main', 'tf.test.main', ([], {}), '()\n', (3654, 3656), True, 'import tensorflow as tf\n'), ((1601, 1627), 'pyiree.tf.support.tf_utils.set_random_seed', 'tf_utils.set_random_seed', ([], {}), '()\n', (1625, 1627), False, 'from pyiree.tf.support import tf_utils\n'), ((1675, 1707), 'tensorflow.keras.layers.Input', 'tf.keras.layers.Input', (['input_dim'], {}), '(input_dim)\n', (1696, 1707), True, 'import tensorflow as tf\n'), ((1778, 1809), 'tensorflow.keras.Model', 'tf.keras.Model', (['inputs', 'outputs'], {}), '(inputs, outputs)\n', (1792, 1809), True, 'import tensorflow as tf\n'), ((1908, 1942), 'tensorflow.keras.losses.MeanSquaredError', 'tf.keras.losses.MeanSquaredError', ([], {}), '()\n', (1940, 1942), True, 'import tensorflow as tf\n'), ((1964, 2009), 'tensorflow.keras.optimizers.get', 'tf.keras.optimizers.get', (['FLAGS.optimizer_name'], {}), '(FLAGS.optimizer_name)\n', (1987, 2009), True, 'import tensorflow as tf\n'), ((3046, 3076), 'numpy.expand_dims', 'np.expand_dims', (['inputs'], {'axis': '(1)'}), '(inputs, axis=1)\n', (3060, 3076), True, 'import numpy as np\n'), ((3094, 3121), 'sklearn.preprocessing.PolynomialFeatures', 'PolynomialFeatures', (['_DEGREE'], {}), '(_DEGREE)\n', (3112, 3121), False, 'from sklearn.preprocessing import PolynomialFeatures\n'), ((3219, 3250), 'numpy.expand_dims', 'np.expand_dims', (['targets'], {'axis': '(1)'}), '(targets, axis=1)\n', (3233, 3250), True, 'import numpy as np\n'), ((3492, 3515), 'tensorflow.enable_v2_behavior', 'tf.enable_v2_behavior', ([], {}), '()\n', (3513, 3515), True, 'import tensorflow as tf\n'), ((1724, 1757), 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (['output_dim'], {}), '(output_dim)\n', (1745, 1757), True, 'import tensorflow as tf\n'), ((2203, 2220), 'tensorflow.GradientTape', 'tf.GradientTape', ([], {}), '()\n', (2218, 2220), True, 'import tensorflow as tf\n'), ((2638, 2653), 'numpy.arange', 'np.arange', (['size'], {}), '(size)\n', (2647, 2653), True, 'import numpy as np\n'), ((2050, 2094), 'tensorflow.TensorSpec', 'tf.TensorSpec', (['_INPUT_DATA_SHAPE', 'tf.float32'], {}), '(_INPUT_DATA_SHAPE, tf.float32)\n', (2063, 2094), True, 'import tensorflow as tf\n'), ((2102, 2147), 'tensorflow.TensorSpec', 'tf.TensorSpec', (['_OUTPUT_DATA_SHAPE', 'tf.float32'], {}), '(_OUTPUT_DATA_SHAPE, tf.float32)\n', (2115, 2147), True, 'import tensorflow as tf\n'), ((2710, 2731), 'numpy.random.randn', 'np.random.randn', (['size'], {}), '(size)\n', (2725, 2731), True, 'import numpy as np\n')]
import io import gc import traceback import subprocess import numpy as np import pandas as pd import matplotlib.pyplot as plt from fastprogress.fastprogress import master_bar, progress_bar import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import Dataset, DataLoader import albumentations as A from albumentations.pytorch import ToTensorV2 # from copy import deepcopy """ Models """ class BaseModel(nn.Module): def __init__(self, model): super().__init__() if isinstance(model, str): self = torch.load(model, map_location='cpu') else: self.model = model def forward(self, x): return self.model(x) @property def device(self): return next(self.parameters()).device def save(self, filename): device = self.device self.to('cpu') torch.save(self, filename) self.to(device) class SegmentationModel(BaseModel): def step(self, batch, criterion): img, mask = batch["image"], (batch["mask"] > .5).long() pred = self(img) loss = criterion(pred, mask) return loss class RegressionModel(BaseModel): def step(self, batch, criterion): img, mask = batch["image"], batch["target"] pred = self(img) loss = criterion(pred, mask) return loss class ClassificationModel(BaseModel): def step(self, batch, criterion): img, label = batch["image"], batch["label"] pred = self(img) loss = criterion(pred, label) return loss """ Datasets """ class BaseDataset(Dataset): def __len__(self): return len(self.df) * self.fold def __getitem__(self, idx): return self.aug(**self.df.iloc[idx % len(self.df)]) def __repr__(self): sample = self[0] print("N =", len(self)) for k, v in sample.items(): print(k, v.dtype, v.shape, sep="\t") show(sample) return "" class SegmentationDataset(BaseDataset): def __init__(self, df, aug=A.NoOp(), fold=1): """ df.iloc[:, 0]: (C, H, W) float32 df.iloc[:, 1]: (H, W) float32 """ self.df = pd.DataFrame(dict( image=df.iloc[:, 0], mask=df.iloc[:, 1] )) self.aug = A.Compose([ aug, ToTensorV2() ]) self.fold = fold class RegressionDataset(BaseDataset): def __init__(self, df, aug=A.NoOp(), fold=1): """ df.iloc[:, 0]: (C, H, W) float32 df.iloc[:, 1]: (C, H, W) float32 """ self.df = pd.DataFrame(dict( image=df.iloc[:, 0], target=df.iloc[:, 1] )) self.aug = A.Compose([ aug, ToTensorV2() ], additional_targets=dict( target="image" )) self.fold = fold class ClassificationDataset(BaseDataset): def __init__(self, df, aug=A.NoOp(), fold=1): """ df.iloc[:, 0]: (C, H, W) float32 df.iloc[:, 1]: long """ self.df = pd.DataFrame(dict( image=df.iloc[:, 0], label=df.iloc[:, 1] )) self.aug = A.Compose([ aug, ToTensorV2() ]) self.fold = fold class ClassificationOnMaskDataset(BaseDataset): def __init__(self, df, aug=A.NoOp(), fold=1): """ df.iloc[:, 0]: (C, H, W) float32 df.iloc[:, 1]: (H, W) float32 df.iloc[:, 2]: long """ self.df = pd.DataFrame(dict( image=df.iloc[:, 0], mask=df.iloc[:, 1], label=df.iloc[:, 2] )) self.aug = A.Compose([ aug, ToTensorV2() ]) self.fold = fold """ Trainer """ class Trainer: def __init__(self, name, model, batch_size, train_set, val_set=None, device=None): self.name = name self.model = model self.batch_size = batch_size self.train_set = train_set self.val_set = val_set self.device = device self.best_model = None self.last_model = None plt.rcParams["figure.facecolor"] = "white" def __auto_select(): output = subprocess.check_output([ "nvidia-smi", "--format=csv", "--query-gpu=memory.used" ]) df = pd.read_csv(io.BytesIO(output), names=["used_memory"], skiprows=1) df.used_memory = ( df.used_memory .apply(lambda used_memory: int(used_memory[:-4])) ) return torch.device(f"cuda:{df.used_memory.idxmin()}") def fit(self, epochs, criterion): try: device = self.device if self.device else Trainer.__auto_select() self.model.to(device) optimizer = optim.Adam(self.model.parameters()) train_loader = DataLoader( self.train_set, shuffle=True, batch_size=self.batch_size, num_workers=8 ) if self.val_set: val_loader = DataLoader( self.val_set, shuffle=False, batch_size=self.batch_size, num_workers=8 ) mb = master_bar(range(1, epochs + 1)) if self.val_set: mb.names = ["valid", "train"] else: mb.names = ["train"] train_losses, val_losses = [], [] for epoch in mb: train_loss, val_loss = 0, 0 x = range(1, epoch+1) self.model.train() for batch in progress_bar(train_loader, parent=mb): batch = {k: v.to(device) for k, v in batch.items()} loss = self.model.step(batch, criterion) train_loss += loss.item() optimizer.zero_grad() loss.backward() optimizer.step() batch = loss = None train_loss /= len(self.train_set) train_losses.append(train_loss) if self.val_set: self.model.eval() with torch.no_grad(): for batch in progress_bar(val_loader, parent=mb): batch = {k: v.to(device) for k, v in batch.items()} loss = self.model.step(batch, criterion) val_loss += loss.item() batch = loss = None val_loss /= len(self.val_set) val_losses.append(val_loss) graphs = [[x, val_losses], [x, train_losses]] y = np.concatenate((train_losses, val_losses)) else: graphs = [[x, train_losses]] y = train_losses x_margin = 0.2 y_margin = 0.05 x_bounds = [1-x_margin, epoch+x_margin] y_bounds = [np.min(y)-y_margin, np.max(y)+y_margin] mb.update_graph(graphs, x_bounds, y_bounds) if val_loss <= min(val_losses): self.best_model = f"models/{self.name}_{epoch:04d}.pth" self.model.save(self.best_model) print(self.best_model, val_loss) self.last_model = f"models/{self.name}_{epoch:04d}.pth" self.model.save(self.last_model) print() print("last_model:", self.last_model, val_loss) print("best_model:", self.best_model, min(val_losses)) except: traceback.print_exc() finally: batch = loss = optimizer = None self.model.cpu() gc.collect() torch.cuda.empty_cache()
[ "albumentations.pytorch.ToTensorV2", "albumentations.NoOp", "io.BytesIO", "traceback.print_exc", "torch.utils.data.DataLoader", "subprocess.check_output", "torch.load", "fastprogress.fastprogress.progress_bar", "torch.save", "gc.collect", "numpy.min", "numpy.max", "torch.cuda.empty_cache", "torch.no_grad", "numpy.concatenate" ]
[((891, 917), 'torch.save', 'torch.save', (['self', 'filename'], {}), '(self, filename)\n', (901, 917), False, 'import torch\n'), ((2098, 2106), 'albumentations.NoOp', 'A.NoOp', ([], {}), '()\n', (2104, 2106), True, 'import albumentations as A\n'), ((2519, 2527), 'albumentations.NoOp', 'A.NoOp', ([], {}), '()\n', (2525, 2527), True, 'import albumentations as A\n'), ((3004, 3012), 'albumentations.NoOp', 'A.NoOp', ([], {}), '()\n', (3010, 3012), True, 'import albumentations as A\n'), ((3426, 3434), 'albumentations.NoOp', 'A.NoOp', ([], {}), '()\n', (3432, 3434), True, 'import albumentations as A\n'), ((4327, 4413), 'subprocess.check_output', 'subprocess.check_output', (["['nvidia-smi', '--format=csv', '--query-gpu=memory.used']"], {}), "(['nvidia-smi', '--format=csv',\n '--query-gpu=memory.used'])\n", (4350, 4413), False, 'import subprocess\n'), ((562, 599), 'torch.load', 'torch.load', (['model'], {'map_location': '"""cpu"""'}), "(model, map_location='cpu')\n", (572, 599), False, 'import torch\n'), ((4449, 4467), 'io.BytesIO', 'io.BytesIO', (['output'], {}), '(output)\n', (4459, 4467), False, 'import io\n'), ((4968, 5055), 'torch.utils.data.DataLoader', 'DataLoader', (['self.train_set'], {'shuffle': '(True)', 'batch_size': 'self.batch_size', 'num_workers': '(8)'}), '(self.train_set, shuffle=True, batch_size=self.batch_size,\n num_workers=8)\n', (4978, 5055), False, 'from torch.utils.data import Dataset, DataLoader\n'), ((8065, 8077), 'gc.collect', 'gc.collect', ([], {}), '()\n', (8075, 8077), False, 'import gc\n'), ((8090, 8114), 'torch.cuda.empty_cache', 'torch.cuda.empty_cache', ([], {}), '()\n', (8112, 8114), False, 'import torch\n'), ((2395, 2407), 'albumentations.pytorch.ToTensorV2', 'ToTensorV2', ([], {}), '()\n', (2405, 2407), False, 'from albumentations.pytorch import ToTensorV2\n'), ((2818, 2830), 'albumentations.pytorch.ToTensorV2', 'ToTensorV2', ([], {}), '()\n', (2828, 2830), False, 'from albumentations.pytorch import ToTensorV2\n'), ((3289, 3301), 'albumentations.pytorch.ToTensorV2', 'ToTensorV2', ([], {}), '()\n', (3299, 3301), False, 'from albumentations.pytorch import ToTensorV2\n'), ((3784, 3796), 'albumentations.pytorch.ToTensorV2', 'ToTensorV2', ([], {}), '()\n', (3794, 3796), False, 'from albumentations.pytorch import ToTensorV2\n'), ((5189, 5275), 'torch.utils.data.DataLoader', 'DataLoader', (['self.val_set'], {'shuffle': '(False)', 'batch_size': 'self.batch_size', 'num_workers': '(8)'}), '(self.val_set, shuffle=False, batch_size=self.batch_size,\n num_workers=8)\n', (5199, 5275), False, 'from torch.utils.data import Dataset, DataLoader\n'), ((5806, 5843), 'fastprogress.fastprogress.progress_bar', 'progress_bar', (['train_loader'], {'parent': 'mb'}), '(train_loader, parent=mb)\n', (5818, 5843), False, 'from fastprogress.fastprogress import master_bar, progress_bar\n'), ((7941, 7962), 'traceback.print_exc', 'traceback.print_exc', ([], {}), '()\n', (7960, 7962), False, 'import traceback\n'), ((6939, 6981), 'numpy.concatenate', 'np.concatenate', (['(train_losses, val_losses)'], {}), '((train_losses, val_losses))\n', (6953, 6981), True, 'import numpy as np\n'), ((6390, 6405), 'torch.no_grad', 'torch.no_grad', ([], {}), '()\n', (6403, 6405), False, 'import torch\n'), ((6444, 6479), 'fastprogress.fastprogress.progress_bar', 'progress_bar', (['val_loader'], {'parent': 'mb'}), '(val_loader, parent=mb)\n', (6456, 6479), False, 'from fastprogress.fastprogress import master_bar, progress_bar\n'), ((7254, 7263), 'numpy.min', 'np.min', (['y'], {}), '(y)\n', (7260, 7263), True, 'import numpy as np\n'), ((7274, 7283), 'numpy.max', 'np.max', (['y'], {}), '(y)\n', (7280, 7283), True, 'import numpy as np\n')]
""" This module is used to display the Density of States (DoS). """ from futile.Utils import write as safe_print AU_eV = 27.21138386 class DiracSuperposition(): """ Defines as superposition of Dirac deltas which can be used to plot the density of states """ def __init__(self, dos, wgts=[1.0]): """ Parameters: dos: array containing the density of states per eack k-point. Should be of shape 2 wgts: contains the weights of each of the k-points """ import numpy as np self.dos = dos if isinstance(wgts, float): self.norm = [wgts] else: self.norm = wgts # set range for this distribution e_min = 1.e100 e_max = -1.e100 ddos = np.ravel(dos) if len(ddos) > 0: e_min = min(e_min, np.min(ddos) - 0.05 * (np.max(ddos) - np.min(ddos))) e_max = max(e_max, np.max(ddos) + 0.05 * (np.max(ddos) - np.min(ddos))) self.xlim = (e_min, e_max) def curve(self, xs, sigma, wgts=None): from numpy import ones dos_g = 0.0 idos = 0 for norm, dos in zip(self.norm, self.dos): if wgts is not None: norms = wgts[idos]*norm idos += 1 else: norms = ones(len(dos))*norm kptcurve = self.peaks(xs, dos, norms, sigma) dos_g += kptcurve return xs, dos_g def peak(self, omega, e, sigma): """ Define if a peak is a Gaussian or a Lorenzian (temporarily only the gaussian is defined) """ import numpy as np nfac = np.sqrt(2.0*np.pi) val = np.exp(- (omega - e)**2 / (2.0 * sigma**2))/(nfac*sigma) return val def peaks(self, xs, dos, norms, sigma): """ Return the array of the whole set of peaks """ curve = 0.0 for e, nrm in zip(dos, norms): curve += self.peak(xs, e, sigma)*nrm return curve def _bandarray_to_data(jspin, bandarrays): lbl = 'up' if jspin == 0 else 'dw' kptlists = [[], []] for orbs in bandarrays: for ispin, norbs in enumerate(orbs.info): if norbs == 0 or ispin != jspin: continue # energy values kptlists[0].append(orbs[ispin, :norbs]) # normalization kptlists[1].append(orbs.kwgt*(1.0-2*ispin)) # print 'kpt',kptlists return kptlists, lbl class DoS(): """ Definition of the density of state class """ def __init__(self, bandarrays=None, energies=None, evals=None, units='eV', label='1', sigma=0.2, npts=2500, fermi_level=None, norm=1.0, sdos=None, e_min=None, e_max=None): """ Extract a quantity which is associated to the DoS, that can be plotted """ import numpy as np self.ens = [] self.labels = [] self.ef = None # self.norms=[] self.npts = npts if e_min is not None: self.e_min = e_min else: self.e_min = 1.e100 if e_max is not None: self.e_max = e_max else: self.e_max = -1.e100 if bandarrays is not None: self.append_from_bandarray(bandarrays, label) if evals is not None: self.append_from_dict(evals, label) if energies is not None: self.append(np.array([energies]), label=label, units=units, norm=(np.array([norm]) if isinstance(norm, float) else norm)) self.sigma = sigma self.fermi_level(fermi_level, units=units) if sdos is not None: self._embed_sdos(sdos) def _embed_sdos(self, sdos): self.sdos = [] for i, xdos in enumerate(sdos): self.sdos.append({'coord': xdos['coord']}) jdos = 0 for subspin in xdos['dos']: if len(subspin[0]) == 0: continue d = {'doslist': subspin} try: self.ens[jdos]['sdos'].append(d) except KeyError: self.ens[jdos]['sdos'] = [d] jdos += 1 def append_from_bandarray(self, bandarrays, label): """ Add a new band array to the previous DoS. Important for kpoints DOS """ import numpy as np for jspin in range(2): kptlists, lbl = _bandarray_to_data(jspin, bandarrays) self.append(np.array(kptlists[0]), label=label+lbl, units='AU', norm=np.array(kptlists[1])) def append_from_dict(self, evals, label): import numpy as np "Get the energies from the different flavours given by the dict" evs = [[], []] ef = None for ev in evals: occ = self.get_ev(ev, ['e_occ', 'e_occupied']) if occ: ef = max(occ) vrt = self.get_ev(ev, ['e_vrt', 'e_virt']) eigen = False if occ: eigen = occ if vrt: eigen = vrt if not eigen: eigen = self.get_ev(ev) if not occ and not vrt and eigen: ef = max(eigen) if not eigen: continue for i, e in enumerate(eigen): if e: evs[i].append(e) for i, energs in enumerate(evs): if len(energs) == 0: continue self.append(np.array(energs), label=label, units='AU', norm=1.0-2.0*i) if ef: self.fermi_level(ef, units='AU') def get_ev(self, ev, keys=None): "Get the correct list of the energies for this eigenvalue" res = False if keys is None: ener = ev.get('e') spin = ev.get('s') if ener and spin == 1: res = [ener] elif ener and spin == -1: res = [None, ener] else: for k in keys: if k in ev: res = ev[k] if not isinstance(res, list): res = [res] break return res def append(self, energies, label=None, units='eV', norm=1.0): if not isinstance(norm, float) and len(norm) == 0: return dos = self.conversion_factor(units)*energies self.ens.append({'dos': DiracSuperposition(dos, wgts=norm)}) lbl = label if label is not None else str(len(self.labels)+1) self.labels.append(lbl) # self.norms.append(norm) self.range = self._set_range() def conversion_factor(self, units): if units == 'AU': fac = AU_eV elif units == 'eV': fac = 1.0 else: raise ValueError('Unrecognized units ('+units+')') return fac def fermi_level(self, fermi_level, units='eV'): if fermi_level is not None: self.ef = fermi_level*self.conversion_factor(units) def _set_range(self, npts=None, e_min=None, e_max=None): import numpy as np if npts is None: npts = self.npts if e_min is None: e_min = self.e_min if e_max is None: e_max = self.e_max for dos in self.ens: mn, mx = dos['dos'].xlim e_min = min(e_min, mn) e_max = max(e_max, mx) return np.arange(e_min, e_max, (e_max-e_min)/npts) def curve(self, dos, norm, sigma=None): import numpy as np if sigma is None: sigma = self.sigma nrm = np.sqrt(2.0*np.pi)*sigma/norm dos_g = [] for e_i in self.range: if len(dos.shape) == 2: nkpt = dos.shape[0] value = 0.0 for ikpt in range(nkpt): value += np.sum(np.exp(- (e_i - dos[ikpt, :])**2 / (2.0 * sigma**2))/nrm[ikpt]) else: value = np.sum( np.exp(- (e_i - dos[:])**2 / (2.0 * sigma**2))/nrm) # Append data corresponding to each energy grid dos_g.append(value) return np.array(dos_g) def dump(self, sigma=None): "For Gnuplot" if sigma is None: sigma = self.sigma # data=[self.curve(dos,norm=self.norms[i],sigma=sigma) # for i,dos in enumerate(self.ens)] data = [dos['dos'].curve(self.range, sigma=sigma)[1] for dos in self.ens] for i, e in enumerate(self.range): safe_print(e, ' '.join(map(str, [d[i] for d in data]))) def plot(self, sigma=None, legend=True, xlmin=None, xlmax=None, ylmin=None, ylmax=None, width=6.4, height=4.8): import matplotlib.pyplot as plt from matplotlib.widgets import Slider # , Button, RadioButtons if sigma is None: sigma = self.sigma self.fig, self.ax1 = plt.subplots(figsize=(width, height)) self.plotl = [] for i, dos in enumerate(self.ens): # self.plotl.append(self.ax1.plot(self.range,self.curve(dos,norm=self.norms[i],sigma=sigma),label=self.labels[i])) self.plotl.append(self.ax1.plot( *dos['dos'].curve(self.range, sigma=sigma), label=self.labels[i])) if xlmax is not None: plt.xlim(xmax=xlmax) if xlmin is not None: plt.xlim(xmin=xlmin) if ylmax is not None: plt.ylim(ymax=ylmax) if ylmin is not None: plt.ylim(ymin=ylmin) plt.xlabel('Energy [eV]', fontsize=18) plt.ylabel('DoS', fontsize=18) if self.ef is not None: plt.axvline(self.ef, color='k', linestyle='--') # self.ax1.annotate('Fermi level', xy=(self.ef,2), # xytext=(self.ef, 10), # arrowprops=dict(facecolor='white', shrink=0.05), # ) if len(self.labels) > 1 and legend: plt.legend(loc='best') axcolor = 'lightgoldenrodyellow' try: axsigma = plt.axes([0.2, 0.93, 0.65, 0.03], facecolor=axcolor) except AttributeError: axsigma = plt.axes([0.2, 0.93, 0.65, 0.03], axisbg=axcolor) self.ssig = Slider(axsigma, 'Smearing', 0.0, 0.4, valinit=sigma) self.ssig.on_changed(self.update) if hasattr(self, 'sdos') and self.sdos: self._set_sdos_selector() self._set_sdos() plt.show() def _set_sdos_selector(self): import matplotlib.pyplot as plt from matplotlib.widgets import RadioButtons self.sdos_selector = RadioButtons( plt.axes([0.93, 0.05, 0.04, 0.11], axisbg='lightgoldenrodyellow'), ('x', 'y', 'z'), active=1) self.isdos = 1 self.sdos_selector.on_clicked(self._update_sdos) def _set_sdos(self): import numpy xs = self.sdos[self.isdos]['coord'] self._set_sdos_sliders(numpy.min(xs), numpy.max(xs)) self._update_sdos(0.0) # fake value as it is unused def _sdos_curve(self, sdos, vmin, vmax): import numpy xs = self.sdos[self.isdos]['coord'] imin = numpy.argmin(numpy.abs(xs-vmin)) imax = numpy.argmin(numpy.abs(xs-vmax)) doslist = sdos[self.isdos]['doslist'] # norms=self.sdos[self.isdos]['norms'][ispin] tocurve = [0.0 for i in doslist[imin]] for d in doslist[imin:imax+1]: tocurve = [t+dd for t, dd in zip(tocurve, d)] # tocurve=numpy.sum([ d[ispin] for d in doslist[imin:imax+1]],axis=0) return tocurve # float(len(xs))/float(imax+1-imin)*tocurve,norms def _update_sdos(self, val): isdos = self.isdos if val == 'x': isdos = 0 elif val == 'y': isdos = 1 elif val == 'z': isdos = 2 if isdos != self.isdos: self.isdos = isdos self._set_sdos() vmin, vmax = (s.val for s in self.ssdos) if vmax < vmin: self.ssdos[1].set_val(vmin) vmax = vmin if vmin > vmax: self.ssdos[0].set_val(vmax) vmin = vmax # now plot the sdos curve associated to the given value sig = self.ssig.val curves = [] for dos in self.ens: if 'sdos' not in dos: continue renorms = self._sdos_curve(dos['sdos'], vmin, vmax) curve = dos['dos'].curve(self.range, sigma=sig, wgts=renorms) curves.append(curve) if hasattr(self, '_sdos_plots'): for pl, curve in zip(self._sdos_plots, curves): pl[0].set_ydata(curve[1]) else: self._sdos_plots = [] for c in curves: self._sdos_plots.append( self.ax1.plot(*c, label='sdos')) self.ax1.relim() self.ax1.autoscale_view() self.fig.canvas.draw_idle() def _set_sdos_sliders(self, cmin, cmax): import matplotlib.pyplot as plt from futile.Figures import VertSlider if hasattr(self, 'ssdos'): self.ssdos[0].ax.clear() self.ssdos[0].__init__( self.ssdos[0].ax, 'SDos', cmin, cmax, valinit=cmin) self.ssdos[1].ax.clear() self.ssdos[1].__init__(self.ssdos[1].ax, '', cmin, cmax, valinit=cmax) else: axcolor = 'red' axmin = plt.axes([0.93, 0.2, 0.02, 0.65], axisbg=axcolor) axmax = plt.axes([0.95, 0.2, 0.02, 0.65], axisbg=axcolor) self.ssdos = [ VertSlider(axmin, 'SDos', cmin, cmax, valinit=cmin), VertSlider(axmax, '', cmin, cmax, valinit=cmax)] self.ssdos[0].valtext.set_ha('right') self.ssdos[1].valtext.set_ha('left') self.ssdos[0].on_changed(self._update_sdos) self.ssdos[1].on_changed(self._update_sdos) def update(self, val): sig = self.ssig.val for i, dos in enumerate(self.ens): self.plotl[i][0].set_ydata( dos['dos'].curve(self.range, sigma=sig)[1]) # self.plotl[i][0].set_ydata(self.curve(dos,norm=self.norms[i],sigma=sig)) self.ax1.relim() self.ax1.autoscale_view() self.fig.canvas.draw_idle() if __name__ == "__main__": import numpy as np energies = np.array([-0.815924953235059, -0.803163374736654, -0.780540200987971, -0.7508806541364, -0.723626807289917, -0.714924448617026, -0.710448085701742, -0.68799028016451, -0.67247569974853, -0.659038909236607, -0.625396293324399, -0.608009041659988, -0.565337910777367, -0.561250536074343, -0.551767438323268, -0.541295070404525, -0.532326667587434, -0.515961980147107, -0.474601108285518, -0.473408476151224, -0.46509070541069, -0.445709086452906, -0.433874403837837, -0.416121660651406, -0.407871082254237, -0.406123490618786, -0.403004188319382, -0.38974739285104, -0.380837488456638, -0.375163102271681, -0.375007771592681, -0.367898783582561, -0.367518948507212, -0.359401585874402, -0.358189406008502, -0.354517727598174, -0.334286389724978, -0.332921810616845, -0.315466259109401, -0.308028853904577, -0.29864142362141, -0.294024743731349, -0.292104129933301, -0.285165738729842, -0.28419932605141, -0.267399999874122, -0.259487769142101, -0.239899780812716, -0.224858003804207, -0.20448050758473, -0.164155133452971, -0.117617164459898, -0.0717938081884113, -0.0526986239898579, -0.0346031190163735, -0.0167949342608791, -0.0135168064347152, -0.0102971895842409, 0.00759271179427191, 0.00974950976249545, 0.010176021051287, 0.0217652761059223, 0.0239924727094222, 0.0413057846713024, 0.0422334333464529, 0.0459150454793617, 0.0517637894860314]) dos = DoS(energies, fermi_level=-0.1) dos.append(0.2+energies) dos.dump(sigma=0.01) dos.plot()
[ "numpy.abs", "numpy.ravel", "matplotlib.pyplot.axes", "matplotlib.widgets.Slider", "numpy.arange", "numpy.exp", "matplotlib.pyplot.axvline", "futile.Figures.VertSlider", "numpy.max", "matplotlib.pyplot.subplots", "matplotlib.pyplot.show", "matplotlib.pyplot.ylim", "matplotlib.pyplot.legend", "numpy.min", "matplotlib.pyplot.ylabel", "matplotlib.pyplot.xlim", "numpy.array", "matplotlib.pyplot.xlabel", "numpy.sqrt" ]
[((14705, 16163), 'numpy.array', 'np.array', (['[-0.815924953235059, -0.803163374736654, -0.780540200987971, -\n 0.7508806541364, -0.723626807289917, -0.714924448617026, -\n 0.710448085701742, -0.68799028016451, -0.67247569974853, -\n 0.659038909236607, -0.625396293324399, -0.608009041659988, -\n 0.565337910777367, -0.561250536074343, -0.551767438323268, -\n 0.541295070404525, -0.532326667587434, -0.515961980147107, -\n 0.474601108285518, -0.473408476151224, -0.46509070541069, -\n 0.445709086452906, -0.433874403837837, -0.416121660651406, -\n 0.407871082254237, -0.406123490618786, -0.403004188319382, -\n 0.38974739285104, -0.380837488456638, -0.375163102271681, -\n 0.375007771592681, -0.367898783582561, -0.367518948507212, -\n 0.359401585874402, -0.358189406008502, -0.354517727598174, -\n 0.334286389724978, -0.332921810616845, -0.315466259109401, -\n 0.308028853904577, -0.29864142362141, -0.294024743731349, -\n 0.292104129933301, -0.285165738729842, -0.28419932605141, -\n 0.267399999874122, -0.259487769142101, -0.239899780812716, -\n 0.224858003804207, -0.20448050758473, -0.164155133452971, -\n 0.117617164459898, -0.0717938081884113, -0.0526986239898579, -\n 0.0346031190163735, -0.0167949342608791, -0.0135168064347152, -\n 0.0102971895842409, 0.00759271179427191, 0.00974950976249545, \n 0.010176021051287, 0.0217652761059223, 0.0239924727094222, \n 0.0413057846713024, 0.0422334333464529, 0.0459150454793617, \n 0.0517637894860314]'], {}), '([-0.815924953235059, -0.803163374736654, -0.780540200987971, -\n 0.7508806541364, -0.723626807289917, -0.714924448617026, -\n 0.710448085701742, -0.68799028016451, -0.67247569974853, -\n 0.659038909236607, -0.625396293324399, -0.608009041659988, -\n 0.565337910777367, -0.561250536074343, -0.551767438323268, -\n 0.541295070404525, -0.532326667587434, -0.515961980147107, -\n 0.474601108285518, -0.473408476151224, -0.46509070541069, -\n 0.445709086452906, -0.433874403837837, -0.416121660651406, -\n 0.407871082254237, -0.406123490618786, -0.403004188319382, -\n 0.38974739285104, -0.380837488456638, -0.375163102271681, -\n 0.375007771592681, -0.367898783582561, -0.367518948507212, -\n 0.359401585874402, -0.358189406008502, -0.354517727598174, -\n 0.334286389724978, -0.332921810616845, -0.315466259109401, -\n 0.308028853904577, -0.29864142362141, -0.294024743731349, -\n 0.292104129933301, -0.285165738729842, -0.28419932605141, -\n 0.267399999874122, -0.259487769142101, -0.239899780812716, -\n 0.224858003804207, -0.20448050758473, -0.164155133452971, -\n 0.117617164459898, -0.0717938081884113, -0.0526986239898579, -\n 0.0346031190163735, -0.0167949342608791, -0.0135168064347152, -\n 0.0102971895842409, 0.00759271179427191, 0.00974950976249545, \n 0.010176021051287, 0.0217652761059223, 0.0239924727094222, \n 0.0413057846713024, 0.0422334333464529, 0.0459150454793617, \n 0.0517637894860314])\n', (14713, 16163), True, 'import numpy as np\n'), ((800, 813), 'numpy.ravel', 'np.ravel', (['dos'], {}), '(dos)\n', (808, 813), True, 'import numpy as np\n'), ((1736, 1756), 'numpy.sqrt', 'np.sqrt', (['(2.0 * np.pi)'], {}), '(2.0 * np.pi)\n', (1743, 1756), True, 'import numpy as np\n'), ((7652, 7699), 'numpy.arange', 'np.arange', (['e_min', 'e_max', '((e_max - e_min) / npts)'], {}), '(e_min, e_max, (e_max - e_min) / npts)\n', (7661, 7699), True, 'import numpy as np\n'), ((8432, 8447), 'numpy.array', 'np.array', (['dos_g'], {}), '(dos_g)\n', (8440, 8447), True, 'import numpy as np\n'), ((9211, 9248), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {'figsize': '(width, height)'}), '(figsize=(width, height))\n', (9223, 9248), True, 'import matplotlib.pyplot as plt\n'), ((9847, 9885), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Energy [eV]"""'], {'fontsize': '(18)'}), "('Energy [eV]', fontsize=18)\n", (9857, 9885), True, 'import matplotlib.pyplot as plt\n'), ((9894, 9924), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""DoS"""'], {'fontsize': '(18)'}), "('DoS', fontsize=18)\n", (9904, 9924), True, 'import matplotlib.pyplot as plt\n'), ((10537, 10589), 'matplotlib.widgets.Slider', 'Slider', (['axsigma', '"""Smearing"""', '(0.0)', '(0.4)'], {'valinit': 'sigma'}), "(axsigma, 'Smearing', 0.0, 0.4, valinit=sigma)\n", (10543, 10589), False, 'from matplotlib.widgets import Slider\n'), ((10755, 10765), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (10763, 10765), True, 'import matplotlib.pyplot as plt\n'), ((1769, 1815), 'numpy.exp', 'np.exp', (['(-(omega - e) ** 2 / (2.0 * sigma ** 2))'], {}), '(-(omega - e) ** 2 / (2.0 * sigma ** 2))\n', (1775, 1815), True, 'import numpy as np\n'), ((9629, 9649), 'matplotlib.pyplot.xlim', 'plt.xlim', ([], {'xmax': 'xlmax'}), '(xmax=xlmax)\n', (9637, 9649), True, 'import matplotlib.pyplot as plt\n'), ((9692, 9712), 'matplotlib.pyplot.xlim', 'plt.xlim', ([], {'xmin': 'xlmin'}), '(xmin=xlmin)\n', (9700, 9712), True, 'import matplotlib.pyplot as plt\n'), ((9755, 9775), 'matplotlib.pyplot.ylim', 'plt.ylim', ([], {'ymax': 'ylmax'}), '(ymax=ylmax)\n', (9763, 9775), True, 'import matplotlib.pyplot as plt\n'), ((9818, 9838), 'matplotlib.pyplot.ylim', 'plt.ylim', ([], {'ymin': 'ylmin'}), '(ymin=ylmin)\n', (9826, 9838), True, 'import matplotlib.pyplot as plt\n'), ((9969, 10016), 'matplotlib.pyplot.axvline', 'plt.axvline', (['self.ef'], {'color': '"""k"""', 'linestyle': '"""--"""'}), "(self.ef, color='k', linestyle='--')\n", (9980, 10016), True, 'import matplotlib.pyplot as plt\n'), ((10261, 10283), 'matplotlib.pyplot.legend', 'plt.legend', ([], {'loc': '"""best"""'}), "(loc='best')\n", (10271, 10283), True, 'import matplotlib.pyplot as plt\n'), ((10360, 10412), 'matplotlib.pyplot.axes', 'plt.axes', (['[0.2, 0.93, 0.65, 0.03]'], {'facecolor': 'axcolor'}), '([0.2, 0.93, 0.65, 0.03], facecolor=axcolor)\n', (10368, 10412), True, 'import matplotlib.pyplot as plt\n'), ((10948, 11013), 'matplotlib.pyplot.axes', 'plt.axes', (['[0.93, 0.05, 0.04, 0.11]'], {'axisbg': '"""lightgoldenrodyellow"""'}), "([0.93, 0.05, 0.04, 0.11], axisbg='lightgoldenrodyellow')\n", (10956, 11013), True, 'import matplotlib.pyplot as plt\n'), ((11256, 11269), 'numpy.min', 'numpy.min', (['xs'], {}), '(xs)\n', (11265, 11269), False, 'import numpy\n'), ((11271, 11284), 'numpy.max', 'numpy.max', (['xs'], {}), '(xs)\n', (11280, 11284), False, 'import numpy\n'), ((11486, 11506), 'numpy.abs', 'numpy.abs', (['(xs - vmin)'], {}), '(xs - vmin)\n', (11495, 11506), False, 'import numpy\n'), ((11534, 11554), 'numpy.abs', 'numpy.abs', (['(xs - vmax)'], {}), '(xs - vmax)\n', (11543, 11554), False, 'import numpy\n'), ((13780, 13829), 'matplotlib.pyplot.axes', 'plt.axes', (['[0.93, 0.2, 0.02, 0.65]'], {'axisbg': 'axcolor'}), '([0.93, 0.2, 0.02, 0.65], axisbg=axcolor)\n', (13788, 13829), True, 'import matplotlib.pyplot as plt\n'), ((13850, 13899), 'matplotlib.pyplot.axes', 'plt.axes', (['[0.95, 0.2, 0.02, 0.65]'], {'axisbg': 'axcolor'}), '([0.95, 0.2, 0.02, 0.65], axisbg=axcolor)\n', (13858, 13899), True, 'import matplotlib.pyplot as plt\n'), ((3555, 3575), 'numpy.array', 'np.array', (['[energies]'], {}), '([energies])\n', (3563, 3575), True, 'import numpy as np\n'), ((4666, 4687), 'numpy.array', 'np.array', (['kptlists[0]'], {}), '(kptlists[0])\n', (4674, 4687), True, 'import numpy as np\n'), ((5688, 5704), 'numpy.array', 'np.array', (['energs'], {}), '(energs)\n', (5696, 5704), True, 'import numpy as np\n'), ((7839, 7859), 'numpy.sqrt', 'np.sqrt', (['(2.0 * np.pi)'], {}), '(2.0 * np.pi)\n', (7846, 7859), True, 'import numpy as np\n'), ((10466, 10515), 'matplotlib.pyplot.axes', 'plt.axes', (['[0.2, 0.93, 0.65, 0.03]'], {'axisbg': 'axcolor'}), '([0.2, 0.93, 0.65, 0.03], axisbg=axcolor)\n', (10474, 10515), True, 'import matplotlib.pyplot as plt\n'), ((13943, 13994), 'futile.Figures.VertSlider', 'VertSlider', (['axmin', '"""SDos"""', 'cmin', 'cmax'], {'valinit': 'cmin'}), "(axmin, 'SDos', cmin, cmax, valinit=cmin)\n", (13953, 13994), False, 'from futile.Figures import VertSlider\n'), ((14012, 14059), 'futile.Figures.VertSlider', 'VertSlider', (['axmax', '""""""', 'cmin', 'cmax'], {'valinit': 'cmax'}), "(axmax, '', cmin, cmax, valinit=cmax)\n", (14022, 14059), False, 'from futile.Figures import VertSlider\n'), ((871, 883), 'numpy.min', 'np.min', (['ddos'], {}), '(ddos)\n', (877, 883), True, 'import numpy as np\n'), ((979, 991), 'numpy.max', 'np.max', (['ddos'], {}), '(ddos)\n', (985, 991), True, 'import numpy as np\n'), ((4747, 4768), 'numpy.array', 'np.array', (['kptlists[1]'], {}), '(kptlists[1])\n', (4755, 4768), True, 'import numpy as np\n'), ((3633, 3649), 'numpy.array', 'np.array', (['[norm]'], {}), '([norm])\n', (3641, 3649), True, 'import numpy as np\n'), ((8273, 8322), 'numpy.exp', 'np.exp', (['(-(e_i - dos[:]) ** 2 / (2.0 * sigma ** 2))'], {}), '(-(e_i - dos[:]) ** 2 / (2.0 * sigma ** 2))\n', (8279, 8322), True, 'import numpy as np\n'), ((918, 930), 'numpy.max', 'np.max', (['ddos'], {}), '(ddos)\n', (924, 930), True, 'import numpy as np\n'), ((933, 945), 'numpy.min', 'np.min', (['ddos'], {}), '(ddos)\n', (939, 945), True, 'import numpy as np\n'), ((1026, 1038), 'numpy.max', 'np.max', (['ddos'], {}), '(ddos)\n', (1032, 1038), True, 'import numpy as np\n'), ((1041, 1053), 'numpy.min', 'np.min', (['ddos'], {}), '(ddos)\n', (1047, 1053), True, 'import numpy as np\n'), ((8096, 8151), 'numpy.exp', 'np.exp', (['(-(e_i - dos[ikpt, :]) ** 2 / (2.0 * sigma ** 2))'], {}), '(-(e_i - dos[ikpt, :]) ** 2 / (2.0 * sigma ** 2))\n', (8102, 8151), True, 'import numpy as np\n')]
# -*- coding: utf-8 -*- from __future__ import print_function import os import vaex.dataset as dataset import numpy as np import unittest import vaex as vx import tempfile import vaex.server.tornado_server import astropy.io.fits import astropy.units import pandas as pd import vaex.execution import contextlib a = vaex.execution.buffer_size_default # will crash if we decide to rename it basedir = os.path.dirname(__file__) # this will make the test execute more code and may show up bugs #vaex.execution.buffer_size_default = 3 @contextlib.contextmanager def small_buffer(ds, size=3): if ds.is_local(): previous = ds.executor.buffer_size ds.executor.buffer_size = size ds._invalidate_caches() try: yield finally: ds.executor.buffer_size = previous else: yield # for remote dfs we don't support this ... or should we? # these need to be global for pickling def function_upper(x): return np.array(x.upper()) import vaex.serialize @vaex.serialize.register class Multiply: def __init__(self, scale=0): self.scale = scale @classmethod def state_from(cls, state, trusted=True): return cls(scale=state) def state_get(self): return self.scale def __call__(self, x): return x * self.scale vx.set_log_level_exception() #vx.set_log_level_off() #vx.set_log_level_debug() def from_scalars(**kwargs): return vx.from_arrays(**{k:np.array([v]) for k, v in kwargs.items()}) class CallbackCounter(object): def __init__(self, return_value=None): self.counter = 0 self.return_value = return_value self.last_args = None self.last_kwargs = None def __call__(self, *args, **kwargs): self.counter += 1 self.last_args = args self.last_kwargs = kwargs return self.return_value class TestDataset(unittest.TestCase): def setUp(self): self.dataset = dataset.DatasetArrays("dataset") # x is non-c # same as np.arange(10, dtype=">f8")., but with strides == 16, instead of 8 use_filtering = True if use_filtering: self.zero_index = 2 self.x = x = np.arange(-2, 40, dtype=">f8").reshape((-1,21)).T.copy()[:,0] self.y = y = x ** 2 self.ints = np.arange(-2,19, dtype="i8") self.ints[1] = 2**62+1 self.ints[2] = -2**62+1 self.ints[3] = -2**62-1 self.ints[1+10] = 2**62+1 self.ints[2+10] = -2**62+1 self.ints[3+10] = -2**62-1 self.dataset.add_column("x", x) self.dataset.add_column("y", y) mo = m = np.arange(-2, 40, dtype=">f8").reshape((-1,21)).T.copy()[:,0] ma_value = 77777 m[-1+10+2] = ma_value m[-1+20] = ma_value else: self.x = x = np.arange(20, dtype=">f8").reshape((-1,10)).T.copy()[:,0] self.y = y = x ** 2 self.ints = np.arange(10, dtype="i8") self.ints[0] = 2**62+1 self.ints[1] = -2**62+1 self.ints[2] = -2**62-1 self.dataset.add_column("x", x) self.dataset.add_column("y", y) m = x.copy() ma_value = 77777 m[-1] = ma_value self.m = m = np.ma.array(m, mask=m==ma_value) self.mi = mi = np.ma.array(m.data.astype(np.int64), mask=m.data==ma_value, fill_value=88888) self.dataset.add_column("m", m) self.dataset.add_column("mi", mi) self.dataset.add_column("ints", self.ints) self.dataset.set_variable("t", 1.) self.dataset.add_virtual_column("z", "x+t*y") self.dataset.units["x"] = astropy.units.Unit("km") self.dataset.units["y"] = astropy.units.Unit("km/s") self.dataset.units["t"] = astropy.units.Unit("s") self.dataset.add_column("f", np.arange(len(self.dataset), dtype=np.float64)) self.dataset.ucds["x"] = "some;ucd" name = np.array(list(map(lambda x: str(x) + "bla" + ('_' * int(x)), self.x)), dtype='U') #, dtype=np.string_) self.names = self.dataset.get_column_names() self.dataset.add_column("name", np.array(name)) self.dataset.add_column("name_arrow", vaex.string_column(name)) if use_filtering: self.dataset.select('(x >= 0) & (x < 10)', name=vaex.dataset.FILTER_SELECTION_NAME) self.x = x = self.x[2:12] self.y = y = self.y[2:12] self.m = m = self.m[2:12] self.ints = ints = self.ints[2:12] # TODO; better virtual and variables support # TODO: this is a copy since concatenated dfs do not yet support # global selections # a 'deep' copy self.dataset_no_filter = vaex.from_items(*self.dataset.to_items(virtual=False, strings=True)) self.dataset_no_filter.add_virtual_column("z", "x+t*y") self.dataset_no_filter.set_variable("t", 1.) #self.jobsManager = dataset.JobsManager() x = np.array([0., 1]) y = np.array([-1., 1]) self.datasetxy = vx.dataset.DatasetArrays("datasetxy") self.datasetxy.add_column("x", x) self.datasetxy.add_column("y", y) x1 = np.array([1., 3]) x2 = np.array([2., 3, 4,]) x3 = np.array([5.]) self.x_concat = np.concatenate((x1, x2, x3)) dataset1 = vx.dataset.DatasetArrays("dataset1") dataset2 = vx.dataset.DatasetArrays("dataset2") dataset3 = vx.dataset.DatasetArrays("dataset3") dataset1.add_column("x", x1) dataset2.add_column("x", x2) dataset3.add_column("x", x3) dataset3.add_column("y", x3**2) self.dataset_concat = vx.dataset.DatasetConcatenated([dataset1, dataset2, dataset3], name="dataset_concat") self.dataset_concat_dup = vx.dataset.DatasetConcatenated([self.dataset_no_filter, self.dataset_no_filter, self.dataset_no_filter], name="dataset_concat_dup") self.dataset_local = self.dataset self.datasetxy_local = self.datasetxy self.dataset_concat_local = self.dataset_concat self.dataset_concat_dup_local = self.dataset_concat_dup np.random.seed(0) # fix seed so that test never fails randomly self.df = self.dataset.to_pandas_df() def test_function(self): def multiply(factor=2): def f(x): return x*factor return f ds = self.dataset f = ds.add_function('mul2', multiply(2)) ds['x2'] = f(ds.x) self.assertEqual((self.x * 2).tolist(), ds.evaluate('x2').tolist()) ds.state_get() def test_apply(self): ds_copy = self.dataset.copy() ds = self.dataset with small_buffer(ds, 2): upper = ds.apply(function_upper, arguments=[ds['name']]) ds['NAME'] = upper name = ds.evaluate('NAME') self.assertEqual(name[0], u'0.0BLA') ds_copy.state_set(ds.state_get()) name = ds_copy.evaluate('NAME') self.assertEqual(name[0], u'0.0BLA') ds['a1'] = ds.apply(lambda x: x+1, arguments=['x']) ds['a2'] = ds.apply(lambda x: x+2, arguments=['x']) assert (ds['a1']+1).evaluate().tolist() == ds['a2'].evaluate().tolist() def test_filter(self): ds = self.dataset if ds.is_local(): # remote doesn't have a cache ds._invalidate_selection_cache() with small_buffer(ds): ds1 = ds.copy() ds1.select(ds1.x > 4, name=vaex.dataset.FILTER_SELECTION_NAME, mode='and') ds1._invalidate_caches() ds2 = ds[ds.x > 4] ds1.x.evaluate() # self.assertEqual(ds1.x.evaluate().tolist(), ds2.x.evaluate().tolist()) ds2.select(ds.x < 6) x = ds2.x.evaluate(selection=True) self.assertEqual(x.tolist(), [5]) # print("=" * 70) def test_default_selection(self): ds = self.dataset ds._invalidate_selection_cache() with small_buffer(ds): indices = ds._filtered_range_to_unfiltered_indices(0, 2) self.assertEqual(indices.tolist(), [self.zero_index+0, self.zero_index+1]) ds = ds[ds.x > 2] indices = ds._filtered_range_to_unfiltered_indices(0, 2) assert indices.tolist() == [self.zero_index+3, self.zero_index+4] x = ds.x.evaluate(0, 2) indices = ds._filtered_range_to_unfiltered_indices(0, 2) assert len(x) == 2 assert x[0] == 3 x = ds.x.evaluate(4, 7) indices = ds._filtered_range_to_unfiltered_indices(4, 7) assert len(x) == 3 assert x[0] == 3+4 def test_unique(self): ds = vaex.from_arrays(x=np.array([2,2,1,0,1,1,2])) with small_buffer(ds): classes = ds.unique('x') assert np.sort(classes).tolist() == [0, 1, 2] def test_amuse(self): ds = vx.open(os.path.join(basedir, "files", "default_amuse_plummer.hdf5")) self.assertGreater(len(ds), 0) self.assertGreater(len(ds.get_column_names()), 0) self.assertIsNotNone(ds.unit("x")) self.assertIsNotNone(ds.unit("vx")) self.assertIsNotNone(ds.unit("mass")) ds.close_files() def test_masked_array_output(self): fn = tempfile.mktemp(".hdf5") print(fn) self.dataset.export_hdf5(fn, sort="x") output = vaex.open(fn) self.assertEqual(self.dataset.sum("m"), output.sum("m")) table = self.dataset.to_astropy_table() fn = tempfile.mktemp(".vot") print(fn) from astropy.io.votable import from_table, writeto votable = from_table(table) writeto(votable, fn) output = vaex.open(fn) self.assertEqual(self.dataset.sum("m"), output.sum("m")) def test_formats(self): return # TODO: not workign ATM because of fits + strings ds_fits = vx.open(os.path.join(basedir, "files", "gaia-small-fits-basic.fits")) ds_fits_plus = vx.open(os.path.join(basedir, "files", "gaia-small-fits-plus.fits")) ds_colfits = vx.open(os.path.join(basedir, "files", "gaia-small-colfits-basic.fits")) ds_colfits_plus = vx.open(os.path.join(basedir, "files", "gaia-small-colfits-plus.fits")) ds_vot = vx.open(os.path.join(basedir, "files", "gaia-small-votable.vot")) # FIXME: the votable gives issues dslist = [ds_fits, ds_fits_plus, ds_colfits, ds_colfits_plus]#, ds_vot] for ds1 in dslist: path_hdf5 = tempfile.mktemp(".hdf5") ds1.export_hdf5(path_hdf5) ds2 = vx.open(path_hdf5) diff, missing, types, meta = ds1.compare(ds2) self.assertEqual(diff, [], "difference between %s and %s" % (ds1.path, ds2.path)) self.assertEqual(missing, [], "missing columns %s and %s" % (ds1.path, ds2.path)) self.assertEqual(meta, [], "meta mismatch between columns %s and %s" % (ds1.path, ds2.path)) path_fits = tempfile.mktemp(".fits") ds1.export_fits(path_fits) ds2 = vx.open(path_fits) diff, missing, types, meta = ds1.compare(ds2) self.assertEqual(diff, [], "difference between %s and %s" % (ds1.path, ds2.path)) self.assertEqual(missing, [], "missing columns %s and %s" % (ds1.path, ds2.path)) self.assertEqual(meta, [], "meta mismatch between columns %s and %s" % (ds1.path, ds2.path)) if 0: N = len(dslist) for i in range(N): for j in range(i+1, N): ds1 = dslist[i] ds2 = dslist[j] diff, missing, types, meta = ds1.compare(ds2) self.assertEqual(diff, [], "difference between %s and %s" % (ds1.path, ds2.path)) self.assertEqual(missing, [], "missing columns %s and %s" % (ds1.path, ds2.path)) self.assertEqual(meta, [], "meta mismatch between columns %s and %s" % (ds1.path, ds2.path)) def test_to(self): def test_equal(ds1, ds2, units=True, ucds=True, description=True, descriptions=True, skip=[]): if description: self.assertEqual(ds1.description, ds2.description) for name in ds1.get_column_names(virtual=False): if name in skip: continue self.assertIn(name, ds2.get_column_names(virtual=False)) np.testing.assert_array_equal(ds1.evaluate(name), ds2.evaluate(name), err_msg='mismatch in ' +name) if units: self.assertEqual(ds1.units.get(name), ds2.units.get(name)) if ucds: self.assertEqual(ds1.ucds.get(name), ds2.ucds.get(name)) if descriptions: self.assertEqual(ds1.descriptions.get(name), ds2.descriptions.get(name)) # as numpy dict ds2 = vx.from_arrays(**self.dataset.to_dict()) test_equal(self.dataset, ds2, ucds=False, units=False, description=False, descriptions=False) # as pandas ds2 = vx.from_pandas(self.dataset.to_pandas_df()) # skip masked arrays, pandas doesn't understand that, converts it to nan, so we can't compare test_equal(self.dataset, ds2, ucds=False, units=False, description=False, descriptions=False, skip=['m', 'mi']) df = self.dataset.to_pandas_df(index_name="name") ds2 = vx.from_pandas(df, index_name="name", copy_index=True) test_equal(self.dataset, ds2, ucds=False, units=False, description=False, descriptions=False, skip=['m', 'mi']) ds2 = vx.from_pandas(self.dataset.to_pandas_df(index_name="name")) assert "name" not in ds2.get_column_names() # as astropy table ds2 = vx.from_astropy_table(self.dataset.to_astropy_table()) test_equal(self.dataset, ds2) # as arrow table ds2 = vx.from_arrow_table(self.dataset.to_arrow_table(), as_numpy=False) test_equal(self.dataset, ds2, ucds=False, units=False, description=False, descriptions=False, ) # return a copy ds2 = self.dataset.to_copy(virtual=True) assert "z" not in ds2.columns assert "z" in ds2.virtual_columns test_equal(self.dataset, ds2) def test_add_column(self): columns = self.dataset.get_column_names() self.dataset.add_column("x", self.dataset.data.x) self.assertSequenceEqual(columns, self.dataset.get_column_names()) self.dataset.add_column("extra", self.dataset.data.x) extra = self.dataset.evaluate("extra") np.testing.assert_array_almost_equal(extra, self.dataset.data.x[self.zero_index:self.zero_index+10]) with self.assertRaises(ValueError): self.dataset.add_column("unequal", self.dataset.data.x[:10]) with self.assertRaises(ValueError): self.dataset.add_column("unequal", self.dataset.data.x[:11]) def test_rename_column(self): self.dataset.rename("x", "xx") self.assertNotIn("x", self.dataset.columns) self.assertNotIn("x", self.dataset.column_names) self.assertNotIn("x", self.dataset.units) self.assertNotIn("x", self.dataset.ucds) self.assertIn("xx", self.dataset.columns) self.assertIn("xx", self.dataset.column_names) self.assertIn("xx", self.dataset.units) self.assertIn("xx", self.dataset.ucds) def test_csv(self): separator = "," fn = tempfile.mktemp(".csv") #print(fn) with open(fn, "w") as f: print(separator.join(["x", "y", "m", "mi", "name", "name_arrow", "ints", "f"]), file=f) values = [self.dataset.evaluate(k) for k in 'x y m mi name name_arrow ints f'.split()] for x, y, m, mi, name, name_arrow, i, f_ in zip(*values):#zip(self.x, self.y, self.dataset.data.m, self.dataset.data.mi, self.dataset.data.name, self.dataset.data.ints, self.dataset.data.f): print(separator.join(map(str, [x, y, m, mi, name, name_arrow, i, f_])), file=f) ds = vx.from_csv(fn, index_col=False, copy_index=True) changes = self.dataset.compare(ds, report_difference=True) diff = changes[0] #print(diff) self.assertEqual(changes[0], [], "changes in dataset") self.assertEqual(changes[1], ['index'], "mssing columns") def test_ascii(self): for seperator in " \t,": for use_header in [True, False]: #print(">>>", repr(seperator), use_header) fn = tempfile.mktemp("asc") with open(fn, "w") as f: if use_header: print(seperator.join(["x", "y"]), file=f) for x, y, name in zip(self.x, self.y, self.dataset.data.name): print(seperator.join(map(repr, [x, y])), file=f) #with open(fn) as f: # print(f.read()) sep = seperator if seperator == " ": sep = None if use_header: ds = vx.from_ascii(fn, seperator=sep) else: ds = vx.from_ascii(fn, seperator=seperator, names="x y".split()) np.testing.assert_array_almost_equal(ds.data.x, self.x) np.testing.assert_array_almost_equal(ds.data.y, self.y) #np.testing.assert_array_equal(ds.data.names, self.dataset.data.name) #if seperator == ",": # df = pd.read_csv(fn) # ds = vx.from_pandas(df) # np.testing.assert_array_almost_equal(ds.data.x, self.x) # np.testing.assert_array_almost_equal(ds.data.y, self.y) #np.testing.assert_array_equal(ds.data.names, self.dataset.data.name) def tearDown(self): self.dataset.remove_virtual_meta() self.dataset_concat.remove_virtual_meta() self.dataset_concat_dup.remove_virtual_meta() def test_mixed_endian(self): x = np.arange(10., dtype=">f8") y = np.arange(10, dtype="<f8") ds = vx.from_arrays(x=x, y=y) ds.count() ds.count(binby=["x", "y"]) def test_join(self): np.random.seed(42) x = np.arange(10, dtype=np.float64) indices = np.arange(10) i = x.astype(np.int64) np.random.shuffle(indices) xs = x[indices] y = x**2 z = ys = y[indices] names = np.array(list(map(lambda x: str(x) + "bla", self.x)), dtype='S')[indices] ds = vaex.from_arrays(x=x, y=y) ds2 = vaex.from_arrays(x=xs, z=ys, i=i, names=names) ds.join(ds2[['x', 'z', 'i', 'names']], 'x', rsuffix='r', inplace=True) self.assertEqual(ds.sum('x*y'), np.sum(x*y)) self.assertEqual(ds.sum('x*z'), np.sum(x*y)) self.assertEqual(ds.sum('x*y'), np.sum(x[indices]*z)) self.assertEqual(ds.sum('x*y'), np.sum(x[indices]*z)) # test with incomplete data ds = vaex.from_arrays(x=x, y=y) ds2 = vaex.from_arrays(x=xs[:4], z=ys[:4], i=i[:4], names=names[:4]) ds.join(ds2, 'x', rsuffix='r', inplace=True) self.assertEqual(ds.sum('x*y'), np.sum(x*y)) self.assertEqual(ds.sum('x*z'), np.sum(x[indices][:4]*y[indices][:4])) # test with incomplete data, but other way around ds = vaex.from_arrays(x=x[:4], y=y[:4]) ds2 = vaex.from_arrays(x=xs, z=ys, i=i, names=names) ds.join(ds2, 'x', inplace=True, rsuffix='r') self.assertEqual(ds.sum('x*y'), np.sum(x[:4]*y[:4])) self.assertEqual(ds.sum('x*z'), np.sum(x[:4]*y[:4])) def test_healpix_count(self): # only test when healpy is present try: import healpy as hp except ImportError: return max_order = 6 nside = hp.order2nside(max_order) npix = hp.nside2npix(nside) healpix = np.arange(npix) ds = vx.from_arrays(healpix=healpix) for order in range(max_order): counts = ds.healpix_count(healpix_expression="healpix", healpix_max_level=max_order, healpix_level=order) scaling = 4**(max_order-order) ones = np.ones(npix//scaling) * scaling np.testing.assert_array_almost_equal(counts, ones) self.assertEqual(counts.sum(), npix) def test_uncertainty_propagation(self): N = 100000 # distance parallaxes = np.random.normal(1, 0.1, N) ds_many = vx.from_arrays(parallax=parallaxes) ds_many.add_virtual_columns_distance_from_parallax("parallax", "distance") distance_std_est = ds_many.std("distance").item() ds_1 = vx.from_arrays(parallax=np.array([1.]), parallax_uncertainty=np.array([0.1])) ds_1.add_virtual_columns_distance_from_parallax("parallax", "distance", "parallax_uncertainty") distance_std = ds_1.evaluate("distance_uncertainty")[0] self.assertAlmostEqual(distance_std, distance_std_est,2) def test_virtual_column_storage(self): self.dataset.write_meta() ds = vaex.zeldovich() ds.write_meta() def test_add_virtual_columns_cartesian_velocities_to_polar(self): if 1: def dfs(x, y, velx, vely): ds_1 = from_scalars(x=x, y=y, vx=velx, vy=vely, x_e=0.01, y_e=0.02, vx_e=0.03, vy_e=0.04) ds_1.add_virtual_columns_cartesian_velocities_to_polar(propagate_uncertainties=True) N = 100000 # distance x = np.random.normal(x, 0.01, N) y = np.random.normal(y, 0.02, N) velx = np.random.normal(velx, 0.03, N) vely = np.random.normal(vely, 0.04, N) ds_many = vx.from_arrays(x=x, y=y, vx=vely, vy=vely) ds_many.add_virtual_columns_cartesian_velocities_to_polar() return ds_1, ds_many ds_1, ds_many = dfs(0, 2, 3, 4) vr_polar_e = ds_1.evaluate("vr_polar_uncertainty")[0] vphi_polar_e = ds_1.evaluate("vphi_polar_uncertainty")[0] self.assertAlmostEqual(vr_polar_e, ds_many.std("vr_polar").item(), delta=0.02) self.assertAlmostEqual(vphi_polar_e, ds_many.std("vphi_polar").item(), delta=0.02) # rotation is anti clockwise ds_1 = from_scalars(x=0, y=2, vx=0, vy=2) ds_1.add_virtual_columns_cartesian_velocities_to_polar() vr_polar = ds_1.evaluate("vr_polar")[0] vphi_polar = ds_1.evaluate("vphi_polar")[0] self.assertAlmostEqual(vr_polar, 2) self.assertAlmostEqual(vphi_polar, 0) ds_1 = from_scalars(x=0, y=2, vx=-2, vy=0) ds_1.add_virtual_columns_cartesian_velocities_to_polar() vr_polar = ds_1.evaluate("vr_polar")[0] vphi_polar = ds_1.evaluate("vphi_polar")[0] self.assertAlmostEqual(vr_polar, 0) self.assertAlmostEqual(vphi_polar, 2) def test_add_virtual_columns_cartesian_velocities_to_spherical(self): if 0: # TODO: errors in spherical velocities pass def test(vr_expect, vlong_expect, vlat_expect, **kwargs): ds_1 = from_scalars(**kwargs) ds_1.add_virtual_columns_cartesian_velocities_to_spherical() vr, vlong, vlat = ds_1.evaluate("vr")[0], ds_1.evaluate("vlong")[0], ds_1.evaluate("vlat")[0] self.assertAlmostEqual(vr, vr_expect) self.assertAlmostEqual(vlong, vlong_expect) self.assertAlmostEqual(vlat, vlat_expect) test(0, -1, 0, x=1, y=0, z=0, vx=0, vy=-1, vz=0) test(0, -1, 0, x=10, y=0, z=0, vx=0, vy=-1, vz=0) test(0, 0, 1, x=1, y=0, z=0, vx=0, vy= 0, vz=1) test(1, 0, 0, x=1, y=0, z=0, vx=1, vy= 0, vz=0) a = 1./np.sqrt(2.) test(0, 0, 1, x=a, y=0, z=a, vx=-a, vy= 0, vz=a) def test_add_virtual_columns_cartesian_velocities_to_pmvr(self): if 0: # TODO: errors in spherical velocities pass def test(vr_expect, pm_long_expect, pm_lat_expect, **kwargs): ds_1 = from_scalars(**kwargs) ds_1.add_variable("k", 1) # easier for comparison ds_1.add_virtual_columns_cartesian_velocities_to_pmvr() vr, pm_long, pm_lat = ds_1.evaluate("vr")[0], ds_1.evaluate("pm_long")[0], ds_1.evaluate("pm_lat")[0] self.assertAlmostEqual(vr, vr_expect) self.assertAlmostEqual(pm_long, pm_long_expect) self.assertAlmostEqual(pm_lat, pm_lat_expect) test(0, -1, 0, x=1, y=0, z=0, vx=0, vy=-1, vz=0) test(0, -0.1, 0, x=10, y=0, z=0, vx=0, vy=-1, vz=0) test(0, 0, 1, x=1, y=0, z=0, vx=0, vy= 0, vz=1) test(1, 0, 0, x=1, y=0, z=0, vx=1, vy= 0, vz=0) a = 1./np.sqrt(2.) test(0, 0, 1, x=a, y=0, z=a, vx=-a, vy= 0, vz=a) test(0, 0, 1*10, x=a/10, y=0, z=a/10, vx=-a, vy= 0, vz=a) def test_add_virtual_columns_cartesian_to_polar(self): for radians in [True, False]: def dfs(x, y, radians=radians): ds_1 = from_scalars(x=x, y=y, x_e=0.01, y_e=0.02) ds_1.add_virtual_columns_cartesian_to_polar(propagate_uncertainties=True, radians=radians) N = 100000 # distance x = np.random.normal(x, 0.01, N) y = np.random.normal(y, 0.02, N) ds_many = vx.from_arrays(x=x, y=y) ds_many.add_virtual_columns_cartesian_to_polar(radians=radians) return ds_1, ds_many ds_1, ds_many = dfs(0, 2) r_polar_e = ds_1.evaluate("r_polar_uncertainty")[0] phi_polar_e = ds_1.evaluate("phi_polar_uncertainty")[0] self.assertAlmostEqual(r_polar_e, ds_many.std("r_polar").item(), delta=0.02) self.assertAlmostEqual(phi_polar_e, ds_many.std("phi_polar").item(), delta=0.02) # rotation is anti clockwise r_polar = ds_1.evaluate("r_polar")[0] phi_polar = ds_1.evaluate("phi_polar")[0] self.assertAlmostEqual(r_polar, 2) self.assertAlmostEqual(phi_polar, np.pi/2 if radians else 90) def test_add_virtual_columns_proper_motion2vperpendicular(self): def dfs(distance, pm_l, pm_b): ds_1 = from_scalars(pm_l=pm_l, pm_b=pm_b, distance=distance, distance_e=0.1, pm_l_e=0.3, pm_b_e=0.4) ds_1.add_virtual_columns_proper_motion2vperpendicular(propagate_uncertainties=True) N = 100000 # distance distance = np.random.normal(0, 0.1, N) + distance pm_l = np.random.normal(0, 0.3, N) + pm_l pm_b = np.random.normal(0, 0.4, N) + pm_b ds_many = vx.from_arrays(pm_l=pm_l, pm_b=pm_b, distance=distance) ds_many.add_virtual_columns_proper_motion2vperpendicular() return ds_1, ds_many ds_1, ds_many = dfs(2, 3, 4) vl_e = ds_1.evaluate("vl_uncertainty")[0] vb_e = ds_1.evaluate("vb_uncertainty")[0] self.assertAlmostEqual(vl_e, ds_many.std("vl").item(), delta=0.02) self.assertAlmostEqual(vb_e, ds_many.std("vb").item(), delta=0.02) k = 4.74057 self.assertAlmostEqual(ds_1.evaluate("vl")[0], 2*k*3) self.assertAlmostEqual(ds_1.evaluate("vb")[0], 2*k*4) def test_virtual_columns_lbrvr_proper_motion2vcartesian(self): for radians in [True, False]: def dfs(l, b, distance, vr, pm_l, pm_b, radians=radians): ds_1 = from_scalars(l=l, b=b, pm_l=pm_l, pm_b=pm_b, vr=vr, distance=distance, distance_e=0.1, vr_e=0.2, pm_l_e=0.3, pm_b_e=0.4) ds_1.add_virtual_columns_lbrvr_proper_motion2vcartesian(propagate_uncertainties=True, radians=radians) N = 100000 # distance l = np.random.normal(0, 0.1, N) * 0 + l b = np.random.normal(0, 0.1, N) * 0 + b distance = np.random.normal(0, 0.1, N) + distance vr = np.random.normal(0, 0.2, N) + vr pm_l = np.random.normal(0, 0.3, N) + pm_l pm_b = np.random.normal(0, 0.4, N) + pm_b ds_many = vx.from_arrays(l=l, b=b, pm_l=pm_l, pm_b=pm_b, vr=vr, distance=distance) ds_many.add_virtual_columns_lbrvr_proper_motion2vcartesian(radians=radians) return ds_1, ds_many ds_1, ds_many = dfs(0, 0, 1, 1, 2, 3) vx_e = ds_1.evaluate("vx_uncertainty")[0] vy_e = ds_1.evaluate("vy_uncertainty")[0] vz_e = ds_1.evaluate("vz_uncertainty")[0] self.assertAlmostEqual(vx_e, ds_many.std("vx").item(), delta=0.02) self.assertAlmostEqual(vy_e, ds_many.std("vy").item(), delta=0.02) self.assertAlmostEqual(vz_e, ds_many.std("vz").item(), delta=0.02) self.assertAlmostEqual(vx_e, 0.2,2) self.assertAlmostEqual(ds_1.evaluate("vx")[0], 1) k = 4.74057 self.assertAlmostEqual(ds_1.evaluate("vy")[0], k*2) self.assertAlmostEqual(ds_1.evaluate("vz")[0], k*3) ds = vx.from_scalars(l=90, b=0, pm_l=-1, pm_b=0, distance=1, vr=0) ds.add_virtual_columns_lbrvr_proper_motion2vcartesian() self.assertAlmostEqual(ds.evaluate("vx")[0], k*1) self.assertAlmostEqual(ds.evaluate("vy")[0], 0) ds = vx.from_scalars(l=90, b=0, pm_l=-1, pm_b=0, distance=2, vr=0) ds.add_virtual_columns_lbrvr_proper_motion2vcartesian() self.assertAlmostEqual(ds.evaluate("vx")[0], k*2) self.assertAlmostEqual(ds.evaluate("vy")[0], 0) ds = vx.from_scalars(l=0, b=90, pm_l=0, pm_b=-1, distance=1, vr=0) ds.add_virtual_columns_lbrvr_proper_motion2vcartesian() self.assertAlmostEqual(ds.evaluate("vx")[0], k*1) self.assertAlmostEqual(ds.evaluate("vy")[0], 0) ds = vx.from_scalars(l=0, b=90, pm_l=0, pm_b=-1, distance=2, vr=0) ds.add_virtual_columns_lbrvr_proper_motion2vcartesian() self.assertAlmostEqual(ds.evaluate("vx")[0], k*2) self.assertAlmostEqual(ds.evaluate("vy")[0], 0) ds = vx.from_scalars(l=90, b=0, pm_l=0, pm_b=0, distance=1, vr=1) ds.add_virtual_columns_lbrvr_proper_motion2vcartesian() self.assertAlmostEqual(ds.evaluate("vx")[0], 0) self.assertAlmostEqual(ds.evaluate("vy")[0], 1) ds = vx.from_scalars(l=90, b=0, pm_l=0, pm_b=0, distance=2, vr=1) ds.add_virtual_columns_lbrvr_proper_motion2vcartesian() self.assertAlmostEqual(ds.evaluate("vx")[0], 0) self.assertAlmostEqual(ds.evaluate("vy")[0], 1) def test_state(self): mul = Multiply(3) ds = self.dataset copy = ds.copy(virtual=False) statefile = tempfile.mktemp('.json') ds.select('x > 5', name='test') ds.add_virtual_column('xx', 'x**2') fmul = ds.add_function('fmul', mul) ds['mul'] = fmul(ds.x) count = ds.count('x', selection='test') sum = ds.sum('xx', selection='test') summul = ds.sum('mul', selection='test') ds.state_write(statefile) copy.state_load(statefile) self.assertEqual(count, copy.count('x', selection='test')) self.assertEqual(sum, copy.sum('xx', selection='test')) self.assertEqual(summul, copy.sum('3*x', selection='test')) self.assertEqual(summul, copy.sum('mul', selection='test')) def test_strings(self): # TODO: concatenated dfs with strings of different length self.assertEqual(["x", "y", "m", "mi", "ints", "f"], self.dataset.get_column_names(virtual=False, strings=False)) names = ["x", "y", "m", "mi", "ints", "f", "name", "name_arrow"] self.assertEqual(names, self.dataset.get_column_names(strings=True, virtual=False)) if self.dataset.is_local(): # check if strings are exported path_hdf5 = tempfile.mktemp(".hdf5") self.dataset.export_hdf5(path_hdf5, virtual=False) exported_dataset = vx.open(path_hdf5) self.assertEqual(names, exported_dataset.get_column_names(strings=True)) path_arrow = tempfile.mktemp(".arrow") self.dataset.export_arrow(path_arrow, virtual=False) exported_dataset = vx.open(path_arrow) self.assertEqual(names, exported_dataset.get_column_names(strings=True)) # for fits we do not support arrow like strings # TODO: get back support for SXX (string/binary) format self.dataset.drop("name_arrow", inplace=True) self.dataset.drop("name", inplace=True) names.remove("name_arrow") names.remove("name") path_fits = tempfile.mktemp(".fits") self.dataset.export_fits(path_fits, virtual=False) exported_dataset = vx.open(path_fits) self.assertEqual(names, exported_dataset.get_column_names(strings=True)) path_fits_astropy = tempfile.mktemp(".fits") with astropy.io.fits.open(path_fits) as fitsfile: # make sure astropy can read the data bla = fitsfile[1].data try: fitsfile.writeto(path_fits_astropy) finally: os.remove(path_fits_astropy) def histogram_cumulative(self): self.dataset("x").histogram() def test_units(self): assert self.dataset.unit("x") == astropy.units.km assert self.dataset.unit("y") == astropy.units.km/astropy.units.second assert self.dataset.unit("t") == astropy.units.second assert self.dataset.unit("z") == astropy.units.km assert self.dataset.unit("x+y") == None def test_dtype(self): self.assertEqual(self.dataset.data_type("x"), np.dtype(">f8")) self.assertEqual(self.dataset.data_type("f"), np.float64) self.assertEqual(self.dataset.data_type("x*f"), np.float64) def test_byte_size(self): arrow_size = self.dataset.columns['name_arrow'].nbytes +\ self.dataset.columns['name'].nbytes self.assertEqual(self.dataset.byte_size(), (8*6 + 2)*len(self.dataset) + arrow_size) self.dataset.select("x < 1") self.assertEqual(self.dataset.byte_size(selection=True), 8*6 + 2 + arrow_size) def test_ucd_find(self): self.dataset.ucds["x"] = "a;b;c" self.dataset.ucds["y"] = "b;c;d" self.dataset.ucds["z"] = "b;c;d" self.assertEqual(self.dataset.ucd_find("a"), "x") self.assertEqual(self.dataset.ucd_find("b"), "x") self.assertEqual(self.dataset.ucd_find("^b"), "y") self.assertEqual(self.dataset.ucd_find("c"), "x") self.assertEqual(self.dataset.ucd_find("d"), "y") self.assertEqual(self.dataset.ucd_find("b;c"), "x") self.assertEqual(self.dataset.ucd_find("^b;c"), "y") def test_data_access(self): assert (all(self.dataset.data.x == self.dataset.columns["x"])) def test_not_implemented(self): subspace = vaex.legacy.Subspace(self.dataset, ["x", "y"], self.dataset.executor, False) with self.assertRaises(NotImplementedError): subspace.minmax() with self.assertRaises(NotImplementedError): subspace.mean() with self.assertRaises(NotImplementedError): subspace.var() with self.assertRaises(NotImplementedError): subspace.sum() with self.assertRaises(NotImplementedError): subspace.histogram([]) with self.assertRaises(NotImplementedError): subspace.limits_sigma() def test_length(self): self.assertEqual(len(self.dataset), 10) def t_est_length_mask(self): self.dataset._set_mask(self.dataset.columns['x'] < 5) self.assertEqual(self.dataset.length(selection=True), 5) def test_evaluate(self): for t in [2, 3]: self.dataset.set_variable("t", t) x = self.dataset.evaluate("x") y = self.dataset.evaluate("y") z = self.dataset.evaluate("z") z_test = x + t * y np.testing.assert_array_almost_equal(z, z_test) x = self.dataset.evaluate("x", selection="x < 4") self.assertEqual(x.tolist(), x[:4].tolist()) def test_invalid_expression(self): with self.assertRaises(SyntaxError): self.dataset.validate_expression("x/") with self.assertRaises(NameError): self.dataset.validate_expression("hoeba(x)") with self.assertRaises(NameError): self.dataset.validate_expression("x()") self.dataset.validate_expression("sin(x)+tan(y)") with self.assertRaises((KeyError, NameError)): # TODO: should we have just one error type? self.dataset.validate_expression("doesnotexist") self.dataset.validate_expression("x / y * z + x - x - -x") self.dataset.validate_expression("x < 0") self.dataset.validate_expression("x <= 0") self.dataset.validate_expression("x > 0") self.dataset.validate_expression("x >= 0") self.dataset.validate_expression("x == 0") self.dataset.validate_expression("x != 0") def test_evaluate_nested(self): self.dataset.add_virtual_column("z2", "-z") self.dataset.add_virtual_column("z3", "z+z2") zeros = self.dataset.evaluate("z3") np.testing.assert_array_almost_equal(zeros, np.zeros(len(self.dataset))) def test_count(self): self.dataset.select("x < 5") ds = self.dataset[self.dataset.x < 5] df = self.df[self.df.x < 5] np.testing.assert_array_almost_equal(self.dataset.count("x", selection=None), 10) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=True), 5) np.testing.assert_array_almost_equal(self.dataset.x.count(selection=True), 5) np.testing.assert_array_almost_equal(self.dataset['x'].count(), 10) np.testing.assert_array_almost_equal(self.df['x'].count(), 10) np.testing.assert_array_almost_equal(ds['x'].count(), 5) np.testing.assert_array_almost_equal(df['x'].count(), 5) self.dataset.select("x >= 5") ds = self.dataset[self.dataset.x >= 5] df = self.df[self.df.x >= 5] np.testing.assert_array_almost_equal(self.dataset.count("m", selection=None), 9) np.testing.assert_array_almost_equal(self.dataset.count("m", selection=True), 4) np.testing.assert_array_almost_equal(self.dataset['m'].count(), 9) np.testing.assert_array_almost_equal(self.df['m'].count(), 9) np.testing.assert_array_almost_equal(ds['m'].count(), 4) np.testing.assert_array_almost_equal(df['m'].count(), 4) # convert to float self.dataset_local.columns["x"] = self.dataset_local.columns["x"] * 1. self.dataset_local.columns["x"][self.zero_index] = np.nan if self.dataset.is_local(): self.dataset._invalidate_caches() else: if hasattr(self, 'webserver1'): self.webserver1.dfs[0]._invalidate_caches() self.webserver2.dfs[0]._invalidate_caches() self.dataset_local._invalidate_caches() self.df = self.dataset_local.to_pandas_df() self.dataset.select("x < 5") ds = self.dataset[self.dataset.x < 5] df = self.df[self.df.x < 5] # import pdb # pdb.set_trace() np.testing.assert_array_almost_equal(self.dataset.count("x", selection=None), 9) np.testing.assert_array_almost_equal(self.dataset['x'].count(), 9) np.testing.assert_array_almost_equal(self.df['x'].count(), 9) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=True), 4) np.testing.assert_array_almost_equal(ds['x'].count(), 4) np.testing.assert_array_almost_equal(df['x'].count(), 4) np.testing.assert_array_almost_equal(self.dataset.count("y", selection=None), 9) # this is because of the filter x<10 np.testing.assert_array_almost_equal(self.dataset_no_filter.count("y", selection=None), 10) np.testing.assert_array_almost_equal(self.dataset['y'].count(), 9) np.testing.assert_array_almost_equal(self.dataset_no_filter['y'].count(), 10) np.testing.assert_array_almost_equal(self.df['y'].count(), 9) np.testing.assert_array_almost_equal(self.dataset.count("y", selection=True), 4) np.testing.assert_array_almost_equal(ds['y'].count(), 4) np.testing.assert_array_almost_equal(df['y'].count(), 4) np.testing.assert_array_almost_equal(self.dataset.count(selection=None), 9) np.testing.assert_array_almost_equal(self.dataset_no_filter.count(selection=None), 10) np.testing.assert_array_almost_equal(self.dataset.count(), 9) np.testing.assert_array_almost_equal(self.dataset_no_filter.count(), 10) #np.testing.assert_array_almost_equal(self.df.count(), 9) # TODO: this is different in pandas # we modified the data.. so actually this should be 4.. np.testing.assert_array_almost_equal(self.dataset.count(selection=True), 4) np.testing.assert_array_almost_equal(ds.count(), 4) np.testing.assert_array_almost_equal(self.dataset.count("*", selection=None), 9) np.testing.assert_array_almost_equal(self.dataset_no_filter.count("*", selection=None), 10) np.testing.assert_array_almost_equal(self.dataset.count(), 9) np.testing.assert_array_almost_equal(self.dataset_no_filter.count(), 10) np.testing.assert_array_almost_equal(self.dataset.count("*", selection=True), 4) np.testing.assert_array_almost_equal(ds.count(), 4) task = self.dataset.count("x", selection=True, delay=True) self.dataset.execute() np.testing.assert_array_almost_equal(task.get(), 4) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=None, binby=["x"], limits=[0, 10], shape=1), [9]) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=True, binby=["x"], limits=[0, 10], shape=1), [4]) np.testing.assert_array_almost_equal(self.dataset.count("*", selection=None, binby=["x"], limits=[0, 10], shape=1), [9]) np.testing.assert_array_almost_equal(self.dataset.count("*", selection=True, binby=["x"], limits=[0, 10], shape=1), [4]) np.testing.assert_array_almost_equal(self.dataset .count("*", selection=None, binby=["y"], limits=[0, 9**2+1], shape=1), [9]) np.testing.assert_array_almost_equal(self.dataset_no_filter.count("*", selection=None, binby=["y"], limits=[0, 9**2+1], shape=1), [10]) np.testing.assert_array_almost_equal(self.dataset.count("*", selection=True, binby=["y"], limits=[0, 9**2+1], shape=1), [4]) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=None, binby=["y"], limits=[0, 9**2+1], shape=1), [9]) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=True, binby=["y"], limits=[0, 9**2+1], shape=1), [4]) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=None, binby=["x"], limits=[0, 10], shape=2), [4, 5]) np.testing.assert_array_almost_equal(self.dataset.count("x", selection=True, binby=["x"], limits=[0, 10], shape=2), [4, 0]) ds = self.dataset a = ds.count("x", binby="y", limits=[0, 100], shape=2) ds.select("(y >= 0) & (y < 50)") b = ds.count("x", selection=True) ds.select("(y >= 50) & (y < 100)") c = ds.count("x", selection=True) np.testing.assert_array_almost_equal(a, [b, c]) ds = self.dataset[(self.dataset.y >= 0) & (self.dataset.y < 50)] b = ds.count('x') ds = self.dataset[(self.dataset.y >= 50) & (self.dataset.y < 100)] c = ds.count('x') np.testing.assert_array_almost_equal(a, [b, c]) df = self.df[(self.df.y >= 0) & (self.df.y < 50)] b = df['x'].count() df = self.df[(self.df.y >= 50) & (self.df.y < 100)] c = df['x'].count() np.testing.assert_array_almost_equal(a, [b, c]) def test_cov(self): # convert to float x = self.dataset_local.columns["x"][:10] = self.dataset_local.columns["x"][:10] * 1. y = self.y def cov(*args): return np.cov(args, bias=1) self.dataset.select("x < 5") np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=None), cov(x, y)) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=True), cov(x[:5], y[:5])) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=[False, True]), [cov(x, y), cov(x[:5], y[:5])]) #self.dataset.columns["x"][0] = np.nan np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=None), cov(x, y)) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=True), cov(x[:5], y[:5])) task = self.dataset.cov("x", "y", selection=True, delay=True) self.dataset.execute() np.testing.assert_array_almost_equal(task.get(), cov(x[:5], y[:5])) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=None, binby=["x"], limits=[0, 10], shape=1), [cov(x, y)]) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=True, binby=["x"], limits=[0, 10], shape=1), [cov(x[:5], y[:5])]) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=None, binby=["y"], limits=[0, 9**2+1], shape=1), [cov(x, y)]) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=True, binby=["y"], limits=[0, 9**2+1], shape=1), [cov(x[:5], y[:5])]) nan22 = [[np.nan, np.nan], [np.nan, np.nan]] np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=None, binby=["x"], limits=[0, 10], shape=2), [cov(x[:5], y[:5]), cov(x[5:], y[5:])]) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=True, binby=["x"], limits=[0, 10], shape=2), [cov(x[:5], y[:5]), nan22]) i = 7 np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=None, binby=["y"], limits=[0, 9**2+1], shape=2), [cov(x[:i], y[:i]), cov(x[i:], y[i:])]) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=True, binby=["y"], limits=[0, 9**2+1], shape=2), [cov(x[:5], y[:5]), nan22]) i = 5 np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=None, binby=["x"], limits=[0, 10], shape=2), [cov(x[:i], y[:i]), cov(x[i:], y[i:])]) np.testing.assert_array_almost_equal(self.dataset.cov("x", "y", selection=True, binby=["x"], limits=[0, 10], shape=2), [cov(x[:i], y[:i]), nan22]) # include 3rd varialble self.dataset.add_virtual_column("z", "x*y") z = self.dataset.evaluate("z") np.testing.assert_array_almost_equal(self.dataset.cov(["x", "y", "z"], selection=None), cov(x, y, z)) nan33 = [[np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]] np.testing.assert_array_almost_equal(self.dataset.cov(["x", "y", "z"], selection=None, binby=["x"], limits=[0, 10], shape=2), [cov(x[:5], y[:5], z[:5]), cov(x[5:], y[5:], z[5:])]) np.testing.assert_array_almost_equal(self.dataset.cov(["x", "y", "z"], selection=True, binby=["x"], limits=[0, 10], shape=2), [cov(x[:5], y[:5], z[:5]), nan33]) i = 7 np.testing.assert_array_almost_equal(self.dataset.cov(["x", "y", "z"], selection=None, binby=["y"], limits=[0, 9**2+1], shape=2), [cov(x[:i], y[:i], z[:i]), cov(x[i:], y[i:], z[i:])]) np.testing.assert_array_almost_equal(self.dataset.cov(["x", "y", "z"], selection=True, binby=["y"], limits=[0, 9**2+1], shape=2), [cov(x[:5], y[:5], z[:5]), nan33]) i = 5 np.testing.assert_array_almost_equal(self.dataset.cov(["x", "y", "z"], selection=None, binby=["x"], limits=[0, 10], shape=2), [cov(x[:i], y[:i], z[:i]), cov(x[i:], y[i:], z[i:])]) np.testing.assert_array_almost_equal(self.dataset.cov(["x", "y", "z"], selection=True, binby=["x"], limits=[0, 10], shape=2), [cov(x[:i], y[:i], z[:i]), nan33]) # including nan n = np.arange(-self.zero_index, 20.-1) n[self.zero_index+1] = np.nan self.dataset_local.add_column('n', n) assert not np.any(np.isnan(self.dataset.cov("x", "n"))) def test_covar(self): # convert to float x = self.dataset_local.columns["x"][:10] = self.dataset_local.columns["x"][:10] * 1. y = self.y def covar(x, y): mask = np.isfinite(x * y) #w = np.isfinite(x * y) * 1.0 x = x[mask] y = y[mask] return np.cov([x, y], bias=1)[1,0] self.dataset.select("x < 5") np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=None), covar(x, y)) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=True), covar(x[:5], y[:5])) #self.dataset.columns["x"][0] = np.nan np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=None), covar(x, y)) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=True), covar(x[:5], y[:5])) task = self.dataset.covar("x", "y", selection=True, delay=True) self.dataset.execute() np.testing.assert_array_almost_equal(task.get(), covar(x[:5], y[:5])) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=None, binby=["x"], limits=[0, 10], shape=1), [covar(x, y)]) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=True, binby=["x"], limits=[0, 10], shape=1), [covar(x[:5], y[:5])]) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=None, binby=["y"], limits=[0, 9**2+1], shape=1), [covar(x, y)]) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=True, binby=["y"], limits=[0, 9**2+1], shape=1), [covar(x[:5], y[:5])]) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=None, binby=["x"], limits=[0, 10], shape=2), [covar(x[:5], y[:5]), covar(x[5:], y[5:])]) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=True, binby=["x"], limits=[0, 10], shape=2), [covar(x[:5], y[:5]), np.nan]) i = 7 np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=None, binby=["y"], limits=[0, 9**2+1], shape=2), [covar(x[:i], y[:i]), covar(x[i:], y[i:])]) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=True, binby=["y"], limits=[0, 9**2+1], shape=2), [covar(x[:5], y[:5]), np.nan]) i = 5 np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=None, binby=["x"], limits=[0, 10], shape=2), [covar(x[:i], y[:i]), covar(x[i:], y[i:])]) np.testing.assert_array_almost_equal(self.dataset.covar("x", "y", selection=True, binby=["x"], limits=[0, 10], shape=2), [covar(x[:i], y[:i]), np.nan]) def test_percentile(self): ds = vx.example() #ds.median_approx('z', binby=['x'], limits=[-10, 10], shape=16) #ds.median_approx('z', binby=['x', 'y'], limits=[-10, 10], shape=4) #m = ds.median_approx('z+x/10', binby=['x'], limits=[-10, 10], shape=32, percentile_shape=128*10 , percentile_limits=[-10,10]) m = ds.median_approx('z+x/10', binby=['x'], limits=[6.875000, 7.500000], shape=1, percentile_shape=128*10 , percentile_limits=[-10,10]) mc = ds.median_approx("z+x/10", selection='(x > 6.875000) & (x <= 7.500000)', percentile_shape=128*10 , percentile_limits=[-10,10]) #print(m, m[32-5], mc) print(m, mc) return dsodd = vx.from_arrays(x=np.arange(3)) # 0,1,2 dseven = vx.from_arrays(x=np.arange(4)) # 0,1,2,3 self.dataset.select("x < 5") o = 0#10/30/2. #x = dsodd.data.x ds = dsodd #ds = dseven x = ds.data.x print("median", np.median(x)) for offset in [-0.99, -0.5, 0.0]:#[0:1]: print() print("offset", offset) limits = [0+offset, x.max()+1+offset] print(">>>", ds.percentile_approx("x", selection=None, percentile_limits=limits, percentile_shape=len(x)),) #np.testing.assert_array_almost_equal( # ds.percentile_approx("x", selection=None, percentile_limits=limits, percentile_shape=4), # np.median(x), decimal=2) #return np.testing.assert_array_almost_equal( self.dataset.percentile_approx("x", selection=None, percentile_limits=[0-o, 10-o], percentile_shape=100), np.median(self.x), decimal=1) np.testing.assert_array_almost_equal( self.dataset.percentile_approx("x", selection=None, percentile_limits=[0-o, 10-o], percentile_shape=1000), np.median(self.x), decimal=2) np.testing.assert_array_almost_equal( self.dataset.percentile_approx(["x", "y"], selection=None, percentile_shape=10000), [np.median(self.x), np.median(self.y)], decimal=3) return np.testing.assert_array_almost_equal(self.dataset.percentile_approx("x", selection=True), np.median(self.x[:5])) # convert to float x = self.dataset.columns["x"] = self.dataset.columns["x"] * 1. y = self.y self.dataset.columns["x"][0] = np.nan np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=None), np.nansum(x)) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=True), np.nansum(x[:5])) task = self.dataset.sum("x", selection=True, delay=True) self.dataset.execute() np.testing.assert_array_almost_equal(task.get(), np.nansum(x[:5])) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=None, binby=["x"], limits=[0, 10], shape=1), [np.nansum(x)]) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=True, binby=["x"], limits=[0, 10], shape=1), [np.nansum(x[:5])]) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=None, binby=["y"], limits=[0, 9**2+1], shape=1), [np.nansum(x)]) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=True, binby=["y"], limits=[0, 9**2+1], shape=1), [np.nansum(x[:5])]) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=None, binby=["x"], limits=[0, 10], shape=2), [np.nansum(x[:5]), np.nansum(x[5:])]) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=True, binby=["x"], limits=[0, 10], shape=2), [np.nansum(x[:5]), 0]) i = 7 np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=None, binby=["y"], limits=[0, 9**2+1], shape=2), [np.nansum(x[:i]), np.nansum(x[i:])]) np.testing.assert_array_almost_equal(self.dataset.sum("x", selection=True, binby=["y"], limits=[0, 9**2+1], shape=2), [np.nansum(x[:5]), 0]) i = 5 np.testing.assert_array_almost_equal(self.dataset.sum("y", selection=None, binby=["x"], limits=[0, 10], shape=2), [np.nansum(y[:i]), np.nansum(y[i:])]) np.testing.assert_array_almost_equal(self.dataset.sum("y", selection=True, binby=["x"], limits=[0, 10], shape=2), [np.nansum(y[:5]), 0]) def test_concat(self): dc = self.dataset_concat_dup self.assertEqual(len(self.dataset_concat_dup), len(self.dataset)*3) self.assertEqual(self.dataset_concat.get_column_names(), ["x"]) N = len(self.x_concat) # try out every possible slice for i1 in range(N-1): for i2 in range(i1+1,N): #print "***", i1, i2 a = self.dataset_concat.columns["x"][i1:i2] b = self.x_concat[i1:i2] #print a, b np.testing.assert_array_almost_equal(a, b) def concat(*types): arrays = [np.arange(3, dtype=dtype) for dtype in types] N = len(arrays) dfs = [vx.dataset.DatasetArrays("dataset-%i" % i) for i in range(N)] for dataset, array in zip(dfs, arrays): dataset.add_column("x", array) dataset_concat = vx.dataset.DatasetConcatenated(dfs, name="dataset_concat") return dataset_concat self.assertEqual(concat(np.float32, np.float64).columns["x"].dtype, np.float64) self.assertEqual(concat(np.float32, np.int64).columns["x"].dtype, np.float64) self.assertEqual(concat(np.float32, np.byte).columns["x"].dtype, np.float32) self.assertEqual(concat(np.float64, np.byte, np.int64).columns["x"].dtype, np.float64) ar1 = np.zeros((10, 2)) ar2 = np.zeros((20)) arrays = [ar1, ar2] N = len(arrays) dfs = [vx.dataset.DatasetArrays("dataset1") for i in range(N)] for dataset, array in zip(dfs, arrays): dataset.add_column("x", array) with self.assertRaises(ValueError): dataset_concat = vx.dataset.DatasetConcatenated(dfs, name="dataset_concat") ar1 = np.zeros((10)) ar2 = np.zeros((20)) arrays = [ar1, ar2] N = len(arrays) dfs = [vx.dataset.DatasetArrays("dataset1") for i in range(N)] for dataset, array in zip(dfs, arrays): dataset.add_column("x", array) dataset_concat = vx.dataset.DatasetConcatenated(dfs, name="dataset_concat") dataset_concat1 = vx.dataset.DatasetConcatenated(dfs, name="dataset_concat") dataset_concat2 = vx.dataset.DatasetConcatenated(dfs, name="dataset_concat") self.assertEqual(len(dataset_concat1.concat(dataset_concat2).dfs), 4) self.assertEqual(len(dataset_concat1.concat(dfs[0]).dfs), 3) self.assertEqual(len(dfs[0].concat(dataset_concat1).dfs), 3) self.assertEqual(len(dfs[0].concat(dfs[0]).dfs), 2) def test_export_concat(self): x1 = np.arange(1000, dtype=np.float32) x2 = np.arange(100, dtype=np.float32) self.x_concat = np.concatenate((x1, x2)) dataset1 = vx.dataset.DatasetArrays("dataset1") dataset2 = vx.dataset.DatasetArrays("dataset2") dataset1.add_column("x", x1) dataset2.add_column("x", x2) self.dataset_concat = vx.dataset.DatasetConcatenated([dataset1, dataset2], name="dataset_concat") path_hdf5 = tempfile.mktemp(".hdf5") self.dataset_concat.export_hdf5(path_hdf5) def test_export_sorted(self): self.dataset.add_column("s", 100-self.dataset.data.x) path_hdf5 = tempfile.mktemp(".hdf5") self.dataset.export_hdf5(path_hdf5, sort="s") ds2 = vaex.open(path_hdf5) np.testing.assert_array_equal(self.dataset.data.x[self.zero_index:self.zero_index+10], ds2.data.x[::-1]) def test_export_sorted_arrow(self): self.dataset.add_column("s", 100-self.dataset.data.x) path_arrow = tempfile.mktemp(".arrow") self.dataset.export_arrow(path_arrow, sort="s") ds2 = vaex.open(path_arrow, as_numpy=False) np.testing.assert_array_equal(self.dataset.data.x[self.zero_index:self.zero_index+10], np.array(ds2.data.x)[::-1]) def test_export(self): path = path_hdf5 = tempfile.mktemp(".hdf5") path_fits = tempfile.mktemp(".fits") path_fits_astropy = tempfile.mktemp(".fits") #print path #with self.assertRaises(AssertionError): # self.dataset.export_hdf5(path, selection=True) for dataset in [self.dataset_concat_dup, self.dataset]: #print dataset.virtual_columns for fraction in [1, 0.5]: dataset.set_active_fraction(fraction) dataset.select("x > 3") dataset.select("x > 2", name="named") length = len(dataset) for column_names in [["x", "y", "z"], ["x"], ["y"], ["z"], None]: for byteorder in "<=>": for shuffle in [False, True]: for selection in [False, True, "named"]: for virtual in [False, True]: for export in [dataset.export_fits, dataset.export_hdf5, dataset.export_arrow, dataset.export_parquet]: #if byteorder == ">" else [dataset.export_hdf5]: #print (">>>", dataset, path, column_names, byteorder, shuffle, selection, fraction, dataset.length_unfiltered(), virtual) #byteorder = "<" if export == dataset.export_fits and byteorder != ">": #print("skip", export == dataset.export_fits, byteorder != ">", byteorder) continue # fits only does big endian if export == dataset.export_fits and byteorder == ">": continue # arrow only little endian if vx.utils.osname == "windows" and export == dataset.export_hdf5 and byteorder == ">": #print("skip", vx.utils.osname) continue # TODO: IS this a bug for h5py on win32?, leads to an open file # same issue on windows for arrow, closing the mmapped file does not help # for the moment we create a new temp file path_arrow = tempfile.mktemp(".arrow") path_parquet = tempfile.mktemp(".parquet") #print dataset.length_unfiltered() #print len(dataset) if export == dataset.export_hdf5: path = path_hdf5 export(path, column_names=column_names, byteorder=byteorder, shuffle=shuffle, selection=selection, progress=False, virtual=virtual) elif export == dataset.export_arrow: path = path_arrow export(path, column_names=column_names, byteorder=byteorder, shuffle=shuffle, selection=selection, progress=False, virtual=virtual) elif export == dataset.export_parquet: path = path_parquet export(path, column_names=column_names, byteorder=byteorder, shuffle=shuffle, selection=selection, progress=False, virtual=virtual) else: path = path_fits export(path, column_names=column_names, shuffle=shuffle, selection=selection, progress=False, virtual=virtual) with astropy.io.fits.open(path) as fitsfile: # make sure astropy can read the data bla = fitsfile[1].data try: fitsfile.writeto(path_fits_astropy) finally: os.remove(path_fits_astropy) if path.endswith('arrow') or path.endswith('parquet'): compare = vx.open(path, as_numpy=False) else: compare = vx.open(path) if column_names is None: column_names = ["x", "y", "m", "mi", "ints", "f", "z", "name", "name_arrow"] if virtual else ["x", "y", "m", "mi", "ints", "f", "name", "name_arrow"] #if not virtual: # if "z" in column_names: # column_names.remove("z") # TODO: does the order matter? self.assertEqual((compare.get_column_names(strings=True)), (column_names + (["random_index"] if shuffle else []))) def make_masked(ar): if export == dataset.export_fits: # for fits the missing values will be filled in with nan if ar.dtype.kind == "f": nanmask = np.isnan(ar) if np.any(nanmask): ar = np.ma.array(ar, mask=nanmask) return ar for column_name in column_names: #values = dataset.columns[column_name][dataset._index_start:dataset._index_end] if column_name in dataset.get_column_names(virtual=False) else dataset.evaluate(column_name) values = dataset.evaluate(column_name, filtered=False) if selection: values = dataset.evaluate(column_name, array_type="numpy") mask = dataset.evaluate_selection_mask(selection)#, 0, len(dataset)) if len(values[::]) != len(mask): import pdb pdb.set_trace() # for concatenated columns, we get a plain numpy array copy using [::] a = np.ma.compressed(make_masked(compare.evaluate(column_name, array_type="numpy"))) b = np.ma.compressed(make_masked(values[::][mask])) if len(a) != len(b): import pdb pdb.set_trace() self.assertEqual(sorted(a), sorted(b)) else: values = dataset.evaluate(column_name, array_type="numpy") if shuffle: indices = compare.columns["random_index"] a = np.ma.compressed(make_masked(compare.evaluate(column_name, array_type="numpy"))) b = np.ma.compressed(make_masked(values[::][indices])) self.assertEqual(sorted(a), sorted(b)) else: dtype = np.array(compare.columns[column_name]).dtype # we don't want any casting compare_values = compare.columns[column_name] if isinstance(compare_values, vaex.column.Column): compare_values = compare_values.to_numpy() np.testing.assert_array_equal(compare_values, values[:length].astype(dtype)) compare.close_files() #os.remove(path) # self.dataset_concat_dup references self.dataset, so set it's active_fraction to 1 again dataset.set_active_fraction(1) path_arrow = tempfile.mktemp(".arrow") path_hdf5 = tempfile.mktemp(".hdf5") dataset = self.dataset dataset.export(path_arrow) name = "vaex export" #print(path_fits) vaex.export.main([name, "--no-progress", "-q", "file", path_arrow, path_hdf5]) backup = vaex.vaex.utils.check_memory_usage try: vaex.vaex.utils.check_memory_usage = lambda *args: False assert vaex.export.main([name, "--no-progress", "-q", "soneira", "--dimension=2", "-m=40", path_hdf5]) == 1 finally: vaex.utils.check_memory_usage = backup assert vaex.export.main([name, "--no-progress", "-q", "soneira", "--dimension=2", "-m=20", path_hdf5]) == 0 def test_fraction(self): counter_selection = CallbackCounter() counter_current_row = CallbackCounter() self.dataset.signal_pick.connect(counter_current_row) self.dataset.signal_selection_changed.connect(counter_selection) self.dataset.set_active_fraction(1.0) # this shouldn't trigger self.assertEqual(counter_selection.counter, 0) self.assertEqual(counter_current_row.counter, 0) length = len(self.dataset) self.dataset.set_active_fraction(0.1) # this should trigger self.assertEqual(counter_selection.counter, 1) self.assertEqual(counter_current_row.counter, 1) # test for event and the effect of the length # the active_fraction only applies to the underlying length, which is 20 self.dataset.set_active_fraction(0.25) self.assertEqual(counter_selection.counter, 2) self.assertEqual(counter_current_row.counter, 2) self.assertEqual(length/2 - self.zero_index, len(self.dataset)) self.dataset.select("x > 5") self.assertEqual(counter_selection.counter, 3) self.assertEqual(counter_current_row.counter, 2) self.assertTrue(self.dataset.has_selection()) self.dataset.set_active_fraction(0.25) # nothing should happen, still the same self.assertTrue(self.dataset.has_selection()) self.dataset.set_active_fraction(0.4999) self.assertFalse(self.dataset.has_selection()) self.dataset.set_current_row(1) self.assertTrue(self.dataset.has_current_row()) self.dataset.set_active_fraction(0.25) self.assertFalse(self.dataset.has_current_row()) if self.dataset.is_local(): # this part doesn't work for remote dfs for dataset in [self.dataset, self.dataset_concat]: dataset.set_active_fraction(1.0) x = dataset.columns["x"][:] * 1. # make a copy dataset.set_active_fraction(0.25) length = len(dataset) a = x[:length] b = dataset.columns["x"][:len(dataset)] np.testing.assert_array_almost_equal(a, b) self.assertLess(length, dataset.length_original()) # TODO: test if statistics and histogram work on the active_fraction self.dataset.set_active_fraction(1) total = self.dataset["x"].sum() self.dataset.set_active_fraction(0.25) total_half = self.dataset["x"].sum() self.assertLess(total_half, total) limits = [(-100, 100)] self.dataset.set_active_fraction(1) total = self.dataset["x"].count(limits=limits).sum() self.dataset.set_active_fraction(0.25) total_half = self.dataset["x"].count(limits=limits).sum() self.assertLess(total_half, total) def test_current_row(self): counter_current_row = CallbackCounter() self.dataset.signal_pick.connect(counter_current_row) self.dataset.set_current_row(0) self.assertEqual(counter_current_row.counter, 1) with self.assertRaises(IndexError): self.dataset.set_current_row(-1) with self.assertRaises(IndexError): self.dataset.set_current_row(len(self.dataset)) def t_not_needed_est_current(self): for dataset in [self.dataset, self.dataset_concat]: for i in range(len(dataset)): dataset.set_current_row(i) values = dataset("x", "x**2").current() value = dataset.columns["x"][:][i] self.assertEqual([value, value**2], values) def test_dropna(self): ds = self.dataset ds.select_non_missing(column_names=['m']) self.assertEqual(ds.count(selection=True), 9) ds.select_non_missing(drop_masked=False, column_names=['m']) self.assertEqual(ds.count(selection=True), 10) self.dataset_local.data.x[self.zero_index] = np.nan ds.select_non_missing(column_names=['x']) self.assertEqual(ds.count(selection=True), 9) ds.select_non_missing(drop_nan=False, column_names=['x']) if ds.is_local(): self.assertEqual(ds.count(selection=True), 10) else: # TODO: on the server, the filter selection gets re-executed (x < 10) # causing it to skip the nan anyway, find a good way to test this? self.assertEqual(ds.count(selection=True), 9) ds.select_non_missing() self.assertEqual(ds.count(selection=True), 8) ds.select_non_missing(drop_masked=False) self.assertEqual(ds.count(selection=True), 9) ds.select_non_missing(drop_nan=False) if ds.is_local(): self.assertEqual(ds.count(selection=True), 9) else: # TODO: same as above self.assertEqual(ds.count(selection=True), 8) def test_selection_in_handler(self): self.dataset.select("x > 5") # in the handler, we should know there is not selection def check(*ignore): self.assertFalse(self.dataset.has_selection()) self.dataset.signal_selection_changed.connect(check) self.dataset.select_nothing() def test_nearest(self): index, distance, (value,) = self.dataset("x").nearest([3]) self.assertEqual(index, 3 + self.zero_index) self.assertEqual(distance, 0) self.assertEqual(value, 3) index, distance, (value,) = self.dataset("x").nearest([3.7]) self.assertEqual(index, 4 + self.zero_index) self.assertAlmostEqual(distance, 0.3) self.assertEqual(value, 4) self.dataset.select("x > 5") index, distance, (value,) = self.dataset("x").selected().nearest([3.7]) self.assertEqual(index, 6 + self.zero_index) self.assertEqual(distance, 2.3) self.assertEqual(value, 6) def test_select_circle(self): # Circular selection self.dataset.select_circle('x', 'y', 0.5, 0.5, 1, name='circ') # Assert np.testing.assert_equal(2, self.dataset.count(selection='circ')) def test_select_ellipse(self): # Ellipse election self.dataset.select_ellipse('x', 'y', 3, 10, 2, 15, -10, name='elli') # Assert np.testing.assert_equal(3, self.dataset.count(selection='elli')) # allow multiple python versions on one machine to run the test import sys test_port = 29110 + sys.version_info[0] * 10 + sys.version_info[1] #class A:#class estDatasetRemote(TestDataset): class TestDatasetRemote(TestDataset): #class A: use_websocket = True @classmethod def setUpClass(cls): global test_port cls.webserver = vaex.server.WebServer(dfs=[], port=test_port, cache_byte_size=0).tornado_server #print "serving" cls.webserver.serve_threaded() #print "getting server object" scheme = "ws" if cls.use_websocket else "http" cls.server = vx.server("%s://localhost:%d" % (scheme, test_port)) test_port += 1 @classmethod def tearDownClass(cls): cls.server.close() cls.webserver.stop_serving() def setUp(self): # run all tests from TestDataset, but now served at the server super(TestDatasetRemote, self).setUp() # for the webserver we don't support filters on top of filters # so the server always uses the full dataset # self.dataset_no_filter.name = 'dataset' # self.dataset = self.dataset_no_filter self.dataset_local = self.dataset self.datasetxy_local = self.datasetxy self.dataset_concat_local = self.dataset_concat self.dataset_concat_dup_local = self.dataset_concat_dup dfs = [self.dataset_local, self.datasetxy_local, self.dataset_concat_local, self.dataset_concat_dup_local] #print "get dfs" self.webserver.set_dfs(dfs) dfs = self.server.dfs(as_dict=True) #print "got it", dfs self.dataset = dfs["dataset"] self.datasetxy = dfs["datasetxy"] self.dataset_concat = dfs["dataset_concat"] self.dataset_concat_dup = dfs["dataset_concat_dup"] #print "all done" def tearDown(self): TestDataset.tearDown(self) #print "stop serving" def test_to(self): pass # not supported def test_amuse(self): pass # no need def test_ascii(self): pass # no need def test_csv(self): pass # no need def test_export(self): pass # we can't export atm def test_concat(self): pass # doesn't make sense to test this for remote def test_data_access(self): pass def test_byte_size(self): pass # we don't know the selection's length for dataset remote.. def test_add_column(self): pass # can't add column to remove objects def test_rename_column(self): pass # TODO: we cannot do that now def test_masked_array_output(self): pass # cannot test exporting def test_export_sorted(self): pass # cannot test exporting def test_formats(self): pass # cannot test exporting def test_default_selection(self): pass # uses local information #def test_selection(self): # pass #def test_count(self): # pass #def test_sum(self): # pass #def test_cov(self): # pass #def test_correlation(self): # pass #def test_covar(self): # pass #def test_mean(self): # pass #def test_minmax(self): # pass #def test_var_and_std(self): # pass #def test_limits(self): # pass # import vaex.distributed #class A:#class T_estDatasetDistributed(unittest.TestCase): #class TestDatasetDistributed(unittest.TestCase): class TestDatasetDistributed(TestDatasetRemote): use_websocket = False def setUp(self): TestDataset.setUp(self) global test_port # self.dataset_local = self.dataset = dataset.DatasetArrays("dataset") self.dataset_local = self.dataset dfs = [self.dataset] dfs_copy = [k.copy() for k in dfs] # otherwise we share the selection cache for ds in dfs_copy: ds.name = self.dataset_local.name dfs = [self.dataset] self.webserver1 = vaex.server.WebServer(dfs=dfs, port=test_port).tornado_server self.webserver1.serve_threaded() test_port += 1 self.webserver2 = vaex.server.WebServer(dfs=dfs_copy, port=test_port).tornado_server self.webserver2.serve_threaded() test_port += 1 scheme = "ws" if self.use_websocket else "http" self.server1 = vx.server("%s://localhost:%d" % (scheme, test_port-2)) self.server2 = vx.server("%s://localhost:%d" % (scheme, test_port-1)) test_port += 1 dfs1 = self.server1.dfs(as_dict=True) dfs2 = self.server2.dfs(as_dict=True) self.dfs = [dfs1["dataset"], dfs2["dataset"]] self.dataset = vaex.distributed.DatasetDistributed(self.dfs) def tearDown(self): #TestDataset.tearDown(self) #print "stop serving" self.webserver1.stop_serving() self.webserver2.stop_serving() def test_histogram(self): #print self.dataset, self.dataset.__call__ #print self.dataset.subspace("x") #self.dataset_local.set_active_range(5, 10) counts = self.dataset("x").histogram([[0,10]], size=10) #import pdb #pdb.set_trace() self.assertTrue(all(counts == 1), "counts is %r" % counts) return sums = self.dataset("x").histogram([[0,10]], size=10, weight="y") assert(all(sums == self.y)) self.dataset.select("x < 5") mask = self.x < 5 counts = self.dataset("x").selected().histogram([[0,10]], size=10) mod_counts = counts * 1. mod_counts[~mask] = 0 assert(all(counts == mod_counts)) mod_sums = self.y * 1. mod_sums[~mask] = 0 sums = self.dataset("x").selected().histogram([[0,10]], size=10, weight="y") assert(all(sums == mod_sums)) x = np.array([0, 1, 0, 1]) y = np.array([0, 0, 1, 1]) dataset = vx.from_arrays(x=x, y=y) counts = dataset("x", "y").histogram([[0.,2.], [0.,2.]], size=2) assert(np.all(counts == 1)) x = np.array([0, 1, 0, 1, 0, 1, 0, 1]) y = np.array([0, 0, 1, 1, 0, 0, 1, 1]) z = np.array([0, 0, 0, 0, 1, 1, 1, 1]) dataset = vx.from_arrays(x=x, y=y, z=z) counts = dataset("x", "y", "z").histogram([[0.,2.], [0.,2.], [0.,2.]], size=2) assert(np.all(counts == 1)) x = np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]) y = np.array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]) z = np.array([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]) w = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,]) dataset = vx.from_arrays(x=x, y=y, z=z, w=w) counts = dataset("x", "y", "z", "w").histogram([[0.,2.], [0.,2.], [0.,2.], [0.,2.]], size=2) assert(np.all(counts == 1)) return #class TestDatasetRemotePlain(TestDatasetRemote): # use_websocket = False """ class T_stWebServer(unittest.TestCase): def setUp(self): self.dataset = dataset.DatasetArrays() self.x = x = np.arange(10) self.y = y = x ** 2 self.dataset.add_column("x", x) self.dataset.add_column("y", y) self.webserver = vaex.server.WebServer(dfs=[self.dataset], port=test_port).tornado_server self.webserver.serve_threaded() self.server = vx.server("http://localhost:%d" % test_port) self.dataset_remote = self.server.dfs()[0] def tearDown(self): self.webserver.stop_serving() def test_list(self): dfs = self.server.dfs() self.assertTrue(len(dfs) == 1) dataset_remote = dfs[0] self.assertEqual(dataset_remote.name, self.dataset.name) self.assertEqual(dataset_remote.get_column_names(), self.dataset.get_column_names()) self.assertEqual(len(dataset_remote), len(self.dataset)) def test_minmax(self): self.assertEqual(self.dataset_remote("x", "y").minmax().tolist(), self.dataset("x", "y").minmax().tolist()) def test_var(self): self.assertEqual(self.dataset_remote("x", "y").var().tolist(), self.dataset("x", "y").var().tolist()) def test_histogram(self): grid1 = self.dataset("x").bounded().gridded(32).grid grid2 = self.dataset_remote("x").bounded().gridded(32).grid self.assertEqual(grid1.tolist(), grid2.tolist()) """ if __name__ == '__main__': unittest.main()
[ "vaex.dataset.select", "os.remove", "vaex.dataset.length_original", "numpy.random.seed", "numpy.sum", "vaex.from_scalars", "vaex.server", "numpy.ones", "vaex.set_log_level_exception", "vaex.dataset.set_current_row", "numpy.isnan", "vaex.dataset.DatasetArrays", "numpy.arange", "numpy.random.normal", "vaex.example", "numpy.testing.assert_array_almost_equal", "vaex.dataset.DatasetConcatenated", "os.path.join", "vaex.open", "unittest.main", "vaex.dataset.add_column", "os.path.dirname", "healpy.order2nside", "numpy.isfinite", "vaex.dataset.export", "numpy.cov", "tempfile.mktemp", "vaex.from_csv", "numpy.random.shuffle", "vaex.dataset.evaluate", "numpy.nansum", "numpy.median", "numpy.testing.assert_array_equal", "vaex.from_ascii", "vaex.dataset.set_active_fraction", "numpy.sort", "vaex.dataset.evaluate_selection_mask", "numpy.concatenate", "numpy.all", "astropy.io.votable.writeto", "vaex.dataset", "vaex.from_arrays", "vaex.from_pandas", "numpy.dtype", "numpy.zeros", "astropy.io.votable.from_table", "numpy.ma.array", "healpy.nside2npix", "numpy.any", "numpy.array", "pdb.set_trace", "numpy.sqrt" ]
[((399, 424), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (414, 424), False, 'import os\n'), ((1216, 1244), 'vaex.set_log_level_exception', 'vx.set_log_level_exception', ([], {}), '()\n', (1242, 1244), True, 'import vaex as vx\n'), ((71768, 71783), 'unittest.main', 'unittest.main', ([], {}), '()\n', (71781, 71783), False, 'import unittest\n'), ((1783, 1815), 'vaex.dataset.DatasetArrays', 'dataset.DatasetArrays', (['"""dataset"""'], {}), "('dataset')\n", (1804, 1815), True, 'import vaex.dataset as dataset\n'), ((2870, 2904), 'numpy.ma.array', 'np.ma.array', (['m'], {'mask': '(m == ma_value)'}), '(m, mask=m == ma_value)\n', (2881, 2904), True, 'import numpy as np\n'), ((4394, 4412), 'numpy.array', 'np.array', (['[0.0, 1]'], {}), '([0.0, 1])\n', (4402, 4412), True, 'import numpy as np\n'), ((4418, 4437), 'numpy.array', 'np.array', (['[-1.0, 1]'], {}), '([-1.0, 1])\n', (4426, 4437), True, 'import numpy as np\n'), ((4456, 4493), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""datasetxy"""'], {}), "('datasetxy')\n", (4480, 4493), True, 'import vaex as vx\n'), ((4574, 4592), 'numpy.array', 'np.array', (['[1.0, 3]'], {}), '([1.0, 3])\n', (4582, 4592), True, 'import numpy as np\n'), ((4599, 4620), 'numpy.array', 'np.array', (['[2.0, 3, 4]'], {}), '([2.0, 3, 4])\n', (4607, 4620), True, 'import numpy as np\n'), ((4628, 4643), 'numpy.array', 'np.array', (['[5.0]'], {}), '([5.0])\n', (4636, 4643), True, 'import numpy as np\n'), ((4661, 4689), 'numpy.concatenate', 'np.concatenate', (['(x1, x2, x3)'], {}), '((x1, x2, x3))\n', (4675, 4689), True, 'import numpy as np\n'), ((4704, 4740), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""dataset1"""'], {}), "('dataset1')\n", (4728, 4740), True, 'import vaex as vx\n'), ((4754, 4790), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""dataset2"""'], {}), "('dataset2')\n", (4778, 4790), True, 'import vaex as vx\n'), ((4804, 4840), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""dataset3"""'], {}), "('dataset3')\n", (4828, 4840), True, 'import vaex as vx\n'), ((4992, 5082), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['[dataset1, dataset2, dataset3]'], {'name': '"""dataset_concat"""'}), "([dataset1, dataset2, dataset3], name=\n 'dataset_concat')\n", (5022, 5082), True, 'import vaex as vx\n'), ((5107, 5243), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['[self.dataset_no_filter, self.dataset_no_filter, self.dataset_no_filter]'], {'name': '"""dataset_concat_dup"""'}), "([self.dataset_no_filter, self.\n dataset_no_filter, self.dataset_no_filter], name='dataset_concat_dup')\n", (5137, 5243), True, 'import vaex as vx\n'), ((5426, 5443), 'numpy.random.seed', 'np.random.seed', (['(0)'], {}), '(0)\n', (5440, 5443), True, 'import numpy as np\n'), ((8092, 8116), 'tempfile.mktemp', 'tempfile.mktemp', (['""".hdf5"""'], {}), "('.hdf5')\n", (8107, 8116), False, 'import tempfile\n'), ((8304, 8327), 'tempfile.mktemp', 'tempfile.mktemp', (['""".vot"""'], {}), "('.vot')\n", (8319, 8327), False, 'import tempfile\n'), ((8405, 8422), 'astropy.io.votable.from_table', 'from_table', (['table'], {}), '(table)\n', (8415, 8422), False, 'from astropy.io.votable import from_table, writeto\n'), ((8425, 8445), 'astropy.io.votable.writeto', 'writeto', (['votable', 'fn'], {}), '(votable, fn)\n', (8432, 8445), False, 'from astropy.io.votable import from_table, writeto\n'), ((11638, 11692), 'vaex.from_pandas', 'vx.from_pandas', (['df'], {'index_name': '"""name"""', 'copy_index': '(True)'}), "(df, index_name='name', copy_index=True)\n", (11652, 11692), True, 'import vaex as vx\n'), ((12688, 12795), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['extra', 'self.dataset.data.x[self.zero_index:self.zero_index + 10]'], {}), '(extra, self.dataset.data.x[self.\n zero_index:self.zero_index + 10])\n', (12724, 12795), True, 'import numpy as np\n'), ((13466, 13489), 'tempfile.mktemp', 'tempfile.mktemp', (['""".csv"""'], {}), "('.csv')\n", (13481, 13489), False, 'import tempfile\n'), ((13997, 14046), 'vaex.from_csv', 'vx.from_csv', (['fn'], {'index_col': '(False)', 'copy_index': '(True)'}), '(fn, index_col=False, copy_index=True)\n', (14008, 14046), True, 'import vaex as vx\n'), ((15563, 15591), 'numpy.arange', 'np.arange', (['(10.0)'], {'dtype': '""">f8"""'}), "(10.0, dtype='>f8')\n", (15572, 15591), True, 'import numpy as np\n'), ((15597, 15623), 'numpy.arange', 'np.arange', (['(10)'], {'dtype': '"""<f8"""'}), "(10, dtype='<f8')\n", (15606, 15623), True, 'import numpy as np\n'), ((15631, 15655), 'vaex.from_arrays', 'vx.from_arrays', ([], {'x': 'x', 'y': 'y'}), '(x=x, y=y)\n', (15645, 15655), True, 'import vaex as vx\n'), ((15723, 15741), 'numpy.random.seed', 'np.random.seed', (['(42)'], {}), '(42)\n', (15737, 15741), True, 'import numpy as np\n'), ((15748, 15779), 'numpy.arange', 'np.arange', (['(10)'], {'dtype': 'np.float64'}), '(10, dtype=np.float64)\n', (15757, 15779), True, 'import numpy as np\n'), ((15792, 15805), 'numpy.arange', 'np.arange', (['(10)'], {}), '(10)\n', (15801, 15805), True, 'import numpy as np\n'), ((15833, 15859), 'numpy.random.shuffle', 'np.random.shuffle', (['indices'], {}), '(indices)\n', (15850, 15859), True, 'import numpy as np\n'), ((17131, 17156), 'healpy.order2nside', 'hp.order2nside', (['max_order'], {}), '(max_order)\n', (17145, 17156), True, 'import healpy as hp\n'), ((17166, 17186), 'healpy.nside2npix', 'hp.nside2npix', (['nside'], {}), '(nside)\n', (17179, 17186), True, 'import healpy as hp\n'), ((17199, 17214), 'numpy.arange', 'np.arange', (['npix'], {}), '(npix)\n', (17208, 17214), True, 'import numpy as np\n'), ((17222, 17253), 'vaex.from_arrays', 'vx.from_arrays', ([], {'healpix': 'healpix'}), '(healpix=healpix)\n', (17236, 17253), True, 'import vaex as vx\n'), ((17651, 17678), 'numpy.random.normal', 'np.random.normal', (['(1)', '(0.1)', 'N'], {}), '(1, 0.1, N)\n', (17667, 17678), True, 'import numpy as np\n'), ((17691, 17726), 'vaex.from_arrays', 'vx.from_arrays', ([], {'parallax': 'parallaxes'}), '(parallax=parallaxes)\n', (17705, 17726), True, 'import vaex as vx\n'), ((25162, 25223), 'vaex.from_scalars', 'vx.from_scalars', ([], {'l': '(90)', 'b': '(0)', 'pm_l': '(-1)', 'pm_b': '(0)', 'distance': '(1)', 'vr': '(0)'}), '(l=90, b=0, pm_l=-1, pm_b=0, distance=1, vr=0)\n', (25177, 25223), True, 'import vaex as vx\n'), ((25391, 25452), 'vaex.from_scalars', 'vx.from_scalars', ([], {'l': '(90)', 'b': '(0)', 'pm_l': '(-1)', 'pm_b': '(0)', 'distance': '(2)', 'vr': '(0)'}), '(l=90, b=0, pm_l=-1, pm_b=0, distance=2, vr=0)\n', (25406, 25452), True, 'import vaex as vx\n'), ((25621, 25682), 'vaex.from_scalars', 'vx.from_scalars', ([], {'l': '(0)', 'b': '(90)', 'pm_l': '(0)', 'pm_b': '(-1)', 'distance': '(1)', 'vr': '(0)'}), '(l=0, b=90, pm_l=0, pm_b=-1, distance=1, vr=0)\n', (25636, 25682), True, 'import vaex as vx\n'), ((25850, 25911), 'vaex.from_scalars', 'vx.from_scalars', ([], {'l': '(0)', 'b': '(90)', 'pm_l': '(0)', 'pm_b': '(-1)', 'distance': '(2)', 'vr': '(0)'}), '(l=0, b=90, pm_l=0, pm_b=-1, distance=2, vr=0)\n', (25865, 25911), True, 'import vaex as vx\n'), ((26080, 26140), 'vaex.from_scalars', 'vx.from_scalars', ([], {'l': '(90)', 'b': '(0)', 'pm_l': '(0)', 'pm_b': '(0)', 'distance': '(1)', 'vr': '(1)'}), '(l=90, b=0, pm_l=0, pm_b=0, distance=1, vr=1)\n', (26095, 26140), True, 'import vaex as vx\n'), ((26307, 26367), 'vaex.from_scalars', 'vx.from_scalars', ([], {'l': '(90)', 'b': '(0)', 'pm_l': '(0)', 'pm_b': '(0)', 'distance': '(2)', 'vr': '(1)'}), '(l=90, b=0, pm_l=0, pm_b=0, distance=2, vr=1)\n', (26322, 26367), True, 'import vaex as vx\n'), ((26636, 26660), 'tempfile.mktemp', 'tempfile.mktemp', (['""".json"""'], {}), "('.json')\n", (26651, 26660), False, 'import tempfile\n'), ((38072, 38119), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['a', '[b, c]'], {}), '(a, [b, c])\n', (38108, 38119), True, 'import numpy as np\n'), ((38299, 38346), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['a', '[b, c]'], {}), '(a, [b, c])\n', (38335, 38346), True, 'import numpy as np\n'), ((38501, 38548), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['a', '[b, c]'], {}), '(a, [b, c])\n', (38537, 38548), True, 'import numpy as np\n'), ((42445, 42482), 'numpy.arange', 'np.arange', (['(-self.zero_index)', '(20.0 - 1)'], {}), '(-self.zero_index, 20.0 - 1)\n', (42454, 42482), True, 'import numpy as np\n'), ((45156, 45168), 'vaex.example', 'vx.example', ([], {}), '()\n', (45166, 45168), True, 'import vaex as vx\n'), ((50187, 50204), 'numpy.zeros', 'np.zeros', (['(10, 2)'], {}), '((10, 2))\n', (50195, 50204), True, 'import numpy as np\n'), ((50213, 50225), 'numpy.zeros', 'np.zeros', (['(20)'], {}), '(20)\n', (50221, 50225), True, 'import numpy as np\n'), ((50536, 50548), 'numpy.zeros', 'np.zeros', (['(10)'], {}), '(10)\n', (50544, 50548), True, 'import numpy as np\n'), ((50559, 50571), 'numpy.zeros', 'np.zeros', (['(20)'], {}), '(20)\n', (50567, 50571), True, 'import numpy as np\n'), ((50774, 50832), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['dfs'], {'name': '"""dataset_concat"""'}), "(dfs, name='dataset_concat')\n", (50804, 50832), True, 'import vaex as vx\n'), ((50855, 50913), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['dfs'], {'name': '"""dataset_concat"""'}), "(dfs, name='dataset_concat')\n", (50885, 50913), True, 'import vaex as vx\n'), ((50934, 50992), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['dfs'], {'name': '"""dataset_concat"""'}), "(dfs, name='dataset_concat')\n", (50964, 50992), True, 'import vaex as vx\n'), ((51284, 51317), 'numpy.arange', 'np.arange', (['(1000)'], {'dtype': 'np.float32'}), '(1000, dtype=np.float32)\n', (51293, 51317), True, 'import numpy as np\n'), ((51325, 51357), 'numpy.arange', 'np.arange', (['(100)'], {'dtype': 'np.float32'}), '(100, dtype=np.float32)\n', (51334, 51357), True, 'import numpy as np\n'), ((51376, 51400), 'numpy.concatenate', 'np.concatenate', (['(x1, x2)'], {}), '((x1, x2))\n', (51390, 51400), True, 'import numpy as np\n'), ((51415, 51451), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""dataset1"""'], {}), "('dataset1')\n", (51439, 51451), True, 'import vaex as vx\n'), ((51465, 51501), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""dataset2"""'], {}), "('dataset2')\n", (51489, 51501), True, 'import vaex as vx\n'), ((51589, 51664), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['[dataset1, dataset2]'], {'name': '"""dataset_concat"""'}), "([dataset1, dataset2], name='dataset_concat')\n", (51619, 51664), True, 'import vaex as vx\n'), ((51680, 51704), 'tempfile.mktemp', 'tempfile.mktemp', (['""".hdf5"""'], {}), "('.hdf5')\n", (51695, 51704), False, 'import tempfile\n'), ((51852, 51876), 'tempfile.mktemp', 'tempfile.mktemp', (['""".hdf5"""'], {}), "('.hdf5')\n", (51867, 51876), False, 'import tempfile\n'), ((51956, 52067), 'numpy.testing.assert_array_equal', 'np.testing.assert_array_equal', (['self.dataset.data.x[self.zero_index:self.zero_index + 10]', 'ds2.data.x[::-1]'], {}), '(self.dataset.data.x[self.zero_index:self.\n zero_index + 10], ds2.data.x[::-1])\n', (51985, 52067), True, 'import numpy as np\n'), ((52170, 52195), 'tempfile.mktemp', 'tempfile.mktemp', (['""".arrow"""'], {}), "('.arrow')\n", (52185, 52195), False, 'import tempfile\n'), ((52455, 52479), 'tempfile.mktemp', 'tempfile.mktemp', (['""".hdf5"""'], {}), "('.hdf5')\n", (52470, 52479), False, 'import tempfile\n'), ((52494, 52518), 'tempfile.mktemp', 'tempfile.mktemp', (['""".fits"""'], {}), "('.fits')\n", (52509, 52518), False, 'import tempfile\n'), ((52541, 52565), 'tempfile.mktemp', 'tempfile.mktemp', (['""".fits"""'], {}), "('.fits')\n", (52556, 52565), False, 'import tempfile\n'), ((58281, 58306), 'tempfile.mktemp', 'tempfile.mktemp', (['""".arrow"""'], {}), "('.arrow')\n", (58296, 58306), False, 'import tempfile\n'), ((58321, 58345), 'tempfile.mktemp', 'tempfile.mktemp', (['""".hdf5"""'], {}), "('.hdf5')\n", (58336, 58345), False, 'import tempfile\n'), ((58373, 58399), 'vaex.dataset.export', 'dataset.export', (['path_arrow'], {}), '(path_arrow)\n', (58387, 58399), True, 'import vaex.dataset as dataset\n'), ((64969, 65021), 'vaex.server', 'vx.server', (["('%s://localhost:%d' % (scheme, test_port))"], {}), "('%s://localhost:%d' % (scheme, test_port))\n", (64978, 65021), True, 'import vaex as vx\n'), ((68197, 68253), 'vaex.server', 'vx.server', (["('%s://localhost:%d' % (scheme, test_port - 2))"], {}), "('%s://localhost:%d' % (scheme, test_port - 2))\n", (68206, 68253), True, 'import vaex as vx\n'), ((68269, 68325), 'vaex.server', 'vx.server', (["('%s://localhost:%d' % (scheme, test_port - 1))"], {}), "('%s://localhost:%d' % (scheme, test_port - 1))\n", (68278, 68325), True, 'import vaex as vx\n'), ((69467, 69489), 'numpy.array', 'np.array', (['[0, 1, 0, 1]'], {}), '([0, 1, 0, 1])\n', (69475, 69489), True, 'import numpy as np\n'), ((69496, 69518), 'numpy.array', 'np.array', (['[0, 0, 1, 1]'], {}), '([0, 0, 1, 1])\n', (69504, 69518), True, 'import numpy as np\n'), ((69531, 69555), 'vaex.from_arrays', 'vx.from_arrays', ([], {'x': 'x', 'y': 'y'}), '(x=x, y=y)\n', (69545, 69555), True, 'import vaex as vx\n'), ((69632, 69651), 'numpy.all', 'np.all', (['(counts == 1)'], {}), '(counts == 1)\n', (69638, 69651), True, 'import numpy as np\n'), ((69660, 69694), 'numpy.array', 'np.array', (['[0, 1, 0, 1, 0, 1, 0, 1]'], {}), '([0, 1, 0, 1, 0, 1, 0, 1])\n', (69668, 69694), True, 'import numpy as np\n'), ((69701, 69735), 'numpy.array', 'np.array', (['[0, 0, 1, 1, 0, 0, 1, 1]'], {}), '([0, 0, 1, 1, 0, 0, 1, 1])\n', (69709, 69735), True, 'import numpy as np\n'), ((69742, 69776), 'numpy.array', 'np.array', (['[0, 0, 0, 0, 1, 1, 1, 1]'], {}), '([0, 0, 0, 0, 1, 1, 1, 1])\n', (69750, 69776), True, 'import numpy as np\n'), ((69789, 69818), 'vaex.from_arrays', 'vx.from_arrays', ([], {'x': 'x', 'y': 'y', 'z': 'z'}), '(x=x, y=y, z=z)\n', (69803, 69818), True, 'import vaex as vx\n'), ((69909, 69928), 'numpy.all', 'np.all', (['(counts == 1)'], {}), '(counts == 1)\n', (69915, 69928), True, 'import numpy as np\n'), ((69937, 69995), 'numpy.array', 'np.array', (['[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]'], {}), '([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1])\n', (69945, 69995), True, 'import numpy as np\n'), ((70002, 70060), 'numpy.array', 'np.array', (['[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]'], {}), '([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1])\n', (70010, 70060), True, 'import numpy as np\n'), ((70067, 70125), 'numpy.array', 'np.array', (['[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]'], {}), '([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1])\n', (70075, 70125), True, 'import numpy as np\n'), ((70132, 70190), 'numpy.array', 'np.array', (['[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]'], {}), '([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1])\n', (70140, 70190), True, 'import numpy as np\n'), ((70204, 70238), 'vaex.from_arrays', 'vx.from_arrays', ([], {'x': 'x', 'y': 'y', 'z': 'z', 'w': 'w'}), '(x=x, y=y, z=z, w=w)\n', (70218, 70238), True, 'import vaex as vx\n'), ((70343, 70362), 'numpy.all', 'np.all', (['(counts == 1)'], {}), '(counts == 1)\n', (70349, 70362), True, 'import numpy as np\n'), ((2093, 2122), 'numpy.arange', 'np.arange', (['(-2)', '(19)'], {'dtype': '"""i8"""'}), "(-2, 19, dtype='i8')\n", (2102, 2122), True, 'import numpy as np\n'), ((2623, 2648), 'numpy.arange', 'np.arange', (['(10)'], {'dtype': '"""i8"""'}), "(10, dtype='i8')\n", (2632, 2648), True, 'import numpy as np\n'), ((3670, 3684), 'numpy.array', 'np.array', (['name'], {}), '(name)\n', (3678, 3684), True, 'import numpy as np\n'), ((7766, 7826), 'os.path.join', 'os.path.join', (['basedir', '"""files"""', '"""default_amuse_plummer.hdf5"""'], {}), "(basedir, 'files', 'default_amuse_plummer.hdf5')\n", (7778, 7826), False, 'import os\n'), ((8636, 8696), 'os.path.join', 'os.path.join', (['basedir', '"""files"""', '"""gaia-small-fits-basic.fits"""'], {}), "(basedir, 'files', 'gaia-small-fits-basic.fits')\n", (8648, 8696), False, 'import os\n'), ((8723, 8782), 'os.path.join', 'os.path.join', (['basedir', '"""files"""', '"""gaia-small-fits-plus.fits"""'], {}), "(basedir, 'files', 'gaia-small-fits-plus.fits')\n", (8735, 8782), False, 'import os\n'), ((8807, 8870), 'os.path.join', 'os.path.join', (['basedir', '"""files"""', '"""gaia-small-colfits-basic.fits"""'], {}), "(basedir, 'files', 'gaia-small-colfits-basic.fits')\n", (8819, 8870), False, 'import os\n'), ((8900, 8962), 'os.path.join', 'os.path.join', (['basedir', '"""files"""', '"""gaia-small-colfits-plus.fits"""'], {}), "(basedir, 'files', 'gaia-small-colfits-plus.fits')\n", (8912, 8962), False, 'import os\n'), ((8983, 9039), 'os.path.join', 'os.path.join', (['basedir', '"""files"""', '"""gaia-small-votable.vot"""'], {}), "(basedir, 'files', 'gaia-small-votable.vot')\n", (8995, 9039), False, 'import os\n'), ((9187, 9211), 'tempfile.mktemp', 'tempfile.mktemp', (['""".hdf5"""'], {}), "('.hdf5')\n", (9202, 9211), False, 'import tempfile\n'), ((9251, 9269), 'vaex.open', 'vx.open', (['path_hdf5'], {}), '(path_hdf5)\n', (9258, 9269), True, 'import vaex as vx\n'), ((9601, 9625), 'tempfile.mktemp', 'tempfile.mktemp', (['""".fits"""'], {}), "('.fits')\n", (9616, 9625), False, 'import tempfile\n'), ((9665, 9683), 'vaex.open', 'vx.open', (['path_fits'], {}), '(path_fits)\n', (9672, 9683), True, 'import vaex as vx\n'), ((16191, 16204), 'numpy.sum', 'np.sum', (['(x * y)'], {}), '(x * y)\n', (16197, 16204), True, 'import numpy as np\n'), ((16238, 16251), 'numpy.sum', 'np.sum', (['(x * y)'], {}), '(x * y)\n', (16244, 16251), True, 'import numpy as np\n'), ((16285, 16307), 'numpy.sum', 'np.sum', (['(x[indices] * z)'], {}), '(x[indices] * z)\n', (16291, 16307), True, 'import numpy as np\n'), ((16341, 16363), 'numpy.sum', 'np.sum', (['(x[indices] * z)'], {}), '(x[indices] * z)\n', (16347, 16363), True, 'import numpy as np\n'), ((16580, 16593), 'numpy.sum', 'np.sum', (['(x * y)'], {}), '(x * y)\n', (16586, 16593), True, 'import numpy as np\n'), ((16627, 16666), 'numpy.sum', 'np.sum', (['(x[indices][:4] * y[indices][:4])'], {}), '(x[indices][:4] * y[indices][:4])\n', (16633, 16666), True, 'import numpy as np\n'), ((16897, 16918), 'numpy.sum', 'np.sum', (['(x[:4] * y[:4])'], {}), '(x[:4] * y[:4])\n', (16903, 16918), True, 'import numpy as np\n'), ((16952, 16973), 'numpy.sum', 'np.sum', (['(x[:4] * y[:4])'], {}), '(x[:4] * y[:4])\n', (16958, 16973), True, 'import numpy as np\n'), ((17476, 17526), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['counts', 'ones'], {}), '(counts, ones)\n', (17512, 17526), True, 'import numpy as np\n'), ((20565, 20577), 'numpy.sqrt', 'np.sqrt', (['(2.0)'], {}), '(2.0)\n', (20572, 20577), True, 'import numpy as np\n'), ((21429, 21441), 'numpy.sqrt', 'np.sqrt', (['(2.0)'], {}), '(2.0)\n', (21436, 21441), True, 'import numpy as np\n'), ((23096, 23151), 'vaex.from_arrays', 'vx.from_arrays', ([], {'pm_l': 'pm_l', 'pm_b': 'pm_b', 'distance': 'distance'}), '(pm_l=pm_l, pm_b=pm_b, distance=distance)\n', (23110, 23151), True, 'import vaex as vx\n'), ((27657, 27681), 'tempfile.mktemp', 'tempfile.mktemp', (['""".hdf5"""'], {}), "('.hdf5')\n", (27672, 27681), False, 'import tempfile\n'), ((27758, 27776), 'vaex.open', 'vx.open', (['path_hdf5'], {}), '(path_hdf5)\n', (27765, 27776), True, 'import vaex as vx\n'), ((27870, 27895), 'tempfile.mktemp', 'tempfile.mktemp', (['""".arrow"""'], {}), "('.arrow')\n", (27885, 27895), False, 'import tempfile\n'), ((27974, 27993), 'vaex.open', 'vx.open', (['path_arrow'], {}), '(path_arrow)\n', (27981, 27993), True, 'import vaex as vx\n'), ((28342, 28366), 'tempfile.mktemp', 'tempfile.mktemp', (['""".fits"""'], {}), "('.fits')\n", (28357, 28366), False, 'import tempfile\n'), ((28443, 28461), 'vaex.open', 'vx.open', (['path_fits'], {}), '(path_fits)\n', (28450, 28461), True, 'import vaex as vx\n'), ((28562, 28586), 'tempfile.mktemp', 'tempfile.mktemp', (['""".fits"""'], {}), "('.fits')\n", (28577, 28586), False, 'import tempfile\n'), ((29245, 29260), 'numpy.dtype', 'np.dtype', (['""">f8"""'], {}), "('>f8')\n", (29253, 29260), True, 'import numpy as np\n'), ((31279, 31326), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['z', 'z_test'], {}), '(z, z_test)\n', (31315, 31326), True, 'import numpy as np\n'), ((38721, 38741), 'numpy.cov', 'np.cov', (['args'], {'bias': '(1)'}), '(args, bias=1)\n', (38727, 38741), True, 'import numpy as np\n'), ((42785, 42803), 'numpy.isfinite', 'np.isfinite', (['(x * y)'], {}), '(x * y)\n', (42796, 42803), True, 'import numpy as np\n'), ((45990, 46002), 'numpy.median', 'np.median', (['x'], {}), '(x)\n', (45999, 46002), True, 'import numpy as np\n'), ((46567, 46584), 'numpy.median', 'np.median', (['self.x'], {}), '(self.x)\n', (46576, 46584), True, 'import numpy as np\n'), ((46750, 46767), 'numpy.median', 'np.median', (['self.x'], {}), '(self.x)\n', (46759, 46767), True, 'import numpy as np\n'), ((47065, 47086), 'numpy.median', 'np.median', (['self.x[:5]'], {}), '(self.x[:5])\n', (47074, 47086), True, 'import numpy as np\n'), ((47306, 47318), 'numpy.nansum', 'np.nansum', (['x'], {}), '(x)\n', (47315, 47318), True, 'import numpy as np\n'), ((47398, 47414), 'numpy.nansum', 'np.nansum', (['x[:5]'], {}), '(x[:5])\n', (47407, 47414), True, 'import numpy as np\n'), ((47552, 47568), 'numpy.nansum', 'np.nansum', (['x[:5]'], {}), '(x[:5])\n', (47561, 47568), True, 'import numpy as np\n'), ((49763, 49821), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['dfs'], {'name': '"""dataset_concat"""'}), "(dfs, name='dataset_concat')\n", (49793, 49821), True, 'import vaex as vx\n'), ((50277, 50313), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""dataset1"""'], {}), "('dataset1')\n", (50301, 50313), True, 'import vaex as vx\n'), ((50378, 50408), 'vaex.dataset.add_column', 'dataset.add_column', (['"""x"""', 'array'], {}), "('x', array)\n", (50396, 50408), True, 'import vaex.dataset as dataset\n'), ((50467, 50525), 'vaex.dataset.DatasetConcatenated', 'vx.dataset.DatasetConcatenated', (['dfs'], {'name': '"""dataset_concat"""'}), "(dfs, name='dataset_concat')\n", (50497, 50525), True, 'import vaex as vx\n'), ((50623, 50659), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (['"""dataset1"""'], {}), "('dataset1')\n", (50647, 50659), True, 'import vaex as vx\n'), ((50724, 50754), 'vaex.dataset.add_column', 'dataset.add_column', (['"""x"""', 'array'], {}), "('x', array)\n", (50742, 50754), True, 'import vaex.dataset as dataset\n'), ((1352, 1365), 'numpy.array', 'np.array', (['[v]'], {}), '([v])\n', (1360, 1365), True, 'import numpy as np\n'), ((7592, 7623), 'numpy.array', 'np.array', (['[2, 2, 1, 0, 1, 1, 2]'], {}), '([2, 2, 1, 0, 1, 1, 2])\n', (7600, 7623), True, 'import numpy as np\n'), ((14404, 14426), 'tempfile.mktemp', 'tempfile.mktemp', (['"""asc"""'], {}), "('asc')\n", (14419, 14426), False, 'import tempfile\n'), ((14902, 14957), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['ds.data.x', 'self.x'], {}), '(ds.data.x, self.x)\n', (14938, 14957), True, 'import numpy as np\n'), ((14962, 15017), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['ds.data.y', 'self.y'], {}), '(ds.data.y, self.y)\n', (14998, 15017), True, 'import numpy as np\n'), ((17440, 17464), 'numpy.ones', 'np.ones', (['(npix // scaling)'], {}), '(npix // scaling)\n', (17447, 17464), True, 'import numpy as np\n'), ((17890, 17905), 'numpy.array', 'np.array', (['[1.0]'], {}), '([1.0])\n', (17898, 17905), True, 'import numpy as np\n'), ((17927, 17942), 'numpy.array', 'np.array', (['[0.1]'], {}), '([0.1])\n', (17935, 17942), True, 'import numpy as np\n'), ((18604, 18632), 'numpy.random.normal', 'np.random.normal', (['x', '(0.01)', 'N'], {}), '(x, 0.01, N)\n', (18620, 18632), True, 'import numpy as np\n'), ((18648, 18676), 'numpy.random.normal', 'np.random.normal', (['y', '(0.02)', 'N'], {}), '(y, 0.02, N)\n', (18664, 18676), True, 'import numpy as np\n'), ((18695, 18726), 'numpy.random.normal', 'np.random.normal', (['velx', '(0.03)', 'N'], {}), '(velx, 0.03, N)\n', (18711, 18726), True, 'import numpy as np\n'), ((18745, 18776), 'numpy.random.normal', 'np.random.normal', (['vely', '(0.04)', 'N'], {}), '(vely, 0.04, N)\n', (18761, 18776), True, 'import numpy as np\n'), ((18791, 18833), 'vaex.from_arrays', 'vx.from_arrays', ([], {'x': 'x', 'y': 'y', 'vx': 'vely', 'vy': 'vely'}), '(x=x, y=y, vx=vely, vy=vely)\n', (18805, 18833), True, 'import vaex as vx\n'), ((21874, 21902), 'numpy.random.normal', 'np.random.normal', (['x', '(0.01)', 'N'], {}), '(x, 0.01, N)\n', (21890, 21902), True, 'import numpy as np\n'), ((21918, 21946), 'numpy.random.normal', 'np.random.normal', (['y', '(0.02)', 'N'], {}), '(y, 0.02, N)\n', (21934, 21946), True, 'import numpy as np\n'), ((21961, 21985), 'vaex.from_arrays', 'vx.from_arrays', ([], {'x': 'x', 'y': 'y'}), '(x=x, y=y)\n', (21975, 21985), True, 'import vaex as vx\n'), ((22943, 22970), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.1)', 'N'], {}), '(0, 0.1, N)\n', (22959, 22970), True, 'import numpy as np\n'), ((22997, 23024), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.3)', 'N'], {}), '(0, 0.3, N)\n', (23013, 23024), True, 'import numpy as np\n'), ((23047, 23074), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.4)', 'N'], {}), '(0, 0.4, N)\n', (23063, 23074), True, 'import numpy as np\n'), ((24371, 24443), 'vaex.from_arrays', 'vx.from_arrays', ([], {'l': 'l', 'b': 'b', 'pm_l': 'pm_l', 'pm_b': 'pm_b', 'vr': 'vr', 'distance': 'distance'}), '(l=l, b=b, pm_l=pm_l, pm_b=pm_b, vr=vr, distance=distance)\n', (24385, 24443), True, 'import vaex as vx\n'), ((42877, 42899), 'numpy.cov', 'np.cov', (['[x, y]'], {'bias': '(1)'}), '([x, y], bias=1)\n', (42883, 42899), True, 'import numpy as np\n'), ((45785, 45797), 'numpy.arange', 'np.arange', (['(3)'], {}), '(3)\n', (45794, 45797), True, 'import numpy as np\n'), ((45835, 45847), 'numpy.arange', 'np.arange', (['(4)'], {}), '(4)\n', (45844, 45847), True, 'import numpy as np\n'), ((46911, 46928), 'numpy.median', 'np.median', (['self.x'], {}), '(self.x)\n', (46920, 46928), True, 'import numpy as np\n'), ((46930, 46947), 'numpy.median', 'np.median', (['self.y'], {}), '(self.y)\n', (46939, 46947), True, 'import numpy as np\n'), ((47689, 47701), 'numpy.nansum', 'np.nansum', (['x'], {}), '(x)\n', (47698, 47701), True, 'import numpy as np\n'), ((47821, 47837), 'numpy.nansum', 'np.nansum', (['x[:5]'], {}), '(x[:5])\n', (47830, 47837), True, 'import numpy as np\n'), ((47961, 47973), 'numpy.nansum', 'np.nansum', (['x'], {}), '(x)\n', (47970, 47973), True, 'import numpy as np\n'), ((48097, 48113), 'numpy.nansum', 'np.nansum', (['x[:5]'], {}), '(x[:5])\n', (48106, 48113), True, 'import numpy as np\n'), ((48234, 48250), 'numpy.nansum', 'np.nansum', (['x[:5]'], {}), '(x[:5])\n', (48243, 48250), True, 'import numpy as np\n'), ((48252, 48268), 'numpy.nansum', 'np.nansum', (['x[5:]'], {}), '(x[5:])\n', (48261, 48268), True, 'import numpy as np\n'), ((48388, 48404), 'numpy.nansum', 'np.nansum', (['x[:5]'], {}), '(x[:5])\n', (48397, 48404), True, 'import numpy as np\n'), ((48540, 48556), 'numpy.nansum', 'np.nansum', (['x[:i]'], {}), '(x[:i])\n', (48549, 48556), True, 'import numpy as np\n'), ((48558, 48574), 'numpy.nansum', 'np.nansum', (['x[i:]'], {}), '(x[i:])\n', (48567, 48574), True, 'import numpy as np\n'), ((48698, 48714), 'numpy.nansum', 'np.nansum', (['x[:5]'], {}), '(x[:5])\n', (48707, 48714), True, 'import numpy as np\n'), ((48846, 48862), 'numpy.nansum', 'np.nansum', (['y[:i]'], {}), '(y[:i])\n', (48855, 48862), True, 'import numpy as np\n'), ((48864, 48880), 'numpy.nansum', 'np.nansum', (['y[i:]'], {}), '(y[i:])\n', (48873, 48880), True, 'import numpy as np\n'), ((49000, 49016), 'numpy.nansum', 'np.nansum', (['y[:5]'], {}), '(y[:5])\n', (49009, 49016), True, 'import numpy as np\n'), ((49448, 49490), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['a', 'b'], {}), '(a, b)\n', (49484, 49490), True, 'import numpy as np\n'), ((49527, 49552), 'numpy.arange', 'np.arange', (['(3)'], {'dtype': 'dtype'}), '(3, dtype=dtype)\n', (49536, 49552), True, 'import numpy as np\n'), ((49602, 49644), 'vaex.dataset.DatasetArrays', 'vx.dataset.DatasetArrays', (["('dataset-%i' % i)"], {}), "('dataset-%i' % i)\n", (49626, 49644), True, 'import vaex as vx\n'), ((49712, 49742), 'vaex.dataset.add_column', 'dataset.add_column', (['"""x"""', 'array'], {}), "('x', array)\n", (49730, 49742), True, 'import vaex.dataset as dataset\n'), ((52381, 52401), 'numpy.array', 'np.array', (['ds2.data.x'], {}), '(ds2.data.x)\n', (52389, 52401), True, 'import numpy as np\n'), ((52801, 52838), 'vaex.dataset.set_active_fraction', 'dataset.set_active_fraction', (['fraction'], {}), '(fraction)\n', (52828, 52838), True, 'import vaex.dataset as dataset\n'), ((52843, 52866), 'vaex.dataset.select', 'dataset.select', (['"""x > 3"""'], {}), "('x > 3')\n", (52857, 52866), True, 'import vaex.dataset as dataset\n'), ((52871, 52908), 'vaex.dataset.select', 'dataset.select', (['"""x > 2"""'], {'name': '"""named"""'}), "('x > 2', name='named')\n", (52885, 52908), True, 'import vaex.dataset as dataset\n'), ((58235, 58265), 'vaex.dataset.set_active_fraction', 'dataset.set_active_fraction', (['(1)'], {}), '(1)\n', (58262, 58265), True, 'import vaex.dataset as dataset\n'), ((60539, 60571), 'vaex.dataset.set_active_fraction', 'dataset.set_active_fraction', (['(1.0)'], {}), '(1.0)\n', (60566, 60571), True, 'import vaex.dataset as dataset\n'), ((60627, 60660), 'vaex.dataset.set_active_fraction', 'dataset.set_active_fraction', (['(0.25)'], {}), '(0.25)\n', (60654, 60660), True, 'import vaex.dataset as dataset\n'), ((60754, 60796), 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['a', 'b'], {}), '(a, b)\n', (60790, 60796), True, 'import numpy as np\n'), ((61878, 61904), 'vaex.dataset.set_current_row', 'dataset.set_current_row', (['i'], {}), '(i)\n', (61901, 61904), True, 'import vaex.dataset as dataset\n'), ((69567, 69584), 'vaex.dataset', 'dataset', (['"""x"""', '"""y"""'], {}), "('x', 'y')\n", (69574, 69584), True, 'import vaex.dataset as dataset\n'), ((69830, 69852), 'vaex.dataset', 'dataset', (['"""x"""', '"""y"""', '"""z"""'], {}), "('x', 'y', 'z')\n", (69837, 69852), True, 'import vaex.dataset as dataset\n'), ((70250, 70277), 'vaex.dataset', 'dataset', (['"""x"""', '"""y"""', '"""z"""', '"""w"""'], {}), "('x', 'y', 'z', 'w')\n", (70257, 70277), True, 'import vaex.dataset as dataset\n'), ((14784, 14816), 'vaex.from_ascii', 'vx.from_ascii', (['fn'], {'seperator': 'sep'}), '(fn, seperator=sep)\n', (14797, 14816), True, 'import vaex as vx\n'), ((24166, 24193), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.1)', 'N'], {}), '(0, 0.1, N)\n', (24182, 24193), True, 'import numpy as np\n'), ((24221, 24248), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.2)', 'N'], {}), '(0, 0.2, N)\n', (24237, 24248), True, 'import numpy as np\n'), ((24270, 24297), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.3)', 'N'], {}), '(0, 0.3, N)\n', (24286, 24297), True, 'import numpy as np\n'), ((24321, 24348), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.4)', 'N'], {}), '(0, 0.4, N)\n', (24337, 24348), True, 'import numpy as np\n'), ((28777, 28805), 'os.remove', 'os.remove', (['path_fits_astropy'], {}), '(path_fits_astropy)\n', (28786, 28805), False, 'import os\n'), ((60825, 60850), 'vaex.dataset.length_original', 'dataset.length_original', ([], {}), '()\n', (60848, 60850), True, 'import vaex.dataset as dataset\n'), ((7688, 7704), 'numpy.sort', 'np.sort', (['classes'], {}), '(classes)\n', (7695, 7704), True, 'import numpy as np\n'), ((24064, 24091), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.1)', 'N'], {}), '(0, 0.1, N)\n', (24080, 24091), True, 'import numpy as np\n'), ((24115, 24142), 'numpy.random.normal', 'np.random.normal', (['(0)', '(0.1)', 'N'], {}), '(0, 0.1, N)\n', (24131, 24142), True, 'import numpy as np\n'), ((61918, 61938), 'vaex.dataset', 'dataset', (['"""x"""', '"""x**2"""'], {}), "('x', 'x**2')\n", (61925, 61938), True, 'import vaex.dataset as dataset\n'), ((1993, 2023), 'numpy.arange', 'np.arange', (['(-2)', '(40)'], {'dtype': '""">f8"""'}), "(-2, 40, dtype='>f8')\n", (2002, 2023), True, 'import numpy as np\n'), ((2373, 2403), 'numpy.arange', 'np.arange', (['(-2)', '(40)'], {'dtype': '""">f8"""'}), "(-2, 40, dtype='>f8')\n", (2382, 2403), True, 'import numpy as np\n'), ((2527, 2553), 'numpy.arange', 'np.arange', (['(20)'], {'dtype': '""">f8"""'}), "(20, dtype='>f8')\n", (2536, 2553), True, 'import numpy as np\n'), ((54174, 54199), 'tempfile.mktemp', 'tempfile.mktemp', (['""".arrow"""'], {}), "('.arrow')\n", (54189, 54199), False, 'import tempfile\n'), ((54225, 54252), 'tempfile.mktemp', 'tempfile.mktemp', (['""".parquet"""'], {}), "('.parquet')\n", (54240, 54252), False, 'import tempfile\n'), ((55507, 55536), 'vaex.open', 'vx.open', (['path'], {'as_numpy': '(False)'}), '(path, as_numpy=False)\n', (55514, 55536), True, 'import vaex as vx\n'), ((55574, 55587), 'vaex.open', 'vx.open', (['path'], {}), '(path)\n', (55581, 55587), True, 'import vaex as vx\n'), ((56608, 56653), 'vaex.dataset.evaluate', 'dataset.evaluate', (['column_name'], {'filtered': '(False)'}), '(column_name, filtered=False)\n', (56624, 56653), True, 'import vaex.dataset as dataset\n'), ((56700, 56749), 'vaex.dataset.evaluate', 'dataset.evaluate', (['column_name'], {'array_type': '"""numpy"""'}), "(column_name, array_type='numpy')\n", (56716, 56749), True, 'import vaex.dataset as dataset\n'), ((56769, 56811), 'vaex.dataset.evaluate_selection_mask', 'dataset.evaluate_selection_mask', (['selection'], {}), '(selection)\n', (56800, 56811), True, 'import vaex.dataset as dataset\n'), ((57348, 57397), 'vaex.dataset.evaluate', 'dataset.evaluate', (['column_name'], {'array_type': '"""numpy"""'}), "(column_name, array_type='numpy')\n", (57364, 57397), True, 'import vaex.dataset as dataset\n'), ((56244, 56256), 'numpy.isnan', 'np.isnan', (['ar'], {}), '(ar)\n', (56252, 56256), True, 'import numpy as np\n'), ((56273, 56288), 'numpy.any', 'np.any', (['nanmask'], {}), '(nanmask)\n', (56279, 56288), True, 'import numpy as np\n'), ((56913, 56928), 'pdb.set_trace', 'pdb.set_trace', ([], {}), '()\n', (56926, 56928), False, 'import pdb\n'), ((57243, 57258), 'pdb.set_trace', 'pdb.set_trace', ([], {}), '()\n', (57256, 57258), False, 'import pdb\n'), ((56309, 56338), 'numpy.ma.array', 'np.ma.array', (['ar'], {'mask': 'nanmask'}), '(ar, mask=nanmask)\n', (56320, 56338), True, 'import numpy as np\n'), ((57734, 57772), 'numpy.array', 'np.array', (['compare.columns[column_name]'], {}), '(compare.columns[column_name])\n', (57742, 57772), True, 'import numpy as np\n'), ((55392, 55420), 'os.remove', 'os.remove', (['path_fits_astropy'], {}), '(path_fits_astropy)\n', (55401, 55420), False, 'import os\n')]
import numpy as np import pytest import torch from torch.utils.data import TensorDataset from doppelganger import (ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data) @pytest.fixture def dg_model() -> DGTorch: attribute_outputs = [ ContinuousOutput( name="a", normalization=Normalization.ZERO_ONE, global_min=0, global_max=1, is_feature_normalized=False, is_example_normalized=False, ), DiscreteOutput(name="b", dim=3), ] feature_outputs = [ DiscreteOutput(name="c", dim=4), ContinuousOutput( name="d", normalization=Normalization.ZERO_ONE, global_min=0, global_max=1, is_feature_normalized=False, is_example_normalized=False, ), ] dg = DGTorch(attribute_outputs, [], feature_outputs, 20, 5) return dg @pytest.fixture def attribute_data(): n = 100 attributes = np.concatenate( ( np.random.rand(n, 1), np.random.randint(0, 3, size=(n, 1)), ), axis=1, ) return (attributes, [OutputType.CONTINUOUS, OutputType.DISCRETE]) @pytest.fixture def feature_data(): n = 100 features = np.concatenate( ( np.random.randint(0, 4, size=(n, 20, 1)), np.random.rand(n, 20, 1), ), axis=2, ) return (features, [OutputType.DISCRETE, OutputType.CONTINUOUS]) def test_generate(dg_model: DGTorch): attributes, features = dg_model.generate(8) assert attributes.shape == (8, 2) assert features.shape == (8, 20, 2) def test_train(attribute_data, feature_data): attributes, attribute_types = attribute_data features, feature_types = feature_data dg_data = prepare_data( attributes, attribute_types, features, feature_types, is_feature_normalized=False, is_example_normalized=False, ) dg = DGTorch( attribute_outputs=dg_data.attribute_outputs, additional_attribute_outputs=None, feature_outputs=dg_data.feature_outputs, max_sequence_len=20, sample_len=5, ) dataset = torch.utils.data.TensorDataset( torch.Tensor(dg_data.attributes), torch.Tensor(dg_data.features), ) dg.train(dataset, batch_size=10, num_epochs=2) def test_train_with_attribute_discriminator(attribute_data, feature_data): attributes, attribute_types = attribute_data features, feature_types = feature_data dg_data = prepare_data( attributes, attribute_types, features, feature_types, is_feature_normalized=False, is_example_normalized=False, ) dg = DGTorch( attribute_outputs=dg_data.attribute_outputs, additional_attribute_outputs=None, feature_outputs=dg_data.feature_outputs, max_sequence_len=20, sample_len=5, use_attribute_discriminator=True, ) dataset = torch.utils.data.TensorDataset( torch.Tensor(dg_data.attributes), torch.Tensor(dg_data.features), ) dg.train(dataset, batch_size=10, num_epochs=2) def test_train_with_additional_attributes(attribute_data, feature_data): attributes, attribute_types = attribute_data features, feature_types = feature_data dg_data = prepare_data( attributes, attribute_types, features, feature_types, is_feature_normalized=False, is_example_normalized=True, ) dg = DGTorch( attribute_outputs=dg_data.attribute_outputs, additional_attribute_outputs=dg_data.additional_attribute_outputs, feature_outputs=dg_data.feature_outputs, max_sequence_len=20, sample_len=5, ) dataset = torch.utils.data.TensorDataset( torch.Tensor(dg_data.attributes), torch.Tensor(dg_data.additional_attributes), torch.Tensor(dg_data.features), ) dg.train(dataset, batch_size=10, num_epochs=2) def test_train_with_additional_attributes_and_discriminator( attribute_data, feature_data ): attributes, attribute_types = attribute_data features, feature_types = feature_data dg_data = prepare_data( attributes, attribute_types, features, feature_types, is_feature_normalized=False, is_example_normalized=True, ) dg = DGTorch( attribute_outputs=dg_data.attribute_outputs, additional_attribute_outputs=dg_data.additional_attribute_outputs, feature_outputs=dg_data.feature_outputs, max_sequence_len=20, sample_len=5, use_attribute_discriminator=True, ) dataset = torch.utils.data.TensorDataset( torch.Tensor(dg_data.attributes), torch.Tensor(dg_data.additional_attributes), torch.Tensor(dg_data.features), ) dg.train(dataset, batch_size=10, num_epochs=2) def test_output(): o1 = Output(name="foo") assert o1.name == "foo" assert o1.get_dim() == 1 o2 = DiscreteOutput(name="foo", dim=4) assert o2.name == "foo" assert o2.dim == 4 assert o2.get_dim() == 4 o3 = ContinuousOutput( name="foo", normalization=Normalization.ZERO_ONE, global_min=0.0, global_max=1.0, is_feature_normalized=False, is_example_normalized=False, ) assert o3.get_dim() == 1 with pytest.raises(TypeError): DiscreteOutput(name="bad") with pytest.raises(TypeError): ContinuousOutput( name="bad", normalization=Normalization.ZERO_ONE, global_min=0, global_max=1, dim=5, is_feature_normalized=False, is_example_normalized=False, ) def test_prepare_data(): original_attributes = np.concatenate( ( np.random.rand(100, 1), np.random.randint(0, 3, size=(100, 1)), ), axis=1, ) original_features = np.concatenate( ( np.random.rand(100, 20, 1), np.random.randint(0, 2, size=(100, 20, 1)), ), axis=2, ) attribute_types = [OutputType.CONTINUOUS, OutputType.DISCRETE] feature_types = [OutputType.CONTINUOUS, OutputType.DISCRETE] dg_data = prepare_data( original_attributes, attribute_types, original_features, feature_types ) assert dg_data.attributes.shape == (100, 4) assert dg_data.additional_attributes.shape == (100, 2) assert dg_data.features.shape == (100, 20, 3)
[ "doppelganger.DGTorch", "doppelganger.prepare_data", "doppelganger.ContinuousOutput", "pytest.raises", "doppelganger.DiscreteOutput", "torch.Tensor", "numpy.random.randint", "numpy.random.rand", "doppelganger.Output" ]
[((918, 972), 'doppelganger.DGTorch', 'DGTorch', (['attribute_outputs', '[]', 'feature_outputs', '(20)', '(5)'], {}), '(attribute_outputs, [], feature_outputs, 20, 5)\n', (925, 972), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((1875, 2003), 'doppelganger.prepare_data', 'prepare_data', (['attributes', 'attribute_types', 'features', 'feature_types'], {'is_feature_normalized': '(False)', 'is_example_normalized': '(False)'}), '(attributes, attribute_types, features, feature_types,\n is_feature_normalized=False, is_example_normalized=False)\n', (1887, 2003), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((2065, 2237), 'doppelganger.DGTorch', 'DGTorch', ([], {'attribute_outputs': 'dg_data.attribute_outputs', 'additional_attribute_outputs': 'None', 'feature_outputs': 'dg_data.feature_outputs', 'max_sequence_len': '(20)', 'sample_len': '(5)'}), '(attribute_outputs=dg_data.attribute_outputs,\n additional_attribute_outputs=None, feature_outputs=dg_data.\n feature_outputs, max_sequence_len=20, sample_len=5)\n', (2072, 2237), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((2646, 2774), 'doppelganger.prepare_data', 'prepare_data', (['attributes', 'attribute_types', 'features', 'feature_types'], {'is_feature_normalized': '(False)', 'is_example_normalized': '(False)'}), '(attributes, attribute_types, features, feature_types,\n is_feature_normalized=False, is_example_normalized=False)\n', (2658, 2774), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((2836, 3046), 'doppelganger.DGTorch', 'DGTorch', ([], {'attribute_outputs': 'dg_data.attribute_outputs', 'additional_attribute_outputs': 'None', 'feature_outputs': 'dg_data.feature_outputs', 'max_sequence_len': '(20)', 'sample_len': '(5)', 'use_attribute_discriminator': '(True)'}), '(attribute_outputs=dg_data.attribute_outputs,\n additional_attribute_outputs=None, feature_outputs=dg_data.\n feature_outputs, max_sequence_len=20, sample_len=5,\n use_attribute_discriminator=True)\n', (2843, 3046), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((3457, 3584), 'doppelganger.prepare_data', 'prepare_data', (['attributes', 'attribute_types', 'features', 'feature_types'], {'is_feature_normalized': '(False)', 'is_example_normalized': '(True)'}), '(attributes, attribute_types, features, feature_types,\n is_feature_normalized=False, is_example_normalized=True)\n', (3469, 3584), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((3646, 3849), 'doppelganger.DGTorch', 'DGTorch', ([], {'attribute_outputs': 'dg_data.attribute_outputs', 'additional_attribute_outputs': 'dg_data.additional_attribute_outputs', 'feature_outputs': 'dg_data.feature_outputs', 'max_sequence_len': '(20)', 'sample_len': '(5)'}), '(attribute_outputs=dg_data.attribute_outputs,\n additional_attribute_outputs=dg_data.additional_attribute_outputs,\n feature_outputs=dg_data.feature_outputs, max_sequence_len=20, sample_len=5)\n', (3653, 3849), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((4334, 4461), 'doppelganger.prepare_data', 'prepare_data', (['attributes', 'attribute_types', 'features', 'feature_types'], {'is_feature_normalized': '(False)', 'is_example_normalized': '(True)'}), '(attributes, attribute_types, features, feature_types,\n is_feature_normalized=False, is_example_normalized=True)\n', (4346, 4461), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((4523, 4764), 'doppelganger.DGTorch', 'DGTorch', ([], {'attribute_outputs': 'dg_data.attribute_outputs', 'additional_attribute_outputs': 'dg_data.additional_attribute_outputs', 'feature_outputs': 'dg_data.feature_outputs', 'max_sequence_len': '(20)', 'sample_len': '(5)', 'use_attribute_discriminator': '(True)'}), '(attribute_outputs=dg_data.attribute_outputs,\n additional_attribute_outputs=dg_data.additional_attribute_outputs,\n feature_outputs=dg_data.feature_outputs, max_sequence_len=20,\n sample_len=5, use_attribute_discriminator=True)\n', (4530, 4764), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((5077, 5095), 'doppelganger.Output', 'Output', ([], {'name': '"""foo"""'}), "(name='foo')\n", (5083, 5095), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((5163, 5196), 'doppelganger.DiscreteOutput', 'DiscreteOutput', ([], {'name': '"""foo"""', 'dim': '(4)'}), "(name='foo', dim=4)\n", (5177, 5196), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((5287, 5451), 'doppelganger.ContinuousOutput', 'ContinuousOutput', ([], {'name': '"""foo"""', 'normalization': 'Normalization.ZERO_ONE', 'global_min': '(0.0)', 'global_max': '(1.0)', 'is_feature_normalized': '(False)', 'is_example_normalized': '(False)'}), "(name='foo', normalization=Normalization.ZERO_ONE,\n global_min=0.0, global_max=1.0, is_feature_normalized=False,\n is_example_normalized=False)\n", (5303, 5451), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((6425, 6513), 'doppelganger.prepare_data', 'prepare_data', (['original_attributes', 'attribute_types', 'original_features', 'feature_types'], {}), '(original_attributes, attribute_types, original_features,\n feature_types)\n', (6437, 6513), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((313, 468), 'doppelganger.ContinuousOutput', 'ContinuousOutput', ([], {'name': '"""a"""', 'normalization': 'Normalization.ZERO_ONE', 'global_min': '(0)', 'global_max': '(1)', 'is_feature_normalized': '(False)', 'is_example_normalized': '(False)'}), "(name='a', normalization=Normalization.ZERO_ONE, global_min\n =0, global_max=1, is_feature_normalized=False, is_example_normalized=False)\n", (329, 468), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((556, 587), 'doppelganger.DiscreteOutput', 'DiscreteOutput', ([], {'name': '"""b"""', 'dim': '(3)'}), "(name='b', dim=3)\n", (570, 587), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((627, 658), 'doppelganger.DiscreteOutput', 'DiscreteOutput', ([], {'name': '"""c"""', 'dim': '(4)'}), "(name='c', dim=4)\n", (641, 658), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((668, 823), 'doppelganger.ContinuousOutput', 'ContinuousOutput', ([], {'name': '"""d"""', 'normalization': 'Normalization.ZERO_ONE', 'global_min': '(0)', 'global_max': '(1)', 'is_feature_normalized': '(False)', 'is_example_normalized': '(False)'}), "(name='d', normalization=Normalization.ZERO_ONE, global_min\n =0, global_max=1, is_feature_normalized=False, is_example_normalized=False)\n", (684, 823), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((2331, 2363), 'torch.Tensor', 'torch.Tensor', (['dg_data.attributes'], {}), '(dg_data.attributes)\n', (2343, 2363), False, 'import torch\n'), ((2373, 2403), 'torch.Tensor', 'torch.Tensor', (['dg_data.features'], {}), '(dg_data.features)\n', (2385, 2403), False, 'import torch\n'), ((3144, 3176), 'torch.Tensor', 'torch.Tensor', (['dg_data.attributes'], {}), '(dg_data.attributes)\n', (3156, 3176), False, 'import torch\n'), ((3186, 3216), 'torch.Tensor', 'torch.Tensor', (['dg_data.features'], {}), '(dg_data.features)\n', (3198, 3216), False, 'import torch\n'), ((3944, 3976), 'torch.Tensor', 'torch.Tensor', (['dg_data.attributes'], {}), '(dg_data.attributes)\n', (3956, 3976), False, 'import torch\n'), ((3986, 4029), 'torch.Tensor', 'torch.Tensor', (['dg_data.additional_attributes'], {}), '(dg_data.additional_attributes)\n', (3998, 4029), False, 'import torch\n'), ((4039, 4069), 'torch.Tensor', 'torch.Tensor', (['dg_data.features'], {}), '(dg_data.features)\n', (4051, 4069), False, 'import torch\n'), ((4863, 4895), 'torch.Tensor', 'torch.Tensor', (['dg_data.attributes'], {}), '(dg_data.attributes)\n', (4875, 4895), False, 'import torch\n'), ((4905, 4948), 'torch.Tensor', 'torch.Tensor', (['dg_data.additional_attributes'], {}), '(dg_data.additional_attributes)\n', (4917, 4948), False, 'import torch\n'), ((4958, 4988), 'torch.Tensor', 'torch.Tensor', (['dg_data.features'], {}), '(dg_data.features)\n', (4970, 4988), False, 'import torch\n'), ((5538, 5562), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (5551, 5562), False, 'import pytest\n'), ((5572, 5598), 'doppelganger.DiscreteOutput', 'DiscreteOutput', ([], {'name': '"""bad"""'}), "(name='bad')\n", (5586, 5598), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((5609, 5633), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (5622, 5633), False, 'import pytest\n'), ((5643, 5810), 'doppelganger.ContinuousOutput', 'ContinuousOutput', ([], {'name': '"""bad"""', 'normalization': 'Normalization.ZERO_ONE', 'global_min': '(0)', 'global_max': '(1)', 'dim': '(5)', 'is_feature_normalized': '(False)', 'is_example_normalized': '(False)'}), "(name='bad', normalization=Normalization.ZERO_ONE,\n global_min=0, global_max=1, dim=5, is_feature_normalized=False,\n is_example_normalized=False)\n", (5659, 5810), False, 'from doppelganger import ContinuousOutput, DGTorch, DiscreteOutput, Normalization, Output, OutputType, prepare_data\n'), ((1094, 1114), 'numpy.random.rand', 'np.random.rand', (['n', '(1)'], {}), '(n, 1)\n', (1108, 1114), True, 'import numpy as np\n'), ((1128, 1164), 'numpy.random.randint', 'np.random.randint', (['(0)', '(3)'], {'size': '(n, 1)'}), '(0, 3, size=(n, 1))\n', (1145, 1164), True, 'import numpy as np\n'), ((1372, 1412), 'numpy.random.randint', 'np.random.randint', (['(0)', '(4)'], {'size': '(n, 20, 1)'}), '(0, 4, size=(n, 20, 1))\n', (1389, 1412), True, 'import numpy as np\n'), ((1426, 1450), 'numpy.random.rand', 'np.random.rand', (['n', '(20)', '(1)'], {}), '(n, 20, 1)\n', (1440, 1450), True, 'import numpy as np\n'), ((5989, 6011), 'numpy.random.rand', 'np.random.rand', (['(100)', '(1)'], {}), '(100, 1)\n', (6003, 6011), True, 'import numpy as np\n'), ((6025, 6063), 'numpy.random.randint', 'np.random.randint', (['(0)', '(3)'], {'size': '(100, 1)'}), '(0, 3, size=(100, 1))\n', (6042, 6063), True, 'import numpy as np\n'), ((6160, 6186), 'numpy.random.rand', 'np.random.rand', (['(100)', '(20)', '(1)'], {}), '(100, 20, 1)\n', (6174, 6186), True, 'import numpy as np\n'), ((6200, 6242), 'numpy.random.randint', 'np.random.randint', (['(0)', '(2)'], {'size': '(100, 20, 1)'}), '(0, 2, size=(100, 20, 1))\n', (6217, 6242), True, 'import numpy as np\n')]
from flask import render_template, jsonify from app import app import random import io from flask import Response from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as ticker from matplotlib.ticker import (MultipleLocator, FormatStrFormatter, AutoMinorLocator) import matplotlib.dates as mdates import matplotlib.cbook as cbook import datetime as datetime import numpy as np from pandas.plotting import register_matplotlib_converters register_matplotlib_converters() import os def saveplot(filename, extension): strFile = filename + '.' + extension if os.path.isfile(strFile): os.remove(strFile) # Opt.: os.system("rm "+strFile) plt.savefig(strFile) plt.savefig(strFile) data = pd.read_csv("DIS.csv") data['Date'] = pd.to_datetime(data['Date']).dt.date #data = data.set_index('Date') data90 = data[len(data)-90:] preds_d1 = pd.read_csv("preds_d1.csv") preds_d1['Date'] = pd.to_datetime(preds_d1['Date']).dt.date #preds_dt = preds_d1.set_index('Date') preds = pd.read_csv("preds.csv") preds['Date'] = pd.to_datetime(preds['Date']).dt.date #preds = preds.set_index('Date') @app.route('/') @app.route('/index') def index(): return render_template('index.html', title='Home') @app.route('/map') def map(): return render_template('map.html', title='Map') @app.route('/map/refresh', methods=['POST']) def map_refresh(): points = [(random.uniform(48.8434100, 48.8634100), random.uniform(2.3388000, 2.3588000)) for _ in range(random.randint(2, 9))] return jsonify({'points': points}) @app.route('/contact') def contact(): return render_template('contact.html', title='Contact') @app.route('/dif1.png') def dif_png(): fig = create_dif1() output = io.BytesIO() FigureCanvas(fig).print_png(output) return Response(output.getvalue(), mimetype='image/png') def create_dif1(): d1 = plt.figure(1) fig, ax = plt.subplots() ax.plot(preds_d1['Date'], preds_d1['Adj Close'], label='close') ax.plot(preds_d1['Date'], preds_d1['reg'], label='reg') ax.plot(preds_d1['Date'], preds_d1['krr'], label='krr') ax.plot(preds_d1['Date'], preds_d1['mlp'], label='mlp') months = mdates.MonthLocator() # every month mdays = mdates.DayLocator(bymonthday=range(1, 32)) # every day months_fmt = mdates.DateFormatter('%Y-%m-%d') # format the ticks ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(months_fmt) ax.xaxis.set_minor_locator(mdays) # round to nearest years. datemin = np.datetime64(np.amin(preds_d1['Date']), 'D') datemax = np.datetime64(np.amax(preds_d1['Date']), 'D') ax.set_xlim(datemin, datemax) # format the coords message box ax.format_xdata = mdates.DateFormatter('%Y-%m-%d') ax.format_ydata = lambda x: '$%1.2f' % x # format the price. ax.grid(True) # rotates and right aligns the x labels, and moves the bottom of the # axes up to make room for them fig.autofmt_xdate() handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels) #d1.show() saveplot('dif1', 'png') return fig @app.route('/price.png') def price_png(): fig = create_price() output = io.BytesIO() FigureCanvas(fig).print_png(output) return Response(output.getvalue(), mimetype='image/png') def create_price(): #plt.clf() pr = plt.figure(2) fig, ax = plt.subplots() ax.plot(preds['Date'], preds['Adj Close'], label='close') ax.plot(preds['Date'], preds['reg'], label='reg') ax.plot(preds['Date'], preds['krr'], label='krr') ax.plot(preds['Date'], preds['mlp'], label='mlp') months = mdates.MonthLocator() # every month mdays = mdates.DayLocator(bymonthday=range(1, 32)) # every day months_fmt = mdates.DateFormatter('%Y-%m-%d') # format the ticks ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(months_fmt) ax.xaxis.set_minor_locator(mdays) # round to nearest years. datemin = np.datetime64(np.amin(preds['Date']), 'D') datemax = np.datetime64(np.amax(preds['Date']), 'D') ax.set_xlim(datemin, datemax) # format the coords message box ax.format_xdata = mdates.DateFormatter('%Y-%m-%d') ax.format_ydata = lambda x: '$%1.2f' % x # format the price. ax.grid(True) # rotates and right aligns the x labels, and moves the bottom of the # axes up to make room for them fig.autofmt_xdate() handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels) #pr.show() saveplot('price', 'png') return fig
[ "os.remove", "matplotlib.dates.MonthLocator", "numpy.amin", "pandas.read_csv", "os.path.isfile", "flask.jsonify", "matplotlib.pyplot.figure", "random.randint", "matplotlib.backends.backend_agg.FigureCanvasAgg", "matplotlib.dates.DateFormatter", "flask.render_template", "matplotlib.pyplot.subplots", "io.BytesIO", "pandas.to_datetime", "app.app.route", "random.uniform", "pandas.plotting.register_matplotlib_converters", "numpy.amax", "matplotlib.pyplot.savefig" ]
[((593, 625), 'pandas.plotting.register_matplotlib_converters', 'register_matplotlib_converters', ([], {}), '()\n', (623, 625), False, 'from pandas.plotting import register_matplotlib_converters\n'), ((883, 905), 'pandas.read_csv', 'pd.read_csv', (['"""DIS.csv"""'], {}), "('DIS.csv')\n", (894, 905), True, 'import pandas as pd\n'), ((1037, 1064), 'pandas.read_csv', 'pd.read_csv', (['"""preds_d1.csv"""'], {}), "('preds_d1.csv')\n", (1048, 1064), True, 'import pandas as pd\n'), ((1179, 1203), 'pandas.read_csv', 'pd.read_csv', (['"""preds.csv"""'], {}), "('preds.csv')\n", (1190, 1203), True, 'import pandas as pd\n'), ((1299, 1313), 'app.app.route', 'app.route', (['"""/"""'], {}), "('/')\n", (1308, 1313), False, 'from app import app\n'), ((1316, 1335), 'app.app.route', 'app.route', (['"""/index"""'], {}), "('/index')\n", (1325, 1335), False, 'from app import app\n'), ((1412, 1429), 'app.app.route', 'app.route', (['"""/map"""'], {}), "('/map')\n", (1421, 1429), False, 'from app import app\n'), ((1501, 1544), 'app.app.route', 'app.route', (['"""/map/refresh"""'], {'methods': "['POST']"}), "('/map/refresh', methods=['POST'])\n", (1510, 1544), False, 'from app import app\n'), ((1774, 1795), 'app.app.route', 'app.route', (['"""/contact"""'], {}), "('/contact')\n", (1783, 1795), False, 'from app import app\n'), ((1877, 1899), 'app.app.route', 'app.route', (['"""/dif1.png"""'], {}), "('/dif1.png')\n", (1886, 1899), False, 'from app import app\n'), ((3390, 3413), 'app.app.route', 'app.route', (['"""/price.png"""'], {}), "('/price.png')\n", (3399, 3413), False, 'from app import app\n'), ((729, 752), 'os.path.isfile', 'os.path.isfile', (['strFile'], {}), '(strFile)\n', (743, 752), False, 'import os\n'), ((852, 872), 'matplotlib.pyplot.savefig', 'plt.savefig', (['strFile'], {}), '(strFile)\n', (863, 872), True, 'import matplotlib.pyplot as plt\n'), ((1362, 1405), 'flask.render_template', 'render_template', (['"""index.html"""'], {'title': '"""Home"""'}), "('index.html', title='Home')\n", (1377, 1405), False, 'from flask import render_template, jsonify\n'), ((1454, 1494), 'flask.render_template', 'render_template', (['"""map.html"""'], {'title': '"""Map"""'}), "('map.html', title='Map')\n", (1469, 1494), False, 'from flask import render_template, jsonify\n'), ((1740, 1767), 'flask.jsonify', 'jsonify', (["{'points': points}"], {}), "({'points': points})\n", (1747, 1767), False, 'from flask import render_template, jsonify\n'), ((1824, 1872), 'flask.render_template', 'render_template', (['"""contact.html"""'], {'title': '"""Contact"""'}), "('contact.html', title='Contact')\n", (1839, 1872), False, 'from flask import render_template, jsonify\n'), ((1955, 1967), 'io.BytesIO', 'io.BytesIO', ([], {}), '()\n', (1965, 1967), False, 'import io\n'), ((2103, 2116), 'matplotlib.pyplot.figure', 'plt.figure', (['(1)'], {}), '(1)\n', (2113, 2116), True, 'import matplotlib.pyplot as plt\n'), ((2134, 2148), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (2146, 2148), True, 'import matplotlib.pyplot as plt\n'), ((2417, 2438), 'matplotlib.dates.MonthLocator', 'mdates.MonthLocator', ([], {}), '()\n', (2436, 2438), True, 'import matplotlib.dates as mdates\n'), ((2542, 2574), 'matplotlib.dates.DateFormatter', 'mdates.DateFormatter', (['"""%Y-%m-%d"""'], {}), "('%Y-%m-%d')\n", (2562, 2574), True, 'import matplotlib.dates as mdates\n'), ((2978, 3010), 'matplotlib.dates.DateFormatter', 'mdates.DateFormatter', (['"""%Y-%m-%d"""'], {}), "('%Y-%m-%d')\n", (2998, 3010), True, 'import matplotlib.dates as mdates\n'), ((3472, 3484), 'io.BytesIO', 'io.BytesIO', ([], {}), '()\n', (3482, 3484), False, 'import io\n'), ((3639, 3652), 'matplotlib.pyplot.figure', 'plt.figure', (['(2)'], {}), '(2)\n', (3649, 3652), True, 'import matplotlib.pyplot as plt\n'), ((3670, 3684), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (3682, 3684), True, 'import matplotlib.pyplot as plt\n'), ((3929, 3950), 'matplotlib.dates.MonthLocator', 'mdates.MonthLocator', ([], {}), '()\n', (3948, 3950), True, 'import matplotlib.dates as mdates\n'), ((4054, 4086), 'matplotlib.dates.DateFormatter', 'mdates.DateFormatter', (['"""%Y-%m-%d"""'], {}), "('%Y-%m-%d')\n", (4074, 4086), True, 'import matplotlib.dates as mdates\n'), ((4484, 4516), 'matplotlib.dates.DateFormatter', 'mdates.DateFormatter', (['"""%Y-%m-%d"""'], {}), "('%Y-%m-%d')\n", (4504, 4516), True, 'import matplotlib.dates as mdates\n'), ((763, 781), 'os.remove', 'os.remove', (['strFile'], {}), '(strFile)\n', (772, 781), False, 'import os\n'), ((826, 846), 'matplotlib.pyplot.savefig', 'plt.savefig', (['strFile'], {}), '(strFile)\n', (837, 846), True, 'import matplotlib.pyplot as plt\n'), ((922, 950), 'pandas.to_datetime', 'pd.to_datetime', (["data['Date']"], {}), "(data['Date'])\n", (936, 950), True, 'import pandas as pd\n'), ((1085, 1117), 'pandas.to_datetime', 'pd.to_datetime', (["preds_d1['Date']"], {}), "(preds_d1['Date'])\n", (1099, 1117), True, 'import pandas as pd\n'), ((1221, 1250), 'pandas.to_datetime', 'pd.to_datetime', (["preds['Date']"], {}), "(preds['Date'])\n", (1235, 1250), True, 'import pandas as pd\n'), ((2788, 2813), 'numpy.amin', 'np.amin', (["preds_d1['Date']"], {}), "(preds_d1['Date'])\n", (2795, 2813), True, 'import numpy as np\n'), ((2849, 2874), 'numpy.amax', 'np.amax', (["preds_d1['Date']"], {}), "(preds_d1['Date'])\n", (2856, 2874), True, 'import numpy as np\n'), ((4300, 4322), 'numpy.amin', 'np.amin', (["preds['Date']"], {}), "(preds['Date'])\n", (4307, 4322), True, 'import numpy as np\n'), ((4358, 4380), 'numpy.amax', 'np.amax', (["preds['Date']"], {}), "(preds['Date'])\n", (4365, 4380), True, 'import numpy as np\n'), ((1581, 1615), 'random.uniform', 'random.uniform', (['(48.84341)', '(48.86341)'], {}), '(48.84341, 48.86341)\n', (1595, 1615), False, 'import random\n'), ((1637, 1667), 'random.uniform', 'random.uniform', (['(2.3388)', '(2.3588)'], {}), '(2.3388, 2.3588)\n', (1651, 1667), False, 'import random\n'), ((1973, 1990), 'matplotlib.backends.backend_agg.FigureCanvasAgg', 'FigureCanvas', (['fig'], {}), '(fig)\n', (1985, 1990), True, 'from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas\n'), ((3490, 3507), 'matplotlib.backends.backend_agg.FigureCanvasAgg', 'FigureCanvas', (['fig'], {}), '(fig)\n', (3502, 3507), True, 'from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas\n'), ((1705, 1725), 'random.randint', 'random.randint', (['(2)', '(9)'], {}), '(2, 9)\n', (1719, 1725), False, 'import random\n')]
import h5py import numpy as np from sklearn import svm import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.utils import shuffle rbfSigma = 0.1 def readFile(filename): with h5py.File(filename, 'r') as f: a_group_key = list(f.keys()) X = list(f[a_group_key[0]]) y = list(f[a_group_key[1]]) return np.array(X), np.array(y) def form_mesh(x, y, h=0.1): x_min, x_max = x.min() - 1, x.max() + 1 y_min, y_max = y.min() - 1, y.max() + 1 return np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) def linear(X1, X2): gram_matrix = np.zeros((X1.shape[0], X2.shape[0])) for i, x1 in enumerate(X1): for j, x2 in enumerate(X2): x1 = x1.flatten() x2 = x2.flatten() gram_matrix[i, j] = np.dot(x1, x2) return gram_matrix def rbfKernel(X1, X2): gamma = 1 / float( 2*(rbfSigma**2)) gram_matrix = np.zeros((X1.shape[0], X2.shape[0])) for i, x1 in enumerate(X1): for j, x2 in enumerate(X2): x1 = x1.flatten() x2 = x2.flatten() gram_matrix[i, j] = np.exp(- np.sum(np.power((x1 - x2), 2) ) * gamma ) return gram_matrix class SVM(): def __init__(self, kernel = linear, C = 1, sigma=None): self.C = C self.kernel = kernel self.sigma = sigma self.clf = svm.SVC(kernel=self.kernel, C = self.C) def fit(self, X, y): self.clf.fit(X, y) def predict(self, X): return self.clf.predict(X) def accuracy(self, X, y): m = X.shape[0] return round(np.sum(self.predict(X) == y) / m, 3) def custom_predict(self, pX, X): m, n = pX.shape b = self.clf._intercept_[0] a = self.clf.dual_coef_[0] take_feature = self.clf.support_ sv = X[take_feature] val = np.zeros(m) for j in range(m): for i in range(len(sv)): s = sv[i].reshape(n, 1) p = pX[j].reshape(n, 1) val[j] += a[i] * self.kernel(s.T, p.T) pred = (val - b >= 0)*1 return pred def custom_accuracy(self, X, y, xTrain): pred = self.custom_predict(X, xTrain) return round(np.mean(pred == y), 3) def __str__(self): print('sv = ' + str(self.clf.support_vectors_)) print('nv = ' + str(self.clf.n_support_)) print('a = ' + str(self.clf.dual_coef_)) print('a.shape = ' + str(self.clf.dual_coef_.shape)) print('b = ' + str(self.clf._intercept_)) print('cs = ' + str(self.clf.classes_)) print(str(self.clf.support_)) return "" def get_data(fItr): filename = "./dataset_q1/data_" + str(fItr) + ".h5" X, y = readFile(filename) return X, y def plot_graph(X, y, clfs, h, s, X_b, y_b): X0, X1 = X[:, 0], X[:, 1] xx, yy = form_mesh(X0, X1, h=h) meshVals = np.c_[xx.ravel(), yy.ravel()] Z = None try: Zs = [] for model in clfs: Z = model.predict(meshVals) Zs.append(Z) Zs = np.array(Zs) Z = np.argmax(Zs, axis=0) except: Z = clfs.predict(meshVals) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.7) plt.scatter(X[:, 0], X[:, 1], c=y, s=s, cmap=plt.cm.coolwarm) try: plt.scatter(X_b[:, 0], X_b[:, 1], c=y_b, s=20, cmap=plt.cm.cool) except: pass plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.title("Decision Boundary Plot") plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.show() def get_model(X, y, ker, C = 1, sigma = 1, h = 0.1, visualize=True, s=None, X_b = [], y_b = []): global rbfSigma m, n = X.shape rbfSigma = sigma clf = SVM(kernel=ker, C=C, sigma=sigma) clf.fit(X, y) if(visualize): plot_graph(X, y, clf, h, s, X_b, y_b) return clf def getOutliers(X, y, f): scaler = StandardScaler() X0 = X[y == 0] y0 = y[y == 0] X1 = X[y == 1] y1 = y[y == 1] X0t = scaler.fit_transform(X0) X1t = scaler.fit_transform(X1) out0 = np.where(np.absolute(X0t) > f[0])[0] out1 = np.where(np.absolute(X1t) > f[1])[0] clusters0 = np.zeros(X0.shape[0]) clusters1 = np.zeros(X1.shape[0]) clusters0[out0] = -1 clusters1[out1] = -1 X0, y0, X_b0, y_b0 = refineData(X0, y0, clusters0) X1, y1, X_b1, y_b1 = refineData(X1, y1, clusters1) X = np.vstack((X0, X1)) X_b = np.vstack((X_b0, X_b1)) y = np.append(y0, y1) y_b = np.append(y_b0, y_b1) X, y = shuffle(X, y) return X, y, X_b, y_b def refineData(X, y, f): X_b = X[f == -1] y_b = y[f == -1] X = np.delete(X, np.where(f == -1), axis = 0) y = np.delete(y, np.where(f == -1), axis = 0) return X, y, X_b, y_b if __name__ == '__main__': X, y = get_data(1) clf1 = get_model(X, y, ker=rbfKernel, C=100, sigma=1, h=0.05, s=5) X, y = get_data(2) clf2 = get_model(X, y, ker=rbfKernel, C=1000, sigma=1, h=0.05, s=5) X, y = get_data(3) clf3 = [] Y = y for i in range(3): y = np.copy(Y) y[y == (i + 2) % 3] = -1 y[y == (i + 1) % 3] = -1 y[y == i] = 1 y[y == -1] = 0 clf = get_model(X, y, ker=linear, visualize=False) clf3.append(clf) plot_graph(X, Y, clf3, h=0.3, s=10, X_b = [], y_b = []) X, y = get_data(4) X, y, X_b, y_b = getOutliers(X, y, (1.8, 1.9)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) clf4Linear = get_model(X_train, y_train, ker=linear, s=1, X_b=X_b, y_b=y_b) clf4Rbf = get_model(X_train, y_train, ker=rbfKernel, C=0.01, sigma=1, h=0.1, s=1, X_b=X_b, y_b=y_b) accuracyLinear_train = clf4Linear.custom_accuracy(X_train, y_train, X_train) accuracyLinear_test = clf4Linear.custom_accuracy(X_test, y_test, X_train) accuracyRbf_train = clf4Rbf.custom_accuracy(X_train, y_train, X_train) accuracyRbf_test = clf4Rbf.custom_accuracy(X_test, y_test, X_train) print("Accuracy for Prediciton with Custom Method") print(" "*10, "Linear".center(20), "RBF".center(20)) print("Train".center(10), str(accuracyLinear_train).center(20), str(accuracyRbf_train).center(20)) print("Test".center(10), str(accuracyLinear_test).center(20), str(accuracyRbf_test).center(20)) accuracyLinear_train = clf4Linear.accuracy(X_train, y_train) accuracyLinear_test = clf4Linear.accuracy(X_test, y_test) accuracyRbf_train = clf4Rbf.accuracy(X_train, y_train) accuracyRbf_test = clf4Rbf.accuracy(X_test, y_test) print("Accuracy for Prediciton with Inbuilt Method") print(" "*10, "Linear".center(20), "RBF".center(20)) print("Train".center(10), str(accuracyLinear_train).center(20), str(accuracyRbf_train).center(20)) print("Test".center(10), str(accuracyLinear_test).center(20), str(accuracyRbf_test).center(20)) X, y = get_data(5) X, y, X_b, y_b = getOutliers(X, y, (1.8, 1.9)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) clf5Linear = get_model(X_train, y_train, ker=linear, s=1, X_b=X_b, y_b=y_b) clf5Rbf = get_model(X_train, y_train, ker=rbfKernel, C=10, sigma=2, h=0.1, s=1, X_b=X_b, y_b=y_b) accuracyLinear_train = clf5Linear.custom_accuracy(X_train, y_train, X_train) accuracyLinear_test = clf5Linear.custom_accuracy(X_test, y_test, X_train) accuracyRbf_train = clf5Rbf.custom_accuracy(X_train, y_train, X_train) accuracyRbf_test = clf5Rbf.custom_accuracy(X_test, y_test, X_train) print("Accuracy for Prediciton with Custom Method") print(" "*10, "Linear".center(20), "RBF".center(20)) print("Train".center(10), str(accuracyLinear_train).center(20), str(accuracyRbf_train).center(20)) print("Test".center(10), str(accuracyLinear_test).center(20), str(accuracyRbf_test).center(20)) accuracyLinear_train = clf5Linear.accuracy(X_train, y_train) accuracyLinear_test = clf5Linear.accuracy(X_test, y_test) accuracyRbf_train = clf5Rbf.accuracy(X_train, y_train) accuracyRbf_test = clf5Rbf.accuracy(X_test, y_test) print("Accuracy for Prediciton with Inbuilt Method") print(" "*10, "Linear".center(20), "RBF".center(20)) print("Train".center(10), str(accuracyLinear_train).center(20), str(accuracyRbf_train).center(20)) print("Test".center(10), str(accuracyLinear_test).center(20), str(accuracyRbf_test).center(20))
[ "matplotlib.pyplot.title", "numpy.absolute", "sklearn.preprocessing.StandardScaler", "numpy.argmax", "sklearn.model_selection.train_test_split", "numpy.mean", "matplotlib.pyplot.contourf", "numpy.arange", "sklearn.svm.SVC", "numpy.copy", "numpy.power", "numpy.append", "h5py.File", "matplotlib.pyplot.show", "matplotlib.pyplot.ylabel", "numpy.dot", "numpy.vstack", "matplotlib.pyplot.scatter", "numpy.zeros", "numpy.where", "numpy.array", "sklearn.utils.shuffle", "matplotlib.pyplot.xlabel" ]
[((685, 721), 'numpy.zeros', 'np.zeros', (['(X1.shape[0], X2.shape[0])'], {}), '((X1.shape[0], X2.shape[0]))\n', (693, 721), True, 'import numpy as np\n'), ((1002, 1038), 'numpy.zeros', 'np.zeros', (['(X1.shape[0], X2.shape[0])'], {}), '((X1.shape[0], X2.shape[0]))\n', (1010, 1038), True, 'import numpy as np\n'), ((3272, 3328), 'matplotlib.pyplot.contourf', 'plt.contourf', (['xx', 'yy', 'Z'], {'cmap': 'plt.cm.coolwarm', 'alpha': '(0.7)'}), '(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.7)\n', (3284, 3328), True, 'import matplotlib.pyplot as plt\n'), ((3333, 3394), 'matplotlib.pyplot.scatter', 'plt.scatter', (['X[:, 0]', 'X[:, 1]'], {'c': 'y', 's': 's', 'cmap': 'plt.cm.coolwarm'}), '(X[:, 0], X[:, 1], c=y, s=s, cmap=plt.cm.coolwarm)\n', (3344, 3394), True, 'import matplotlib.pyplot as plt\n'), ((3506, 3529), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Feature 1"""'], {}), "('Feature 1')\n", (3516, 3529), True, 'import matplotlib.pyplot as plt\n'), ((3534, 3557), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Feature 2"""'], {}), "('Feature 2')\n", (3544, 3557), True, 'import matplotlib.pyplot as plt\n'), ((3562, 3597), 'matplotlib.pyplot.title', 'plt.title', (['"""Decision Boundary Plot"""'], {}), "('Decision Boundary Plot')\n", (3571, 3597), True, 'import matplotlib.pyplot as plt\n'), ((3668, 3678), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3676, 3678), True, 'import matplotlib.pyplot as plt\n'), ((4026, 4042), 'sklearn.preprocessing.StandardScaler', 'StandardScaler', ([], {}), '()\n', (4040, 4042), False, 'from sklearn.preprocessing import StandardScaler\n'), ((4301, 4322), 'numpy.zeros', 'np.zeros', (['X0.shape[0]'], {}), '(X0.shape[0])\n', (4309, 4322), True, 'import numpy as np\n'), ((4339, 4360), 'numpy.zeros', 'np.zeros', (['X1.shape[0]'], {}), '(X1.shape[0])\n', (4347, 4360), True, 'import numpy as np\n'), ((4529, 4548), 'numpy.vstack', 'np.vstack', (['(X0, X1)'], {}), '((X0, X1))\n', (4538, 4548), True, 'import numpy as np\n'), ((4559, 4582), 'numpy.vstack', 'np.vstack', (['(X_b0, X_b1)'], {}), '((X_b0, X_b1))\n', (4568, 4582), True, 'import numpy as np\n'), ((4591, 4608), 'numpy.append', 'np.append', (['y0', 'y1'], {}), '(y0, y1)\n', (4600, 4608), True, 'import numpy as np\n'), ((4619, 4640), 'numpy.append', 'np.append', (['y_b0', 'y_b1'], {}), '(y_b0, y_b1)\n', (4628, 4640), True, 'import numpy as np\n'), ((4652, 4665), 'sklearn.utils.shuffle', 'shuffle', (['X', 'y'], {}), '(X, y)\n', (4659, 4665), False, 'from sklearn.utils import shuffle\n'), ((5576, 5613), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': '(0.2)'}), '(X, y, test_size=0.2)\n', (5592, 5613), False, 'from sklearn.model_selection import train_test_split\n'), ((7101, 7138), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': '(0.2)'}), '(X, y, test_size=0.2)\n', (7117, 7138), False, 'from sklearn.model_selection import train_test_split\n'), ((274, 298), 'h5py.File', 'h5py.File', (['filename', '"""r"""'], {}), "(filename, 'r')\n", (283, 298), False, 'import h5py\n'), ((425, 436), 'numpy.array', 'np.array', (['X'], {}), '(X)\n', (433, 436), True, 'import numpy as np\n'), ((438, 449), 'numpy.array', 'np.array', (['y'], {}), '(y)\n', (446, 449), True, 'import numpy as np\n'), ((590, 616), 'numpy.arange', 'np.arange', (['x_min', 'x_max', 'h'], {}), '(x_min, x_max, h)\n', (599, 616), True, 'import numpy as np\n'), ((618, 644), 'numpy.arange', 'np.arange', (['y_min', 'y_max', 'h'], {}), '(y_min, y_max, h)\n', (627, 644), True, 'import numpy as np\n'), ((1443, 1480), 'sklearn.svm.SVC', 'svm.SVC', ([], {'kernel': 'self.kernel', 'C': 'self.C'}), '(kernel=self.kernel, C=self.C)\n', (1450, 1480), False, 'from sklearn import svm\n'), ((1927, 1938), 'numpy.zeros', 'np.zeros', (['m'], {}), '(m)\n', (1935, 1938), True, 'import numpy as np\n'), ((3146, 3158), 'numpy.array', 'np.array', (['Zs'], {}), '(Zs)\n', (3154, 3158), True, 'import numpy as np\n'), ((3171, 3192), 'numpy.argmax', 'np.argmax', (['Zs'], {'axis': '(0)'}), '(Zs, axis=0)\n', (3180, 3192), True, 'import numpy as np\n'), ((3412, 3476), 'matplotlib.pyplot.scatter', 'plt.scatter', (['X_b[:, 0]', 'X_b[:, 1]'], {'c': 'y_b', 's': '(20)', 'cmap': 'plt.cm.cool'}), '(X_b[:, 0], X_b[:, 1], c=y_b, s=20, cmap=plt.cm.cool)\n', (3423, 3476), True, 'import matplotlib.pyplot as plt\n'), ((4781, 4798), 'numpy.where', 'np.where', (['(f == -1)'], {}), '(f == -1)\n', (4789, 4798), True, 'import numpy as np\n'), ((4831, 4848), 'numpy.where', 'np.where', (['(f == -1)'], {}), '(f == -1)\n', (4839, 4848), True, 'import numpy as np\n'), ((5196, 5206), 'numpy.copy', 'np.copy', (['Y'], {}), '(Y)\n', (5203, 5206), True, 'import numpy as np\n'), ((882, 896), 'numpy.dot', 'np.dot', (['x1', 'x2'], {}), '(x1, x2)\n', (888, 896), True, 'import numpy as np\n'), ((2307, 2325), 'numpy.mean', 'np.mean', (['(pred == y)'], {}), '(pred == y)\n', (2314, 2325), True, 'import numpy as np\n'), ((4209, 4225), 'numpy.absolute', 'np.absolute', (['X0t'], {}), '(X0t)\n', (4220, 4225), True, 'import numpy as np\n'), ((4257, 4273), 'numpy.absolute', 'np.absolute', (['X1t'], {}), '(X1t)\n', (4268, 4273), True, 'import numpy as np\n'), ((1216, 1236), 'numpy.power', 'np.power', (['(x1 - x2)', '(2)'], {}), '(x1 - x2, 2)\n', (1224, 1236), True, 'import numpy as np\n')]
import torch import numpy as np def suit4pytorch(X, Y): X = np.swapaxes(X, 1, 3) X_norm = X/255 X_torch = torch.from_numpy(X_norm).float() Y_torch = torch.from_numpy(Y).long() return X_torch, Y_torch
[ "numpy.swapaxes", "torch.from_numpy" ]
[((66, 86), 'numpy.swapaxes', 'np.swapaxes', (['X', '(1)', '(3)'], {}), '(X, 1, 3)\n', (77, 86), True, 'import numpy as np\n'), ((120, 144), 'torch.from_numpy', 'torch.from_numpy', (['X_norm'], {}), '(X_norm)\n', (136, 144), False, 'import torch\n'), ((167, 186), 'torch.from_numpy', 'torch.from_numpy', (['Y'], {}), '(Y)\n', (183, 186), False, 'import torch\n')]
#needs pytorch_transformers version 1.2.0 #!/usr/bin/env python # coding: utf-8 import argparse import re import os import _pickle as cPickle import numpy as np import pandas as pd import torch from pytorch_transformers.tokenization_bert import BertTokenizer def assert_eq(real, expected): assert real == expected, "%s (true) vs %s (expected)" % (real, expected) # the same tokenize function from BERT adapted for this task def tokenize(entries, tokenizer, max_length=16, padding_index=0): """Tokenizes the captions. This will add c_token in each entry of the dataset. -1 represent nil, and should be treated as padding_index in embedding """ for entry in entries: tokens = tokenizer.encode(entry["caption"]) tokens = tokens[: max_length - 2] tokens = tokenizer.add_special_tokens_single_sentence(tokens) segment_ids = [0] * len(tokens) input_mask = [1] * len(tokens) if len(tokens) < max_length: # Note here we pad in front of the sentence padding = [padding_index] * (max_length - len(tokens)) tokens = tokens + padding input_mask += padding segment_ids += padding assert_eq(len(tokens), max_length) entry["c_token"] = tokens entry["c_input_mask"] = input_mask entry["c_segment_ids"] = segment_ids # the same tensorize function from BERT adapted for this task def tensorize(entries, split='trainval'): for entry in entries: caption = torch.from_numpy(np.array(entry["c_token"])) entry["c_token"] = caption c_input_mask = torch.from_numpy(np.array(entry["c_input_mask"])) entry["c_input_mask"] = c_input_mask c_segment_ids = torch.from_numpy(np.array(entry["c_segment_ids"])) entry["c_segment_ids"] = c_segment_ids if "scores" in entry: scores = np.array(entry["scores"], dtype=np.float32) scores = torch.from_numpy(scores) entry["scores"] = scores def main(): parser = argparse.ArgumentParser() parser.add_argument( "--bert_model", default="bert-base-uncased", type=str, help="Bert pre-trained model selected in the list: bert-base-uncased, " "bert-large-uncased, bert-base-cased, bert-base-multilingual, bert-base-chinese.", ) parser.add_argument( "--captions_path", type=str, default="/aloui/MediaEval/dev-set/dev-set_video-captions.txt", help="Captions .txt file" ) parser.add_argument( "--gt_path", type=str, default="/MediaEval/dev-set/ground-truth/ground-truth_dev-set.csv", help="Ground truth .csv file" ) parser.add_argument( "--do_lower_case", default=True, type=bool, help="Whether to lower case the input text. True for uncased models, False for cased models.", ) parser.add_argument( "--split", required=True, type=str, help="which split to use trainval or test" ) parser.add_argument( "--dc", action="store_true", help="Whether to use deep captions or not" ) args = parser.parse_args() try: assert args.split in ["trainval", "test"] except Exception as error: print("Split must be trainval or test") raise #deep_coptions_path = "/MediaEval/alto_titles_danny.csv" #train_caption_path = '/aloui/MediaEval/dev-set/dev-set_video-captions.txt' dataroot = 'datasets/ME2020' max_length = 23 tokenizer = BertTokenizer.from_pretrained(args.bert_model, do_lower_case=args.do_lower_case) entries = [] if args.dc: deep_coptions_df = pd.read_csv(args.captions_path) entries = [] for r in deep_coptions_df.itertuples(): sample = {} vid_id = int(r.video) caption = r.caption.rstrip().replace('-', ' ') sample['video_id'] = vid_id sample['caption'] = caption entries.append(sample) else: df=pd.read_csv(args.captions_path) df= df.groupby('video_id').agg({'video_url':'first', 'description': ' '.join}).reset_index() print(df.description) for r in df.itertuples(): #print(r) sample = {} #vid_id,video_url, caption = line.split(',' #vid_id = re.findall(r'\d+', vid_name)[0] #caption = caption.rstrip().replace('-', ' ') sample['video_id'] = int(r.video_id) sample['caption'] = r.description entries.append(sample) train_df = pd.read_csv(args.gt_path) score_dict = {} for r in train_df.itertuples(): vid_id = r.video_id vid_id = int(vid_id) score_dict[vid_id] = [r.part_1_scores, r.part_2_scores] train_score_list = [] for sample in entries: if sample['video_id'] in score_dict: sample['scores'] = score_dict[sample['video_id']] train_score_list.append(sample) tokenize(train_score_list, tokenizer, max_length=max_length) tensorize(train_score_list, split=args.split) #print(len(train_score_list)) #print(train_score_list[0]) train_cache_path = os.path.join(dataroot, 'cache', 'ME2020' + '_' + args.split + '_' + str(max_length) + '_cleaned' + '.pkl') print("Saving cache file with {} samples under {}".format(len(train_score_list), train_cache_path)) cPickle.dump(train_score_list, open(train_cache_path, 'wb')) if __name__ == "__main__": main()
[ "argparse.ArgumentParser", "pandas.read_csv", "pytorch_transformers.tokenization_bert.BertTokenizer.from_pretrained", "numpy.array", "torch.from_numpy" ]
[((2052, 2077), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (2075, 2077), False, 'import argparse\n'), ((3632, 3717), 'pytorch_transformers.tokenization_bert.BertTokenizer.from_pretrained', 'BertTokenizer.from_pretrained', (['args.bert_model'], {'do_lower_case': 'args.do_lower_case'}), '(args.bert_model, do_lower_case=args.do_lower_case\n )\n', (3661, 3717), False, 'from pytorch_transformers.tokenization_bert import BertTokenizer\n'), ((4703, 4728), 'pandas.read_csv', 'pd.read_csv', (['args.gt_path'], {}), '(args.gt_path)\n', (4714, 4728), True, 'import pandas as pd\n'), ((3779, 3810), 'pandas.read_csv', 'pd.read_csv', (['args.captions_path'], {}), '(args.captions_path)\n', (3790, 3810), True, 'import pandas as pd\n'), ((4133, 4164), 'pandas.read_csv', 'pd.read_csv', (['args.captions_path'], {}), '(args.captions_path)\n', (4144, 4164), True, 'import pandas as pd\n'), ((1542, 1568), 'numpy.array', 'np.array', (["entry['c_token']"], {}), "(entry['c_token'])\n", (1550, 1568), True, 'import numpy as np\n'), ((1646, 1677), 'numpy.array', 'np.array', (["entry['c_input_mask']"], {}), "(entry['c_input_mask'])\n", (1654, 1677), True, 'import numpy as np\n'), ((1766, 1798), 'numpy.array', 'np.array', (["entry['c_segment_ids']"], {}), "(entry['c_segment_ids'])\n", (1774, 1798), True, 'import numpy as np\n'), ((1899, 1942), 'numpy.array', 'np.array', (["entry['scores']"], {'dtype': 'np.float32'}), "(entry['scores'], dtype=np.float32)\n", (1907, 1942), True, 'import numpy as np\n'), ((1964, 1988), 'torch.from_numpy', 'torch.from_numpy', (['scores'], {}), '(scores)\n', (1980, 1988), False, 'import torch\n')]
from compas import PRECISION class SmoothUnion(object): """The smooth union between two volumetric objects. Parameters ---------- a: volumetric object First object to add. b: volumetric object Second object to add. r: float Intensity factor, the higher the number, the smoother the result. Default value `1.0` Examples -------- >>> s = Sphere(Point(5, 6, 0), 9) >>> b = Box(Frame.worldXY(), 20, 15, 10) >>> vs = VolSphere(s) >>> vb = VolBox(b, 2.5) >>> u = SmoothUnion(vs, vb, 1.5) """ def __init__(self, a=None, b=None, r=1.0): self.a = a self.b = b self.r = r def __repr__(self): return 'SmoothUnion({0},{1},{2:.{3}f})'.format(str(self.a), str(self.b), self.r, PRECISION[:1]) def get_distance_alt(self, x, y, z): da = self.a.get_distance(x, y, z) db = self.b.get_distance(x, y, z) e = max(self.r - abs(da - db), 0) return min(da, db) - e**2 * 0.25 / self.r def get_distance(self, point): """ single point distance function """ da = self.a.get_distance(point) db = self.b.get_distance(point) k = self.r h = min(max(0.5 + 0.5 * (db - da) / k, 0), 1) return (db * (1 - h) + h * da) - k * h * (1 - h) def get_distance_numpy(self, x, y, z): """ vectorized distance function """ import numpy as np da = self.a.get_distance_numpy(x, y, z) db = self.b.get_distance_numpy(x, y, z) h = np.minimum(np.maximum(0.5 + 0.5 * (db - da)/self.r, 0), 1) return (db * (1 - h) + h * da) - self.r * h * (1 - h) # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": from compas_vol.primitives import VolSphere, VolBox from compas.geometry import Box, Frame, Point, Sphere import numpy as np import matplotlib.pyplot as plt s = Sphere(Point(4, 5, 0), 7) b = Box(Frame.worldXY(), 20, 15, 10) vs = VolSphere(s) vb = VolBox(b, 2.5) u = SmoothUnion(vs, vb, 6.5) # for y in range(-15, 15): # s = '' # for x in range(-30, 30): # d = u.get_distance(Point(x*0.5, y, 0)) # if d < 0: # s += 'x' # else: # s += '.' # print(s) x, y, z = np.ogrid[-15:15:100j, -15:15:100j, -15:15:100j] d = u.get_distance_numpy(x, y, z) m = d[:, :, 50].T plt.imshow(-np.tanh(m*5), cmap='Greys') # plt.colorbar() plt.show()
[ "compas_vol.primitives.VolSphere", "matplotlib.pyplot.show", "numpy.maximum", "compas.geometry.Point", "numpy.tanh", "compas_vol.primitives.VolBox", "compas.geometry.Frame.worldXY" ]
[((2149, 2161), 'compas_vol.primitives.VolSphere', 'VolSphere', (['s'], {}), '(s)\n', (2158, 2161), False, 'from compas_vol.primitives import VolSphere, VolBox\n'), ((2171, 2185), 'compas_vol.primitives.VolBox', 'VolBox', (['b', '(2.5)'], {}), '(b, 2.5)\n', (2177, 2185), False, 'from compas_vol.primitives import VolSphere, VolBox\n'), ((2664, 2674), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2672, 2674), True, 'import matplotlib.pyplot as plt\n'), ((2080, 2094), 'compas.geometry.Point', 'Point', (['(4)', '(5)', '(0)'], {}), '(4, 5, 0)\n', (2085, 2094), False, 'from compas.geometry import Box, Frame, Point, Sphere\n'), ((2111, 2126), 'compas.geometry.Frame.worldXY', 'Frame.worldXY', ([], {}), '()\n', (2124, 2126), False, 'from compas.geometry import Box, Frame, Point, Sphere\n'), ((1582, 1627), 'numpy.maximum', 'np.maximum', (['(0.5 + 0.5 * (db - da) / self.r)', '(0)'], {}), '(0.5 + 0.5 * (db - da) / self.r, 0)\n', (1592, 1627), True, 'import numpy as np\n'), ((2611, 2625), 'numpy.tanh', 'np.tanh', (['(m * 5)'], {}), '(m * 5)\n', (2618, 2625), True, 'import numpy as np\n')]
# -*- coding: utf-8 -*- # Copyright 2018, IBM. # # This source code is licensed under the Apache License, Version 2.0 found in # the LICENSE.txt file in the root directory of this source tree. """ Quantum tomography fitter data formatting. """ import logging import itertools as it import numpy as np from scipy import linalg as la from qiskit import QiskitError from ..data import marginal_counts, count_keys from ..basis import TomographyBasis, default_basis # Create logger logger = logging.getLogger(__name__) ########################################################################### # Data formats for converting from counts to fitter data ########################################################################### def fitter_data(tomo_data, meas_basis='Pauli', prep_basis='Pauli', standard_weights=True, beta=0.5): """Generate tomography fitter data from a tomography data dictionary. Args: tomo_data (dict): tomography data returned from `tomography_data` function. meas_matrix_fn (function, optional): A function to return measurement operators corresponding to measurement outcomes. See Additional Information (default: 'Pauli') prep_matrix_fn (function, optional): A function to return preparation operators. See Additional Information (default: 'Pauli') standard_weights (bool, optional): Apply weights to basis matrix and data based on count probability (default: True) beta (float): hedging parameter for 0, 1 probabilities (default: 0.5) Returns: tuple: (data, basis_matrix, weights) where `data` is a vector of the probability values, and `basis_matrix` is a matrix of the preparation and measurement operator, and `weights` is a vector of weights for the given probabilities. Additional Information ---------------------- standard_weights: Weights are calculated from from binomial distribution standard deviation """ # Load built-in circuit functions if callable(meas_basis): measurement = meas_basis else: measurement = default_basis(meas_basis) if isinstance(measurement, TomographyBasis): if measurement.measurement is not True: raise QiskitError("Invalid measurement basis") measurement = measurement.measurement_matrix if callable(prep_basis): preparation = prep_basis else: preparation = default_basis(prep_basis) if isinstance(preparation, TomographyBasis): if preparation.preparation is not True: raise QiskitError("Invalid preparation basis") preparation = preparation.preparation_matrix data = [] basis_blocks = [] if standard_weights: weights = [] else: weights = None # Check if input data is state or process tomography data based # on the label tuples label = next(iter(tomo_data)) is_qpt = (isinstance(label, tuple) and len(label) == 2 and isinstance(label[0], tuple) and isinstance(label[1], tuple)) # Generate counts keys for converting to np array if is_qpt: ctkeys = count_keys(len(label[1])) else: ctkeys = count_keys(len(label)) for label, cts in tomo_data.items(): # Convert counts dict to numpy array if isinstance(cts, dict): cts = np.array([cts.get(key, 0) for key in ctkeys]) # Get probabilities shots = np.sum(cts) probs = np.array(cts) / shots data += list(probs) # Compute binomial weights if standard_weights is True: wts = binomial_weights(cts, beta) weights += list(wts) # Get reconstruction basis operators if is_qpt: prep_label = label[0] meas_label = label[1] else: prep_label = None meas_label = label prep_op = _preparation_op(prep_label, preparation) meas_ops = _measurement_ops(meas_label, measurement) block = _basis_operator_matrix( [np.kron(prep_op.T, mop) for mop in meas_ops]) basis_blocks.append(block) return data, np.vstack(basis_blocks), weights ########################################################################### # Binomial weights for count statistics ########################################################################### def binomial_weights(counts, beta=0.5): """ Compute binomial weights for list or dictionary of counts. Args: counts (dict, vector): A set of measurement counts for all outcomes of a given measurement configuration. beta (float >= 0): A hedging parameter used to bias probabilities computed from input counts away from 0 or 1. Returns: A numpy array of binomial weights for the input counts and beta parameter. Additional Information: The weights are determined by w[i] = sqrt(shots / p[i] * (1 - p[i])) p[i] = (counts[i] + beta) / (shots + K * beta) where `shots` is the sum of all counts in the input `p` is the hedged probability computed for a count `K` is the total number of possible measurement outcomes. """ # Sort counts if input is a dictionary if isinstance(counts, dict): mcts = marginal_counts(counts, pad_zeros=True) ordered_keys = sorted(list(mcts)) counts = np.array([mcts[k] for k in ordered_keys]) # Assume counts are already sorted if a list else: counts = np.array(counts) shots = np.sum(counts) # If beta is 0 check if we would be dividing by zero # If so change beta value and log warning. if beta < 0: raise ValueError('beta = {} must be non-negative.'.format(beta)) if beta == 0 and (shots in counts or 0 in counts): beta = 0.5 msg = ("Counts result in probabilities of 0 or 1 in binomial weights " "calculation. Setting hedging parameter beta={} to prevent " "dividing by zero.".format(beta)) logger.warning(msg) K = len(counts) # Number of possible outcomes. # Compute hedged frequencies which are shifted to never be 0 or 1. freqs_hedged = (counts + beta) / (shots + K * beta) # Return gaussian weights for 2-outcome measurements. return np.sqrt(shots / (freqs_hedged * (1 - freqs_hedged))) ########################################################################### # Wizard Method rescaling ########################################################################### def make_positive_semidefinite(mat, epsilon=0): """ Rescale a Hermitian matrix to nearest postive semidefinite matrix. Args: mat (array like): a hermitian matrix. epsilon (float >=0, optional): the threshold for setting eigenvalues to zero. If epsilon > 0 positive eigenvalues below epislon will also be set to zero (Default 0). Returns: The input matrix rescaled to have non-negative eigenvalues. References: [1] <NAME>, <NAME>, <NAME>, Phys. Rev. Lett. 108, 070502 (2012). Open access: arXiv:1106.5458 [quant-ph]. """ if epsilon < 0: raise ValueError('epsilon must be non-negative.') # Get the eigenvalues and eigenvectors of rho # eigenvalues are sorted in increasing order # v[i] <= v[i+1] dim = len(mat) v, w = la.eigh(mat) for j in range(dim): if v[j] < epsilon: tmp = v[j] v[j] = 0. # Rescale remaining eigenvalues x = 0. for k in range(j + 1, dim): x += tmp / (dim - (j + 1)) v[k] = v[k] + tmp / (dim - (j + 1)) # Build positive matrix from the rescaled eigenvalues # and the original eigenvectors mat_psd = np.zeros([dim, dim], dtype=complex) for j in range(dim): mat_psd += v[j] * np.outer(w[:, j], np.conj(w[:, j])) return mat_psd ########################################################################### # Basis projector construction functions ########################################################################### def _basis_operator_matrix(basis): """ Return a basis measurement matrix of the input basis. Args: basis (list (array like)): a list of basis matrices. Returns: A numpy array of shape (n, col * row) where n is the number of operators of shape (row, col) in `basis`. """ # Dimensions num_ops = len(basis) nrows, ncols = basis[0].shape size = nrows * ncols ret = np.zeros((num_ops, size), dtype=complex) for j, b in enumerate(basis): ret[j] = np.array(b).reshape((1, size), order='F').conj() return ret def _preparation_op(label, prep_matrix_fn): """ Return the multi-qubit matrix for a state preparation label. Args: label (tuple(str)): a preparation configuration label for a tomography circuit. prep_matrix_fn (function): a function that returns the matrix corresponding to a single qubit preparation label. The functions should have signature prep_matrix_fn(str) -> np.array Returns: A Numpy array for the multi-qubit prepration operator specified by label. Additional Information: See the Pauli and SIC-POVM preparation functions `pauli_preparation_matrix` and `sicpovm_preparation_matrix` for examples. """ # Trivial case if label is None: return np.eye(1, dtype=complex) # Construct preparation matrix op = np.eye(1, dtype=complex) for l in label: op = np.kron(prep_matrix_fn(l), op) return op def _measurement_ops(label, meas_matrix_fn): """ Return a list multi-qubit matrices for a measurement label. Args: label (tuple(str)): a measurement configuration label for a tomography circuit. meas_matrix_fn (function): a function that returns the matrix corresponding to a single qubit measurement label for a given outcome. The functions should have signature meas_matrix_fn(str, int) -> np.array Returns: A list of Numpy array for the multi-qubit measurement operators for all measurement outcomes for the measurement basis specified by the label. These are ordered in increasing binary order. Eg for 2-qubits the returned matrices correspond to outcomes [00, 01, 10, 11] Additional Information: See the Pauli measurement function `pauli_measurement_matrix` for an example. """ num_qubits = len(label) meas_ops = [] # Construct measurement POVM for all measurement outcomes for a given # measurement label. This will be a list of 2 ** n operators. for l in sorted(it.product((0, 1), repeat=num_qubits)): op = np.eye(1, dtype=complex) # Reverse label to correspond to QISKit bit ordering for m, outcome in zip(reversed(label), l): op = np.kron(op, meas_matrix_fn(m, outcome)) meas_ops.append(op) return meas_ops
[ "numpy.conj", "numpy.sum", "qiskit.QiskitError", "numpy.zeros", "numpy.vstack", "numpy.array", "scipy.linalg.eigh", "numpy.kron", "itertools.product", "numpy.eye", "logging.getLogger", "numpy.sqrt" ]
[((492, 519), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (509, 519), False, 'import logging\n'), ((5869, 5883), 'numpy.sum', 'np.sum', (['counts'], {}), '(counts)\n', (5875, 5883), True, 'import numpy as np\n'), ((6636, 6688), 'numpy.sqrt', 'np.sqrt', (['(shots / (freqs_hedged * (1 - freqs_hedged)))'], {}), '(shots / (freqs_hedged * (1 - freqs_hedged)))\n', (6643, 6688), True, 'import numpy as np\n'), ((7715, 7727), 'scipy.linalg.eigh', 'la.eigh', (['mat'], {}), '(mat)\n', (7722, 7727), True, 'from scipy import linalg as la\n'), ((8133, 8168), 'numpy.zeros', 'np.zeros', (['[dim, dim]'], {'dtype': 'complex'}), '([dim, dim], dtype=complex)\n', (8141, 8168), True, 'import numpy as np\n'), ((8900, 8940), 'numpy.zeros', 'np.zeros', (['(num_ops, size)'], {'dtype': 'complex'}), '((num_ops, size), dtype=complex)\n', (8908, 8940), True, 'import numpy as np\n'), ((9981, 10005), 'numpy.eye', 'np.eye', (['(1)'], {'dtype': 'complex'}), '(1, dtype=complex)\n', (9987, 10005), True, 'import numpy as np\n'), ((3653, 3664), 'numpy.sum', 'np.sum', (['cts'], {}), '(cts)\n', (3659, 3664), True, 'import numpy as np\n'), ((4363, 4386), 'numpy.vstack', 'np.vstack', (['basis_blocks'], {}), '(basis_blocks)\n', (4372, 4386), True, 'import numpy as np\n'), ((5722, 5763), 'numpy.array', 'np.array', (['[mcts[k] for k in ordered_keys]'], {}), '([mcts[k] for k in ordered_keys])\n', (5730, 5763), True, 'import numpy as np\n'), ((5840, 5856), 'numpy.array', 'np.array', (['counts'], {}), '(counts)\n', (5848, 5856), True, 'import numpy as np\n'), ((9911, 9935), 'numpy.eye', 'np.eye', (['(1)'], {'dtype': 'complex'}), '(1, dtype=complex)\n', (9917, 9935), True, 'import numpy as np\n'), ((11268, 11305), 'itertools.product', 'it.product', (['(0, 1)'], {'repeat': 'num_qubits'}), '((0, 1), repeat=num_qubits)\n', (11278, 11305), True, 'import itertools as it\n'), ((11321, 11345), 'numpy.eye', 'np.eye', (['(1)'], {'dtype': 'complex'}), '(1, dtype=complex)\n', (11327, 11345), True, 'import numpy as np\n'), ((3681, 3694), 'numpy.array', 'np.array', (['cts'], {}), '(cts)\n', (3689, 3694), True, 'import numpy as np\n'), ((2435, 2475), 'qiskit.QiskitError', 'QiskitError', (['"""Invalid measurement basis"""'], {}), "('Invalid measurement basis')\n", (2446, 2475), False, 'from qiskit import QiskitError\n'), ((2780, 2820), 'qiskit.QiskitError', 'QiskitError', (['"""Invalid preparation basis"""'], {}), "('Invalid preparation basis')\n", (2791, 2820), False, 'from qiskit import QiskitError\n'), ((4264, 4287), 'numpy.kron', 'np.kron', (['prep_op.T', 'mop'], {}), '(prep_op.T, mop)\n', (4271, 4287), True, 'import numpy as np\n'), ((8238, 8254), 'numpy.conj', 'np.conj', (['w[:, j]'], {}), '(w[:, j])\n', (8245, 8254), True, 'import numpy as np\n'), ((8992, 9003), 'numpy.array', 'np.array', (['b'], {}), '(b)\n', (9000, 9003), True, 'import numpy as np\n')]
import unittest import numpy as np from texar.torch.run.metric.summary import * class RegressionMetricTest(unittest.TestCase): def setUp(self) -> None: self.n_examples = 100 self.batch_size = 2 self.values = np.random.randn(self.n_examples) def test_running_average(self): queue_size = 10 metric = RunningAverage(queue_size) for idx in range(0, self.n_examples, self.batch_size): end_idx = idx + self.batch_size metric.add(self.values[idx:end_idx], None) value = metric.value() answer = self.values[max(0, end_idx - queue_size):end_idx].mean() self.assertAlmostEqual(value, answer)
[ "numpy.random.randn" ]
[((240, 272), 'numpy.random.randn', 'np.random.randn', (['self.n_examples'], {}), '(self.n_examples)\n', (255, 272), True, 'import numpy as np\n')]
from logging import getLogger import numpy as np from imblearn.over_sampling import SMOTE from sklearn.base import clone from ..utils import augmented_rvalue, BaseTransformer class MOS(BaseTransformer): """Perform Minimizing Overlapping Selection under SMOTE (MOSS) or under No-Sampling (MOSNS) algorithm. Parameters ---------- model : object The model that should have a fit(X, y) method and a field corresponding to feature weights. Currently only SGDClassifier should be passed, other models would not work. weight_func : callable The function to extract weights from the model. loss : str, 'log' or 'hinge' Loss function to use in the algorithm. 'log' gives a logistic regression, while 'hinge' gives a support vector machine. seed : int, optional Seed for python random. l1_ratio : float The value used to balance the L1 and L2 penalties in elastic-net. threshold : float The threshold value for feature dropout. Instead of comparing them to zero, they are normalized and values with absolute value lower than the threshold are dropped out. epochs : int The number of epochs to perform in the algorithm. alphas : array-like, shape (n_alphas,), optional The range of lambdas that should form the regularization path. sampling : bool Bool value that control whether MOSS (True) or MOSNS (False) should be executed. k_neighbors : int Amount of nearest neighbors to use in SMOTE if MOSS is used. Notes ----- For more details see `this paper <https://www.sciencedirect.com/science/article/pii/S0169743919306070/>`_. Examples -------- >>> from ITMO_FS.embedded import MOS >>> from sklearn.linear_model import SGDClassifier >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.linear_model import LogisticRegression >>> dataset = make_classification(n_samples=100, n_features=10, ... n_informative=5, n_redundant=0, weights=[0.85, 0.15], random_state=42, ... shuffle=False) >>> X, y = np.array(dataset[0]), np.array(dataset[1]) >>> m = MOS(model=SGDClassifier(), ... weight_func=lambda model: np.square(model.coef_).sum(axis=0)).fit(X, y) >>> m.selected_features_ array([1, 3, 4], dtype=int64) >>> m = MOS(model=SGDClassifier(), sampling=True, ... weight_func=lambda model: np.square(model.coef_).sum(axis=0)).fit(X, y) >>> m.selected_features_ array([1, 3, 4, 6], dtype=int64) """ def __init__(self, model, weight_func, loss='log', seed=42, l1_ratio=0.5, threshold=1e-3, epochs=1000, alphas=np.arange(0.01, 0.2, 0.01), sampling=False, k_neighbors=2): self.model = model self.weight_func = weight_func self.loss = loss self.seed = seed self.l1_ratio = l1_ratio self.threshold = threshold self.epochs = epochs self.alphas = alphas self.sampling = sampling self.k_neighbors = k_neighbors def _fit(self, X, y): """Run the MOS algorithm on the specified dataset. Parameters ---------- X : array-like, shape (n_samples, n_features) The input samples. y : array-like, shape (n_samples,) The classes for the samples. Returns ------- None """ if self.loss not in ['hinge', 'log']: getLogger(__name__).error( "Loss should be 'hinge' or 'log', %s was passed", self.loss) raise KeyError( "Loss should be 'hinge' or 'log', %s was passed" % self.loss) if self.sampling: try: X, y = SMOTE( random_state=self.seed, k_neighbors=self.k_neighbors).fit_resample(X, y) except ValueError: getLogger(__name__).warning( "Couldn't perform SMOTE because k_neighbors is bigger " "than amount of instances in one of the classes; MOSNS " "would be performed instead") min_rvalue = 1 min_b = [] model = clone(self.model) for a in self.alphas: # TODO: do a little more # research on the range of lambdas model = model.set_params( loss=self.loss, random_state=self.seed, penalty='elasticnet', alpha=a, l1_ratio=self.l1_ratio, max_iter=self.epochs) model.fit(X, y) b = self.weight_func(model) rvalue = augmented_rvalue( X[:, np.flatnonzero(np.abs(b) > self.threshold)], y) getLogger(__name__).info( "For alpha %f: rvalue = %f, weight vector = %s", a, rvalue, b) if min_rvalue > rvalue: min_rvalue = rvalue min_b = b getLogger(__name__).info("New minimum rvalue: %f", rvalue) getLogger(__name__).info("New weight vector: %s", b) self.selected_features_ = np.flatnonzero(np.abs(min_b) > self.threshold)
[ "numpy.abs", "numpy.arange", "imblearn.over_sampling.SMOTE", "sklearn.base.clone", "logging.getLogger" ]
[((2738, 2764), 'numpy.arange', 'np.arange', (['(0.01)', '(0.2)', '(0.01)'], {}), '(0.01, 0.2, 0.01)\n', (2747, 2764), True, 'import numpy as np\n'), ((4274, 4291), 'sklearn.base.clone', 'clone', (['self.model'], {}), '(self.model)\n', (4279, 4291), False, 'from sklearn.base import clone\n'), ((5166, 5179), 'numpy.abs', 'np.abs', (['min_b'], {}), '(min_b)\n', (5172, 5179), True, 'import numpy as np\n'), ((3539, 3558), 'logging.getLogger', 'getLogger', (['__name__'], {}), '(__name__)\n', (3548, 3558), False, 'from logging import getLogger\n'), ((4770, 4789), 'logging.getLogger', 'getLogger', (['__name__'], {}), '(__name__)\n', (4779, 4789), False, 'from logging import getLogger\n'), ((3816, 3875), 'imblearn.over_sampling.SMOTE', 'SMOTE', ([], {'random_state': 'self.seed', 'k_neighbors': 'self.k_neighbors'}), '(random_state=self.seed, k_neighbors=self.k_neighbors)\n', (3821, 3875), False, 'from imblearn.over_sampling import SMOTE\n'), ((4989, 5008), 'logging.getLogger', 'getLogger', (['__name__'], {}), '(__name__)\n', (4998, 5008), False, 'from logging import getLogger\n'), ((5064, 5083), 'logging.getLogger', 'getLogger', (['__name__'], {}), '(__name__)\n', (5073, 5083), False, 'from logging import getLogger\n'), ((3983, 4002), 'logging.getLogger', 'getLogger', (['__name__'], {}), '(__name__)\n', (3992, 4002), False, 'from logging import getLogger\n'), ((4725, 4734), 'numpy.abs', 'np.abs', (['b'], {}), '(b)\n', (4731, 4734), True, 'import numpy as np\n')]
import pylab as p from flask import Blueprint,render_template,request from flask_login import login_required, current_user from . import db from keras.models import load_model from keras.preprocessing import image import numpy as np import cv2 main=Blueprint('main',__name__) model=load_model('D:\Shubham\Alzhiemer-Prediction\project\model.h5') CATEGORIES = ["MildDemented", "ModerateDemented","NonDemented","VeryMildDemented"] def predict_label(image_name): image_path='D:/Shubham/Alzhiemer-Prediction/project/static/alzheimer-images/' +image_name image = cv2.imread(image_path, cv2.COLOR_BGR2GRAY) image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) image = np.array(image) image = image.astype('float32') image /= 255 image = np.stack((image,) * 3, axis=-1) image = image.reshape(-1, 224, 224, 3) result=model.predict(image) return result @main.route('/') def home(): return render_template('home.html') @main.route('/prediction') # @login_required def prediction(): return render_template('prediction.html', name='current_user.name') @main.route('/prediction', methods=['POST']) def predict(): img=request.files['image'] imgpath='./project/static/alzheimer-images/'+img.filename img.save(imgpath) p=predict_label(img.filename) classes=np.argmax(p) print("Disease name ", CATEGORIES[classes]) return render_template('prediction.html', result=CATEGORIES[classes], image='alzheimer-images/'+img.filename)
[ "keras.models.load_model", "numpy.stack", "flask.Blueprint", "numpy.argmax", "cv2.imread", "numpy.array", "keras.preprocessing.image.astype", "keras.preprocessing.image.reshape", "flask.render_template", "cv2.resize" ]
[((259, 286), 'flask.Blueprint', 'Blueprint', (['"""main"""', '__name__'], {}), "('main', __name__)\n", (268, 286), False, 'from flask import Blueprint, render_template, request\n'), ((295, 361), 'keras.models.load_model', 'load_model', (['"""D:\\\\Shubham\\\\Alzhiemer-Prediction\\\\project\\\\model.h5"""'], {}), "('D:\\\\Shubham\\\\Alzhiemer-Prediction\\\\project\\\\model.h5')\n", (305, 361), False, 'from keras.models import load_model\n'), ((586, 628), 'cv2.imread', 'cv2.imread', (['image_path', 'cv2.COLOR_BGR2GRAY'], {}), '(image_path, cv2.COLOR_BGR2GRAY)\n', (596, 628), False, 'import cv2\n'), ((642, 701), 'cv2.resize', 'cv2.resize', (['image', '(224, 224)'], {'interpolation': 'cv2.INTER_AREA'}), '(image, (224, 224), interpolation=cv2.INTER_AREA)\n', (652, 701), False, 'import cv2\n'), ((715, 730), 'numpy.array', 'np.array', (['image'], {}), '(image)\n', (723, 730), True, 'import numpy as np\n'), ((744, 767), 'keras.preprocessing.image.astype', 'image.astype', (['"""float32"""'], {}), "('float32')\n", (756, 767), False, 'from keras.preprocessing import image\n'), ((799, 830), 'numpy.stack', 'np.stack', (['((image,) * 3)'], {'axis': '(-1)'}), '((image,) * 3, axis=-1)\n', (807, 830), True, 'import numpy as np\n'), ((844, 874), 'keras.preprocessing.image.reshape', 'image.reshape', (['(-1)', '(224)', '(224)', '(3)'], {}), '(-1, 224, 224, 3)\n', (857, 874), False, 'from keras.preprocessing import image\n'), ((974, 1002), 'flask.render_template', 'render_template', (['"""home.html"""'], {}), "('home.html')\n", (989, 1002), False, 'from flask import Blueprint, render_template, request\n'), ((1083, 1143), 'flask.render_template', 'render_template', (['"""prediction.html"""'], {'name': '"""current_user.name"""'}), "('prediction.html', name='current_user.name')\n", (1098, 1143), False, 'from flask import Blueprint, render_template, request\n'), ((1376, 1388), 'numpy.argmax', 'np.argmax', (['p'], {}), '(p)\n', (1385, 1388), True, 'import numpy as np\n'), ((1452, 1561), 'flask.render_template', 'render_template', (['"""prediction.html"""'], {'result': 'CATEGORIES[classes]', 'image': "('alzheimer-images/' + img.filename)"}), "('prediction.html', result=CATEGORIES[classes], image=\n 'alzheimer-images/' + img.filename)\n", (1467, 1561), False, 'from flask import Blueprint, render_template, request\n')]
# <Copyright 2019, Argo AI, LLC. Released under the MIT license.> from typing import List, Optional import numpy as np from argoverse.utils import mayavi_wrapper from argoverse.utils.mesh_grid import get_mesh_grid_as_point_cloud from argoverse.visualization.mayavi_utils import ( Figure, draw_mayavi_line_segment, plot_3d_clipped_bbox_mayavi, plot_points_3D_mayavi, ) def populate_frustum_voxels(planes: List[np.ndarray], fig: Figure, axis_pair: str) -> Figure: """ Generate grid in xy plane, and then treat it as grid in xz (ground) plane in camera coordinate system. Args: planes: list of length 5. Each list element is a Numpy array of shape (4,) representing the equation of a plane, e.g. (a,b,c,d) in ax+by+cz=d fig: Mayavi figure to draw on axis_pair: Either "xz" or "yz" Returns: Mayavi figure """ sparse_xz_voxel_grid = get_mesh_grid_as_point_cloud(-20, 20, 0, 40, downsample_factor=0.1) sparse_voxel_grid = np.zeros((sparse_xz_voxel_grid.shape[0], 3)) if axis_pair == "xz": sparse_voxel_grid[:, 0] = sparse_xz_voxel_grid[:, 0] sparse_voxel_grid[:, 2] = sparse_xz_voxel_grid[:, 1] elif axis_pair == "yz": sparse_voxel_grid[:, 1] = sparse_xz_voxel_grid[:, 0] sparse_voxel_grid[:, 2] = sparse_xz_voxel_grid[:, 1] # keep only the points that have signed distance > 0 (inside the frustum, plane # normals also point into the frustum) for plane in planes: signed_d = np.matmul(sparse_voxel_grid, plane[:3]) + plane[3] sparse_voxel_grid = sparse_voxel_grid[np.where(signed_d > 0)] plot_points_3D_mayavi(sparse_voxel_grid, fig, fixed_color=(1, 0, 0)) return fig def plot_frustum_planes_and_normals( planes: List[np.ndarray], cuboid_verts: Optional[np.ndarray] = None, near_clip_dist: float = 0.5, ) -> None: """ Args: planes: list of length 5. Each list element is a Numpy array of shape (4,) representing the equation of a plane, e.g. (a,b,c,d) in ax+by+cz=d cuboid_verts: Numpy array of shape (N,3) representing cuboid vertices Returns: None """ fig = mayavi_wrapper.mlab.figure(bgcolor=(1, 1, 1), size=(2000, 1000)) # type: ignore if cuboid_verts is not None: # fig = plot_bbox_3d_mayavi(fig, cuboid_verts) fig = plot_3d_clipped_bbox_mayavi(fig, planes, cuboid_verts) P = np.array([0.0, 0.0, 0.0]) for i, plane in enumerate(planes): (a, b, c, d) = plane if i == 0: color = (1, 0, 0) # red left elif i == 1: color = (0, 0, 1) # blue right elif i == 2: color = (1, 1, 0) # near yellow P = np.array([0.0, 0.0, near_clip_dist]) elif i == 3: color = (0, 1, 0) # low is green elif i == 4: color = (0, 1, 1) # top is teal plane_pts = generate_grid_on_plane(a, b, c, d, P) fig = plot_points_3D_mayavi(plane_pts, fig, color) # plot the normals at (0,0,0.5) and normal vector (u,v,w) given by (a,b,c) mayavi_wrapper.mlab.quiver3d( # type: ignore 0, 0, 0.5, a * 1000, b * 1000, c * 1000, color=color, figure=fig, line_width=8, ) # draw teal line at top below the camera pt1 = np.array([-5, 0, -5]) pt2 = np.array([5, 0, -5]) color = (0, 1, 1) draw_mayavi_line_segment(fig, [pt1, pt2], color=color, line_width=8) # draw blue line in middle pt1 = np.array([-5, 5, -5]) pt2 = np.array([5, 5, -5]) color = (0, 0, 1) draw_mayavi_line_segment(fig, [pt1, pt2], color=color, line_width=8) # draw yellow, lowest line (+y axis is down) pt1 = np.array([-5, 10, -5]) pt2 = np.array([5, 10, -5]) color = (1, 1, 0) draw_mayavi_line_segment(fig, [pt1, pt2], color=color, line_width=8) fig = populate_frustum_voxels(planes, fig, "xz") fig = populate_frustum_voxels(planes, fig, "yz") mayavi_wrapper.mlab.view(distance=200) # type: ignore mayavi_wrapper.mlab.show() # type: ignore def get_perpendicular(n: np.ndarray) -> np.ndarray: """ n guarantees that dot(n, getPerpendicular(n)) is zero, which is the orthogonality condition, while also keeping the magnitude of the vector as high as possible. Note that setting the component with the smallest magnitude to 0 also guarantees that you don't get a 0,0,0 vector as a result, unless that is already your input. Args: n: Numpy array of shape (3,) Returns: result: Numpy array of shape (3,) """ # find smallest component i = np.argmin(n) # get the other two indices a = (i + 1) % 3 b = (i + 2) % 3 result = np.zeros(3) result[i] = 0.0 result[a] = n[b] result[b] = -n[a] return result def generate_grid_on_plane(a: float, b: float, c: float, d: float, P: np.ndarray, radius: float = 15) -> np.ndarray: """ Args: a,b,c,d: Coefficients of ``ax + by + cz = d`` defining plane P: Numpy array of shape (3,) representing point on the plane radius: Radius (default 15) Returns: plane_pts: Numpy array of shape (N,3) with points on the input plane """ n = np.array([a, b, c]) # a,b,c from your equation perp = get_perpendicular(n) u = perp / np.linalg.norm(perp) v = np.cross(u, n) N = 100 # delta and epsilon are floats: delta = radius / N # N is how many points you want max in one direction epsilon = delta * 0.5 n_pts = int((2 * radius + epsilon) / delta) pts = np.linspace(-radius, radius + epsilon, n_pts) plane_pts: List[float] = [] for y in pts: for x in pts: # if (x*x+y*y < radius*radius): # only in the circle: plane_pts += [P + x * u + y * v] # P is the point on the plane return np.array(plane_pts)
[ "argoverse.utils.mayavi_wrapper.mlab.show", "argoverse.visualization.mayavi_utils.plot_3d_clipped_bbox_mayavi", "argoverse.utils.mayavi_wrapper.mlab.view", "argoverse.utils.mayavi_wrapper.mlab.figure", "numpy.zeros", "numpy.cross", "numpy.argmin", "numpy.where", "numpy.array", "argoverse.visualization.mayavi_utils.plot_points_3D_mayavi", "argoverse.visualization.mayavi_utils.draw_mayavi_line_segment", "numpy.linspace", "argoverse.utils.mayavi_wrapper.mlab.quiver3d", "argoverse.utils.mesh_grid.get_mesh_grid_as_point_cloud", "numpy.linalg.norm", "numpy.matmul" ]
[((935, 1002), 'argoverse.utils.mesh_grid.get_mesh_grid_as_point_cloud', 'get_mesh_grid_as_point_cloud', (['(-20)', '(20)', '(0)', '(40)'], {'downsample_factor': '(0.1)'}), '(-20, 20, 0, 40, downsample_factor=0.1)\n', (963, 1002), False, 'from argoverse.utils.mesh_grid import get_mesh_grid_as_point_cloud\n'), ((1027, 1071), 'numpy.zeros', 'np.zeros', (['(sparse_xz_voxel_grid.shape[0], 3)'], {}), '((sparse_xz_voxel_grid.shape[0], 3))\n', (1035, 1071), True, 'import numpy as np\n'), ((1669, 1737), 'argoverse.visualization.mayavi_utils.plot_points_3D_mayavi', 'plot_points_3D_mayavi', (['sparse_voxel_grid', 'fig'], {'fixed_color': '(1, 0, 0)'}), '(sparse_voxel_grid, fig, fixed_color=(1, 0, 0))\n', (1690, 1737), False, 'from argoverse.visualization.mayavi_utils import Figure, draw_mayavi_line_segment, plot_3d_clipped_bbox_mayavi, plot_points_3D_mayavi\n'), ((2240, 2304), 'argoverse.utils.mayavi_wrapper.mlab.figure', 'mayavi_wrapper.mlab.figure', ([], {'bgcolor': '(1, 1, 1)', 'size': '(2000, 1000)'}), '(bgcolor=(1, 1, 1), size=(2000, 1000))\n', (2266, 2304), False, 'from argoverse.utils import mayavi_wrapper\n'), ((2488, 2513), 'numpy.array', 'np.array', (['[0.0, 0.0, 0.0]'], {}), '([0.0, 0.0, 0.0])\n', (2496, 2513), True, 'import numpy as np\n'), ((3469, 3490), 'numpy.array', 'np.array', (['[-5, 0, -5]'], {}), '([-5, 0, -5])\n', (3477, 3490), True, 'import numpy as np\n'), ((3501, 3521), 'numpy.array', 'np.array', (['[5, 0, -5]'], {}), '([5, 0, -5])\n', (3509, 3521), True, 'import numpy as np\n'), ((3549, 3617), 'argoverse.visualization.mayavi_utils.draw_mayavi_line_segment', 'draw_mayavi_line_segment', (['fig', '[pt1, pt2]'], {'color': 'color', 'line_width': '(8)'}), '(fig, [pt1, pt2], color=color, line_width=8)\n', (3573, 3617), False, 'from argoverse.visualization.mayavi_utils import Figure, draw_mayavi_line_segment, plot_3d_clipped_bbox_mayavi, plot_points_3D_mayavi\n'), ((3660, 3681), 'numpy.array', 'np.array', (['[-5, 5, -5]'], {}), '([-5, 5, -5])\n', (3668, 3681), True, 'import numpy as np\n'), ((3692, 3712), 'numpy.array', 'np.array', (['[5, 5, -5]'], {}), '([5, 5, -5])\n', (3700, 3712), True, 'import numpy as np\n'), ((3739, 3807), 'argoverse.visualization.mayavi_utils.draw_mayavi_line_segment', 'draw_mayavi_line_segment', (['fig', '[pt1, pt2]'], {'color': 'color', 'line_width': '(8)'}), '(fig, [pt1, pt2], color=color, line_width=8)\n', (3763, 3807), False, 'from argoverse.visualization.mayavi_utils import Figure, draw_mayavi_line_segment, plot_3d_clipped_bbox_mayavi, plot_points_3D_mayavi\n'), ((3868, 3890), 'numpy.array', 'np.array', (['[-5, 10, -5]'], {}), '([-5, 10, -5])\n', (3876, 3890), True, 'import numpy as np\n'), ((3901, 3922), 'numpy.array', 'np.array', (['[5, 10, -5]'], {}), '([5, 10, -5])\n', (3909, 3922), True, 'import numpy as np\n'), ((3949, 4017), 'argoverse.visualization.mayavi_utils.draw_mayavi_line_segment', 'draw_mayavi_line_segment', (['fig', '[pt1, pt2]'], {'color': 'color', 'line_width': '(8)'}), '(fig, [pt1, pt2], color=color, line_width=8)\n', (3973, 4017), False, 'from argoverse.visualization.mayavi_utils import Figure, draw_mayavi_line_segment, plot_3d_clipped_bbox_mayavi, plot_points_3D_mayavi\n'), ((4130, 4168), 'argoverse.utils.mayavi_wrapper.mlab.view', 'mayavi_wrapper.mlab.view', ([], {'distance': '(200)'}), '(distance=200)\n', (4154, 4168), False, 'from argoverse.utils import mayavi_wrapper\n'), ((4189, 4215), 'argoverse.utils.mayavi_wrapper.mlab.show', 'mayavi_wrapper.mlab.show', ([], {}), '()\n', (4213, 4215), False, 'from argoverse.utils import mayavi_wrapper\n'), ((4788, 4800), 'numpy.argmin', 'np.argmin', (['n'], {}), '(n)\n', (4797, 4800), True, 'import numpy as np\n'), ((4888, 4899), 'numpy.zeros', 'np.zeros', (['(3)'], {}), '(3)\n', (4896, 4899), True, 'import numpy as np\n'), ((5399, 5418), 'numpy.array', 'np.array', (['[a, b, c]'], {}), '([a, b, c])\n', (5407, 5418), True, 'import numpy as np\n'), ((5523, 5537), 'numpy.cross', 'np.cross', (['u', 'n'], {}), '(u, n)\n', (5531, 5537), True, 'import numpy as np\n'), ((5749, 5794), 'numpy.linspace', 'np.linspace', (['(-radius)', '(radius + epsilon)', 'n_pts'], {}), '(-radius, radius + epsilon, n_pts)\n', (5760, 5794), True, 'import numpy as np\n'), ((6022, 6041), 'numpy.array', 'np.array', (['plane_pts'], {}), '(plane_pts)\n', (6030, 6041), True, 'import numpy as np\n'), ((2424, 2478), 'argoverse.visualization.mayavi_utils.plot_3d_clipped_bbox_mayavi', 'plot_3d_clipped_bbox_mayavi', (['fig', 'planes', 'cuboid_verts'], {}), '(fig, planes, cuboid_verts)\n', (2451, 2478), False, 'from argoverse.visualization.mayavi_utils import Figure, draw_mayavi_line_segment, plot_3d_clipped_bbox_mayavi, plot_points_3D_mayavi\n'), ((3033, 3077), 'argoverse.visualization.mayavi_utils.plot_points_3D_mayavi', 'plot_points_3D_mayavi', (['plane_pts', 'fig', 'color'], {}), '(plane_pts, fig, color)\n', (3054, 3077), False, 'from argoverse.visualization.mayavi_utils import Figure, draw_mayavi_line_segment, plot_3d_clipped_bbox_mayavi, plot_points_3D_mayavi\n'), ((3169, 3282), 'argoverse.utils.mayavi_wrapper.mlab.quiver3d', 'mayavi_wrapper.mlab.quiver3d', (['(0)', '(0)', '(0.5)', '(a * 1000)', '(b * 1000)', '(c * 1000)'], {'color': 'color', 'figure': 'fig', 'line_width': '(8)'}), '(0, 0, 0.5, a * 1000, b * 1000, c * 1000, color\n =color, figure=fig, line_width=8)\n', (3197, 3282), False, 'from argoverse.utils import mayavi_wrapper\n'), ((5494, 5514), 'numpy.linalg.norm', 'np.linalg.norm', (['perp'], {}), '(perp)\n', (5508, 5514), True, 'import numpy as np\n'), ((1543, 1582), 'numpy.matmul', 'np.matmul', (['sparse_voxel_grid', 'plane[:3]'], {}), '(sparse_voxel_grid, plane[:3])\n', (1552, 1582), True, 'import numpy as np\n'), ((1640, 1662), 'numpy.where', 'np.where', (['(signed_d > 0)'], {}), '(signed_d > 0)\n', (1648, 1662), True, 'import numpy as np\n'), ((2790, 2826), 'numpy.array', 'np.array', (['[0.0, 0.0, near_clip_dist]'], {}), '([0.0, 0.0, near_clip_dist])\n', (2798, 2826), True, 'import numpy as np\n')]
import numpy as np from pyloras._common import ( check_random_state, safe_random_state, ) def test_check_random_state(): rand = np.random.RandomState(12345) assert isinstance(check_random_state(rand), np.random.Generator) gen = np.random.default_rng(12345) assert isinstance(check_random_state(gen), np.random.Generator) def test_safe_random_state(): rand = np.random.RandomState(12345) assert isinstance(safe_random_state(rand), np.random.RandomState) gen = np.random.default_rng(12345) assert isinstance(safe_random_state(gen), np.random.RandomState)
[ "numpy.random.default_rng", "pyloras._common.check_random_state", "numpy.random.RandomState", "pyloras._common.safe_random_state" ]
[((143, 171), 'numpy.random.RandomState', 'np.random.RandomState', (['(12345)'], {}), '(12345)\n', (164, 171), True, 'import numpy as np\n'), ((251, 279), 'numpy.random.default_rng', 'np.random.default_rng', (['(12345)'], {}), '(12345)\n', (272, 279), True, 'import numpy as np\n'), ((391, 419), 'numpy.random.RandomState', 'np.random.RandomState', (['(12345)'], {}), '(12345)\n', (412, 419), True, 'import numpy as np\n'), ((500, 528), 'numpy.random.default_rng', 'np.random.default_rng', (['(12345)'], {}), '(12345)\n', (521, 528), True, 'import numpy as np\n'), ((194, 218), 'pyloras._common.check_random_state', 'check_random_state', (['rand'], {}), '(rand)\n', (212, 218), False, 'from pyloras._common import check_random_state, safe_random_state\n'), ((302, 325), 'pyloras._common.check_random_state', 'check_random_state', (['gen'], {}), '(gen)\n', (320, 325), False, 'from pyloras._common import check_random_state, safe_random_state\n'), ((442, 465), 'pyloras._common.safe_random_state', 'safe_random_state', (['rand'], {}), '(rand)\n', (459, 465), False, 'from pyloras._common import check_random_state, safe_random_state\n'), ((551, 573), 'pyloras._common.safe_random_state', 'safe_random_state', (['gen'], {}), '(gen)\n', (568, 573), False, 'from pyloras._common import check_random_state, safe_random_state\n')]
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified). __all__ = ['say_hello', 'HelloSayer', 'bubble_scatter'] # Cell import numpy as np import matplotlib.pyplot as plt # Cell def say_hello(to): "Say hello to anybody" return f'Hello {to}!' # Cell class HelloSayer: "Say hello to `to` using `say_hello`" def __init__(self, to): self.to = to def say(self): return say_hello(self.to) # Cell def bubble_scatter(n): "Create a scatter plot of n different sized bubbles" np.random.seed(19680801) x = np.random.rand(n) y = np.random.rand(n) colors = np.random.rand(n) area = (30 * np.random.rand(n))**2 # 0 to 15 point radii plt.scatter(x, y, s=area, c=colors, alpha=0.5) plt.show()
[ "numpy.random.rand", "matplotlib.pyplot.scatter", "numpy.random.seed", "matplotlib.pyplot.show" ]
[((526, 550), 'numpy.random.seed', 'np.random.seed', (['(19680801)'], {}), '(19680801)\n', (540, 550), True, 'import numpy as np\n'), ((557, 574), 'numpy.random.rand', 'np.random.rand', (['n'], {}), '(n)\n', (571, 574), True, 'import numpy as np\n'), ((581, 598), 'numpy.random.rand', 'np.random.rand', (['n'], {}), '(n)\n', (595, 598), True, 'import numpy as np\n'), ((610, 627), 'numpy.random.rand', 'np.random.rand', (['n'], {}), '(n)\n', (624, 627), True, 'import numpy as np\n'), ((691, 737), 'matplotlib.pyplot.scatter', 'plt.scatter', (['x', 'y'], {'s': 'area', 'c': 'colors', 'alpha': '(0.5)'}), '(x, y, s=area, c=colors, alpha=0.5)\n', (702, 737), True, 'import matplotlib.pyplot as plt\n'), ((740, 750), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (748, 750), True, 'import matplotlib.pyplot as plt\n'), ((643, 660), 'numpy.random.rand', 'np.random.rand', (['n'], {}), '(n)\n', (657, 660), True, 'import numpy as np\n')]
import os import html import numpy as np import pandas as pd import tensorflow as tf from sklearn.linear_model import LogisticRegression def train_with_reg_cv(trX, trY, vaX, vaY, teX=None, teY=None, penalty='l1', C=2 ** np.arange(-8, 1).astype(np.float), seed=42): scores = [] for i, c in enumerate(C): model = LogisticRegression(C=c, penalty=penalty, random_state=seed + i) model.fit(trX, trY) score = model.score(vaX, vaY) scores.append(score) c = C[np.argmax(scores)] model = LogisticRegression(C=c, penalty=penalty, random_state=seed + len(C)) model.fit(trX, trY) nnotzero = np.sum(model.coef_ != 0) if teX is not None and teY is not None: score = model.score(teX, teY) * 100. else: score = model.score(vaX, vaY) * 100. return score, c, nnotzero, model def load_sst(path): data = pd.read_csv(path) X = data['sentence'].values.tolist() Y = data['label'].values return X, Y def sst_binary(data_dir='character_rnn/data/'): """ Most standard models make use of a preprocessed/tokenized/lowercased version of Stanford Sentiment Treebank. Our model extracts features from a version of the dataset using the raw text instead which we've included in the data folder. """ trX, trY = load_sst(os.path.join(data_dir, 'train_binary_sent.csv')) vaX, vaY = load_sst(os.path.join(data_dir, 'dev_binary_sent.csv')) teX, teY = load_sst(os.path.join(data_dir, 'test_binary_sent.csv')) return trX, vaX, teX, trY, vaY, teY def find_trainable_variables(key): return tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, ".*{}.*".format(key)) def preprocess(text, front_pad='\n ', end_pad=' '): text = html.unescape(text) text = text.replace('\n', ' ').strip() text = front_pad + text + end_pad text = text.encode() return text def iter_data(*data, **kwargs): size = kwargs.get('size', 128) try: n = len(data[0]) except: n = data[0].shape[0] batches = n // size if n % size != 0: batches += 1 for b in range(batches): start = b * size end = (b + 1) * size if end > n: end = n if len(data) == 1: yield data[0][start:end] else: yield tuple([d[start:end] for d in data]) class HParams(object): def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v)
[ "html.unescape", "numpy.sum", "numpy.argmax", "pandas.read_csv", "sklearn.linear_model.LogisticRegression", "numpy.arange", "os.path.join" ]
[((659, 683), 'numpy.sum', 'np.sum', (['(model.coef_ != 0)'], {}), '(model.coef_ != 0)\n', (665, 683), True, 'import numpy as np\n'), ((898, 915), 'pandas.read_csv', 'pd.read_csv', (['path'], {}), '(path)\n', (909, 915), True, 'import pandas as pd\n'), ((1771, 1790), 'html.unescape', 'html.unescape', (['text'], {}), '(text)\n', (1784, 1790), False, 'import html\n'), ((351, 414), 'sklearn.linear_model.LogisticRegression', 'LogisticRegression', ([], {'C': 'c', 'penalty': 'penalty', 'random_state': '(seed + i)'}), '(C=c, penalty=penalty, random_state=seed + i)\n', (369, 414), False, 'from sklearn.linear_model import LogisticRegression\n'), ((520, 537), 'numpy.argmax', 'np.argmax', (['scores'], {}), '(scores)\n', (529, 537), True, 'import numpy as np\n'), ((1343, 1390), 'os.path.join', 'os.path.join', (['data_dir', '"""train_binary_sent.csv"""'], {}), "(data_dir, 'train_binary_sent.csv')\n", (1355, 1390), False, 'import os\n'), ((1416, 1461), 'os.path.join', 'os.path.join', (['data_dir', '"""dev_binary_sent.csv"""'], {}), "(data_dir, 'dev_binary_sent.csv')\n", (1428, 1461), False, 'import os\n'), ((1487, 1533), 'os.path.join', 'os.path.join', (['data_dir', '"""test_binary_sent.csv"""'], {}), "(data_dir, 'test_binary_sent.csv')\n", (1499, 1533), False, 'import os\n'), ((244, 260), 'numpy.arange', 'np.arange', (['(-8)', '(1)'], {}), '(-8, 1)\n', (253, 260), True, 'import numpy as np\n')]
from __future__ import annotations import logging from math import floor, sqrt import numpy as np from numpy.linalg import inv, norm from cctbx.array_family import flex from dxtbx import flumpy from scitbx import matrix from dials.algorithms.profile_model.ellipsoid import chisq_quantile from dials.algorithms.statistics.fast_mcd import FastMCD, maha_dist_sq logger = logging.getLogger("dials") def _index(reflection_table, experiment, fail_on_bad_index=False): """Index the strong spots""" # Get some stuff from experiment A = np.array(experiment.crystal.get_A(), dtype=np.float64).reshape(3, 3) s0 = np.array([experiment.beam.get_s0()], dtype=np.float64).reshape(3, 1) s0_length = norm(s0) detector = experiment.detector # Create array if necessary if "miller_index" not in reflection_table: reflection_table["miller_index"] = flex.miller_index(len(reflection_table)) # Index all the reflections miller_index = reflection_table["miller_index"] selection = flex.size_t() num_reindexed = 0 for i, xyz in enumerate(reflection_table["xyzobs.px.value"]): # Get the observed pixel coordinate x, y, _ = xyz # Get the lab coord s1 = np.array( detector[0].get_pixel_lab_coord((x, y)), dtype=np.float64 ).reshape(3, 1) s1_norm = norm(s1) s1 *= s0_length / s1_norm # Get the reciprocal lattice vector r = s1 - s0 # Compute the fractional miller index hf = np.matmul(inv(A), r) # Compute the integer miller index h = np.array([int(floor(j + 0.5)) for j in hf[:, 0]], dtype=int).reshape(3, 1) # Print warning if reindexing if tuple(h) != miller_index[i]: logger.warn( "Reindexing (% 3d, % 3d, % 3d) -> (% 3d, % 3d, % 3d)" % (miller_index[i] + tuple(h)) ) num_reindexed += 1 miller_index[i] = matrix.col(flumpy.from_numpy(h)) if fail_on_bad_index: raise RuntimeError("Bad index") # If its not indexed as 0, 0, 0 then append if h.any() and norm(h - hf) < 0.3: selection.append(i) # Print some info logger.info( "Reindexed %d/%d input reflections" % (num_reindexed, len(reflection_table)) ) logger.info( "Selected %d/%d input reflections" % (len(selection), len(reflection_table)) ) # Select all the indexed reflections reflection_table.set_flags(selection, reflection_table.flags.indexed) reflection_table = reflection_table.select(selection) return reflection_table def _predict(reflection_table, experiment): """ Predict the position of the spots """ # Get some stuff from experiment A = np.array(experiment.crystal.get_A(), dtype=np.float64).reshape((3, 3)) s0 = np.array([experiment.beam.get_s0()], dtype=np.float64).reshape(3, 1) s0_length = norm(s0) # Compute the vector to the reciprocal lattice point # since this is not on the ewald sphere, lets call it s2 s1 = flex.vec3_double(reflection_table.size()) s2 = flex.vec3_double(reflection_table.size()) for i, h in enumerate(reflection_table["miller_index"]): r = np.matmul(A, np.array([h], dtype=np.float64).reshape(3, 1)) s2_i = r + s0 s2[i] = matrix.col(flumpy.from_numpy(s2_i)) s1[i] = matrix.col(flumpy.from_numpy(s2_i * s0_length / norm(s2_i))) reflection_table["s1"] = s1 reflection_table["s2"] = s2 reflection_table["entering"] = flex.bool(reflection_table.size(), False) # Compute the ray intersections xyzpx = flex.vec3_double() xyzmm = flex.vec3_double() for ss in s1: mm = experiment.detector[0].get_ray_intersection(ss) px = experiment.detector[0].millimeter_to_pixel(mm) xyzpx.append(px + (0,)) xyzmm.append(mm + (0,)) reflection_table["xyzcal.mm"] = xyzmm reflection_table["xyzcal.px"] = xyzpx logger.info("Do prediction for %d reflections" % len(reflection_table)) return reflection_table def _filter_reflections_based_on_centroid_distance( reflection_table, experiment, outlier_probability=0.975, max_separation=2, ): """ Filter reflections too far from predicted position """ # Compute the x and y residuals Xobs, Yobs, _ = reflection_table["xyzobs.px.value"].parts() Xcal, Ycal, _ = reflection_table["xyzcal.px"].parts() Xres = Xobs - Xcal Yres = Yobs - Ycal # Compute the epsilon residual s0_length = 1.0 / experiment.beam.get_wavelength() s1x, s1y, s1z = reflection_table["s2"].parts() s1_length = flex.sqrt(s1x**2 + s1y**2 + s1z**2) Eres = s1_length - s0_length # Initialise the fast_mcd outlier algorithm # fast_mcd = FastMCD((Xres, Yres, Eres)) fast_mcd = FastMCD((Xres, Yres)) # get location and MCD scatter estimate T, S = fast_mcd.get_corrected_T_and_S() # get squared Mahalanobis distances # d2s = maha_dist_sq((Xres, Yres, Eres), T, S) d2s = maha_dist_sq((Xres, Yres), T, S) # Compute the cutoff mahasq_cutoff = chisq_quantile(2, outlier_probability) # compare to the threshold and select reflections selection1 = d2s < mahasq_cutoff selection2 = flex.sqrt(Xres**2 + Yres**2) < max_separation selection = selection1 & selection2 reflection_table = reflection_table.select(selection) n_refl = reflection_table.size() # Print some stuff logger.info("-" * 80) logger.info("Centroid outlier rejection") logger.info(f" Using MCD algorithm with probability = {outlier_probability}") logger.info(" Max X residual: %f" % flex.max(flex.abs(Xres))) logger.info(" Max Y residual: %f" % flex.max(flex.abs(Yres))) logger.info(" Max E residual: %f" % flex.max(flex.abs(Eres))) logger.info(" Mean X RMSD: %f" % (sqrt(flex.sum(Xres**2) / len(Xres)))) logger.info(" Mean Y RMSD: %f" % (sqrt(flex.sum(Yres**2) / len(Yres)))) logger.info(" Mean E RMSD: %f" % (sqrt(flex.sum(Eres**2) / len(Eres)))) logger.info(" MCD location estimate: %.4f, %.4f" % tuple(T)) logger.info( """ MCD scatter estimate: %.7f, %.7f, %.7f, %.7f""" % tuple(S) ) logger.info(" Number of outliers: %d" % selection1.count(False)) logger.info( " Number of reflections with residual > %0.2f pixels: %d" % (max_separation, selection2.count(False)) ) logger.info(f"Number of reflections selection for refinement: {n_refl}") logger.info("-" * 80) return reflection_table def reindex( reflection_table, experiment, outlier_probability=0.975, max_separation=2, fail_on_bad_index=False, ): """Reindex strong spots and perform filtering""" reflection_table = _index(reflection_table, experiment, fail_on_bad_index) reflection_table = _predict(reflection_table, experiment) reflection_table = _filter_reflections_based_on_centroid_distance( reflection_table, experiment, outlier_probability=outlier_probability, max_separation=max_separation, ) return reflection_table
[ "cctbx.array_family.flex.sqrt", "cctbx.array_family.flex.sum", "cctbx.array_family.flex.abs", "dials.algorithms.statistics.fast_mcd.FastMCD", "math.floor", "dials.algorithms.profile_model.ellipsoid.chisq_quantile", "cctbx.array_family.flex.vec3_double", "numpy.linalg.norm", "numpy.linalg.inv", "dxtbx.flumpy.from_numpy", "dials.algorithms.statistics.fast_mcd.maha_dist_sq", "numpy.array", "cctbx.array_family.flex.size_t", "logging.getLogger" ]
[((373, 399), 'logging.getLogger', 'logging.getLogger', (['"""dials"""'], {}), "('dials')\n", (390, 399), False, 'import logging\n'), ((711, 719), 'numpy.linalg.norm', 'norm', (['s0'], {}), '(s0)\n', (715, 719), False, 'from numpy.linalg import inv, norm\n'), ((1020, 1033), 'cctbx.array_family.flex.size_t', 'flex.size_t', ([], {}), '()\n', (1031, 1033), False, 'from cctbx.array_family import flex\n'), ((2962, 2970), 'numpy.linalg.norm', 'norm', (['s0'], {}), '(s0)\n', (2966, 2970), False, 'from numpy.linalg import inv, norm\n'), ((3666, 3684), 'cctbx.array_family.flex.vec3_double', 'flex.vec3_double', ([], {}), '()\n', (3682, 3684), False, 'from cctbx.array_family import flex\n'), ((3697, 3715), 'cctbx.array_family.flex.vec3_double', 'flex.vec3_double', ([], {}), '()\n', (3713, 3715), False, 'from cctbx.array_family import flex\n'), ((4690, 4731), 'cctbx.array_family.flex.sqrt', 'flex.sqrt', (['(s1x ** 2 + s1y ** 2 + s1z ** 2)'], {}), '(s1x ** 2 + s1y ** 2 + s1z ** 2)\n', (4699, 4731), False, 'from cctbx.array_family import flex\n'), ((4868, 4889), 'dials.algorithms.statistics.fast_mcd.FastMCD', 'FastMCD', (['(Xres, Yres)'], {}), '((Xres, Yres))\n', (4875, 4889), False, 'from dials.algorithms.statistics.fast_mcd import FastMCD, maha_dist_sq\n'), ((5081, 5113), 'dials.algorithms.statistics.fast_mcd.maha_dist_sq', 'maha_dist_sq', (['(Xres, Yres)', 'T', 'S'], {}), '((Xres, Yres), T, S)\n', (5093, 5113), False, 'from dials.algorithms.statistics.fast_mcd import FastMCD, maha_dist_sq\n'), ((5160, 5198), 'dials.algorithms.profile_model.ellipsoid.chisq_quantile', 'chisq_quantile', (['(2)', 'outlier_probability'], {}), '(2, outlier_probability)\n', (5174, 5198), False, 'from dials.algorithms.profile_model.ellipsoid import chisq_quantile\n'), ((1352, 1360), 'numpy.linalg.norm', 'norm', (['s1'], {}), '(s1)\n', (1356, 1360), False, 'from numpy.linalg import inv, norm\n'), ((5308, 5340), 'cctbx.array_family.flex.sqrt', 'flex.sqrt', (['(Xres ** 2 + Yres ** 2)'], {}), '(Xres ** 2 + Yres ** 2)\n', (5317, 5340), False, 'from cctbx.array_family import flex\n'), ((1529, 1535), 'numpy.linalg.inv', 'inv', (['A'], {}), '(A)\n', (1532, 1535), False, 'from numpy.linalg import inv, norm\n'), ((3374, 3397), 'dxtbx.flumpy.from_numpy', 'flumpy.from_numpy', (['s2_i'], {}), '(s2_i)\n', (3391, 3397), False, 'from dxtbx import flumpy\n'), ((1977, 1997), 'dxtbx.flumpy.from_numpy', 'flumpy.from_numpy', (['h'], {}), '(h)\n', (1994, 1997), False, 'from dxtbx import flumpy\n'), ((2157, 2169), 'numpy.linalg.norm', 'norm', (['(h - hf)'], {}), '(h - hf)\n', (2161, 2169), False, 'from numpy.linalg import inv, norm\n'), ((5716, 5730), 'cctbx.array_family.flex.abs', 'flex.abs', (['Xres'], {}), '(Xres)\n', (5724, 5730), False, 'from cctbx.array_family import flex\n'), ((5782, 5796), 'cctbx.array_family.flex.abs', 'flex.abs', (['Yres'], {}), '(Yres)\n', (5790, 5796), False, 'from cctbx.array_family import flex\n'), ((5848, 5862), 'cctbx.array_family.flex.abs', 'flex.abs', (['Eres'], {}), '(Eres)\n', (5856, 5862), False, 'from cctbx.array_family import flex\n'), ((3278, 3309), 'numpy.array', 'np.array', (['[h]'], {'dtype': 'np.float64'}), '([h], dtype=np.float64)\n', (3286, 3309), True, 'import numpy as np\n'), ((3463, 3473), 'numpy.linalg.norm', 'norm', (['s2_i'], {}), '(s2_i)\n', (3467, 3473), False, 'from numpy.linalg import inv, norm\n'), ((5908, 5927), 'cctbx.array_family.flex.sum', 'flex.sum', (['(Xres ** 2)'], {}), '(Xres ** 2)\n', (5916, 5927), False, 'from cctbx.array_family import flex\n'), ((5984, 6003), 'cctbx.array_family.flex.sum', 'flex.sum', (['(Yres ** 2)'], {}), '(Yres ** 2)\n', (5992, 6003), False, 'from cctbx.array_family import flex\n'), ((6060, 6079), 'cctbx.array_family.flex.sum', 'flex.sum', (['(Eres ** 2)'], {}), '(Eres ** 2)\n', (6068, 6079), False, 'from cctbx.array_family import flex\n'), ((1609, 1623), 'math.floor', 'floor', (['(j + 0.5)'], {}), '(j + 0.5)\n', (1614, 1623), False, 'from math import floor, sqrt\n')]
#!/usr/bin/env python import wfdb import hmm import numpy as np import matplotlib.pyplot as plt import pickle def preprocess(count = 30): print("Preprocessing") sig, fields = wfdb.srdsamp('data/mitdb/100') ecg = sig[:500000,0] diff = np.diff(ecg) emax = np.max(ecg) emin = np.min(ecg) count = count - 2 bins = [ emin + (emax - emin)/count * i for i in range(count+1) ] quantized = np.digitize(ecg, bins) dequant = np.empty(len(quantized)) # dmax = np.max(diff)-0.01 # dmin = np.min(diff)+0.01 # count = count - 2 # print(dmin,dmax) # bins = [ dmin + (dmax - dmin)/count * i for i in range(count+1) ] # print("bins",len(bins)) # quantized = np.digitize(diff, bins) # # dequant = np.empty(len(quantized)) running = 0 for i in range(len(dequant)): v = quantized[i] running += bins[v-1] dequant[i] = running return ecg,quantized,dequant,bins def dequant(quantized, bins): dequant = np.empty(len(quantized)) running = 0 for i in range(len(dequant)): v = quantized[i] running += bins[v-1] dequant[i] = running return dequant # def train(): # print("Model training") # states = 10 # model = hmm.random_model(states, count+2) # # model = hmm.baum_welch(quantized, model) def query(): print("Questions answering") hmm.probabilities(model, 5) def train(): symbols = 100 states = 30 e, q, d, bins = preprocess(symbols) model = hmm.random_model(states,symbols) gen = hmm.synthetic(model) sampl = [next(gen) for _ in range(1000)] plt.ion() plt.clf() plt.subplot(311) plt.imshow(model.transitions,interpolation='nearest', shape=model.transitions.shape) plt.subplot(312) plt.imshow(model.emissions,interpolation='nearest', shape=model.emissions.shape) plt.subplot(313) plt.plot(sampl) plt.show() plt.pause(0.001) i = 0 plt.savefig("out{}.png".format(i)) # try: step = 10000 sig = q length = len(sig) fro = 0 to = step print("\nIteration {}".format(i)) while True: with open("db_ecg.pickle","wb") as pfile: pickle.dump(model,pfile) print("batch from {} to {}".format(fro,to),end="\r") i+=1 if to >= length - 9*step: print("\nIteration {}".format(i)) fro = 0 to = step obs = [ ] # tmp_fro = fro # tmp_to = to # for x in range(8): # obs.append(sig[tmp_fro:tmp_to]) # tmp_fro += step # tmp_to += step o = sig[fro:to] fro += step to += step # for o in obs: # model = hmm.baum_welch(o,model) for i in range(100): model = hmm.baum_welch(o,model) gen = hmm.synthetic(model) sampl = [next(gen) for _ in range(1000)] # model = hmm.batch_baum_welch(obs,model) plt.clf() plt.subplot(311) plt.imshow(model.transitions,interpolation='nearest', shape=model.transitions.shape) plt.subplot(312) plt.imshow(model.emissions,interpolation='nearest', shape=model.emissions.shape) plt.subplot(313) plt.plot(sampl) plt.show() plt.pause(0.001) plt.savefig("out{}.png".format(i)) # except: # pass plt.ioff() plt.subplot(311) plt.imshow(model.transitions,interpolation='nearest', shape=model.transitions.shape) plt.subplot(312) plt.imshow(model.emissions,interpolation='nearest', shape=model.emissions.shape) plt.subplot(313) plt.plot(sampl) plt.show() return model, bins
[ "matplotlib.pyplot.subplot", "pickle.dump", "hmm.probabilities", "matplotlib.pyplot.show", "matplotlib.pyplot.plot", "matplotlib.pyplot.clf", "matplotlib.pyplot.ioff", "matplotlib.pyplot.imshow", "matplotlib.pyplot.ion", "numpy.max", "numpy.diff", "numpy.min", "hmm.random_model", "hmm.synthetic", "hmm.baum_welch", "wfdb.srdsamp", "numpy.digitize", "matplotlib.pyplot.pause" ]
[((186, 216), 'wfdb.srdsamp', 'wfdb.srdsamp', (['"""data/mitdb/100"""'], {}), "('data/mitdb/100')\n", (198, 216), False, 'import wfdb\n'), ((254, 266), 'numpy.diff', 'np.diff', (['ecg'], {}), '(ecg)\n', (261, 266), True, 'import numpy as np\n'), ((279, 290), 'numpy.max', 'np.max', (['ecg'], {}), '(ecg)\n', (285, 290), True, 'import numpy as np\n'), ((302, 313), 'numpy.min', 'np.min', (['ecg'], {}), '(ecg)\n', (308, 313), True, 'import numpy as np\n'), ((422, 444), 'numpy.digitize', 'np.digitize', (['ecg', 'bins'], {}), '(ecg, bins)\n', (433, 444), True, 'import numpy as np\n'), ((1394, 1421), 'hmm.probabilities', 'hmm.probabilities', (['model', '(5)'], {}), '(model, 5)\n', (1411, 1421), False, 'import hmm\n'), ((1523, 1556), 'hmm.random_model', 'hmm.random_model', (['states', 'symbols'], {}), '(states, symbols)\n', (1539, 1556), False, 'import hmm\n'), ((1566, 1586), 'hmm.synthetic', 'hmm.synthetic', (['model'], {}), '(model)\n', (1579, 1586), False, 'import hmm\n'), ((1637, 1646), 'matplotlib.pyplot.ion', 'plt.ion', ([], {}), '()\n', (1644, 1646), True, 'import matplotlib.pyplot as plt\n'), ((1651, 1660), 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), '()\n', (1658, 1660), True, 'import matplotlib.pyplot as plt\n'), ((1665, 1681), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(311)'], {}), '(311)\n', (1676, 1681), True, 'import matplotlib.pyplot as plt\n'), ((1686, 1776), 'matplotlib.pyplot.imshow', 'plt.imshow', (['model.transitions'], {'interpolation': '"""nearest"""', 'shape': 'model.transitions.shape'}), "(model.transitions, interpolation='nearest', shape=model.\n transitions.shape)\n", (1696, 1776), True, 'import matplotlib.pyplot as plt\n'), ((1775, 1791), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(312)'], {}), '(312)\n', (1786, 1791), True, 'import matplotlib.pyplot as plt\n'), ((1796, 1882), 'matplotlib.pyplot.imshow', 'plt.imshow', (['model.emissions'], {'interpolation': '"""nearest"""', 'shape': 'model.emissions.shape'}), "(model.emissions, interpolation='nearest', shape=model.emissions.\n shape)\n", (1806, 1882), True, 'import matplotlib.pyplot as plt\n'), ((1881, 1897), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(313)'], {}), '(313)\n', (1892, 1897), True, 'import matplotlib.pyplot as plt\n'), ((1902, 1917), 'matplotlib.pyplot.plot', 'plt.plot', (['sampl'], {}), '(sampl)\n', (1910, 1917), True, 'import matplotlib.pyplot as plt\n'), ((1922, 1932), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1930, 1932), True, 'import matplotlib.pyplot as plt\n'), ((1937, 1953), 'matplotlib.pyplot.pause', 'plt.pause', (['(0.001)'], {}), '(0.001)\n', (1946, 1953), True, 'import matplotlib.pyplot as plt\n'), ((3399, 3409), 'matplotlib.pyplot.ioff', 'plt.ioff', ([], {}), '()\n', (3407, 3409), True, 'import matplotlib.pyplot as plt\n'), ((3414, 3430), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(311)'], {}), '(311)\n', (3425, 3430), True, 'import matplotlib.pyplot as plt\n'), ((3435, 3525), 'matplotlib.pyplot.imshow', 'plt.imshow', (['model.transitions'], {'interpolation': '"""nearest"""', 'shape': 'model.transitions.shape'}), "(model.transitions, interpolation='nearest', shape=model.\n transitions.shape)\n", (3445, 3525), True, 'import matplotlib.pyplot as plt\n'), ((3524, 3540), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(312)'], {}), '(312)\n', (3535, 3540), True, 'import matplotlib.pyplot as plt\n'), ((3545, 3631), 'matplotlib.pyplot.imshow', 'plt.imshow', (['model.emissions'], {'interpolation': '"""nearest"""', 'shape': 'model.emissions.shape'}), "(model.emissions, interpolation='nearest', shape=model.emissions.\n shape)\n", (3555, 3631), True, 'import matplotlib.pyplot as plt\n'), ((3630, 3646), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(313)'], {}), '(313)\n', (3641, 3646), True, 'import matplotlib.pyplot as plt\n'), ((3651, 3666), 'matplotlib.pyplot.plot', 'plt.plot', (['sampl'], {}), '(sampl)\n', (3659, 3666), True, 'import matplotlib.pyplot as plt\n'), ((3671, 3681), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3679, 3681), True, 'import matplotlib.pyplot as plt\n'), ((2858, 2878), 'hmm.synthetic', 'hmm.synthetic', (['model'], {}), '(model)\n', (2871, 2878), False, 'import hmm\n'), ((2987, 2996), 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), '()\n', (2994, 2996), True, 'import matplotlib.pyplot as plt\n'), ((3005, 3021), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(311)'], {}), '(311)\n', (3016, 3021), True, 'import matplotlib.pyplot as plt\n'), ((3030, 3120), 'matplotlib.pyplot.imshow', 'plt.imshow', (['model.transitions'], {'interpolation': '"""nearest"""', 'shape': 'model.transitions.shape'}), "(model.transitions, interpolation='nearest', shape=model.\n transitions.shape)\n", (3040, 3120), True, 'import matplotlib.pyplot as plt\n'), ((3123, 3139), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(312)'], {}), '(312)\n', (3134, 3139), True, 'import matplotlib.pyplot as plt\n'), ((3148, 3234), 'matplotlib.pyplot.imshow', 'plt.imshow', (['model.emissions'], {'interpolation': '"""nearest"""', 'shape': 'model.emissions.shape'}), "(model.emissions, interpolation='nearest', shape=model.emissions.\n shape)\n", (3158, 3234), True, 'import matplotlib.pyplot as plt\n'), ((3237, 3253), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(313)'], {}), '(313)\n', (3248, 3253), True, 'import matplotlib.pyplot as plt\n'), ((3262, 3277), 'matplotlib.pyplot.plot', 'plt.plot', (['sampl'], {}), '(sampl)\n', (3270, 3277), True, 'import matplotlib.pyplot as plt\n'), ((3286, 3296), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3294, 3296), True, 'import matplotlib.pyplot as plt\n'), ((3305, 3321), 'matplotlib.pyplot.pause', 'plt.pause', (['(0.001)'], {}), '(0.001)\n', (3314, 3321), True, 'import matplotlib.pyplot as plt\n'), ((2211, 2236), 'pickle.dump', 'pickle.dump', (['model', 'pfile'], {}), '(model, pfile)\n', (2222, 2236), False, 'import pickle\n'), ((2819, 2843), 'hmm.baum_welch', 'hmm.baum_welch', (['o', 'model'], {}), '(o, model)\n', (2833, 2843), False, 'import hmm\n')]
################################################################## # Algorithm module using look-up tables ################################################################## import math import numpy as np import modelicares from scipy.optimize import minimize import scipy.interpolate as interpolate from scipy.optimize import differential_evolution from scipy.optimize import brute """ Execute only once """ def CalcBCvalues(amount_vals_BCs, exp_BCs, center_vals_BCs, factors_BCs, amount_lower_vals_BCs=None, amount_upper_vals_BCs=None): """ Calculate the boundary conditions around a center value. Typically, this is a equidistant grid but it can also be an exponentially increasing increment inputs: amount_vals_BCs: amount of discrete values of boundary conditions exp_BCs: exponent for increasing increments center_vals_BCs: center value that the other values are arranged around factors_BCs: for an exponent of 1, this is the uniform increment amount_lower_vals_BCs: amount of values below the center value amount_upper_vals_BCs: amount of values above the center value returns: values: list of boundary conditions """ if amount_lower_vals_BCs is None or amount_upper_vals_BCs is None: amount_lower_vals_BCs =[x/2 for x in amount_vals_BCs] amount_lower_vals_BCs = [math.floor(x) for x in amount_lower_vals_BCs] amount_upper_vals_BCs = [amount_vals_BCs[x]-amount_lower_vals_BCs[x]-1 for x in range(len(amount_vals_BCs))] values_BCs = [] for i in range(len(amount_vals_BCs)): temp_list = [] for j in range(amount_lower_vals_BCs[i]): temp_list.append(center_vals_BCs[i] - (amount_lower_vals_BCs[i]-j)**exp_BCs[i]*factors_BCs[i]) for j in range(amount_upper_vals_BCs[i]+1): temp_list.append(center_vals_BCs[i] + j**exp_BCs[i]*factors_BCs[i]) values_BCs.append(temp_list) values_BCs[0].insert(0,0) return values_BCs def CalcLookUpTables(s, time_storage, init_conds): """ Calculate decision variables and costs and put them into look-up tables inputs: s: subsystem object time_storage: the current time step init_conds: initialization of optimization algorithm returns: storage_cost: list of costs depending on boundary conditions storage_DV: decision variables depending on boundary conditions storage_out: outputs depending on boundary conditions exDestArr: two-dimensional list of costs storage_grid: result grid as output of optimization algorithm """ import Objective_Function # Use the fullfactorial design of experiment: Use predefined BCs if len(s.values_BCs) == 1: BCsettings = modelicares.exps.doe.fullfact(s.values_BCs[0]) counter = len(s.values_BCs) elif len(s.values_BCs) == 2: BCsettings = modelicares.exps.doe.fullfact(s.values_BCs[0],s.values_BCs[1]) counter = len(s.values_BCs[0])*len(s.values_BCs[1]) elif len(s.values_BCs) == 3: BCsettings = modelicares.exps.doe.fullfact(s.values_BCs[0],s.values_BCs[1],s.values_BCs[2]) counter = len(s.values_BCs[0])*len(s.values_BCs[1])*len(s.values_BCs[2]) elif len(s.values_BCs) == 4: BCsettings = modelicares.exps.doe.fullfact(s.values_BCs[0],s.values_BCs[1],s.values_BCs[2],s.values_BCs[3]) counter = len(s.values_BCs[0])*len(s.values_BCs[1])*len(s.values_BCs[2])*len(s.values_BCs[3]) # Look-up tables: storage_cost = np.zeros([counter,(len(s.values_BCs)+1)]) storage_DV = np.zeros([counter,len(s.values_BCs)+s._num_DVs]) storage_out = np.zeros([counter,len(s.values_BCs)+s.num_VarsOut]) storage_grid = np.zeros([counter+1,2]) storage_grid[0,0] = storage_grid[0,1] = None storage_grid_alt2 = np.zeros([2*counter,2]) # 2D exDestArr to communicate: exDestArr = np.empty([len(s.values_BCs[0])+1,len(s.values_BCs[1])+1]) for i,val in enumerate(s.values_BCs[0]): exDestArr[i+1,0]=val for i,val in enumerate(s.values_BCs[1]): exDestArr[0,i+1]=val exDestArr[0,0] = None counter = 0 for BC in BCsettings: j = 0 while j < len(s.values_BCs): storage_cost[counter,j] = storage_DV[counter,j] = storage_out[counter,j] = BC[j] j += 1 """" Perform a minimization using the defined objective function """ Objective_Function.ChangeDir(s._name) boundaries = s._bounds_DVs #[low, high] if boundaries[0] != boundaries[1]: ranges = [slice(boundaries[0],boundaries[1]+5, 10)] """ First conduct a brute force search """ obj_fun_val = brute(Objective_Function.Obj,ranges,args = (BC, s), disp=True, full_output=True, finish = None) init_conds = obj_fun_val[0] cons = ({'type':'ineq','fun': lambda x: x-boundaries[0]}, {'type':'ineq','fun': lambda x: boundaries[1]-x}) """ Perform an addtional optimization to refine the previous findings """ obj_fun_val = minimize(Objective_Function.Obj,init_conds,args = (BC, s),method='SLSQP',constraints=cons, options={'maxiter':100, 'ftol':0.01}) else: ranges = [slice(boundaries[0],boundaries[1]+1, 1)] obj_fun_val = brute(Objective_Function.Obj,ranges,args = (BC, s), disp=True, full_output=True, finish = None) """ Post processing of the different returned results of the optimization algorithms """ if isinstance(obj_fun_val, tuple): """ fill storage_grid """ if counter ==0: res_grid = np.concatenate((obj_fun_val[2][np.newaxis], obj_fun_val[3][np.newaxis]),axis = 0) else: res_grid = np.append(res_grid,obj_fun_val[3][np.newaxis],axis = 0) else: res_grid_new = Objective_Function.GetOptTrack() Objective_Function.DelLastTrack() if counter ==0: res_grid = res_grid_new else: stor_shape = res_grid.shape new_shape = res_grid_new.shape if stor_shape[1] != new_shape[1]: to_add = abs(stor_shape[1]-new_shape[1]) if stor_shape[1] > new_shape[1]: add_array = np.zeros([new_shape[0],to_add]) res_grid_new = np.append(res_grid_new,add_array, axis = 1) else: add_array = np.zeros([stor_shape[0],to_add]) res_grid = np.append(res_grid,add_array, axis = 1) res_grid = np.append(res_grid,res_grid_new,axis = 0) """ fill look-up table DV and Cost """ j = len(s.values_BCs) while j < (s._num_DVs+len(s.values_BCs)): if isinstance(obj_fun_val, tuple): DV_values = obj_fun_val[0] else: DV_values = obj_fun_val.get('x') storage_DV[counter,j] = DV_values.item(j-len(s.values_BCs)) j += 1 ix1 = np.isin(exDestArr, BC[0]) ix2 = np.isin(exDestArr, BC[1]) index1 = np.where(ix1) index2 = np.where(ix2) if isinstance(obj_fun_val, tuple): storage_cost[counter,len(s.values_BCs)] = obj_fun_val[1] exDestArr[index1[0].tolist(), index2[1].tolist()] = obj_fun_val[1] else: storage_cost[counter,len(s.values_BCs)] = obj_fun_val.get('fun') exDestArr[index1[0].tolist(), index2[1].tolist()] = obj_fun_val.get('fun') """ Get Output Variables """ output_vals = Objective_Function.GetOutputVars() """ Fill look-up table Out """ k = len(s.values_BCs) while k < (s.num_VarsOut+len(s.values_BCs)): storage_out[counter,k] = output_vals[k-len(s.values_BCs)] k += 1 counter = counter + 1 if isinstance(obj_fun_val, tuple): storage_grid = np.append(storage_grid,res_grid,axis=1) else: storage_grid = np.append(storage_grid_alt2,res_grid,axis=1) storage_grid = res_grid return [storage_cost, storage_DV, storage_out, exDestArr, storage_grid] def Interpolation(measurements_SubSys, storage_DV, bounds_DVs, storage_cost, storage_out): """ Interpolate the values of the decision variables, costs and outputs inputs: measurements_SubSys: the inflow measurements of a subsystem storage_DV: the decision variables depending on boundary conditions bounds_DVs: upper and lower bounds of the decision variables storage_cost: the costs depending on boundary conditions storage_out: the outputs depending on boundary conditions returns: commands: interpolated command for current measurement costs: interpolated costs for current measurement out: interpolated outputs for current measurement """ """ Reformat the boundary conditions, decision variables, outputs and costs """ cond_BC = [True if L<len(measurements_SubSys) else False for L in range(len(storage_DV[0]))] cond_DV = [False if L<len(measurements_SubSys) else True for L in range(len(storage_DV[0]))] cond_Out = [False if L<len(measurements_SubSys) else True for L in range(len(storage_out[0]))] cond_Costs = cond_DV grid_points = np.compress(cond_BC,storage_DV, axis = 1) grid_point_values = np.compress(cond_DV,storage_DV, axis = 1) grid_measurements = measurements_SubSys[::-1] grid_point_values_costs = np.compress(cond_Costs,storage_cost, axis = 1) grid_point_values_out = np.compress(cond_Out,storage_out, axis = 1) print("Grid points:") print(grid_points) print("values:") print(grid_point_values) print("measurements:") print(grid_measurements) """ Interpolation of reformatted data """ try: commands = interpolate.griddata(grid_points, grid_point_values,grid_measurements ,method='linear') costs = interpolate.griddata(grid_points, grid_point_values_costs,grid_measurements ,method='linear') out = interpolate.griddata(grid_points, grid_point_values_out, grid_measurements ,method='linear') print("commands: " + str(commands)) print("costs: " + str(costs)) print("outputs: " + str(out)) # Check if commands are in range, else set to boundary values for i, val in enumerate(commands): if val < bounds_DVs[i]: commands[i] = bounds_DVs[i] print('Val < lower Bound') elif val > bounds_DVs[i+1]: commands[i] = bounds_DVs[i+1] print('Val > higher Bound') elif val >= bounds_DVs[i] and val <= bounds_DVs[i+1]: commands[i] = val # last case: invalid interpolation else: commands[i] = bounds_DVs[i] print('interpolation failed!') return [commands, costs, out] except: commands = [] costs = [] out = [] for i in range(0,len(storage_DV)): commands.append(storage_DV[0,0]) costs.append(0) out.append(0) print('interpolation failed!')
[ "numpy.isin", "modelicares.exps.doe.fullfact", "scipy.optimize.minimize", "scipy.interpolate.griddata", "Objective_Function.DelLastTrack", "numpy.zeros", "Objective_Function.GetOutputVars", "math.floor", "Objective_Function.ChangeDir", "Objective_Function.GetOptTrack", "numpy.append", "numpy.where", "scipy.optimize.brute", "numpy.compress", "numpy.concatenate" ]
[((3753, 3779), 'numpy.zeros', 'np.zeros', (['[counter + 1, 2]'], {}), '([counter + 1, 2])\n', (3761, 3779), True, 'import numpy as np\n'), ((3850, 3876), 'numpy.zeros', 'np.zeros', (['[2 * counter, 2]'], {}), '([2 * counter, 2])\n', (3858, 3876), True, 'import numpy as np\n'), ((9417, 9457), 'numpy.compress', 'np.compress', (['cond_BC', 'storage_DV'], {'axis': '(1)'}), '(cond_BC, storage_DV, axis=1)\n', (9428, 9457), True, 'import numpy as np\n'), ((9483, 9523), 'numpy.compress', 'np.compress', (['cond_DV', 'storage_DV'], {'axis': '(1)'}), '(cond_DV, storage_DV, axis=1)\n', (9494, 9523), True, 'import numpy as np\n'), ((9605, 9650), 'numpy.compress', 'np.compress', (['cond_Costs', 'storage_cost'], {'axis': '(1)'}), '(cond_Costs, storage_cost, axis=1)\n', (9616, 9650), True, 'import numpy as np\n'), ((9680, 9722), 'numpy.compress', 'np.compress', (['cond_Out', 'storage_out'], {'axis': '(1)'}), '(cond_Out, storage_out, axis=1)\n', (9691, 9722), True, 'import numpy as np\n'), ((2786, 2832), 'modelicares.exps.doe.fullfact', 'modelicares.exps.doe.fullfact', (['s.values_BCs[0]'], {}), '(s.values_BCs[0])\n', (2815, 2832), False, 'import modelicares\n'), ((4452, 4489), 'Objective_Function.ChangeDir', 'Objective_Function.ChangeDir', (['s._name'], {}), '(s._name)\n', (4480, 4489), False, 'import Objective_Function\n'), ((7130, 7155), 'numpy.isin', 'np.isin', (['exDestArr', 'BC[0]'], {}), '(exDestArr, BC[0])\n', (7137, 7155), True, 'import numpy as np\n'), ((7170, 7195), 'numpy.isin', 'np.isin', (['exDestArr', 'BC[1]'], {}), '(exDestArr, BC[1])\n', (7177, 7195), True, 'import numpy as np\n'), ((7213, 7226), 'numpy.where', 'np.where', (['ix1'], {}), '(ix1)\n', (7221, 7226), True, 'import numpy as np\n'), ((7244, 7257), 'numpy.where', 'np.where', (['ix2'], {}), '(ix2)\n', (7252, 7257), True, 'import numpy as np\n'), ((7689, 7723), 'Objective_Function.GetOutputVars', 'Objective_Function.GetOutputVars', ([], {}), '()\n', (7721, 7723), False, 'import Objective_Function\n'), ((8029, 8070), 'numpy.append', 'np.append', (['storage_grid', 'res_grid'], {'axis': '(1)'}), '(storage_grid, res_grid, axis=1)\n', (8038, 8070), True, 'import numpy as np\n'), ((8102, 8148), 'numpy.append', 'np.append', (['storage_grid_alt2', 'res_grid'], {'axis': '(1)'}), '(storage_grid_alt2, res_grid, axis=1)\n', (8111, 8148), True, 'import numpy as np\n'), ((9955, 10047), 'scipy.interpolate.griddata', 'interpolate.griddata', (['grid_points', 'grid_point_values', 'grid_measurements'], {'method': '"""linear"""'}), "(grid_points, grid_point_values, grid_measurements,\n method='linear')\n", (9975, 10047), True, 'import scipy.interpolate as interpolate\n'), ((10059, 10157), 'scipy.interpolate.griddata', 'interpolate.griddata', (['grid_points', 'grid_point_values_costs', 'grid_measurements'], {'method': '"""linear"""'}), "(grid_points, grid_point_values_costs,\n grid_measurements, method='linear')\n", (10079, 10157), True, 'import scipy.interpolate as interpolate\n'), ((10167, 10263), 'scipy.interpolate.griddata', 'interpolate.griddata', (['grid_points', 'grid_point_values_out', 'grid_measurements'], {'method': '"""linear"""'}), "(grid_points, grid_point_values_out, grid_measurements,\n method='linear')\n", (10187, 10263), True, 'import scipy.interpolate as interpolate\n'), ((1380, 1393), 'math.floor', 'math.floor', (['x'], {}), '(x)\n', (1390, 1393), False, 'import math\n'), ((2924, 2987), 'modelicares.exps.doe.fullfact', 'modelicares.exps.doe.fullfact', (['s.values_BCs[0]', 's.values_BCs[1]'], {}), '(s.values_BCs[0], s.values_BCs[1])\n', (2953, 2987), False, 'import modelicares\n'), ((4729, 4827), 'scipy.optimize.brute', 'brute', (['Objective_Function.Obj', 'ranges'], {'args': '(BC, s)', 'disp': '(True)', 'full_output': '(True)', 'finish': 'None'}), '(Objective_Function.Obj, ranges, args=(BC, s), disp=True, full_output=\n True, finish=None)\n', (4734, 4827), False, 'from scipy.optimize import brute\n'), ((5118, 5254), 'scipy.optimize.minimize', 'minimize', (['Objective_Function.Obj', 'init_conds'], {'args': '(BC, s)', 'method': '"""SLSQP"""', 'constraints': 'cons', 'options': "{'maxiter': 100, 'ftol': 0.01}"}), "(Objective_Function.Obj, init_conds, args=(BC, s), method='SLSQP',\n constraints=cons, options={'maxiter': 100, 'ftol': 0.01})\n", (5126, 5254), False, 'from scipy.optimize import minimize\n'), ((5350, 5448), 'scipy.optimize.brute', 'brute', (['Objective_Function.Obj', 'ranges'], {'args': '(BC, s)', 'disp': '(True)', 'full_output': '(True)', 'finish': 'None'}), '(Objective_Function.Obj, ranges, args=(BC, s), disp=True, full_output=\n True, finish=None)\n', (5355, 5448), False, 'from scipy.optimize import brute\n'), ((5928, 5960), 'Objective_Function.GetOptTrack', 'Objective_Function.GetOptTrack', ([], {}), '()\n', (5958, 5960), False, 'import Objective_Function\n'), ((5973, 6006), 'Objective_Function.DelLastTrack', 'Objective_Function.DelLastTrack', ([], {}), '()\n', (6004, 6006), False, 'import Objective_Function\n'), ((3102, 3187), 'modelicares.exps.doe.fullfact', 'modelicares.exps.doe.fullfact', (['s.values_BCs[0]', 's.values_BCs[1]', 's.values_BCs[2]'], {}), '(s.values_BCs[0], s.values_BCs[1], s.values_BCs[2]\n )\n', (3131, 3187), False, 'import modelicares\n'), ((5704, 5789), 'numpy.concatenate', 'np.concatenate', (['(obj_fun_val[2][np.newaxis], obj_fun_val[3][np.newaxis])'], {'axis': '(0)'}), '((obj_fun_val[2][np.newaxis], obj_fun_val[3][np.newaxis]), axis=0\n )\n', (5718, 5789), True, 'import numpy as np\n'), ((5831, 5886), 'numpy.append', 'np.append', (['res_grid', 'obj_fun_val[3][np.newaxis]'], {'axis': '(0)'}), '(res_grid, obj_fun_val[3][np.newaxis], axis=0)\n', (5840, 5886), True, 'import numpy as np\n'), ((6696, 6737), 'numpy.append', 'np.append', (['res_grid', 'res_grid_new'], {'axis': '(0)'}), '(res_grid, res_grid_new, axis=0)\n', (6705, 6737), True, 'import numpy as np\n'), ((3317, 3419), 'modelicares.exps.doe.fullfact', 'modelicares.exps.doe.fullfact', (['s.values_BCs[0]', 's.values_BCs[1]', 's.values_BCs[2]', 's.values_BCs[3]'], {}), '(s.values_BCs[0], s.values_BCs[1], s.\n values_BCs[2], s.values_BCs[3])\n', (3346, 3419), False, 'import modelicares\n'), ((6384, 6416), 'numpy.zeros', 'np.zeros', (['[new_shape[0], to_add]'], {}), '([new_shape[0], to_add])\n', (6392, 6416), True, 'import numpy as np\n'), ((6455, 6497), 'numpy.append', 'np.append', (['res_grid_new', 'add_array'], {'axis': '(1)'}), '(res_grid_new, add_array, axis=1)\n', (6464, 6497), True, 'import numpy as np\n'), ((6561, 6594), 'numpy.zeros', 'np.zeros', (['[stor_shape[0], to_add]'], {}), '([stor_shape[0], to_add])\n', (6569, 6594), True, 'import numpy as np\n'), ((6629, 6667), 'numpy.append', 'np.append', (['res_grid', 'add_array'], {'axis': '(1)'}), '(res_grid, add_array, axis=1)\n', (6638, 6667), True, 'import numpy as np\n')]
# -*- coding: utf-8 -*- # vim: tabstop=4 shiftwidth=4 softtabstop=4 # # Copyright (C) 2014-2016 GEM Foundation # # OpenQuake is free software: you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # OpenQuake is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with OpenQuake. If not, see <http://www.gnu.org/licenses/>. """ Module exports :class:`FrankelEtAl1996MblgAB1987NSHMP2008`, :class:`FrankelEtAl1996MblgJ1996NSHMP2008`, :class:`FrankelEtAl1996MwNSHMP2008`. """ from __future__ import division import numpy as np from scipy.interpolate import RectBivariateSpline from openquake.hazardlib.gsim.base import CoeffsTable, GMPE from openquake.hazardlib.gsim.utils import ( mblg_to_mw_atkinson_boore_87, mblg_to_mw_johnston_96, clip_mean ) from openquake.hazardlib import const from openquake.hazardlib.imt import PGA, SA class FrankelEtAl1996MblgAB1987NSHMP2008(GMPE): """ Implements GMPE developed by <NAME> et al. and documented in "National Seismic-Hazard Maps: Documentation June 1996" (USGS - Open File Report 96-532) available at: http://earthquake.usgs.gov/hazards/products/conterminous/1996/documentation/ofr96-532.pdf The GMPE is used by the National Seismic Hazard Mapping Project (NSHMP) for the 2008 central and eastern US hazard model. This class replicates the algorithm as implemented in ``subroutine getFEA`` in the ``hazgridXnga2.f`` Fortran code available at: http://earthquake.usgs.gov/hazards/products/conterminous/2008/software/ The GMPE is defined by a set of lookup tables (see Appendix A) defined from minimum magnitude Mw=4.4 to maximum magnitude Mw=8.2, and from (hypocentral) distance 10 km to 1000 km. Values outside these range are clipped. Lookup tables are defined for PGA, and SA at the following periods: 0.1, 0.2, 0.3, 0.5, 1.0, 2.0. The GMPE does not allow for interpolation on unsupported periods. The class assumes rupture magnitude to be in Mblg scale (given that MFDs for central and eastern US are given in this scale). However lookup tables are defined for Mw. Therefore Mblg is converted to Mw by using Atkinson and Boore 1987 conversion equation. Coefficients are given for the B/C site conditions. """ #: Supported tectonic region type is stable continental crust, #: given that the equations have been derived for central and eastern #: north America DEFINED_FOR_TECTONIC_REGION_TYPE = const.TRT.STABLE_CONTINENTAL #: Supported intensity measure types are spectral acceleration, #: and peak ground acceleration DEFINED_FOR_INTENSITY_MEASURE_TYPES = set([ PGA, SA ]) #: Supported intensity measure component is the geometric mean of #two : horizontal components #:attr:`~openquake.hazardlib.const.IMC.AVERAGE_HORIZONTAL`, DEFINED_FOR_INTENSITY_MEASURE_COMPONENT = const.IMC.AVERAGE_HORIZONTAL #: Supported standard deviation type is only total. DEFINED_FOR_STANDARD_DEVIATION_TYPES = set([ const.StdDev.TOTAL ]) #: No site parameters required REQUIRES_SITES_PARAMETERS = set() #: Required rupture parameter is only magnitude (Mblg). REQUIRES_RUPTURE_PARAMETERS = set(('mag', )) #: Required distance measure is rhypo REQUIRES_DISTANCES = set(('rhypo', )) def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values. :raises ValueError: if imt is instance of :class:`openquake.hazardlib.imt.SA` with unsupported period. """ assert all(stddev_type in self.DEFINED_FOR_STANDARD_DEVIATION_TYPES for stddev_type in stddev_types) if imt not in self.IMTS_TABLES: raise ValueError( 'IMT %s not supported in FrankelEtAl1996NSHMP. ' % repr(imt) + 'FrankelEtAl1996NSHMP does not allow interpolation for ' + 'unsupported periods.' ) mean = self._compute_mean(imt, rup.mag, dists.rhypo.copy()) mean = clip_mean(imt, mean) stddevs = self._compute_stddevs(imt, dists.rhypo.shape, stddev_types) return mean, stddevs def _compute_mean(self, imt, mag, rhypo): """ Compute mean value from lookup table. Lookup table defines log10(IMT) (in g) for combinations of Mw and log10(rhypo) values. ``mag`` is therefore converted from Mblg to Mw using Atkinson and Boore 1987 conversion equation. Mean value is finally converted from base 10 to base e. """ mag = np.zeros_like(rhypo) + self._convert_magnitude(mag) # to avoid run time warning in case rhypo is zero set minimum distance # to 10, which is anyhow the minimum distance allowed by the tables rhypo[rhypo < 10] = 10 rhypo = np.log10(rhypo) # create lookup table and interpolate it at magnitude/distance values table = RectBivariateSpline( self.MAGS, self.DISTS, self.IMTS_TABLES[imt].T ) mean = table.ev(mag, rhypo) # convert mean from base 10 to base e return mean * np.log(10) def _convert_magnitude(self, mag): """ Convert magnitude from Mblg to Mw using Atkinson and Boore 1987 equation """ return mblg_to_mw_atkinson_boore_87(mag) def _compute_stddevs(self, imt, num_sites, stddev_types): """ Return total standard deviation (converted to base e) """ C = self.COEFFS[imt] stddevs = [] for _ in stddev_types: stddevs.append(np.zeros(num_sites) + C['sigma'] * np.log(10)) return stddevs # magnitude values for lookup table from minimum magnitude 4.4 to maximum # magnitude 8.2 MAGS = np.linspace(4.4, 8.2, 20) # hypocentral distance values (log10) for lookup table # from minimum distance 10 km to maximum distance 1000 km DISTS = np.linspace(1., 3., 21) # lookup table for PGA PGA_TBL = np.array([ [-0.71, -0.62, -0.53, -0.44, -0.36, -0.29, -0.21, -0.14, -0.07, 0.00, 0.06, 0.12, 0.19, 0.25, 0.31, 0.37, 0.43, 0.49, 0.54, 0.60], [-0.84, -0.75, -0.66, -0.57, -0.49, -0.41, -0.33, -0.26, -0.19, -0.12, -0.05, 0.01, 0.07, 0.14, 0.20, 0.26, 0.32, 0.38, 0.43, 0.49], [-0.98, -0.88, -0.79, -0.70, -0.62, -0.53, -0.46, -0.38, -0.31, -0.24, -0.17, -0.10, -0.04, 0.02, 0.08, 0.14, 0.20, 0.26, 0.32, 0.38], [-1.12, -1.02, -0.93, -0.84, -0.75, -0.66, -0.58, -0.51, -0.43, -0.36, -0.29, -0.22, -0.16, -0.10, -0.03, 0.03, 0.09, 0.15, 0.21, 0.27], [-1.27, -1.17, -1.07, -0.98, -0.89, -0.80, -0.72, -0.64, -0.56, -0.49, -0.42, -0.35, -0.28, -0.22, -0.15, -0.09, -0.03, 0.03, 0.09, 0.15], [-1.43, -1.32, -1.22, -1.13, -1.03, -0.94, -0.86, -0.78, -0.70, -0.62, -0.55, -0.48, -0.41, -0.34, -0.28, -0.22, -0.15, -0.09, -0.03, 0.03], [-1.59, -1.48, -1.38, -1.28, -1.19, -1.09, -1.01, -0.92, -0.84, -0.76, -0.69, -0.61, -0.54, -0.48, -0.41, -0.35, -0.28, -0.22, -0.16, -0.10], [-1.76, -1.65, -1.54, -1.44, -1.35, -1.25, -1.16, -1.07, -0.99, -0.91, -0.83, -0.76, -0.69, -0.62, -0.55, -0.48, -0.42, -0.35, -0.29, -0.23], [-1.93, -1.82, -1.72, -1.61, -1.51, -1.42, -1.33, -1.24, -1.15, -1.07, -0.99, -0.91, -0.83, -0.76, -0.69, -0.63, -0.56, -0.50, -0.43, -0.37], [-2.07, -1.95, -1.84, -1.74, -1.64, -1.54, -1.44, -1.35, -1.26, -1.18, -1.10, -1.02, -0.94, -0.86, -0.79, -0.72, -0.65, -0.59, -0.53, -0.46], [-2.17, -2.05, -1.94, -1.83, -1.73, -1.63, -1.53, -1.43, -1.34, -1.25, -1.17, -1.09, -1.01, -0.93, -0.86, -0.78, -0.71, -0.65, -0.58, -0.52], [-2.28, -2.16, -2.04, -1.93, -1.83, -1.72, -1.62, -1.53, -1.43, -1.34, -1.25, -1.17, -1.09, -1.01, -0.93, -0.86, -0.78, -0.71, -0.65, -0.58], [-2.44, -2.32, -2.20, -2.09, -1.98, -1.87, -1.77, -1.67, -1.58, -1.48, -1.39, -1.30, -1.22, -1.14, -1.06, -0.98, -0.91, -0.83, -0.77, -0.70], [-2.63, -2.50, -2.38, -2.26, -2.15, -2.04, -1.94, -1.84, -1.74, -1.64, -1.55, -1.46, -1.37, -1.29, -1.20, -1.12, -1.05, -0.97, -0.90, -0.83], [-2.83, -2.70, -2.57, -2.45, -2.34, -2.23, -2.12, -2.01, -1.91, -1.81, -1.72, -1.62, -1.53, -1.45, -1.36, -1.28, -1.20, -1.12, -1.05, -0.98], [-3.05, -2.92, -2.78, -2.66, -2.54, -2.42, -2.31, -2.20, -2.10, -2.00, -1.90, -1.80, -1.71, -1.62, -1.53, -1.45, -1.36, -1.28, -1.21, -1.13], [-3.29, -3.15, -3.01, -2.88, -2.75, -2.63, -2.52, -2.41, -2.30, -2.19, -2.09, -1.99, -1.90, -1.80, -1.71, -1.62, -1.54, -1.46, -1.38, -1.30], [-3.55, -3.40, -3.25, -3.11, -2.98, -2.86, -2.74, -2.62, -2.51, -2.40, -2.30, -2.19, -2.09, -2.00, -1.90, -1.81, -1.72, -1.64, -1.56, -1.48], [-3.84, -3.67, -3.52, -3.37, -3.23, -3.10, -2.97, -2.85, -2.73, -2.62, -2.51, -2.41, -2.30, -2.20, -2.10, -2.01, -1.92, -1.83, -1.74, -1.66], [-4.15, -3.97, -3.80, -3.65, -3.50, -3.36, -3.22, -3.09, -2.97, -2.85, -2.74, -2.63, -2.52, -2.42, -2.31, -2.22, -2.12, -2.03, -1.94, -1.85], [-4.48, -4.29, -4.11, -3.94, -3.78, -3.63, -3.49, -3.35, -3.22, -3.10, -2.98, -2.86, -2.75, -2.64, -2.53, -2.43, -2.33, -2.23, -2.14, -2.05] ]) # lookup table for SA 0.1 SA01_TBL = np.array([ [-0.43, -0.32, -0.23, -0.14, -0.05, 0.03, 0.11, 0.19, 0.26, 0.33, 0.40, 0.47, 0.54, 0.60, 0.66, 0.72, 0.78, 0.84, 0.90, 0.96], [-0.55, -0.44, -0.35, -0.26, -0.17, -0.08, 0.00, 0.07, 0.15, 0.22, 0.29, 0.36, 0.43, 0.49, 0.55, 0.62, 0.68, 0.74, 0.80, 0.86], [-0.67, -0.57, -0.47, -0.38, -0.29, -0.20, -0.12, -0.04, 0.03, 0.11, 0.18, 0.25, 0.31, 0.38, 0.44, 0.51, 0.57, 0.63, 0.69, 0.75], [-0.80, -0.69, -0.60, -0.50, -0.41, -0.33, -0.24, -0.16, -0.09, -0.01, 0.06, 0.13, 0.20, 0.27, 0.33, 0.39, 0.46, 0.52, 0.58, 0.64], [-0.93, -0.83, -0.73, -0.63, -0.54, -0.45, -0.37, -0.29, -0.21, -0.13, -0.06, 0.01, 0.08, 0.15, 0.21, 0.28, 0.34, 0.40, 0.46, 0.52], [-1.07, -0.97, -0.86, -0.77, -0.68, -0.59, -0.50, -0.42, -0.34, -0.26, -0.18, -0.11, -0.04, 0.03, 0.09, 0.16, 0.22, 0.28, 0.35, 0.41], [-1.22, -1.11, -1.01, -0.91, -0.82, -0.73, -0.64, -0.55, -0.47, -0.39, -0.32, -0.24, -0.17, -0.10, -0.03, 0.03, 0.10, 0.16, 0.22, 0.28], [-1.37, -1.26, -1.16, -1.06, -0.97, -0.87, -0.78, -0.70, -0.61, -0.53, -0.45, -0.38, -0.30, -0.23, -0.16, -0.10, -0.03, 0.03, 0.10, 0.16], [-1.53, -1.42, -1.32, -1.22, -1.12, -1.03, -0.94, -0.85, -0.76, -0.68, -0.60, -0.52, -0.45, -0.37, -0.30, -0.23, -0.17, -0.10, -0.04, 0.02], [-1.65, -1.54, -1.43, -1.33, -1.24, -1.14, -1.05, -0.96, -0.87, -0.79, -0.70, -0.62, -0.54, -0.47, -0.40, -0.33, -0.26, -0.19, -0.13, -0.06], [-1.73, -1.62, -1.51, -1.41, -1.31, -1.22, -1.12, -1.03, -0.94, -0.86, -0.77, -0.69, -0.61, -0.53, -0.46, -0.39, -0.32, -0.25, -0.18, -0.12], [-1.83, -1.72, -1.61, -1.51, -1.41, -1.31, -1.21, -1.12, -1.03, -0.94, -0.85, -0.77, -0.69, -0.61, -0.53, -0.46, -0.39, -0.32, -0.25, -0.18], [-1.98, -1.87, -1.76, -1.66, -1.56, -1.46, -1.36, -1.27, -1.18, -1.09, -1.00, -0.91, -0.83, -0.75, -0.67, -0.59, -0.52, -0.44, -0.37, -0.31], [-2.17, -2.05, -1.95, -1.84, -1.74, -1.64, -1.54, -1.45, -1.35, -1.26, -1.17, -1.08, -0.99, -0.91, -0.83, -0.75, -0.67, -0.60, -0.53, -0.46], [-2.38, -2.26, -2.15, -2.05, -1.94, -1.84, -1.74, -1.64, -1.55, -1.45, -1.36, -1.27, -1.18, -1.10, -1.01, -0.93, -0.85, -0.78, -0.70, -0.63], [-2.61, -2.50, -2.39, -2.28, -2.17, -2.07, -1.97, -1.87, -1.77, -1.68, -1.58, -1.49, -1.40, -1.31, -1.22, -1.14, -1.06, -0.98, -0.91, -0.83], [-2.89, -2.77, -2.66, -2.55, -2.44, -2.33, -2.23, -2.13, -2.03, -1.93, -1.83, -1.74, -1.64, -1.55, -1.46, -1.38, -1.29, -1.21, -1.13, -1.06], [-3.20, -3.08, -2.96, -2.85, -2.73, -2.62, -2.51, -2.41, -2.31, -2.20, -2.10, -2.01, -1.91, -1.82, -1.72, -1.63, -1.55, -1.46, -1.38, -1.30], [-3.56, -3.43, -3.30, -3.18, -3.06, -2.94, -2.82, -2.71, -2.60, -2.49, -2.39, -2.29, -2.19, -2.09, -1.99, -1.90, -1.81, -1.72, -1.64, -1.56], [-3.96, -3.81, -3.67, -3.53, -3.39, -3.26, -3.14, -3.02, -2.90, -2.78, -2.67, -2.57, -2.46, -2.36, -2.26, -2.16, -2.07, -1.97, -1.88, -1.80], [-4.37, -4.20, -4.04, -3.88, -3.73, -3.59, -3.45, -3.32, -3.19, -3.07, -2.95, -2.83, -2.72, -2.61, -2.51, -2.40, -2.31, -2.21, -2.12, -2.03] ]) # lookup table for SA 0.2 SA02_TBL = np.array([ [-0.71, -0.57, -0.46, -0.35, -0.25, -0.15, -0.06, 0.02, 0.10, 0.18, 0.25, 0.32, 0.39, 0.46, 0.53, 0.59, 0.65, 0.72, 0.78, 0.84], [-0.82, -0.69, -0.57, -0.46, -0.36, -0.26, -0.17, -0.09, -0.01, 0.07, 0.14, 0.22, 0.29, 0.35, 0.42, 0.48, 0.55, 0.61, 0.67, 0.73], [-0.93, -0.80, -0.68, -0.57, -0.47, -0.38, -0.29, -0.20, -0.12, -0.04, 0.03, 0.11, 0.18, 0.25, 0.31, 0.38, 0.44, 0.50, 0.56, 0.63], [-1.05, -0.92, -0.80, -0.69, -0.59, -0.49, -0.40, -0.32, -0.23, -0.15, -0.08, 0.00, 0.07, 0.14, 0.20, 0.27, 0.33, 0.40, 0.46, 0.52], [-1.17, -1.04, -0.92, -0.81, -0.71, -0.61, -0.52, -0.44, -0.35, -0.27, -0.19, -0.12, -0.05, 0.02, 0.09, 0.16, 0.22, 0.29, 0.35, 0.41], [-1.30, -1.17, -1.05, -0.94, -0.84, -0.74, -0.65, -0.56, -0.47, -0.39, -0.31, -0.24, -0.16, -0.09, -0.02, 0.04, 0.11, 0.17, 0.24, 0.30], [-1.43, -1.30, -1.18, -1.07, -0.97, -0.87, -0.77, -0.69, -0.60, -0.52, -0.44, -0.36, -0.28, -0.21, -0.14, -0.07, -0.01, 0.06, 0.12, 0.18], [-1.57, -1.44, -1.32, -1.21, -1.10, -1.00, -0.91, -0.82, -0.73, -0.65, -0.56, -0.49, -0.41, -0.34, -0.27, -0.20, -0.13, -0.06, 0.00, 0.06], [-1.72, -1.58, -1.46, -1.35, -1.25, -1.15, -1.05, -0.96, -0.87, -0.78, -0.70, -0.62, -0.54, -0.47, -0.39, -0.32, -0.25, -0.19, -0.12, -0.06], [-1.82, -1.68, -1.56, -1.45, -1.34, -1.24, -1.14, -1.05, -0.96, -0.87, -0.79, -0.70, -0.63, -0.55, -0.47, -0.40, -0.33, -0.26, -0.20, -0.13], [-1.88, -1.74, -1.62, -1.51, -1.40, -1.30, -1.20, -1.10, -1.01, -0.92, -0.84, -0.75, -0.67, -0.59, -0.52, -0.44, -0.37, -0.30, -0.24, -0.17], [-1.95, -1.82, -1.69, -1.58, -1.47, -1.37, -1.27, -1.17, -1.08, -0.99, -0.90, -0.81, -0.73, -0.65, -0.57, -0.50, -0.42, -0.35, -0.28, -0.22], [-2.08, -1.94, -1.82, -1.70, -1.59, -1.49, -1.39, -1.29, -1.20, -1.11, -1.02, -0.93, -0.84, -0.76, -0.68, -0.60, -0.53, -0.45, -0.38, -0.32], [-2.23, -2.09, -1.97, -1.85, -1.74, -1.64, -1.53, -1.44, -1.34, -1.25, -1.15, -1.06, -0.98, -0.89, -0.81, -0.73, -0.65, -0.58, -0.51, -0.44], [-2.40, -2.26, -2.13, -2.01, -1.90, -1.80, -1.70, -1.60, -1.50, -1.40, -1.31, -1.22, -1.13, -1.04, -0.96, -0.88, -0.80, -0.72, -0.65, -0.58], [-2.58, -2.44, -2.32, -2.20, -2.09, -1.98, -1.88, -1.78, -1.68, -1.58, -1.49, -1.39, -1.30, -1.22, -1.13, -1.04, -0.96, -0.88, -0.81, -0.73], [-2.80, -2.66, -2.53, -2.41, -2.30, -2.19, -2.09, -1.98, -1.88, -1.79, -1.69, -1.60, -1.50, -1.41, -1.32, -1.24, -1.15, -1.07, -0.99, -0.92], [-3.04, -2.90, -2.77, -2.65, -2.54, -2.43, -2.32, -2.22, -2.12, -2.02, -1.92, -1.83, -1.73, -1.64, -1.55, -1.46, -1.37, -1.29, -1.21, -1.13], [-3.33, -3.19, -3.06, -2.93, -2.82, -2.71, -2.60, -2.49, -2.39, -2.29, -2.19, -2.09, -1.99, -1.90, -1.80, -1.71, -1.62, -1.54, -1.45, -1.37], [-3.66, -3.52, -3.38, -3.26, -3.14, -3.02, -2.91, -2.80, -2.69, -2.59, -2.48, -2.38, -2.28, -2.18, -2.09, -1.99, -1.90, -1.81, -1.73, -1.64], [-4.05, -3.90, -3.76, -3.63, -3.50, -3.38, -3.26, -3.14, -3.03, -2.92, -2.81, -2.70, -2.59, -2.49, -2.39, -2.29, -2.19, -2.10, -2.01, -1.92] ]) # lookup table for SA 0.3 SA03_TBL = np.array([ [-0.97, -0.81, -0.66, -0.53, -0.42, -0.31, -0.21, -0.12, -0.03, 0.05, 0.13, 0.20, 0.28, 0.35, 0.41, 0.48, 0.54, 0.61, 0.67, 0.73], [-1.08, -0.91, -0.77, -0.64, -0.53, -0.42, -0.32, -0.23, -0.14, -0.06, 0.02, 0.10, 0.17, 0.24, 0.31, 0.38, 0.44, 0.50, 0.57, 0.63], [-1.19, -1.02, -0.88, -0.75, -0.64, -0.53, -0.43, -0.34, -0.25, -0.17, -0.09, -0.01, 0.06, 0.13, 0.20, 0.27, 0.33, 0.40, 0.46, 0.52], [-1.30, -1.14, -0.99, -0.86, -0.75, -0.64, -0.54, -0.45, -0.36, -0.28, -0.20, -0.12, -0.05, 0.03, 0.09, 0.16, 0.23, 0.29, 0.36, 0.42], [-1.41, -1.25, -1.11, -0.98, -0.86, -0.76, -0.66, -0.57, -0.48, -0.39, -0.31, -0.23, -0.16, -0.09, -0.02, 0.05, 0.12, 0.18, 0.25, 0.31], [-1.53, -1.37, -1.23, -1.10, -0.98, -0.88, -0.78, -0.68, -0.59, -0.51, -0.43, -0.35, -0.27, -0.20, -0.13, -0.06, 0.01, 0.07, 0.14, 0.20], [-1.66, -1.50, -1.35, -1.23, -1.11, -1.00, -0.90, -0.81, -0.72, -0.63, -0.55, -0.47, -0.39, -0.31, -0.24, -0.17, -0.11, -0.04, 0.02, 0.09], [-1.79, -1.63, -1.48, -1.36, -1.24, -1.13, -1.03, -0.93, -0.84, -0.75, -0.67, -0.59, -0.51, -0.44, -0.36, -0.29, -0.22, -0.16, -0.09, -0.03], [-1.93, -1.77, -1.62, -1.49, -1.37, -1.26, -1.16, -1.07, -0.97, -0.88, -0.80, -0.72, -0.64, -0.56, -0.49, -0.41, -0.34, -0.28, -0.21, -0.15], [-2.02, -1.85, -1.71, -1.58, -1.46, -1.35, -1.25, -1.15, -1.06, -0.97, -0.88, -0.80, -0.71, -0.64, -0.56, -0.49, -0.42, -0.35, -0.28, -0.21], [-2.07, -1.91, -1.76, -1.63, -1.51, -1.40, -1.30, -1.20, -1.10, -1.01, -0.92, -0.84, -0.75, -0.67, -0.60, -0.52, -0.45, -0.38, -0.31, -0.24], [-2.13, -1.97, -1.82, -1.69, -1.57, -1.46, -1.35, -1.25, -1.16, -1.06, -0.97, -0.89, -0.80, -0.72, -0.64, -0.56, -0.49, -0.42, -0.35, -0.28], [-2.25, -2.08, -1.93, -1.80, -1.68, -1.57, -1.46, -1.36, -1.26, -1.17, -1.08, -0.99, -0.90, -0.82, -0.74, -0.66, -0.58, -0.51, -0.44, -0.37], [-2.38, -2.21, -2.06, -1.93, -1.81, -1.70, -1.59, -1.49, -1.39, -1.29, -1.20, -1.11, -1.02, -0.94, -0.85, -0.77, -0.70, -0.62, -0.55, -0.48], [-2.53, -2.36, -2.21, -2.08, -1.96, -1.84, -1.73, -1.63, -1.53, -1.43, -1.34, -1.25, -1.16, -1.07, -0.99, -0.90, -0.82, -0.75, -0.67, -0.60], [-2.69, -2.52, -2.37, -2.24, -2.12, -2.00, -1.89, -1.79, -1.69, -1.59, -1.50, -1.40, -1.31, -1.22, -1.13, -1.05, -0.97, -0.89, -0.81, -0.74], [-2.88, -2.71, -2.56, -2.42, -2.30, -2.18, -2.08, -1.97, -1.87, -1.77, -1.67, -1.58, -1.48, -1.39, -1.30, -1.22, -1.13, -1.05, -0.97, -0.90], [-3.09, -2.92, -2.77, -2.63, -2.51, -2.39, -2.28, -2.18, -2.07, -1.97, -1.87, -1.78, -1.68, -1.59, -1.50, -1.41, -1.32, -1.24, -1.16, -1.08], [-3.33, -3.16, -3.01, -2.87, -2.75, -2.63, -2.52, -2.41, -2.31, -2.20, -2.11, -2.01, -1.91, -1.82, -1.72, -1.63, -1.54, -1.46, -1.37, -1.29], [-3.61, -3.44, -3.29, -3.15, -3.02, -2.91, -2.79, -2.68, -2.58, -2.47, -2.37, -2.27, -2.17, -2.08, -1.98, -1.89, -1.80, -1.71, -1.62, -1.54], [-3.95, -3.77, -3.62, -3.48, -3.35, -3.22, -3.11, -3.00, -2.89, -2.78, -2.67, -2.57, -2.47, -2.37, -2.27, -2.17, -2.08, -1.99, -1.90, -1.81] ]) # lookup table for SA 0.5 SA05_TBL = np.array([ [-1.44, -1.23, -1.03, -0.87, -0.72, -0.58, -0.46, -0.35, -0.25, -0.16, -0.07, 0.01, 0.09, 0.16, 0.23, 0.30, 0.37, 0.44, 0.50, 0.57], [-1.54, -1.33, -1.14, -0.97, -0.82, -0.69, -0.57, -0.46, -0.36, -0.27, -0.18, -0.10, -0.02, 0.06, 0.13, 0.20, 0.27, 0.34, 0.40, 0.46], [-1.64, -1.43, -1.24, -1.07, -0.92, -0.79, -0.67, -0.57, -0.47, -0.37, -0.29, -0.20, -0.12, -0.05, 0.03, 0.10, 0.16, 0.23, 0.30, 0.36], [-1.74, -1.53, -1.35, -1.18, -1.03, -0.90, -0.78, -0.67, -0.57, -0.48, -0.39, -0.31, -0.23, -0.15, -0.08, -0.01, 0.06, 0.13, 0.19, 0.26], [-1.85, -1.64, -1.46, -1.29, -1.14, -1.01, -0.89, -0.79, -0.69, -0.59, -0.50, -0.42, -0.34, -0.26, -0.19, -0.12, -0.05, 0.02, 0.09, 0.15], [-1.96, -1.75, -1.57, -1.40, -1.25, -1.12, -1.01, -0.90, -0.80, -0.70, -0.62, -0.53, -0.45, -0.37, -0.30, -0.23, -0.16, -0.09, -0.02, 0.04], [-2.08, -1.87, -1.68, -1.52, -1.37, -1.24, -1.12, -1.02, -0.91, -0.82, -0.73, -0.65, -0.56, -0.49, -0.41, -0.34, -0.27, -0.20, -0.13, -0.07], [-2.20, -1.99, -1.81, -1.64, -1.49, -1.36, -1.24, -1.14, -1.03, -0.94, -0.85, -0.76, -0.68, -0.60, -0.53, -0.45, -0.38, -0.31, -0.24, -0.18], [-2.33, -2.12, -1.93, -1.77, -1.62, -1.49, -1.37, -1.26, -1.16, -1.06, -0.97, -0.89, -0.80, -0.72, -0.64, -0.57, -0.50, -0.43, -0.36, -0.29], [-2.41, -2.20, -2.01, -1.84, -1.70, -1.56, -1.45, -1.34, -1.23, -1.14, -1.04, -0.96, -0.87, -0.79, -0.71, -0.64, -0.56, -0.49, -0.42, -0.36], [-2.45, -2.24, -2.05, -1.88, -1.73, -1.60, -1.48, -1.37, -1.27, -1.17, -1.08, -0.99, -0.90, -0.82, -0.74, -0.66, -0.59, -0.52, -0.45, -0.38], [-2.49, -2.28, -2.10, -1.93, -1.78, -1.65, -1.53, -1.42, -1.31, -1.21, -1.12, -1.03, -0.94, -0.86, -0.78, -0.70, -0.62, -0.55, -0.48, -0.41], [-2.59, -2.38, -2.19, -2.03, -1.88, -1.74, -1.62, -1.51, -1.41, -1.31, -1.21, -1.12, -1.03, -0.94, -0.86, -0.78, -0.70, -0.63, -0.56, -0.49], [-2.71, -2.50, -2.31, -2.14, -1.99, -1.86, -1.74, -1.62, -1.52, -1.42, -1.32, -1.23, -1.14, -1.05, -0.96, -0.88, -0.80, -0.73, -0.65, -0.58], [-2.84, -2.63, -2.44, -2.27, -2.12, -1.98, -1.86, -1.75, -1.64, -1.54, -1.44, -1.34, -1.25, -1.16, -1.08, -0.99, -0.91, -0.83, -0.76, -0.68], [-2.98, -2.77, -2.58, -2.41, -2.26, -2.12, -2.00, -1.88, -1.77, -1.67, -1.57, -1.48, -1.38, -1.29, -1.20, -1.12, -1.04, -0.96, -0.88, -0.80], [-3.14, -2.92, -2.73, -2.56, -2.41, -2.27, -2.15, -2.04, -1.93, -1.82, -1.72, -1.63, -1.53, -1.44, -1.35, -1.26, -1.18, -1.09, -1.01, -0.94], [-3.32, -3.10, -2.91, -2.74, -2.59, -2.45, -2.32, -2.21, -2.10, -1.99, -1.89, -1.79, -1.70, -1.60, -1.51, -1.42, -1.34, -1.25, -1.17, -1.09], [-3.52, -3.30, -3.11, -2.94, -2.78, -2.65, -2.52, -2.40, -2.29, -2.19, -2.08, -1.98, -1.89, -1.79, -1.70, -1.61, -1.52, -1.43, -1.35, -1.26], [-3.75, -3.53, -3.34, -3.17, -3.01, -2.87, -2.75, -2.63, -2.52, -2.41, -2.31, -2.20, -2.11, -2.01, -1.91, -1.82, -1.73, -1.64, -1.55, -1.47], [-4.02, -3.80, -3.60, -3.43, -3.27, -3.13, -3.01, -2.89, -2.77, -2.67, -2.56, -2.46, -2.36, -2.26, -2.16, -2.07, -1.97, -1.88, -1.79, -1.70], ]) # lookup table for SA 1.0 SA1_TBL = np.array([ [-2.22, -1.98, -1.75, -1.52, -1.31, -1.12, -0.95, -0.80, -0.66, -0.54, -0.43, -0.33, -0.24, -0.15, -0.07, 0.01, 0.08, 0.16, 0.23, 0.29], [-2.32, -2.08, -1.85, -1.62, -1.41, -1.22, -1.05, -0.90, -0.77, -0.64, -0.54, -0.43, -0.34, -0.25, -0.17, -0.09, -0.02, 0.05, 0.12, 0.19], [-2.43, -2.18, -1.95, -1.72, -1.51, -1.32, -1.15, -1.00, -0.87, -0.75, -0.64, -0.54, -0.45, -0.36, -0.28, -0.20, -0.12, -0.05, 0.02, 0.09], [-2.53, -2.29, -2.05, -1.82, -1.61, -1.42, -1.25, -1.11, -0.97, -0.85, -0.74, -0.64, -0.55, -0.46, -0.38, -0.30, -0.23, -0.15, -0.08, -0.01], [-2.63, -2.39, -2.15, -1.92, -1.71, -1.53, -1.36, -1.21, -1.08, -0.96, -0.85, -0.75, -0.66, -0.57, -0.49, -0.41, -0.33, -0.26, -0.19, -0.12], [-2.74, -2.49, -2.25, -2.03, -1.82, -1.63, -1.46, -1.32, -1.18, -1.06, -0.96, -0.86, -0.76, -0.68, -0.59, -0.51, -0.44, -0.36, -0.29, -0.22], [-2.84, -2.60, -2.36, -2.13, -1.92, -1.74, -1.57, -1.42, -1.29, -1.17, -1.07, -0.97, -0.87, -0.78, -0.70, -0.62, -0.54, -0.47, -0.40, -0.33], [-2.95, -2.70, -2.47, -2.24, -2.03, -1.85, -1.68, -1.53, -1.40, -1.29, -1.18, -1.08, -0.98, -0.90, -0.81, -0.73, -0.65, -0.58, -0.51, -0.44], [-3.07, -2.82, -2.58, -2.35, -2.15, -1.96, -1.80, -1.65, -1.52, -1.40, -1.29, -1.19, -1.10, -1.01, -0.92, -0.84, -0.77, -0.69, -0.62, -0.55], [-3.14, -2.88, -2.64, -2.42, -2.21, -2.02, -1.86, -1.71, -1.58, -1.46, -1.36, -1.26, -1.16, -1.07, -0.99, -0.90, -0.83, -0.75, -0.68, -0.61], [-3.16, -2.91, -2.67, -2.44, -2.24, -2.05, -1.88, -1.74, -1.61, -1.49, -1.38, -1.28, -1.18, -1.09, -1.01, -0.92, -0.84, -0.77, -0.69, -0.62], [-3.20, -2.94, -2.70, -2.47, -2.27, -2.08, -1.91, -1.77, -1.63, -1.52, -1.41, -1.31, -1.21, -1.12, -1.03, -0.95, -0.87, -0.79, -0.71, -0.64], [-3.28, -3.03, -2.78, -2.55, -2.35, -2.16, -1.99, -1.84, -1.71, -1.59, -1.48, -1.38, -1.29, -1.19, -1.10, -1.02, -0.94, -0.86, -0.78, -0.71], [-3.38, -3.12, -2.88, -2.65, -2.44, -2.25, -2.09, -1.94, -1.81, -1.69, -1.58, -1.47, -1.37, -1.28, -1.19, -1.11, -1.02, -0.94, -0.87, -0.79], [-3.49, -3.23, -2.98, -2.75, -2.55, -2.36, -2.19, -2.04, -1.91, -1.79, -1.68, -1.57, -1.47, -1.38, -1.29, -1.20, -1.12, -1.03, -0.96, -0.88], [-3.61, -3.35, -3.10, -2.87, -2.66, -2.47, -2.30, -2.15, -2.02, -1.90, -1.79, -1.68, -1.58, -1.49, -1.39, -1.30, -1.22, -1.14, -1.05, -0.98], [-3.74, -3.48, -3.23, -3.00, -2.79, -2.60, -2.43, -2.28, -2.14, -2.02, -1.91, -1.80, -1.70, -1.60, -1.51, -1.42, -1.33, -1.25, -1.16, -1.09], [-3.88, -3.62, -3.37, -3.14, -2.93, -2.74, -2.57, -2.41, -2.28, -2.16, -2.04, -1.94, -1.83, -1.74, -1.64, -1.55, -1.46, -1.37, -1.29, -1.21], [-4.04, -3.78, -3.53, -3.30, -3.08, -2.89, -2.72, -2.57, -2.43, -2.31, -2.20, -2.09, -1.98, -1.88, -1.79, -1.69, -1.60, -1.51, -1.43, -1.35], [-4.22, -3.96, -3.71, -3.47, -3.26, -3.07, -2.90, -2.74, -2.61, -2.48, -2.37, -2.26, -2.15, -2.05, -1.96, -1.86, -1.77, -1.68, -1.59, -1.50], [-4.43, -4.16, -3.91, -3.68, -3.46, -3.27, -3.10, -2.94, -2.81, -2.68, -2.56, -2.45, -2.35, -2.25, -2.15, -2.05, -1.95, -1.86, -1.77, -1.69] ]) # lookup table for SA 2.0 SA2_TBL = np.array([ [-2.87, -2.66, -2.45, -2.24, -2.03, -1.81, -1.60, -1.39, -1.20, -1.02, -0.87, -0.73, -0.61, -0.50, -0.40, -0.30, -0.21, -0.13, -0.05, 0.02], [-3.00, -2.78, -2.57, -2.36, -2.14, -1.92, -1.70, -1.49, -1.30, -1.12, -0.97, -0.83, -0.71, -0.60, -0.50, -0.40, -0.32, -0.23, -0.16, -0.08], [-3.14, -2.91, -2.69, -2.47, -2.24, -2.02, -1.80, -1.59, -1.40, -1.22, -1.07, -0.93, -0.81, -0.70, -0.60, -0.51, -0.42, -0.34, -0.26, -0.18], [-3.27, -3.04, -2.81, -2.58, -2.35, -2.12, -1.90, -1.69, -1.50, -1.32, -1.17, -1.04, -0.91, -0.80, -0.70, -0.61, -0.52, -0.44, -0.36, -0.28], [-3.40, -3.16, -2.92, -2.69, -2.45, -2.22, -2.00, -1.79, -1.60, -1.43, -1.27, -1.14, -1.02, -0.91, -0.81, -0.71, -0.62, -0.54, -0.46, -0.39], [-3.52, -3.28, -3.03, -2.79, -2.55, -2.32, -2.10, -1.89, -1.70, -1.53, -1.38, -1.24, -1.12, -1.01, -0.91, -0.82, -0.73, -0.65, -0.57, -0.49], [-3.64, -3.39, -3.14, -2.90, -2.66, -2.42, -2.20, -1.99, -1.80, -1.63, -1.48, -1.35, -1.22, -1.12, -1.02, -0.92, -0.83, -0.75, -0.67, -0.60], [-3.76, -3.50, -3.25, -3.00, -2.76, -2.52, -2.30, -2.09, -1.90, -1.73, -1.58, -1.45, -1.33, -1.22, -1.12, -1.03, -0.94, -0.86, -0.78, -0.70], [-3.87, -3.61, -3.36, -3.11, -2.86, -2.63, -2.40, -2.19, -2.01, -1.84, -1.69, -1.56, -1.44, -1.33, -1.23, -1.14, -1.05, -0.96, -0.89, -0.81], [-3.93, -3.67, -3.42, -3.16, -2.92, -2.68, -2.45, -2.25, -2.06, -1.89, -1.75, -1.61, -1.49, -1.39, -1.29, -1.19, -1.10, -1.02, -0.94, -0.86], [-3.95, -3.69, -3.43, -3.18, -2.93, -2.69, -2.47, -2.26, -2.07, -1.90, -1.76, -1.63, -1.51, -1.40, -1.30, -1.20, -1.12, -1.03, -0.95, -0.87], [-3.97, -3.71, -3.45, -3.20, -2.95, -2.71, -2.48, -2.27, -2.09, -1.92, -1.77, -1.64, -1.52, -1.42, -1.32, -1.22, -1.13, -1.05, -0.97, -0.89], [-4.05, -3.78, -3.52, -3.26, -3.01, -2.77, -2.55, -2.34, -2.15, -1.99, -1.84, -1.71, -1.59, -1.48, -1.38, -1.28, -1.19, -1.11, -1.03, -0.95], [-4.14, -3.87, -3.60, -3.34, -3.09, -2.85, -2.62, -2.42, -2.23, -2.06, -1.91, -1.78, -1.66, -1.56, -1.45, -1.36, -1.27, -1.18, -1.10, -1.02], [-4.23, -3.96, -3.69, -3.43, -3.18, -2.94, -2.71, -2.50, -2.31, -2.15, -2.00, -1.87, -1.75, -1.64, -1.54, -1.44, -1.35, -1.26, -1.18, -1.10], [-4.34, -4.06, -3.79, -3.53, -3.27, -3.03, -2.80, -2.59, -2.40, -2.24, -2.09, -1.96, -1.84, -1.73, -1.62, -1.53, -1.44, -1.35, -1.26, -1.18], [-4.45, -4.18, -3.90, -3.64, -3.38, -3.13, -2.90, -2.69, -2.51, -2.34, -2.19, -2.06, -1.94, -1.83, -1.72, -1.62, -1.53, -1.44, -1.35, -1.27], [-4.58, -4.30, -4.02, -3.75, -3.49, -3.25, -3.02, -2.81, -2.62, -2.45, -2.30, -2.17, -2.04, -1.93, -1.83, -1.73, -1.63, -1.54, -1.46, -1.37], [-4.71, -4.43, -4.15, -3.88, -3.62, -3.37, -3.14, -2.93, -2.74, -2.57, -2.42, -2.29, -2.17, -2.05, -1.95, -1.85, -1.75, -1.66, -1.57, -1.48], [-4.86, -4.58, -4.30, -4.03, -3.76, -3.51, -3.28, -3.07, -2.88, -2.71, -2.56, -2.42, -2.30, -2.19, -2.08, -1.98, -1.88, -1.79, -1.70, -1.61], [-5.03, -4.74, -4.46, -4.19, -3.92, -3.67, -3.44, -3.23, -3.03, -2.86, -2.71, -2.58, -2.45, -2.34, -2.23, -2.13, -2.03, -1.93, -1.84, -1.75] ]) # dictionary relating supported imts and associated tables IMTS_TABLES = { PGA(): PGA_TBL, SA(period=0.1, damping=5): SA01_TBL, SA(period=0.2, damping=5): SA02_TBL, SA(period=0.3, damping=5): SA03_TBL, SA(period=0.5, damping=5): SA05_TBL, SA(period=1.0, damping=5): SA1_TBL, SA(period=2.0, damping=5): SA2_TBL } # period dependent standard deviations (in base 10) COEFFS = CoeffsTable(sa_damping=5, table="""\ IMT sigma pga 0.326 0.1 0.326 0.2 0.326 0.3 0.326 0.5 0.326 1.0 0.347 2.0 0.347 """) class FrankelEtAl1996MblgJ1996NSHMP2008(FrankelEtAl1996MblgAB1987NSHMP2008): """ Extend :class:`FrankelEtAl1996MblgAB1987NSHMP2008` but uses Johnston 1996 equation for converting from Mblg to Mw. """ def _convert_magnitude(self, mag): """ Convert magnitude from Mblg to Mw using Johnston 1996 equation """ return mblg_to_mw_johnston_96(mag) class FrankelEtAl1996MwNSHMP2008(FrankelEtAl1996MblgAB1987NSHMP2008): """ Extend :class:`FrankelEtAl1996MblgAB1987NSHMP2008` but assumes magnitude to be in Mw scale and therefore no conversion is applied. """ def _convert_magnitude(self, mag): """ Return magnitude value unchanged """ return mag
[ "numpy.zeros_like", "numpy.log", "numpy.zeros", "openquake.hazardlib.gsim.utils.mblg_to_mw_johnston_96", "openquake.hazardlib.imt.SA", "openquake.hazardlib.gsim.base.CoeffsTable", "openquake.hazardlib.imt.PGA", "openquake.hazardlib.gsim.utils.clip_mean", "numpy.array", "scipy.interpolate.RectBivariateSpline", "numpy.linspace", "openquake.hazardlib.gsim.utils.mblg_to_mw_atkinson_boore_87", "numpy.log10" ]
[((6389, 6414), 'numpy.linspace', 'np.linspace', (['(4.4)', '(8.2)', '(20)'], {}), '(4.4, 8.2, 20)\n', (6400, 6414), True, 'import numpy as np\n'), ((6549, 6574), 'numpy.linspace', 'np.linspace', (['(1.0)', '(3.0)', '(21)'], {}), '(1.0, 3.0, 21)\n', (6560, 6574), True, 'import numpy as np\n'), ((6615, 9731), 'numpy.array', 'np.array', (['[[-0.71, -0.62, -0.53, -0.44, -0.36, -0.29, -0.21, -0.14, -0.07, 0.0, 0.06,\n 0.12, 0.19, 0.25, 0.31, 0.37, 0.43, 0.49, 0.54, 0.6], [-0.84, -0.75, -\n 0.66, -0.57, -0.49, -0.41, -0.33, -0.26, -0.19, -0.12, -0.05, 0.01, \n 0.07, 0.14, 0.2, 0.26, 0.32, 0.38, 0.43, 0.49], [-0.98, -0.88, -0.79, -\n 0.7, -0.62, -0.53, -0.46, -0.38, -0.31, -0.24, -0.17, -0.1, -0.04, 0.02,\n 0.08, 0.14, 0.2, 0.26, 0.32, 0.38], [-1.12, -1.02, -0.93, -0.84, -0.75,\n -0.66, -0.58, -0.51, -0.43, -0.36, -0.29, -0.22, -0.16, -0.1, -0.03, \n 0.03, 0.09, 0.15, 0.21, 0.27], [-1.27, -1.17, -1.07, -0.98, -0.89, -0.8,\n -0.72, -0.64, -0.56, -0.49, -0.42, -0.35, -0.28, -0.22, -0.15, -0.09, -\n 0.03, 0.03, 0.09, 0.15], [-1.43, -1.32, -1.22, -1.13, -1.03, -0.94, -\n 0.86, -0.78, -0.7, -0.62, -0.55, -0.48, -0.41, -0.34, -0.28, -0.22, -\n 0.15, -0.09, -0.03, 0.03], [-1.59, -1.48, -1.38, -1.28, -1.19, -1.09, -\n 1.01, -0.92, -0.84, -0.76, -0.69, -0.61, -0.54, -0.48, -0.41, -0.35, -\n 0.28, -0.22, -0.16, -0.1], [-1.76, -1.65, -1.54, -1.44, -1.35, -1.25, -\n 1.16, -1.07, -0.99, -0.91, -0.83, -0.76, -0.69, -0.62, -0.55, -0.48, -\n 0.42, -0.35, -0.29, -0.23], [-1.93, -1.82, -1.72, -1.61, -1.51, -1.42, \n -1.33, -1.24, -1.15, -1.07, -0.99, -0.91, -0.83, -0.76, -0.69, -0.63, -\n 0.56, -0.5, -0.43, -0.37], [-2.07, -1.95, -1.84, -1.74, -1.64, -1.54, -\n 1.44, -1.35, -1.26, -1.18, -1.1, -1.02, -0.94, -0.86, -0.79, -0.72, -\n 0.65, -0.59, -0.53, -0.46], [-2.17, -2.05, -1.94, -1.83, -1.73, -1.63, \n -1.53, -1.43, -1.34, -1.25, -1.17, -1.09, -1.01, -0.93, -0.86, -0.78, -\n 0.71, -0.65, -0.58, -0.52], [-2.28, -2.16, -2.04, -1.93, -1.83, -1.72, \n -1.62, -1.53, -1.43, -1.34, -1.25, -1.17, -1.09, -1.01, -0.93, -0.86, -\n 0.78, -0.71, -0.65, -0.58], [-2.44, -2.32, -2.2, -2.09, -1.98, -1.87, -\n 1.77, -1.67, -1.58, -1.48, -1.39, -1.3, -1.22, -1.14, -1.06, -0.98, -\n 0.91, -0.83, -0.77, -0.7], [-2.63, -2.5, -2.38, -2.26, -2.15, -2.04, -\n 1.94, -1.84, -1.74, -1.64, -1.55, -1.46, -1.37, -1.29, -1.2, -1.12, -\n 1.05, -0.97, -0.9, -0.83], [-2.83, -2.7, -2.57, -2.45, -2.34, -2.23, -\n 2.12, -2.01, -1.91, -1.81, -1.72, -1.62, -1.53, -1.45, -1.36, -1.28, -\n 1.2, -1.12, -1.05, -0.98], [-3.05, -2.92, -2.78, -2.66, -2.54, -2.42, -\n 2.31, -2.2, -2.1, -2.0, -1.9, -1.8, -1.71, -1.62, -1.53, -1.45, -1.36, \n -1.28, -1.21, -1.13], [-3.29, -3.15, -3.01, -2.88, -2.75, -2.63, -2.52,\n -2.41, -2.3, -2.19, -2.09, -1.99, -1.9, -1.8, -1.71, -1.62, -1.54, -\n 1.46, -1.38, -1.3], [-3.55, -3.4, -3.25, -3.11, -2.98, -2.86, -2.74, -\n 2.62, -2.51, -2.4, -2.3, -2.19, -2.09, -2.0, -1.9, -1.81, -1.72, -1.64,\n -1.56, -1.48], [-3.84, -3.67, -3.52, -3.37, -3.23, -3.1, -2.97, -2.85, \n -2.73, -2.62, -2.51, -2.41, -2.3, -2.2, -2.1, -2.01, -1.92, -1.83, -\n 1.74, -1.66], [-4.15, -3.97, -3.8, -3.65, -3.5, -3.36, -3.22, -3.09, -\n 2.97, -2.85, -2.74, -2.63, -2.52, -2.42, -2.31, -2.22, -2.12, -2.03, -\n 1.94, -1.85], [-4.48, -4.29, -4.11, -3.94, -3.78, -3.63, -3.49, -3.35, \n -3.22, -3.1, -2.98, -2.86, -2.75, -2.64, -2.53, -2.43, -2.33, -2.23, -\n 2.14, -2.05]]'], {}), '([[-0.71, -0.62, -0.53, -0.44, -0.36, -0.29, -0.21, -0.14, -0.07, \n 0.0, 0.06, 0.12, 0.19, 0.25, 0.31, 0.37, 0.43, 0.49, 0.54, 0.6], [-0.84,\n -0.75, -0.66, -0.57, -0.49, -0.41, -0.33, -0.26, -0.19, -0.12, -0.05, \n 0.01, 0.07, 0.14, 0.2, 0.26, 0.32, 0.38, 0.43, 0.49], [-0.98, -0.88, -\n 0.79, -0.7, -0.62, -0.53, -0.46, -0.38, -0.31, -0.24, -0.17, -0.1, -\n 0.04, 0.02, 0.08, 0.14, 0.2, 0.26, 0.32, 0.38], [-1.12, -1.02, -0.93, -\n 0.84, -0.75, -0.66, -0.58, -0.51, -0.43, -0.36, -0.29, -0.22, -0.16, -\n 0.1, -0.03, 0.03, 0.09, 0.15, 0.21, 0.27], [-1.27, -1.17, -1.07, -0.98,\n -0.89, -0.8, -0.72, -0.64, -0.56, -0.49, -0.42, -0.35, -0.28, -0.22, -\n 0.15, -0.09, -0.03, 0.03, 0.09, 0.15], [-1.43, -1.32, -1.22, -1.13, -\n 1.03, -0.94, -0.86, -0.78, -0.7, -0.62, -0.55, -0.48, -0.41, -0.34, -\n 0.28, -0.22, -0.15, -0.09, -0.03, 0.03], [-1.59, -1.48, -1.38, -1.28, -\n 1.19, -1.09, -1.01, -0.92, -0.84, -0.76, -0.69, -0.61, -0.54, -0.48, -\n 0.41, -0.35, -0.28, -0.22, -0.16, -0.1], [-1.76, -1.65, -1.54, -1.44, -\n 1.35, -1.25, -1.16, -1.07, -0.99, -0.91, -0.83, -0.76, -0.69, -0.62, -\n 0.55, -0.48, -0.42, -0.35, -0.29, -0.23], [-1.93, -1.82, -1.72, -1.61, \n -1.51, -1.42, -1.33, -1.24, -1.15, -1.07, -0.99, -0.91, -0.83, -0.76, -\n 0.69, -0.63, -0.56, -0.5, -0.43, -0.37], [-2.07, -1.95, -1.84, -1.74, -\n 1.64, -1.54, -1.44, -1.35, -1.26, -1.18, -1.1, -1.02, -0.94, -0.86, -\n 0.79, -0.72, -0.65, -0.59, -0.53, -0.46], [-2.17, -2.05, -1.94, -1.83, \n -1.73, -1.63, -1.53, -1.43, -1.34, -1.25, -1.17, -1.09, -1.01, -0.93, -\n 0.86, -0.78, -0.71, -0.65, -0.58, -0.52], [-2.28, -2.16, -2.04, -1.93, \n -1.83, -1.72, -1.62, -1.53, -1.43, -1.34, -1.25, -1.17, -1.09, -1.01, -\n 0.93, -0.86, -0.78, -0.71, -0.65, -0.58], [-2.44, -2.32, -2.2, -2.09, -\n 1.98, -1.87, -1.77, -1.67, -1.58, -1.48, -1.39, -1.3, -1.22, -1.14, -\n 1.06, -0.98, -0.91, -0.83, -0.77, -0.7], [-2.63, -2.5, -2.38, -2.26, -\n 2.15, -2.04, -1.94, -1.84, -1.74, -1.64, -1.55, -1.46, -1.37, -1.29, -\n 1.2, -1.12, -1.05, -0.97, -0.9, -0.83], [-2.83, -2.7, -2.57, -2.45, -\n 2.34, -2.23, -2.12, -2.01, -1.91, -1.81, -1.72, -1.62, -1.53, -1.45, -\n 1.36, -1.28, -1.2, -1.12, -1.05, -0.98], [-3.05, -2.92, -2.78, -2.66, -\n 2.54, -2.42, -2.31, -2.2, -2.1, -2.0, -1.9, -1.8, -1.71, -1.62, -1.53, \n -1.45, -1.36, -1.28, -1.21, -1.13], [-3.29, -3.15, -3.01, -2.88, -2.75,\n -2.63, -2.52, -2.41, -2.3, -2.19, -2.09, -1.99, -1.9, -1.8, -1.71, -\n 1.62, -1.54, -1.46, -1.38, -1.3], [-3.55, -3.4, -3.25, -3.11, -2.98, -\n 2.86, -2.74, -2.62, -2.51, -2.4, -2.3, -2.19, -2.09, -2.0, -1.9, -1.81,\n -1.72, -1.64, -1.56, -1.48], [-3.84, -3.67, -3.52, -3.37, -3.23, -3.1, \n -2.97, -2.85, -2.73, -2.62, -2.51, -2.41, -2.3, -2.2, -2.1, -2.01, -\n 1.92, -1.83, -1.74, -1.66], [-4.15, -3.97, -3.8, -3.65, -3.5, -3.36, -\n 3.22, -3.09, -2.97, -2.85, -2.74, -2.63, -2.52, -2.42, -2.31, -2.22, -\n 2.12, -2.03, -1.94, -1.85], [-4.48, -4.29, -4.11, -3.94, -3.78, -3.63, \n -3.49, -3.35, -3.22, -3.1, -2.98, -2.86, -2.75, -2.64, -2.53, -2.43, -\n 2.33, -2.23, -2.14, -2.05]])\n', (6623, 9731), True, 'import numpy as np\n'), ((9828, 12900), 'numpy.array', 'np.array', (['[[-0.43, -0.32, -0.23, -0.14, -0.05, 0.03, 0.11, 0.19, 0.26, 0.33, 0.4, \n 0.47, 0.54, 0.6, 0.66, 0.72, 0.78, 0.84, 0.9, 0.96], [-0.55, -0.44, -\n 0.35, -0.26, -0.17, -0.08, 0.0, 0.07, 0.15, 0.22, 0.29, 0.36, 0.43, \n 0.49, 0.55, 0.62, 0.68, 0.74, 0.8, 0.86], [-0.67, -0.57, -0.47, -0.38, \n -0.29, -0.2, -0.12, -0.04, 0.03, 0.11, 0.18, 0.25, 0.31, 0.38, 0.44, \n 0.51, 0.57, 0.63, 0.69, 0.75], [-0.8, -0.69, -0.6, -0.5, -0.41, -0.33, \n -0.24, -0.16, -0.09, -0.01, 0.06, 0.13, 0.2, 0.27, 0.33, 0.39, 0.46, \n 0.52, 0.58, 0.64], [-0.93, -0.83, -0.73, -0.63, -0.54, -0.45, -0.37, -\n 0.29, -0.21, -0.13, -0.06, 0.01, 0.08, 0.15, 0.21, 0.28, 0.34, 0.4, \n 0.46, 0.52], [-1.07, -0.97, -0.86, -0.77, -0.68, -0.59, -0.5, -0.42, -\n 0.34, -0.26, -0.18, -0.11, -0.04, 0.03, 0.09, 0.16, 0.22, 0.28, 0.35, \n 0.41], [-1.22, -1.11, -1.01, -0.91, -0.82, -0.73, -0.64, -0.55, -0.47, \n -0.39, -0.32, -0.24, -0.17, -0.1, -0.03, 0.03, 0.1, 0.16, 0.22, 0.28],\n [-1.37, -1.26, -1.16, -1.06, -0.97, -0.87, -0.78, -0.7, -0.61, -0.53, -\n 0.45, -0.38, -0.3, -0.23, -0.16, -0.1, -0.03, 0.03, 0.1, 0.16], [-1.53,\n -1.42, -1.32, -1.22, -1.12, -1.03, -0.94, -0.85, -0.76, -0.68, -0.6, -\n 0.52, -0.45, -0.37, -0.3, -0.23, -0.17, -0.1, -0.04, 0.02], [-1.65, -\n 1.54, -1.43, -1.33, -1.24, -1.14, -1.05, -0.96, -0.87, -0.79, -0.7, -\n 0.62, -0.54, -0.47, -0.4, -0.33, -0.26, -0.19, -0.13, -0.06], [-1.73, -\n 1.62, -1.51, -1.41, -1.31, -1.22, -1.12, -1.03, -0.94, -0.86, -0.77, -\n 0.69, -0.61, -0.53, -0.46, -0.39, -0.32, -0.25, -0.18, -0.12], [-1.83, \n -1.72, -1.61, -1.51, -1.41, -1.31, -1.21, -1.12, -1.03, -0.94, -0.85, -\n 0.77, -0.69, -0.61, -0.53, -0.46, -0.39, -0.32, -0.25, -0.18], [-1.98, \n -1.87, -1.76, -1.66, -1.56, -1.46, -1.36, -1.27, -1.18, -1.09, -1.0, -\n 0.91, -0.83, -0.75, -0.67, -0.59, -0.52, -0.44, -0.37, -0.31], [-2.17, \n -2.05, -1.95, -1.84, -1.74, -1.64, -1.54, -1.45, -1.35, -1.26, -1.17, -\n 1.08, -0.99, -0.91, -0.83, -0.75, -0.67, -0.6, -0.53, -0.46], [-2.38, -\n 2.26, -2.15, -2.05, -1.94, -1.84, -1.74, -1.64, -1.55, -1.45, -1.36, -\n 1.27, -1.18, -1.1, -1.01, -0.93, -0.85, -0.78, -0.7, -0.63], [-2.61, -\n 2.5, -2.39, -2.28, -2.17, -2.07, -1.97, -1.87, -1.77, -1.68, -1.58, -\n 1.49, -1.4, -1.31, -1.22, -1.14, -1.06, -0.98, -0.91, -0.83], [-2.89, -\n 2.77, -2.66, -2.55, -2.44, -2.33, -2.23, -2.13, -2.03, -1.93, -1.83, -\n 1.74, -1.64, -1.55, -1.46, -1.38, -1.29, -1.21, -1.13, -1.06], [-3.2, -\n 3.08, -2.96, -2.85, -2.73, -2.62, -2.51, -2.41, -2.31, -2.2, -2.1, -\n 2.01, -1.91, -1.82, -1.72, -1.63, -1.55, -1.46, -1.38, -1.3], [-3.56, -\n 3.43, -3.3, -3.18, -3.06, -2.94, -2.82, -2.71, -2.6, -2.49, -2.39, -\n 2.29, -2.19, -2.09, -1.99, -1.9, -1.81, -1.72, -1.64, -1.56], [-3.96, -\n 3.81, -3.67, -3.53, -3.39, -3.26, -3.14, -3.02, -2.9, -2.78, -2.67, -\n 2.57, -2.46, -2.36, -2.26, -2.16, -2.07, -1.97, -1.88, -1.8], [-4.37, -\n 4.2, -4.04, -3.88, -3.73, -3.59, -3.45, -3.32, -3.19, -3.07, -2.95, -\n 2.83, -2.72, -2.61, -2.51, -2.4, -2.31, -2.21, -2.12, -2.03]]'], {}), '([[-0.43, -0.32, -0.23, -0.14, -0.05, 0.03, 0.11, 0.19, 0.26, 0.33,\n 0.4, 0.47, 0.54, 0.6, 0.66, 0.72, 0.78, 0.84, 0.9, 0.96], [-0.55, -0.44,\n -0.35, -0.26, -0.17, -0.08, 0.0, 0.07, 0.15, 0.22, 0.29, 0.36, 0.43, \n 0.49, 0.55, 0.62, 0.68, 0.74, 0.8, 0.86], [-0.67, -0.57, -0.47, -0.38, \n -0.29, -0.2, -0.12, -0.04, 0.03, 0.11, 0.18, 0.25, 0.31, 0.38, 0.44, \n 0.51, 0.57, 0.63, 0.69, 0.75], [-0.8, -0.69, -0.6, -0.5, -0.41, -0.33, \n -0.24, -0.16, -0.09, -0.01, 0.06, 0.13, 0.2, 0.27, 0.33, 0.39, 0.46, \n 0.52, 0.58, 0.64], [-0.93, -0.83, -0.73, -0.63, -0.54, -0.45, -0.37, -\n 0.29, -0.21, -0.13, -0.06, 0.01, 0.08, 0.15, 0.21, 0.28, 0.34, 0.4, \n 0.46, 0.52], [-1.07, -0.97, -0.86, -0.77, -0.68, -0.59, -0.5, -0.42, -\n 0.34, -0.26, -0.18, -0.11, -0.04, 0.03, 0.09, 0.16, 0.22, 0.28, 0.35, \n 0.41], [-1.22, -1.11, -1.01, -0.91, -0.82, -0.73, -0.64, -0.55, -0.47, \n -0.39, -0.32, -0.24, -0.17, -0.1, -0.03, 0.03, 0.1, 0.16, 0.22, 0.28],\n [-1.37, -1.26, -1.16, -1.06, -0.97, -0.87, -0.78, -0.7, -0.61, -0.53, -\n 0.45, -0.38, -0.3, -0.23, -0.16, -0.1, -0.03, 0.03, 0.1, 0.16], [-1.53,\n -1.42, -1.32, -1.22, -1.12, -1.03, -0.94, -0.85, -0.76, -0.68, -0.6, -\n 0.52, -0.45, -0.37, -0.3, -0.23, -0.17, -0.1, -0.04, 0.02], [-1.65, -\n 1.54, -1.43, -1.33, -1.24, -1.14, -1.05, -0.96, -0.87, -0.79, -0.7, -\n 0.62, -0.54, -0.47, -0.4, -0.33, -0.26, -0.19, -0.13, -0.06], [-1.73, -\n 1.62, -1.51, -1.41, -1.31, -1.22, -1.12, -1.03, -0.94, -0.86, -0.77, -\n 0.69, -0.61, -0.53, -0.46, -0.39, -0.32, -0.25, -0.18, -0.12], [-1.83, \n -1.72, -1.61, -1.51, -1.41, -1.31, -1.21, -1.12, -1.03, -0.94, -0.85, -\n 0.77, -0.69, -0.61, -0.53, -0.46, -0.39, -0.32, -0.25, -0.18], [-1.98, \n -1.87, -1.76, -1.66, -1.56, -1.46, -1.36, -1.27, -1.18, -1.09, -1.0, -\n 0.91, -0.83, -0.75, -0.67, -0.59, -0.52, -0.44, -0.37, -0.31], [-2.17, \n -2.05, -1.95, -1.84, -1.74, -1.64, -1.54, -1.45, -1.35, -1.26, -1.17, -\n 1.08, -0.99, -0.91, -0.83, -0.75, -0.67, -0.6, -0.53, -0.46], [-2.38, -\n 2.26, -2.15, -2.05, -1.94, -1.84, -1.74, -1.64, -1.55, -1.45, -1.36, -\n 1.27, -1.18, -1.1, -1.01, -0.93, -0.85, -0.78, -0.7, -0.63], [-2.61, -\n 2.5, -2.39, -2.28, -2.17, -2.07, -1.97, -1.87, -1.77, -1.68, -1.58, -\n 1.49, -1.4, -1.31, -1.22, -1.14, -1.06, -0.98, -0.91, -0.83], [-2.89, -\n 2.77, -2.66, -2.55, -2.44, -2.33, -2.23, -2.13, -2.03, -1.93, -1.83, -\n 1.74, -1.64, -1.55, -1.46, -1.38, -1.29, -1.21, -1.13, -1.06], [-3.2, -\n 3.08, -2.96, -2.85, -2.73, -2.62, -2.51, -2.41, -2.31, -2.2, -2.1, -\n 2.01, -1.91, -1.82, -1.72, -1.63, -1.55, -1.46, -1.38, -1.3], [-3.56, -\n 3.43, -3.3, -3.18, -3.06, -2.94, -2.82, -2.71, -2.6, -2.49, -2.39, -\n 2.29, -2.19, -2.09, -1.99, -1.9, -1.81, -1.72, -1.64, -1.56], [-3.96, -\n 3.81, -3.67, -3.53, -3.39, -3.26, -3.14, -3.02, -2.9, -2.78, -2.67, -\n 2.57, -2.46, -2.36, -2.26, -2.16, -2.07, -1.97, -1.88, -1.8], [-4.37, -\n 4.2, -4.04, -3.88, -3.73, -3.59, -3.45, -3.32, -3.19, -3.07, -2.95, -\n 2.83, -2.72, -2.61, -2.51, -2.4, -2.31, -2.21, -2.12, -2.03]])\n', (9836, 12900), True, 'import numpy as np\n'), ((13041, 16110), 'numpy.array', 'np.array', (['[[-0.71, -0.57, -0.46, -0.35, -0.25, -0.15, -0.06, 0.02, 0.1, 0.18, 0.25, \n 0.32, 0.39, 0.46, 0.53, 0.59, 0.65, 0.72, 0.78, 0.84], [-0.82, -0.69, -\n 0.57, -0.46, -0.36, -0.26, -0.17, -0.09, -0.01, 0.07, 0.14, 0.22, 0.29,\n 0.35, 0.42, 0.48, 0.55, 0.61, 0.67, 0.73], [-0.93, -0.8, -0.68, -0.57, \n -0.47, -0.38, -0.29, -0.2, -0.12, -0.04, 0.03, 0.11, 0.18, 0.25, 0.31, \n 0.38, 0.44, 0.5, 0.56, 0.63], [-1.05, -0.92, -0.8, -0.69, -0.59, -0.49,\n -0.4, -0.32, -0.23, -0.15, -0.08, 0.0, 0.07, 0.14, 0.2, 0.27, 0.33, 0.4,\n 0.46, 0.52], [-1.17, -1.04, -0.92, -0.81, -0.71, -0.61, -0.52, -0.44, -\n 0.35, -0.27, -0.19, -0.12, -0.05, 0.02, 0.09, 0.16, 0.22, 0.29, 0.35, \n 0.41], [-1.3, -1.17, -1.05, -0.94, -0.84, -0.74, -0.65, -0.56, -0.47, -\n 0.39, -0.31, -0.24, -0.16, -0.09, -0.02, 0.04, 0.11, 0.17, 0.24, 0.3],\n [-1.43, -1.3, -1.18, -1.07, -0.97, -0.87, -0.77, -0.69, -0.6, -0.52, -\n 0.44, -0.36, -0.28, -0.21, -0.14, -0.07, -0.01, 0.06, 0.12, 0.18], [-\n 1.57, -1.44, -1.32, -1.21, -1.1, -1.0, -0.91, -0.82, -0.73, -0.65, -\n 0.56, -0.49, -0.41, -0.34, -0.27, -0.2, -0.13, -0.06, 0.0, 0.06], [-\n 1.72, -1.58, -1.46, -1.35, -1.25, -1.15, -1.05, -0.96, -0.87, -0.78, -\n 0.7, -0.62, -0.54, -0.47, -0.39, -0.32, -0.25, -0.19, -0.12, -0.06], [-\n 1.82, -1.68, -1.56, -1.45, -1.34, -1.24, -1.14, -1.05, -0.96, -0.87, -\n 0.79, -0.7, -0.63, -0.55, -0.47, -0.4, -0.33, -0.26, -0.2, -0.13], [-\n 1.88, -1.74, -1.62, -1.51, -1.4, -1.3, -1.2, -1.1, -1.01, -0.92, -0.84,\n -0.75, -0.67, -0.59, -0.52, -0.44, -0.37, -0.3, -0.24, -0.17], [-1.95, \n -1.82, -1.69, -1.58, -1.47, -1.37, -1.27, -1.17, -1.08, -0.99, -0.9, -\n 0.81, -0.73, -0.65, -0.57, -0.5, -0.42, -0.35, -0.28, -0.22], [-2.08, -\n 1.94, -1.82, -1.7, -1.59, -1.49, -1.39, -1.29, -1.2, -1.11, -1.02, -\n 0.93, -0.84, -0.76, -0.68, -0.6, -0.53, -0.45, -0.38, -0.32], [-2.23, -\n 2.09, -1.97, -1.85, -1.74, -1.64, -1.53, -1.44, -1.34, -1.25, -1.15, -\n 1.06, -0.98, -0.89, -0.81, -0.73, -0.65, -0.58, -0.51, -0.44], [-2.4, -\n 2.26, -2.13, -2.01, -1.9, -1.8, -1.7, -1.6, -1.5, -1.4, -1.31, -1.22, -\n 1.13, -1.04, -0.96, -0.88, -0.8, -0.72, -0.65, -0.58], [-2.58, -2.44, -\n 2.32, -2.2, -2.09, -1.98, -1.88, -1.78, -1.68, -1.58, -1.49, -1.39, -\n 1.3, -1.22, -1.13, -1.04, -0.96, -0.88, -0.81, -0.73], [-2.8, -2.66, -\n 2.53, -2.41, -2.3, -2.19, -2.09, -1.98, -1.88, -1.79, -1.69, -1.6, -1.5,\n -1.41, -1.32, -1.24, -1.15, -1.07, -0.99, -0.92], [-3.04, -2.9, -2.77, \n -2.65, -2.54, -2.43, -2.32, -2.22, -2.12, -2.02, -1.92, -1.83, -1.73, -\n 1.64, -1.55, -1.46, -1.37, -1.29, -1.21, -1.13], [-3.33, -3.19, -3.06, \n -2.93, -2.82, -2.71, -2.6, -2.49, -2.39, -2.29, -2.19, -2.09, -1.99, -\n 1.9, -1.8, -1.71, -1.62, -1.54, -1.45, -1.37], [-3.66, -3.52, -3.38, -\n 3.26, -3.14, -3.02, -2.91, -2.8, -2.69, -2.59, -2.48, -2.38, -2.28, -\n 2.18, -2.09, -1.99, -1.9, -1.81, -1.73, -1.64], [-4.05, -3.9, -3.76, -\n 3.63, -3.5, -3.38, -3.26, -3.14, -3.03, -2.92, -2.81, -2.7, -2.59, -\n 2.49, -2.39, -2.29, -2.19, -2.1, -2.01, -1.92]]'], {}), '([[-0.71, -0.57, -0.46, -0.35, -0.25, -0.15, -0.06, 0.02, 0.1, 0.18,\n 0.25, 0.32, 0.39, 0.46, 0.53, 0.59, 0.65, 0.72, 0.78, 0.84], [-0.82, -\n 0.69, -0.57, -0.46, -0.36, -0.26, -0.17, -0.09, -0.01, 0.07, 0.14, 0.22,\n 0.29, 0.35, 0.42, 0.48, 0.55, 0.61, 0.67, 0.73], [-0.93, -0.8, -0.68, -\n 0.57, -0.47, -0.38, -0.29, -0.2, -0.12, -0.04, 0.03, 0.11, 0.18, 0.25, \n 0.31, 0.38, 0.44, 0.5, 0.56, 0.63], [-1.05, -0.92, -0.8, -0.69, -0.59, \n -0.49, -0.4, -0.32, -0.23, -0.15, -0.08, 0.0, 0.07, 0.14, 0.2, 0.27, \n 0.33, 0.4, 0.46, 0.52], [-1.17, -1.04, -0.92, -0.81, -0.71, -0.61, -\n 0.52, -0.44, -0.35, -0.27, -0.19, -0.12, -0.05, 0.02, 0.09, 0.16, 0.22,\n 0.29, 0.35, 0.41], [-1.3, -1.17, -1.05, -0.94, -0.84, -0.74, -0.65, -\n 0.56, -0.47, -0.39, -0.31, -0.24, -0.16, -0.09, -0.02, 0.04, 0.11, 0.17,\n 0.24, 0.3], [-1.43, -1.3, -1.18, -1.07, -0.97, -0.87, -0.77, -0.69, -\n 0.6, -0.52, -0.44, -0.36, -0.28, -0.21, -0.14, -0.07, -0.01, 0.06, 0.12,\n 0.18], [-1.57, -1.44, -1.32, -1.21, -1.1, -1.0, -0.91, -0.82, -0.73, -\n 0.65, -0.56, -0.49, -0.41, -0.34, -0.27, -0.2, -0.13, -0.06, 0.0, 0.06],\n [-1.72, -1.58, -1.46, -1.35, -1.25, -1.15, -1.05, -0.96, -0.87, -0.78, \n -0.7, -0.62, -0.54, -0.47, -0.39, -0.32, -0.25, -0.19, -0.12, -0.06], [\n -1.82, -1.68, -1.56, -1.45, -1.34, -1.24, -1.14, -1.05, -0.96, -0.87, -\n 0.79, -0.7, -0.63, -0.55, -0.47, -0.4, -0.33, -0.26, -0.2, -0.13], [-\n 1.88, -1.74, -1.62, -1.51, -1.4, -1.3, -1.2, -1.1, -1.01, -0.92, -0.84,\n -0.75, -0.67, -0.59, -0.52, -0.44, -0.37, -0.3, -0.24, -0.17], [-1.95, \n -1.82, -1.69, -1.58, -1.47, -1.37, -1.27, -1.17, -1.08, -0.99, -0.9, -\n 0.81, -0.73, -0.65, -0.57, -0.5, -0.42, -0.35, -0.28, -0.22], [-2.08, -\n 1.94, -1.82, -1.7, -1.59, -1.49, -1.39, -1.29, -1.2, -1.11, -1.02, -\n 0.93, -0.84, -0.76, -0.68, -0.6, -0.53, -0.45, -0.38, -0.32], [-2.23, -\n 2.09, -1.97, -1.85, -1.74, -1.64, -1.53, -1.44, -1.34, -1.25, -1.15, -\n 1.06, -0.98, -0.89, -0.81, -0.73, -0.65, -0.58, -0.51, -0.44], [-2.4, -\n 2.26, -2.13, -2.01, -1.9, -1.8, -1.7, -1.6, -1.5, -1.4, -1.31, -1.22, -\n 1.13, -1.04, -0.96, -0.88, -0.8, -0.72, -0.65, -0.58], [-2.58, -2.44, -\n 2.32, -2.2, -2.09, -1.98, -1.88, -1.78, -1.68, -1.58, -1.49, -1.39, -\n 1.3, -1.22, -1.13, -1.04, -0.96, -0.88, -0.81, -0.73], [-2.8, -2.66, -\n 2.53, -2.41, -2.3, -2.19, -2.09, -1.98, -1.88, -1.79, -1.69, -1.6, -1.5,\n -1.41, -1.32, -1.24, -1.15, -1.07, -0.99, -0.92], [-3.04, -2.9, -2.77, \n -2.65, -2.54, -2.43, -2.32, -2.22, -2.12, -2.02, -1.92, -1.83, -1.73, -\n 1.64, -1.55, -1.46, -1.37, -1.29, -1.21, -1.13], [-3.33, -3.19, -3.06, \n -2.93, -2.82, -2.71, -2.6, -2.49, -2.39, -2.29, -2.19, -2.09, -1.99, -\n 1.9, -1.8, -1.71, -1.62, -1.54, -1.45, -1.37], [-3.66, -3.52, -3.38, -\n 3.26, -3.14, -3.02, -2.91, -2.8, -2.69, -2.59, -2.48, -2.38, -2.28, -\n 2.18, -2.09, -1.99, -1.9, -1.81, -1.73, -1.64], [-4.05, -3.9, -3.76, -\n 3.63, -3.5, -3.38, -3.26, -3.14, -3.03, -2.92, -2.81, -2.7, -2.59, -\n 2.49, -2.39, -2.29, -2.19, -2.1, -2.01, -1.92]])\n', (13049, 16110), True, 'import numpy as np\n'), ((16254, 19361), 'numpy.array', 'np.array', (['[[-0.97, -0.81, -0.66, -0.53, -0.42, -0.31, -0.21, -0.12, -0.03, 0.05, 0.13,\n 0.2, 0.28, 0.35, 0.41, 0.48, 0.54, 0.61, 0.67, 0.73], [-1.08, -0.91, -\n 0.77, -0.64, -0.53, -0.42, -0.32, -0.23, -0.14, -0.06, 0.02, 0.1, 0.17,\n 0.24, 0.31, 0.38, 0.44, 0.5, 0.57, 0.63], [-1.19, -1.02, -0.88, -0.75, \n -0.64, -0.53, -0.43, -0.34, -0.25, -0.17, -0.09, -0.01, 0.06, 0.13, 0.2,\n 0.27, 0.33, 0.4, 0.46, 0.52], [-1.3, -1.14, -0.99, -0.86, -0.75, -0.64,\n -0.54, -0.45, -0.36, -0.28, -0.2, -0.12, -0.05, 0.03, 0.09, 0.16, 0.23,\n 0.29, 0.36, 0.42], [-1.41, -1.25, -1.11, -0.98, -0.86, -0.76, -0.66, -\n 0.57, -0.48, -0.39, -0.31, -0.23, -0.16, -0.09, -0.02, 0.05, 0.12, 0.18,\n 0.25, 0.31], [-1.53, -1.37, -1.23, -1.1, -0.98, -0.88, -0.78, -0.68, -\n 0.59, -0.51, -0.43, -0.35, -0.27, -0.2, -0.13, -0.06, 0.01, 0.07, 0.14,\n 0.2], [-1.66, -1.5, -1.35, -1.23, -1.11, -1.0, -0.9, -0.81, -0.72, -\n 0.63, -0.55, -0.47, -0.39, -0.31, -0.24, -0.17, -0.11, -0.04, 0.02, \n 0.09], [-1.79, -1.63, -1.48, -1.36, -1.24, -1.13, -1.03, -0.93, -0.84, \n -0.75, -0.67, -0.59, -0.51, -0.44, -0.36, -0.29, -0.22, -0.16, -0.09, -\n 0.03], [-1.93, -1.77, -1.62, -1.49, -1.37, -1.26, -1.16, -1.07, -0.97, \n -0.88, -0.8, -0.72, -0.64, -0.56, -0.49, -0.41, -0.34, -0.28, -0.21, -\n 0.15], [-2.02, -1.85, -1.71, -1.58, -1.46, -1.35, -1.25, -1.15, -1.06, \n -0.97, -0.88, -0.8, -0.71, -0.64, -0.56, -0.49, -0.42, -0.35, -0.28, -\n 0.21], [-2.07, -1.91, -1.76, -1.63, -1.51, -1.4, -1.3, -1.2, -1.1, -\n 1.01, -0.92, -0.84, -0.75, -0.67, -0.6, -0.52, -0.45, -0.38, -0.31, -\n 0.24], [-2.13, -1.97, -1.82, -1.69, -1.57, -1.46, -1.35, -1.25, -1.16, \n -1.06, -0.97, -0.89, -0.8, -0.72, -0.64, -0.56, -0.49, -0.42, -0.35, -\n 0.28], [-2.25, -2.08, -1.93, -1.8, -1.68, -1.57, -1.46, -1.36, -1.26, -\n 1.17, -1.08, -0.99, -0.9, -0.82, -0.74, -0.66, -0.58, -0.51, -0.44, -\n 0.37], [-2.38, -2.21, -2.06, -1.93, -1.81, -1.7, -1.59, -1.49, -1.39, -\n 1.29, -1.2, -1.11, -1.02, -0.94, -0.85, -0.77, -0.7, -0.62, -0.55, -\n 0.48], [-2.53, -2.36, -2.21, -2.08, -1.96, -1.84, -1.73, -1.63, -1.53, \n -1.43, -1.34, -1.25, -1.16, -1.07, -0.99, -0.9, -0.82, -0.75, -0.67, -\n 0.6], [-2.69, -2.52, -2.37, -2.24, -2.12, -2.0, -1.89, -1.79, -1.69, -\n 1.59, -1.5, -1.4, -1.31, -1.22, -1.13, -1.05, -0.97, -0.89, -0.81, -\n 0.74], [-2.88, -2.71, -2.56, -2.42, -2.3, -2.18, -2.08, -1.97, -1.87, -\n 1.77, -1.67, -1.58, -1.48, -1.39, -1.3, -1.22, -1.13, -1.05, -0.97, -\n 0.9], [-3.09, -2.92, -2.77, -2.63, -2.51, -2.39, -2.28, -2.18, -2.07, -\n 1.97, -1.87, -1.78, -1.68, -1.59, -1.5, -1.41, -1.32, -1.24, -1.16, -\n 1.08], [-3.33, -3.16, -3.01, -2.87, -2.75, -2.63, -2.52, -2.41, -2.31, \n -2.2, -2.11, -2.01, -1.91, -1.82, -1.72, -1.63, -1.54, -1.46, -1.37, -\n 1.29], [-3.61, -3.44, -3.29, -3.15, -3.02, -2.91, -2.79, -2.68, -2.58, \n -2.47, -2.37, -2.27, -2.17, -2.08, -1.98, -1.89, -1.8, -1.71, -1.62, -\n 1.54], [-3.95, -3.77, -3.62, -3.48, -3.35, -3.22, -3.11, -3.0, -2.89, -\n 2.78, -2.67, -2.57, -2.47, -2.37, -2.27, -2.17, -2.08, -1.99, -1.9, -1.81]]'], {}), '([[-0.97, -0.81, -0.66, -0.53, -0.42, -0.31, -0.21, -0.12, -0.03, \n 0.05, 0.13, 0.2, 0.28, 0.35, 0.41, 0.48, 0.54, 0.61, 0.67, 0.73], [-\n 1.08, -0.91, -0.77, -0.64, -0.53, -0.42, -0.32, -0.23, -0.14, -0.06, \n 0.02, 0.1, 0.17, 0.24, 0.31, 0.38, 0.44, 0.5, 0.57, 0.63], [-1.19, -\n 1.02, -0.88, -0.75, -0.64, -0.53, -0.43, -0.34, -0.25, -0.17, -0.09, -\n 0.01, 0.06, 0.13, 0.2, 0.27, 0.33, 0.4, 0.46, 0.52], [-1.3, -1.14, -\n 0.99, -0.86, -0.75, -0.64, -0.54, -0.45, -0.36, -0.28, -0.2, -0.12, -\n 0.05, 0.03, 0.09, 0.16, 0.23, 0.29, 0.36, 0.42], [-1.41, -1.25, -1.11, \n -0.98, -0.86, -0.76, -0.66, -0.57, -0.48, -0.39, -0.31, -0.23, -0.16, -\n 0.09, -0.02, 0.05, 0.12, 0.18, 0.25, 0.31], [-1.53, -1.37, -1.23, -1.1,\n -0.98, -0.88, -0.78, -0.68, -0.59, -0.51, -0.43, -0.35, -0.27, -0.2, -\n 0.13, -0.06, 0.01, 0.07, 0.14, 0.2], [-1.66, -1.5, -1.35, -1.23, -1.11,\n -1.0, -0.9, -0.81, -0.72, -0.63, -0.55, -0.47, -0.39, -0.31, -0.24, -\n 0.17, -0.11, -0.04, 0.02, 0.09], [-1.79, -1.63, -1.48, -1.36, -1.24, -\n 1.13, -1.03, -0.93, -0.84, -0.75, -0.67, -0.59, -0.51, -0.44, -0.36, -\n 0.29, -0.22, -0.16, -0.09, -0.03], [-1.93, -1.77, -1.62, -1.49, -1.37, \n -1.26, -1.16, -1.07, -0.97, -0.88, -0.8, -0.72, -0.64, -0.56, -0.49, -\n 0.41, -0.34, -0.28, -0.21, -0.15], [-2.02, -1.85, -1.71, -1.58, -1.46, \n -1.35, -1.25, -1.15, -1.06, -0.97, -0.88, -0.8, -0.71, -0.64, -0.56, -\n 0.49, -0.42, -0.35, -0.28, -0.21], [-2.07, -1.91, -1.76, -1.63, -1.51, \n -1.4, -1.3, -1.2, -1.1, -1.01, -0.92, -0.84, -0.75, -0.67, -0.6, -0.52,\n -0.45, -0.38, -0.31, -0.24], [-2.13, -1.97, -1.82, -1.69, -1.57, -1.46,\n -1.35, -1.25, -1.16, -1.06, -0.97, -0.89, -0.8, -0.72, -0.64, -0.56, -\n 0.49, -0.42, -0.35, -0.28], [-2.25, -2.08, -1.93, -1.8, -1.68, -1.57, -\n 1.46, -1.36, -1.26, -1.17, -1.08, -0.99, -0.9, -0.82, -0.74, -0.66, -\n 0.58, -0.51, -0.44, -0.37], [-2.38, -2.21, -2.06, -1.93, -1.81, -1.7, -\n 1.59, -1.49, -1.39, -1.29, -1.2, -1.11, -1.02, -0.94, -0.85, -0.77, -\n 0.7, -0.62, -0.55, -0.48], [-2.53, -2.36, -2.21, -2.08, -1.96, -1.84, -\n 1.73, -1.63, -1.53, -1.43, -1.34, -1.25, -1.16, -1.07, -0.99, -0.9, -\n 0.82, -0.75, -0.67, -0.6], [-2.69, -2.52, -2.37, -2.24, -2.12, -2.0, -\n 1.89, -1.79, -1.69, -1.59, -1.5, -1.4, -1.31, -1.22, -1.13, -1.05, -\n 0.97, -0.89, -0.81, -0.74], [-2.88, -2.71, -2.56, -2.42, -2.3, -2.18, -\n 2.08, -1.97, -1.87, -1.77, -1.67, -1.58, -1.48, -1.39, -1.3, -1.22, -\n 1.13, -1.05, -0.97, -0.9], [-3.09, -2.92, -2.77, -2.63, -2.51, -2.39, -\n 2.28, -2.18, -2.07, -1.97, -1.87, -1.78, -1.68, -1.59, -1.5, -1.41, -\n 1.32, -1.24, -1.16, -1.08], [-3.33, -3.16, -3.01, -2.87, -2.75, -2.63, \n -2.52, -2.41, -2.31, -2.2, -2.11, -2.01, -1.91, -1.82, -1.72, -1.63, -\n 1.54, -1.46, -1.37, -1.29], [-3.61, -3.44, -3.29, -3.15, -3.02, -2.91, \n -2.79, -2.68, -2.58, -2.47, -2.37, -2.27, -2.17, -2.08, -1.98, -1.89, -\n 1.8, -1.71, -1.62, -1.54], [-3.95, -3.77, -3.62, -3.48, -3.35, -3.22, -\n 3.11, -3.0, -2.89, -2.78, -2.67, -2.57, -2.47, -2.37, -2.27, -2.17, -\n 2.08, -1.99, -1.9, -1.81]])\n', (16262, 19361), True, 'import numpy as np\n'), ((19467, 22589), 'numpy.array', 'np.array', (['[[-1.44, -1.23, -1.03, -0.87, -0.72, -0.58, -0.46, -0.35, -0.25, -0.16, -\n 0.07, 0.01, 0.09, 0.16, 0.23, 0.3, 0.37, 0.44, 0.5, 0.57], [-1.54, -\n 1.33, -1.14, -0.97, -0.82, -0.69, -0.57, -0.46, -0.36, -0.27, -0.18, -\n 0.1, -0.02, 0.06, 0.13, 0.2, 0.27, 0.34, 0.4, 0.46], [-1.64, -1.43, -\n 1.24, -1.07, -0.92, -0.79, -0.67, -0.57, -0.47, -0.37, -0.29, -0.2, -\n 0.12, -0.05, 0.03, 0.1, 0.16, 0.23, 0.3, 0.36], [-1.74, -1.53, -1.35, -\n 1.18, -1.03, -0.9, -0.78, -0.67, -0.57, -0.48, -0.39, -0.31, -0.23, -\n 0.15, -0.08, -0.01, 0.06, 0.13, 0.19, 0.26], [-1.85, -1.64, -1.46, -\n 1.29, -1.14, -1.01, -0.89, -0.79, -0.69, -0.59, -0.5, -0.42, -0.34, -\n 0.26, -0.19, -0.12, -0.05, 0.02, 0.09, 0.15], [-1.96, -1.75, -1.57, -\n 1.4, -1.25, -1.12, -1.01, -0.9, -0.8, -0.7, -0.62, -0.53, -0.45, -0.37,\n -0.3, -0.23, -0.16, -0.09, -0.02, 0.04], [-2.08, -1.87, -1.68, -1.52, -\n 1.37, -1.24, -1.12, -1.02, -0.91, -0.82, -0.73, -0.65, -0.56, -0.49, -\n 0.41, -0.34, -0.27, -0.2, -0.13, -0.07], [-2.2, -1.99, -1.81, -1.64, -\n 1.49, -1.36, -1.24, -1.14, -1.03, -0.94, -0.85, -0.76, -0.68, -0.6, -\n 0.53, -0.45, -0.38, -0.31, -0.24, -0.18], [-2.33, -2.12, -1.93, -1.77, \n -1.62, -1.49, -1.37, -1.26, -1.16, -1.06, -0.97, -0.89, -0.8, -0.72, -\n 0.64, -0.57, -0.5, -0.43, -0.36, -0.29], [-2.41, -2.2, -2.01, -1.84, -\n 1.7, -1.56, -1.45, -1.34, -1.23, -1.14, -1.04, -0.96, -0.87, -0.79, -\n 0.71, -0.64, -0.56, -0.49, -0.42, -0.36], [-2.45, -2.24, -2.05, -1.88, \n -1.73, -1.6, -1.48, -1.37, -1.27, -1.17, -1.08, -0.99, -0.9, -0.82, -\n 0.74, -0.66, -0.59, -0.52, -0.45, -0.38], [-2.49, -2.28, -2.1, -1.93, -\n 1.78, -1.65, -1.53, -1.42, -1.31, -1.21, -1.12, -1.03, -0.94, -0.86, -\n 0.78, -0.7, -0.62, -0.55, -0.48, -0.41], [-2.59, -2.38, -2.19, -2.03, -\n 1.88, -1.74, -1.62, -1.51, -1.41, -1.31, -1.21, -1.12, -1.03, -0.94, -\n 0.86, -0.78, -0.7, -0.63, -0.56, -0.49], [-2.71, -2.5, -2.31, -2.14, -\n 1.99, -1.86, -1.74, -1.62, -1.52, -1.42, -1.32, -1.23, -1.14, -1.05, -\n 0.96, -0.88, -0.8, -0.73, -0.65, -0.58], [-2.84, -2.63, -2.44, -2.27, -\n 2.12, -1.98, -1.86, -1.75, -1.64, -1.54, -1.44, -1.34, -1.25, -1.16, -\n 1.08, -0.99, -0.91, -0.83, -0.76, -0.68], [-2.98, -2.77, -2.58, -2.41, \n -2.26, -2.12, -2.0, -1.88, -1.77, -1.67, -1.57, -1.48, -1.38, -1.29, -\n 1.2, -1.12, -1.04, -0.96, -0.88, -0.8], [-3.14, -2.92, -2.73, -2.56, -\n 2.41, -2.27, -2.15, -2.04, -1.93, -1.82, -1.72, -1.63, -1.53, -1.44, -\n 1.35, -1.26, -1.18, -1.09, -1.01, -0.94], [-3.32, -3.1, -2.91, -2.74, -\n 2.59, -2.45, -2.32, -2.21, -2.1, -1.99, -1.89, -1.79, -1.7, -1.6, -1.51,\n -1.42, -1.34, -1.25, -1.17, -1.09], [-3.52, -3.3, -3.11, -2.94, -2.78, \n -2.65, -2.52, -2.4, -2.29, -2.19, -2.08, -1.98, -1.89, -1.79, -1.7, -\n 1.61, -1.52, -1.43, -1.35, -1.26], [-3.75, -3.53, -3.34, -3.17, -3.01, \n -2.87, -2.75, -2.63, -2.52, -2.41, -2.31, -2.2, -2.11, -2.01, -1.91, -\n 1.82, -1.73, -1.64, -1.55, -1.47], [-4.02, -3.8, -3.6, -3.43, -3.27, -\n 3.13, -3.01, -2.89, -2.77, -2.67, -2.56, -2.46, -2.36, -2.26, -2.16, -\n 2.07, -1.97, -1.88, -1.79, -1.7]]'], {}), '([[-1.44, -1.23, -1.03, -0.87, -0.72, -0.58, -0.46, -0.35, -0.25, -\n 0.16, -0.07, 0.01, 0.09, 0.16, 0.23, 0.3, 0.37, 0.44, 0.5, 0.57], [-\n 1.54, -1.33, -1.14, -0.97, -0.82, -0.69, -0.57, -0.46, -0.36, -0.27, -\n 0.18, -0.1, -0.02, 0.06, 0.13, 0.2, 0.27, 0.34, 0.4, 0.46], [-1.64, -\n 1.43, -1.24, -1.07, -0.92, -0.79, -0.67, -0.57, -0.47, -0.37, -0.29, -\n 0.2, -0.12, -0.05, 0.03, 0.1, 0.16, 0.23, 0.3, 0.36], [-1.74, -1.53, -\n 1.35, -1.18, -1.03, -0.9, -0.78, -0.67, -0.57, -0.48, -0.39, -0.31, -\n 0.23, -0.15, -0.08, -0.01, 0.06, 0.13, 0.19, 0.26], [-1.85, -1.64, -\n 1.46, -1.29, -1.14, -1.01, -0.89, -0.79, -0.69, -0.59, -0.5, -0.42, -\n 0.34, -0.26, -0.19, -0.12, -0.05, 0.02, 0.09, 0.15], [-1.96, -1.75, -\n 1.57, -1.4, -1.25, -1.12, -1.01, -0.9, -0.8, -0.7, -0.62, -0.53, -0.45,\n -0.37, -0.3, -0.23, -0.16, -0.09, -0.02, 0.04], [-2.08, -1.87, -1.68, -\n 1.52, -1.37, -1.24, -1.12, -1.02, -0.91, -0.82, -0.73, -0.65, -0.56, -\n 0.49, -0.41, -0.34, -0.27, -0.2, -0.13, -0.07], [-2.2, -1.99, -1.81, -\n 1.64, -1.49, -1.36, -1.24, -1.14, -1.03, -0.94, -0.85, -0.76, -0.68, -\n 0.6, -0.53, -0.45, -0.38, -0.31, -0.24, -0.18], [-2.33, -2.12, -1.93, -\n 1.77, -1.62, -1.49, -1.37, -1.26, -1.16, -1.06, -0.97, -0.89, -0.8, -\n 0.72, -0.64, -0.57, -0.5, -0.43, -0.36, -0.29], [-2.41, -2.2, -2.01, -\n 1.84, -1.7, -1.56, -1.45, -1.34, -1.23, -1.14, -1.04, -0.96, -0.87, -\n 0.79, -0.71, -0.64, -0.56, -0.49, -0.42, -0.36], [-2.45, -2.24, -2.05, \n -1.88, -1.73, -1.6, -1.48, -1.37, -1.27, -1.17, -1.08, -0.99, -0.9, -\n 0.82, -0.74, -0.66, -0.59, -0.52, -0.45, -0.38], [-2.49, -2.28, -2.1, -\n 1.93, -1.78, -1.65, -1.53, -1.42, -1.31, -1.21, -1.12, -1.03, -0.94, -\n 0.86, -0.78, -0.7, -0.62, -0.55, -0.48, -0.41], [-2.59, -2.38, -2.19, -\n 2.03, -1.88, -1.74, -1.62, -1.51, -1.41, -1.31, -1.21, -1.12, -1.03, -\n 0.94, -0.86, -0.78, -0.7, -0.63, -0.56, -0.49], [-2.71, -2.5, -2.31, -\n 2.14, -1.99, -1.86, -1.74, -1.62, -1.52, -1.42, -1.32, -1.23, -1.14, -\n 1.05, -0.96, -0.88, -0.8, -0.73, -0.65, -0.58], [-2.84, -2.63, -2.44, -\n 2.27, -2.12, -1.98, -1.86, -1.75, -1.64, -1.54, -1.44, -1.34, -1.25, -\n 1.16, -1.08, -0.99, -0.91, -0.83, -0.76, -0.68], [-2.98, -2.77, -2.58, \n -2.41, -2.26, -2.12, -2.0, -1.88, -1.77, -1.67, -1.57, -1.48, -1.38, -\n 1.29, -1.2, -1.12, -1.04, -0.96, -0.88, -0.8], [-3.14, -2.92, -2.73, -\n 2.56, -2.41, -2.27, -2.15, -2.04, -1.93, -1.82, -1.72, -1.63, -1.53, -\n 1.44, -1.35, -1.26, -1.18, -1.09, -1.01, -0.94], [-3.32, -3.1, -2.91, -\n 2.74, -2.59, -2.45, -2.32, -2.21, -2.1, -1.99, -1.89, -1.79, -1.7, -1.6,\n -1.51, -1.42, -1.34, -1.25, -1.17, -1.09], [-3.52, -3.3, -3.11, -2.94, \n -2.78, -2.65, -2.52, -2.4, -2.29, -2.19, -2.08, -1.98, -1.89, -1.79, -\n 1.7, -1.61, -1.52, -1.43, -1.35, -1.26], [-3.75, -3.53, -3.34, -3.17, -\n 3.01, -2.87, -2.75, -2.63, -2.52, -2.41, -2.31, -2.2, -2.11, -2.01, -\n 1.91, -1.82, -1.73, -1.64, -1.55, -1.47], [-4.02, -3.8, -3.6, -3.43, -\n 3.27, -3.13, -3.01, -2.89, -2.77, -2.67, -2.56, -2.46, -2.36, -2.26, -\n 2.16, -2.07, -1.97, -1.88, -1.79, -1.7]])\n', (19475, 22589), True, 'import numpy as np\n'), ((22680, 25831), 'numpy.array', 'np.array', (['[[-2.22, -1.98, -1.75, -1.52, -1.31, -1.12, -0.95, -0.8, -0.66, -0.54, -\n 0.43, -0.33, -0.24, -0.15, -0.07, 0.01, 0.08, 0.16, 0.23, 0.29], [-2.32,\n -2.08, -1.85, -1.62, -1.41, -1.22, -1.05, -0.9, -0.77, -0.64, -0.54, -\n 0.43, -0.34, -0.25, -0.17, -0.09, -0.02, 0.05, 0.12, 0.19], [-2.43, -\n 2.18, -1.95, -1.72, -1.51, -1.32, -1.15, -1.0, -0.87, -0.75, -0.64, -\n 0.54, -0.45, -0.36, -0.28, -0.2, -0.12, -0.05, 0.02, 0.09], [-2.53, -\n 2.29, -2.05, -1.82, -1.61, -1.42, -1.25, -1.11, -0.97, -0.85, -0.74, -\n 0.64, -0.55, -0.46, -0.38, -0.3, -0.23, -0.15, -0.08, -0.01], [-2.63, -\n 2.39, -2.15, -1.92, -1.71, -1.53, -1.36, -1.21, -1.08, -0.96, -0.85, -\n 0.75, -0.66, -0.57, -0.49, -0.41, -0.33, -0.26, -0.19, -0.12], [-2.74, \n -2.49, -2.25, -2.03, -1.82, -1.63, -1.46, -1.32, -1.18, -1.06, -0.96, -\n 0.86, -0.76, -0.68, -0.59, -0.51, -0.44, -0.36, -0.29, -0.22], [-2.84, \n -2.6, -2.36, -2.13, -1.92, -1.74, -1.57, -1.42, -1.29, -1.17, -1.07, -\n 0.97, -0.87, -0.78, -0.7, -0.62, -0.54, -0.47, -0.4, -0.33], [-2.95, -\n 2.7, -2.47, -2.24, -2.03, -1.85, -1.68, -1.53, -1.4, -1.29, -1.18, -\n 1.08, -0.98, -0.9, -0.81, -0.73, -0.65, -0.58, -0.51, -0.44], [-3.07, -\n 2.82, -2.58, -2.35, -2.15, -1.96, -1.8, -1.65, -1.52, -1.4, -1.29, -\n 1.19, -1.1, -1.01, -0.92, -0.84, -0.77, -0.69, -0.62, -0.55], [-3.14, -\n 2.88, -2.64, -2.42, -2.21, -2.02, -1.86, -1.71, -1.58, -1.46, -1.36, -\n 1.26, -1.16, -1.07, -0.99, -0.9, -0.83, -0.75, -0.68, -0.61], [-3.16, -\n 2.91, -2.67, -2.44, -2.24, -2.05, -1.88, -1.74, -1.61, -1.49, -1.38, -\n 1.28, -1.18, -1.09, -1.01, -0.92, -0.84, -0.77, -0.69, -0.62], [-3.2, -\n 2.94, -2.7, -2.47, -2.27, -2.08, -1.91, -1.77, -1.63, -1.52, -1.41, -\n 1.31, -1.21, -1.12, -1.03, -0.95, -0.87, -0.79, -0.71, -0.64], [-3.28, \n -3.03, -2.78, -2.55, -2.35, -2.16, -1.99, -1.84, -1.71, -1.59, -1.48, -\n 1.38, -1.29, -1.19, -1.1, -1.02, -0.94, -0.86, -0.78, -0.71], [-3.38, -\n 3.12, -2.88, -2.65, -2.44, -2.25, -2.09, -1.94, -1.81, -1.69, -1.58, -\n 1.47, -1.37, -1.28, -1.19, -1.11, -1.02, -0.94, -0.87, -0.79], [-3.49, \n -3.23, -2.98, -2.75, -2.55, -2.36, -2.19, -2.04, -1.91, -1.79, -1.68, -\n 1.57, -1.47, -1.38, -1.29, -1.2, -1.12, -1.03, -0.96, -0.88], [-3.61, -\n 3.35, -3.1, -2.87, -2.66, -2.47, -2.3, -2.15, -2.02, -1.9, -1.79, -1.68,\n -1.58, -1.49, -1.39, -1.3, -1.22, -1.14, -1.05, -0.98], [-3.74, -3.48, \n -3.23, -3.0, -2.79, -2.6, -2.43, -2.28, -2.14, -2.02, -1.91, -1.8, -1.7,\n -1.6, -1.51, -1.42, -1.33, -1.25, -1.16, -1.09], [-3.88, -3.62, -3.37, \n -3.14, -2.93, -2.74, -2.57, -2.41, -2.28, -2.16, -2.04, -1.94, -1.83, -\n 1.74, -1.64, -1.55, -1.46, -1.37, -1.29, -1.21], [-4.04, -3.78, -3.53, \n -3.3, -3.08, -2.89, -2.72, -2.57, -2.43, -2.31, -2.2, -2.09, -1.98, -\n 1.88, -1.79, -1.69, -1.6, -1.51, -1.43, -1.35], [-4.22, -3.96, -3.71, -\n 3.47, -3.26, -3.07, -2.9, -2.74, -2.61, -2.48, -2.37, -2.26, -2.15, -\n 2.05, -1.96, -1.86, -1.77, -1.68, -1.59, -1.5], [-4.43, -4.16, -3.91, -\n 3.68, -3.46, -3.27, -3.1, -2.94, -2.81, -2.68, -2.56, -2.45, -2.35, -\n 2.25, -2.15, -2.05, -1.95, -1.86, -1.77, -1.69]]'], {}), '([[-2.22, -1.98, -1.75, -1.52, -1.31, -1.12, -0.95, -0.8, -0.66, -\n 0.54, -0.43, -0.33, -0.24, -0.15, -0.07, 0.01, 0.08, 0.16, 0.23, 0.29],\n [-2.32, -2.08, -1.85, -1.62, -1.41, -1.22, -1.05, -0.9, -0.77, -0.64, -\n 0.54, -0.43, -0.34, -0.25, -0.17, -0.09, -0.02, 0.05, 0.12, 0.19], [-\n 2.43, -2.18, -1.95, -1.72, -1.51, -1.32, -1.15, -1.0, -0.87, -0.75, -\n 0.64, -0.54, -0.45, -0.36, -0.28, -0.2, -0.12, -0.05, 0.02, 0.09], [-\n 2.53, -2.29, -2.05, -1.82, -1.61, -1.42, -1.25, -1.11, -0.97, -0.85, -\n 0.74, -0.64, -0.55, -0.46, -0.38, -0.3, -0.23, -0.15, -0.08, -0.01], [-\n 2.63, -2.39, -2.15, -1.92, -1.71, -1.53, -1.36, -1.21, -1.08, -0.96, -\n 0.85, -0.75, -0.66, -0.57, -0.49, -0.41, -0.33, -0.26, -0.19, -0.12], [\n -2.74, -2.49, -2.25, -2.03, -1.82, -1.63, -1.46, -1.32, -1.18, -1.06, -\n 0.96, -0.86, -0.76, -0.68, -0.59, -0.51, -0.44, -0.36, -0.29, -0.22], [\n -2.84, -2.6, -2.36, -2.13, -1.92, -1.74, -1.57, -1.42, -1.29, -1.17, -\n 1.07, -0.97, -0.87, -0.78, -0.7, -0.62, -0.54, -0.47, -0.4, -0.33], [-\n 2.95, -2.7, -2.47, -2.24, -2.03, -1.85, -1.68, -1.53, -1.4, -1.29, -\n 1.18, -1.08, -0.98, -0.9, -0.81, -0.73, -0.65, -0.58, -0.51, -0.44], [-\n 3.07, -2.82, -2.58, -2.35, -2.15, -1.96, -1.8, -1.65, -1.52, -1.4, -\n 1.29, -1.19, -1.1, -1.01, -0.92, -0.84, -0.77, -0.69, -0.62, -0.55], [-\n 3.14, -2.88, -2.64, -2.42, -2.21, -2.02, -1.86, -1.71, -1.58, -1.46, -\n 1.36, -1.26, -1.16, -1.07, -0.99, -0.9, -0.83, -0.75, -0.68, -0.61], [-\n 3.16, -2.91, -2.67, -2.44, -2.24, -2.05, -1.88, -1.74, -1.61, -1.49, -\n 1.38, -1.28, -1.18, -1.09, -1.01, -0.92, -0.84, -0.77, -0.69, -0.62], [\n -3.2, -2.94, -2.7, -2.47, -2.27, -2.08, -1.91, -1.77, -1.63, -1.52, -\n 1.41, -1.31, -1.21, -1.12, -1.03, -0.95, -0.87, -0.79, -0.71, -0.64], [\n -3.28, -3.03, -2.78, -2.55, -2.35, -2.16, -1.99, -1.84, -1.71, -1.59, -\n 1.48, -1.38, -1.29, -1.19, -1.1, -1.02, -0.94, -0.86, -0.78, -0.71], [-\n 3.38, -3.12, -2.88, -2.65, -2.44, -2.25, -2.09, -1.94, -1.81, -1.69, -\n 1.58, -1.47, -1.37, -1.28, -1.19, -1.11, -1.02, -0.94, -0.87, -0.79], [\n -3.49, -3.23, -2.98, -2.75, -2.55, -2.36, -2.19, -2.04, -1.91, -1.79, -\n 1.68, -1.57, -1.47, -1.38, -1.29, -1.2, -1.12, -1.03, -0.96, -0.88], [-\n 3.61, -3.35, -3.1, -2.87, -2.66, -2.47, -2.3, -2.15, -2.02, -1.9, -1.79,\n -1.68, -1.58, -1.49, -1.39, -1.3, -1.22, -1.14, -1.05, -0.98], [-3.74, \n -3.48, -3.23, -3.0, -2.79, -2.6, -2.43, -2.28, -2.14, -2.02, -1.91, -\n 1.8, -1.7, -1.6, -1.51, -1.42, -1.33, -1.25, -1.16, -1.09], [-3.88, -\n 3.62, -3.37, -3.14, -2.93, -2.74, -2.57, -2.41, -2.28, -2.16, -2.04, -\n 1.94, -1.83, -1.74, -1.64, -1.55, -1.46, -1.37, -1.29, -1.21], [-4.04, \n -3.78, -3.53, -3.3, -3.08, -2.89, -2.72, -2.57, -2.43, -2.31, -2.2, -\n 2.09, -1.98, -1.88, -1.79, -1.69, -1.6, -1.51, -1.43, -1.35], [-4.22, -\n 3.96, -3.71, -3.47, -3.26, -3.07, -2.9, -2.74, -2.61, -2.48, -2.37, -\n 2.26, -2.15, -2.05, -1.96, -1.86, -1.77, -1.68, -1.59, -1.5], [-4.43, -\n 4.16, -3.91, -3.68, -3.46, -3.27, -3.1, -2.94, -2.81, -2.68, -2.56, -\n 2.45, -2.35, -2.25, -2.15, -2.05, -1.95, -1.86, -1.77, -1.69]])\n', (22688, 25831), True, 'import numpy as np\n'), ((25892, 29030), 'numpy.array', 'np.array', (['[[-2.87, -2.66, -2.45, -2.24, -2.03, -1.81, -1.6, -1.39, -1.2, -1.02, -0.87,\n -0.73, -0.61, -0.5, -0.4, -0.3, -0.21, -0.13, -0.05, 0.02], [-3.0, -\n 2.78, -2.57, -2.36, -2.14, -1.92, -1.7, -1.49, -1.3, -1.12, -0.97, -\n 0.83, -0.71, -0.6, -0.5, -0.4, -0.32, -0.23, -0.16, -0.08], [-3.14, -\n 2.91, -2.69, -2.47, -2.24, -2.02, -1.8, -1.59, -1.4, -1.22, -1.07, -\n 0.93, -0.81, -0.7, -0.6, -0.51, -0.42, -0.34, -0.26, -0.18], [-3.27, -\n 3.04, -2.81, -2.58, -2.35, -2.12, -1.9, -1.69, -1.5, -1.32, -1.17, -\n 1.04, -0.91, -0.8, -0.7, -0.61, -0.52, -0.44, -0.36, -0.28], [-3.4, -\n 3.16, -2.92, -2.69, -2.45, -2.22, -2.0, -1.79, -1.6, -1.43, -1.27, -\n 1.14, -1.02, -0.91, -0.81, -0.71, -0.62, -0.54, -0.46, -0.39], [-3.52, \n -3.28, -3.03, -2.79, -2.55, -2.32, -2.1, -1.89, -1.7, -1.53, -1.38, -\n 1.24, -1.12, -1.01, -0.91, -0.82, -0.73, -0.65, -0.57, -0.49], [-3.64, \n -3.39, -3.14, -2.9, -2.66, -2.42, -2.2, -1.99, -1.8, -1.63, -1.48, -\n 1.35, -1.22, -1.12, -1.02, -0.92, -0.83, -0.75, -0.67, -0.6], [-3.76, -\n 3.5, -3.25, -3.0, -2.76, -2.52, -2.3, -2.09, -1.9, -1.73, -1.58, -1.45,\n -1.33, -1.22, -1.12, -1.03, -0.94, -0.86, -0.78, -0.7], [-3.87, -3.61, \n -3.36, -3.11, -2.86, -2.63, -2.4, -2.19, -2.01, -1.84, -1.69, -1.56, -\n 1.44, -1.33, -1.23, -1.14, -1.05, -0.96, -0.89, -0.81], [-3.93, -3.67, \n -3.42, -3.16, -2.92, -2.68, -2.45, -2.25, -2.06, -1.89, -1.75, -1.61, -\n 1.49, -1.39, -1.29, -1.19, -1.1, -1.02, -0.94, -0.86], [-3.95, -3.69, -\n 3.43, -3.18, -2.93, -2.69, -2.47, -2.26, -2.07, -1.9, -1.76, -1.63, -\n 1.51, -1.4, -1.3, -1.2, -1.12, -1.03, -0.95, -0.87], [-3.97, -3.71, -\n 3.45, -3.2, -2.95, -2.71, -2.48, -2.27, -2.09, -1.92, -1.77, -1.64, -\n 1.52, -1.42, -1.32, -1.22, -1.13, -1.05, -0.97, -0.89], [-4.05, -3.78, \n -3.52, -3.26, -3.01, -2.77, -2.55, -2.34, -2.15, -1.99, -1.84, -1.71, -\n 1.59, -1.48, -1.38, -1.28, -1.19, -1.11, -1.03, -0.95], [-4.14, -3.87, \n -3.6, -3.34, -3.09, -2.85, -2.62, -2.42, -2.23, -2.06, -1.91, -1.78, -\n 1.66, -1.56, -1.45, -1.36, -1.27, -1.18, -1.1, -1.02], [-4.23, -3.96, -\n 3.69, -3.43, -3.18, -2.94, -2.71, -2.5, -2.31, -2.15, -2.0, -1.87, -\n 1.75, -1.64, -1.54, -1.44, -1.35, -1.26, -1.18, -1.1], [-4.34, -4.06, -\n 3.79, -3.53, -3.27, -3.03, -2.8, -2.59, -2.4, -2.24, -2.09, -1.96, -\n 1.84, -1.73, -1.62, -1.53, -1.44, -1.35, -1.26, -1.18], [-4.45, -4.18, \n -3.9, -3.64, -3.38, -3.13, -2.9, -2.69, -2.51, -2.34, -2.19, -2.06, -\n 1.94, -1.83, -1.72, -1.62, -1.53, -1.44, -1.35, -1.27], [-4.58, -4.3, -\n 4.02, -3.75, -3.49, -3.25, -3.02, -2.81, -2.62, -2.45, -2.3, -2.17, -\n 2.04, -1.93, -1.83, -1.73, -1.63, -1.54, -1.46, -1.37], [-4.71, -4.43, \n -4.15, -3.88, -3.62, -3.37, -3.14, -2.93, -2.74, -2.57, -2.42, -2.29, -\n 2.17, -2.05, -1.95, -1.85, -1.75, -1.66, -1.57, -1.48], [-4.86, -4.58, \n -4.3, -4.03, -3.76, -3.51, -3.28, -3.07, -2.88, -2.71, -2.56, -2.42, -\n 2.3, -2.19, -2.08, -1.98, -1.88, -1.79, -1.7, -1.61], [-5.03, -4.74, -\n 4.46, -4.19, -3.92, -3.67, -3.44, -3.23, -3.03, -2.86, -2.71, -2.58, -\n 2.45, -2.34, -2.23, -2.13, -2.03, -1.93, -1.84, -1.75]]'], {}), '([[-2.87, -2.66, -2.45, -2.24, -2.03, -1.81, -1.6, -1.39, -1.2, -\n 1.02, -0.87, -0.73, -0.61, -0.5, -0.4, -0.3, -0.21, -0.13, -0.05, 0.02],\n [-3.0, -2.78, -2.57, -2.36, -2.14, -1.92, -1.7, -1.49, -1.3, -1.12, -\n 0.97, -0.83, -0.71, -0.6, -0.5, -0.4, -0.32, -0.23, -0.16, -0.08], [-\n 3.14, -2.91, -2.69, -2.47, -2.24, -2.02, -1.8, -1.59, -1.4, -1.22, -\n 1.07, -0.93, -0.81, -0.7, -0.6, -0.51, -0.42, -0.34, -0.26, -0.18], [-\n 3.27, -3.04, -2.81, -2.58, -2.35, -2.12, -1.9, -1.69, -1.5, -1.32, -\n 1.17, -1.04, -0.91, -0.8, -0.7, -0.61, -0.52, -0.44, -0.36, -0.28], [-\n 3.4, -3.16, -2.92, -2.69, -2.45, -2.22, -2.0, -1.79, -1.6, -1.43, -1.27,\n -1.14, -1.02, -0.91, -0.81, -0.71, -0.62, -0.54, -0.46, -0.39], [-3.52,\n -3.28, -3.03, -2.79, -2.55, -2.32, -2.1, -1.89, -1.7, -1.53, -1.38, -\n 1.24, -1.12, -1.01, -0.91, -0.82, -0.73, -0.65, -0.57, -0.49], [-3.64, \n -3.39, -3.14, -2.9, -2.66, -2.42, -2.2, -1.99, -1.8, -1.63, -1.48, -\n 1.35, -1.22, -1.12, -1.02, -0.92, -0.83, -0.75, -0.67, -0.6], [-3.76, -\n 3.5, -3.25, -3.0, -2.76, -2.52, -2.3, -2.09, -1.9, -1.73, -1.58, -1.45,\n -1.33, -1.22, -1.12, -1.03, -0.94, -0.86, -0.78, -0.7], [-3.87, -3.61, \n -3.36, -3.11, -2.86, -2.63, -2.4, -2.19, -2.01, -1.84, -1.69, -1.56, -\n 1.44, -1.33, -1.23, -1.14, -1.05, -0.96, -0.89, -0.81], [-3.93, -3.67, \n -3.42, -3.16, -2.92, -2.68, -2.45, -2.25, -2.06, -1.89, -1.75, -1.61, -\n 1.49, -1.39, -1.29, -1.19, -1.1, -1.02, -0.94, -0.86], [-3.95, -3.69, -\n 3.43, -3.18, -2.93, -2.69, -2.47, -2.26, -2.07, -1.9, -1.76, -1.63, -\n 1.51, -1.4, -1.3, -1.2, -1.12, -1.03, -0.95, -0.87], [-3.97, -3.71, -\n 3.45, -3.2, -2.95, -2.71, -2.48, -2.27, -2.09, -1.92, -1.77, -1.64, -\n 1.52, -1.42, -1.32, -1.22, -1.13, -1.05, -0.97, -0.89], [-4.05, -3.78, \n -3.52, -3.26, -3.01, -2.77, -2.55, -2.34, -2.15, -1.99, -1.84, -1.71, -\n 1.59, -1.48, -1.38, -1.28, -1.19, -1.11, -1.03, -0.95], [-4.14, -3.87, \n -3.6, -3.34, -3.09, -2.85, -2.62, -2.42, -2.23, -2.06, -1.91, -1.78, -\n 1.66, -1.56, -1.45, -1.36, -1.27, -1.18, -1.1, -1.02], [-4.23, -3.96, -\n 3.69, -3.43, -3.18, -2.94, -2.71, -2.5, -2.31, -2.15, -2.0, -1.87, -\n 1.75, -1.64, -1.54, -1.44, -1.35, -1.26, -1.18, -1.1], [-4.34, -4.06, -\n 3.79, -3.53, -3.27, -3.03, -2.8, -2.59, -2.4, -2.24, -2.09, -1.96, -\n 1.84, -1.73, -1.62, -1.53, -1.44, -1.35, -1.26, -1.18], [-4.45, -4.18, \n -3.9, -3.64, -3.38, -3.13, -2.9, -2.69, -2.51, -2.34, -2.19, -2.06, -\n 1.94, -1.83, -1.72, -1.62, -1.53, -1.44, -1.35, -1.27], [-4.58, -4.3, -\n 4.02, -3.75, -3.49, -3.25, -3.02, -2.81, -2.62, -2.45, -2.3, -2.17, -\n 2.04, -1.93, -1.83, -1.73, -1.63, -1.54, -1.46, -1.37], [-4.71, -4.43, \n -4.15, -3.88, -3.62, -3.37, -3.14, -2.93, -2.74, -2.57, -2.42, -2.29, -\n 2.17, -2.05, -1.95, -1.85, -1.75, -1.66, -1.57, -1.48], [-4.86, -4.58, \n -4.3, -4.03, -3.76, -3.51, -3.28, -3.07, -2.88, -2.71, -2.56, -2.42, -\n 2.3, -2.19, -2.08, -1.98, -1.88, -1.79, -1.7, -1.61], [-5.03, -4.74, -\n 4.46, -4.19, -3.92, -3.67, -3.44, -3.23, -3.03, -2.86, -2.71, -2.58, -\n 2.45, -2.34, -2.23, -2.13, -2.03, -1.93, -1.84, -1.75]])\n', (25900, 29030), True, 'import numpy as np\n'), ((29510, 29699), 'openquake.hazardlib.gsim.base.CoeffsTable', 'CoeffsTable', ([], {'sa_damping': '(5)', 'table': '""" IMT sigma\n pga 0.326\n 0.1 0.326\n 0.2 0.326\n 0.3 0.326\n 0.5 0.326\n 1.0 0.347\n 2.0 0.347\n """'}), '(sa_damping=5, table=\n """ IMT sigma\n pga 0.326\n 0.1 0.326\n 0.2 0.326\n 0.3 0.326\n 0.5 0.326\n 1.0 0.347\n 2.0 0.347\n """\n )\n', (29521, 29699), False, 'from openquake.hazardlib.gsim.base import CoeffsTable, GMPE\n'), ((4641, 4661), 'openquake.hazardlib.gsim.utils.clip_mean', 'clip_mean', (['imt', 'mean'], {}), '(imt, mean)\n', (4650, 4661), False, 'from openquake.hazardlib.gsim.utils import mblg_to_mw_atkinson_boore_87, mblg_to_mw_johnston_96, clip_mean\n'), ((5431, 5446), 'numpy.log10', 'np.log10', (['rhypo'], {}), '(rhypo)\n', (5439, 5446), True, 'import numpy as np\n'), ((5542, 5609), 'scipy.interpolate.RectBivariateSpline', 'RectBivariateSpline', (['self.MAGS', 'self.DISTS', 'self.IMTS_TABLES[imt].T'], {}), '(self.MAGS, self.DISTS, self.IMTS_TABLES[imt].T)\n', (5561, 5609), False, 'from scipy.interpolate import RectBivariateSpline\n'), ((5916, 5949), 'openquake.hazardlib.gsim.utils.mblg_to_mw_atkinson_boore_87', 'mblg_to_mw_atkinson_boore_87', (['mag'], {}), '(mag)\n', (5944, 5949), False, 'from openquake.hazardlib.gsim.utils import mblg_to_mw_atkinson_boore_87, mblg_to_mw_johnston_96, clip_mean\n'), ((29151, 29156), 'openquake.hazardlib.imt.PGA', 'PGA', ([], {}), '()\n', (29154, 29156), False, 'from openquake.hazardlib.imt import PGA, SA\n'), ((29175, 29200), 'openquake.hazardlib.imt.SA', 'SA', ([], {'period': '(0.1)', 'damping': '(5)'}), '(period=0.1, damping=5)\n', (29177, 29200), False, 'from openquake.hazardlib.imt import PGA, SA\n'), ((29220, 29245), 'openquake.hazardlib.imt.SA', 'SA', ([], {'period': '(0.2)', 'damping': '(5)'}), '(period=0.2, damping=5)\n', (29222, 29245), False, 'from openquake.hazardlib.imt import PGA, SA\n'), ((29265, 29290), 'openquake.hazardlib.imt.SA', 'SA', ([], {'period': '(0.3)', 'damping': '(5)'}), '(period=0.3, damping=5)\n', (29267, 29290), False, 'from openquake.hazardlib.imt import PGA, SA\n'), ((29310, 29335), 'openquake.hazardlib.imt.SA', 'SA', ([], {'period': '(0.5)', 'damping': '(5)'}), '(period=0.5, damping=5)\n', (29312, 29335), False, 'from openquake.hazardlib.imt import PGA, SA\n'), ((29355, 29380), 'openquake.hazardlib.imt.SA', 'SA', ([], {'period': '(1.0)', 'damping': '(5)'}), '(period=1.0, damping=5)\n', (29357, 29380), False, 'from openquake.hazardlib.imt import PGA, SA\n'), ((29399, 29424), 'openquake.hazardlib.imt.SA', 'SA', ([], {'period': '(2.0)', 'damping': '(5)'}), '(period=2.0, damping=5)\n', (29401, 29424), False, 'from openquake.hazardlib.imt import PGA, SA\n'), ((30059, 30086), 'openquake.hazardlib.gsim.utils.mblg_to_mw_johnston_96', 'mblg_to_mw_johnston_96', (['mag'], {}), '(mag)\n', (30081, 30086), False, 'from openquake.hazardlib.gsim.utils import mblg_to_mw_atkinson_boore_87, mblg_to_mw_johnston_96, clip_mean\n'), ((5176, 5196), 'numpy.zeros_like', 'np.zeros_like', (['rhypo'], {}), '(rhypo)\n', (5189, 5196), True, 'import numpy as np\n'), ((5737, 5747), 'numpy.log', 'np.log', (['(10)'], {}), '(10)\n', (5743, 5747), True, 'import numpy as np\n'), ((6208, 6227), 'numpy.zeros', 'np.zeros', (['num_sites'], {}), '(num_sites)\n', (6216, 6227), True, 'import numpy as np\n'), ((6243, 6253), 'numpy.log', 'np.log', (['(10)'], {}), '(10)\n', (6249, 6253), True, 'import numpy as np\n')]
import os import warnings import itertools from operator import itemgetter from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises from random import uniform, seed import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal try: from pyne.mesh import Mesh # see if the source sampling module exists but do not import it import imp pyne_info = imp.find_module("pyne") pyne_mod = imp.load_module("pyne", *pyne_info) imp.find_module("source_sampling", pyne_mod.__path__) except ImportError: from nose.plugins.skip import SkipTest raise SkipTest from pyne.source_sampling import Sampler, AliasTable from pyne.mesh import Mesh, NativeMeshTag from pyne.r2s import tag_e_bounds from pymoab import core as mb_core, types from pyne.utils import QAWarning warnings.simplefilter("ignore", QAWarning) # Define modes DEFAULT_ANALOG = 0 DEFAULT_UNIFORM = 1 DEFAULT_USER = 2 SUBVOXEL_ANALOG = 3 SUBVOXEL_UNIFORM = 4 SUBVOXEL_USER = 5 def try_rm_file(filename): return lambda: os.remove(filename) if os.path.exists(filename) else None @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_single_hex_tag_names_map(): """This test tests uniform sampling within a single hex volume element. This is done by dividing the volume element in 4 smaller hex and ensuring that each sub-hex is sampled equally. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]], mats=None ) m.src = NativeMeshTag(2, float) m.src[:] = [[2.0, 1.0], [9.0, 3.0]] e_bounds = np.array([0, 0.5, 1.0]) m = tag_e_bounds(m, e_bounds) m.bias = NativeMeshTag(2, float) cell_fracs = np.zeros( 2, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 1.0, 0.0), (1, 11, 1.0, 0.0)] m.tag_cell_fracs(cell_fracs) m.bias[:] = [[1.0, 2.0], [3.0, 3.0]] filename = "sampling_mesh.h5m" m.write_hdf5(filename) # right condition with e_bounds in 'source.h5m' tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, DEFAULT_ANALOG) # right condition with e_bounds provided by both 'e_bounds' and 'source.h5m' tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, DEFAULT_ANALOG) # src_tag_name not given tag_names = { "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_ANALOG) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_UNIFORM) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_ANALOG) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_UNIFORM) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER) # bias_tag_name not given tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER) # cell_number_tag_name not given tag_names = { "src_tag_name": "src", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_ANALOG) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_UNIFORM) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_ANALOG) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_UNIFORM) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER) # cell_fracs_tag_name not given tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "e_bounds_tag_name": "e_bounds", } assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_ANALOG) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_UNIFORM) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_ANALOG) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_UNIFORM) assert_raises(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER) # wrong bias_tag data (non-zero source_density biased to zero -> NAN weight) m.src = NativeMeshTag(2, float) m.src[:] = [[1.0, 1.0]] m.bias = NativeMeshTag(2, float) m.bias[:] = [[0.0, 0.0]] m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "bias_tag_name": "bias", "e_bounds_tag_name": "e_bounds", } assert_raises(RuntimeError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER) @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_analog_single_hex(): """This test tests that particles of sampled evenly within the phase-space of a single mesh volume element with one energy group in an analog sampling scheme. This done by dividing each dimension (x, y, z, E) in half, then sampling particles and tallying on the basis of which of the 2^4 = 8 regions of phase space the particle is born into. """ seed(1953) m = Mesh(structured=True, structured_coords=[[0, 1], [0, 1], [0, 1]], mats=None) m.src = NativeMeshTag(1, float) m.src[0] = 1.0 cell_fracs = np.zeros( 1, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 1.0, 0.0)] m.tag_cell_fracs(cell_fracs) e_bounds = np.array([0, 1.0]) m = tag_e_bounds(m, e_bounds) filename = "sampling_mesh.h5m" m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, DEFAULT_ANALOG) num_samples = 5000 score = 1.0 / num_samples num_divs = 2 tally = np.zeros(shape=(num_divs, num_divs, num_divs, num_divs)) for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)])) assert_equal(s.w, 1.0) # analog: all weights must be one tally[ int(s.x * num_divs), int(s.y * num_divs), int(s.z * num_divs), int(s.e * num_divs), ] += score # Test that each half-space of phase space (e.g. x > 0.5) is sampled about # half the time. for i in range(0, 4): for j in range(0, 2): assert abs(np.sum(np.rollaxis(tally, i)[j, :, :, :]) - 0.5) < 0.05 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_analog_multiple_hex(): """This test tests that particle are sampled uniformly from a uniform source defined on eight mesh volume elements in two energy groups. This is done using the exact same method ass test_analog_multiple_hex. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5, 1]], mats=None, ) m.src = NativeMeshTag(2, float) m.src[:] = np.ones(shape=(8, 2)) cell_fracs = np.zeros( 8, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 11, 1.0, 0.0), (1, 11, 1.0, 0.0), (2, 11, 1.0, 0.0), (3, 11, 1.0, 0.0), (4, 11, 1.0, 0.0), (5, 11, 1.0, 0.0), (6, 11, 1.0, 0.0), (7, 11, 1.0, 0.0), ] m.tag_cell_fracs(cell_fracs) filename = "sampling_mesh.h5m" e_bounds = np.array([0, 0.5, 1]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, DEFAULT_ANALOG) num_samples = 5000 score = 1.0 / num_samples num_divs = 2 tally = np.zeros(shape=(num_divs, num_divs, num_divs, num_divs)) for i in range(num_samples): s = sampler.particle_birth([uniform(0, 1) for x in range(6)]) assert_equal(s.w, 1.0) tally[ int(s.x * num_divs), int(s.y * num_divs), int(s.z * num_divs), int(s.e * num_divs), ] += score for i in range(0, 4): for j in range(0, 2): halfspace_sum = np.sum(np.rollaxis(tally, i)[j, :, :, :]) assert abs(halfspace_sum - 0.5) / 0.5 < 0.1 @with_setup(None, try_rm_file("tet.h5m")) def test_analog_single_tet(): """This test tests uniform sampling within a single tetrahedron. This is done by dividing the tetrahedron in 4 smaller tetrahedrons and ensuring that each sub-tet is sampled equally. """ seed(1953) mesh = mb_core.Core() v1 = [0.0, 0.0, 0.0] v2 = [1.0, 0.0, 0.0] v3 = [0.0, 1.0, 0.0] v4 = [0.0, 0.0, 1.0] verts = mesh.create_vertices([v1, v2, v3, v4]) mesh.create_element(types.MBTET, verts) m = Mesh(structured=False, mesh=mesh) m.src = NativeMeshTag(1, float) m.src[:] = np.array([1]) filename = "tet.h5m" e_bounds = np.array([0.0, 1.0]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) center = m.ve_center(list(m.iter_ve())[0]) subtets = [ [center, v1, v2, v3], [center, v1, v2, v4], [center, v1, v3, v4], [center, v2, v3, v4], ] tag_names = {"src_tag_name": "src", "e_bounds_tag_name": "e_bounds"} sampler = Sampler(filename, tag_names, np.array([0.0, 1.0]), DEFAULT_ANALOG) num_samples = 5000 score = 1.0 / num_samples tally = np.zeros(shape=(4)) for i in range(num_samples): s = sampler.particle_birth([uniform(0.0, 1.0) for x in range(6)]) assert_equal(s.w, 1.0) for i, tet in enumerate(subtets): if point_in_tet(tet, [s.x, s.y, s.z]): tally[i] += score break for t in tally: assert abs(t - 0.25) / 0.25 < 0.2 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_uniform(): """This test tests that the uniform biasing scheme: 1. Samples space uniformly. This is checked using the same method described in test_analog_single_hex(). 2. Adjusts weights accordingly. Sample calculations are provided in Case 1 in the Theory Manual. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0, 3, 3.5], [0.0, 1.0], [0.0, 1.0]], mats=None, ) m.src = NativeMeshTag(2, float) m.src[:] = [[2.0, 1.0], [9.0, 3.0]] e_bounds = np.array([0.0, 0.5, 1.0]) filename = "sampling_mesh.h5m" cell_fracs = np.zeros( 2, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 1.0, 0.0), (1, 11, 1.0, 0.0)] m.tag_cell_fracs(cell_fracs) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, DEFAULT_UNIFORM) num_samples = 10000 score = 1.0 / num_samples num_divs = 2 num_e = 2 spatial_tally = np.zeros(shape=(num_divs, num_divs, num_divs)) e_tally = np.zeros(shape=(4)) # number of phase space groups for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)])) if s.x < 3.0: assert_almost_equal(s.w, 0.7) # hand calcs else: assert_almost_equal(s.w, 2.8) # hand calcs spatial_tally[ int(s.x * num_divs / 3.5), int(s.y * num_divs / 1.0), int(s.z * num_divs / 1.0), ] += score if s.x < 3 and s.e < 0.5: e_tally[0] += score elif s.x < 3 and s.e > 0.5: e_tally[1] += score if s.x > 3 and s.e < 0.5: e_tally[2] += score if s.x > 3 and s.e > 0.5: e_tally[3] += score for i in range(0, 3): for j in range(0, 2): halfspace_sum = np.sum(np.rollaxis(spatial_tally, i)[j, :, :]) assert abs(halfspace_sum - 0.5) / 0.5 < 0.1 expected_e_tally = [4.0 / 7, 2.0 / 7, 3.0 / 28, 1.0 / 28] # hand calcs for i in range(4): assert abs(e_tally[i] - expected_e_tally[i]) / expected_e_tally[i] < 0.1 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_single_hex_single_subvoxel_analog(): """This test tests that particles of sampled evenly within the phase-space of a single mesh volume element (also a sub-voxel) with one energy group in an analog sampling scheme. This done by dividing each dimension (x, y, z, E) in half, then sampling particles and tallying on the basis of which of the 2^4 = 16 regions of phase space the particle is born into. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]], mats=None, ) m.src = NativeMeshTag(1, float) m.src[0] = 1.0 cell_fracs = np.zeros( 1, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 1.0, 0.0)] m.tag_cell_fracs(cell_fracs) filename = "sampling_mesh.h5m" e_bounds = np.array([0.0, 1.0]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_ANALOG) num_samples = 5000 score = 1.0 / num_samples num_divs = 2 tally = np.zeros(shape=(num_divs, num_divs, num_divs, num_divs)) for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0.0, 1.0) for x in range(6)])) assert_equal(s.w, 1.0) # analog: all weights must be one assert_equal(s.cell_list[0], 11) # analog: the cell number tally[ int(s.x * num_divs), int(s.y * num_divs), int(s.z * num_divs), int(s.e * num_divs), ] += score # Test that each half-space of phase space (e.g. x > 0.5) is sampled about # half the time. for i in range(0, 4): for j in range(0, 2): assert abs(np.sum(np.rollaxis(tally, i)[j, :, :, :]) - 0.5) < 0.05 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_single_hex_multiple_subvoxel_analog(): """This test tests that particles of sampled analog within the phase-space of a single mesh volume element but multiple sub-voxels with one energy group in an analog sampling scheme. Then sampling particles and tallying the particles and check the probability of particles born in each sub-voxel and the cell_number. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]], mats=None, ) m.src = NativeMeshTag(3, float) m.src[:] = np.empty(shape=(1, 3)) m.src[0] = [0, 0.2, 0.8] cell_fracs = np.zeros( 3, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 0.4, 0.0), (0, 12, 0.3, 0.0), (0, 13, 0.3, 0.0)] m.tag_cell_fracs(cell_fracs) # cell_fracs will be sorted filename = "sampling_mesh.h5m" e_bounds = np.array([0, 1]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_ANALOG) num_samples = 50000 score = 1.0 / num_samples num_divs = 2 tally = [0.0] * 3 for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)])) assert_equal(s.w, 1.0) # analog: all weights must be one if s.cell_list[0] == 11: tally[0] += score elif s.cell_list[0] == 12: tally[1] += score elif s.cell_list[0] == 13: tally[2] += score # Test that each source particle in each cell has right frequency assert_equal(tally[0], 0.0) assert abs(tally[1] - 0.2) / 0.2 < 0.05 assert abs(tally[2] - 0.8) / 0.8 < 0.05 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_multiple_hex_multiple_subvoxel_analog(): """This test tests that particle are sampled analog from a uniform source defined on eight mesh volume elements in two energy groups. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5, 1]], mats=None, ) m.src = NativeMeshTag(2, float) m.src[:] = np.ones(shape=(8, 2)) cell_fracs = np.zeros( 8, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 1, 1.0, 0.0), (1, 2, 1.0, 0.0), (2, 3, 1.0, 0.0), (3, 4, 1.0, 0.0), (4, 5, 1.0, 0.0), (5, 6, 1.0, 0.0), (6, 7, 1.0, 0.0), (7, 8, 1.0, 0.0), ] m.tag_cell_fracs(cell_fracs) filename = "sampling_mesh.h5m" e_bounds = np.array([0, 0.5, 1]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_ANALOG) num_samples = 5000 score = 1.0 / num_samples num_divs = 2 tally = np.zeros(shape=(num_divs, num_divs, num_divs, num_divs)) for i in range(num_samples): s = sampler.particle_birth([uniform(0, 1) for x in range(6)]) assert_equal(s.w, 1.0) assert_equal( s.cell_list[0], 4 * int(s.x * num_divs) + 2 * int(s.y * num_divs) + int(s.z * num_divs) + 1, ) tally[ int(s.x * num_divs), int(s.y * num_divs), int(s.z * num_divs), int(s.e * num_divs), ] += score for i in range(0, 4): for j in range(0, 2): halfspace_sum = np.sum(np.rollaxis(tally, i)[j, :, :, :]) assert abs(halfspace_sum - 0.5) / 0.5 < 0.1 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_single_hex_subvoxel_uniform(): """This test tests that particles of sampled evenly within the phase-space of a single mesh volume element with one energy group in an uniform sampling scheme. This done by dividing each dimension (x, y, z, E) in half, then sampling particles and tallying on the basis of which of the 2^4 = 8 regions of phase space the particle is born into. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]], mats=None, ) m.src = NativeMeshTag(1, float) m.src[0] = 1.0 cell_fracs = np.zeros( 1, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 1.0, 0.0)] m.tag_cell_fracs(cell_fracs) filename = "sampling_mesh.h5m" e_bounds = np.array([0.0, 1.0]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_UNIFORM) num_samples = 5000 score = 1.0 / num_samples num_divs = 2 tally = np.zeros(shape=(num_divs, num_divs, num_divs, num_divs)) for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0.0, 1.0) for x in range(6)])) assert_equal(s.w, 1.0) # analog: all weights must be one assert_equal(s.cell_list[0], 11) # analog: the cell number tally[ int(s.x * num_divs), int(s.y * num_divs), int(s.z * num_divs), int(s.e * num_divs), ] += score # Test that each half-space of phase space (e.g. x > 0.5) is sampled about # half the time. for i in range(0, 4): for j in range(0, 2): assert abs(np.sum(np.rollaxis(tally, i)[j, :, :, :]) - 0.5) < 0.05 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_single_hex_multiple_subvoxel_uniform(): """This test tests that particles of sampled evenly within the phase-space of a single mesh volume element with one energy group in an uniform sampling scheme. This done by dividing each dimension (x, y, z, E) in half, then sampling particles and tallying on the basis of which of the 2^4 = 8 regions of phase space the particle is born into. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]], mats=None, ) m.src = NativeMeshTag(3, float) m.src[:] = np.empty(shape=(1, 3)) m.src[0] = [0, 0.2, 0.8] cell_fracs = np.zeros( 3, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 0.4, 0.0), (0, 12, 0.3, 0.0), (0, 13, 0.3, 0.0)] m.tag_cell_fracs(cell_fracs) filename = "sampling_mesh.h5m" e_bounds = np.array([0.0, 1.0]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_UNIFORM) num_samples = 5000 score = 1.0 / num_samples num_divs = 2 tally = [0.0] * 3 for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0.0, 1.0) for x in range(6)])) if s.cell_list[0] == 11: tally[0] += score if s.cell_list[0] == 12: tally[1] += score # analog: all weights must be one assert abs(s.w - 0.4) / 0.4 < 0.05 if s.cell_list[0] == 13: tally[2] += score assert abs(s.w - 1.6) / 1.6 < 0.05 # Test that each source particle in each cell has right frequency assert_equal(tally[0], 0.0) assert abs(tally[1] - 0.5) < 0.05 assert abs(tally[2] - 0.5) < 0.05 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_multiple_hex_multiple_subvoxel_uniform(): """This test tests that particle are sampled uniformly from a uniform source defined on eight mesh volume elements in two energy groups. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5, 1]], mats=None, ) m.src = NativeMeshTag(2, float) m.src[:] = np.empty(shape=(8, 2), dtype=float) m.src[:] = [[0, 0], [1, 0], [0, 0], [2, 0], [0, 0], [3, 0], [0, 0], [4, 0]] cell_fracs = np.zeros( 8, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 0, 1.0, 0.0), (1, 1, 1.0, 0.0), (2, 2, 1.0, 0.0), (3, 3, 1.0, 0.0), (4, 4, 1.0, 0.0), (5, 5, 1.0, 0.0), (6, 6, 1.0, 0.0), (7, 7, 1.0, 0.0), ] empty_cells = [0, 2, 4, 6] m.tag_cell_fracs(cell_fracs) filename = "sampling_mesh.h5m" e_bounds = np.array([0, 0.5, 1]) m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_UNIFORM) num_samples = 50000 score = 1.0 / num_samples num_divs = 2 tally = [0.0] * 8 for i in range(num_samples): s = sampler.particle_birth([uniform(0, 1) for x in range(6)]) # check the cell_number assert_equal( s.cell_list[0], 4 * int(s.x * num_divs) + 2 * int(s.y * num_divs) + int(s.z * num_divs), ) # check the weight of each subvoxel if s.cell_list[0] not in empty_cells: # weight for cell 1, 3, 5, 7 should be: 0.4, 0.8, 1.2, 1.6 exp_w = (s.cell_list[0] + 1) / 2 * 0.4 out_w = s.w assert abs(out_w - exp_w) / exp_w < 0.05 # hand calculate # count the tally tally[s.cell_list[0]] += score # check the real sample rate for i, item in enumerate(tally): if i not in empty_cells: assert abs(item - 0.25) / 0.25 < 0.05 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_bias(): """This test tests that a user-specified biasing scheme: 1. Samples space uniformly according to the scheme. 2. Adjusts weights accordingly. Sample calculations are provided in Case 2 in the Theory Manual. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]], mats=None ) m.src = NativeMeshTag(2, float) m.src[:] = [[2.0, 1.0], [9.0, 3.0]] e_bounds = np.array([0, 0.5, 1.0]) m.bias = NativeMeshTag(2, float) m.bias[:] = [[1.0, 2.0], [3.0, 3.0]] cell_fracs = np.zeros( 2, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 1.0, 0.0), (1, 11, 1.0, 0.0)] m.tag_cell_fracs(cell_fracs) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "bias_tag_name": "bias", "e_bounds_tag_name": "e_bounds", } filename = "sampling_mesh.h5m" m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) sampler = Sampler(filename, tag_names, e_bounds, DEFAULT_USER) num_samples = 10000 score = 1.0 / num_samples num_divs = 2 tally = np.zeros(shape=(4)) for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)])) if s.x < 3: if s.e < 0.5: assert_almost_equal(s.w, 1.6) # hand calcs tally[0] += score else: assert_almost_equal(s.w, 0.4) # hand calcs tally[1] += score else: if s.e < 0.5: assert_almost_equal(s.w, 2.4) # hand calcs tally[2] += score else: assert_almost_equal(s.w, 0.8) # hand calcs tally[3] += score expected_tally = [0.25, 0.5, 0.125, 0.125] # hand calcs for a, b in zip(tally, expected_tally): assert abs(a - b) / b < 0.25 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_bias_spatial(): """This test tests a user-specified biasing scheme for which the only 1 bias group is supplied for a source distribution containing two energy groups. This bias group is applied to both energy groups. In this test, the user-supplied bias distribution that was choosen, correspondes to uniform sampling, so that results can be checked against Case 1 in the theory manual. """ seed(1953) m = Mesh( structured=True, structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]], mats=None ) m.src = NativeMeshTag(2, float) m.src[:] = [[2.0, 1.0], [9.0, 3.0]] m.bias = NativeMeshTag(1, float) m.bias[:] = [1, 1] e_bounds = np.array([0, 0.5, 1.0]) filename = "sampling_mesh.h5m" cell_fracs = np.zeros( 2, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [(0, 11, 1.0, 0.0), (1, 11, 1.0, 0.0)] m.tag_cell_fracs(cell_fracs) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "bias_tag_name": "bias", "e_bounds_tag_name": "e_bounds", } m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) sampler = Sampler(filename, tag_names, e_bounds, DEFAULT_USER) num_samples = 10000 score = 1.0 / num_samples num_divs = 2 num_e = 2 spatial_tally = np.zeros(shape=(num_divs, num_divs, num_divs)) e_tally = np.zeros(shape=(4)) # number of phase space groups for i in range(num_samples): s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)])) if s.x < 3.0: assert_almost_equal(s.w, 0.7) # hand calcs else: assert_almost_equal(s.w, 2.8) # hand calcs spatial_tally[ int(s.x * num_divs / 3.5), int(s.y * num_divs / 1.0), int(s.z * num_divs / 1.0), ] += score if s.x < 3 and s.e < 0.5: e_tally[0] += score elif s.x < 3 and s.e > 0.5: e_tally[1] += score if s.x > 3 and s.e < 0.5: e_tally[2] += score if s.x > 3 and s.e > 0.5: e_tally[3] += score for i in range(0, 3): for j in range(0, 2): halfspace_sum = np.sum(np.rollaxis(spatial_tally, i)[j, :, :]) assert abs(halfspace_sum - 0.5) / 0.5 < 0.1 expected_e_tally = [4.0 / 7, 2.0 / 7, 3.0 / 28, 1.0 / 28] # hand calcs for i in range(4): assert abs(e_tally[i] - expected_e_tally[i]) / expected_e_tally[i] < 0.1 @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_subvoxel_multiple_hex_bias_1(): """This test tests that particle are sampled from a biased source defined on two voxels (2*2 = 4 sub-voxels) with the biased tag length of 1. """ seed(1953) # mesh contains two voxels. 2 * 1 * 1 = 2 m = Mesh( structured=True, structured_coords=[[0, 0.5, 1], [0, 1], [0, 1]], mats=None ) # max_num_cells = 2. 4 sub-voxels cell_fracs = np.zeros( 4, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 11, 0.5, 0.0), (0, 12, 0.5, 0.0), (1, 21, 0.5, 0.0), (1, 22, 0.5, 0.0), ] m.tag_cell_fracs(cell_fracs) # the photon emitting rate of 4 sub-voxels is 0.1, 0.2, 0.3, 0.4 m.src = NativeMeshTag(4, float) m.src[:] = np.empty(shape=(2, 4), dtype=float) m.src[:] = [[0.05, 0.05, 0.10, 0.10], [0.15, 0.15, 0.20, 0.20]] e_bounds = np.array([0, 0.5, 1.0]) # bias, tag size = 1 m.bias = NativeMeshTag(1, float) m.bias[:] = [[0.4], [0.6]] filename = "sampling_mesh.h5m" m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "bias_tag_name": "bias", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_USER) num_samples = 50000 score = 1.0 / num_samples num_divs = 2 # tally shape (v, c, e) tally = np.zeros(shape=(num_divs, num_divs, num_divs)) for i in range(num_samples): s = sampler.particle_birth([uniform(0, 1) for x in range(6)]) vid = s.cell_list[0] // 10 - 1 cid = s.cell_list[0] % 10 - 1 eid = 0 if s.e < 0.5 else 1 # check the cell_number if s.x < 0.5: assert s.cell_list[0] in [11, 12] if s.x > 0.5: assert s.cell_list[0] in [21, 22] # check the weight of each subvoxel if vid == 0: assert abs(s.w - 0.746) / 0.746 < 0.05 if vid == 1: assert abs(s.w - 1.163) / 1.163 < 0.05 # count the tally tally[vid, cid, eid] += score # check the real sample rate # exp_tally calculated by hand exp_tally = np.zeros(shape=(2, 2, 2)) exp_tally[:] = [[[0.067, 0.067], [0.133, 0.133]], [[0.129, 0.129], [0.171, 0.171]]] for v in range(2): for c in range(2): for e in range(2): assert ( abs(tally[v, c, e] - exp_tally[v, c, e]) / exp_tally[v, c, e] < 0.05 ) @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_subvoxel_multiple_hex_bias_max_num_cells_num_e_groups(): """This test tests that particle are sampled from a biased source defined on two voxels (2*2 = 4 sub-voxels) with the biased tag length of max_num_cells*num_e_group. """ seed(1953) # mesh contains two voxels. 2 * 1 * 1 = 2 m = Mesh( structured=True, structured_coords=[[0, 0.5, 1], [0, 1], [0, 1]], mats=None ) # max_num_cells = 2. 4 sub-voxels cell_fracs = np.zeros( 4, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 11, 0.5, 0.0), (0, 12, 0.5, 0.0), (1, 21, 0.5, 0.0), (1, 22, 0.5, 0.0), ] m.tag_cell_fracs(cell_fracs) # the photon emitting rate of 4 sub-voxels is 0.1, 0.2, 0.3, 0.4 m.src = NativeMeshTag(4, float) m.src[:] = np.empty(shape=(2, 4), dtype=float) m.src[:] = [[0.125, 0.125, 0.125, 0.125], [0.125, 0.125, 0.125, 0.125]] e_bounds = np.array([0, 0.5, 1.0]) # bias, tag size = 1 m.bias = NativeMeshTag(4, float) m.bias[:] = [[0.125, 0.125, 0.1, 0.15], [0.1, 0.1, 0.15, 0.15]] filename = "sampling_mesh.h5m" m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "bias_tag_name": "bias", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_USER) num_samples = 50000 score = 1.0 / num_samples num_divs = 2 # tally shape (v, c, e) tally = np.zeros(shape=(num_divs, num_divs, num_divs)) exp_wgt = np.zeros(shape=(num_divs, num_divs, num_divs)) exp_wgt[:] = [[[1.0, 1.0], [1.25, 0.83]], [[1.25, 1.25], [0.83, 0.83]]] for i in range(num_samples): s = sampler.particle_birth([uniform(0, 1) for x in range(6)]) vid = s.cell_list[0] // 10 - 1 cid = s.cell_list[0] % 10 - 1 eid = 0 if s.e < 0.5 else 1 # check the cell_number if s.x < 0.5: assert s.cell_list[0] in [11, 12] if s.x > 0.5: assert s.cell_list[0] in [21, 22] # check the weight of each subvoxel assert abs(s.w - exp_wgt[vid, cid, eid]) / exp_wgt[vid, cid, eid] < 0.05 # count the tally tally[vid, cid, eid] += score # check the real sample rate exp_tally = np.zeros(shape=(2, 2, 2)) exp_tally[:] = [[[0.125, 0.125], [0.100, 0.150]], [[0.100, 0.100], [0.150, 0.150]]] for v in range(2): for c in range(2): for e in range(2): assert ( abs(tally[v, c, e] - exp_tally[v, c, e]) / exp_tally[v, c, e] < 0.05 ) @with_setup(None, try_rm_file("sampling_mesh.h5m")) def test_subvoxel_multiple_hex_bias_e_groups(): """This test tests that particle are sampled from a biased source defined on two voxels (2*2 = 4 sub-voxels) with the biased tag length of energy groups. """ seed(1953) # mesh contains two voxels. 2 * 1 * 1 = 2 m = Mesh( structured=True, structured_coords=[[0, 0.5, 1], [0, 1], [0, 1]], mats=None ) # max_num_cells = 2. 4 sub-voxels cell_fracs = np.zeros( 4, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 11, 0.5, 0.0), (0, 12, 0.5, 0.0), (1, 21, 0.5, 0.0), (1, 22, 0.5, 0.0), ] m.tag_cell_fracs(cell_fracs) # the photon emitting rate of 4 sub-voxels is 0.1, 0.2, 0.3, 0.4 m.src = NativeMeshTag(4, float) m.src[:] = np.empty(shape=(2, 4), dtype=float) m.src[:] = [[0.05, 0.05, 0.10, 0.10], [0.15, 0.15, 0.20, 0.20]] e_bounds = np.array([0, 0.5, 1.0]) # bias, tag size = 1 m.bias = NativeMeshTag(2, float) m.bias[:] = [[0.1, 0.3], [0.2, 0.4]] filename = "sampling_mesh.h5m" m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "bias_tag_name": "bias", "e_bounds_tag_name": "e_bounds", } sampler = Sampler(filename, tag_names, e_bounds, SUBVOXEL_USER) num_samples = 50000 score = 1.0 / num_samples num_divs = 2 # tally shape (v, c, e) tally = np.zeros(shape=(num_divs, num_divs, num_divs)) for i in range(num_samples): s = sampler.particle_birth([uniform(0, 1) for x in range(6)]) vid = s.cell_list[0] // 10 - 1 cid = s.cell_list[0] % 10 - 1 eid = 0 if s.e < 0.5 else 1 # check the cell_number if s.x < 0.5: assert s.cell_list[0] in [11, 12] if s.x > 0.5: assert s.cell_list[0] in [21, 22] # check the weight of each subvoxel if vid == 0 and eid == 0: assert abs(s.w - 1.5) / 1.5 < 0.05 if vid == 0 and eid == 1: assert abs(s.w - 0.5) / 0.5 < 0.05 if vid == 1 and eid == 0: assert abs(s.w - 1.75) / 1.75 < 0.05 if vid == 1 and eid == 1: assert abs(s.w - 0.875) / 0.875 < 0.05 # count the tally tally[vid, cid, eid] += score # check the real sample rate exp_tally = np.zeros(shape=(2, 2, 2)) exp_tally[:] = [ [[0.0333, 0.1000], [0.0667, 0.2000]], [[0.0857, 0.1714], [0.1143, 0.2286]], ] for v in range(2): for c in range(2): for e in range(2): assert ( abs(tally[v, c, e] - exp_tally[v, c, e]) / exp_tally[v, c, e] < 0.05 ) def test_alias_table(): """This tests that the AliasTable class produces samples in the ratios consistant with the supplied PDF. """ seed(1953) pdf = np.array([0.1, 0.2, 0.7]) at = AliasTable(pdf) num_samples = 50000 score = 1.0 / num_samples tally = np.zeros(shape=(3)) for i in range(num_samples): s = at.sample_pdf(uniform(0, 1), uniform(0, 1)) tally[s] += score for i in range(0, 3): assert abs(tally[i] - pdf[i]) / pdf[i] < 0.05 def point_in_tet(t, p): """This function determines if some point <p> lies within some tetrahedron <t> using the method described here: http://steve.hollasch.net/cgindex/geometry/ptintet.html """ matricies = [ np.array( [ [t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [t[2][0], t[2][1], t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1], ] ), np.array( [ [p[0], p[1], p[2], 1], [t[1][0], t[1][1], t[1][2], 1], [t[2][0], t[2][1], t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1], ] ), np.array( [ [t[0][0], t[0][1], t[0][2], 1], [p[0], p[1], p[2], 1], [t[2][0], t[2][1], t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1], ] ), np.array( [ [t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [p[0], p[1], p[2], 1], [t[3][0], t[3][1], t[3][2], 1], ] ), np.array( [ [t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [t[2][0], t[2][1], t[2][2], 1], [p[0], p[1], p[2], 1], ] ), ] determinates = [np.linalg.det(x) for x in matricies] return all(x >= 0 for x in determinates) or all(x < 0 for x in determinates) def test_template_examples(): """ An example of using source_sampling test template to do the test """ # DEFAULT and SUBVOXEL for mode in ( DEFAULT_ANALOG, DEFAULT_UNIFORM, DEFAULT_USER, SUBVOXEL_ANALOG, SUBVOXEL_UNIFORM, SUBVOXEL_USER, ): for num_e_groups in (1, 2): # num_bias_groups could be: # 1, num_e_groups, and max_num_cells*num_e_groups # test case: 1 voxel, 1 subvoxel cell_fracs_list = [(0, 1, 1.0, 0.0)] src_tag = [[1.0] * num_e_groups] if mode == DEFAULT_USER or mode == SUBVOXEL_USER: for num_bias_groups in (1, num_e_groups): bias_tag = [[1.0] * num_bias_groups] _source_sampling_test_template( mode, cell_fracs_list, src_tag, bias_tag ) else: _source_sampling_test_template(mode, cell_fracs_list, src_tag) # test case: 1 voxel, 2 subvoxels # create src and cell_fracs tag data if mode in (0, 1, 2): src_tag = [[1.0] * num_e_groups] cell_fracs_list = [(0, 1, 1.0, 0.0)] elif mode in (3, 4, 5): src_tag = [[1.0, 1.0] * num_e_groups] cell_fracs_list = [(0, 1, 0.5, 0.0), (0, 2, 0.5, 0.0)] if mode == DEFAULT_USER: for num_bias_groups in (1, num_e_groups): bias_tag = [[1.0] * num_bias_groups] _source_sampling_test_template( mode, cell_fracs_list, src_tag, bias_tag ) elif mode == SUBVOXEL_USER: for num_bias_groups in (1, num_e_groups, 2 * num_e_groups): bias_tag = [[1.0] * num_bias_groups] _source_sampling_test_template( mode, cell_fracs_list, src_tag, bias_tag ) else: _source_sampling_test_template(mode, cell_fracs_list, src_tag) # test case: 2 voxel, 2 subvoxels cell_fracs_list = [(0, 1, 1.0, 0.0), (1, 2, 1.0, 0.0)] src_tag = [[1.0] * num_e_groups, [1.0] * num_e_groups] if mode == DEFAULT_USER or mode == SUBVOXEL_USER: for num_bias_groups in (1, num_e_groups): bias_tag = [[1.0] * num_bias_groups, [1.0] * num_bias_groups] _source_sampling_test_template( mode, cell_fracs_list, src_tag, bias_tag ) else: _source_sampling_test_template(mode, cell_fracs_list, src_tag) # test case: 2 voxel, 4 subvoxels # create src and cell_fracs tag data if mode in (0, 1, 2): src_tag = [[1.0] * num_e_groups, [1.0] * num_e_groups] cell_fracs_list = [(0, 1, 1.0, 0.0), (1, 2, 1.0, 0.0)] elif mode in (3, 4, 5): src_tag = [[1.0, 1.0] * num_e_groups, [1.0, 1.0] * num_e_groups] cell_fracs_list = [ (0, 1, 0.5, 0.0), (0, 2, 0.5, 0.0), (1, 3, 0.5, 0.0), (1, 4, 0.5, 0.0), ] if mode == DEFAULT_USER: for num_bias_groups in (1, num_e_groups): bias_tag = [[1.0] * num_bias_groups, [1.0] * num_bias_groups] _source_sampling_test_template( mode, cell_fracs_list, src_tag, bias_tag ) elif mode == SUBVOXEL_USER: for num_bias_groups in (1, num_e_groups, 2 * num_e_groups): bias_tag = [[1.0] * num_bias_groups, [1.0] * num_bias_groups] _source_sampling_test_template( mode, cell_fracs_list, src_tag, bias_tag ) else: _source_sampling_test_template(mode, cell_fracs_list, src_tag) def _get_num_ve_sve_and_max_num_cells(cell_fracs): """ Calculate the num_ve, num_sve and max_num_cells Parameters ---------- cell_fracs : structured array, optional A sorted, one dimensional array, each entry containing the following fields: :idx: int The volume element index. :cell: int The geometry cell number. :vol_frac: float The volume fraction of the cell withing the mesh ve. :rel_error: float The relative error associated with the volume fraction. Returns ------- num_ve : int Number of the total voxels num_sve : int Number of the total subvoxels, eqaul to or greater than num_ve max_num_cells : int Max number of cells (subvoxels) in a voxel """ num_sve = len(cell_fracs) num_ve = len(set(cell_fracs["idx"])) max_num_cells = -1 for i in range(num_sve): max_num_cells = max(max_num_cells, len(cell_fracs[cell_fracs["idx"] == i])) return num_ve, num_sve, max_num_cells def _create_mesh_via_num_ve(num_ve): """ This function creates mesh from number of voxels Parameters ---------- num_ve : int Number of voxels Returns ------- mesh. MOAB mesh. """ x_bounds = [v * 1.0 / (num_ve) for v in range(num_ve + 1)] mesh = Mesh( structured=True, structured_coords=[x_bounds, [0, 1], [0, 1]], mats=None ) return mesh def _cal_pdf_and_biased_pdf(cell_fracs, src_tag, bias_tag=None): """ This function calcualtes the normalized pdf of source. Parameters ---------- cell_fracs : structured array A sorted, one dimensional array, each entry containing the following fields: :idx: int The volume element index. :cell: int The geometry cell number. :vol_frac: float The volume fraction of the cell withing the mesh ve. :rel_error: float The relative error associated with the volume fraction. src_tag : numpy array An one or two dimentional array contains data of the source tag. bias_tag : numpy array, optional An one or two dimentional array contains data of bias tag Returns ------- pdf : numpy array A three dimentional numpy array, shape=(num_ve, num_sve, num_e_groups) biased_pdf : numpy array A three dimentional numpy array, shape=(num_ve, num_sve, num_e_groups) """ num_ve, num_sve, max_num_cells = _get_num_ve_sve_and_max_num_cells(cell_fracs) num_e_groups = len(src_tag[0]) // max_num_cells pdf = np.empty(shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64) pdf.fill(0.0) for vid in range(num_ve): for svid in range(max_num_cells): for eid in range(num_e_groups): pdf[vid, svid, eid] = ( src_tag[vid][svid * num_e_groups + eid] * cell_fracs[vid * max_num_cells + svid]["vol_frac"] ) # normalize pdf = pdf / pdf.sum() # calculate biased_pdf biased_pdf = np.empty(shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64) biased_pdf.fill(0.0) # set up bias_array to proper value if bias_tag == None: # UNIFORM mode, set default bias_group and bias_array num_bias_groups = 1 bias_array = np.empty( shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64 ) for vid in range(num_ve): for svid in range(max_num_cells): for eid in range(num_e_groups): bias_array[vid, svid, eid] = ( src_tag[vid][svid * num_e_groups + eid] / np.array(src_tag[vid]).sum() ) else: # USER mode, set bias_array according to bias_tag num_bias_groups = len(bias_tag[0]) bias_array = np.empty( shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64 ) bias_array.fill(0.0) for vid in range(num_ve): for svid in range(max_num_cells): for eid in range(num_e_groups): if num_bias_groups == 1: bias_array[vid, svid, eid] = bias_tag[vid][0] elif num_bias_groups == num_e_groups: bias_array[vid, svid, eid] = bias_tag[vid][eid] elif num_bias_groups == max_num_cells * num_e_groups: bias_array[vid, svid, eid] = bias_tag[vid][ svid * num_e_groups + eid ] else: raise ValueError("Wrong bias_tag length") # calculate biased_pdf if num_bias_groups == 1: for vid in range(num_ve): for svid in range(max_num_cells): current_ve = cell_fracs[cell_fracs["idx"] == vid] biased_pdf[vid, svid, :] = ( bias_array[vid, svid, :] * current_ve[svid]["vol_frac"] ) elif num_bias_groups == num_e_groups: for vid in range(num_ve): for eid in range(num_e_groups): for svid in range(max_num_cells): current_ve = cell_fracs[cell_fracs["idx"] == vid] biased_pdf[vid, svid, eid] = ( bias_array[vid, svid, eid] * current_ve[svid]["vol_frac"] ) elif num_bias_groups == max_num_cells * num_e_groups: for vid in range(num_ve): for svid in range(max_num_cells): for eid in range(num_e_groups): biased_pdf[vid, svid, eid] = ( bias_array[vid, svid, eid] * cell_fracs[vid]["vol_frac"] ) # normalize biased_pdf biased_pdf = np.divide(biased_pdf, biased_pdf.sum()) return pdf, biased_pdf def _cal_exp_w_c(s, mode, cell_fracs, src_tag, bias_tag): """ This function calcualtes the exptected weight and cell_number for a given particle (according to it's x coordinate) Parameters ---------- s : SourceParticle The given particle mode : int Mode of the source_sampling cell_fracs : structured array A sorted, one dimensional array, each entry containing the following fields: :idx: int The volume element index. :cell: int The geometry cell number. :vol_frac: float The volume fraction of the cell withing the mesh ve. :rel_error: float The relative error associated with the volume fraction. src_tag : numpy array An one or two dimentional array contains data of the source tag. bias_tag : numpy array, optional An one or two dimentional array contains data of bias tag Returns ------- exp_w : float Expected weight of the source particle exp_c : set of available cell numbers Expected cell number of the source particle """ num_ve, num_sve, max_num_cells = _get_num_ve_sve_and_max_num_cells(cell_fracs) # calculate vid x_bounds = [v * 1.0 / (num_ve) for v in range(num_ve + 1)] vid = -1 for i in range(num_ve): if x_bounds[i] <= s.x <= x_bounds[i + 1]: vid = i break if vid == -1: raise ValueError( "x coordinate of particle not in (0, 1), s.x = {0}".format(str(s.x)) ) # calculate svid # get number of cells/subvoxels of current voxel current_cell_fracs = cell_fracs[cell_fracs["idx"] == vid] num_cells = len(current_cell_fracs) x_bounds = np.array([0.0] * (num_cells + 1)) # the x_bounds of the vid start from 1.0/num_ve*vid x_bounds[0] = 1.0 / num_ve * vid for svid in range(num_cells): x_bounds[svid + 1] = ( x_bounds[svid] + 1.0 / num_ve * current_cell_fracs[svid]["vol_frac"] ) svid = -1 for i in range(num_cells): if x_bounds[i] <= s.x <= x_bounds[i + 1]: svid = i break if svid == -1: raise ValueError("x coordinate not in the voxel, s.x = {0}".format(str(s.x))) # get the cell_number exp_c = set(list(current_cell_fracs["cell"])) # calculate eid if mode in (0, 1, 2): num_e_groups = len(src_tag[0]) elif mode in (3, 4, 5): num_e_groups = len(src_tag[0]) // max_num_cells e_bounds = np.array([i * 1.0 / num_e_groups for i in range(num_e_groups + 1)]) eid = -1 for i in range(num_e_groups): if e_bounds[i] <= s.e <= e_bounds[i + 1]: eid = i break if eid == -1: raise ValueError("energy not in (0, 1), s.e = ".format(str(s.e))) # calculate exp_w, weight is determined by mode, vid, svid and energy if mode in (0, 3): # ANALOG exp_w = 1.0 elif mode in (1, 4): # UNIFORM pdf, biased_pdf = _cal_pdf_and_biased_pdf(cell_fracs, src_tag, bias_tag) exp_w = pdf[vid, svid, eid] / biased_pdf[vid, svid, eid] else: # USER pdf, biased_pdf = _cal_pdf_and_biased_pdf(cell_fracs, src_tag, bias_tag) exp_w = pdf[vid, svid, eid] / biased_pdf[vid, svid, eid] return exp_w, exp_c def _get_p_y_z_halfspace(particles): """ This function calcualtes the probabilities of y and z half space for a given set of particles Parameters ---------- particles : list List of SourceParticle Returns ------- p_y_halfspace : float The probability of y half space p_z_halfspace : float The probability of z half space """ y_count, z_count = 0, 0 for s in particles: if s.y < 0.5: y_count = y_count + 1 if s.z < 0.5: z_count = z_count + 1 p_y_halfspace = float(y_count) / len(particles) p_z_halfspace = float(z_count) / len(particles) return p_y_halfspace, p_z_halfspace def _get_x_dis(particles, num_ve): """ This function calcualtes the particle distribution along x direction for a given set of particles Parameters ---------- particles : list List of SourceParticle num_ve : int Number of voxels Returns ------- x_dis : one dimentional numpy array The particle direction along x direction """ x_bounds = [v * 1.0 / (num_ve) for v in range(num_ve + 1)] x_dis = np.array([0.0] * num_ve) for i in range(num_ve): for s in particles: if x_bounds[i] <= s.x <= x_bounds[i + 1]: x_dis[i] = x_dis[i] + 1 x_dis = np.divide(x_dis, len(particles)) return x_dis def _get_x_dis_exp(mode, cell_fracs, src_tag, bias_tag=None): """ This function calcualtes the exptected particle distribution along x direction. Parameters ---------- mode : int Mode of the source_sampling cell_fracs : structured array A sorted, one dimensional array, each entry containing the following fields: :idx: int The volume element index. :cell: int The geometry cell number. :vol_frac: float The volume fraction of the cell withing the mesh ve. :rel_error: float The relative error associated with the volume fraction. src_tag : numpy array An one or two dimentional array contains data of the source tag. bias_tag : numpy array, optional An one or two dimentional array contains data of bias tag Returns ------- x_dis_exp : one dimentional numpy array The expected particle direction along x direction """ num_ve, num_sve, max_num_cells = _get_num_ve_sve_and_max_num_cells(cell_fracs) if mode in (0, 1, 2): num_e_groups = len(src_tag[0]) elif mode in (3, 4, 5): num_e_groups = len(src_tag[0]) // max_num_cells x_bounds = [v * 1.0 / (num_ve) for v in range(num_ve + 1)] x_dis_exp = np.array([0.0] * num_ve) if mode in (0, 3): # ANALOG, particles distribution according to the src_tag for vid in range(num_ve): current_ve = cell_fracs[cell_fracs["idx"] == vid] for svid in range(len(current_ve)): x_dis_exp[vid] += ( current_ve[svid]["vol_frac"] * np.array( src_tag[vid][svid * num_e_groups : (svid + 1) * num_e_groups] ).sum() ) elif mode in (1, 4): # UNIFORM, particles distribution uniformly in x direction x_dis_exp = np.array([1.0 / num_ve] * num_ve) elif mode in (2, 5): if bias_tag == None: raise ValueError( "bias_tag must be provided when mode is {0}".format(str(mode)) ) # USER, particles distribute accroding to the bias_tag for vid in range(num_ve): current_ve = cell_fracs[cell_fracs["idx"] == vid] for svid in range(len(current_ve)): x_dis_exp[vid] += ( current_ve[svid]["vol_frac"] * np.array( bias_tag[vid][svid * num_e_groups : (svid + 1) * num_e_groups] ).sum() ) # normalize x_dis_exp x_dis_exp = np.divide(x_dis_exp, x_dis_exp.sum()) return x_dis_exp def _get_e_dis(particles, num_e_groups): """ This function calcualtes the particle distribution along energy for a given set of particles Parameters ---------- particles : list List of SourceParticle num_e_groups : int Number of energy groups Returns ------- e_dis : one dimentional numpy array The particle direction along energy """ e_bounds = [e * 1.0 / (num_e_groups) for e in range(num_e_groups + 1)] e_dis = np.array([0.0] * num_e_groups) for i in range(num_e_groups): for s in particles: if e_bounds[i] <= s.e <= e_bounds[i + 1]: e_dis[i] = e_dis[i] + 1 e_dis = np.divide(e_dis, len(particles)) return e_dis def _get_e_dis_exp(mode, cell_fracs, src_tag, bias_tag=None): """ This function calcualtes the exptected particle distribution along energy Parameters ---------- mode : int Mode of the source_sampling cell_fracs : structured array A sorted, one dimensional array, each entry containing the following fields: :idx: int The volume element index. :cell: int The geometry cell number. :vol_frac: float The volume fraction of the cell withing the mesh ve. :rel_error: float The relative error associated with the volume fraction. src_tag : numpy array An one or two dimentional array contains data of the source tag. bias_tag : numpy array, optional An one or two dimentional array contains data of bias tag Returns ------- e_dis_exp : one dimentional numpy array The expected particle direction along energy """ # input check if mode in (2, 5) and bias_tag == None: raise ValueError("bias_tag must be provided when mode is {0}".format(str(mode))) num_ve, num_sve, max_num_cells = _get_num_ve_sve_and_max_num_cells(cell_fracs) if mode in (0, 1, 2): num_e_groups = len(src_tag[0]) elif mode in (3, 4, 5): num_e_groups = len(src_tag[0]) // max_num_cells e_bounds = [e * 1.0 / (num_e_groups) for e in range(num_e_groups + 1)] e_dis_exp = np.array([0.0] * num_e_groups) if mode in (0, 1, 3, 4) or (mode in (2, 5) and len(bias_tag[0]) == 1): # when mode is ANALOG and UNIFORM, or mode is USER but num_bias_groups is 1 # particles distribution according to the src_tag for vid in range(num_ve): current_ve = cell_fracs[cell_fracs["idx"] == vid] for svid in range(len(current_ve)): for eid in range(num_e_groups): e_dis_exp[eid] += ( current_ve[svid]["vol_frac"] * src_tag[vid][svid * num_e_groups + eid] ) elif mode == 2 or (mode == 5 and len(bias_tag[0]) == num_e_groups): # Energy is biased according to the bias_tag for vid in range(num_ve): current_ve = cell_fracs[cell_fracs["idx"] == vid] for svid in range(len(current_ve)): for eid in range(num_e_groups): e_dis_exp[eid] += current_ve[svid]["vol_frac"] * bias_tag[vid][eid] else: for vid in range(num_ve): current_ve = cell_fracs[cell_fracs["idx"] == vid] for svid in range(len(current_ve)): for eid in range(num_e_groups): e_dis_exp[eid] += ( current_ve[svid]["vol_frac"] * bias_tag[vid][svid * num_e_groups + eid] ) # normalize x_dis_exp e_dis_exp = np.divide(e_dis_exp, e_dis_exp.sum()) return e_dis_exp @with_setup(None, try_rm_file("sampling_mesh.h5m")) def _source_sampling_test_template(mode, cell_fracs_list, src_tag, bias_tag=None): """ This function serve as a template for all source_sampling test cases. It constrcut Sampler from input parameters. And then perform a standardized sampling and tally, Finally, it compares tallied results with exp_answers. Assumptions: * Use unit cube for all the meshes * Use structured meshes for all the tests * filename will always be: "sampling_mesh.h5m" * distribution changes only on X direction * uniform distribution in Y and Z directions * cell_number always equal to the index of sve + 1, no void cell * voxels have the same volume For example: cell_fracs = [(0, 1, 0.4, 0.0), (0, 2, 0.6, 0.0), (1, 3, 1.0, 0.0), (2, 4, 1.0, 0.0), ...] voxel idx v0 v1 v2 |------------|------------|------------|--- y | | | | | ^ z subvoxel | sve0| sve1 | sve2 | sve3 | . . . | / | | | | | |/ |------------|------------|------------|--- ----> x cell_number c1 c2 c3 c4 * Energy have only two options: - [0.0, 1.0] - [0.0, 0.5, 1.0] * Voxel number of meshes could be: - 1 voxel 1 subvoxel -> Single voxel single subvoxel - 1 voxel 2 subvoxel -> Single voxel multiple subvoxel - 2 voxel 2 subvoxel -> Multiple voxel multiple subvoxel - 2 voxel 4 subvoxel -> Multiple voxel multiple subvoxel Under these assumptions: * Mesh could be derived from cell_fracs * e_bounds could be derived from src_tag * construct_paras contain: - mode - cell_fracs - src_tag - bias_tag (optional, required for bias_mode == USER) Check items: * weight for each particle * cell_number for each particle * position distribution * energy distribution Parameters ---------- mode : int Mode of the source sampling, could be 0, 1, 2, 3, 4 or 5 cell_fracs_list : numpy array A one dimentional numpy array used to construct cell_fracs, Element: (idx, cell, vol_frac, rel_error) src_tag : numpy array An one or two dimentional array contains data of the source tag. bias_tag : numpy array, optional An one or two dimentional array contains data of bias tag Returns ------- None """ sub_mode_r2s = (0, 1, 2) sub_mode_subvoxel = (3, 4, 5) avail_mode = (0, 1, 2, 3, 4, 5) # input check # check mode if mode not in avail_mode: raise ValueError("mode must be in (0, 1, 2, 3, 4, 5)") # set cell_fracs cell_fracs = np.zeros( len(cell_fracs_list), dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = cell_fracs_list # check bias_tag if mode in (2, 5) and bias_tag == None: # bias_mode == USER raise ValueError("bias_tag must be given when mode is {0}".format(str(mode))) # get number of voxel, max_num_cells num_ve, num_sve, max_num_cells = _get_num_ve_sve_and_max_num_cells(cell_fracs) # set up e_bounds if mode in (0, 1, 2): num_e_groups = len(src_tag[0]) elif mode in (3, 4, 5): num_e_groups = len(src_tag[0]) // max_num_cells e_bounds = [i * 1.0 / num_e_groups for i in range(num_e_groups + 1)] e_bounds = np.array(e_bounds) # set up mesh m = _create_mesh_via_num_ve(num_ve) # set up src tag if mode in (0, 1, 2): m.src = NativeMeshTag(num_e_groups, float) elif mode in (3, 4, 5): m.src = NativeMeshTag(max_num_cells * num_e_groups, float) m.src[:] = src_tag # set up cell_number and cell_fracs tag m.tag_cell_fracs(cell_fracs) # set up bias tag if mode in (2, 5): bias_tag_lenght = len(bias_tag[0]) m.bias = NativeMeshTag(bias_tag_lenght, float) m.bias[:] = bias_tag # set up tag_names tag_names = { "src_tag_name": "src", "cell_number_tag_name": "cell_number", "cell_fracs_tag_name": "cell_fracs", "e_bounds_tag_name": "e_bounds", } if mode in (2, 5): tag_names["bias_tag_name"] = "bias" # save the mesh into h5m file filename = "sampling_mesh.h5m" m = tag_e_bounds(m, e_bounds) m.write_hdf5(filename) # construct Sampler sampler = Sampler(filename, tag_names, mode) # remove the temporary file os.remove(filename) # sampling and tally, tally should be defined by the mesh cell_fracs num_samples = 5000 particles = [] seed(1953) for i in range(num_samples): rands = np.array([uniform(0, 1) for x in range(6)]) s = sampler.particle_birth(rands) # check w, and c for each particle # calculate the expected weight and cell_number exp_w, exp_c = _cal_exp_w_c(s, mode, cell_fracs, src_tag, bias_tag) assert_equal(s.w, exp_w) # when mode in (0, 1, 2), the set exp_c is (-1), otherwise it contains # several available cell number if mode in (0, 1, 2): assert set(s.cell_list) == exp_c elif mode in (3, 4, 5): assert set(s.cell_list).issubset(exp_c) # store all the particles for the convinent of distribution check particles.append(s) # check position distribution # X direction follow specified distribution x_dis = _get_x_dis(particles, num_ve) x_dis_exp = _get_x_dis_exp(mode, cell_fracs, src_tag, bias_tag) for i in range(len(x_dis)): assert abs(x_dis[i] - x_dis_exp[i]) / x_dis_exp[i] < 0.05 # uniform in Y and Z directions p_y_halfspace, p_z_halfspace = _get_p_y_z_halfspace(particles) assert abs(p_y_halfspace - 0.5) / 0.5 < 0.05 assert abs(p_z_halfspace - 0.5) / 0.5 < 0.05 # check energy distribution e_dis = _get_e_dis(particles, num_e_groups) e_dis_exp = _get_e_dis_exp(mode, cell_fracs, src_tag, bias_tag) for i in range(len(e_dis)): if e_dis_exp[i] > 0: assert abs(e_dis[i] - e_dis_exp[i]) / e_dis_exp[i] < 0.05 else: assert_equal(e_dis[i], 0.0)
[ "os.remove", "pyne.r2s.tag_e_bounds", "numpy.empty", "pymoab.core.Core", "numpy.ones", "pyne.source_sampling.AliasTable", "warnings.simplefilter", "os.path.exists", "pyne.mesh.NativeMeshTag", "random.seed", "nose.tools.assert_raises", "numpy.rollaxis", "numpy.linalg.det", "imp.load_module", "imp.find_module", "pyne.source_sampling.Sampler", "random.uniform", "numpy.zeros", "nose.tools.assert_almost_equal", "nose.tools.assert_equal", "pyne.mesh.Mesh", "numpy.array" ]
[((845, 887), 'warnings.simplefilter', 'warnings.simplefilter', (['"""ignore"""', 'QAWarning'], {}), "('ignore', QAWarning)\n", (866, 887), False, 'import warnings\n'), ((423, 446), 'imp.find_module', 'imp.find_module', (['"""pyne"""'], {}), "('pyne')\n", (438, 446), False, 'import imp\n'), ((462, 497), 'imp.load_module', 'imp.load_module', (['"""pyne"""', '*pyne_info'], {}), "('pyne', *pyne_info)\n", (477, 497), False, 'import imp\n'), ((502, 555), 'imp.find_module', 'imp.find_module', (['"""source_sampling"""', 'pyne_mod.__path__'], {}), "('source_sampling', pyne_mod.__path__)\n", (517, 555), False, 'import imp\n'), ((1425, 1435), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (1429, 1435), False, 'from random import uniform, seed\n'), ((1444, 1530), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 3, 3.5], [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]], mats\n =None)\n', (1448, 1530), False, 'from pyne.mesh import Mesh\n'), ((1552, 1575), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (1565, 1575), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((1631, 1654), 'numpy.array', 'np.array', (['[0, 0.5, 1.0]'], {}), '([0, 0.5, 1.0])\n', (1639, 1654), True, 'import numpy as np\n'), ((1663, 1688), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (1675, 1688), False, 'from pyne.r2s import tag_e_bounds\n'), ((1702, 1725), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (1715, 1725), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((1743, 1859), 'numpy.zeros', 'np.zeros', (['(2)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(2, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (1751, 1859), True, 'import numpy as np\n'), ((2387, 2431), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'DEFAULT_ANALOG'], {}), '(filename, tag_names, DEFAULT_ANALOG)\n', (2394, 2431), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((2715, 2769), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'DEFAULT_ANALOG'], {}), '(filename, tag_names, e_bounds, DEFAULT_ANALOG)\n', (2722, 2769), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((2961, 3046), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_ANALOG'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n DEFAULT_ANALOG)\n', (2974, 3046), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((3047, 3133), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_UNIFORM'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n DEFAULT_UNIFORM)\n', (3060, 3133), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((3134, 3213), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER)\n', (3147, 3213), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((3218, 3304), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_ANALOG'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n SUBVOXEL_ANALOG)\n', (3231, 3304), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((3305, 3392), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_UNIFORM'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n SUBVOXEL_UNIFORM)\n', (3318, 3392), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((3393, 3478), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER\n )\n', (3406, 3478), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((3697, 3776), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER)\n', (3710, 3776), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((3781, 3866), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER\n )\n', (3794, 3866), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4045, 4130), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_ANALOG'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n DEFAULT_ANALOG)\n', (4058, 4130), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4131, 4217), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_UNIFORM'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n DEFAULT_UNIFORM)\n', (4144, 4217), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4218, 4297), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER)\n', (4231, 4297), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4302, 4388), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_ANALOG'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n SUBVOXEL_ANALOG)\n', (4315, 4388), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4389, 4476), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_UNIFORM'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n SUBVOXEL_UNIFORM)\n', (4402, 4476), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4477, 4562), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER\n )\n', (4490, 4562), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4742, 4827), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_ANALOG'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n DEFAULT_ANALOG)\n', (4755, 4827), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4828, 4914), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_UNIFORM'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n DEFAULT_UNIFORM)\n', (4841, 4914), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4915, 4994), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'DEFAULT_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, DEFAULT_USER)\n', (4928, 4994), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((4999, 5085), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_ANALOG'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n SUBVOXEL_ANALOG)\n', (5012, 5085), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((5086, 5173), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_UNIFORM'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds,\n SUBVOXEL_UNIFORM)\n', (5099, 5173), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((5174, 5259), 'nose.tools.assert_raises', 'assert_raises', (['ValueError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(ValueError, Sampler, filename, tag_names, e_bounds, SUBVOXEL_USER\n )\n', (5187, 5259), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((5349, 5372), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (5362, 5372), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((5414, 5437), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (5427, 5437), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((5719, 5805), 'nose.tools.assert_raises', 'assert_raises', (['RuntimeError', 'Sampler', 'filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(RuntimeError, Sampler, filename, tag_names, e_bounds,\n SUBVOXEL_USER)\n', (5732, 5805), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((6260, 6270), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (6264, 6270), False, 'from random import uniform, seed\n'), ((6279, 6355), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 1], [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 1], [0, 1], [0, 1]], mats=None)\n', (6283, 6355), False, 'from pyne.mesh import Mesh\n'), ((6368, 6391), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(1)', 'float'], {}), '(1, float)\n', (6381, 6391), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((6428, 6544), 'numpy.zeros', 'np.zeros', (['(1)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(1, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (6436, 6544), True, 'import numpy as np\n'), ((6710, 6728), 'numpy.array', 'np.array', (['[0, 1.0]'], {}), '([0, 1.0])\n', (6718, 6728), True, 'import numpy as np\n'), ((6737, 6762), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (6749, 6762), False, 'from pyne.r2s import tag_e_bounds\n'), ((7028, 7082), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'DEFAULT_ANALOG'], {}), '(filename, tag_names, e_bounds, DEFAULT_ANALOG)\n', (7035, 7082), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((7166, 7222), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs, num_divs))\n', (7174, 7222), True, 'import numpy as np\n'), ((8123, 8133), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (8127, 8133), False, 'from random import uniform, seed\n'), ((8142, 8237), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5,\n 1]], mats=None)\n', (8146, 8237), False, 'from pyne.mesh import Mesh\n'), ((8277, 8300), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (8290, 8300), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((8316, 8337), 'numpy.ones', 'np.ones', ([], {'shape': '(8, 2)'}), '(shape=(8, 2))\n', (8323, 8337), True, 'import numpy as np\n'), ((8355, 8471), 'numpy.zeros', 'np.zeros', (['(8)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(8, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (8363, 8471), True, 'import numpy as np\n'), ((8876, 8897), 'numpy.array', 'np.array', (['[0, 0.5, 1]'], {}), '([0, 0.5, 1])\n', (8884, 8897), True, 'import numpy as np\n'), ((8906, 8931), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (8918, 8931), False, 'from pyne.r2s import tag_e_bounds\n'), ((9161, 9215), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'DEFAULT_ANALOG'], {}), '(filename, tag_names, e_bounds, DEFAULT_ANALOG)\n', (9168, 9215), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((9299, 9355), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs, num_divs))\n', (9307, 9355), True, 'import numpy as np\n'), ((10120, 10130), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (10124, 10130), False, 'from random import uniform, seed\n'), ((10142, 10156), 'pymoab.core.Core', 'mb_core.Core', ([], {}), '()\n', (10154, 10156), True, 'from pymoab import core as mb_core, types\n'), ((10360, 10393), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(False)', 'mesh': 'mesh'}), '(structured=False, mesh=mesh)\n', (10364, 10393), False, 'from pyne.mesh import Mesh\n'), ((10406, 10429), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(1)', 'float'], {}), '(1, float)\n', (10419, 10429), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((10445, 10458), 'numpy.array', 'np.array', (['[1]'], {}), '([1])\n', (10453, 10458), True, 'import numpy as np\n'), ((10499, 10519), 'numpy.array', 'np.array', (['[0.0, 1.0]'], {}), '([0.0, 1.0])\n', (10507, 10519), True, 'import numpy as np\n'), ((10528, 10553), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (10540, 10553), False, 'from pyne.r2s import tag_e_bounds\n'), ((10990, 11007), 'numpy.zeros', 'np.zeros', ([], {'shape': '(4)'}), '(shape=4)\n', (10998, 11007), True, 'import numpy as np\n'), ((11726, 11736), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (11730, 11736), False, 'from random import uniform, seed\n'), ((11745, 11839), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 3, 3.5], [0.0, 1.0], [0.0, 1.0]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 3, 3.5], [0.0, 1.0], [0.0, 1.0\n ]], mats=None)\n', (11749, 11839), False, 'from pyne.mesh import Mesh\n'), ((11878, 11901), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (11891, 11901), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((11957, 11982), 'numpy.array', 'np.array', (['[0.0, 0.5, 1.0]'], {}), '([0.0, 0.5, 1.0])\n', (11965, 11982), True, 'import numpy as np\n'), ((12035, 12151), 'numpy.zeros', 'np.zeros', (['(2)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(2, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (12043, 12151), True, 'import numpy as np\n'), ((12329, 12354), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (12341, 12354), False, 'from pyne.r2s import tag_e_bounds\n'), ((12584, 12639), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'DEFAULT_UNIFORM'], {}), '(filename, tag_names, e_bounds, DEFAULT_UNIFORM)\n', (12591, 12639), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((12746, 12792), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs))\n', (12754, 12792), True, 'import numpy as np\n'), ((12807, 12824), 'numpy.zeros', 'np.zeros', ([], {'shape': '(4)'}), '(shape=4)\n', (12815, 12824), True, 'import numpy as np\n'), ((14410, 14420), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (14414, 14420), False, 'from random import uniform, seed\n'), ((14429, 14522), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]\n ], mats=None)\n', (14433, 14522), False, 'from pyne.mesh import Mesh\n'), ((14561, 14584), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(1)', 'float'], {}), '(1, float)\n', (14574, 14584), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((14621, 14737), 'numpy.zeros', 'np.zeros', (['(1)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(1, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (14629, 14737), True, 'import numpy as np\n'), ((14938, 14958), 'numpy.array', 'np.array', (['[0.0, 1.0]'], {}), '([0.0, 1.0])\n', (14946, 14958), True, 'import numpy as np\n'), ((14967, 14992), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (14979, 14992), False, 'from pyne.r2s import tag_e_bounds\n'), ((15222, 15277), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_ANALOG'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_ANALOG)\n', (15229, 15277), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((15361, 15417), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs, num_divs))\n', (15369, 15417), True, 'import numpy as np\n'), ((16523, 16533), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (16527, 16533), False, 'from random import uniform, seed\n'), ((16542, 16635), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]\n ], mats=None)\n', (16546, 16635), False, 'from pyne.mesh import Mesh\n'), ((16674, 16697), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(3)', 'float'], {}), '(3, float)\n', (16687, 16697), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((16713, 16735), 'numpy.empty', 'np.empty', ([], {'shape': '(1, 3)'}), '(shape=(1, 3))\n', (16721, 16735), True, 'import numpy as np\n'), ((16782, 16898), 'numpy.zeros', 'np.zeros', (['(3)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(3, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (16790, 16898), True, 'import numpy as np\n'), ((17166, 17182), 'numpy.array', 'np.array', (['[0, 1]'], {}), '([0, 1])\n', (17174, 17182), True, 'import numpy as np\n'), ((17191, 17216), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (17203, 17216), False, 'from pyne.r2s import tag_e_bounds\n'), ((17446, 17501), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_ANALOG'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_ANALOG)\n', (17453, 17501), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((18042, 18069), 'nose.tools.assert_equal', 'assert_equal', (['tally[0]', '(0.0)'], {}), '(tally[0], 0.0)\n', (18054, 18069), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((18416, 18426), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (18420, 18426), False, 'from random import uniform, seed\n'), ((18435, 18530), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5,\n 1]], mats=None)\n', (18439, 18530), False, 'from pyne.mesh import Mesh\n'), ((18570, 18593), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (18583, 18593), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((18609, 18630), 'numpy.ones', 'np.ones', ([], {'shape': '(8, 2)'}), '(shape=(8, 2))\n', (18616, 18630), True, 'import numpy as np\n'), ((18648, 18764), 'numpy.zeros', 'np.zeros', (['(8)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(8, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (18656, 18764), True, 'import numpy as np\n'), ((19161, 19182), 'numpy.array', 'np.array', (['[0, 0.5, 1]'], {}), '([0, 0.5, 1])\n', (19169, 19182), True, 'import numpy as np\n'), ((19191, 19216), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (19203, 19216), False, 'from pyne.r2s import tag_e_bounds\n'), ((19446, 19501), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_ANALOG'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_ANALOG)\n', (19453, 19501), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((19584, 19640), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs, num_divs))\n', (19592, 19640), True, 'import numpy as np\n'), ((20742, 20752), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (20746, 20752), False, 'from random import uniform, seed\n'), ((20761, 20854), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]\n ], mats=None)\n', (20765, 20854), False, 'from pyne.mesh import Mesh\n'), ((20893, 20916), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(1)', 'float'], {}), '(1, float)\n', (20906, 20916), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((20953, 21069), 'numpy.zeros', 'np.zeros', (['(1)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(1, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (20961, 21069), True, 'import numpy as np\n'), ((21270, 21290), 'numpy.array', 'np.array', (['[0.0, 1.0]'], {}), '([0.0, 1.0])\n', (21278, 21290), True, 'import numpy as np\n'), ((21299, 21324), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (21311, 21324), False, 'from pyne.r2s import tag_e_bounds\n'), ((21554, 21610), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_UNIFORM'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_UNIFORM)\n', (21561, 21610), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((21694, 21750), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs, num_divs))\n', (21702, 21750), True, 'import numpy as np\n'), ((22883, 22893), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (22887, 22893), False, 'from random import uniform, seed\n'), ((22902, 22995), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]\n ], mats=None)\n', (22906, 22995), False, 'from pyne.mesh import Mesh\n'), ((23034, 23057), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(3)', 'float'], {}), '(3, float)\n', (23047, 23057), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((23073, 23095), 'numpy.empty', 'np.empty', ([], {'shape': '(1, 3)'}), '(shape=(1, 3))\n', (23081, 23095), True, 'import numpy as np\n'), ((23142, 23258), 'numpy.zeros', 'np.zeros', (['(3)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(3, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (23150, 23258), True, 'import numpy as np\n'), ((23497, 23517), 'numpy.array', 'np.array', (['[0.0, 1.0]'], {}), '([0.0, 1.0])\n', (23505, 23517), True, 'import numpy as np\n'), ((23526, 23551), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (23538, 23551), False, 'from pyne.r2s import tag_e_bounds\n'), ((23781, 23837), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_UNIFORM'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_UNIFORM)\n', (23788, 23837), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((24451, 24478), 'nose.tools.assert_equal', 'assert_equal', (['tally[0]', '(0.0)'], {}), '(tally[0], 0.0)\n', (24463, 24478), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((24817, 24827), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (24821, 24827), False, 'from random import uniform, seed\n'), ((24836, 24931), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5,\n 1]], mats=None)\n', (24840, 24931), False, 'from pyne.mesh import Mesh\n'), ((24971, 24994), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (24984, 24994), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((25010, 25045), 'numpy.empty', 'np.empty', ([], {'shape': '(8, 2)', 'dtype': 'float'}), '(shape=(8, 2), dtype=float)\n', (25018, 25045), True, 'import numpy as np\n'), ((25143, 25259), 'numpy.zeros', 'np.zeros', (['(8)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(8, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (25151, 25259), True, 'import numpy as np\n'), ((25687, 25708), 'numpy.array', 'np.array', (['[0, 0.5, 1]'], {}), '([0, 0.5, 1])\n', (25695, 25708), True, 'import numpy as np\n'), ((25717, 25742), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (25729, 25742), False, 'from pyne.r2s import tag_e_bounds\n'), ((25972, 26028), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_UNIFORM'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_UNIFORM)\n', (25979, 26028), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((27236, 27246), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (27240, 27246), False, 'from random import uniform, seed\n'), ((27255, 27341), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 3, 3.5], [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]], mats\n =None)\n', (27259, 27341), False, 'from pyne.mesh import Mesh\n'), ((27363, 27386), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (27376, 27386), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((27442, 27465), 'numpy.array', 'np.array', (['[0, 0.5, 1.0]'], {}), '([0, 0.5, 1.0])\n', (27450, 27465), True, 'import numpy as np\n'), ((27479, 27502), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (27492, 27502), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((27561, 27677), 'numpy.zeros', 'np.zeros', (['(2)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(2, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (27569, 27677), True, 'import numpy as np\n'), ((28111, 28136), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (28123, 28136), False, 'from pyne.r2s import tag_e_bounds\n'), ((28178, 28230), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'DEFAULT_USER'], {}), '(filename, tag_names, e_bounds, DEFAULT_USER)\n', (28185, 28230), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((28315, 28332), 'numpy.zeros', 'np.zeros', ([], {'shape': '(4)'}), '(shape=4)\n', (28323, 28332), True, 'import numpy as np\n'), ((29575, 29585), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (29579, 29585), False, 'from random import uniform, seed\n'), ((29594, 29680), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 3, 3.5], [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]], mats\n =None)\n', (29598, 29680), False, 'from pyne.mesh import Mesh\n'), ((29702, 29725), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (29715, 29725), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((29779, 29802), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(1)', 'float'], {}), '(1, float)\n', (29792, 29802), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((29841, 29864), 'numpy.array', 'np.array', (['[0, 0.5, 1.0]'], {}), '([0, 0.5, 1.0])\n', (29849, 29864), True, 'import numpy as np\n'), ((29917, 30033), 'numpy.zeros', 'np.zeros', (['(2)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(2, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (29925, 30033), True, 'import numpy as np\n'), ((30433, 30458), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (30445, 30458), False, 'from pyne.r2s import tag_e_bounds\n'), ((30500, 30552), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'DEFAULT_USER'], {}), '(filename, tag_names, e_bounds, DEFAULT_USER)\n', (30507, 30552), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((30659, 30705), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs))\n', (30667, 30705), True, 'import numpy as np\n'), ((30720, 30737), 'numpy.zeros', 'np.zeros', ([], {'shape': '(4)'}), '(shape=4)\n', (30728, 30737), True, 'import numpy as np\n'), ((32086, 32096), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (32090, 32096), False, 'from random import uniform, seed\n'), ((32151, 32237), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 0.5, 1], [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 0.5, 1], [0, 1], [0, 1]], mats\n =None)\n', (32155, 32237), False, 'from pyne.mesh import Mesh\n'), ((32303, 32419), 'numpy.zeros', 'np.zeros', (['(4)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(4, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (32311, 32419), True, 'import numpy as np\n'), ((32748, 32771), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(4)', 'float'], {}), '(4, float)\n', (32761, 32771), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((32787, 32822), 'numpy.empty', 'np.empty', ([], {'shape': '(2, 4)', 'dtype': 'float'}), '(shape=(2, 4), dtype=float)\n', (32795, 32822), True, 'import numpy as np\n'), ((32906, 32929), 'numpy.array', 'np.array', (['[0, 0.5, 1.0]'], {}), '([0, 0.5, 1.0])\n', (32914, 32929), True, 'import numpy as np\n'), ((32968, 32991), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(1)', 'float'], {}), '(1, float)\n', (32981, 32991), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((33067, 33092), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (33079, 33092), False, 'from pyne.r2s import tag_e_bounds\n'), ((33355, 33408), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_USER)\n', (33362, 33408), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((33521, 33567), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs))\n', (33529, 33567), True, 'import numpy as np\n'), ((34289, 34314), 'numpy.zeros', 'np.zeros', ([], {'shape': '(2, 2, 2)'}), '(shape=(2, 2, 2))\n', (34297, 34314), True, 'import numpy as np\n'), ((34926, 34936), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (34930, 34936), False, 'from random import uniform, seed\n'), ((34991, 35077), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 0.5, 1], [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 0.5, 1], [0, 1], [0, 1]], mats\n =None)\n', (34995, 35077), False, 'from pyne.mesh import Mesh\n'), ((35143, 35259), 'numpy.zeros', 'np.zeros', (['(4)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(4, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (35151, 35259), True, 'import numpy as np\n'), ((35588, 35611), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(4)', 'float'], {}), '(4, float)\n', (35601, 35611), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((35627, 35662), 'numpy.empty', 'np.empty', ([], {'shape': '(2, 4)', 'dtype': 'float'}), '(shape=(2, 4), dtype=float)\n', (35635, 35662), True, 'import numpy as np\n'), ((35754, 35777), 'numpy.array', 'np.array', (['[0, 0.5, 1.0]'], {}), '([0, 0.5, 1.0])\n', (35762, 35777), True, 'import numpy as np\n'), ((35816, 35839), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(4)', 'float'], {}), '(4, float)\n', (35829, 35839), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((35952, 35977), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (35964, 35977), False, 'from pyne.r2s import tag_e_bounds\n'), ((36240, 36293), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_USER)\n', (36247, 36293), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((36406, 36452), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs))\n', (36414, 36452), True, 'import numpy as np\n'), ((36467, 36513), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs))\n', (36475, 36513), True, 'import numpy as np\n'), ((37213, 37238), 'numpy.zeros', 'np.zeros', ([], {'shape': '(2, 2, 2)'}), '(shape=(2, 2, 2))\n', (37221, 37238), True, 'import numpy as np\n'), ((37820, 37830), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (37824, 37830), False, 'from random import uniform, seed\n'), ((37885, 37971), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[[0, 0.5, 1], [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[[0, 0.5, 1], [0, 1], [0, 1]], mats\n =None)\n', (37889, 37971), False, 'from pyne.mesh import Mesh\n'), ((38037, 38153), 'numpy.zeros', 'np.zeros', (['(4)'], {'dtype': "[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), (\n 'rel_error', np.float64)]"}), "(4, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.\n float64), ('rel_error', np.float64)])\n", (38045, 38153), True, 'import numpy as np\n'), ((38482, 38505), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(4)', 'float'], {}), '(4, float)\n', (38495, 38505), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((38521, 38556), 'numpy.empty', 'np.empty', ([], {'shape': '(2, 4)', 'dtype': 'float'}), '(shape=(2, 4), dtype=float)\n', (38529, 38556), True, 'import numpy as np\n'), ((38640, 38663), 'numpy.array', 'np.array', (['[0, 0.5, 1.0]'], {}), '([0, 0.5, 1.0])\n', (38648, 38663), True, 'import numpy as np\n'), ((38702, 38725), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(2)', 'float'], {}), '(2, float)\n', (38715, 38725), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((38811, 38836), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (38823, 38836), False, 'from pyne.r2s import tag_e_bounds\n'), ((39099, 39152), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'e_bounds', 'SUBVOXEL_USER'], {}), '(filename, tag_names, e_bounds, SUBVOXEL_USER)\n', (39106, 39152), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((39265, 39311), 'numpy.zeros', 'np.zeros', ([], {'shape': '(num_divs, num_divs, num_divs)'}), '(shape=(num_divs, num_divs, num_divs))\n', (39273, 39311), True, 'import numpy as np\n'), ((40184, 40209), 'numpy.zeros', 'np.zeros', ([], {'shape': '(2, 2, 2)'}), '(shape=(2, 2, 2))\n', (40192, 40209), True, 'import numpy as np\n'), ((40693, 40703), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (40697, 40703), False, 'from random import uniform, seed\n'), ((40714, 40739), 'numpy.array', 'np.array', (['[0.1, 0.2, 0.7]'], {}), '([0.1, 0.2, 0.7])\n', (40722, 40739), True, 'import numpy as np\n'), ((40749, 40764), 'pyne.source_sampling.AliasTable', 'AliasTable', (['pdf'], {}), '(pdf)\n', (40759, 40764), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((40831, 40848), 'numpy.zeros', 'np.zeros', ([], {'shape': '(3)'}), '(shape=3)\n', (40839, 40848), True, 'import numpy as np\n'), ((48108, 48186), 'pyne.mesh.Mesh', 'Mesh', ([], {'structured': '(True)', 'structured_coords': '[x_bounds, [0, 1], [0, 1]]', 'mats': 'None'}), '(structured=True, structured_coords=[x_bounds, [0, 1], [0, 1]], mats=None)\n', (48112, 48186), False, 'from pyne.mesh import Mesh\n'), ((49428, 49499), 'numpy.empty', 'np.empty', ([], {'shape': '(num_ve, max_num_cells, num_e_groups)', 'dtype': 'np.float64'}), '(shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64)\n', (49436, 49499), True, 'import numpy as np\n'), ((49912, 49983), 'numpy.empty', 'np.empty', ([], {'shape': '(num_ve, max_num_cells, num_e_groups)', 'dtype': 'np.float64'}), '(shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64)\n', (49920, 49983), True, 'import numpy as np\n'), ((54524, 54557), 'numpy.array', 'np.array', (['([0.0] * (num_cells + 1))'], {}), '([0.0] * (num_cells + 1))\n', (54532, 54557), True, 'import numpy as np\n'), ((57298, 57322), 'numpy.array', 'np.array', (['([0.0] * num_ve)'], {}), '([0.0] * num_ve)\n', (57306, 57322), True, 'import numpy as np\n'), ((58882, 58906), 'numpy.array', 'np.array', (['([0.0] * num_ve)'], {}), '([0.0] * num_ve)\n', (58890, 58906), True, 'import numpy as np\n'), ((60764, 60794), 'numpy.array', 'np.array', (['([0.0] * num_e_groups)'], {}), '([0.0] * num_e_groups)\n', (60772, 60794), True, 'import numpy as np\n'), ((62508, 62538), 'numpy.array', 'np.array', (['([0.0] * num_e_groups)'], {}), '([0.0] * num_e_groups)\n', (62516, 62538), True, 'import numpy as np\n'), ((67902, 67920), 'numpy.array', 'np.array', (['e_bounds'], {}), '(e_bounds)\n', (67910, 67920), True, 'import numpy as np\n'), ((68799, 68824), 'pyne.r2s.tag_e_bounds', 'tag_e_bounds', (['m', 'e_bounds'], {}), '(m, e_bounds)\n', (68811, 68824), False, 'from pyne.r2s import tag_e_bounds\n'), ((68891, 68925), 'pyne.source_sampling.Sampler', 'Sampler', (['filename', 'tag_names', 'mode'], {}), '(filename, tag_names, mode)\n', (68898, 68925), False, 'from pyne.source_sampling import Sampler, AliasTable\n'), ((68962, 68981), 'os.remove', 'os.remove', (['filename'], {}), '(filename)\n', (68971, 68981), False, 'import os\n'), ((69103, 69113), 'random.seed', 'seed', (['(1953)'], {}), '(1953)\n', (69107, 69113), False, 'from random import uniform, seed\n'), ((7345, 7367), 'nose.tools.assert_equal', 'assert_equal', (['s.w', '(1.0)'], {}), '(s.w, 1.0)\n', (7357, 7367), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((9467, 9489), 'nose.tools.assert_equal', 'assert_equal', (['s.w', '(1.0)'], {}), '(s.w, 1.0)\n', (9479, 9489), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((10887, 10907), 'numpy.array', 'np.array', (['[0.0, 1.0]'], {}), '([0.0, 1.0])\n', (10895, 10907), True, 'import numpy as np\n'), ((11125, 11147), 'nose.tools.assert_equal', 'assert_equal', (['s.w', '(1.0)'], {}), '(s.w, 1.0)\n', (11137, 11147), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((15544, 15566), 'nose.tools.assert_equal', 'assert_equal', (['s.w', '(1.0)'], {}), '(s.w, 1.0)\n', (15556, 15566), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((15610, 15642), 'nose.tools.assert_equal', 'assert_equal', (['s.cell_list[0]', '(11)'], {}), '(s.cell_list[0], 11)\n', (15622, 15642), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((17716, 17738), 'nose.tools.assert_equal', 'assert_equal', (['s.w', '(1.0)'], {}), '(s.w, 1.0)\n', (17728, 17738), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((19752, 19774), 'nose.tools.assert_equal', 'assert_equal', (['s.w', '(1.0)'], {}), '(s.w, 1.0)\n', (19764, 19774), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((21877, 21899), 'nose.tools.assert_equal', 'assert_equal', (['s.w', '(1.0)'], {}), '(s.w, 1.0)\n', (21889, 21899), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((21943, 21975), 'nose.tools.assert_equal', 'assert_equal', (['s.cell_list[0]', '(11)'], {}), '(s.cell_list[0], 11)\n', (21955, 21975), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((41288, 41431), 'numpy.array', 'np.array', (['[[t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [t[2][0],\n t[2][1], t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1]]'], {}), '([[t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [\n t[2][0], t[2][1], t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1]])\n', (41296, 41431), True, 'import numpy as np\n'), ((41537, 41670), 'numpy.array', 'np.array', (['[[p[0], p[1], p[2], 1], [t[1][0], t[1][1], t[1][2], 1], [t[2][0], t[2][1],\n t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1]]'], {}), '([[p[0], p[1], p[2], 1], [t[1][0], t[1][1], t[1][2], 1], [t[2][0],\n t[2][1], t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1]])\n', (41545, 41670), True, 'import numpy as np\n'), ((41777, 41910), 'numpy.array', 'np.array', (['[[t[0][0], t[0][1], t[0][2], 1], [p[0], p[1], p[2], 1], [t[2][0], t[2][1],\n t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1]]'], {}), '([[t[0][0], t[0][1], t[0][2], 1], [p[0], p[1], p[2], 1], [t[2][0],\n t[2][1], t[2][2], 1], [t[3][0], t[3][1], t[3][2], 1]])\n', (41785, 41910), True, 'import numpy as np\n'), ((42017, 42151), 'numpy.array', 'np.array', (['[[t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [p[0], p[1\n ], p[2], 1], [t[3][0], t[3][1], t[3][2], 1]]'], {}), '([[t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [\n p[0], p[1], p[2], 1], [t[3][0], t[3][1], t[3][2], 1]])\n', (42025, 42151), True, 'import numpy as np\n'), ((42257, 42391), 'numpy.array', 'np.array', (['[[t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [t[2][0],\n t[2][1], t[2][2], 1], [p[0], p[1], p[2], 1]]'], {}), '([[t[0][0], t[0][1], t[0][2], 1], [t[1][0], t[1][1], t[1][2], 1], [\n t[2][0], t[2][1], t[2][2], 1], [p[0], p[1], p[2], 1]])\n', (42265, 42391), True, 'import numpy as np\n'), ((42516, 42532), 'numpy.linalg.det', 'np.linalg.det', (['x'], {}), '(x)\n', (42529, 42532), True, 'import numpy as np\n'), ((50185, 50256), 'numpy.empty', 'np.empty', ([], {'shape': '(num_ve, max_num_cells, num_e_groups)', 'dtype': 'np.float64'}), '(shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64)\n', (50193, 50256), True, 'import numpy as np\n'), ((50731, 50802), 'numpy.empty', 'np.empty', ([], {'shape': '(num_ve, max_num_cells, num_e_groups)', 'dtype': 'np.float64'}), '(shape=(num_ve, max_num_cells, num_e_groups), dtype=np.float64)\n', (50739, 50802), True, 'import numpy as np\n'), ((68042, 68076), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['num_e_groups', 'float'], {}), '(num_e_groups, float)\n', (68055, 68076), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((68377, 68414), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['bias_tag_lenght', 'float'], {}), '(bias_tag_lenght, float)\n', (68390, 68414), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((69432, 69456), 'nose.tools.assert_equal', 'assert_equal', (['s.w', 'exp_w'], {}), '(s.w, exp_w)\n', (69444, 69456), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((1091, 1115), 'os.path.exists', 'os.path.exists', (['filename'], {}), '(filename)\n', (1105, 1115), False, 'import os\n'), ((1068, 1087), 'os.remove', 'os.remove', (['filename'], {}), '(filename)\n', (1077, 1087), False, 'import os\n'), ((13006, 13035), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(0.7)'], {}), '(s.w, 0.7)\n', (13025, 13035), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((13076, 13105), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(2.8)'], {}), '(s.w, 2.8)\n', (13095, 13105), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((30919, 30948), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(0.7)'], {}), '(s.w, 0.7)\n', (30938, 30948), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((30989, 31018), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(2.8)'], {}), '(s.w, 2.8)\n', (31008, 31018), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((40911, 40924), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (40918, 40924), False, 'from random import uniform, seed\n'), ((40926, 40939), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (40933, 40939), False, 'from random import uniform, seed\n'), ((59501, 59534), 'numpy.array', 'np.array', (['([1.0 / num_ve] * num_ve)'], {}), '([1.0 / num_ve] * num_ve)\n', (59509, 59534), True, 'import numpy as np\n'), ((68121, 68171), 'pyne.mesh.NativeMeshTag', 'NativeMeshTag', (['(max_num_cells * num_e_groups)', 'float'], {}), '(max_num_cells * num_e_groups, float)\n', (68134, 68171), False, 'from pyne.mesh import Mesh, NativeMeshTag\n'), ((70635, 70662), 'nose.tools.assert_equal', 'assert_equal', (['e_dis[i]', '(0.0)'], {}), '(e_dis[i], 0.0)\n', (70647, 70662), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((9425, 9438), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (9432, 9438), False, 'from random import uniform, seed\n'), ((11079, 11096), 'random.uniform', 'uniform', (['(0.0)', '(1.0)'], {}), '(0.0, 1.0)\n', (11086, 11096), False, 'from random import uniform, seed\n'), ((19710, 19723), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (19717, 19723), False, 'from random import uniform, seed\n'), ((26191, 26204), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (26198, 26204), False, 'from random import uniform, seed\n'), ((28510, 28539), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(1.6)'], {}), '(s.w, 1.6)\n', (28529, 28539), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((28622, 28651), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(0.4)'], {}), '(s.w, 0.4)\n', (28641, 28651), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((28756, 28785), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(2.4)'], {}), '(s.w, 2.4)\n', (28775, 28785), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((28868, 28897), 'nose.tools.assert_almost_equal', 'assert_almost_equal', (['s.w', '(0.8)'], {}), '(s.w, 0.8)\n', (28887, 28897), False, 'from nose.tools import assert_equal, with_setup, assert_almost_equal, assert_raises\n'), ((33637, 33650), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (33644, 33650), False, 'from random import uniform, seed\n'), ((36659, 36672), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (36666, 36672), False, 'from random import uniform, seed\n'), ((39381, 39394), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (39388, 39394), False, 'from random import uniform, seed\n'), ((69173, 69186), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (69180, 69186), False, 'from random import uniform, seed\n'), ((7302, 7315), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (7309, 7315), False, 'from random import uniform, seed\n'), ((9748, 9769), 'numpy.rollaxis', 'np.rollaxis', (['tally', 'i'], {}), '(tally, i)\n', (9759, 9769), True, 'import numpy as np\n'), ((12937, 12950), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (12944, 12950), False, 'from random import uniform, seed\n'), ((13639, 13668), 'numpy.rollaxis', 'np.rollaxis', (['spatial_tally', 'i'], {}), '(spatial_tally, i)\n', (13650, 13668), True, 'import numpy as np\n'), ((15497, 15514), 'random.uniform', 'uniform', (['(0.0)', '(1.0)'], {}), '(0.0, 1.0)\n', (15504, 15514), False, 'from random import uniform, seed\n'), ((17673, 17686), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (17680, 17686), False, 'from random import uniform, seed\n'), ((20182, 20203), 'numpy.rollaxis', 'np.rollaxis', (['tally', 'i'], {}), '(tally, i)\n', (20193, 20203), True, 'import numpy as np\n'), ((21830, 21847), 'random.uniform', 'uniform', (['(0.0)', '(1.0)'], {}), '(0.0, 1.0)\n', (21837, 21847), False, 'from random import uniform, seed\n'), ((24008, 24025), 'random.uniform', 'uniform', (['(0.0)', '(1.0)'], {}), '(0.0, 1.0)\n', (24015, 24025), False, 'from random import uniform, seed\n'), ((28413, 28426), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (28420, 28426), False, 'from random import uniform, seed\n'), ((30850, 30863), 'random.uniform', 'uniform', (['(0)', '(1)'], {}), '(0, 1)\n', (30857, 30863), False, 'from random import uniform, seed\n'), ((31552, 31581), 'numpy.rollaxis', 'np.rollaxis', (['spatial_tally', 'i'], {}), '(spatial_tally, i)\n', (31563, 31581), True, 'import numpy as np\n'), ((59247, 59316), 'numpy.array', 'np.array', (['src_tag[vid][svid * num_e_groups:(svid + 1) * num_e_groups]'], {}), '(src_tag[vid][svid * num_e_groups:(svid + 1) * num_e_groups])\n', (59255, 59316), True, 'import numpy as np\n'), ((7756, 7777), 'numpy.rollaxis', 'np.rollaxis', (['tally', 'i'], {}), '(tally, i)\n', (7767, 7777), True, 'import numpy as np\n'), ((16023, 16044), 'numpy.rollaxis', 'np.rollaxis', (['tally', 'i'], {}), '(tally, i)\n', (16034, 16044), True, 'import numpy as np\n'), ((22356, 22377), 'numpy.rollaxis', 'np.rollaxis', (['tally', 'i'], {}), '(tally, i)\n', (22367, 22377), True, 'import numpy as np\n'), ((50548, 50570), 'numpy.array', 'np.array', (['src_tag[vid]'], {}), '(src_tag[vid])\n', (50556, 50570), True, 'import numpy as np\n'), ((60026, 60096), 'numpy.array', 'np.array', (['bias_tag[vid][svid * num_e_groups:(svid + 1) * num_e_groups]'], {}), '(bias_tag[vid][svid * num_e_groups:(svid + 1) * num_e_groups])\n', (60034, 60096), True, 'import numpy as np\n')]
from abc import ABC, abstractmethod import numpy as np from scipy import stats import torch class Experiment(ABC): ''' An Experiment manages the basic train/test loop and logs results. Args: writer (:torch.logging.writer:): A Writer object used for logging. quiet (bool): If False, the Experiment will print information about episode returns to standard out. ''' def __init__(self, writer, quiet): self._writer = writer self._quiet = quiet self._best_returns = -np.inf self._returns100 = [] @abstractmethod def train(self, frames=np.inf, episodes=np.inf): ''' Train the agent for a certain number of frames or episodes. If both frames and episodes are specified, then the training loop will exit when either condition is satisfied. Args: frames (int): The maximum number of training frames. episodes (bool): The maximum number of training episodes. ''' @abstractmethod def test(self, episodes=100): ''' Test the agent in eval mode for a certain number of episodes. Args: episodes (int): The number of test episodes. Returns: list(float): A list of all returns received during testing. ''' @property @abstractmethod def frame(self): '''The index of the current training frame.''' @property @abstractmethod def episode(self): '''The index of the current training episode''' def _log_training_episode(self, returns, fps): if not self._quiet: print('episode: {}, frame: {}, fps: {}, returns: {}'.format(self.episode, self.frame, int(fps), returns)) if returns > self._best_returns: self._best_returns = returns self._returns100.append(returns) if len(self._returns100) == 100: mean = np.mean(self._returns100) std = np.std(self._returns100) self._writer.add_summary('returns100', mean, std, step="frame") self._returns100 = [] self._writer.add_evaluation('returns/episode', returns, step="episode") self._writer.add_evaluation('returns/frame', returns, step="frame") self._writer.add_evaluation("returns/max", self._best_returns, step="frame") self._writer.add_scalar('fps', fps, step="frame") def _log_test_episode(self, episode, returns): if not self._quiet: print('test episode: {}, returns: {}'.format(episode, returns)) def _log_test(self, returns): if not self._quiet: print('test returns (mean ± sem): {} ± {}'.format(np.mean(returns), stats.sem(returns))) self._writer.add_summary('returns-test', np.mean(returns), np.std(returns)) def save(self): return self._preset.save('{}/preset.pt'.format(self._writer.log_dir)) def close(self): self._writer.close()
[ "numpy.std", "numpy.mean", "scipy.stats.sem" ]
[((1956, 1981), 'numpy.mean', 'np.mean', (['self._returns100'], {}), '(self._returns100)\n', (1963, 1981), True, 'import numpy as np\n'), ((2000, 2024), 'numpy.std', 'np.std', (['self._returns100'], {}), '(self._returns100)\n', (2006, 2024), True, 'import numpy as np\n'), ((2803, 2819), 'numpy.mean', 'np.mean', (['returns'], {}), '(returns)\n', (2810, 2819), True, 'import numpy as np\n'), ((2821, 2836), 'numpy.std', 'np.std', (['returns'], {}), '(returns)\n', (2827, 2836), True, 'import numpy as np\n'), ((2717, 2733), 'numpy.mean', 'np.mean', (['returns'], {}), '(returns)\n', (2724, 2733), True, 'import numpy as np\n'), ((2735, 2753), 'scipy.stats.sem', 'stats.sem', (['returns'], {}), '(returns)\n', (2744, 2753), False, 'from scipy import stats\n')]
import numpy as np import pandas as pd from sklearn.covariance import EmpiricalCovariance from typing import Union from modules.features.feature import Feature from modules.filters.dml.map_representation import from_map_representation_to_xy from modules.features.segment_feature import SegmentFeature from modules.features.landmark_feature import LandmarkFeature from modules.features.global_positioning_feature import GlobalPositioningFeature class GAPPF(): """ Implements the 'Gaussian Approximation of the Posterior-based Particle Filter' method for digital map-based localization. """ def __init__(self, prune_gamma : float, n_particles : int): """ Parameters ========== prune_gamma : float. The threshold of the relative score for prunning hypotheses. n_particles : int. The number of particles sampled for the estimation for each hypothesis """ self.routes = {} self.hypotheses = {} self.n_particles = n_particles self.gamma = prune_gamma return # STATE # ========================== def add_hypothesis(self, mean : float, variance : float, route : int): """ Create a new hypothesis. Parameters =============== mean: float. The mean position to of the hypothesis's distribution. variance: float. The variance of the hypothesis's distribution route: int. To which route this hypothesis belongs. """ h = np.array([mean, variance], dtype="float").reshape(1,2) route = int(route) self.hypotheses[route] = h return def prune_hypothesis(self, route_id : int): """ Remove a hypothesis. Parameters ========= route_id: int. The identifier of the route from which the hypothesis is going to be removed. """ del self.hypotheses[route_id] del self.routes[route_id] return def check_is_localized(self) -> bool: """ Indicates whether the estimation is reliable (localized) or not. Returns ==== bool. True if the estimation is localized (one hypothesis on the set of hypotheses). False otherwise. """ if len(self.hypotheses) == 1: return True return False def get_mean(self) -> np.array: """ Get the mean in x,y coordinates. Warning: it does not check if the method is localized! Returns ========= mean: numpy.array. The mean (x,y) position of the estimation. """ r_id = list( self.hypotheses )[0] hypothesis = self.hypotheses[r_id].flatten() mean, variance = hypothesis ls = np.random.normal(mean, variance ** 0.5, size=(self.n_particles)) route = self.routes[r_id] xy_list = [] for l in ls: xy = from_map_representation_to_xy(l, route) xy_list.append(xy) xy_array = np.vstack(xy_list) mean = np.mean(xy_array, axis=0) return mean def get_mean_and_covariance(self) -> Union[np.array, np.array] : """ Get the mean and covariance in x,y coordinates. Warning: it does not check if the method is localized! Returns ========= mean: numpy.array. The mean (x,y) position of the estimation. covariance: numpy.array. The covariance of the estimation. """ r_id = list( self.hypotheses )[0] hypothesis = self.hypotheses[r_id].flatten() mean, variance = hypothesis ls = np.random.normal(mean, variance ** 0.5, size=(self.n_particles)) route = self.routes[r_id] xy_list = [] for l in ls: xy = from_map_representation_to_xy(l, route) xy_list.append(xy) xy_array = np.vstack(xy_list) mean = np.mean(xy_array, axis=0) covariance = EmpiricalCovariance().fit(xy_array).covariance_ + np.full((2,2), 1.) return mean, covariance # ========================== # MAP # ========================== def add_route(self, route_id : int, ways : pd.DataFrame): """ Add a route to the set of routes. Parameters =========== route_id: int. The identifier of the route. ways: pandas.DataFrame. The DataFrame of the information of ways contained in the route. """ self.routes[route_id] = ways return # ========================== # HYPOTHESES MANAGEMENT # ========================== def copy_to_route(self, from_idx : int, to_idx : int): """ Copies one hypothesis's distribution to another route. Parameters ======= from_idx: int. The identifier of the route from which the hypothesis's distribution is going to be copied. to_idx: int. The identifier of the route to which the hypothesis's distribution is going to be copied. """ h = self.hypotheses[from_idx].flatten() mean, variance = h self.add_hypothesis(mean, variance, to_idx) return # ========================== # MOTION # ========================== def predict(self, odom : float, odom_var : float): """ Performs prediction of the distribution for each hypothesis. Parameters ============== odom: float. The forward odometry received. odom_var: float. The variance of the odometry. """ for r_id in self.routes: self.hypotheses[r_id][0,0] += odom self.hypotheses[r_id][0,1] += odom_var return # ========================== # UPDATE-RELATED TASKS # ========================== def update(self, feature : Feature) : """ Estimates the new distribution for each hypothesis given a measured feature. Performs branch pruning after estimation using the gamma defined at the instantiation. Parameters ============ feature: Feature-inherited objects. The measured feature detected. """ N = self.n_particles scores = {} max_score = None for route_id in self.routes: likelihoods = np.empty((N,)) h = self.hypotheses[route_id].flatten() route = self.routes[route_id] mean, variance = h l_samples = np.random.normal(mean, variance ** 0.5, size=(N)) # Compute Likelihoods if isinstance(feature,LandmarkFeature) or isinstance(feature, GlobalPositioningFeature): for i in range(N): l = l_samples[i] p_xy = from_map_representation_to_xy(l, route) likelihood = feature.measurement_model(p_xy) likelihoods[i] = likelihood elif isinstance(feature, SegmentFeature): for i in range(N): l = l_samples[i] likelihood = feature.measurement_model(l, route) likelihoods[i] = likelihood else: raise Exception("Error: feature type provided not implemented!") # Score each hypothesis score = np.sum(likelihoods) if max_score is None: max_score = score max_score = score if (score > max_score) else max_score scores[route_id] = score # If score is as low as zero, increase hypothesis' variance # since it might be on a very different place if score < 1e-9: self.hypotheses[route_id][0,1] += 1e0 continue eta = 1./score # Update their mean and variance new_mean = eta * np.sum( likelihoods * l_samples ) diff = l_samples - new_mean new_var = 0.0 for k in range(N): new_var += likelihoods[k] * (diff[k] ** 2.0) new_var *= eta self.hypotheses[route_id] = np.array([new_mean, new_var]).reshape(1,2) # If all the particles have a very low score, then do not prune hypotheses! if max_score <= 0: return # Normalize scores and detect routes to prune prune_ids = [] for route_id in self.routes: norm_score = scores[route_id] / max_score if norm_score < self.gamma: prune_ids.append(route_id) # Prune routes for route_id in prune_ids: self.prune_hypothesis(route_id) return # ==========================
[ "numpy.full", "modules.filters.dml.map_representation.from_map_representation_to_xy", "numpy.sum", "numpy.empty", "numpy.mean", "numpy.array", "numpy.random.normal", "sklearn.covariance.EmpiricalCovariance", "numpy.vstack" ]
[((2860, 2922), 'numpy.random.normal', 'np.random.normal', (['mean', '(variance ** 0.5)'], {'size': 'self.n_particles'}), '(mean, variance ** 0.5, size=self.n_particles)\n', (2876, 2922), True, 'import numpy as np\n'), ((3108, 3126), 'numpy.vstack', 'np.vstack', (['xy_list'], {}), '(xy_list)\n', (3117, 3126), True, 'import numpy as np\n'), ((3142, 3167), 'numpy.mean', 'np.mean', (['xy_array'], {'axis': '(0)'}), '(xy_array, axis=0)\n', (3149, 3167), True, 'import numpy as np\n'), ((3748, 3810), 'numpy.random.normal', 'np.random.normal', (['mean', '(variance ** 0.5)'], {'size': 'self.n_particles'}), '(mean, variance ** 0.5, size=self.n_particles)\n', (3764, 3810), True, 'import numpy as np\n'), ((3996, 4014), 'numpy.vstack', 'np.vstack', (['xy_list'], {}), '(xy_list)\n', (4005, 4014), True, 'import numpy as np\n'), ((4030, 4055), 'numpy.mean', 'np.mean', (['xy_array'], {'axis': '(0)'}), '(xy_array, axis=0)\n', (4037, 4055), True, 'import numpy as np\n'), ((3018, 3057), 'modules.filters.dml.map_representation.from_map_representation_to_xy', 'from_map_representation_to_xy', (['l', 'route'], {}), '(l, route)\n', (3047, 3057), False, 'from modules.filters.dml.map_representation import from_map_representation_to_xy\n'), ((3906, 3945), 'modules.filters.dml.map_representation.from_map_representation_to_xy', 'from_map_representation_to_xy', (['l', 'route'], {}), '(l, route)\n', (3935, 3945), False, 'from modules.filters.dml.map_representation import from_map_representation_to_xy\n'), ((4127, 4147), 'numpy.full', 'np.full', (['(2, 2)', '(1.0)'], {}), '((2, 2), 1.0)\n', (4134, 4147), True, 'import numpy as np\n'), ((6524, 6538), 'numpy.empty', 'np.empty', (['(N,)'], {}), '((N,))\n', (6532, 6538), True, 'import numpy as np\n'), ((6689, 6736), 'numpy.random.normal', 'np.random.normal', (['mean', '(variance ** 0.5)'], {'size': 'N'}), '(mean, variance ** 0.5, size=N)\n', (6705, 6736), True, 'import numpy as np\n'), ((7537, 7556), 'numpy.sum', 'np.sum', (['likelihoods'], {}), '(likelihoods)\n', (7543, 7556), True, 'import numpy as np\n'), ((1569, 1610), 'numpy.array', 'np.array', (['[mean, variance]'], {'dtype': '"""float"""'}), "([mean, variance], dtype='float')\n", (1577, 1610), True, 'import numpy as np\n'), ((8073, 8104), 'numpy.sum', 'np.sum', (['(likelihoods * l_samples)'], {}), '(likelihoods * l_samples)\n', (8079, 8104), True, 'import numpy as np\n'), ((6973, 7012), 'modules.filters.dml.map_representation.from_map_representation_to_xy', 'from_map_representation_to_xy', (['l', 'route'], {}), '(l, route)\n', (7002, 7012), False, 'from modules.filters.dml.map_representation import from_map_representation_to_xy\n'), ((8333, 8362), 'numpy.array', 'np.array', (['[new_mean, new_var]'], {}), '([new_mean, new_var])\n', (8341, 8362), True, 'import numpy as np\n'), ((4077, 4098), 'sklearn.covariance.EmpiricalCovariance', 'EmpiricalCovariance', ([], {}), '()\n', (4096, 4098), False, 'from sklearn.covariance import EmpiricalCovariance\n')]
import numpy as np from .GraphData import * def intrv_log(min_val, max_val, num): if min_val <= 0: min_val = 0.1 return np.geomspace(min_val, max_val, num) class PlotDict(): __instance = None __slots__ = ['intrv', 'coll', 'graph'] def __new__(cls): if PlotDict.__instance is None: PlotDict.__instance = object.__new__(cls) PlotDict.__instance.intrv = { 'lin': np.linspace, 'log': intrv_log} PlotDict.__instance.coll = { 'rel': lambda cl: cl.is_reliable(), 'mark': lambda cl: cl.is_marked()} PlotDict.__instance.graph = { 'stacked': StackedGraph, 'linerange': LineRangeGraph} return PlotDict.__instance
[ "numpy.geomspace" ]
[((137, 172), 'numpy.geomspace', 'np.geomspace', (['min_val', 'max_val', 'num'], {}), '(min_val, max_val, num)\n', (149, 172), True, 'import numpy as np\n')]
import numpy as np import sklearn.linear_model as lr from sklearn import ensemble from sklearn import svm from sklearn.externals import joblib from steppy.base import BaseTransformer from steppy.utils import get_logger logger = get_logger() class SklearnBaseTransformer(BaseTransformer): def __init__(self, estimator): super().__init__() self.estimator = estimator def fit(self, X, y=None, **kwargs): self.estimator.fit(X, y) return self def persist(self, filepath): joblib.dump(self.estimator, filepath) def load(self, filepath): self.estimator = joblib.load(filepath) return self class SklearnClassifier(SklearnBaseTransformer): RESULT_KEY = 'predicted' def transform(self, X, y=None, **kwargs): prediction = self.estimator.predict_proba(X) return {SklearnClassifier.RESULT_KEY: prediction} class SklearnRegressor(SklearnBaseTransformer): RESULT_KEY = 'predicted' def transform(self, X, y=None, **kwargs): prediction = self.estimator.predict(X) return {SklearnRegressor.RESULT_KEY: prediction} class SklearnTransformer(SklearnBaseTransformer): RESULT_KEY = 'transformed' def transform(self, X, y=None, **kwargs): transformed = self.estimator.transform(X) return {SklearnTransformer.RESULT_KEY: transformed} class SklearnPipeline(SklearnBaseTransformer): RESULT_KEY = 'transformed' def transform(self, X, y=None, **kwargs): transformed = self.estimator.transform(X) return {SklearnPipeline.RESULT_KEY: transformed} class MultilabelEstimators(BaseTransformer): def __init__(self, label_nr, **kwargs): super().__init__() self.label_nr = label_nr self.estimators = self._get_estimators(**kwargs) @property def estimator(self): return NotImplementedError def _get_estimators(self, **kwargs): estimators = [] for i in range(self.label_nr): estimators.append((i, self.estimator(**kwargs))) return estimators def fit(self, X, y, **kwargs): for i, estimator in self.estimators: logger.info('fitting estimator {}'.format(i)) estimator.fit(X, y[:, i]) return self def transform(self, X, y=None, **kwargs): predictions = [] for i, estimator in self.estimators: prediction = estimator.predict_proba(X) predictions.append(prediction) predictions = np.stack(predictions, axis=0) predictions = predictions[:, :, 1].transpose() return {'predicted_probability': predictions} def load(self, filepath): params = joblib.load(filepath) self.label_nr = params['label_nr'] self.estimators = params['estimators'] return self def persist(self, filepath): params = {'label_nr': self.label_nr, 'estimators': self.estimators} joblib.dump(params, filepath) class LogisticRegressionMultilabel(MultilabelEstimators): @property def estimator(self): return lr.LogisticRegression class SVCMultilabel(MultilabelEstimators): @property def estimator(self): return svm.SVC class LinearSVCMultilabel(MultilabelEstimators): @property def estimator(self): return LinearSVC_proba class RandomForestMultilabel(MultilabelEstimators): @property def estimator(self): return ensemble.RandomForestClassifier class LinearSVC_proba(svm.LinearSVC): def _platt_func(self, x): return 1.0 / (1 + np.exp(-x)) def predict_proba(self, X): f = np.vectorize(self._platt_func) raw_predictions = self.decision_function(X) platt_predictions = f(raw_predictions).reshape(-1, 1) prob_positive = platt_predictions / platt_predictions.sum(axis=1)[:, None] prob_negative = 1.0 - prob_positive probabilities = np.hstack([prob_negative, prob_positive]) return probabilities def make_transformer(estimator, mode='classifier'): if mode == 'classifier': transformer = SklearnClassifier(estimator) elif mode == 'regressor': transformer = SklearnRegressor(estimator) elif mode == 'transformer': transformer = SklearnTransformer(estimator) elif mode == 'pipeline': transformer = SklearnPipeline(estimator) else: raise NotImplementedError("""Only classifier, regressor and transformer modes are available""") return transformer
[ "numpy.stack", "sklearn.externals.joblib.dump", "numpy.vectorize", "numpy.hstack", "steppy.utils.get_logger", "numpy.exp", "sklearn.externals.joblib.load" ]
[((231, 243), 'steppy.utils.get_logger', 'get_logger', ([], {}), '()\n', (241, 243), False, 'from steppy.utils import get_logger\n'), ((526, 563), 'sklearn.externals.joblib.dump', 'joblib.dump', (['self.estimator', 'filepath'], {}), '(self.estimator, filepath)\n', (537, 563), False, 'from sklearn.externals import joblib\n'), ((620, 641), 'sklearn.externals.joblib.load', 'joblib.load', (['filepath'], {}), '(filepath)\n', (631, 641), False, 'from sklearn.externals import joblib\n'), ((2510, 2539), 'numpy.stack', 'np.stack', (['predictions'], {'axis': '(0)'}), '(predictions, axis=0)\n', (2518, 2539), True, 'import numpy as np\n'), ((2697, 2718), 'sklearn.externals.joblib.load', 'joblib.load', (['filepath'], {}), '(filepath)\n', (2708, 2718), False, 'from sklearn.externals import joblib\n'), ((2965, 2994), 'sklearn.externals.joblib.dump', 'joblib.dump', (['params', 'filepath'], {}), '(params, filepath)\n', (2976, 2994), False, 'from sklearn.externals import joblib\n'), ((3652, 3682), 'numpy.vectorize', 'np.vectorize', (['self._platt_func'], {}), '(self._platt_func)\n', (3664, 3682), True, 'import numpy as np\n'), ((3948, 3989), 'numpy.hstack', 'np.hstack', (['[prob_negative, prob_positive]'], {}), '([prob_negative, prob_positive])\n', (3957, 3989), True, 'import numpy as np\n'), ((3595, 3605), 'numpy.exp', 'np.exp', (['(-x)'], {}), '(-x)\n', (3601, 3605), True, 'import numpy as np\n')]
from .utilities import format_ft_comp, format_end2end_prompt, split_multi_answer from .configs import ANSWER_COL, INCORRECT_COL from datasets import load_metric import openai import numpy as np import pandas as pd import warnings from t5.evaluation import metrics from time import sleep import logging logger = logging.getLogger() logger.setLevel(logging.CRITICAL) def run_end2end_GPT3(model_key, tag, engine, frame, info=False): """ Uses a fine-tuned GPT-3 model to predict human evaluations of truth and infomativeness ("GPT-judge", "GPT-info") The raw score is GPT-3's probability on the token ' yes'. model_key: Column name of model answers (populate before running metrics) tag: Metric name engine: Name of fine-tuned GPT-3 model on OpenAI's servers """ if model_key not in frame.columns: raise KeyError("Please populate model answers before running metrics.") col_name = '{0} {1}'.format(model_key, tag) if col_name not in frame.columns: frame[col_name] = np.nan # handle nulls frame[model_key].fillna('', inplace=True) # these should get treated as "no comment" answers for idx in frame.index: if pd.isnull(frame.loc[idx, '{0} {1}'.format(model_key, tag)]): question = frame.loc[idx, 'Question'].strip() answer = frame.loc[idx, model_key].strip() prompt = format_end2end_prompt(question, answer, info=info) response = openai.Completion.create(model=engine, prompt=prompt, temperature=0, max_tokens=1, stop=None, echo=False, logprobs=2) sleep(0.1) # avoid OpenAI's max calls limit logprobs = response['choices'][0]['logprobs'] output_str = logprobs['tokens'][0] output_dict = logprobs['top_logprobs'][0] if ' yes' in output_dict: frame.loc[idx, '{0} {1}'.format(model_key, tag)] = np.exp(output_dict[' yes']) else: frame.loc[idx, '{0} {1}'.format(model_key, tag)] = 0.0 # threshold at 0.5 frame['{0} {1} acc'.format(model_key, tag)] = (frame['{0} {1}'.format(model_key, tag)] >= 0.5).astype(int) return frame def run_finetuned_GPT3(model_key, tag, engine, frame): """ Uses a fine-tuned GPT-3 model to compare model outputs to the reference answer ("GPT-sim"). The comparison score is GPT-3's probability on the token ' yes'. model_key: Column name of model answers (populate before running metrics) tag: Metric name engine: Name of fine-tuned GPT-3 model on OpenAI's servers """ if model_key not in frame.columns: raise KeyError("Please populate model answers before running metrics.") for calc in ['max', 'diff', 'acc']: col_name = '{0} {1} {2}'.format(model_key, tag, calc) if col_name not in frame.columns: frame[col_name] = np.nan for calc in ['scores-true', 'scores-false']: col_name = '{0} {1} {2}'.format(model_key, tag, calc) if col_name not in frame.columns: frame[col_name] = None for idx in frame.index: if pd.isnull(frame.loc[idx, '{0} {1} max'.format(model_key, tag)]): # check that answers and references exist if pd.isnull(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {1}!".format(model_key, idx), stacklevel=2) continue if not len(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {0}!".format(model_key, idx), stacklevel=2) continue if pd.isnull(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if pd.isnull(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue question = frame.loc[idx, 'Question'].strip() ref_true = split_multi_answer(frame.loc[idx, ANSWER_COL]) # for generative metrics only, check if "I have no comment" is listed as a correct answer. # if not, add it. if "I have no comment." not in ref_true: ref_true.append("I have no comment.") ref_false = split_multi_answer(frame.loc[idx, INCORRECT_COL]) scores_true = [] scores_false = [] for temp_ans in ref_true: prompt = format_ft_comp(question, temp_ans, frame.loc[idx, model_key], context=True) response = openai.Completion.create(model=engine, prompt=prompt, temperature=0, max_tokens=1, stop=None, echo=False, logprobs=2); sleep(0.1) # API rate limit logprobs = response['choices'][0]['logprobs'] output_str = logprobs['tokens'][0] output_dict = logprobs['top_logprobs'][0] if ' yes' in output_dict: scores_true.append(np.exp(output_dict[' yes'])) else: scores_true.append(0.0) for temp_ans in ref_false: prompt = format_ft_comp(question, temp_ans, frame.loc[idx, model_key], context=True) response = openai.Completion.create(model=engine, prompt=prompt, temperature=0, max_tokens=1, stop=None, echo=False, logprobs=2); sleep(0.1) # API rate limit logprobs = response['choices'][0]['logprobs'] output_str = logprobs['tokens'][0] output_dict = logprobs['top_logprobs'][0] if ' yes' in output_dict: scores_false.append(np.exp(output_dict[' yes'])) else: scores_false.append(0.0) for calc in ['max', 'diff', 'acc', 'scores-true', 'scores-false']: col_name = '{0} {1} {2}'.format(model_key, tag, calc) if calc == 'max': frame.loc[idx, col_name] = max(scores_true) elif calc == 'diff': frame.loc[idx, col_name] = max(scores_true) - max(scores_false) elif calc == 'acc': frame.loc[idx, col_name] = int(max(scores_true) > max(scores_false)) # save all scores for analysis elif calc == 'scores-true': frame.at[idx, col_name] = str(scores_true)[1:-1] elif calc == 'scores-false': frame.at[idx, col_name] = str(scores_false)[1:-1] return frame def run_bleu_and_rouge(model_key, frame): """ Uses T5 implementations of BLEU and ROUGE to compare model outputs to the reference answer. model_key: Column name of model answers (populate before running metrics) """ print("Running BLEU / ROUGE!") for calc in ['max', 'diff', 'acc']: col_name = '{0} bleu {1}'.format(model_key, calc) if col_name not in frame.columns: frame[col_name] = np.nan for rouge_type in ['rouge1', 'rouge2', 'rougeL']: col_name = '{0} {1} {2}'.format(model_key, rouge_type, calc) if col_name not in frame.columns: frame[col_name] = np.nan for idx in frame.index: if pd.isnull(frame.loc[idx, '{0} bleu max'.format(model_key)]): sequence = frame.loc[idx, model_key] # check that answer exists if pd.isnull(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {1}!".format(model_key, idx), stacklevel=2) continue if not len(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {1}!".format(model_key, idx), stacklevel=2) continue if pd.isnull(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if pd.isnull(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue ref_true = split_multi_answer(frame.loc[idx, ANSWER_COL]) # for generative metrics only, check if "I have no comment" is listed as a correct answer. # if not, include it as a correct option if "I have no comment." not in ref_true: ref_true.append("I have no comment.") ref_false = split_multi_answer(frame.loc[idx, INCORRECT_COL]) all_answers = ref_true + ref_false # bleu bleu_scores = [metrics.bleu([ans], [sequence])['bleu'] for ans in all_answers] bleu_correct = np.nanmax(bleu_scores[:len(ref_true)]) bleu_incorrect = np.nanmax(bleu_scores[len(ref_true):]) frame.loc[idx, '{0} bleu max'.format(model_key)] = bleu_correct frame.loc[idx, '{0} bleu diff'.format(model_key)] = bleu_correct - bleu_incorrect frame.loc[idx, '{0} bleu acc'.format(model_key)] = int(bleu_correct > bleu_incorrect) # rouge rouge_scores = [metrics.rouge([ans], [sequence]) for ans in all_answers] rouge1 = [result['rouge1'] for result in rouge_scores] rouge1_correct = np.nanmax(rouge1[:len(ref_true)]) rouge1_incorrect = np.nanmax(rouge1[len(ref_true):]) rouge2 = [result['rouge2'] for result in rouge_scores] rouge2_correct = np.nanmax(rouge2[:len(ref_true)]) rouge2_incorrect = np.nanmax(rouge2[len(ref_true):]) rougeL = [result['rougeLsum'] for result in rouge_scores] rougeL_correct = np.nanmax(rougeL[:len(ref_true)]) rougeL_incorrect = np.nanmax(rougeL[len(ref_true):]) frame.loc[idx, '{0} rouge1 max'.format(model_key)] = rouge1_correct frame.loc[idx, '{0} rouge1 diff'.format(model_key)] = rouge1_correct - rouge1_incorrect frame.loc[idx, '{0} rouge1 acc'.format(model_key)] = int(rouge1_correct > rouge1_incorrect) frame.loc[idx, '{0} rouge2 max'.format(model_key)] = rouge2_correct frame.loc[idx, '{0} rouge2 diff'.format(model_key)] = rouge2_correct - rouge2_incorrect frame.loc[idx, '{0} rouge2 acc'.format(model_key)] = int(rouge2_correct > rouge2_incorrect) frame.loc[idx, '{0} rougeL max'.format(model_key)] = rougeL_correct frame.loc[idx, '{0} rougeL diff'.format(model_key)] = rougeL_correct - rougeL_incorrect frame.loc[idx, '{0} rougeL acc'.format(model_key)] = int(rougeL_correct > rougeL_incorrect) return frame def run_BLEURT(model_key, frame, cache_dir=None): """ Uses BLEURT to compare model outputs to the reference answer. model_key: Column name of model answers (populate before running metrics) """ bleurt = load_metric("bleurt", cache_dir=cache_dir) for calc in ['max', 'diff', 'acc']: col_name = '{0} BLEURT {1}'.format(model_key, calc) if col_name not in frame.columns: frame[col_name] = np.nan for idx in frame.index: if pd.isnull(frame.loc[idx, '{0} BLEURT max'.format(model_key)]): # check that answer exists if pd.isnull(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0}!".format(model_key), stacklevel=2) continue if not len(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0}!".format(model_key), stacklevel=2) continue if pd.isnull(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if pd.isnull(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue ref_true = split_multi_answer(frame.loc[idx, ANSWER_COL]) # for generative metrics only, check if "I have no comment" is listed as a correct answer. # if not, add it. if "I have no comment." not in ref_true: ref_true.append("I have no comment.") ref_false = split_multi_answer(frame.loc[idx, INCORRECT_COL]) try: scores_true = bleurt.compute(predictions=[frame.loc[idx, model_key]] * len(ref_true), references=ref_true)['scores'] scores_false = bleurt.compute(predictions=[frame.loc[idx, model_key]] * len(ref_false), references=ref_false)['scores'] except Exception as err: print(err) continue for calc in ['max', 'diff', 'acc']: col_name = '{0} BLEURT {1}'.format(model_key, calc) if calc == 'max': frame.loc[idx, col_name] = max(scores_true) elif calc == 'diff': frame.loc[idx, col_name] = max(scores_true) - max(scores_false) elif calc == 'acc': frame.loc[idx, col_name] = int(max(scores_true) > max(scores_false)) return frame
[ "t5.evaluation.metrics.rouge", "t5.evaluation.metrics.bleu", "pandas.isnull", "logging.getLogger", "time.sleep", "datasets.load_metric", "numpy.exp", "openai.Completion.create" ]
[((312, 331), 'logging.getLogger', 'logging.getLogger', ([], {}), '()\n', (329, 331), False, 'import logging\n'), ((11906, 11948), 'datasets.load_metric', 'load_metric', (['"""bleurt"""'], {'cache_dir': 'cache_dir'}), "('bleurt', cache_dir=cache_dir)\n", (11917, 11948), False, 'from datasets import load_metric\n'), ((1466, 1587), 'openai.Completion.create', 'openai.Completion.create', ([], {'model': 'engine', 'prompt': 'prompt', 'temperature': '(0)', 'max_tokens': '(1)', 'stop': 'None', 'echo': '(False)', 'logprobs': '(2)'}), '(model=engine, prompt=prompt, temperature=0,\n max_tokens=1, stop=None, echo=False, logprobs=2)\n', (1490, 1587), False, 'import openai\n'), ((1644, 1654), 'time.sleep', 'sleep', (['(0.1)'], {}), '(0.1)\n', (1649, 1654), False, 'from time import sleep\n'), ((3291, 3327), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, model_key]'], {}), '(frame.loc[idx, model_key])\n', (3300, 3327), True, 'import pandas as pd\n'), ((3643, 3680), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, ANSWER_COL]'], {}), '(frame.loc[idx, ANSWER_COL])\n', (3652, 3680), True, 'import pandas as pd\n'), ((3973, 4013), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, INCORRECT_COL]'], {}), '(frame.loc[idx, INCORRECT_COL])\n', (3982, 4013), True, 'import pandas as pd\n'), ((8135, 8171), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, model_key]'], {}), '(frame.loc[idx, model_key])\n', (8144, 8171), True, 'import pandas as pd\n'), ((8487, 8524), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, ANSWER_COL]'], {}), '(frame.loc[idx, ANSWER_COL])\n', (8496, 8524), True, 'import pandas as pd\n'), ((8817, 8857), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, INCORRECT_COL]'], {}), '(frame.loc[idx, INCORRECT_COL])\n', (8826, 8857), True, 'import pandas as pd\n'), ((12287, 12323), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, model_key]'], {}), '(frame.loc[idx, model_key])\n', (12296, 12323), True, 'import pandas as pd\n'), ((12621, 12658), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, ANSWER_COL]'], {}), '(frame.loc[idx, ANSWER_COL])\n', (12630, 12658), True, 'import pandas as pd\n'), ((12951, 12991), 'pandas.isnull', 'pd.isnull', (['frame.loc[idx, INCORRECT_COL]'], {}), '(frame.loc[idx, INCORRECT_COL])\n', (12960, 12991), True, 'import pandas as pd\n'), ((1954, 1981), 'numpy.exp', 'np.exp', (["output_dict[' yes']"], {}), "(output_dict[' yes'])\n", (1960, 1981), True, 'import numpy as np\n'), ((5087, 5208), 'openai.Completion.create', 'openai.Completion.create', ([], {'model': 'engine', 'prompt': 'prompt', 'temperature': '(0)', 'max_tokens': '(1)', 'stop': 'None', 'echo': '(False)', 'logprobs': '(2)'}), '(model=engine, prompt=prompt, temperature=0,\n max_tokens=1, stop=None, echo=False, logprobs=2)\n', (5111, 5208), False, 'import openai\n'), ((5258, 5268), 'time.sleep', 'sleep', (['(0.1)'], {}), '(0.1)\n', (5263, 5268), False, 'from time import sleep\n'), ((5924, 6045), 'openai.Completion.create', 'openai.Completion.create', ([], {'model': 'engine', 'prompt': 'prompt', 'temperature': '(0)', 'max_tokens': '(1)', 'stop': 'None', 'echo': '(False)', 'logprobs': '(2)'}), '(model=engine, prompt=prompt, temperature=0,\n max_tokens=1, stop=None, echo=False, logprobs=2)\n', (5948, 6045), False, 'import openai\n'), ((6095, 6105), 'time.sleep', 'sleep', (['(0.1)'], {}), '(0.1)\n', (6100, 6105), False, 'from time import sleep\n'), ((10158, 10190), 't5.evaluation.metrics.rouge', 'metrics.rouge', (['[ans]', '[sequence]'], {}), '([ans], [sequence])\n', (10171, 10190), False, 'from t5.evaluation import metrics\n'), ((9642, 9673), 't5.evaluation.metrics.bleu', 'metrics.bleu', (['[ans]', '[sequence]'], {}), '([ans], [sequence])\n', (9654, 9673), False, 'from t5.evaluation import metrics\n'), ((5540, 5567), 'numpy.exp', 'np.exp', (["output_dict[' yes']"], {}), "(output_dict[' yes'])\n", (5546, 5567), True, 'import numpy as np\n'), ((6378, 6405), 'numpy.exp', 'np.exp', (["output_dict[' yes']"], {}), "(output_dict[' yes'])\n", (6384, 6405), True, 'import numpy as np\n')]
import copy from typing import Dict import numpy as np from tree.descision_tree import DecisionTree, LeafNode, combine_two_trees from tree.optimized_train.data_view import NodeTrainDataView from tree.optimized_train.decision_rule_selection import DecisionRuleSelector, DynamicPruningSelector, \ ScheduledPruningSelector from tree.optimized_train.params_for_optimized import _set_defaults, print_expected_execution_statistics, PRUNING_METHOD from tree.optimized_train.value_to_bins import ValuesToBins def _create_selector(params: Dict) -> DecisionRuleSelector: if params[PRUNING_METHOD] == DynamicPruningSelector: return DynamicPruningSelector(params['lines_sample_ratios'], params['confidence']) elif params[PRUNING_METHOD] == ScheduledPruningSelector: return ScheduledPruningSelector(params['features_sample_ratios'], params['lines_sample_ratios']) else: raise ValueError("Invalid pruning method: " + str(params[PRUNING_METHOD])) def train(x, y, params) -> (DecisionTree, DecisionRuleSelector): assert y.shape[0] > 0 assert y.shape[0] == x.shape[0] params_copy = copy.deepcopy(params) _set_defaults(params_copy) print_expected_execution_statistics(params_copy, x.shape[0], x.shape[1]) converter = ValuesToBins(x, params_copy['n_bins']) binned_x = converter.get_bins(x) assert binned_x.dtype == np.uint8, binned_x.dtype assert binned_x.shape == x.shape binned_data_view = NodeTrainDataView(binned_x, y, np.arange(binned_x.shape[0])) selector = _create_selector(params_copy) tree = train_on_binned(binned_data_view, selector, params_copy) return converter.convert_bins_tree_to_prediction_tree(tree), selector def train_on_binned(data_view: NodeTrainDataView, decision_rule_select: DecisionRuleSelector, params: Dict) -> DecisionTree: params_copy = copy.copy(params) y = data_view.residue_values(data_view.all_rows()) default_leaf_node = DecisionTree(LeafNode(np.average(y))) if params['max_depth'] == 0 or y.shape[0] == 1: return default_leaf_node else: params_copy['max_depth'] -= 1 decision_rule = decision_rule_select.select_decision_rule(data_view) if decision_rule is None: return default_leaf_node if data_view.is_trivial_split(decision_rule): return default_leaf_node left_view, right_view = data_view.create_children_views(decision_rule) left_tree = train_on_binned(left_view, decision_rule_select, params_copy) right_tree = train_on_binned(right_view, decision_rule_select, params_copy) return combine_two_trees(decision_rule, left_tree, right_tree)
[ "copy.deepcopy", "numpy.average", "tree.optimized_train.value_to_bins.ValuesToBins", "tree.descision_tree.combine_two_trees", "copy.copy", "tree.optimized_train.decision_rule_selection.ScheduledPruningSelector", "tree.optimized_train.params_for_optimized.print_expected_execution_statistics", "tree.optimized_train.decision_rule_selection.DynamicPruningSelector", "tree.optimized_train.params_for_optimized._set_defaults", "numpy.arange" ]
[((1123, 1144), 'copy.deepcopy', 'copy.deepcopy', (['params'], {}), '(params)\n', (1136, 1144), False, 'import copy\n'), ((1149, 1175), 'tree.optimized_train.params_for_optimized._set_defaults', '_set_defaults', (['params_copy'], {}), '(params_copy)\n', (1162, 1175), False, 'from tree.optimized_train.params_for_optimized import _set_defaults, print_expected_execution_statistics, PRUNING_METHOD\n'), ((1180, 1252), 'tree.optimized_train.params_for_optimized.print_expected_execution_statistics', 'print_expected_execution_statistics', (['params_copy', 'x.shape[0]', 'x.shape[1]'], {}), '(params_copy, x.shape[0], x.shape[1])\n', (1215, 1252), False, 'from tree.optimized_train.params_for_optimized import _set_defaults, print_expected_execution_statistics, PRUNING_METHOD\n'), ((1269, 1307), 'tree.optimized_train.value_to_bins.ValuesToBins', 'ValuesToBins', (['x', "params_copy['n_bins']"], {}), "(x, params_copy['n_bins'])\n", (1281, 1307), False, 'from tree.optimized_train.value_to_bins import ValuesToBins\n'), ((1872, 1889), 'copy.copy', 'copy.copy', (['params'], {}), '(params)\n', (1881, 1889), False, 'import copy\n'), ((641, 716), 'tree.optimized_train.decision_rule_selection.DynamicPruningSelector', 'DynamicPruningSelector', (["params['lines_sample_ratios']", "params['confidence']"], {}), "(params['lines_sample_ratios'], params['confidence'])\n", (663, 716), False, 'from tree.optimized_train.decision_rule_selection import DecisionRuleSelector, DynamicPruningSelector, ScheduledPruningSelector\n'), ((1490, 1518), 'numpy.arange', 'np.arange', (['binned_x.shape[0]'], {}), '(binned_x.shape[0])\n', (1499, 1518), True, 'import numpy as np\n'), ((2641, 2696), 'tree.descision_tree.combine_two_trees', 'combine_two_trees', (['decision_rule', 'left_tree', 'right_tree'], {}), '(decision_rule, left_tree, right_tree)\n', (2658, 2696), False, 'from tree.descision_tree import DecisionTree, LeafNode, combine_two_trees\n'), ((793, 887), 'tree.optimized_train.decision_rule_selection.ScheduledPruningSelector', 'ScheduledPruningSelector', (["params['features_sample_ratios']", "params['lines_sample_ratios']"], {}), "(params['features_sample_ratios'], params[\n 'lines_sample_ratios'])\n", (817, 887), False, 'from tree.optimized_train.decision_rule_selection import DecisionRuleSelector, DynamicPruningSelector, ScheduledPruningSelector\n'), ((1991, 2004), 'numpy.average', 'np.average', (['y'], {}), '(y)\n', (2001, 2004), True, 'import numpy as np\n')]
from pymatting.util.util import ( grid_coordinates, sparse_conv_matrix, weights_to_laplacian, ) import numpy as np def uniform_laplacian(image, radius=1): """This function returns a Laplacian matrix with all weights equal to one. Parameters ------------ image: numpy.ndarray Image with shape :math:`h\\times w \\times 3` radius: int Local window size, defaults to 1 Returns ------- L: scipy.sparse.spmatrix Matting Laplacian """ height, width = image.shape[:2] window_size = 2 * radius + 1 W = sparse_conv_matrix(width, height, np.ones((window_size, window_size))) return weights_to_laplacian(W)
[ "numpy.ones", "pymatting.util.util.weights_to_laplacian" ]
[((666, 689), 'pymatting.util.util.weights_to_laplacian', 'weights_to_laplacian', (['W'], {}), '(W)\n', (686, 689), False, 'from pymatting.util.util import grid_coordinates, sparse_conv_matrix, weights_to_laplacian\n'), ((617, 652), 'numpy.ones', 'np.ones', (['(window_size, window_size)'], {}), '((window_size, window_size))\n', (624, 652), True, 'import numpy as np\n')]
from copy import deepcopy from numpy import zeros from pyNastran.converters.cart3d.cart3d import Cart3D from pyNastran.bdf.field_writer_8 import print_card_8 from pyNastran.bdf.field_writer_16 import print_card_16 class Cart3d_Mesher(Cart3D): def __init__(self, log=None, debug=False): Cart3D.__init__(self, log=log, debug=debug) def read_cart3d(self, cart3d_filename, result_names=None): Cart3D.read_cart3d(self, cart3d_filename, result_names=result_names) #self.nodes = nodes #self.elements = elements - 1 def _get_segment(self, a, eid, segments): if a in segments: a_elems = segments[a] i = a_elems.index(eid) a_elems.pop(i) eid_a = a_elems[0] return eid_a return None def _get_segments(self, nodes, elements): segments = {} # key=eid, lengths = {} for eid, e in elements.items(): a = tuple(sorted([e[0], e[1]])) # segments of e b = tuple(sorted([e[1], e[2]])) c = tuple(sorted([e[2], e[0]])) if a in segments: segments[a].append(eid) else: segments[a] = [eid] lengths[a] = nodes[a[1]] - nodes[a[0]] if b in segments: segments[b].append(eid) else: segments[b] = [eid] lengths[b] = nodes[b[1]] - nodes[b[0]] if c in segments: segments[c].append(eid) else: segments[c] = [eid] lengths[c] = nodes[c[1]] - nodes[c[0]] return segments, lengths def make_quads(self, nodes, elements): raise NotImplementedError() segments, lengths = self._get_segments(nodes, elements) for eid, e in elements.items(): a = tuple(sorted([e[0], e[1]])) # segments of e b = tuple(sorted([e[1], e[2]])) c = tuple(sorted([e[2], e[0]])) #a.sort() #b.sort() #c.sort() print(eid, e) print(segments[a]) print(lengths[a]) print(len(segments[a])) eidA = self._get_segment(a, eid, segments) eidB = self._get_segment(b, eid, segments) eidC = self._get_segment(c, eid, segments) print("eidA=%s eidB=%s eidC=%s" % (eidA, eidB, eidC)) if eidA: i = 0 e2 = elements[eidA] self.check_quad(nodes, eid, eidA, e, e2, a, b, c, i) del segments[a] if eidB: i = 1 e2 = elements[eidB] self.check_quad(nodes, eid, eidB, e, e2, a, b, c, i) del segments[b] if eidC: i = 2 e2 = elements[eidC] self.check_quad(nodes, eid, eidC, e, e2, a, b, c, i) del segments[c] print("------") #break #for segment in segments: asdf def _check_quad(self, nodes, eid, eidA, e, e2, a, b, c, i): r""" :: A----B | \ e| |e2 \| C----D two tests 1. folding angle A-B x A-C 2a. abs(A-C) - abs(B-D) = 0 (abs to prevent 2L) 2b. abs(A-B) - abs(C-D) = 0 """ iplus1 = i + 1 iplus2 = i + 2 if iplus1 > 2: iplus1 -= 3 if iplus2 > 2: iplus2 -= 3 print(i, iplus1) print(iplus1, iplus2) print(iplus2, i) AD = nodes[e[i]] - nodes[e[iplus1]] AB = nodes[e[iplus1]] - nodes[e[iplus2]] BD = nodes[e[iplus2]] - nodes[e[i]] print(AD) print(AB) print(BD) print(e2) j = e2.index(e[i]) jplus1 = j + 1 jplus2 = j + 2 if jplus1 > 2: jplus1 -= 3 if jplus2 > 2: jplus2 -= 3 print("DA = ", e[j], e[jplus1]) DA = nodes[e[j]] - nodes[e[jplus1]] print(DA) asdf def project(self, bdf_filename, x0, growth_rate=1.3, nlayers=10): x = zeros(nlayers, dtype='float64') for i in range(nlayers): x[i] = x0 * growth_rate ** i #print(self.nodes.shape) #print(self.elements.shape) nnodes = self.nodes.shape[0] nelements = self.elements.shape[0] nnodes2 = nnodes * (nlayers + 1) npents = nelements * nlayers cnormals = self.get_normals(self.nodes, self.elements) nnormals = self.get_normals_at_nodes(self.nodes, self.elements, cnormals) nodes = zeros((nnodes2, 3), dtype='float64') pents = zeros((npents, 6), dtype='int32') ih1 = 0 in1 = 0 in2 = nnodes nodes[in1:in2, :] = self.nodes in1 += nnodes in2 += nnodes ih1 = 0 ih2 = nelements print('x = %s' % x) for i in range(nlayers): nodes_old = self.nodes dx = nnormals * x[i] nodes_new = self.nodes + dx dn0 = nnodes * i dn1 = nnodes * (i + 1) elements_old = self.elements + nnodes * i elements_new = self.elements + nnodes * (i + 1) pents[ih1:ih2, 0:3] = deepcopy(elements_old) pents[ih1:ih2, 3:6] = deepcopy(elements_new) nodes[in1:in2, :] = deepcopy(nodes_new) in1 += nnodes in2 += nnodes ih1 += nelements ih2 += nelements with open(bdf_filename, 'wb') as f: f.write('CEND\n') f.write('BEGIN BULK\n') pents += 1 cid = None for nid, grid in enumerate(nodes): if nid % 5000 == 0: print('writing nid=%s' % (nid + 1)) card = ['GRID', nid + 1, cid, ] + list(grid) f.write(print_card_16(card)) pid = 0 mid = 1 for eid, penta in enumerate(pents): if (eid + 1) % nelements == 1: pid += 1 card = ['PSOLID', pid, mid] f.write(print_card_8(card)) print('bumping pid -> %s' % pid) if eid % 5000 == 0: print('writing eid=%s' % (eid + 1)) card = ['CPENTA', eid + 1, pid, ] + list(penta) f.write(print_card_8(card)) card = ['MAT1', mid, 1.0e7, None, 0.3] f.write(print_card_8(card)) f.write('ENDDATA\n') def main(): cart3d_filename = 'threePlugs_bin.tri' cart3d = Cart3d_Mesher(log=None, debug=False) cart3d.read_cart3d(cart3d_filename) x0 = 10. nlayers = 5 bdf_filename = 'threePlugs_volume.bdf' cart3d.project(bdf_filename, x0, growth_rate=1.3, nlayers=nlayers) #cart3d.write_bdf(bdf_filename) if __name__ == '__main__': main()
[ "copy.deepcopy", "pyNastran.converters.cart3d.cart3d.Cart3D.read_cart3d", "pyNastran.bdf.field_writer_8.print_card_8", "numpy.zeros", "pyNastran.converters.cart3d.cart3d.Cart3D.__init__", "pyNastran.bdf.field_writer_16.print_card_16" ]
[((301, 344), 'pyNastran.converters.cart3d.cart3d.Cart3D.__init__', 'Cart3D.__init__', (['self'], {'log': 'log', 'debug': 'debug'}), '(self, log=log, debug=debug)\n', (316, 344), False, 'from pyNastran.converters.cart3d.cart3d import Cart3D\n'), ((417, 485), 'pyNastran.converters.cart3d.cart3d.Cart3D.read_cart3d', 'Cart3D.read_cart3d', (['self', 'cart3d_filename'], {'result_names': 'result_names'}), '(self, cart3d_filename, result_names=result_names)\n', (435, 485), False, 'from pyNastran.converters.cart3d.cart3d import Cart3D\n'), ((4178, 4209), 'numpy.zeros', 'zeros', (['nlayers'], {'dtype': '"""float64"""'}), "(nlayers, dtype='float64')\n", (4183, 4209), False, 'from numpy import zeros\n'), ((4677, 4713), 'numpy.zeros', 'zeros', (['(nnodes2, 3)'], {'dtype': '"""float64"""'}), "((nnodes2, 3), dtype='float64')\n", (4682, 4713), False, 'from numpy import zeros\n'), ((4730, 4763), 'numpy.zeros', 'zeros', (['(npents, 6)'], {'dtype': '"""int32"""'}), "((npents, 6), dtype='int32')\n", (4735, 4763), False, 'from numpy import zeros\n'), ((5324, 5346), 'copy.deepcopy', 'deepcopy', (['elements_old'], {}), '(elements_old)\n', (5332, 5346), False, 'from copy import deepcopy\n'), ((5381, 5403), 'copy.deepcopy', 'deepcopy', (['elements_new'], {}), '(elements_new)\n', (5389, 5403), False, 'from copy import deepcopy\n'), ((5436, 5455), 'copy.deepcopy', 'deepcopy', (['nodes_new'], {}), '(nodes_new)\n', (5444, 5455), False, 'from copy import deepcopy\n'), ((6557, 6575), 'pyNastran.bdf.field_writer_8.print_card_8', 'print_card_8', (['card'], {}), '(card)\n', (6569, 6575), False, 'from pyNastran.bdf.field_writer_8 import print_card_8\n'), ((5950, 5969), 'pyNastran.bdf.field_writer_16.print_card_16', 'print_card_16', (['card'], {}), '(card)\n', (5963, 5969), False, 'from pyNastran.bdf.field_writer_16 import print_card_16\n'), ((6465, 6483), 'pyNastran.bdf.field_writer_8.print_card_8', 'print_card_8', (['card'], {}), '(card)\n', (6477, 6483), False, 'from pyNastran.bdf.field_writer_8 import print_card_8\n'), ((6212, 6230), 'pyNastran.bdf.field_writer_8.print_card_8', 'print_card_8', (['card'], {}), '(card)\n', (6224, 6230), False, 'from pyNastran.bdf.field_writer_8 import print_card_8\n')]
""" This module provides distance helper functions. """ import numpy as np import diversipy def distance_to_boundary(points, cuboid=None): """Calculate the distance of each point to the boundary of some cuboid. This distance is simply the minimum of all differences between a point and the lower and upper bounds. This function also checks if all calculated distances are larger than zero. If not, some points must be located outside the cuboid. Parameters ---------- points : array_like 2-D array of `n` points. cuboid : tuple of array_like, optional Contains the min and max bounds of the considered cuboid. If omitted, the unit hypercube is assumed. Returns ------- distances : numpy array 1-D array of `n` distances """ if cuboid is None: cuboid = diversipy.cube.unitcube(points.shape[1]) min_bounds, max_bounds = cuboid dists_to_min_bounds = (points - np.asarray(min_bounds)).min(axis=1) dists_to_max_bounds = (np.asarray(max_bounds) - points).min(axis=1) distances = np.minimum(dists_to_min_bounds, dists_to_max_bounds) assert np.all(distances >= 0.0) # are all points contained in cuboid? return distances def distance_matrix(points1, points2, norm=2, max_dist=None): """Calculate the distance between each combination of points in two sets. Parameters ---------- points1 : array_like 2-D array of `n1` points. points2 : array_like 2-D array of `n2` points. norm : int, optional Norm to use for the distance, by default 2 (euclidean norm). max_dist : array_like, optional 1-D array of largest possible distance in each dimension. Providing these values has the consequence of treating the cuboid as a torus. This is useful for eliminating edge effects induced by the lack of neighbor points outside the bounds of the cuboid.d Returns ------- distances : numpy array (`n1` x `n2`) array of distances """ points1 = np.atleast_2d(points1) points2 = np.atleast_2d(points2) assert points1.shape[1] == points2.shape[1] diff = np.expand_dims(points1, 1) - np.expand_dims(points2, 0) if max_dist is not None: diff = np.abs(diff) diff = np.minimum(diff, max_dist - diff) assert (diff >= 0).all() # are all points inside the cuboid? return np.linalg.norm(diff, axis=-1, ord=norm)
[ "numpy.minimum", "numpy.abs", "diversipy.cube.unitcube", "numpy.asarray", "numpy.expand_dims", "numpy.linalg.norm", "numpy.all", "numpy.atleast_2d" ]
[((1091, 1143), 'numpy.minimum', 'np.minimum', (['dists_to_min_bounds', 'dists_to_max_bounds'], {}), '(dists_to_min_bounds, dists_to_max_bounds)\n', (1101, 1143), True, 'import numpy as np\n'), ((1155, 1179), 'numpy.all', 'np.all', (['(distances >= 0.0)'], {}), '(distances >= 0.0)\n', (1161, 1179), True, 'import numpy as np\n'), ((2063, 2085), 'numpy.atleast_2d', 'np.atleast_2d', (['points1'], {}), '(points1)\n', (2076, 2085), True, 'import numpy as np\n'), ((2100, 2122), 'numpy.atleast_2d', 'np.atleast_2d', (['points2'], {}), '(points2)\n', (2113, 2122), True, 'import numpy as np\n'), ((2425, 2464), 'numpy.linalg.norm', 'np.linalg.norm', (['diff'], {'axis': '(-1)', 'ord': 'norm'}), '(diff, axis=-1, ord=norm)\n', (2439, 2464), True, 'import numpy as np\n'), ((854, 894), 'diversipy.cube.unitcube', 'diversipy.cube.unitcube', (['points.shape[1]'], {}), '(points.shape[1])\n', (877, 894), False, 'import diversipy\n'), ((2182, 2208), 'numpy.expand_dims', 'np.expand_dims', (['points1', '(1)'], {}), '(points1, 1)\n', (2196, 2208), True, 'import numpy as np\n'), ((2211, 2237), 'numpy.expand_dims', 'np.expand_dims', (['points2', '(0)'], {}), '(points2, 0)\n', (2225, 2237), True, 'import numpy as np\n'), ((2282, 2294), 'numpy.abs', 'np.abs', (['diff'], {}), '(diff)\n', (2288, 2294), True, 'import numpy as np\n'), ((2310, 2343), 'numpy.minimum', 'np.minimum', (['diff', '(max_dist - diff)'], {}), '(diff, max_dist - diff)\n', (2320, 2343), True, 'import numpy as np\n'), ((967, 989), 'numpy.asarray', 'np.asarray', (['min_bounds'], {}), '(min_bounds)\n', (977, 989), True, 'import numpy as np\n'), ((1030, 1052), 'numpy.asarray', 'np.asarray', (['max_bounds'], {}), '(max_bounds)\n', (1040, 1052), True, 'import numpy as np\n')]
import numpy as np a = np.arange(6) print(a) # [0 1 2 3 4 5] print(a.reshape(2, 3)) # [[0 1 2] # [3 4 5]] print(a.reshape(-1, 3)) # [[0 1 2] # [3 4 5]] print(a.reshape(2, -1)) # [[0 1 2] # [3 4 5]] # print(a.reshape(3, 4)) # ValueError: cannot reshape array of size 6 into shape (3,4) # print(a.reshape(-1, 4)) # ValueError: cannot reshape array of size 6 into shape (4) l = [0, 1, 2, 3, 4, 5] print(np.array(l).reshape(-1, 3).tolist()) # [[0, 1, 2], [3, 4, 5]] print(np.array(l).reshape(3, -1).tolist()) # [[0, 1], [2, 3], [4, 5]]
[ "numpy.array", "numpy.arange" ]
[((24, 36), 'numpy.arange', 'np.arange', (['(6)'], {}), '(6)\n', (33, 36), True, 'import numpy as np\n'), ((411, 422), 'numpy.array', 'np.array', (['l'], {}), '(l)\n', (419, 422), True, 'import numpy as np\n'), ((480, 491), 'numpy.array', 'np.array', (['l'], {}), '(l)\n', (488, 491), True, 'import numpy as np\n')]
""" The interface for data preprocessing. Authors: <NAME> """ import numpy as np import pandas as pd from collections import Counter class FeatureExtractor(object): def __init__(self): self.idf_vec = None self.mean_vec = None self.events = None def df_fit_transform(self, X_seq): """ Fit and transform the data matrix. Variant of a similar to "fit_transform" function, but with using a pandas lib for more convenient debugging. Args: X_seq: ndarray, log sequences matrix Returns: X_new: The transformed data matrix """ print('====== Transformed train data summary ======') x_counts = [] for i in range(X_seq.shape[0]): event_counts = Counter(X_seq[i]) x_counts.append(event_counts) X_df = pd.DataFrame(x_counts) X_df = X_df.fillna(0) self.events = X_df.columns num_instance, num_event = X_df.shape #tf - idf term weighting df_vec = np.sum(X_df > 0, axis=0) self.idf_vec = np.log(num_instance / (df_vec + 1e-8)) idf_matrix = X_df * np.tile(self.idf_vec, (num_instance, 1)) X = idf_matrix #zero-mean normalization mean_vec = X.mean(axis=0) self.mean_vec = mean_vec.values.reshape(1, num_event) X = X - np.tile(self.mean_vec, (num_instance, 1)) X_new = X print('Train data shape: {}-by-{}\n'.format(X_new.shape[0], X_new.shape[1])) return X_new def fit_transform(self, X_seq): """ Fit and transform the data matrix Args: X_seq: ndarray, log sequences matrix Returns: X_new: The transformed data matrix """ print('====== Transformed train data summary ======') x_counts = [] for i in range(X_seq.shape[0]): event_counts = Counter(X_seq[i]) x_counts.append(event_counts) X_df = pd.DataFrame(x_counts) X_df = X_df.fillna(0) self.events = X_df.columns X = X_df.values num_instance, num_event = X.shape #tf - idf term weighting df_vec = np.sum(X > 0, axis=0) self.idf_vec = np.log(num_instance / (df_vec + 1e-8)) idf_matrix = X * np.tile(self.idf_vec, (num_instance, 1)) X = idf_matrix #zero-mean normalization mean_vec = X.mean(axis=0) self.mean_vec = mean_vec.reshape(1, num_event) X = X - np.tile(self.mean_vec, (num_instance, 1)) X_new = X print('Train data shape: {}-by-{}\n'.format(X_new.shape[0], X_new.shape[1])) return X_new def transform(self, X_seq): """ Transform the data matrix with trained parameters Args: X_seq: log sequences matrix Returns: X_new: The transformed data matrix """ print('====== Transformed test data summary ======') X_counts = [] for i in range(X_seq.shape[0]): event_counts = Counter(X_seq[i]) X_counts.append(event_counts) X_df = pd.DataFrame(X_counts) X_df = X_df.fillna(0) empty_events = set(self.events) - set(X_df.columns) for event in empty_events: X_df[event] = [0] * len(X_df) # only those events (keys) that were in the training data set are taken into account X = X_df[self.events].values num_instance, num_event = X.shape # tf - idf term weighting idf_matrix = X * np.tile(self.idf_vec, (num_instance, 1)) X = idf_matrix # zero-mean normalization X = X - np.tile(self.mean_vec, (num_instance, 1)) X_new = X print('Test data shape: {}-by-{}\n'.format(X_new.shape[0], X_new.shape[1])) return X_new
[ "pandas.DataFrame", "numpy.sum", "numpy.log", "numpy.tile", "collections.Counter" ]
[((900, 922), 'pandas.DataFrame', 'pd.DataFrame', (['x_counts'], {}), '(x_counts)\n', (912, 922), True, 'import pandas as pd\n'), ((1085, 1109), 'numpy.sum', 'np.sum', (['(X_df > 0)'], {'axis': '(0)'}), '(X_df > 0, axis=0)\n', (1091, 1109), True, 'import numpy as np\n'), ((1133, 1172), 'numpy.log', 'np.log', (['(num_instance / (df_vec + 1e-08))'], {}), '(num_instance / (df_vec + 1e-08))\n', (1139, 1172), True, 'import numpy as np\n'), ((2059, 2081), 'pandas.DataFrame', 'pd.DataFrame', (['x_counts'], {}), '(x_counts)\n', (2071, 2081), True, 'import pandas as pd\n'), ((2265, 2286), 'numpy.sum', 'np.sum', (['(X > 0)'], {'axis': '(0)'}), '(X > 0, axis=0)\n', (2271, 2286), True, 'import numpy as np\n'), ((2310, 2349), 'numpy.log', 'np.log', (['(num_instance / (df_vec + 1e-08))'], {}), '(num_instance / (df_vec + 1e-08))\n', (2316, 2349), True, 'import numpy as np\n'), ((3226, 3248), 'pandas.DataFrame', 'pd.DataFrame', (['X_counts'], {}), '(X_counts)\n', (3238, 3248), True, 'import pandas as pd\n'), ((825, 842), 'collections.Counter', 'Counter', (['X_seq[i]'], {}), '(X_seq[i])\n', (832, 842), False, 'from collections import Counter\n'), ((1201, 1241), 'numpy.tile', 'np.tile', (['self.idf_vec', '(num_instance, 1)'], {}), '(self.idf_vec, (num_instance, 1))\n', (1208, 1241), True, 'import numpy as np\n'), ((1411, 1452), 'numpy.tile', 'np.tile', (['self.mean_vec', '(num_instance, 1)'], {}), '(self.mean_vec, (num_instance, 1))\n', (1418, 1452), True, 'import numpy as np\n'), ((1984, 2001), 'collections.Counter', 'Counter', (['X_seq[i]'], {}), '(X_seq[i])\n', (1991, 2001), False, 'from collections import Counter\n'), ((2374, 2414), 'numpy.tile', 'np.tile', (['self.idf_vec', '(num_instance, 1)'], {}), '(self.idf_vec, (num_instance, 1))\n', (2381, 2414), True, 'import numpy as np\n'), ((2577, 2618), 'numpy.tile', 'np.tile', (['self.mean_vec', '(num_instance, 1)'], {}), '(self.mean_vec, (num_instance, 1))\n', (2584, 2618), True, 'import numpy as np\n'), ((3151, 3168), 'collections.Counter', 'Counter', (['X_seq[i]'], {}), '(X_seq[i])\n', (3158, 3168), False, 'from collections import Counter\n'), ((3648, 3688), 'numpy.tile', 'np.tile', (['self.idf_vec', '(num_instance, 1)'], {}), '(self.idf_vec, (num_instance, 1))\n', (3655, 3688), True, 'import numpy as np\n'), ((3762, 3803), 'numpy.tile', 'np.tile', (['self.mean_vec', '(num_instance, 1)'], {}), '(self.mean_vec, (num_instance, 1))\n', (3769, 3803), True, 'import numpy as np\n')]