def main(data, target): """ Main generate function get data and target id """ years = [x for x in range(1970, 2017) if x != 1993] per_cent = [] targets = {1: "Business", 2: "Government (General)", 3: "Police", 4: "Military", 5: "Abortion Related", 6: "Airports & Aircraft", 7: "Government (Diplomatic)", 8: "Educational Institution", 9: "Food or Water Supply", 10: "Journalists & Media", 11: "Maritime", 12: "NGO", 13: "Other", 14: "Private Citizen & Property", 15: "Religious", 16: "Telecommunication", 17: "Terrorists/Non-State Militias", 18: "Tourists", 19: "Transportation", 20: "Unknown", 21: "Utilities", 22: "Violent Political Parties"} for year in years: frequency_in_target = len(data[(data['targtype1'] == target) & (data['iyear'] == year)]) frequency = frequency_in_target per_cent.append(frequency) # Graph generate goes here! chart = pygal.Line(x_labels_major_count=8, show_minor_x_labels=False, truncate_legend=40, legend_at_bottom=True, truncate_label=20) chart.title = 'Frequency of %s from 1970 to 2016 (Except 1993)' %(targets[target]) chart.x_labels = [str(x) for x in years] chart.add('Frequency (Time)', per_cent) # Export file name as Targets_Frequency_<target_id> chart.render_to_file('Targets_Frequency_%s.svg' %target) pd.set_option('display.expand_frame_repr', False) pd.set_option('display.max_rows', None) print("\nGraph generated!\n") # End of modules and return back to main targets_main.main(data)
def frequency(data, test=False): """ Main generate function get data and weapon id = 0 (Overall) """ # Graph generate goes here! # Export file name as Targets_Frequency_Overall targets = { 1: "Business", 2: "Government (General)", 3: "Police", 4: "Military", 5: "Abortion Related", 6: "Airports & Aircraft", 7: "Government (Diplomatic)", 8: "Educational Institution", 9: "Food or Water Supply", 10: "Journalists & Media", 11: "Maritime", 12: "NGO", 13: "Other", 14: "Private Citizen & Property", 15: "Religious", 16: "Telecommunication", 17: "Terrorists/Non-State Militias", 18: "Tourists", 19: "Transportation", 20: "Unknown", 21: "Utilities", 22: "Violent Political Parties" } result_x = [targets[x] for x in sorted(targets)] result_y = [] for x in sorted(targets): freq = len(data[data['targtype1'] == x]) result_y.append(freq) chart = pygal.Bar(show_minor_x_labels=False, truncate_legend=40, truncate_label=20, x_label_rotation=90, logarithmic=True, y_labels_major_every=3, show_minor_y_labels=False) chart.x_labels = [] chart.title = 'Overall incidents by target categories from 1970 to 2016 except 1993' for x in range(22): chart.add(result_x[x] + " (" + str(result_y[x]) + ")", result_y[x]) chart.render_to_file('Charts/Targets_Frequency_Overall.svg') # End of modules and return back to main if test: return print("\nGraph generated!") targets_main.main(data)
def main(data, target, test=False): """ Main generate function get data and target id """ # Initialize year list years = [x for x in range(1970, 2017) if x != 1993] # Initialize frequency list freq = [] # Initialize targets list targets = { 1: "Business", 2: "Government (General)", 3: "Police", 4: "Military", 5: "Abortion Related", 6: "Airports & Aircraft", 7: "Government (Diplomatic)", 8: "Educational Institution", 9: "Food or Water Supply", 10: "Journalists & Media", 11: "Maritime", 12: "NGO", 13: "Other", 14: "Private Citizen & Property", 15: "Religious", 16: "Telecommunication", 17: "Terrorists or Non-State Militias", 18: "Tourists", 19: "Transportation", 20: "Unknown", 21: "Utilities", 22: "Violent Political Parties" } # Calculate result for year in years: frequency = len(data[(data['targtype1'] == target) & (data['iyear'] == year)]) freq.append(frequency) # Graph generate goes here! chart = pygal.Line(x_labels_major_count=8, show_minor_x_labels=False, truncate_legend=40, legend_at_bottom=True, truncate_label=20) # Set chart title chart.title = 'Frequency of %s from 1970 to 2016 (Except 1993)' % ( targets[target]) # Set label chart.x_labels = [str(x) for x in years] # Add data chart.add('Frequency (Times)', freq) # Export file name as Targets_Frequency_<target_id> chart.render_to_file('Charts/Targets_Frequency_%s.svg' % targets[target]) # End of modules and return back to main if test: return print("\nGraph generated!\n") targets_main.main(data)
def menu_targets(data): """ Main menu of targets """ targets.main(data)
def main(data, target, test=False): """ Main generate function get data and target id """ # Initialize year list years = [x for x in range(1970, 2017) if x != 1993] # Initialize percent list per_cent = [] # Initialize targets list targets = { 1: "Business", 2: "Government (General)", 3: "Police", 4: "Military", 5: "Abortion Related", 6: "Airports & Aircraft", 7: "Government (Diplomatic)", 8: "Educational Institution", 9: "Food or Water Supply", 10: "Journalists & Media", 11: "Maritime", 12: "NGO", 13: "Other", 14: "Private Citizen & Property", 15: "Religious", 16: "Telecommunication", 17: "Terrorists or Non-State Militias", 18: "Tourists", 19: "Transportation", 20: "Unknown", 21: "Utilities", 22: "Violent Political Parties" } # Calculate result for year in years: # Count which success success_in_target = len( data[(data['targtype1'] == target) & (data['success'] == 1) & (data['iyear'] == year)]) # Count all success_all = len(data[(data['success'] == 1) & (data['iyear'] == year)]) # Prevent divided by zero if success_all == 0: if test: return print("\nGraph can't generated!") targets_main.main(data) # Calculate success rate success = round((success_in_target / success_all) * 100, 2) # Add success rate into list per_cent.append(success) # Graph generate goes here! chart = pygal.Line(x_labels_major_count=8, show_minor_x_labels=False, truncate_legend=40, legend_at_bottom=True, truncate_label=20, value_formatter=lambda x: "%.2f%%" % x) # Add title into chart chart.title = 'Success Rate of %s from 1970 to 2016 (Except 1993) (in percent)' % ( targets[target]) # Add label into chart chart.x_labels = [str(x) for x in years] # Add data into chart chart.add('Success Rate (%)', per_cent) # Export file name as Target_Success_<target_id> chart.render_to_file('Charts/Targets_Success_%s.svg' % targets[target]) # End of modules and return back to main if test: return print("\nGraph generated!\n") targets_main.main(data)
def victims(data, test=False): """ Horizontal Bar chart show highest Victim in categories """ targets = { 1: "Business", 2: "Government (General)", 3: "Police", 4: "Military", 5: "Abortion Related", 6: "Airports & Aircraft", 7: "Government (Diplomatic)", 8: "Educational Institution", 9: "Food or Water Supply", 10: "Journalists & Media", 11: "Maritime", 12: "NGO", 13: "Other", 14: "Private Citizen & Property", 15: "Religious", 16: "Telecommunication", 17: "Terrorists/Non-State Militias", 18: "Tourists", 19: "Transportation", 20: "Unknown", 21: "Utilities", 22: "Violent Political Parties" } result_x = [targets[x] for x in sorted(targets)] result_y = [] for x in sorted(targets): maximum = max(data[data['targtype1'] == x]['nkill']) if isnan(maximum): result_y.append(0) continue freq = data[(data['targtype1'] == x) & (data['nkill'] == maximum)].nkill.values[0] result_y.append(freq) # Initialize Line Chart chart = pygal.HorizontalBar(x_labels_major_count=8, show_minor_x_labels=False, truncate_legend=40, truncate_label=20) # Chart title chart.title = 'Compare the most victims incident by targets' # X-Axis Label chart.x_labels = [] # Y-Axis and label for i in range(22): if result_y == 0: continue chart.add(result_x[i], result_y[i]) # Save chart into file filename = 'Charts/Targets_Compare_Maximum_Victims.svg' chart.render_to_file(filename) # End of modules and return back to main if test: return print("\nGraph generated!") targets_main.main(data)