Пример #1
0
def plot(settings):
    # load interaction data
    interact = interactionData(settings['interactionFileLoc'])
    interactScore, interactDate = segmentInteractionIntoDays(interact)

    # load PA data
    PA = PAdata(settings['PAfileLoc'])
    PAscore, PAdate = segmentPAIntoDays(PA)

    pltName = 'user PA and avatar influence vs time'
    barWidth = 0.2
    print 'making plot "' + pltName + '"'
    pylab.figure(pltName)
    p1 = pylab.plt.bar(date2num(interactDate) + barWidth / 2.0,
                       interactScore,
                       color='g',
                       width=barWidth,
                       linewidth=0)
    p2 = pylab.plt.bar(date2num(PAdate) - barWidth / 2.0,
                       PAscore,
                       color='b',
                       width=barWidth,
                       linewidth=0)
    pylab.plt.legend((p1, p2), ('avatar influence', 'subject PA'))

    pylab.plt.ylabel('PA score & interaction score')
    pylab.plt.xlabel('time')

    pylab.plt.draw()
def plot():
    # load in all the data
    participants = list()
    PA = list()

    for pNum in range(HIGHEST_P_NUMBER + 1):  #cycle through all participants

        settings = setup(dataset='USF', data_loc=DATA_LOC, subject_n=pNum)

        try:
            pid = str(pNum)
            fName = settings.getFileName('fitbit')
            pa = PAdata(fName, method='fitbit', timeScale='minute')

            # TODO the rest...
        except IOError:
            print 'p ' + pid + ' PA file "' + fName + '" not found'
            continue

        # if no problems:
        participants.append(pid)
        PA.append(pa)

        print 'p' + pid + ' data loaded.'

    #scale all the data
    #TODO

    print '\n =================================================='
    print '\t total participants: ' + str(len(participants))
    print ' =================================================='

    # shift to time since start...
    #	for pnum in range(0,len(PA)):
    #		start_t = min(PA[pnum].time)
    #		for i in range (0, len(PA[pnum])):
    #			PA[pnum].time[i] -= start_t

    #	# shift all to same daily cycle
    #	for pnum in range(0,len(PA)):
    #		timeOfDay = PA[pnum].time[0]	#assuming earliest time is first...
    #		ToD_start = (timeOfDay.hour*60 + timeOfDay.minute) * 60
    #		for i in range (0, len(PA[pnum])):
    #			PA[pnum].time[i] += ToD_start

    # make the plots
    figName = "PA_sparkScatterplot"
    pylab.figure(figName)
    num = 0
    for pa in PA:
        pylab.subplot(len(PA), 1, num)
        pylab.scatter(pa.time, pa.steps, marker='x', color='b')
        num += 1
Пример #3
0
def plot(settings):
    # load interaction data
    data = PAdata(settings['PAfileLoc'])

    scores, dates = segmentPAIntoDays(data)

    pltName = 'physical activity vs day'
    print 'making plot "' + pltName + '"'
    pylab.figure(pltName)
    pylab.plt.bar(dates, scores)

    pylab.plt.ylabel('day')
    pylab.plt.xlabel('PA score')

    pylab.plt.draw()
Пример #4
0
def plot(settings):
    PA = PAdata(settings['PAfileLoc'])
    print 'making plots...'
    pylab.figure('dailyMinutes')
    #p = pylab.figure()
    # lineGraph = p.add_subplot(211)
    # lineGraph.plot(x,y)
    #barGraph = p.add_subplot(111)
    pl1 = pylab.plt.bar(PA.time, PA.sedentary, linewidth=0, color='b')
    base = PA.sedentary
    pl2 = pylab.plt.bar(PA.time,
                        PA.light,
                        linewidth=0,
                        bottom=base,
                        color='indigo')
    base = [base[i] + PA.light[i] for i in range(len(base))]
    pl3 = pylab.plt.bar(PA.time, PA.mod, linewidth=0, bottom=base, color='r')
    base = [base[i] + PA.mod[i] for i in range(len(base))]
    pl4 = pylab.plt.bar(PA.time,
                        PA.mod_vig,
                        linewidth=0,
                        bottom=base,
                        color='orange')
    base = [base[i] + PA.mod_vig[i] for i in range(len(base))]
    pl5 = pylab.plt.bar(PA.time, PA.vig, linewidth=0, bottom=base, color='y')
    base = [base[i] + PA.vig[i] for i in range(len(base))]
    pl6 = pylab.plt.bar(PA.time,
                        PA.nonWear,
                        linewidth=0,
                        bottom=base,
                        color='dimGrey')
    base = [base[i] + PA.nonWear[i] for i in range(len(base))]
    pl7 = pylab.plt.bar(PA.time,
                        PA.unknown,
                        linewidth=0,
                        bottom=base,
                        color='lightGrey')

    pylab.plt.ylabel('amount [min]')
    pylab.plt.legend( (pl1[0],pl2[0],pl3[0],pl4[0],pl5[0],pl6[0],pl7[0]),\
         ('sedentary','light','mod','mod_vig','vig','nonWear','unknown') )

    pylab.plt.draw()
Пример #5
0
    def test_dataLooksGood(self):
        PA = PAdata(self.settings['PAfileLoc'])
        self.assertTrue(PA.count == 5)
        self.assertTrue(PA.rowCount == 6)

        self.assertTrue(PA.sedentary == [161.0, 0.0, 0.0, 324.0, 43.0])
        self.assertTrue(PA.light == [64.0, 0.0, 25.0, 191.0, 17.0])
        self.assertTrue(PA.mod == [43.0, 0.0, 9.0, 40.0, 4.0])
        self.assertTrue(PA.mod_vig == [0.0, 0.0, 0.0, 0.0, 0.0])
        self.assertTrue(PA.vig == [0.0, 0.0, 0.0, 0.0, 0.0])
        self.assertTrue(PA.nonWear == [27.0, 0.0, 233.0, 884.0, 227.0])
        self.assertTrue(PA.unknown == [1145.4, 1440.0, 1173.0, 0.6, 1148.4])
        import datetime
        self.assertTrue(PA.time == [
            datetime.datetime(2013, 6, 16, 0, 0),
            datetime.datetime(2013, 6, 15, 0, 0),
            datetime.datetime(2013, 6, 14, 0, 0),
            datetime.datetime(2013, 6, 13, 0, 0),
            datetime.datetime(2013, 6, 12, 0, 0)
        ])
Пример #6
0
def plot(dataset='test',
         dataLoc="./data/",
         paMethod=DEFAULT_METHOD,
         bypass_data_check=False):
    # change plot font
    #    font = {'family' : 'monospace',
    #            'weight' : 'normal',
    #            'size'   : 16}
    #    pylab.plt.rc('font', **font)

    pltName = 'participants\' avg PA for sedentary & active avatar days'
    print 'making plot "' + pltName + '"'
    pylab.figure(pltName)
    cmap = pylab.cm.get_cmap(name='terrain')

    activePAs = list()
    sedentPAs = list()
    zeroPAs = list()

    dataCounter = 0  # a count of how many datapoints are used so far (for colormapping and debug)

    for pNum in range(HIGHEST_P_NUMBER + 1):  #cycle through all participants
        print '===p:' + str(pNum) + '==='

        if pNum == 1 or pNum == 3:
            print 'skipping p1 & p3 b/c data is incomplete.\n'
            continue
        elif pNum == 13:
            print 'skip p13 b/c data needs some massaging.\n'
            continue
        elif pNum == 14:
            print 'skip p14 b/c data is incomplete.\n'
            continue
        else:

            activePAtotal = sedentPAtotal = zeroPAtotal = 0
            activePAcount = sedentPAcount = zeroPAcount = 0

            try:
                settings = setup(dataset=dataset,
                                 data_loc=dataLoc,
                                 subject_n=pNum)

                # load interaction data
                interact = interactionData(settings.getFileName('viewLog'))
                interactScore, interactDate = segmentInteractionIntoDays(
                    interact)

                # load PA data
                PA = PAdata(PAfile=settings.getFileName(paMethod),
                            method=paMethod,
                            timeScale='daily')
                if paMethod == 'mMonitor':
                    PAscore, PAdate = segmentPAIntoDays(
                        PA, PAscoreFunction=getPAscore_postiveOnly)
                elif paMethod == 'fitbit':
                    PAscore = PA.steps
                    PAdate = PA.time
                else:
                    raise ValueError('unknown PA method "' + str(paMethod) +
                                     '"')

                if not bypass_data_check:
                    trim_data(interactDate, PAdate, interactScore, PAscore)

                interactions = list()
                for iScore in interactScore:
                    if iScore > 0:
                        interactions.append(1)  #'active')
                    elif iScore < 0:
                        interactions.append(-1)  #'sedentary')
                    else:  # iScore = 0
                        interactions.append(0)  #'0')

                for day in range(len(PAscore)):
                    #print 'day     ='+str(day)
                    #print 'interact='+str(interactions[day])
                    #print 'PAscore ='+str(PAscore[day])
                    if interactions[day] > 0:
                        activePAtotal += PAscore[day]
                        activePAcount += 1
                    elif interactions[day] < 0:
                        sedentPAtotal += PAscore[day]
                        sedentPAcount += 1
                    else:  # interactions[day] == 0
                        zeroPAtotal += PAscore[day]
                        zeroPAcount += 1

                activePAs.append(activePAtotal / activePAcount)
                sedentPAs.append(sedentPAtotal / sedentPAcount)
                try:
                    zeroPAs.append(zeroPAtotal / zeroPAcount)
                except ZeroDivisionError:
                    zeroPAs.append(0)

                # no exceptions means this data was good, increment counter
                dataCounter += 1
                print str(pNum) + ' loaded.\n'

            except InputError as e:
                print e.message
                print 'participant ' + str(pNum) + ' not valid.\n'
            except IOError as e:
                print e.message
                print 'participant ' + str(pNum) + ' not found.\n'
            except Warning as w:
                print w.message
                print 'some issues loading participant ' + str(
                    pNum) + '; continuing anyway.\n'

    if paMethod == 'mMonitor':
        pylab.plt.ylabel('physical activity score')
    elif paMethod == 'fitbit':
        pylab.plt.ylabel('average step counts')

    pylab.plt.xlabel(
        '<-sedentary                                      active->\navatar behavior'
    )

    pylab.plt.gca().axes.get_xaxis().set_ticks([])

    pylab.plt.draw()

    print activePAs
    print sedentPAs
    print zeroPAs

    # TODO: colormapping should use dataCounter and actual number of data available (instead of pNum and HIGHEST_P_NUM, respectively)
    base = 0
    for PA in activePAs:
        pylab.plt.bar(
            1, PA, bottom=base, linewidth=1,
            width=1.7)  #, color=cmap(float(pNum)/float(HIGHEST_P_NUMBER)) )
        base += PA
    base = 0
    for PA in sedentPAs:
        pylab.plt.bar(
            -1, PA, bottom=base, linewidth=1,
            width=1.7)  #, color=cmap(float(pNum)/float(HIGHEST_P_NUMBER)) )
        base += PA
    base = 0
    for PA in zeroPAs:
        pylab.plt.bar(
            0, PA, bottom=base, linewidth=1,
            width=1.7)  #, color=cmap(float(pNum)/float(HIGHEST_P_NUMBER)) )
        base += PA

    paired_sample = stats.ttest_rel(sedentPAs, activePAs)
    print "================================================"
    print str(dataCounter) + " subjects analyzed using " + paMethod + " data."
    print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample
    print "================================================"

    print 'done.'
Пример #7
0
def plot(settings):
    # load interaction data
    interact = interactionData(settings['interactionFileLoc'])
    interactScore, interactDate = segmentInteractionIntoDays(interact)

    # load PA data
    PA = PAdata(settings['PAfileLoc'])
    PAscore, PAdate = segmentPAIntoDays(PA)

    # data check
    print 'data is ' + str(len(PAscore)) + 'x' + str(len(interactScore))

    while (interactDate[0].date() != PAdate[0].date()) or (
            interactDate[-1].date() !=
            PAdate[-1].date()) or (len(interactScore) != len(PAscore)):
        print 'day mismatch: '
        #print 'data is ' + str(len(PAscore)) + 'x' + str(len(interactScore))
        print '\t NAME   \tSTART \t\t\tEND \t\t\tLEN'
        print '\t interact\t' + str(interactDate[0]) + '\t' + str(
            interactDate[-1]) + '\t' + str(len(interactScore))
        print '\t PA     \t' + str(PAdate[0]) + '\t' + str(
            PAdate[-1]) + '\t' + str(len(PAscore)) + '\n'
        if (PAdate[0].date() <
                interactDate[0].date()):  #if pa starts before interact
            print 'pa data removed from start'
            PAdate.pop(0)
            PAscore.pop(0)
        elif PAdate[0].date() > interactDate[0].date(
        ):  #if pa starts after interact
            print 'interact data removed from start'
            interactDate.pop(0)
            interactScore.pop(0)
        elif PAdate[-1].date() < interactDate[-1].date(
        ):  #if pa ends before interact
            print 'pa data removed from end'
            interactDate.pop()
            interactScore.pop()
        elif PAdate[-1].date() > interactDate[-1].date(
        ):  #if pa ends after interact
            print 'interact data removed from end'
            PAscore.pop()
            PAdate.pop()
        else:  # uneven values must be from missing days in middle of one dataset
            longer = list()
            shorter = list()
            ldate = list()
            sdate = list()
            shortName = ''
            if len(PAscore) > len(interactScore):
                shortName = 'interaction'
                longName = 'PA'
                longer = PAscore
                shorter = interactScore
                ldate = PAdate
                sdate = interactDate
            else:
                shortName = 'PA'
                longName = 'interaction'
                longer = interactScore
                shorter = PAscore
                ldate = interactDate
                sdate = PAdate
            for i in range(len(longer)):
                #if i >= len(shorter): #check for end of shortlist reached
                #    print '\nERR: unknown data mismatch!!!\n'
                #    print 'data dump:'
                #    print '===   PA   ==='
                #    print 'DATES='+str(PAdate)
                #    print 'VALUES='+str(PAscore)
                #    print '===interact==='
                #    print 'DATES='+str(interactDate)
                #    print 'VALUES='+str(interactScore)
                #    return
                if i >= len(shorter) or sdate[i].date() != ldate[i].date(
                ):  #remove extra dates not in shortlist
                    print 'value removed from ' + longName + ' at ' + str(
                        ldate[i])
                    longer.pop(i)
                    ldate.pop(i)
                    #shorter.insert(i,0)
                    #sdate.insert(i,ldate[i])
                    #print 'zero value inserted into '+shortName+' at '+str(sdate[i])
                    break
        if len(PAscore) <= 0 or len(interactScore) <= 0:
            print '\n ERR: data has no overlap!\n'
            return

    print '\t NAME   \tSTART \t\t\tEND \t\t\tLEN'
    print '\t interact\t' + str(interactDate[0]) + '\t' + str(
        interactDate[-1]) + '\t' + str(len(interactScore))
    print '\t PA     \t' + str(PAdate[0]) + '\t' + str(
        PAdate[-1]) + '\t' + str(len(PAscore)) + '\n'

    #print '\n'+str(interactDate)
    #print '\n'+str(PAdate)+'\n'

    pltName = 'PA vs interaction'
    print 'making plot "' + pltName + '"'
    pylab.figure(pltName)
    #p = pylab.figure()
    # lineGraph = p.add_subplot(211)
    # lineGraph.plot(x,y)
    #barGraph = p.add_subplot(111)
    pylab.plt.scatter(PAscore, interactScore, color='b')

    pylab.plt.ylabel('physical activity score')
    pylab.plt.xlabel('proteus PA influence')

    pylab.plt.draw()
Пример #8
0
def plot():
    # load in all the data
    PA_participants = list()
    PA = list()
    for pNum in range(HIGHEST_P_NUMBER + 1):  #cycle through all participants

        settings = setup(dataset='USF', data_loc=DATA_LOC, subject_n=pNum)

        try:
            pid = pNum
            fName = settings.getFileName('fitbit')
            pa = PAdata(fName, method='fitbit', timeScale='minute')
        except IOError:
            print 'p ' + str(pid) + ' PA file "' + fName + '" not found'
            continue

        # if no problems:
        PA_participants.append(pid)
        PA.append(pa)

#		print 'p' + pid + ' data loaded.'

    interact_participants = list()
    interactions = list()
    for pNum in range(HIGHEST_P_NUMBER + 1):  #cycle through all participants

        settings = setup(dataset='USF', data_loc=DATA_LOC, subject_n=pNum)

        try:
            pid = pNum

            fName = settings.getFileName('viewLog')
            interact = (interactionData(fName))
        except IOError:
            print 'p ' + str(pid) + ' interactions file not found'
            continue

        # if no problems:
        interact_participants.append(pid)
        interactions.append(interact)

#		print 'p' + pid + ' data loaded.'

#scale all the data
    PAscaler = 1.0 / 50.0
    for pnum in range(0, len(PA)):
        for i in range(0, len(PA[pnum])):
            PA[pnum].steps[i] *= PAscaler

    # shift to time since start...
    # for pnum in range(0,len(PA)):
    # start_t = min(PA[pnum].timestamp)
    # for i in range (0, len(PA[pnum])):
    # PA[pnum].timestamp[i] -= start_t

    # for pnum in range(0,len(interactions)):
    # start_t = min(interactions[pnum].t)
    # for i in range (0, len(interactions[pnum])):
    # interactions[pnum].t[i] -= start_t

    # # shift all to same daily cycle
    # for pnum in range(0,len(PA)):
    # timeOfDay = PA[pnum].time[0]	#assuming earliest time is first...
    # for i in range (0, len(PA[pnum])):
    # PA[pnum].time[i] += timeOfDay

    # timeOfDay = interactions[pnum].t[0] #assuming earliest time is first...
    # ToD_start = (timeOfDay.hour*60 + timeOfDay.minute) * 60
    # for i in range (0, len(interactions[pnum])):
    # interactions[pnum].t[i] += ToD_start

#	class Bunch:
#		''' bunches! makes a dictionary look like a class! bam! '''
#		__init__ = lambda self, **kw: setattr(self, '__dict__', kw)

# make the plots
    figName = "scatterplot_masterdash"
    pylab.figure(figName)
    pylab.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
    num = 0
    max_p = max(max(PA_participants), max(interact_participants)) + 1
    pCount = max(len(PA_participants), len(interact_participants)) + 1
    actualCount = 1
    for i in range(max_p):
        pylab.subplot(pCount, 1, actualCount)
        try:
            pa = PA[PA_participants.index(num)]
        except ValueError as m:
            print 'p' + m.message + ' of PA'
            pa = None  # Bunch(time=0,steps=0)
        try:
            interact = interactions[interact_participants.index(num)]
        except ValueError as m:
            print 'p' + m.message + ' of interactions'
            interact = None  # Bunch(t=0,v=0)

        num += 1

        isData = False
        try:
            pylab.scatter(pa.timestamp, pa.steps, marker='x', color='g')
            isData = True
        except AttributeError as e:
            pass
        try:
            pylab.scatter(interact.viewTimes, [0] * len(interact.viewTimes),
                          marker='+',
                          color=interact.viewMarkerColor)
            isData = True
        except AttributeError as e:
            pass

        if isData:
            actualCount += 1
Пример #9
0
def plot(settings):
    # load interaction data
    interact = interactionData(settings['interactionFileLoc'])
    interactScore, interactDate = segmentInteractionIntoDays(interact)

    # load PA data
    PA = PAdata(settings['PAfileLoc'])
    PAscore, PAdate = segmentPAIntoDays(PA,
                                        PAscoreFunction=getPAscore_postiveOnly)

    # data check
    print 'data is ' + str(len(PAscore)) + 'x' + str(len(interactScore))

    while (interactDate[0].date() != PAdate[0].date()) or (
            interactDate[-1].date() !=
            PAdate[-1].date()) or (len(interactScore) != len(PAscore)):
        print 'day mismatch: '
        #print 'data is ' + str(len(PAscore)) + 'x' + str(len(interactScore))
        print '\t NAME   \tSTART \t\t\tEND \t\t\tLEN'
        print '\t interact\t' + str(interactDate[0]) + '\t' + str(
            interactDate[-1]) + '\t' + str(len(interactScore))
        print '\t PA     \t' + str(PAdate[0]) + '\t' + str(
            PAdate[-1]) + '\t' + str(len(PAscore)) + '\n'
        if (PAdate[0].date() <
                interactDate[0].date()):  #if pa starts before interact
            print 'pa data removed from start'
            PAdate.pop(0)
            PAscore.pop(0)
        elif PAdate[0].date() > interactDate[0].date(
        ):  #if pa starts after interact
            print 'interact data removed from start'
            interactDate.pop(0)
            interactScore.pop(0)
        elif PAdate[-1].date() < interactDate[-1].date(
        ):  #if pa ends before interact
            print 'pa data removed from end'
            interactDate.pop()
            interactScore.pop()
        elif PAdate[-1].date() > interactDate[-1].date(
        ):  #if pa ends after interact
            print 'interact data removed from end'
            PAscore.pop()
            PAdate.pop()
        else:  # uneven values must be from missing days in middle of one dataset
            longer = list()
            shorter = list()
            ldate = list()
            sdate = list()
            shortName = ''
            if len(PAscore) > len(interactScore):
                shortName = 'interaction'
                longName = 'PA'
                longer = PAscore
                shorter = interactScore
                ldate = PAdate
                sdate = interactDate
            else:
                shortName = 'PA'
                longName = 'interaction'
                longer = interactScore
                shorter = PAscore
                ldate = interactDate
                sdate = PAdate
            for i in range(len(longer)):
                #if i >= len(shorter): #check for end of shortlist reached
                #	print '\nERR: unknown data mismatch!!!\n'
                #	print 'data dump:'
                #	print '===   PA   ==='
                #	print 'DATES='+str(PAdate)
                #	print 'VALUES='+str(PAscore)
                #	print '===interact==='
                #	print 'DATES='+str(interactDate)
                #	print 'VALUES='+str(interactScore)
                #	return
                if i >= len(shorter) or sdate[i].date() != ldate[i].date(
                ):  #remove extra dates not in shortlist
                    print 'value removed from ' + longName + ' at ' + str(
                        ldate[i])
                    longer.pop(i)
                    ldate.pop(i)
                    #shorter.insert(i,0)
                    #sdate.insert(i,ldate[i])
                    #print 'zero value inserted into '+shortName+' at '+str(sdate[i])
                    break
        if len(PAscore) <= 0 or len(interactScore) <= 0:
            print '\n ERR: data has no overlap!\n'
            return

    print '\t NAME   \tSTART \t\t\tEND \t\t\tLEN'
    print '\t interact\t' + str(interactDate[0]) + '\t' + str(
        interactDate[-1]) + '\t' + str(len(interactScore))
    print '\t PA     \t' + str(PAdate[0]) + '\t' + str(
        PAdate[-1]) + '\t' + str(len(PAscore)) + '\n'

    # modify interaction score to just separate (+) and (-) values
    interactions = list()
    for iScore in interactScore:
        if iScore > 0:
            interactions.append(1)  #'active')
        elif iScore < 0:
            interactions.append(-1)  #'sedentary')
        else:  # iScore = 0
            interactions.append(0)  #'0')

    pltName = 'user PA for sedentary & active avatar days'
    print 'making plot "' + pltName + '"'
    pylab.figure(pltName)
    activeBase = 0
    sedentBase = 0
    zeroBase = 0
    for day in range(len(PAscore)):
        #print 'day     ='+str(day)
        #print 'interact='+str(interactions[day])
        #print 'PAscore ='+str(PAscore[day])
        if interactions[day] > 0:
            pylab.plt.bar(interactions[day],
                          PAscore[day],
                          bottom=activeBase,
                          linewidth=1)
            activeBase += PAscore[day]
        elif interactions[day] < 0:
            pylab.plt.bar(interactions[day],
                          PAscore[day],
                          bottom=sedentBase,
                          linewidth=1)
            sedentBase += PAscore[day]
        else:  # interactions[day] == 0
            pylab.plt.bar(interactions[day],
                          PAscore[day],
                          bottom=zeroBase,
                          linewidth=1)
            zeroBase += PAscore[day]

    pylab.plt.ylabel('physical activity score')
    pylab.plt.xlabel(
        '<-sedentary                   zero                   active->\navatar behavior'
    )

    pylab.plt.draw()