changeset 206:59eea6eed91b

Comment and rating scripts: robust against audioholders without id or audioelements; fixes issue where plots were not cleared before adding new content; showing legend optional; robust against non-UTF8 characters in legend names
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 15 Jun 2015 13:09:46 +0100
parents 06b253e9b394
children 32e16bf9303a
files scripts/comment_parser.py scripts/score_boxplot.py scripts/score_individual.py scripts/score_parser.py
diffstat 4 files changed, 41 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/comment_parser.py	Fri Jun 12 12:01:14 2015 +0100
+++ b/scripts/comment_parser.py	Mon Jun 15 13:09:46 2015 +0100
@@ -11,6 +11,9 @@
         # get list of all page names
         for audioholder in root.findall("./audioholder"):   # iterate over pages
             page_name = audioholder.get('id')               # get page name
+            
+            if page_name is None: # ignore 'empty' audio_holders
+                break
 
             # create folder [page_name] if not yet created
             if not os.path.exists(page_name):
@@ -18,21 +21,26 @@
 
             # for page [page_name], print comments related to fragment [id]
             for audioelement in root.findall("*/[@id='"+page_name+"']/audioelement"):
-                audio_id = str(audioelement.get('id'))
-                # append to file [page_name]/[page_name]-comments-[id].csv
-                with open(page_name+'/'+page_name+'-comments-'+audio_id+'.csv', 'a') as csvfile:
-                    commentstr = root.find("*/[@id='"
-                                           + page_name
-                                           + "']/audioelement/[@id='"
-                                           + audio_id
-                                           + "']/comment/response").text
-                    writer = csv.writer(csvfile, delimiter=',')
-                    writer.writerow([commentstr.encode("utf-8")])
-                    #TODO Comma doesn't act as delimiter now!
-                    # (when adding more than just a comment per line):
-                    # writer.writerow([file + ',' + commentstr.encode("utf-8")])
+                if audioelement is not None: # Check it exists
+                    audio_id = str(audioelement.get('id'))
 
-                    #TODO Replace 'new line' with something else?
+                    # append to file [page_name]/[page_name]-comments-[id].csv
+                    with open(page_name+'/'+page_name+'-comments-'+audio_id+'.csv', 'a') as csvfile:
+                        writer = csv.writer(csvfile, delimiter=',')
+                        commentstr = root.find("*/[@id='"
+                                               + page_name
+                                               + "']/audioelement/[@id='"
+                                               + audio_id
+                                               + "']/comment/response").text
+                        if commentstr is None:
+                            writer.writerow([''])
+                        else:
+                            writer.writerow([commentstr.encode("utf-8")])
+                        #TODO Comma doesn't act as delimiter now!
+                        # (when adding more than just a comment per line):
+                        # writer.writerow([file + ',' + commentstr.encode("utf-8")])
 
-                    #TODO 'Append' means duplicate entries if run several times...
+                        #TODO Replace 'new line' with something else?
 
+                        #TODO 'Append' means duplicate entries if run several times...
+
--- a/scripts/score_boxplot.py	Fri Jun 12 12:01:14 2015 +0100
+++ b/scripts/score_boxplot.py	Mon Jun 15 13:09:46 2015 +0100
@@ -67,4 +67,5 @@
         #plt.show() # show plot
         #exit()
 
-        plt.savefig(rating_folder+page_name+"-ind.png")
+        plt.savefig(rating_folder+page_name+"-box.png")
+        plt.close()
--- a/scripts/score_individual.py	Fri Jun 12 12:01:14 2015 +0100
+++ b/scripts/score_individual.py	Mon Jun 15 13:09:46 2015 +0100
@@ -8,9 +8,12 @@
 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] # colormap for to cycle through
 markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"]
 
+show_legend = False
+
 # get every csv file in folder
 for file in os.listdir(rating_folder):
     if file.endswith(".csv"):
+        
         page_name = file[:-4] # file name (without extension) is page ID
 
         with open(rating_folder+file, 'r') as readfile: # read this csv file
@@ -34,7 +37,7 @@
                         )
                 increment += 1 # increase counter
                 linehandles.append(plothandle)
-                legendnames.append(subject_id)
+                legendnames.append(subject_id.decode("utf-8")) # avoid decoding problems
 
 
             plt.xlabel('Fragment')
@@ -45,16 +48,18 @@
             plt.ylabel('Rating')
             plt.ylim(0,1)
 
-            plt.legend(linehandles, legendnames,
-                       loc='upper right',
-                       bbox_to_anchor=(1.1, 1),
-                       borderaxespad=0.,
-                       numpoints=1 # remove extra marker
-                       )
+            if show_legend:
+                plt.legend(linehandles, legendnames,
+                           loc='upper right',
+                           bbox_to_anchor=(1.1, 1),
+                           borderaxespad=0.,
+                           numpoints=1 # remove extra marker
+                           )
 
             #TODO Put legend outside of box
 
             #plt.show() # show plot
             #exit()
-
+            
             plt.savefig(rating_folder+page_name+"-ind.png")
+            plt.close()
--- a/scripts/score_parser.py	Fri Jun 12 12:01:14 2015 +0100
+++ b/scripts/score_parser.py	Mon Jun 15 13:09:46 2015 +0100
@@ -18,6 +18,9 @@
         for audioholder in root.findall("./audioholder"):    # iterate over pages
             page_name = audioholder.get('id') # get page name
             #print ["DEBUG    page " + page_name]
+            
+            if page_name is None: # ignore 'empty' audio_holders
+                break
 
             file_name = 'ratings/'+page_name+'-ratings.csv' # score file name