changeset 1064:cbc026dacf0b

changed interval string representation
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 15 Jul 2018 22:52:26 -0400
parents 3c37d8d20e97
children d4d052a05337
files scripts/process.py trafficintelligence/moving.py trafficintelligence/tests/moving.txt
diffstat 3 files changed, 41 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/process.py	Fri Jul 13 09:59:01 2018 -0400
+++ b/scripts/process.py	Sun Jul 15 22:52:26 2018 -0400
@@ -16,7 +16,7 @@
 parser = argparse.ArgumentParser(description='This program manages the processing of several files based on a description of the sites and video data in an SQLite database following the metadata module.')
 # input
 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True)
-parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int)
+parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*')
 parser.add_argument('--sites', dest = 'siteIds', help = 'indices of the video sequences', nargs = '*')
 
 # main function
@@ -84,27 +84,31 @@
 videoSequences = []
 sites = []
 if args.videoIds is not None:
-    videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds]
-    siteIds = set([vs.cameraView.siteIdx for vs in videoSequences])
+    for videoId in args.videoIds:
+        if '-' in videoId:
+            videoSequences.extend([session.query(VideoSequence).get(i) for i in moving.TimeInterval.parse(videoId)])
+        else:
+            videoSequences.append(session.query(VideoSequence).get(int(videoId)))
+    sites = set([vs.cameraView.site for vs in videoSequences])
 elif args.siteIds is not None:
-    siteIds = set(args.siteIds)
-    for siteId in siteIds:
-        tmpsites = getSite(session, siteId)
-        sites.extend(tmpsites)
-        for site in tmpsites:
-            videoSequences.extend(getSiteVideoSequences(site))
+    for siteId in args.siteIds:
+        if '-' in siteId:
+            sites.extend([session.query(Site).get(i) for i in moving.TimeInterval.parse(siteId)])
+        else:
+            sites.append(session.query(Site).get(int(siteId)))
+    for site in sites:
+        videoSequences.extend(getSiteVideoSequences(site))
 else:
     print('No video/site to process')
-
+sys.exit()
 if args.nProcesses > 1:
     pool = Pool(args.nProcesses)
 
 #################################
-# Delete
+# Report progress in the processing
 #################################
 if args.progress:
     print('Providing information on data progress')
-    print('TODO')
 
 #################################
 # Delete
@@ -245,8 +249,8 @@
     if args.output == 'figure':
         for name in headers[4:]:
             plt.ioff()
-            plt.figure()
-            plt.boxplot([data.loc[data['sites']==siteId, name] for siteId in siteIds], labels = [session.query(Site).get(siteId).name for siteId in siteIds])
+            plt.figure() # siteids does not exist
+            plt.boxplot([data.loc[data['site']==site.name, name] for site in sites], labels = [site.name for site in sites])
             plt.ylabel(name+' Speeds (km/h)')
             plt.savefig(name.lower()+'-speeds.png', dpi=dpi)
             plt.close()
--- a/trafficintelligence/moving.py	Fri Jul 13 09:59:01 2018 -0400
+++ b/trafficintelligence/moving.py	Sun Jul 15 22:52:26 2018 -0400
@@ -33,7 +33,7 @@
             self.last=last
 
     def __str__(self):
-        return '[{0}, {1}]'.format(self.first, self.last)
+        return '{0}-{1}'.format(self.first, self.last)
 
     def __repr__(self):
         return self.__str__()
@@ -69,6 +69,15 @@
         self.last += offset
 
     @classmethod
+    def parse(cls, s):
+        if '-' in s:
+            tmp = s.split('-')
+            if len(tmp) == 2:
+                return cls(int(tmp[0]), int(tmp[1])) # TODO with floats?
+        print(s+' is not a valid representation of an interval')
+        return None
+    
+    @classmethod
     def union(cls, interval1, interval2):
         '''Smallest interval comprising self and interval2'''
         return cls(min(interval1.first, interval2.first), max(interval2.last, interval2.last))
--- a/trafficintelligence/tests/moving.txt	Fri Jul 13 09:59:01 2018 -0400
+++ b/trafficintelligence/tests/moving.txt	Sun Jul 15 22:52:26 2018 -0400
@@ -7,7 +7,7 @@
 >>> Interval(0,1).empty()
 False
 >>> Interval(0,1)
-[0, 1]
+0-1
 >>> Interval(0,1).length()
 1.0
 >>> Interval(23.2,24.9).length()
@@ -15,6 +15,17 @@
 >>> Interval(10,8).length()
 0.0
 
+>>> i = Interval.parse('3-5')
+>>> i.first == 3 and i.last == 5
+True
+>>> type(i)
+<class 'trafficintelligence.moving.Interval'>
+>>> i = TimeInterval.parse('3-5')
+>>> type(i)
+<class 'trafficintelligence.moving.TimeInterval'>
+>>> list(i)
+[3, 4, 5]
+
 >>> TimeInterval(0,1).length()
 2.0
 >>> TimeInterval(10,8).length()
@@ -45,7 +56,7 @@
 >>> TimeInterval(20,30).distance(TimeInterval(3,15))
 5
 >>> TimeInterval.unionIntervals([TimeInterval(3,6), TimeInterval(8,10),TimeInterval(11,15)])
-[3, 15]
+3-15
 
 >>> Point(0,3) == Point(0,3)
 True