SigAnlalyze_cmd.py 4.34 KB
Newer Older
Hurstel's avatar
Hurstel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
'''
Created on 27 august 2014

@author: hurstel
'''

import sys
import os.path

from argparse import ArgumentParser
from argparse import RawDescriptionHelpFormatter
from dev.CSVFileLooker import CSVFileLooker
from dev.HapticAnalyzer import HapticAnalyzer
from dev.AuditiveAnalyzer import AuditiveAnalyzer
from dev.VisualAnalyzer import VisualAnalyzer


def writeReport(reportPath, sigConf, clusters):
    print "Writing report"
    path= reportPath
    i=1
    while ( os.path.isfile(path) ) :
        i+=1
        path_split= reportPath.split('.')
        if len(path_split)>1:
            path= ('.'.join(path_split[:-1]))+("_{0}".format(i))+'.'+path_split[-1:][0]
        else:
            path= reportPath+("_{0}".format(i))
    
    reportFile= open(path, 'w')
    print "---- writing at:  "+path
    
    l1= len(clusters[0])
    l2= len(clusters[1])
    if (l1 != l2) :
        reportFile.write("# (W) The same amount of clusters hasn't been detected in both entries\n\n")
    
    reportFile.write("signal ; cluster_detect_position ; time\n")
    
    groupClust= [ (c,e) for e in [0,1] for c in clusters[e] ]
    groupClust.sort()
    
    print "fuu: %s" % str(groupClust)
    
    lg= len(groupClust)
    for i in range(0, lg):
        if i<(lg-1) and groupClust[i][0]==groupClust[i+1][1]:
            w_s= sigConf[0]+sigConf[1]
        else :
            w_s= str(sigConf[groupClust[i][1]])
        p= groupClust[i][0]
        reportFile.write("{0} ; {1} ; {2}\n".format(w_s, p, float(p)/4000.0))
    
    reportFile.close()

def main(argv=None):
    if (argv is None):
        argv= sys.argv
    else:
        sys.argv.extend(argv)
        
    
    parser= ArgumentParser(description="Test", formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument("csvFilePath", help="path to the csv file to analyze")
    parser.add_argument("signalConfig", help="signal configuration for both entries (ex: 'HV' for haptic on entry\
     1 and visual on entry; 'AH' for auditive on entry 1 and haptic on entry 2)")
    parser.add_argument("--report", dest="report", help="print a report file (give path)")
    
    args= parser.parse_args()
    
    looker= CSVFileLooker(args.csvFilePath)
    looker.read()

    sigConf= args.signalConfig[:2].upper()
    
    print "\n\n\n"
    print "---------"
    print "This is a help script."
    print "Its goal is to find quickly the signals occurence in the given sampled dataSet."
    print "There are no warranty nor reliability for the results."
    print "(especially regarding the auditive signal which is highly unstable)"
    print "You'll probably want to (and should) double check manually the dataSet."
    print "Feel free to edit and improve this script."
    print "---------\n\n"
    
    class SignalConfigError(Exception):
        def __init__(self):
            Exception.__init__(self)
            
        def __str__(self):
            return "The script's 'signalConfig' argument is invalid."

    
    analyzers= [None,None]
    if (len(sigConf)>=2):
        data1=  [v[0] for v in looker.data]
        data2=  [v[1] for v in looker.data]
        
        if sigConf[0]=='A':
            analyzers[0]= AuditiveAnalyzer(data1)
        elif sigConf[0]=='V':
            analyzers[0]= VisualAnalyzer(data1)
        else:
            analyzers[0]= HapticAnalyzer(data1)
            
        
        if sigConf[1]=='H':
            analyzers[1]= HapticAnalyzer(data2)
        elif sigConf[1]=='V':
            analyzers[1]= VisualAnalyzer(data2)
        else:
            analyzers[1]= AuditiveAnalyzer(data2)
        
        foundClusters= [None, None]
        for i in [0,1]:
            analyzers[i].compute()
            foundClusters[i]= analyzers[i].findCluster()
        
        ev= [None,None]
        for i in [0,1]:
            print '%c signal:' % sigConf[i]
            print '---- foundClusters: %s' % foundClusters[i]
            v= []
            for clust in foundClusters[i]:
                v.append(analyzers[i].probableSignalClusterSignificantPos(clust))
            print '---- signal occurences in dataset: %s' % v
            print '---- signal times occurence in sampling: %s' % str([ float(p)/4000.0 for p in v])
            ev[i]=v
            
        if args.report is not None :
            writeReport(args.report, sigConf, ev)
    else :
        raise SignalConfigError
    
    return 0

if __name__ == "__main__":
    sys.exit(main())