def compute_performance(scenario, G, index, demand, no_damage_travel_time, no_damage_vmt):
  import time
  import pickle
  import pdb
  import networkx 
  import util
  from travel_main_simple_simplev3 import damage_bridges
  from travel_main_simple_simplev3 import measure_performance
  from travel_main_simple_simplev3 import damage_network
  from travel_main_simple_simplev3 import get_graph
  start_time = time.time()

  if G == None:
    G = get_graph()

  #figure out bridge damage for each scenario
  damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges = damage_bridges(scenario) #e.g., [1, 89, 598] #num_bridges_out is highway bridges only

  # #figure out network damage and output Cube files to this effect
  G = damage_network(damaged_bridges_internal, damaged_bridges_new, G, time.strftime("%Y%m%d")+'_filesForCube/', index)

  # #figure out impact (performance metrics)
  flow, shortest_paths, travel_time, vmt = measure_performance(G, num_damaged_bridges, demand, no_damage_travel_time, no_damage_vmt)
  G = util.clean_up_graph(G)
  print 'total scenario time: ', time.time() - start_time
  return damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges, flow, shortest_paths, travel_time, vmt
def main():
  '''can change the number of epsilons below'''
  seed(0) #set seed 
  simple = False  #simple is just %bridges out, which is computationally efficient
  number_of_highway_bridges = 1743
  numeps = 3 #the number of epsilons
  tol = 0.00001 #the minimum annual rate that you care about in the original event set (the weight now is the original annual rate / number of epsilons per event)
  demand = bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids_dummies.csv') #we just take a percentage in ita.py, namely  #to get morning flows, take 5.3% of daily driver values. 11.5/(4.5*6+11.5*10+14*4+4.5*4) from Figure S10 of http://www.nature.com/srep/2012/121220/srep01001/extref/srep01001-s1.pdf
  #figure out ground motions
  # lnsas, weights = ground_motions(numeps, tol, 'input/SF2_mtc_total_3909scenarios_1743bridgesPlusBART_1epsFake.txt')
  lnsas, weights, magnitudes = ground_motions(numeps, tol, 'input/SF2_mtc_total_3909scenarios_1743bridgesPlusBART_3eps.txt')
  # with open ('input/20140114_lnsas_1epsFake.pkl', 'wb') as f:
  #   pickle.dump(lnsas, f)
  with open ('input/20140114_magnitudes_3eps.pkl', 'wb') as f:
    pickle.dump(magnitudes, f)
  with open('input/20140114_lnsas_3eps.pkl','rb') as f:
    lnsas = pickle.load(f)
  # with open('input/20140114_lnsas_1epsFake.pkl','rb') as f:
  #   lnsas = pickle.load(f)
  print 'the number of ground motion events we are considering: ', len(lnsas)
  print 'first length: ', len(lnsas[0])

  bart_dict = transit_to_damage.make_bart_dict()
  muni_dict = transit_to_damage.make_muni_dict()

  bridge_array_new = []
  bridge_array_internal = []
  travel_index_times = []
  # G = nx.read_gpickle("input/graphMTC_noCentroidsLength15.gpickle")
  G = get_graph()

  print 'am I a multi graph? I really do not want to be!', G.is_multigraph() #An undirected graph class that can store multiedges. Multiedges are multiple edges between two nodes. Each edge can hold optional data or attributes.A MultiGraph holds undirected edges. Self loops are allowed.
  no_damage_travel_time, no_damage_vmt = compute_tt_vmt(G, demand)
  G = util.clean_up_graph(G)
  # make_directories(range(len(lnsas)))
  # transit_to_damage.set_main_path('input/trn/transit_lines/', 'input/trncopy/transit_lines/') #TODO: need to change THREE file paths (these plus bart)

  # run in SERIES
  #---------------------------------------------
  # targets = [0, 5000]
  # # targets = range(len(lnsas))
  # for i in targets:
  #   print i
  #   start = time.time()
  #   damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges, flow, shortest_paths, travel_time, vmt = compute_performance(lnsas[i], G, i, demand, no_damage_travel_time, no_damage_vmt)
  #   bridge_array_internal.append(damaged_bridges_internal)
  #   bridge_array_new.append(damaged_bridges_new)
  #   travel_index_times.append((i, num_damaged_bridges, flow, shortest_paths, travel_time, vmt, num_damaged_bridges/float(number_of_highway_bridges), magnitudes[i]))
  #   print 'time for one: ', time.time() - start
  #   if i%3909 == 0:
      # save_results(bridge_array_internal, bridge_array_new, travel_index_times, int((i + 1)/float(3909)))
  
  #   # scenario = lnsas[i]
  #   # #figure out bridge damage for each scenario
  #   # damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges = damage_bridges(scenario) #e.g., [1, 89, 598] #num_bridges_out is highway bridges only
  #   # bridge_array_internal.append(damaged_bridges_internal)
  #   # bridge_array_new.append(damaged_bridges_new)

  #   # #figure out network damage and output Cube files to this effect
  #   # G = damage_network(damaged_bridges_internal, damaged_bridges_new, G, time.strftime("%Y%m%d")+'_filesForCube/', i)

  #   # #figure out impact (performance metrics)
  #   # flow, shortest_paths, travel_time, vmt = measure_performance(G, num_damaged_bridges, demand, no_damage_travel_time, no_damage_vmt)
  #   # travel_index_times.append((i, num_damaged_bridges, flow, shortest_paths, travel_time, vmt, num_damaged_bridges/float(number_of_highway_bridges), magnitudes[i]))
  #   # G = util.clean_up_graph(G)
  #   # # if i%3909 == 0:
  #   # if i%1 == 0:
  #   #   save_results(bridge_array_internal, bridge_array_new, travel_index_times, int(i/float(3909)))

  # # #---------------------------------------------

  # # # run in PARALLEL
  # # # #---------------------------------------------
  ppservers = ()    
  # Creates jobserver with automatically detected number of workers
  job_server = pp.Server(ppservers=ppservers)
  print "Starting pp with", job_server.get_ncpus(), "workers"
  start_time = time.time()
  # set up jobs
  jobs = []
  targets = range(3909, len(lnsas)) #len(lnsas)) 7818, 
  # targets = [0, 33, 5000]
  # for i in range(len(lnsas)):
  for i in targets:
    jobs.append(job_server.submit(compute_performance, (lnsas[i], None, i, demand, no_damage_travel_time, no_damage_vmt, ), modules = ('networkx', ))) # functions, modules
  # get results
  # if len(jobs) != len(lnsas):
  #   pdb.set_trace() # error checking!
  index = 0
  for job in jobs:
    (damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges, flow, shortest_paths, travel_time, vmt) = job()
    i = targets[index]
    print 'target id: ', i
    bridge_array_internal.append(damaged_bridges_internal)
    bridge_array_new.append(damaged_bridges_new)
    travel_index_times.append((i, num_damaged_bridges, flow, shortest_paths, travel_time, vmt, num_damaged_bridges/float(number_of_highway_bridges), magnitudes[i]))
    if i%3909 == 0:
      save_results(bridge_array_internal, bridge_array_new, travel_index_times, int((i + 1)/float(3909)))
    index += 1

  # #---------------------------------------------
  save_results(bridge_array_internal, bridge_array_new, travel_index_times, int((i + 1)/float(3909)))