Best Python code snippet using ATX
error_handler.py
Source:error_handler.py
1import logging2from typing import Any, Callable, List3from actions.commands.home import HomeCommand4from actions.commands.move import MoveCommand5from actions.commands.pause import PauseCommand6from actions.commands.set_pumps import SetPumpsCommand7from actions.feedback.feedback_manager import FeedbackManager8from actions.memory import Memory9from common.config import Config10from common.enums import State, ErrorHandlerCode11from common.log_event import Logger12from common.redis_client import Redis13from common.serial_manager import SerialManagerAbstract14from common.types import Command, Instruction, ErrorHandlerFactoryFunc15class ErrorHandler:16 def __init__(self,17 memory: Memory,18 feedback_manager: FeedbackManager,19 serial: SerialManagerAbstract,20 redis: Redis,21 config: Config,22 logger: Logger,23 cancel: Callable[[Instruction, Command, ErrorHandlerFactoryFunc, FeedbackManager, Memory, Redis,24 SerialManagerAbstract, Config, Logger, bool, bool], bool]) -> None:25 self._redis = redis26 self._config = config27 self._logger = logger28 self._memory = memory29 self._dependencies: List[Any] = [30 feedback_manager,31 memory,32 redis,33 serial,34 config,35 logger36 ]37 self._cancel = cancel38 def get_handler(self, code: ErrorHandlerCode) -> Callable[[Instruction, Command], None]:39 return {40 ErrorHandlerCode.FATAL_HOMING: self.fatal_handler_homing,41 ErrorHandlerCode.FATAL_RECOVERY_HOMING: self.fatal_handler_recovery_homing,42 ErrorHandlerCode.FATAL_MOVING: self.fatal_handler_moving,43 ErrorHandlerCode.FATAL_RECOVERY_MOVING: self.fatal_handler_recovery_moving,44 ErrorHandlerCode.FATAL_RECOVERY_PAUSE: self.fatal_handler_recovery_pause,45 ErrorHandlerCode.FATAL_RECOVERY_SET_PUMPS: self.fatal_handler_recovery_set_pumps,46 ErrorHandlerCode.FATAL_SET_POS: self.fatal_handler_set_pos,47 ErrorHandlerCode.STANDARD: self.standard_error_handler,48 ErrorHandlerCode.RESET_Z: self.reset_z_error_handler,49 ErrorHandlerCode.NOOP: self.noop_handler50 }[code]51 def fatal_handler_homing(self, instruction: Instruction, command: Command) -> None:52 self._logger.log_system(logging.FATAL, 'Something went wrong on startup while homing!')53 self._logger.log_system(logging.FATAL,54 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')55 self._logger.add_to_event(criticalStatusCode='startupHomingError',56 criticaErrorMessage='Something went wrong on startup while homing!')57 self._logger.send_event(logging.FATAL)58 self._redis.del_position()59 exit(-5)60 def fatal_handler_recovery_homing(self, instruction: Instruction, command: Command) -> None:61 self._logger.log_system(logging.FATAL, 'Something went wrong on recovery while homing!')62 self._logger.log_system(logging.FATAL,63 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')64 self._logger.add_to_event(criticalStatusCode='recoveryHomingError',65 criticaErrorMessage='Something went wrong on recovery while homing!')66 self._logger.send_event(logging.FATAL)67 self._redis.del_position()68 exit(-5)69 def fatal_handler_moving(self, instruction: Instruction, command: Command) -> None:70 pos = self._redis.get_position()71 if pos:72 x, y, z = pos73 if len(command['val']) == 7:74 x_to_bytes = command['val'][1][1:-1].split('|')[self._memory.current_choice].split('_')75 x_to = int(x_to_bytes[0]) + int(x_to_bytes[1]) * 25676 y_to = int(command['val'][2]) + int(command['val'][3]) * 25677 z_to = int(command['val'][4]) + int(command['val'][5]) * 25678 else:79 x_to = int(command['val'][1]) + int(command['val'][2]) * 25680 y_to = int(command['val'][3]) + int(command['val'][4]) * 25681 z_to = int(command['val'][5]) + int(command['val'][6]) * 25682 self._logger.log_system(logging.FATAL,83 f'Something went wrong on startup while moving from x: {x} y: {y} z: {z} '84 f'to x: {x_to} y: {y_to} z: {z_to}! :(')85 self._logger.log_system(logging.FATAL,86 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')87 self._logger.add_to_event(criticalStatusCode='startupMovingError',88 criticaErrorMessage=f'Something went wrong on startup while moving '89 f'from x: {x} y: {y} z: {z} '90 f'to x: {x_to} y: {y_to} z: {z_to}! :(')91 self._logger.send_event(logging.FATAL)92 else:93 self._logger.log_system(logging.FATAL, 'Something went wrong on startup while moving! :(')94 self._logger.log_system(logging.FATAL,95 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')96 self._logger.add_to_event(criticalStatusCode='startupMovingError',97 criticaErrorMessage='Something went wrong on startup while moving! :(')98 self._logger.send_event(logging.FATAL)99 exit(-5)100 def fatal_handler_recovery_moving(self, instruction: Instruction, command: Command) -> None:101 pos = self._redis.get_position()102 if pos:103 x, y, z = pos104 if len(command['val']) == 7:105 x_to_bytes = command['val'][1][1:-1].split('|')[self._memory.current_choice].split('_')106 x_to = int(x_to_bytes[0]) + int(x_to_bytes[1]) * 256107 y_to = int(command['val'][2]) + int(command['val'][3]) * 256108 z_to = int(command['val'][4]) + int(command['val'][5]) * 256109 else:110 x_to = int(command['val'][1]) + int(command['val'][2]) * 256111 y_to = int(command['val'][3]) + int(command['val'][4]) * 256112 z_to = int(command['val'][5]) + int(command['val'][6]) * 256113 self._logger.log_system(logging.FATAL,114 f'Something went wrong on recovery while moving from x: {x} y: {y} z: {z} '115 f'to x: {x_to} y: {y_to} z: {z_to}! :(')116 self._logger.log_system(logging.FATAL,117 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')118 self._logger.add_to_event(criticalStatusCode='recoveryMovingError',119 criticaErrorMessage=f'Something went wrong on recovery while moving '120 f'from x: {x} y: {y} z: {z} '121 f'to x: {x_to} y: {y_to} z: {z_to}! :(')122 self._logger.send_event(logging.FATAL)123 else:124 self._logger.log_system(logging.FATAL, 'Something went wrong on recovery while moving! :(')125 self._logger.log_system(logging.FATAL,126 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')127 self._logger.add_to_event(criticalStatusCode='recoveryMovingError',128 criticaErrorMessage='Something went wrong on recovery while moving! :(')129 self._logger.send_event(logging.FATAL)130 exit(-5)131 def fatal_handler_recovery_pause(self, instruction: Instruction, command: Command) -> None:132 self._logger.log_system(logging.FATAL, 'Something went wrong on recovery while sleeping!')133 self._logger.log_system(logging.FATAL,134 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')135 self._logger.add_to_event(criticalStatusCode='recoveryPauseError',136 criticaErrorMessage='Something went wrong on recovery while sleeping!')137 self._logger.send_event(logging.FATAL)138 exit(-5)139 def fatal_handler_recovery_set_pumps(self, instruction: Instruction, command: Command) -> None:140 self._logger.log_system(logging.FATAL, 'Something went wrong on recovery while setting pumps!')141 self._logger.log_system(logging.FATAL,142 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')143 self._logger.add_to_event(criticalStatusCode='recoverySetPumpsError',144 criticaErrorMessage='Something went wrong on recovery while setting pumps!')145 self._logger.send_event(logging.FATAL)146 exit(-5)147 def fatal_handler_set_pos(self, instruction: Instruction, command: Command) -> None:148 self._logger.log_system(logging.FATAL, 'Something went wrong while setting position!')149 self._logger.log_system(logging.FATAL,150 'Fatal error detected shutdown Bot script.\n\rHuman knowledge is needed.')151 self._logger.add_to_event(criticalStatusCode='settingPositionError',152 criticaErrorMessage='Something went wrong while setting position!')153 self._logger.send_event(logging.FATAL)154 self._redis.del_position()155 exit(-5)156 def noop_handler(self, instruction: Instruction, command: Command) -> None:157 pass158 def reset_z_error_handler(self, instruction: Instruction, command: Command) -> None:159 pos = self._redis.get_position()160 x, y, _ = [[axis % 256, int(axis / 256)] for axis in (pos if pos else [0, 0, 0])]161 if pos:162 MoveCommand.run(instruction, {'val': [0] + x + y + [50, 0, 100]}, self.get_handler,163 *self._dependencies, fatal_recovery=True)164 HomeCommand.run(instruction, {'val': [7, 0, 0, 0, 0, 1, 0, 20]}, self.get_handler,165 *self._dependencies, fatal_recovery=True)166 def standard_error_handler(self, instruction: Instruction, command: Command) -> None:167 PauseCommand.run(instruction, {'val': [3, 2]}, self.get_handler, *self._dependencies, fatal_recovery=True)168 state = self._redis.get_current_state()169 if not State.has_state(state, State.HOMING_Y | State.MOVING_Y):170 HomeCommand.run(instruction, {'val': [7, 0, 0, 0, 0, 1, 0, 20]}, self.get_handler,171 *self._dependencies, fatal_recovery=True)172 pos = self._redis.get_position()173 x, y, z = [[axis % 256, int(axis / 256)] for axis in (pos if pos else [0, 0, 0])]174 if State.has_state(state, State.TOGGLE_PUMPS_ON):175 SetPumpsCommand.run(instruction, {'val': [2, 0, 0, 0, 0, 0, 0]}, self.get_handler,176 *self._dependencies, fatal_recovery=True)177 HomeCommand.run(instruction, {'val': [7, 0, 0, 0, 0, 1, 0, 20]}, self.get_handler,178 *self._dependencies, fatal_recovery=True)179 if State.has_state(state, State.MOVING_X | State.HOMING_X):180 if pos:181 MoveCommand.run(instruction, {'val': [0, 0, 0] + y + z + [100]}, self.get_handler,182 *self._dependencies, fatal_recovery=True)183 HomeCommand.run(instruction, {'val': [7, 1, 0, 0, 0, 0, 0, 20]}, self.get_handler,184 *self._dependencies, fatal_recovery=True)185 if State.has_state(state, State.MOVING_Z_UP | State.MOVING_Z_DOWN | State.HOMING_Z):186 if pos:187 MoveCommand.run(instruction, {'val': [0] + x + y + [0, 0, 100]}, self.get_handler,188 *self._dependencies, fatal_recovery=True)189 HomeCommand.run(instruction, {'val': [7, 0, 0, 0, 0, 1, 0, 20]}, self.get_handler,190 *self._dependencies, fatal_recovery=True)191 if State.has_state(state, State.HOMING_Y | State.MOVING_Y):192 if pos:193 MoveCommand.run(instruction, {'val': [0] + x + [114, 56] + z + [100]}, self.get_handler,194 *self._dependencies, fatal_recovery=True)195 HomeCommand.run(instruction, {'val': [7, 0, 0, 2, 0, 0, 0, 20]}, self.get_handler,196 *self._dependencies, fatal_recovery=True)...
fe.py
Source:fe.py
1import pandas as pd2import numpy as np3fatal_encounters_df = pd.read_csv('fatal-encounters_2.csv')4# Drop unwanted columns5fatal_encounters_df = fatal_encounters_df.drop(6 ["Unique ID",7 "Subject's name",8 "URL of image of deceased",9 "Location of injury (address)",10 "Full Address",11 "A brief description of the circumstances surrounding the death",12 "Dispositions/Exclusions INTERNAL USE, NOT FOR ANALYSIS",13 "Link to news article or photo of official document",14 "Video",15 "Brief description",16 "Unique ID formula",17 "Unique identifier (redundant)",18 "Date (Year)",19 "URL of image (PLS NO HOTLINKS)",20 "Unnamed: 24",21 "Unnamed: 25",22 "Supporting document link",23 "Location of death (zip code)",24 "Imputation probability",25 "Location of death (city)",26 "Location of death (county)",27 "Race",28 "Latitude",29 "Longitude",30 "Name"],31 axis='columns', errors='ignore')32# Rename33fatal_encounters_df.rename(columns={34 "Subject's age": "Age",35 "Subject's gender": "Gender",36 "Race with imputations": "Race",37 "Date of injury resulting in death (month/day/year)": "Date",38 "Intended use of force (Developing)": "Intended use of force",39 "Location of death (state)": "State",40 "Foreknowledge of mental illness? INTERNAL USE, NOT FOR ANALYSIS": "Mental illness"41}, inplace=True)42# Drop missing43fatal_encounters_df.dropna(inplace=True)44# Age45fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '3 days', '0', inplace=True)46fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '2 months', '0', inplace=True)47fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '3 months', '0', inplace=True)48fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '4 months', '0', inplace=True)49fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '6 months', '0', inplace=True)50fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '7 months', '0', inplace=True)51fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '8 months', '0', inplace=True)52fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '9 months', '0', inplace=True)53fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '10 months', '0', inplace=True)54fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '18 months', '1', inplace=True)55fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '55.', '55', inplace=True)56fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '20s', '25', inplace=True)57fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '30s', '35', inplace=True)58fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '40s', '45', inplace=True)59fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '50s', '55', inplace=True)60fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '60s', '65', inplace=True)61fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '70s', '75', inplace=True)62fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '18-25', '20', inplace=True)63fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '40-50', '45', inplace=True)64fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '46/53', '50', inplace=True)65fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '20s-30s', '30', inplace=True)66fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.25', '0', inplace=True)67fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '0.25', '0', inplace=True)68fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.8', '0', inplace=True)69fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '0.8', '0', inplace=True)70fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.5', '0', inplace=True)71fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.17', '0', inplace=True)72fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.75', '0', inplace=True)73fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.66', '0', inplace=True)74fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.58', '0', inplace=True)75fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.33', '0', inplace=True)76fatal_encounters_df['Age'].mask(fatal_encounters_df['Age'] == '.08', '0', inplace=True)77fatal_encounters_df.Age = fatal_encounters_df.Age.astype('int')78fatal_encounters_df = fatal_encounters_df[fatal_encounters_df['Age'] < 100]79age_bins = [-1,4,9,14,19,24,29,34,39,44,49,54,59,64,69,74,79,84,89,94,99]80age_labels = ['00-04','05-09', '10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99']81fatal_encounters_df['Age Group'] = pd.cut(fatal_encounters_df.Age, age_bins, labels=age_labels)82# # Gender83fatal_encounters_df = fatal_encounters_df[fatal_encounters_df['Gender'] != 'Transgender']84# Race85fatal_encounters_df['Race'].mask(fatal_encounters_df['Race'] == 'Asian/Pacific Islander', 'Other', inplace=True)86fatal_encounters_df['Race'].mask(fatal_encounters_df['Race'] == 'Middle Eastern', 'Other', inplace=True)87fatal_encounters_df['Race'].mask(fatal_encounters_df['Race'] == 'Native American/Alaskan', 'Other', inplace=True)88fatal_encounters_df['Race'].mask(fatal_encounters_df['Race'] == 'Race unspecified', 'Other', inplace=True)89# Date90fatal_encounters_df[['Month', 'Day', 'Year']] = fatal_encounters_df.Date.str.split('/', expand=True)91fatal_encounters_df.Date = fatal_encounters_df[['Year','Month']].apply(lambda x: '-'.join(x), axis=1)92fatal_encounters_df = fatal_encounters_df[fatal_encounters_df['Year'] != '2020']93# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '01', 'Jan', inplace=True)94# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '02', 'Feb', inplace=True)95# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '03', 'Mar', inplace=True)96# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '04', 'Apr', inplace=True)97# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '05', 'May', inplace=True)98# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '06', 'Jun', inplace=True)99# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '07', 'Jul', inplace=True)100# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '08', 'Aug', inplace=True)101# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '09', 'Sep', inplace=True)102# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '10', 'Oct', inplace=True)103# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '11', 'Nov', inplace=True)104# fatal_encounters_df['Month'].mask(fatal_encounters_df['Month'] == '12', 'Dec', inplace=True)105# States106fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'AL', 'Alabama', inplace=True)107fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'AK', 'Alaska', inplace=True)108fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'AZ', 'Arizona', inplace=True)109fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'AR', 'Arkansas', inplace=True)110fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'CA', 'California', inplace=True)111fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'CO', 'Colorado', inplace=True)112fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'CT', 'Connecticut', inplace=True)113fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'DE', 'Delaware', inplace=True)114fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'DC', 'District of Columbia', inplace=True)115fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'FL', 'Florida', inplace=True)116fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'GA', 'Georgia', inplace=True)117fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'HI', 'Hawaii', inplace=True)118fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'ID', 'Idaho', inplace=True)119fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'IL', 'Illinois', inplace=True)120fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'IN', 'Indiana', inplace=True)121fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'IA', 'Iowa', inplace=True)122fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'KS', 'Kansas', inplace=True)123fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'KY', 'Kentucky', inplace=True)124fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'LA', 'Louisiana', inplace=True)125fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'ME', 'Maine', inplace=True)126fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'MD', 'Maryland', inplace=True)127fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'MA', 'Massachusetts', inplace=True)128fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'MI', 'Michigan', inplace=True)129fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'MN', 'Minnesota', inplace=True)130fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'MS', 'Mississippi', inplace=True)131fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'MO', 'Missouri', inplace=True)132fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'MT', 'Montana', inplace=True)133fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'NE', 'Nebraska', inplace=True)134fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'NV', 'Nevada', inplace=True)135fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'NH', 'New Hampshire', inplace=True)136fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'NJ', 'New Jersey', inplace=True)137fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'NM', 'New Mexico', inplace=True)138fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'NY', 'New York', inplace=True)139fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'NC', 'North Carolina', inplace=True)140fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'ND', 'North Dakota', inplace=True)141fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'OH', 'Ohio', inplace=True)142fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'OK', 'Oklahoma', inplace=True)143fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'OR', 'Oregon', inplace=True)144fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'PA', 'Pennsylvania', inplace=True)145fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'RI', 'Rhode Island', inplace=True)146fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'SC', 'South Carolina', inplace=True)147fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'SD', 'South Dakota', inplace=True)148fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'TN', 'Tennessee', inplace=True)149fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'TX', 'Texas', inplace=True)150fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'UT', 'Utah', inplace=True)151fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'VT', 'Vermont', inplace=True)152fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'VA', 'Virginia', inplace=True)153fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'WA', 'Washington', inplace=True)154fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'WV', 'West Virginia', inplace=True)155fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'WI', 'Wisconsin', inplace=True)156fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'WY', 'Wyoming', inplace=True)157fatal_encounters_df['State'].mask(fatal_encounters_df['State'] == 'AL', 'Alabama', inplace=True)158fatal_encounters_df['Intended use of force'].mask(fatal_encounters_df['Intended use of force'] == 'Vehic/Purs', 'Vehicle/Pursuit', inplace=True)159fatal_encounters_df['Intended use of force'].mask(fatal_encounters_df['Intended use of force'] == 'Vehicle', 'Vehicle/Pursuit', inplace=True)160fatal_encounters_df['Intended use of force'].mask(fatal_encounters_df['Intended use of force'] == 'Pursuit', 'Vehicle/Pursuit', inplace=True)161fatal_encounters_df.drop(columns=['Date', 'Agency or agencies involved', 'Day'], inplace=True)...
LeafletMap.js
Source:LeafletMap.js
1// ############### Leaflet map - load data directly, better plugin (heatmap) support !! ####2var currentYearData = [];3var heatLayer;4var incidentMarkerLayer;5var fatalIncidentsLayer;6var map;7var fatalMarkerDays = [];8var yearFatalIncidentCount = 0;9var droppedFatalIncidentCount = 0;10var fatalIndicentCallback;11function LeafletMap(mapID){12 13 // TODO: Icons with Icon.extend - http://leafletjs.com/examples/custom-icons.html14 15 // Create map with no attribution16 map = L.map(mapID, {attributionControl: false, zoomControl: false}).setView([-42.881321987572, 147.3328511961707], 7);17 18 // add an OpenStreetMap tile layer19 L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {20 attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' }).addTo(map);21 22 // Add back attribution in the right place, because we care.. 23 //TODO: data link24 var attribution = L.control.attribution({prefix: 'Data obtained from <a href="#">data.gov.au</a>', position: 'topright'}).addTo(map);25 var zoomControl = L.control.zoom({position:'topright'}).addTo(map);26};27LeafletMap.prototype.loadYearData = function(yearData, daysInYear){28 var fromProj = "+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"; 29 var toProj = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";30 31 // Reset current year data!32 currentYearData = [];33 yearFatalIncidentCount = 0;34 35 fatalIncidentsLayer = new L.layerGroup().addTo(map);36 // isLeap = new Date(year, 1, 29).getMonth() == 137 // TODO: calc days in year38 //var daysInYear = 365;39 40 // Add crash data per day to be rendered later41 for (var i = 0; i < daysInYear; i++){42 for(var j = 0; j < yearData.result.records.length; j++){43 44 var cDateDOY = new Date(yearData.result.records[j]['dt']).getDOY();45 46 // Create empty array for each day of the year to append new crash events to47 if(currentYearData[i] == undefined){48 currentYearData[i] = [];49 }50 if(cDateDOY == i){51 if(currentYearData[i] == null){52 currentYearData[i] = [];53 }54 //console.log("Adding result for day " + i + " " + yearData.result.records[j]);55 var tmpData = yearData.result.records[j];56 //console.log(tmpData);57 coords = new proj4(fromProj, toProj, [tmpData.x,tmpData.y]); //any object will do as long as it has 'x' and 'y' properties58 tmpData.lat = coords[1];59 tmpData.lon = coords[0];60 tmpData.x = null;61 tmpData.y = null;62 tmpData.ct = yearData.result.records[j].ct;63 tmpData.sev = yearData.result.records[j].sev;64 //console.log(parseInt(tmpData.sev));65 if(parseInt(tmpData.sev) == 6){66 yearFatalIncidentCount++;67 }68 /* default data properties69 * ct: count70 * dt: date (YYYY-MM-DD)71 * lat: latittude72 * lon: longtitude73 * sev: severity74 */ 75 currentYearData[i].push(tmpData);76 }77 } 78 }79 console.log("Total fatal incidents this year: " + yearFatalIncidentCount);80};81LeafletMap.prototype.getYearFatalIncidentCount = function(){82 return yearFatalIncidentCount;83}84LeafletMap.prototype.hideFatalIncidents = function(){85 map.removeLayer(fatalIncidentsLayer);86};87LeafletMap.prototype.showFatalIncidents = function(){88 map.addLayer(fatalIncidentsLayer);89}90// Clear fatal incidents when changing year91LeafletMap.prototype.clearFatalIncidents = function(){92 fatalMarkerDays = [];93 droppedFatalIncidentCount = 0;94 map.removeLayer(fatalIncidentsLayer);95 fatalIncidentLayer = null;96};97LeafletMap.prototype.setFatalIncidentDropCallback = function(callback){98 fatalIndicentCallback = callback;99}100LeafletMap.prototype.dropFatalMarker = function(eventInfo){101 102 var fatalIconName = "fatal-marker";103 var markerDims = [64, 64];104 105 // TODO: shadows106 var fatalMarkerIcon = L.icon({107 iconUrl: './images/' + fatalIconName + '.png',108 iconSize: markerDims,109 shadowSize: markerDims,110 iconAnchor: [markerDims[0] * 0.5, markerDims[1] * 0.96],111 shadowAnchor: [0,0],112 popupAnchor: [0, -markerDims[1] * 0.95]113 }); 114 115 var markerText = "Fatal Incident! on " + eventInfo.dt + ": <br>Severity: " + eventInfo.sev + "<br/>Count: " + eventInfo.ct;116 var incidentMarker = L.marker([eventInfo.lat, eventInfo.lon], {117 icon: fatalMarkerIcon, 118 bounceOnAdd: true, 119 bounceOnAddOptions: {120 duration: 500, height: 100 } }).bindPopup(markerText);121 122 fatalIncidentsLayer.addLayer(incidentMarker);123};124LeafletMap.prototype.renderFatalMarkersInRange = function(day, range){125 if(!map.hasLayer(fatalIncidentsLayer)){126 fatalIncidentsLayer = new L.layerGroup().addTo(map);127 }128 129 for(var i = 0; i < range; i++){130 var dayNum = +day.getDOY() + 1 - i;131 if(dayNum > 0){132 133 var tmpDate = new Date(day.getFullYear(), day.getMonth(), day.getDate());134 tmpDate.setDate(day.getDate() - i);135 //console.log("Rendering date " + i + ", " + tmpDate.toDateString());136 this.renderFatalMarkers(tmpDate);137 }138 }139};140// Look at current day's events and drop markers if there was a fatal incident :( 141LeafletMap.prototype.renderFatalMarkers = function(day){142 var currentDayEvents = [];143 //console.log("rendering fatal markers for date" + day.getDate());144 var dayNum = +day.getDOY() + 1;145 var eventsOnDay = currentYearData[dayNum];146 if(eventsOnDay != null){147 var fatalMarkers = [];148 var dayAlreadyRendered = false;149 var fatalIncidentDay = false;150 for(var i = 0; i < eventsOnDay.length; i++){151 if(eventsOnDay[i].sev == 6){152 for(var j = 0; j < fatalMarkerDays.length; j++){153 if(fatalMarkerDays[j] == dayNum){154 dayAlreadyRendered = true;155 }156 }157 if(!dayAlreadyRendered){158 setTimeout(this.dropFatalMarker, i * 100, eventsOnDay[i]);159 droppedFatalIncidentCount++;160 fatalIncidentDay = true;161 }162 }163 }164 if(!dayAlreadyRendered && fatalIncidentDay){165 fatalMarkerDays.push(dayNum);166 fatalIndicentCallback(droppedFatalIncidentCount);167 }168 }169 170 // Add fatal incidents layer to map if it's not already there171 //if(!map.hasLayer(fatalIncidentsLayer)){}172};173// Immediately render last few days of accidents, as a heatmap174LeafletMap.prototype.renderHeatDay = function(day, range){175 var eventsInRange = []; 176 for(var i = 0; i < range; i++){177 178 var dayNum = +day.getDOY() + 1 - i;179 var eventsOnDay = currentYearData[dayNum];180 181 if (eventsOnDay != undefined){182 //console.log("rendering day: " + dayNum + " i: " + i + " day: " + (day.getDOY() + 1));183 //console.log(eventsOnDay);184 for(var j = 0; j < eventsOnDay.length; j++){185 var dayData = [ eventsOnDay[j].lat, eventsOnDay[j].lon, eventsOnDay[j].ct ];186 187 // repeat to increase density???188 for(var k = 0; k < +eventsOnDay[j].ct; k++){189 eventsInRange.push(dayData); 190 } 191 }192 } else {193 //console.log("No events on day: " + dayNum);194 }195 }196 if(map.hasLayer(heatLayer)){197 heatLayer.setLatLngs(eventsInRange);198 //map.removeLayer(heatLayer);199 //heatLayer = null;200 } else {201 heatLayer = L.heatLayer(eventsInRange, {max: 2, radius: 20, blur: 10, maxZoom: 11}).addTo(map); 202 } 203};204// TODO: sort by severity205LeafletMap.prototype.dropAccidentsOnDay = function(day, range){206 console.log("dropping markers for day: " + day);207 var tempMarkers = [];208 209 if(map.hasLayer(incidentMarkerLayer)){210 map.removeLayer(incidentMarkerLayer);211 incidentMarkerLayer = null;212 }213 incidentMarkerLayer = new L.layerGroup().addTo(map);214 for(var i = 0; i < range; i++){215 var dayNum = +day.getDOY() + 1 - i;216 var eventsOnDay = currentYearData[dayNum];217 if(eventsOnDay != undefined){218 for(var j = 0; j < eventsOnDay.length; j++){219 if(eventsOnDay[j].sev < 6 && eventsOnDay[j].sev > 2){220 var markerIcon = this.getIconBySevAndAge(eventsOnDay[j].sev, i+1, range);221 setTimeout(this.addMarker, i * 60 + j * 30, eventsOnDay[j], markerIcon);222 } 223 }224 }225 }226};227LeafletMap.prototype.getIconBySevAndAge = function(sev, age, range){228 229 var markerAge = Math.floor(age/range) * 2;230 // console.log("Marker age: " + markerAge + " Severity: " + sev);231 // old large size: 50, 82232 // old mid size: 37, 61233 var iconSizes = [[25, 41], [25, 41], [12, 20]]; 234 var markerDims = iconSizes[markerAge];235 var ageSize = ["1-5x", "1-5x", "1x"];236 var markerPrefix = "4359_0_marker-icon-";237 var sizeStr = ageSize[markerAge];238 var sevStr = "-" + (6-sev);239 var iconStr = markerPrefix + sizeStr + sevStr;240 241 // TODO: shadows242 return L.icon({243 iconUrl: './images/' + iconStr + '.png',244 iconSize: markerDims,245 shadowSize: markerDims,246 iconAnchor: [markerDims[0] * 0.5, markerDims[1] * 0.96],247 shadowAnchor: [0,0],248 popupAnchor: [0, markerDims[1] * -.75]249 }); 250};251LeafletMap.prototype.addMarker = function(eventInfo, markerIcon){252 var markerText = "Crash Info on " + eventInfo.dt + ": <br>Severity: " + eventInfo.sev + "<br/>Count: " + eventInfo.ct;253 var incidentMarker = L.marker([eventInfo.lat, eventInfo.lon], {254 icon: markerIcon, 255 bounceOnAdd: true, 256 bounceOnAddOptions: {257 duration: 500, height: 100 } }).bindPopup(markerText);258 incidentMarkerLayer.addLayer(incidentMarker);259};260LeafletMap.prototype.clearIncidentMarkers = function(){261 if(map.hasLayer(incidentMarkerLayer)){262 map.removeLayer(incidentMarkerLayer);263 incidentMarkerLayer = null;264 }265};266LeafletMap.prototype.disableInteraction = function(){267 // Disable panning & zooming (can crash some browsers XD)268 map.dragging.disable();269 map.touchZoom.disable();270 map.doubleClickZoom.disable();271 map.scrollWheelZoom.disable();272 map.boxZoom.disable();273 map.keyboard.disable();274};275LeafletMap.prototype.enableInteraction = function(){276 // re-enable zooming/panning277 map.dragging.enable();278 map.touchZoom.enable();279 map.doubleClickZoom.enable();280 map.scrollWheelZoom.enable();281 map.boxZoom.enable();282 map.keyboard.enable();...
ProcessData_ONCovidCases_DL.py
Source:ProcessData_ONCovidCases_DL.py
1# load CSV data...2import pandas as pd3# datafiles... downloaded separately4DataFileCases = 'CANCovidData_Cases.csv'5DataFileFatal = 'CANCovidData_Fatal.csv'6# read in the data7df_CanCovidCases = pd.read_csv (DataFileCases)8df_CanCovidFatal = pd.read_csv (DataFileFatal)9# get just the ontario cases...10df_OntCovidCases = df_CanCovidCases[df_CanCovidCases['province'] == 'Ontario']11df_OntCovidFatal = df_CanCovidFatal[df_CanCovidFatal['province'] == 'Ontario']12# uncomment for testing...13# display (df_OntCovidCases)14# display (df_OntCovidFatal)15# need to convert the date format to YYYY-MM-DD16cleanDatesCases = []17dateCaseList = list(df_OntCovidCases['date_report'].unique())18for date in dateCaseList:19 20 dateElems = date.split('-')21 dateElems.reverse()22 cleanDatesCases.append ('-'.join(dateElems))23 24dateCaseList = cleanDatesCases25cleanDatesFatal = []26dateFatalList = list(df_OntCovidFatal['date_death_report'].unique())27for date in dateFatalList:28 29 dateElems = date.split('-')30 dateElems.reverse()31 cleanDatesFatal.append ('-'.join(dateElems))32 33dateFatalList = cleanDatesFatal34# initialize dictionaries to store data... daily cases, cumulative, and the derivative35resultsCasesDay = {}36resultsCasesCum = {}37resultsCasesDer = {}38# assign the date values...39resultsCasesDay['Dates'] = dateCaseList40resultsCasesCum['Dates'] = dateCaseList41resultsCasesDer['Dates'] = dateCaseList42# get the list of health regions...43HRCasesList = df_OntCovidCases['health_region'].unique()44# go through all the health regions and either get the data or compute the derivative45for HR in HRCasesList:46 47 HR_DF = df_OntCovidCases[df_OntCovidCases['health_region'] == HR]48 caseList = list (HR_DF['cases'])49 resultsCasesDay[HR] = caseList50 51 cumCasesList = list (HR_DF['cumulative_cases'])52 resultsCasesCum[HR] = cumCasesList 53 54 # Make a copy of the cumulative case list, and pad the beginning and end with55 # appropriate copies so that the moving average can be found for each element in56 # the original list57 tmpCases = cumCasesList.copy ()58 tmpCases.insert (0, cumCasesList[0])59 tmpCases.insert (0, cumCasesList[0])60 tmpCases.append (cumCasesList[-1])61 tmpCases.append (cumCasesList[-1])62 # calculate the moving average for each element 63 MA = []64 for i in range (2, len(tmpCases) - 2):65 MA.append ( (tmpCases[i-2] + tmpCases[i-1] \66 + tmpCases[i] + \67 tmpCases[i+1] + tmpCases[i+2] ) / 5 )68 69 # pad the moving average array so the derivative can be found for each element70 MA.insert (0, MA[0])71 MA.append (MA[-1])72 73 # finally compute the first derivative 74 firstDer = [] 75 for i in range(1, len(MA)-1):76 firstDer.append ( (MA[i+1] - MA[i-1]) / 2 ) 77 78 resultsCasesDer[HR] = firstDer79 80 81# Finally make dataframes for the values just found/computed. Note that types82# are assigned so that values can be separated during visualization.83resultsCasesCumDF = pd.DataFrame.from_dict (resultsCasesCum)84resultsCasesCumDF['Type'] = 185resultsCasesDayDF = pd.DataFrame.from_dict (resultsCasesDay)86resultsCasesDayDF['Type'] = 287resultsCasesDerDF = pd.DataFrame.from_dict (resultsCasesDer)88resultsCasesDerDF['Type'] = 389#90# Deaths Data file...91#92# Initialize some dictionaries for the results...93resultsFatalDay = {}94resultsFatalCum = {}95resultsFatalDer = {}96# Assign the dates... note that these dates can be different compared to the97# case date list98resultsFatalDay['Dates'] = dateFatalList99resultsFatalCum['Dates'] = dateFatalList100resultsFatalDer['Dates'] = dateFatalList101# get the list of health regions...102HRDeathsList = df_OntCovidFatal['health_region'].unique()103# uncomment for debugging...104# print (HRDeathsList)105# In order to concatenate the dataframes later, be need to add fields for 106# items that do not exist in the case data set. If we don't do this we will get107# NAs... which we don't like.108for HR in HRCasesList:109 if HR not in HRDeathsList:110 resultsFatalDay[HR] = 0111 resultsFatalCum[HR] = 0112 resultsFatalDer[HR] = 0113 114# go through all the health regions and either get the data or compute the derivative115for HR in HRDeathsList:116 117 HR_DF = df_OntCovidFatal[df_OntCovidFatal['health_region'] == HR]118 deathList = list(HR_DF['deaths'])119 resultsFatalDay[HR] = deathList120 121 cumDeathList = list(HR_DF['cumulative_deaths'])122 resultsFatalCum[HR] = cumDeathList123 124 # uncomment for debugging 125 # print ('cumDeathList', cumDeathList[-1])126 127 # make a copy of the cumulative deaths and then pad the front and back so that128 # the moving average can be determined for each element129 tmpCases = cumDeathList.copy ()130 tmpCases.insert (0, cumDeathList[0])131 tmpCases.insert (0, cumDeathList[0])132 tmpCases.append (cumDeathList[-1])133 tmpCases.append (cumDeathList[-1])134 # compute the moving average135 MA = []136 for i in range (2, len(tmpCases) - 2):137 MA.append ( (tmpCases[i-2] + tmpCases[i-1] \138 + tmpCases[i] + \139 tmpCases[i+1] + tmpCases[i+2] ) / 5 )140 141 # pad the moving average array so that the first derivative can be calculated for all142 # elements in the list143 MA.insert (0, MA[0])144 MA.append (MA[-1])145 146 # finally compute the first derivative 147 firstDer = [] 148 for i in range(1, len(MA)-1):149 firstDer.append ( (MA[i+1] - MA[i-1]) / 2 ) 150 151 resultsFatalDer[HR] = firstDer152 153# make dataframes of all the results, and label then with the type.154resultsFatalCumDF = pd.DataFrame.from_dict (resultsFatalCum)155resultsFatalCumDF['Type'] = 4156resultsFatalDayDF = pd.DataFrame.from_dict (resultsFatalDay)157resultsFatalDayDF['Type'] = 5158resultsFatalDerDF = pd.DataFrame.from_dict (resultsFatalDer)159resultsFatalDerDF['Type'] = 6160# combine the all the dataframes... so we have one big dataframe of all the data161frames = [resultsCasesCumDF, resultsCasesDayDF, resultsCasesDerDF, \162 resultsFatalCumDF, resultsFatalDayDF, resultsFatalDerDF ]163finalResultsDF = pd.concat (frames, sort=True)164# uncomment for testing...165# display (finalResultsDF)166# write to CSV...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!