def generate(self, head: Any = None) -> Generator[Tuple[Any, str], None, None]: """Return a generator that yields a new message on every next call. Args: head: User to start the interaction with. If no user supplied, the sender of the earliest message is used. Note, this user will not send a message but will be the user the first generated message responds to. Examples: # Generate 2 messages >>> gen = cc.generate('Alice') >>> for _ in range(2): tpl = next(gen) print(": ".join(tpl)) Bob: Hey, what's up? Charlie: Shuf is fantastic. I tried it on a 78 billion line text file. # Generates an infinite amount of messages >>> for tpl in cc.generate('Alice')(): print(": ".join(tpl)) Bob: Hey, what's up? Charlie: Shuf is fantastic. I tried it on a 78 billion line text file. ... """ user = pykov.Vector({self.messages[0][0] if head is None else head: 1}) # starting point for markov chain while True: choice = ( user * self.Musers).choose() # choose next user to write a message if choice is None: return user = pykov.Vector( {choice: 1}) # create pykov vector for future user calculation if choice in self.word_matrices: Mword = self.word_matrices[choice] else: # create word matrix Mword = pykov.Matrix({(1, 1): 1}) # create matrix for word chain for mes in [m for m in self.messages if m[0] == choice]: # add every word to matrix splt = mes[1].split(' ') if (0, splt[0]) not in Mword: Mword[(0, splt[0])] = 0 Mword[(0, splt[0])] += 1 for word, nxt in zip(splt[:-1], splt[1:]): if (word, nxt) not in Mword: Mword[(word, nxt)] = 0 Mword[(word, nxt)] += 1 if (splt[-1], 1) not in Mword: Mword[(splt[-1], 1)] = 0 Mword[(splt[-1], 1)] += 1 Mword.stochastic() if not self.enhance_speed: self.word_matrices[choice] = Mword C = pykov.Chain(Mword) yield (choice, " ".join(C.walk(self.MAX_WALK_LENGTH, 0, 1)[1:-1]))
def get_succesor(user_id, place_id, time): temp = pk.Vector() day = time.weekday() place = get_common_place_from_db(place_id) for suc in place["successors"]: trip = esdctp.get_common_trip_from_db(user_id, place_id, suc) for temp_hour in range(time.hour, esdctp.HOURS_IN_DAY): counter_key = ("%s" % suc, temp_hour) temp[counter_key] = trip.probabilites[day, temp_hour] return boi.ObjectId(temp.choose())
def get_normalized_times(lst_of_trips): counter = pk.Vector() i = 0 for trip in lst_of_trips: counter[i] = get_time_of_trip(trip) i += 1 counter.normalize() to_return = [] for i in range(len(lst_of_trips)): to_return.append(counter[i]) return to_return
def setFirstLocationAndWhen(self, first_location, first_when): self.current_cell = first_location self.current_when = first_when self.where_inputs.append(self.current_cell) self.when_inputs.append(self.current_when) self.counter_vector = pykov.Vector({self.current_cell: 1}) self.normalized_vector = self.counter_vector.copy() self.T = pykov.Chain({(self.current_cell, self.current_cell): 1}) self.learn_max_probable_cells_from_each_cells( self.most_probable_transition_matrix, self.t)
def get_normalized_beauty(lst_of_trips): counter = pk.Vector() i = 0 for trip in lst_of_trips: factor = get_beauty_score_of_trip(trip) print("beauty_factor : %s" % factor) counter[i] = factor i += 1 counter.normalize() to_return = [] for i in range(len(lst_of_trips)): to_return.append(counter[i]) return to_return
def get_normalized_sweat(lst_of_trips, testing=False): counter = pk.Vector() i = 0 for trip in lst_of_trips: factor = get_sweat_factor(trip, testing) print("sweat_factor : %s" % factor) counter[i] = factor i += 1 counter.normalize() to_return = [] for i in range(len(lst_of_trips)): to_return.append(counter[i]) return to_return
def main(): # N = 100 # B = list(range(0, int(N/2), 1)) # C1 = [int((N - i)/2) for i in B] # C2 = [N - B[i] - C1[i] for i in range(len(B))] # k = 20 # alpha = list(range(int(k/2), k+1)) N = 100 B = 20 C1 = 40 C2 = 40 k = 20 alpha = 15 states = [] for i in range(int((C1 + C2) / 2), C1 + C2 + 1): states.append((i, C1 + C2 - i)) chain = pykov.Chain() for i in range(1, len(states) - 1): # print(states[i]) # Prob of going up is prob choosing red and getting maj blue up = (states[i][0] / (C1 + C2)) * hyper(N, states[i][1], k, alpha) # Prob of going down is prob of choosing blue down = (states[i][1] / (C1 + C2)) * hyper(N, states[i][0], k, alpha) # Prob of staying is prob of choosing blue and getting maj blue # or choosing red and getting maj red # stay = ((states[i][0]/(C1+C2))*hyper(N, states[i][0], k, alpha))+((states[i][1]/(C1+C2))*hyper(N, states[i][1], k, alpha)) stay = 1 - up - down chain[(str(states[i]), str(states[i - 1]))] = up chain[(str(states[i]), str(states[i + 1]))] = down chain[(str(states[i]), str(states[i]))] = stay chain[(str(states[0]), str( states[0]))] = 1 - (states[0][1] / (C1 + C2)) * hyper(N, states[0][0], k, alpha) chain[(str(states[0]), str(states[1]))] = (states[0][1] / (C1 + C2)) * hyper(N, states[0][0], k, alpha) chain[(str(states[len(states) - 1]), str( states[len(states) - 1]))] = 1 - (states[i][0] / (C1 + C2)) * hyper(N, states[i][1], k, alpha) chain[(str(states[len(states) - 1]), str(states[len(states) - 2]))] = (states[i][0] / (C1 + C2)) * hyper(N, states[i][1], k, alpha) # for state_i in states: # for state_j in states: for k in chain: print(k, chain[k]) p = pykov.Vector({str((40, 40)): 1}) print(chain.pow(p, 300000))
def markov_update_words(pykov_words, association, reward): pykov_words[association] = pykov_words[association] + reward summ = sum(pykov_words.values()) normalized_probabilities = list( map(lambda i: float(i) / summ, pykov_words.values())) for i in range(len(normalized_probabilities)): if normalized_probabilities[i] < 4.768371584931426e-04: normalized_probabilities[i] = 0.001 normalized_probabilities = OrderedDict( zip(pykov_words.keys(), normalized_probabilities)) return pykov.Vector(normalized_probabilities)
def mobility_prediction(pykov_chain, current_cell_id, current_date, current_time, predicted_date, predicted_time): initial_minute_of_day = support.calc_minute_of_day(current_time) initial_day = support.calc_day(current_date) initial_key = UserEntry(initial_day, initial_minute_of_day, current_cell_id).to_pykov_key() initial_pykov_vector = pykov.Vector({initial_key: 1}) difference_in_seconds = ( datetime.datetime.combine(predicted_date, predicted_time) - datetime.datetime.combine(current_date, current_time)).total_seconds() difference_in_minutes = int(difference_in_seconds / 60) distribution_dict = pykov_chain.pow(initial_pykov_vector, difference_in_minutes) print 'eeeeeee', distribution_dict return distribution_dict
def markov_chain(states, transitions, policy): import pykov T = pykov.Chain() q = Queue.Queue() visited = [False] * len(states) visited[0] = True q.put(0) start = pykov.Vector({states[0]: 1}) while not q.empty(): state_idx = q.get() pol = policy[state_idx] for i, p in enumerate(transitions[pol][state_idx]): if p > 0: if i == len(states): T[(states[state_idx], "exit")] = p T[("exit", "exit")] = 1 else: T[(states[state_idx], states[i])] = p if not visited[i]: q.put(i) visited[i] = True return T, start
def mp(): transiant_num = 10 thr_user_number = 5 #get transiant_num and thr_user_number from input ## set time and data using system time Day = datetime.date.today().strftime("%A") Time = time.strftime("%H:%M") current_time = Time # start a while loop to calculate and save the results in 'ICNoutput' folder till time 24:00-transiant_num current_time_in_min = sum( int(x) * 60**i for i, x in enumerate(reversed(current_time.split(':')))) ##$print current_time_in_min #while (sum(int(x) * 60 ** i for i,x in enumerate(reversed(current_time.split(':'))))<(1440-transiant_num)): ##$print ' ' ##$print '**********Group Mobility Prediction is runing *****************' ##$print 'transiant time is : ', transiant_num ##$print 'thresh old for user number is :', thr_user_number ##$print 'Current date is: ', Day , ' & Currer time is : ', Time #print 'Currer day is : ', +Day ' & Current time is: ', +Time ##$print '****************************************************************' ##$print "------The Calculation of the Group Mobility Prediction based on the Trajectory Trace Files------" #print 'Current time :' +Time #print 'Current date :' +Day if Day == 'Saturday': current_day = 'sat' elif Day == 'Sunday': current_day = 'sun' elif Day == 'Monday': current_day = 'mon' elif Day == 'Tuesday': current_day = 'tue' elif Day == 'Wednesday': current_day = 'wed' elif Day == 'Thursday': current_day = 'thu' elif Day == 'Friday': current_day = 'fri' #shutil.rmtree('/home/ubuntu/mobaas/Groupeoutput') if not os.path.exists('/home/ubuntu/mobaas/Groupeoutput'): #os.makedirs('/home/ubuntu/mobaas/Groupeoutput') print "Create the folder Groupeoutput" return 0 folder = '/home/ubuntu/mobaas/Groupeoutput' for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) if os.path.isfile(file_path): if "threshold_cell_users.txt" not in file_path: os.unlink(file_path) #elif os.path.isdir(file_path): shutil.rmtree(file_path) all_users_start_move_cell_prob = [ ] # a matrix to save all_users_start_move_cell_prob f8 = open( '/home/ubuntu/mobaas/Groupeoutput/all_users_start_move_cell_prob.txt', 'a') for file in dirs: user_id = int(file) #print '...................' ##$print 'User ID :' , user_id ##$print '...................' #transiant_num =20 # after how many minutes! path = '/home/ubuntu/mobaas/test_data/Trace_Files/' '%d%s' % ( user_id, '/Step_1') number_of_files_all_day = len([ item for item in os.listdir(path) if os.path.isfile(os.path.join(path, item)) ]) #total number of files #print number_of_files_all_day total_input_file = number_of_files_all_day / 7 #total input trace file for each day #print total_input_file file_num = int( total_input_file * 0.8 ) # 80% number of the input traced files for each week day for estimation #print file_num total_file_num = 1 * file_num # number of total input traced files array_of_traces = [ ] # an empty array to input traced files as matrix for all dayes #print " " #print " " #print "------The Calculation of the Probabilities based on the Trajectory Trace Files -------" ## bades on the date read the trace files for specifie day ##Saturdays if current_day == 'sat': sat_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Saturday for i in range(0, file_num): sat_trace_file = numpy.loadtxt( '%s/Sat_%d.dat' % (path, i)) #read the traced files(Saturday) size = len(sat_trace_file) # size of trace files step sat_array_of_traces[ i] = sat_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = sat_array_of_traces #print 'Current day is :' , current_day ##Sundays elif current_day == 'sun': sun_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Sundays for i in range(0, file_num): sun_trace_file = numpy.loadtxt( '%s/Sun_%d.dat' % (path, i)) #read the traced files(Sundays) size = len(sun_trace_file) # size of trace files step sun_array_of_traces[ i] = sun_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = sun_array_of_traces #print 'Current day =' , current_day ##Mondays elif current_day == 'mon': mon_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Monday for i in range(0, file_num): mon_trace_file = numpy.loadtxt( '%s/Mon_%d.dat' % (path, i)) #read the traced files(Monday) size = len(mon_trace_file) # size of trace files step mon_array_of_traces[ i] = mon_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = mon_array_of_traces #print 'Current day =' , current_day ##Tusedays elif current_day == 'tue': tue_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Tuseday for i in range(0, file_num): tue_trace_file = numpy.loadtxt( '%s/Tue_%d.dat' % (path, i)) #read the traced files(Tuseday) size = len(tue_trace_file) # size of trace files step tue_array_of_traces[ i] = tue_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = tue_array_of_traces #print 'Current day =', current_day ##Wednesdays elif current_day == 'wed': wed_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Wednesday for i in range(0, file_num): wed_trace_file = numpy.loadtxt( '%s/Wed_%d.dat' % (path, i)) #read the traced files(Wednesday) size = len(wed_trace_file) # size of trace files step wed_array_of_traces[ i] = wed_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = wed_array_of_traces #print 'Current day =' , current_day ##Thursdays elif current_day == 'thu': thu_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Thursday for i in range(0, file_num): thu_trace_file = numpy.loadtxt( '%s/Thu_%d.dat' % (path, i)) #read the traced files(Thursday) size = len(thu_trace_file) # size of trace files step thu_array_of_traces[ i] = thu_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = thu_array_of_traces #print 'Current day =' , current_day ##Fraidays elif current_day == 'fri': fri_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as for all Friday for i in range(0, file_num): fri_trace_file = numpy.loadtxt( '%s/Fri_%d.dat' % (path, i)) #read the traced files(Friday) size = len(fri_trace_file) # size of trace files step fri_array_of_traces[ i] = fri_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = fri_array_of_traces #print 'Current day =' , current_day #********************************************************************************************************************* ##count number of visited cell arry_of_temp_cell = [ 0 ] * total_file_num #an array to put the cell ID in each step arry_of_number_cell = [ 0 ] * size #an array to count the number of visited cell ID arry_of_number_cell_copy = [ 0 ] * size #an array to count the number of visited cell ID for j in range(0, size): for i in range(0, total_file_num): arry_of_temp_cell[i] = array_of_traces[i][j][1] arry_of_number_cell[j] = [[x, arry_of_temp_cell.count(x)] for x in set(arry_of_temp_cell)] arry_of_temp_cell[i] = array_of_traces[i][j][1] arry_of_number_cell_copy[j] = [[x, arry_of_temp_cell.count(x)] for x in set(arry_of_temp_cell)] ##count percentage of visited cell arry_of_percentage_cell = arry_of_number_cell_copy #an array to calculate the percentage for each visited cell for j in range(0, size): sum_cell = 0. for n in range(0, len(arry_of_percentage_cell[j])): sum_cell += arry_of_percentage_cell[j][n][ 1] #calculate all visited cell for n in range(0, len(arry_of_percentage_cell[j])): arry_of_percentage_cell[j][n][1] = arry_of_percentage_cell[j][ n][1] / sum_cell #calculate the percentage for each visited cell ##make an array to save time,percentage and cell ID arry_of_time_percentage_cell = arry_of_percentage_cell #an array to save [time_cellID, percentage] for j in range(0, ): for n in range(0, len(arry_of_percentage_cell[j])): arry_of_time_percentage_cell[j][n][0] = [ '%d%s%d' % (array_of_traces[0][j][0], '-', arry_of_percentage_cell[j][n][0]) ] #********************************************************************************************************************* #open file to save the state file (state file is calculated from this file that has non zero transient probability) f1 = open('/home/ubuntu/mobaas/Groupeoutput/statefile.txt', 'w') f1 = open('/home/ubuntu/mobaas/Groupeoutput/statefile.txt', 'a') ##calculate the percentage of going from once cell to other cell in each step #matrix_of_number_next_cell_percentage=[0]*t for j in range(0, size - 2): for n in range( 0, len(arry_of_number_cell[j]) ): #an array which has the number of visited cell in each step #print (n) arry_of_temp_next_cell = [] arry_of_number_next_cell = [] arry_of_number_next_cell_copy = [] temp = arry_of_number_cell[j][n][ 0] # temp get each of the visited cell for i in range(0, total_file_num): if array_of_traces[i][j][1] == temp: #an array to save visited cell in the next step arry_of_temp_next_cell.append(array_of_traces[i][j + 1][1]) #an array which has the number of visited cell in the next step arry_of_number_next_cell = [[ x, arry_of_temp_next_cell.count(x) ] for x in set(arry_of_temp_next_cell)] #make a copy arry_of_number_next_cell_copy = [[ x, arry_of_temp_next_cell.count(x) ] for x in set(arry_of_temp_next_cell)] arry_of_number_next_cell_percentage = arry_of_number_next_cell_copy #calculate the percentage of going to the next cell sum_next_cell = 0. for n in range( 0, len(arry_of_number_next_cell_percentage)): sum_next_cell += arry_of_number_next_cell_percentage[ n][1] #print(sum_next_cell) for n in range( 0, len(arry_of_number_next_cell_percentage)): arry_of_number_next_cell_percentage[n][ 1] = arry_of_number_next_cell_percentage[n][ 1] / sum_next_cell #print(sum_next_cell) #save the the transient probability in state file for m in range(0, len(arry_of_number_next_cell_percentage)): f1.write('%s%d' % ('c', array_of_traces[0][j][0])) f1.write('%s%d ' % ('_', temp)) f1.write('%s%d' % ('c', array_of_traces[0][j + 1][0])) f1.write('%s%d %.3f \n' % ('_', arry_of_number_next_cell_percentage[m][0], arry_of_number_next_cell_percentage[m][1])) f1.close() #********************************************************************************************************************* ##calculate the transient probability state_mat = pykov.readmat( '/home/ubuntu/mobaas/Groupeoutput/statefile.txt') #print len(state_mat) #print state_mat.keys() ## change hh:mm to time step (e.g, 06:21-->381 ) current_time_setp = sum( int(x) * 60**i for i, x in enumerate(reversed(current_time.split(':')))) #current_time_setp = current_time_setp - transiant_num # set the start time "transiant_num" ago to from current time to start M.C current_time_setp = current_time_setp # set the start time "transiant_num" ago to from current time to start M.C #print current_time_setp #print len(state_mat) #Clear the cellprobabilityfile.txt for new calculation f4 = open('/home/ubuntu/mobaas/Groupeoutput/cellprobabilityfile.txt', 'w') f4.close() #check the "current_time_setp - transiant_num" in state_mat to find the valid cell on that time for i in range(0, len(state_mat)): if int(state_mat.keys()[i][0] [1:state_mat.keys()[i][0].index('_')]) == current_time_setp: if int(state_mat.keys()[i][0] [state_mat.keys()[i][0].index('_') + 1:]) != 0: ##$print '**************************************************************************************' #print state_mat.keys()[i][0] current_time_cell = state_mat.keys()[i][0] #verey important point here is that now "current_cell" is not the cell at current time, #here it shows the cell that user was ther at "current_time_setp -transiant_num " current_cell = int(state_mat.keys()[i][0] [state_mat.keys()[i][0].index('_') + 1:]) #print current_time_cell #print current_cell ## make time step and cell as 'c381_5384 ' initial_state = pykov.Vector([(current_time_cell, 1)]) #print initial_state next_cell_probability = state_mat.pow( initial_state, transiant_num) f3 = open('/home/ubuntu/mobaas/Groupeoutput/state_mat.txt', 'w') for i in range(0, len(state_mat.values())): f3.write('%s %f \n' % (state_mat.keys()[i], state_mat.values()[i])) f3.close() #the result file x = [] cell_name = [] y = [] #return next_cell_probability ## print the caclulation of probability in the screen and save it in the "cellprobabilityfile.txt" file, this file only show the probability of moving fron start cell to current cell ##$print "Starting time for estimation = "'{:02d}:{:02d}'.format(*divmod(current_time_setp, 60)), " , User is in cell = ", (current_cell) ##$print "......................................................................................" #f4 = open('/home/ubuntu/mypath/groupprediction/cellprobabilityfile.txt', 'w') f4 = open( '/home/ubuntu/mobaas/Groupeoutput/cellprobabilityfile.txt', 'a') for i in range(0, len(next_cell_probability.values())): x.append(i + 1) cell_name.append(next_cell_probability.keys()[i]) y.append(next_cell_probability.values()[i]) # f4.write('%s %f \n' % (next_cell_probability.keys()[i],next_cell_probability.values()[i])) temp_next_cell = next_cell_probability.keys()[i] #f4.write('%s %s %s %f \n' % (current_cell, '{:02d}:{:02d}'.format(*divmod(int(temp_next_cell[1:temp_next_cell.index('_')]), 60)), temp_next_cell[temp_next_cell.index('_') + 1:], next_cell_probability.values()[i]) ) #f4.write('%s %s %f \n' % (current_cell, temp_next_cell[temp_next_cell.index('_') + 1:], next_cell_probability.values()[i]) ) #f4.write('\n------------------') if temp_next_cell[temp_next_cell.index('_') + 1:] != '0': f4.write( '%s %s %f \n' % (current_cell, temp_next_cell[temp_next_cell.index('_') + 1:], next_cell_probability.values()[i])) ##$print 'Next Time = ' '{:02d}:{:02d}'.format(*divmod(int(temp_next_cell[1:temp_next_cell.index('_')]), 60)),' , Current Cell = ', temp_next_cell[temp_next_cell.index('_') + 1:],' , The Probability = ', next_cell_probability.values()[i] ##$elif temp_next_cell[temp_next_cell.index('_') + 1:] == '0': ##$print 'Next Time = ' '{:02d}:{:02d}'.format(*divmod(int(temp_next_cell[1:temp_next_cell.index('_')]), 60)),' , Current Cell = ', temp_next_cell[temp_next_cell.index('_') + 1:],'(Not trace data)',' , The Probability = ', next_cell_probability.values()[i] #f4.write('--------\n') f4.close() # load cellprobabilityfile in a matrix prob_mat = numpy.loadtxt( '/home/ubuntu/mobaas/Groupeoutput/cellprobabilityfile.txt') #print 'original prob_mat : ',prob_mat #print'...................' number_of_element_prob_mat = numpy.count_nonzero(prob_mat) #print 'number of element in original prob_mat', number_of_element_prob_mat #print'...................' #find unique row in prob_mat def unique_rows(a): a = numpy.ascontiguousarray(a) unique_a = numpy.unique(a.view([('', a.dtype)] * a.shape[1])) return unique_a.view(a.dtype).reshape( (unique_a.shape[0], a.shape[1])) if number_of_element_prob_mat > 3: prob_mat = unique_rows(prob_mat) #print 'unic prob_mat',prob_mat #print'...................' number_of_element_prob_mat = numpy.count_nonzero(prob_mat) #print 'number of element in unic prob_mat', number_of_element_prob_mat # check if prob_mat dose not have only one raw(it has three elements), inorder to find unic raw in it if number_of_element_prob_mat == 3: #print 'number of raw in prob_mat', raw_of_prob_mat new_prob_mat_1 = numpy.append(prob_mat, 0) new_prob_mat_2 = numpy.append(new_prob_mat_1, 0) #print 'now prob_mat', new_prob_mat_2 prob_mat_extend = new_prob_mat_2 #new = numpy.append(prob_mat, 0) #new1 = numpy.append(new, 0) #raw_of_punique_rows_prob_mat = len(unique_rows_prob_mat) #print 'number of raw in raw_of_punique', raw_of_punique_rows_prob_mat #prob_mat_extend= np.c_[ unique_rows_prob_mat, 0, 0 ] #unique_rows_prob_mat.extend([0, 0]) #C =[0]*2 #prob_mat_extend= np.c_[ unique_rows_prob_mat, np.zeros(1), np.zeros(1) ] #print prob_mat_extend #print len(prob_mat_extend) size_of_prob_mat_extend = 1 #print size_of_prob_mat_extend elif number_of_element_prob_mat != 3: #print 'number of raw in prob_mat', raw_of_prob_mat unique_rows_prob_mat = unique_rows(prob_mat) raw_of_punique_rows_prob_mat = len(unique_rows_prob_mat) #print 'number of raw in raw_of_punique', raw_of_punique_rows_prob_mat prob_mat_extend = np.c_[unique_rows_prob_mat, np.zeros(raw_of_punique_rows_prob_mat), np.zeros(raw_of_punique_rows_prob_mat)] #print prob_mat_extend size_of_prob_mat_extend = len(prob_mat_extend) #print size_of_prob_mat_extend # calculate the probability of starting from one specific cell count_start_cell_numb = Counter(unique_rows_prob_mat[:, 0]) #print 'count_start_cell_numb', count_start_cell_numb uniq_start_cell_numb = 1. * len(unique_rows_prob_mat[:, 0]) #print 'uniq_start_cell_numb', uniq_start_cell_numb count_start_cell_perc = numpy.zeros(shape=(len(count_start_cell_numb), 2)) for i in range(0, len(count_start_cell_numb)): count_start_cell_perc[i, 0] = count_start_cell_numb.keys()[i] count_start_cell_perc[ i, 1] = count_start_cell_numb.values()[i] / uniq_start_cell_numb #print count_start_cell_perc[i,0] #print count_start_cell_perc[i,1] #print count_start_cell_perc for i in range(0, len(count_start_cell_perc)): temp_cell = count_start_cell_perc[i, 0] temp_prob = count_start_cell_perc[ i, 1] #probability that user was in the start cell #for j in range(0,len(prob_mat_extend[:,0])): if size_of_prob_mat_extend == 1: #print 'in size 1 :', size_of_prob_mat_extend #print 'prob_mat_extend =', prob_mat_extend #print 'prob_mat_extend[0]=', prob_mat_extend[2] #for j in range(0,size_of_prob_mat_extend): if prob_mat_extend[0] == temp_cell: prob_mat_extend[ 3] = temp_prob #probability that user was in the start cell prob_mat_extend[4] = prob_mat_extend[3] * prob_mat_extend[ 2] #probability that user was in the start cell and move to the current cell else: #print 'in size more than 1:', size_of_prob_mat_extend for j in range(0, size_of_prob_mat_extend): if prob_mat_extend[j, 0] == temp_cell: prob_mat_extend[ j, 3] = temp_prob #probability that user was in the start cell prob_mat_extend[ j, 4] = prob_mat_extend[j, 3] * prob_mat_extend[ j, 2] #probability that user was in the start cell and move to the current cell #find unique row in prob_mat_extend def unique_rows(a): a = numpy.ascontiguousarray(a) unique_a = numpy.unique(a.view([('', a.dtype)] * a.shape[1])) return unique_a.view(a.dtype).reshape( (unique_a.shape[0], a.shape[1])) if size_of_prob_mat_extend != 1: unique_rows_prob_mat_extend = unique_rows(prob_mat_extend) #useID_add_prob_mat_extend = prob_mat_extend useID_add_prob_mat_extend = unique_rows_prob_mat_extend useID_add_prob_mat_extend = np.insert(useID_add_prob_mat_extend, 0, values=user_id, axis=1) ##numpy.savetxt('/home/ubuntu/mobaas/Groupeoutput/users_startcells_movecell_probability_%d.txt' % (user_id), useID_add_prob_mat_extend ,fmt='%.4f') numpy.savetxt(f8, useID_add_prob_mat_extend, fmt='%.4f') if size_of_prob_mat_extend == 1: unique_rows_prob_mat_extend = prob_mat_extend useID_add_prob_mat_extend = unique_rows_prob_mat_extend useID_add_prob_mat_extend = np.insert(useID_add_prob_mat_extend, 0, user_id) #useID_add_prob_mat_extend = useID_add_prob_mat_extend.T ##numpy.savetxt('/home/ubuntu/mobaas/Groupeoutput/users_startcells_movecell_probability_%d.txt' % (user_id), useID_add_prob_mat_extend[None] ,fmt='%.4f') numpy.savetxt(f8, useID_add_prob_mat_extend[None], fmt='%.4f') #np.savetxt(f,a) f8.close() ## in this matrix: the rows are : userID-start cell-visited cell-pobability to move to visited cell(P1)-pobalility that user was in start cell(P2)-P1*P2 #numpy.savetxt('/home/ubuntu/mypath/groupprediction/Groupeoutput/users_startcells_movecell_probability_%d.txt' % (user_id), useID_add_prob_mat_extend ,fmt='%.4f') #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ all_users_start_move_cell_prob = numpy.loadtxt( '/home/ubuntu/mobaas/Groupeoutput/all_users_start_move_cell_prob.txt') #all_users_start_move_cell_prob is sorted based on row 2 (move cell) to find all users that move to 'move cell' sorted_all_users_start_move_cell_prob = all_users_start_move_cell_prob[ all_users_start_move_cell_prob[:, 2].argsort()] numpy.savetxt( '/home/ubuntu/mobaas/Groupeoutput/sorted_all_users_start_move_cell_prob.txt', sorted_all_users_start_move_cell_prob, fmt='%.4f') #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% # count how many number of users(with unic ID) are moved to 'move cell' count_user_in_move_cell = Counter(sorted_all_users_start_move_cell_prob[:, 2]) for i in range(0, len(count_user_in_move_cell)): temp_mat = sorted_all_users_start_move_cell_prob[numpy.where( sorted_all_users_start_move_cell_prob[:, 2] == count_user_in_move_cell.keys()[i])] #print 'temp_mat',temp_mat[0,2] unic_user = Counter(temp_mat[:, 0]) unic_user_number = len(unic_user) #print 'unic_user', unic_user_number count_user_in_move_cell[temp_mat[0, 2]] = unic_user_number #print 'count_user_in_move_cell.values()[i]', count_user_in_move_cell.values()[i] #print 'count_user_in_move_cell second', count_user_in_move_cell #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% # make another matrix to save the probabilites of users(for all users P1*P2) count_user_in_move_cell_extend = numpy.zeros( shape=(len(count_user_in_move_cell), 4)) for i in range(0, len(count_user_in_move_cell)): count_user_in_move_cell_extend[i, 0] = count_user_in_move_cell.keys()[i] count_user_in_move_cell_extend[i, 1] = count_user_in_move_cell.values()[i] #print 'number is user is : ' count_user_in_move_cell.values()[i] # sort the 'count_user_in_move_cell_extend' based on number of users sorted_count_user_in_move_cell_extend = count_user_in_move_cell_extend[ count_user_in_move_cell_extend[:, 1].argsort()] #print 'sorted_count_user_in_move_cell_extend :' , sorted_count_user_in_move_cell_extend Temp_time = '{:02d}:{:02d}'.format( *divmod(int(temp_next_cell[1:temp_next_cell.index('_')]), 60)) ##$print ' ' ##$print '******** Notification******' ##$print 'It is estimated at time (',Temp_time,') number of users in following cell will be more than defined threshold!' numpy.savetxt( '/home/ubuntu/mobaas/Groupeoutput/sorted_count_user_in_move_cell_extend.txt', sorted_count_user_in_move_cell_extend, fmt='%.4f') f9 = open('/home/ubuntu/mobaas/Groupeoutput/threshold_cell_users.txt', 'w') #f9 = open('/home/ubuntu/mobaas/Groupeoutput/threshold_cell_users.txt', 'a') for i in range(0, len(sorted_count_user_in_move_cell_extend)): if sorted_count_user_in_move_cell_extend[i, 1] > thr_user_number: ##$print 'In cell (', sorted_count_user_in_move_cell_extend[i,0], ') --> ', sorted_count_user_in_move_cell_extend[i,1] , 'users' f9.write('%s %s %s \n' % (Temp_time, sorted_count_user_in_move_cell_extend[i, 0], sorted_count_user_in_move_cell_extend[i, 1])) f9.close() ##################### # list for time, cell, number of users threshold_cell_users = [ line.strip() for line in open( '/home/ubuntu/mobaas/Groupeoutput/threshold_cell_users.txt') ] print threshold_cell_users #################### for i in range(0, len(sorted_count_user_in_move_cell_extend)): temp_cell = sorted_count_user_in_move_cell_extend[i, 0] #print temp_cell temp_prob_1 = 1. temp_prob_2 = 1. n = 0. for j in range(0, len(sorted_all_users_start_move_cell_prob)): if temp_cell == sorted_all_users_start_move_cell_prob[j, 2]: n = n + 1 temp_prob_1 = temp_prob_1 * sorted_all_users_start_move_cell_prob[ j, 5] + 0.0000001 #if temp_cell != sorted_all_users_start_move_cell_prob[j,2]: # n=n+1 # temp_prob_2=temp_prob_2*(1-sorted_all_users_start_move_cell_prob[j,5]) #print n #print 'temp_prob_1 =' , temp_prob_1 #print 'temp_prob_2 =' , temp_prob_2 sorted_count_user_in_move_cell_extend[i, 2] = (temp_prob_1 * temp_prob_2) * 100 #print '(temp_prob_1*temp_prob_2)*100 = x=', (temp_prob_1*temp_prob_2)*100 #sorted_count_user_in_move_cell_extend[i,3]=math.log10(temp_prob_1*temp_prob_2) sorted_count_user_in_move_cell_extend[i, 3] = log10(temp_prob_1 * temp_prob_2) numpy.savetxt( '/home/ubuntu/mobaas/Groupeoutput/sorted_count_user_in_move_cell_extend.txt', sorted_count_user_in_move_cell_extend, fmt='%.4f') '''
str_states = ['S', 'I', 'H', 'U', 'O', 'R'] states = dict(zip(list(range(1, len(a) + 1)), str_states)) dic = {(states[i + 1], states[j + 1]): a[i][j] for i in range(len(a)) for j in range(len(a[i]))} M = pykov.Matrix(dic) print(M, end='\n\n') # Todos os estados print(M.states(), end='\n\n') # Antecessores de cada estado print(M.pred(), end='\n\n') # Sucessores de cada estado print(M.succ(), end='\n\n') C = pykov.Chain(dic) state = pykov.Vector(S=1) # Distribuição de prob após N dias, começando no estado state n_inicio = 282 # dias, entre o dia do primeiro infetado em Portugal e o dia da submissão do projeto dia274 = C.pow(state, n_inicio) n_fim = 365 - (31 + 28 + 1) fim2020 = C.pow(state, n_fim) # % aumento de recuperados esperado entre 09/12/2020 e 01/01/2021 rate_r = (fim2020 - dia274)['R'] * 100 # média de recuperados diários esperada entre 09/12/2020 e 01/01/2021 pop = 10.28 * 10**6 dias = n_fim - n_inicio avg_r_daily = (rate_r / 100 * pop / dias) print('rate_r {}\navg_r_daily {}\n'.format(round(rate_r, 2), round(avg_r_daily)))
# Instantiate a MIDI Track (contains a list of MIDI events) track = midi.Track() # Append the track to the pattern pattern.append(track) # Instantiate a MIDI note on event, append it to the track notes = {'A_3' : midi.A_3, 'B_3' : midi.B_3, 'C_3' : midi.C_3, 'D_3' : midi.D_3, 'E_3' : midi.E_3, 'F_3' : midi.F_3, 'G_3' : midi.G_3 } p = pykov.Vector({'G_3':.3, 'B_3':.5, 'A_3':.2}) seq=[] for i in range(0,10): seq.append(p.choose()) print 'seq',seq ticks = [0,10,20,30,40,50,60,70,80,90] print len(seq) print len(ticks) for note, t in zip(seq, ticks): on = midi.NoteOnEvent(tick=t, velocity=20, pitch=notes[note]) track.append(on) # Instantiate a MIDI note off event, append it to the track off = midi.NoteOffEvent(tick=t+10, pitch=notes[note]) track.append(off)
def normalize_sounds(self): counter = pk.Vector() for k,v in self.time_to_noise.items(): counter[k] = v counter.normalize() self.time_to_noise = counter
def get_pykov_vector(self): factor = 1.0 / sum(self.states.values()) for k in self.states: self.states[k] = self.states[k] * factor p = pykov.Vector(self.states) return p
def mp(user_id, current_time, current_day, transiant_num): # Dict to store all the individual results, to be returned at the end of the code result = {} ## print current input time and dat print '********************************************************************************' print 'Current time is: ', current_time, ' & Currer day is : ', current_day print '********************************************************************************' ## set time and data using system time Day = current_day if Day == 'Saturday': current_day = 'sat' elif Day == 'Sunday': current_day = 'sun' elif Day == 'Monday': current_day = 'mon' elif Day == 'Tuesday': current_day = 'tue' elif Day == 'Wednesday': current_day = 'wed' elif Day == 'Thursday': current_day = 'thu' elif Day == 'Friday': current_day = 'fri' path_user = '******' dirs = os.listdir(path_user) # remove the 'ICNoutput' and create a new one to save results shutil.rmtree('./mobaas/ICNoutput') if not os.path.exists('./mobaas/ICNoutput'): os.makedirs('./mobaas/ICNoutput') # start a while loop to calculate and save the results in 'ICNoutput' folder till time 24:00-transiant_num current_time_in_min = sum( int(x) * 60**i for i, x in enumerate(reversed(current_time.split(':')))) #while (sum(int(x) * 60 ** i for i,x in enumerate(reversed(current_time.split(':'))))<(1440-transiant_num)): #stop prediction at 18:40, for the purpose of demo to limit the period of prediction while (sum( int(x) * 60**i for i, x in enumerate(reversed(current_time.split(':')))) < (1130 - transiant_num)): #change current time to min, add ransiant_num to it, change to xx:yy format, put in Temp_time current_time_in_min = sum( int(x) * 60**i for i, x in enumerate(reversed(current_time.split(':')))) next_time_in_min = current_time_in_min + transiant_num Temp_time = '{:02d}:{:02d}'.format(*divmod(next_time_in_min, 60)) #open and save the result in an new file f4 = open( './mobaas/ICNoutput/cellprobability_file_ICN_%s.txt' % Temp_time, 'w') f4 = open( './mobaas/ICNoutput/cellprobability_file_ICN_%s.txt' % Temp_time, 'a') f4.write('%s %s \n' % (Day, current_time)) f4.write('%s \n' % ('---------------------------------------------------')) f4.write('%s \n' % ('user--current cell--next time--next cell--probability')) f4.write('%s \n' % ('---------------------------------------------------')) for i in range(0, len(dirs)): user_id = int(dirs[i]) ##print 'Current user : '******'./mobaas/test_data/Trace_Files/' '%d%s' % (user_id, '/Step_1') number_of_files_all_day = len([ item for item in os.listdir(path) if os.path.isfile(os.path.join(path, item)) ]) #total number of files #print number_of_files_all_day total_input_file = number_of_files_all_day / 7 #total input trace file for each day #print total_input_file file_num = int( total_input_file * 0.7 ) # 70% number of the input traced files for each week day for estimation #print file_num total_file_num = 1 * file_num # number of total input traced files array_of_traces = [ ] # an empty array to input traced files as matrix for all dayes ## bades on the date read the trace files for specifie day ##Saturdays if current_day == 'sat': sat_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Saturday for i in range(0, file_num): sat_trace_file = numpy.loadtxt( '%s/Sat_%d.dat' % (path, i)) #read the traced files(Saturday) size = len(sat_trace_file) # size of trace files step sat_array_of_traces[ i] = sat_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = sat_array_of_traces #print 'Current day is :' , current_day ##Sundays elif current_day == 'sun': sun_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Sundays for i in range(0, file_num): sun_trace_file = numpy.loadtxt( '%s/Sun_%d.dat' % (path, i)) #read the traced files(Sundays) size = len(sun_trace_file) # size of trace files step sun_array_of_traces[ i] = sun_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = sun_array_of_traces #print 'Current day =' , current_day ##Mondays elif current_day == 'mon': mon_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Monday for i in range(0, file_num): mon_trace_file = numpy.loadtxt( '%s/Mon_%d.dat' % (path, i)) #read the traced files(Monday) size = len(mon_trace_file) # size of trace files step mon_array_of_traces[ i] = mon_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = mon_array_of_traces #print 'Current day =' , current_day ##Tusedays elif current_day == 'tue': tue_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Tuseday for i in range(0, file_num): tue_trace_file = numpy.loadtxt( '%s/Tue_%d.dat' % (path, i)) #read the traced files(Tuseday) size = len(tue_trace_file) # size of trace files step tue_array_of_traces[ i] = tue_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = tue_array_of_traces #print 'Current day =', current_day ##Wednesdays elif current_day == 'wed': wed_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Wednesday for i in range(0, file_num): wed_trace_file = numpy.loadtxt( '%s/Wed_%d.dat' % (path, i)) #read the traced files(Wednesday) size = len(wed_trace_file) # size of trace files step wed_array_of_traces[ i] = wed_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = wed_array_of_traces #print 'Current day =' , current_day ##Thursdays elif current_day == 'thu': thu_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as matrix for all Thursday for i in range(0, file_num): thu_trace_file = numpy.loadtxt( '%s/Thu_%d.dat' % (path, i)) #read the traced files(Thursday) size = len(thu_trace_file) # size of trace files step thu_array_of_traces[ i] = thu_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = thu_array_of_traces #print 'Current day =' , current_day ##Fraidays elif current_day == 'fri': fri_array_of_traces = [ 0 ] * file_num # an empty array to input traced files as for all Friday for i in range(0, file_num): fri_trace_file = numpy.loadtxt( '%s/Fri_%d.dat' % (path, i)) #read the traced files(Friday) size = len(fri_trace_file) # size of trace files step fri_array_of_traces[ i] = fri_trace_file # input each traces fiel in the array #array_of_traces.extend(sat_array_of_traces) array_of_traces = fri_array_of_traces #print 'Current day =' , current_day #********************************************************************************************************************* ##count number of visited cell arry_of_temp_cell = [ 0 ] * total_file_num #an array to put the cell ID in each step arry_of_number_cell = [ 0 ] * size #an array to count the number of visited cell ID arry_of_number_cell_copy = [ 0 ] * size #an array to count the number of visited cell ID for j in range(0, size): for i in range(0, total_file_num): arry_of_temp_cell[i] = array_of_traces[i][j][1] arry_of_number_cell[j] = [[x, arry_of_temp_cell.count(x)] for x in set(arry_of_temp_cell)] arry_of_temp_cell[i] = array_of_traces[i][j][1] arry_of_number_cell_copy[j] = [[ x, arry_of_temp_cell.count(x) ] for x in set(arry_of_temp_cell)] ##count percentage of visited cell arry_of_percentage_cell = arry_of_number_cell_copy #an array to calculate the percentage for each visited cell for j in range(0, size): sum_cell = 0. for n in range(0, len(arry_of_percentage_cell[j])): sum_cell += arry_of_percentage_cell[j][n][ 1] #calculate all visited cell for n in range(0, len(arry_of_percentage_cell[j])): arry_of_percentage_cell[j][n][ 1] = arry_of_percentage_cell[j][n][ 1] / sum_cell #calculate the percentage for each visited cell ##make an array to save time,percentage and cell ID arry_of_time_percentage_cell = arry_of_percentage_cell #an array to save [time_cellID, percentage] for j in range(0, ): for n in range(0, len(arry_of_percentage_cell[j])): arry_of_time_percentage_cell[j][n][0] = [ '%d%s%d' % (array_of_traces[0][j][0], '-', arry_of_percentage_cell[j][n][0]) ] #********************************************************************************************************************* #open file to save the state file (state file is calculated from this file that has non zero transient probability) f1 = open('./mobaas/ICNoutput/statefile_ICN.txt', 'w') f1 = open('./mobaas/ICNoutput/statefile_ICN.txt', 'a') ##calculate the percentage of going from once cell to other cell in each step #matrix_of_number_next_cell_percentage=[0]*t for j in range(0, size - 2): for n in range( 0, len(arry_of_number_cell[j]) ): #an array which has the number of visited cell in each step #print (n) arry_of_temp_next_cell = [] arry_of_number_next_cell = [] arry_of_number_next_cell_copy = [] temp = arry_of_number_cell[j][n][ 0] # temp get each of the visited cell for i in range(0, total_file_num): if array_of_traces[i][j][1] == temp: #an array to save visited cell in the next step arry_of_temp_next_cell.append( array_of_traces[i][j + 1][1]) #an array which has the number of visited cell in the next step arry_of_number_next_cell = [[ x, arry_of_temp_next_cell.count(x) ] for x in set(arry_of_temp_next_cell)] #make a copy arry_of_number_next_cell_copy = [[ x, arry_of_temp_next_cell.count(x) ] for x in set(arry_of_temp_next_cell)] arry_of_number_next_cell_percentage = arry_of_number_next_cell_copy #calculate the percentage of going to the next cell sum_next_cell = 0. for n in range( 0, len(arry_of_number_next_cell_percentage)): sum_next_cell += arry_of_number_next_cell_percentage[ n][1] #print(sum_next_cell) for n in range( 0, len(arry_of_number_next_cell_percentage)): arry_of_number_next_cell_percentage[n][ 1] = arry_of_number_next_cell_percentage[ n][1] / sum_next_cell #print(sum_next_cell) #save the the transient probability in state file for m in range(0, len(arry_of_number_next_cell_percentage)): f1.write('%s%d' % ('c', array_of_traces[0][j][0])) f1.write('%s%d ' % ('_', temp)) f1.write('%s%d' % ('c', array_of_traces[0][j + 1][0])) f1.write( '%s%d %.3f \n' % ('_', arry_of_number_next_cell_percentage[m][0], arry_of_number_next_cell_percentage[m][1])) f1.close() #******************************************************************************************************************** ## change hh:mm to time step (e.g, 06:21-->381 ) current_time_setp = sum( int(x) * 60**i for i, x in enumerate(reversed(current_time.split(':')))) ##calculate the transient probability state_mat = pykov.readmat('./mobaas/ICNoutput/statefile_ICN.txt') ## save list of cell in current time current_cell_list = [] for i in range(0, len(state_mat)): temp_time = int(state_mat.keys()[i][0] [1:state_mat.keys()[i][0].index('_')]) if temp_time == current_time_setp: #print '-------------' temp_cell = int(state_mat.keys()[i][0] [state_mat.keys()[i][0].index('_') + 1:]) if temp_cell != 0: current_cell_list.append(temp_cell) ## consider only one possibility form all currrent cell ## chech if 'current_cell_list' is not empty! if current_cell_list: current_cell = current_cell_list[0] #print 'current cell : ', current_cell ## make time step and cell as 'c381_5384 ' current_time_cell = ( '%s%d%s%d' % ('c', current_time_setp, '_', current_cell)) #print current_time_cell #initial_state = pykov.Vector(c614_39184 = 1) initial_state = pykov.Vector([(current_time_cell, 1)]) next_cell_probability = state_mat.pow(initial_state, transiant_num) result[(user_id, current_cell, Temp_time)] = [] for prob_key in next_cell_probability.keys(): next_cell = prob_key[prob_key.index('_') + 1:] if next_cell != '0': result[(user_id, current_cell, Temp_time)] += [ (next_cell, "%.4f" % next_cell_probability[prob_key]) ] f3 = open('./mobaas/ICNoutput/state_mat_ICN.txt', 'w') for i in range(0, len(state_mat.values())): f3.write('%s %f \n' % (state_mat.keys()[i], state_mat.values()[i])) f3.close() #the result file x = [] cell_name = [] y = [] for i in range(0, len(next_cell_probability.values())): x.append(i + 1) cell_name.append(next_cell_probability.keys()[i]) y.append(next_cell_probability.values()[i]) temp_next_cell = next_cell_probability.keys()[i] if temp_next_cell[temp_next_cell.index('_') + 1:] != '0': f4.write('%d ' % (user_id)) f4.write('%d ' % (current_cell)) f4.write( '%s %s %f \n' % ('{:02d}:{:02d}'.format(*divmod( int(temp_next_cell[1:temp_next_cell.index('_')] ), 60)), temp_next_cell[temp_next_cell.index('_') + 1:], next_cell_probability.values()[i])) f4.write('%s \n' % (' ')) f4.close() # put the (current_time)=(current_time+transiant_num) current_time = Temp_time ###print current_time print "result:" pprint.pprint(result) return result