comparison scripts/extract-user-counts.py @ 1299:c4bef099d0a2

adding script to extract users counts
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 09 Apr 2025 15:57:50 -0400
parents
children
comparison
equal deleted inserted replaced
1298:f4d4bb9ec34f 1299:c4bef099d0a2
1 #! /usr/bin/env python
2
3 #import numpy as np
4 import argparse # os
5 import pandas as pd
6 import sqlite3
7
8 from trafficintelligence import utils, moving
9
10 parser = argparse.ArgumentParser(description='The program generates a csv of the user counts per SQLite database in the directory ')
11 parser.add_argument('-i', dest = 'dirname', help = 'name of the directory containing all the databases', default = '.')
12 args = parser.parse_args()
13
14 out = open('user-counts.csv', 'w')
15 out.write('filename')
16 for i in range(1,7):
17 out.write(','+moving.userTypeNames[i])
18 out.write('\n')
19 for fn in utils.listfiles(args.dirname, 'sqlite'):
20 with sqlite3.connect(fn) as connection:
21 data = pd.read_sql('SELECT road_user_type, count(*) AS n FROM objects GROUP BY road_user_type', connection)
22 counts = {i:'0' for i in range(1,7)}
23 for i,r in data.iterrows():
24 counts[r['road_user_type']]=str(r['n'])
25 fn2 = fn.split('/')[-1].rsplit('.',1)[0]
26 out.write(fn2+','+','.join(counts.values())+'\n')
27 out.close()