SlideShare a Scribd company logo
import re
import time
import math
# --- Parameter ---
FREQUENCY = 60 # (Hz)
DECIMAL = 3 # 2nd decimal place (About 70km)
ALPHA = 4.07 # Standard deviation of GPS measurement
'''
BETA value
'''
BETA = 1 # Exponential distribution parameter
# --- Structure ---
gps_data = [] # [(Latitude, Longitude) ...]
road_network = {} # {Edge ID: (From Node ID, To Node ID, Two Way, Speed
(m/s))}
road_list = [] # [(Edge ID, Segment ID, Latitude, Longitude, Status) ...] Status
0 - skip, 1 - do HMM
road_map = {} # {(Latitude(2nd decimal place), Longitude(2nd decimal
place)): [Edge ID, Segment ID, Latitude, Longitude ...])
ground_true = [] # (Edge, Traverse)
# --- Function ---
def deg2rad(d): # degree to radian
return d * math.pi / 180.0
EARTH_RADIUS_METER = 6378137.0
def spherical_distance(f, t): # caculate the spherical distance of two points
flat = deg2rad(f[0])
flon = deg2rad(f[1])
tlat = deg2rad(t[0])
tlon = deg2rad(t[1])
con = math.sin(flat) * math.sin(tlat)
con += math.cos(flat) * math.cos(tlat) * math.cos(flon - tlon)
return math.acos(con) * EARTH_RADIUS_METER # (m)
def route_distance(f, t):
'''
route algo
'''
return 0
def measure_probability(f, t):
return 1 / (math.sqrt(2 * math.pi) * ALPHA) * math.exp(-0.5 *
math.pow(spherical_distance(f, t) / ALPHA, 2))
def transition_probability(z1, z2, x1, x2):
return 1 / BETA * math.exp(-1 * (spherical_distance(z1, z2) - route_distance(x1,
x2)) / BETA)
'''
def viterbi(obs, states, start_p, trans_p, emit_p):
V = [{}]
path = {}
# Initialize base cases (t == 0)
for y in states:
V[0][y] = start_p[y] * emit_p[y][obs[0]]
path[y] = [y]
# Run Viterbi for t > 0
for t in range(1, len(obs)):
V.append({})
newpath = {}
for y in states:
(prob, state) = max((V[t-1][y0] * trans_p[y0][y] * emit_p[y][obs[t]], y0)
for y0 in states)
V[t][y] = prob
newpath[y] = path[state] + [y]
# Don't need to remember the old paths
path = newpath
n = 0 # if only one element is observed max is sought in the
initialization values
if len(obs)!=1:
n = t
print_dptable(V)
(prob, state) = max((V[n][y], y) for y in states)
return (prob, path[state])
'''
#====================================================================
=========================================#
# --- Parsing ---
print('Parsing...')
tStart = time.time() # (ms)
# [GPS data]
gps_data_file = open('gps_data.txt', 'r', encoding = 'UTF-8')
info = gps_data_file.readline() # [(Date (UTC), Time (UTC), Latitude, Longitude)
counter = 0
for line in gps_data_file:
counter += 1
if(counter == 60 / FREQUENCY):
temp = line.split()
gps_data.append((float(temp[2]), float(temp[3])))
counter = 0
print("GPS data (number): ", len(gps_data))
gps_data_file.close()
# [Road network data]
road_network_file = open('road_network.txt', 'r', encoding = 'UTF-8')
info = road_network_file.readline()
for line in road_network_file:
temp = line.split()
temp_dic_network = {int(temp[0]): ((int(temp[1]), int(temp[2]), int(temp[3]),
float(temp[4])))}
road_network.update(temp_dic_network) # (Edge ID, From Node ID, To
Node ID, Two Way, Speed (m/s))
for i in range(int(temp[5])):
temp_num_lati = float(re.sub('[^-.0-9]', '', temp[(i * 2 + 1) + 6])) #
Latitude
temp_num_longi = float(re.sub('[^-.0-9]', '', temp[(i * 2) + 6])) #
Longitude
# [Road Map]
if((round(temp_num_lati, DECIMAL), round(temp_num_longi, DECIMAL))
not in road_map):
temp_dic_map = {(round(temp_num_lati, DECIMAL),
round(temp_num_longi, DECIMAL)): [(int(temp[0]), i, temp_num_lati,
temp_num_longi)]}
road_map.update(temp_dic_map)
else:
road_map.get((round(temp_num_lati, DECIMAL),
round(temp_num_longi, DECIMAL))).append([int(temp[0]), i, temp_num_lati,
temp_num_longi])
# [Road List]
road_list.append([int(temp[0]), i, temp_num_lati, temp_num_longi], 0)
print("Road segment (number): ", len(road_list))
road_network_file.close()
# [Ground true data]
ground_true_file = open('ground_truth_route.txt', 'r', encoding = 'UTF-8')
info = ground_true_file.readline() # (Edge ID, Traversed From to To)
for line in ground_true_file:
temp = line.split()
ground_true.append((int(temp[0]), temp[1]))
print("Ground true (number): ", len(ground_true))
ground_true_file.close()
print('Elapsed time = ', round(time.time() - tStart, 2))
# --- Analysising ---
print('Analysising...')
tStart = time.time() # (ms)
V = [{}]
path = {}
# Need valid HMM states
# Initialize base cases (t == 0)
for y in road_list:
# Search road map
for i in range(3):
for j in range(3):
if ((round(y[0], DECIMAL) + i, round(y[1], DECIMAL) + j) in road_map):
temp = road_map.get(round(y[0], DECIMAL) + i, round(y[1],
DECIMAL) + j)
'''
Set status and count propability
'''
prev = (0, 0)
for t in range(len(gps_data)):
if(t == 0):
prev = gps_data[t]
elif(spherical_distance(prev, gps_data[t]) > 2 * ALPHA):
'''
do something
'''
prev = gps_data[t]
'''
1. Removing points that are within 2 * Standard Deviation of the previous point
2. HMM Breaks
2.1 More than (200m)
2.2 Route more than great circle (2000m)
2.3 Vehicle speed 3x in route or more than 50 m/s
'''
print('Elapsed time = ', round(time.time() - tStart, 2))
# --- Evaluation ---
'''
print('Evaluation...')
tStart = time.time() # (ms)
print('Elapsed time = ', round(time.time() - tStart, 2))
'''

More Related Content

DOCX
imager package in R and examples..
DOCX
Advanced Data Visualization in R- Somes Examples.
DOCX
Basic Calculus in R.
PDF
KEY
R meets Hadoop
KEY
RHadoop の紹介
DOCX
CLUSTERGRAM
PDF
Plotting position and velocity
imager package in R and examples..
Advanced Data Visualization in R- Somes Examples.
Basic Calculus in R.
R meets Hadoop
RHadoop の紹介
CLUSTERGRAM
Plotting position and velocity

What's hot (19)

PDF
Noise detection from the signal matlab code, Signal Diagnosis
PDF
ARM 7 LPC 2148 lecture
PPTX
Lecture 15 data structures and algorithms
PPTX
K10692 control theory
TXT
Script jantung copy
PDF
TensorFlow Tutorial
PDF
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
PDF
Calculus III
PDF
Derivatives Lesson Oct 14
PDF
Program implementation and testing
PDF
PDF
Quantum espresso G Vector distributon
RTF
DOCX
Mosaic plot in R.
PDF
Network flow problems
TXT
/Root/exam unidad1/muestraip red
PDF
Finagle By Twitter Engineer @ Knoldus
PDF
bpftrace - Tracing Summit 2018
Noise detection from the signal matlab code, Signal Diagnosis
ARM 7 LPC 2148 lecture
Lecture 15 data structures and algorithms
K10692 control theory
Script jantung copy
TensorFlow Tutorial
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Calculus III
Derivatives Lesson Oct 14
Program implementation and testing
Quantum espresso G Vector distributon
Mosaic plot in R.
Network flow problems
/Root/exam unidad1/muestraip red
Finagle By Twitter Engineer @ Knoldus
bpftrace - Tracing Summit 2018
Ad

Similar to Python hmm (20)

PDF
Write Python for Speed
DOCX
Company_X_Data_Analyst_Challenge
PDF
Trees And More With Postgre S Q L
DOCX
CDMA simulation code for wireless Network.docx
PPTX
A scrupulous code review - 15 bugs in C++ code
PDF
TSP algorithm (Computational Thinking) Dropbox
PDF
Questions has 4 parts.1st part Program to implement sorting algor.pdf
PDF
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
DOCX
PRACTICAL COMPUTING
PDF
The Uncertain Enterprise
DOC
Seg code
PDF
Gentle Introduction to Functional Programming
PDF
verilog HDL introduction - beginners guide
PDF
Frsa
PDF
Please finish the codes in Graph.h class.#################### Vert.pdf
PPT
chapter1.ppt
TXT
Senior design project code for PPG
PDF
Coscup2021-rust-toturial
PPTX
Fourier project presentation
DOCX
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
Write Python for Speed
Company_X_Data_Analyst_Challenge
Trees And More With Postgre S Q L
CDMA simulation code for wireless Network.docx
A scrupulous code review - 15 bugs in C++ code
TSP algorithm (Computational Thinking) Dropbox
Questions has 4 parts.1st part Program to implement sorting algor.pdf
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
PRACTICAL COMPUTING
The Uncertain Enterprise
Seg code
Gentle Introduction to Functional Programming
verilog HDL introduction - beginners guide
Frsa
Please finish the codes in Graph.h class.#################### Vert.pdf
chapter1.ppt
Senior design project code for PPG
Coscup2021-rust-toturial
Fourier project presentation
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
Ad

Recently uploaded (20)

PDF
WRN_Investor_Presentation_August 2025.pdf
PPTX
HR Introduction Slide (1).pptx on hr intro
PDF
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
PPT
340036916-American-Literature-Literary-Period-Overview.ppt
PPTX
5 Stages of group development guide.pptx
PDF
Laughter Yoga Basic Learning Workshop Manual
PDF
COST SHEET- Tender and Quotation unit 2.pdf
PDF
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
PPTX
Belch_12e_PPT_Ch18_Accessible_university.pptx
PPTX
ICG2025_ICG 6th steering committee 30-8-24.pptx
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
DOCX
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PDF
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
PDF
Business model innovation report 2022.pdf
PPTX
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
PPTX
New Microsoft PowerPoint Presentation - Copy.pptx
PDF
IFRS Notes in your pocket for study all the time
PPT
Chapter four Project-Preparation material
WRN_Investor_Presentation_August 2025.pdf
HR Introduction Slide (1).pptx on hr intro
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
340036916-American-Literature-Literary-Period-Overview.ppt
5 Stages of group development guide.pptx
Laughter Yoga Basic Learning Workshop Manual
COST SHEET- Tender and Quotation unit 2.pdf
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
Belch_12e_PPT_Ch18_Accessible_university.pptx
ICG2025_ICG 6th steering committee 30-8-24.pptx
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
unit 1 COST ACCOUNTING AND COST SHEET
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
Business model innovation report 2022.pdf
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
New Microsoft PowerPoint Presentation - Copy.pptx
IFRS Notes in your pocket for study all the time
Chapter four Project-Preparation material

Python hmm

  • 1. import re import time import math # --- Parameter --- FREQUENCY = 60 # (Hz) DECIMAL = 3 # 2nd decimal place (About 70km) ALPHA = 4.07 # Standard deviation of GPS measurement ''' BETA value ''' BETA = 1 # Exponential distribution parameter # --- Structure --- gps_data = [] # [(Latitude, Longitude) ...] road_network = {} # {Edge ID: (From Node ID, To Node ID, Two Way, Speed (m/s))} road_list = [] # [(Edge ID, Segment ID, Latitude, Longitude, Status) ...] Status 0 - skip, 1 - do HMM road_map = {} # {(Latitude(2nd decimal place), Longitude(2nd decimal place)): [Edge ID, Segment ID, Latitude, Longitude ...]) ground_true = [] # (Edge, Traverse) # --- Function --- def deg2rad(d): # degree to radian return d * math.pi / 180.0 EARTH_RADIUS_METER = 6378137.0 def spherical_distance(f, t): # caculate the spherical distance of two points flat = deg2rad(f[0]) flon = deg2rad(f[1]) tlat = deg2rad(t[0]) tlon = deg2rad(t[1]) con = math.sin(flat) * math.sin(tlat) con += math.cos(flat) * math.cos(tlat) * math.cos(flon - tlon) return math.acos(con) * EARTH_RADIUS_METER # (m)
  • 2. def route_distance(f, t): ''' route algo ''' return 0 def measure_probability(f, t): return 1 / (math.sqrt(2 * math.pi) * ALPHA) * math.exp(-0.5 * math.pow(spherical_distance(f, t) / ALPHA, 2)) def transition_probability(z1, z2, x1, x2): return 1 / BETA * math.exp(-1 * (spherical_distance(z1, z2) - route_distance(x1, x2)) / BETA) ''' def viterbi(obs, states, start_p, trans_p, emit_p): V = [{}] path = {} # Initialize base cases (t == 0) for y in states: V[0][y] = start_p[y] * emit_p[y][obs[0]] path[y] = [y] # Run Viterbi for t > 0 for t in range(1, len(obs)): V.append({}) newpath = {} for y in states: (prob, state) = max((V[t-1][y0] * trans_p[y0][y] * emit_p[y][obs[t]], y0) for y0 in states) V[t][y] = prob newpath[y] = path[state] + [y] # Don't need to remember the old paths path = newpath n = 0 # if only one element is observed max is sought in the
  • 3. initialization values if len(obs)!=1: n = t print_dptable(V) (prob, state) = max((V[n][y], y) for y in states) return (prob, path[state]) ''' #==================================================================== =========================================# # --- Parsing --- print('Parsing...') tStart = time.time() # (ms) # [GPS data] gps_data_file = open('gps_data.txt', 'r', encoding = 'UTF-8') info = gps_data_file.readline() # [(Date (UTC), Time (UTC), Latitude, Longitude) counter = 0 for line in gps_data_file: counter += 1 if(counter == 60 / FREQUENCY): temp = line.split() gps_data.append((float(temp[2]), float(temp[3]))) counter = 0 print("GPS data (number): ", len(gps_data)) gps_data_file.close() # [Road network data] road_network_file = open('road_network.txt', 'r', encoding = 'UTF-8') info = road_network_file.readline() for line in road_network_file: temp = line.split() temp_dic_network = {int(temp[0]): ((int(temp[1]), int(temp[2]), int(temp[3]), float(temp[4])))} road_network.update(temp_dic_network) # (Edge ID, From Node ID, To
  • 4. Node ID, Two Way, Speed (m/s)) for i in range(int(temp[5])): temp_num_lati = float(re.sub('[^-.0-9]', '', temp[(i * 2 + 1) + 6])) # Latitude temp_num_longi = float(re.sub('[^-.0-9]', '', temp[(i * 2) + 6])) # Longitude # [Road Map] if((round(temp_num_lati, DECIMAL), round(temp_num_longi, DECIMAL)) not in road_map): temp_dic_map = {(round(temp_num_lati, DECIMAL), round(temp_num_longi, DECIMAL)): [(int(temp[0]), i, temp_num_lati, temp_num_longi)]} road_map.update(temp_dic_map) else: road_map.get((round(temp_num_lati, DECIMAL), round(temp_num_longi, DECIMAL))).append([int(temp[0]), i, temp_num_lati, temp_num_longi]) # [Road List] road_list.append([int(temp[0]), i, temp_num_lati, temp_num_longi], 0) print("Road segment (number): ", len(road_list)) road_network_file.close() # [Ground true data] ground_true_file = open('ground_truth_route.txt', 'r', encoding = 'UTF-8') info = ground_true_file.readline() # (Edge ID, Traversed From to To) for line in ground_true_file: temp = line.split() ground_true.append((int(temp[0]), temp[1])) print("Ground true (number): ", len(ground_true)) ground_true_file.close() print('Elapsed time = ', round(time.time() - tStart, 2)) # --- Analysising --- print('Analysising...') tStart = time.time() # (ms)
  • 5. V = [{}] path = {} # Need valid HMM states # Initialize base cases (t == 0) for y in road_list: # Search road map for i in range(3): for j in range(3): if ((round(y[0], DECIMAL) + i, round(y[1], DECIMAL) + j) in road_map): temp = road_map.get(round(y[0], DECIMAL) + i, round(y[1], DECIMAL) + j) ''' Set status and count propability ''' prev = (0, 0) for t in range(len(gps_data)): if(t == 0): prev = gps_data[t] elif(spherical_distance(prev, gps_data[t]) > 2 * ALPHA): ''' do something ''' prev = gps_data[t] ''' 1. Removing points that are within 2 * Standard Deviation of the previous point 2. HMM Breaks 2.1 More than (200m) 2.2 Route more than great circle (2000m) 2.3 Vehicle speed 3x in route or more than 50 m/s ''' print('Elapsed time = ', round(time.time() - tStart, 2)) # --- Evaluation ---
  • 6. ''' print('Evaluation...') tStart = time.time() # (ms) print('Elapsed time = ', round(time.time() - tStart, 2)) '''