Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 915:13434f5017dd
work to save trajectory assignment to origin and destinations
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 04 Jul 2017 17:03:29 -0400 |
| parents | 1cd878812529 |
| children | 89cc05867c4c |
comparison
equal
deleted
inserted
replaced
| 914:f228fd649644 | 915:13434f5017dd |
|---|---|
| 505 elif dataType == 'interaction': | 505 elif dataType == 'interaction': |
| 506 dropTables(connection, ['interactions', 'indicators']) | 506 dropTables(connection, ['interactions', 'indicators']) |
| 507 elif dataType == 'bb': | 507 elif dataType == 'bb': |
| 508 dropTables(connection, ['bounding_boxes']) | 508 dropTables(connection, ['bounding_boxes']) |
| 509 elif dataType == 'pois': | 509 elif dataType == 'pois': |
| 510 dropTables(connection, ['gaussians2d']) | 510 dropTables(connection, ['gaussians2d', 'objects_pois']) |
| 511 elif dataType == 'prototype': | 511 elif dataType == 'prototype': |
| 512 dropTables(connection, ['prototypes']) | 512 dropTables(connection, ['prototypes']) |
| 513 else: | 513 else: |
| 514 print('Unknown data type {} to delete from database'.format(dataType)) | 514 print('Unknown data type {} to delete from database'.format(dataType)) |
| 515 connection.close() | 515 connection.close() |
| 643 connection.commit() | 643 connection.commit() |
| 644 except sqlite3.OperationalError as error: | 644 except sqlite3.OperationalError as error: |
| 645 printDBError(error) | 645 printDBError(error) |
| 646 connection.close() | 646 connection.close() |
| 647 | 647 |
| 648 def savePOIAssignments(filename, objects): | |
| 649 'save the od fields of objects' | |
| 650 connection = sqlite3.connect(filename) | |
| 651 cursor = connection.cursor() | |
| 652 try: | |
| 653 cursor.execute('CREATE TABLE IF NOT EXISTS objects_pois (object_id INTEGER, origin_poi_id INTEGER, destination_poi_id INTEGER, PRIMARY KEY(object_id))') | |
| 654 for o in objects: | |
| 655 cursor.execute('INSERT INTO objects_pois VALUES({},{},{})'.format(o.getNum(), o.od[0], o.od[1])) | |
| 656 connection.commit() | |
| 657 except sqlite3.OperationalError as error: | |
| 658 printDBError(error) | |
| 659 connection.close() | |
| 660 | |
| 648 def loadPOIs(filename): | 661 def loadPOIs(filename): |
| 649 'Loads all 2D Gaussians in the database' | 662 'Loads all 2D Gaussians in the database' |
| 650 from sklearn import mixture # todo if not avalaible, load data in duck-typed class with same fields | 663 from sklearn import mixture # todo if not avalaible, load data in duck-typed class with same fields |
| 651 from ast import literal_eval | 664 from ast import literal_eval |
| 652 connection = sqlite3.connect(filename) | 665 connection = sqlite3.connect(filename) |
| 696 ######################### | 709 ######################### |
| 697 # saving and loading for scene interpretation (Mohamed Gomaa Mohamed's PhD) | 710 # saving and loading for scene interpretation (Mohamed Gomaa Mohamed's PhD) |
| 698 ######################### | 711 ######################### |
| 699 | 712 |
| 700 def writePrototypesToSqlite(prototypes,nMatching, outputFilename): | 713 def writePrototypesToSqlite(prototypes,nMatching, outputFilename): |
| 701 """ prototype dataset is a dictionary with keys== routes, values== prototypes Ids """ | 714 ''' prototype dataset is a dictionary with keys== routes, values== prototypes Ids ''' |
| 702 connection = sqlite3.connect(outputFilename) | 715 connection = sqlite3.connect(outputFilename) |
| 703 cursor = connection.cursor() | 716 cursor = connection.cursor() |
| 704 | 717 |
| 705 cursor.execute("CREATE TABLE IF NOT EXISTS prototypes (prototype_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, nMatching INTEGER, PRIMARY KEY(prototype_id))") | 718 cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (prototype_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, nMatching INTEGER, PRIMARY KEY(prototype_id))') |
| 706 | 719 |
| 707 for route in prototypes.keys(): | 720 for route in prototypes.keys(): |
| 708 if prototypes[route]!=[]: | 721 if prototypes[route]!=[]: |
| 709 for i in prototypes[route]: | 722 for i in prototypes[route]: |
| 710 cursor.execute("insert into prototypes (prototype_id, routeIDstart,routeIDend, nMatching) values (?,?,?,?)",(i,route[0],route[1],nMatching[route][i])) | 723 cursor.execute('insert into prototypes (prototype_id, routeIDstart,routeIDend, nMatching) values (?,?,?,?)',(i,route[0],route[1],nMatching[route][i])) |
| 711 | 724 |
| 712 connection.commit() | 725 connection.commit() |
| 713 connection.close() | 726 connection.close() |
| 714 | 727 |
| 715 def readPrototypesFromSqlite(filename): | 728 def readPrototypesFromSqlite(filename): |
| 716 """ | 729 ''' |
| 717 This function loads the prototype file in the database | 730 This function loads the prototype file in the database |
| 718 It returns a dictionary for prototypes for each route and nMatching | 731 It returns a dictionary for prototypes for each route and nMatching |
| 719 """ | 732 ''' |
| 720 prototypes = {} | 733 prototypes = {} |
| 721 nMatching={} | 734 nMatching={} |
| 722 | 735 |
| 723 connection = sqlite3.connect(filename) | 736 connection = sqlite3.connect(filename) |
| 724 cursor = connection.cursor() | 737 cursor = connection.cursor() |
