diff trafficintelligence/storage.py @ 1030:aafbc0bab925

moved method around to avoid cross-dependencies
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 19 Jun 2018 10:04:52 -0400
parents cc5cb04b04b0
children 8ffb3ae9f3d2
line wrap: on
line diff
--- a/trafficintelligence/storage.py	Mon Jun 18 22:50:59 2018 -0400
+++ b/trafficintelligence/storage.py	Tue Jun 19 10:04:52 2018 -0400
@@ -2,20 +2,17 @@
 # -*- coding: utf-8 -*-
 '''Various utilities to save and load data'''
 
-from trafficintelligence import utils, moving, events, indicators
-from trafficintelligence.base import VideoFilenameAddable
-
 from pathlib import Path
 import shutil
 from copy import copy
 import sqlite3, logging
+
 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
 from pandas import read_csv, merge
 
+from trafficintelligence import utils, moving, events, indicators
+from trafficintelligence.base import VideoFilenameAddable
 
-commentChar = '#'
-
-delimiterChar = '%';
 
 ngsimUserTypes = {'twowheels':1,
                   'car':2,
@@ -881,48 +878,19 @@
 # txt files
 #########################
 
-def openCheck(filename, option = 'r', quitting = False):
-    '''Open file filename in read mode by default
-    and checks it is open'''
-    try:
-        return open(filename, option)
-    except IOError:
-        print('File {} could not be opened.'.format(filename))
-        if quitting:
-            from sys import exit
-            exit()
-        return None
-
-def readline(f, commentCharacters = commentChar):
-    '''Modified readline function to skip comments
-    Can take a list of characters or a string (in will work in both)'''
-    s = f.readline()
-    while (len(s) > 0) and s[0] in commentCharacters:
-        s = f.readline()
-    return s.strip()
-
-def getLines(f, delimiterChar = delimiterChar, commentCharacters = commentChar):
-    '''Gets a complete entry (all the lines) in between delimiterChar.'''
-    dataStrings = []
-    s = readline(f, commentCharacters)
-    while len(s) > 0 and s[0] != delimiterChar:
-        dataStrings += [s.strip()]
-        s = readline(f, commentCharacters)
-    return dataStrings
-
 def saveList(filename, l):
-    f = openCheck(filename, 'w')
+    f = utils.openCheck(filename, 'w')
     for x in l:
         f.write('{}\n'.format(x))
     f.close()
 
-def loadListStrings(filename, commentCharacters = commentChar):
-    f = openCheck(filename, 'r')
-    result = getLines(f, commentCharacters)
+def loadListStrings(filename, commentCharacters = utils.commentChar):
+    f = utils.openCheck(filename, 'r')
+    result = utils.getLines(f, commentCharacters)
     f.close()
     return result
 
-def getValuesFromINIFile(filename, option, delimiterChar = '=', commentCharacters = commentChar):
+def getValuesFromINIFile(filename, option, delimiterChar = '=', commentCharacters = utils.commentChar):
     values = []
     for l in loadListStrings(filename, commentCharacters):
         if l.startswith(option):
@@ -942,7 +910,7 @@
 def loadPemsTraffic(filename):
     '''Loads traffic data downloaded from the http://pems.dot.ca.gov clearinghouse 
     into pandas dataframe'''
-    f = openCheck(filename)
+    f = utils.openCheck(filename)
     l = f.readline().strip()
     items = l.split(',')
     headers = ['time', 'station', 'district', 'route', 'direction', 'lanetype', 'length', 'nsamples', 'pctobserved', 'flow', 'occupancy', 'speed', 'delay35', 'delay40', 'delay45', 'delay50', 'delay55', 'delay60']
@@ -960,7 +928,7 @@
     sqlite3 [file.sqlite] < import_fzp.sql'''
     sqlScriptFilename = "import_fzp.sql"
     # create sql file
-    out = openCheck(sqlScriptFilename, "w")
+    out = utils.openCheck(sqlScriptFilename, "w")
     out.write(".separator \";\"\n"+
               "CREATE TABLE IF NOT EXISTS curvilinear_positions (t REAL, trajectory_id INTEGER, link_id INTEGER, lane_id INTEGER, s_coordinate REAL, y_coordinate REAL, speed REAL, PRIMARY KEY (t, trajectory_id));\n"+
               ".import "+filename+" curvilinear_positions\n"+
@@ -968,7 +936,7 @@
     out.close()
     # system call
     from subprocess import run
-    out = openCheck("err.log", "w")
+    out = utils.openCheck("err.log", "w")
     run("sqlite3 "+utils.removeExtension(filename)+".sqlite < "+sqlScriptFilename, stderr = out)
     out.close()
     shutil.os.remove(sqlScriptFilename)
@@ -1027,8 +995,8 @@
                 return list(objects.values())
     else:
         if filename.endswith(".fzp"):
-            inputfile = openCheck(filename, quitting = True)
-            line = readline(inputfile, '*$')
+            inputfile = utils.openCheck(filename, quitting = True)
+            line = utils.readline(inputfile, '*$')
             while len(line) > 0:#for line in inputfile:
                 data = line.strip().split(';')
                 objNum = int(data[1])
@@ -1044,7 +1012,7 @@
                 if (warmUpLastInstant is None or instant >= warmUpLastInstant) and objNum in objects:
                     objects[objNum].timeInterval.last = instant
                     objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane)
-                line = readline(inputfile, '*$')
+                line = utils.readline(inputfile, '*$')
         elif filename.endswith(".sqlite"):
             with sqlite3.connect(filename) as connection:
                 cursor = connection.cursor()
@@ -1156,7 +1124,7 @@
     and returns the list of Feature objects'''
     objects = []
 
-    inputfile = openCheck(filename, quitting = True)
+    inputfile = utils.openCheck(filename, quitting = True)
 
     def createObject(numbers):
         firstFrameNum = int(numbers[1])
@@ -1180,7 +1148,7 @@
         obj.size = [float(numbers[8]), float(numbers[9])] # 8 lengh, 9 width # TODO: temporary, should use a geometry object
         return obj
 
-    numbers = readline(inputfile).strip().split()
+    numbers = utils.readline(inputfile).strip().split()
     if (len(numbers) > 0):
         obj = createObject(numbers)
 
@@ -1218,9 +1186,9 @@
     '''Reads data from the trajectory data provided by NGSIM project
     and converts to our current format.'''
     if append:
-        out = openCheck(outputfile,'a')
+        out = utils.openCheck(outputfile,'a')
     else:
-        out = openCheck(outputfile,'w')
+        out = utils.openCheck(outputfile,'w')
     nObjectsPerType = [0,0,0]
 
     features = loadNgsimFile(inputfile, sequenceNum)
@@ -1237,8 +1205,8 @@
     (pinhole camera model, http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html)
     and returns a dictionary'''
     if tanalystFormat:
-        f = openCheck(filename, quitting = True)
-        content = getLines(f)
+        f = utils.openCheck(filename, quitting = True)
+        content = utils.getLines(f)
         cameraData = {}
         for l in content:
             tmp = l.split(':')
@@ -1261,7 +1229,7 @@
         f.write(s+'\n')
 
 def saveTrajectoriesToCsv(filename, objects):
-    f = openCheck(filename, 'w')
+    f = utils.openCheck(filename, 'w')
     for i,obj in enumerate(objects):
         savePositionsToCsv(f, obj)
     f.close()
@@ -1277,7 +1245,7 @@
         from configparser import ConfigParser
 
         config = ConfigParser()
-        config.read_file(addSectionHeader(openCheck(filename)))
+        config.read_file(addSectionHeader(utils.openCheck(filename)))
 
         parentPath = Path(filename).parent
         self.sectionHeader = config.sections()[0]
@@ -1346,7 +1314,7 @@
         from configparser import ConfigParser
 
         config = ConfigParser(strict=False)
-        config.read_file(addSectionHeader(openCheck(filename)))
+        config.read_file(addSectionHeader(utils.openCheck(filename)))
 
         parentPath = Path(filename).parent
         self.sectionHeader = config.sections()[0]
@@ -1462,7 +1430,7 @@
     def loadConfigFile(filename):
         from configparser import ConfigParser
         config = ConfigParser()
-        config.readfp(openCheck(filename))
+        config.readfp(utils.openCheck(filename))
         configDict = dict()
         for sectionName in config.sections():
             configDict[sectionName] = SceneParameters(config, sectionName)