comparison trafficintelligence/storage.py @ 1143:8ac52ebff5f7

avoid using chdir
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 21 Apr 2020 00:55:02 -0400
parents 3e0f43edb4d6
children aa88acf06876
comparison
equal deleted inserted replaced
1142:b3ee75b4978a 1143:8ac52ebff5f7
4 4
5 from pathlib import Path 5 from pathlib import Path
6 import shutil 6 import shutil
7 from copy import copy 7 from copy import copy
8 import sqlite3, logging 8 import sqlite3, logging
9 from os import chdir
10 9
11 from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt, floor as npfloor, ceil as npceil, linalg, int32, int64 10 from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt, floor as npfloor, ceil as npceil, linalg, int32, int64
12 from pandas import read_csv, merge 11 from pandas import read_csv, merge
13 12
14 from trafficintelligence import utils, moving, events, indicators 13 from trafficintelligence import utils, moving, events, indicators
660 659
661 def loadPrototypesFromSqlite(filename, withTrajectories = True): 660 def loadPrototypesFromSqlite(filename, withTrajectories = True):
662 'Loads prototype ids and matchings (if stored)' 661 'Loads prototype ids and matchings (if stored)'
663 prototypes = [] 662 prototypes = []
664 if Path(filename).is_file(): 663 if Path(filename).is_file():
665 filename = str(Path(filename).resolve()) 664 parentPath = Path(filename).resolve().parent
666 pwd = Path.cwd()
667 chdir(Path(filename).parent)
668 with sqlite3.connect(filename) as connection: 665 with sqlite3.connect(filename) as connection:
669 cursor = connection.cursor() 666 cursor = connection.cursor()
670 objects = [] 667 objects = []
671 try: 668 try:
672 cursor.execute('SELECT * FROM prototypes') 669 cursor.execute('SELECT * FROM prototypes')
673 for row in cursor: 670 for row in cursor:
674 prototypes.append(moving.Prototype(row[0], row[1], row[2], row[3])) 671 prototypes.append(moving.Prototype(row[0], row[1], row[2], row[3]))
675 if withTrajectories: 672 if withTrajectories:
676 for p in prototypes: 673 for p in prototypes:
677 p.setMovingObject(loadTrajectoriesFromSqlite(p.getFilename(), p.getTrajectoryType(), [p.getNum()])[0]) 674 p.setMovingObject(loadTrajectoriesFromSqlite(str(parentPath/p.getFilename()), p.getTrajectoryType(), [p.getNum()])[0])
678 # loadingInformation = {} # complicated slightly optimized 675 # loadingInformation = {} # complicated slightly optimized
679 # for p in prototypes: 676 # for p in prototypes:
680 # dbfn = p.getFilename() 677 # dbfn = p.getFilename()
681 # trajType = p.getTrajectoryType() 678 # trajType = p.getTrajectoryType()
682 # if (dbfn, trajType) in loadingInformation: 679 # if (dbfn, trajType) in loadingInformation:
687 # objects += loadTrajectoriesFromSqlite(k[0], k[1], [p.getNum() for p in v]) 684 # objects += loadTrajectoriesFromSqlite(k[0], k[1], [p.getNum() for p in v])
688 except sqlite3.OperationalError as error: 685 except sqlite3.OperationalError as error:
689 printDBError(error) 686 printDBError(error)
690 if len(set([p.getTrajectoryType() for p in prototypes])) > 1: 687 if len(set([p.getTrajectoryType() for p in prototypes])) > 1:
691 print('Different types of prototypes in database ({}).'.format(set([p.getTrajectoryType() for p in prototypes]))) 688 print('Different types of prototypes in database ({}).'.format(set([p.getTrajectoryType() for p in prototypes])))
692 chdir(pwd)
693 return prototypes 689 return prototypes
694 690
695 def savePOIsToSqlite(filename, gmm, gmmType, gmmId): 691 def savePOIsToSqlite(filename, gmm, gmmType, gmmId):
696 '''Saves a Gaussian mixture model (of class sklearn.mixture.GaussianMixture) 692 '''Saves a Gaussian mixture model (of class sklearn.mixture.GaussianMixture)
697 gmmType is a type of GaussianMixture, learnt either from beginnings or ends of trajectories''' 693 gmmType is a type of GaussianMixture, learnt either from beginnings or ends of trajectories'''