Example #1
0
def chi_sq_edits_per_user_2(user):
    notes = Note.objects.filter(owner=user)
    edits_ = ca.note_edits_for_user(notes[0].owner)
    chiMatrix = r.matrix(c([0 for i in range(14)]), ncol=2,nrow=7)
    for n in notes:
        if n.jid in edits_:
            createdDate = wUtil.msecToDate(n.created)
            createdDOW = createdDate.weekday()
            topOfDay = dd.datetime(year=createdDate.year, month=createdDate.month, day=createdDate.day)
            for edit_action in edits_[n.jid]:
                timeDelta = wUtil.msecToDate(edit_action['when']) - topOfDay
                if timeDelta.days >= 7:
                    ## For each edit of this note, find editDOW and increment spot in matrix
                    editDOW = wUtil.msecToDate(edit_action['when']).weekday()
                    if createdDOW == editDOW:
                        chiMatrix[createdDOW] += 1
                    else:
                        chiMatrix[createdDOW+7] += 1
                    pass
                pass
            pass
        pass
    print chiMatrix
    print '--------------------------------------------'
    result = r('chisq.test')(chiMatrix,p=r.c(1/7,6/7))
    print result
    print '--------------------------------------------'
    return result
Example #2
0
def chisq_edits_for_users_2(users):
    chiMatrix = r.matrix(c([0 for i in range(14)]), ncol=2,nrow=7)
    for user in users:
        print user.id
        notes = Note.objects.filter(owner=user)
        edits_ = ca.note_edits_for_user(user)
        for n in notes:
            if n.jid in edits_:
                createdDOW = wUtil.msecToDate(n.created).weekday()
                for edit_action in edits_[n.jid]:
                    timeDelta = edit_action['when'] - n.created
                    if timeDelta > 1000*3600*24*7:
                    ## For each edit of this note, find editDOW and increment spot in matrix
                        editDOW = wUtil.msecToDate(edit_action['when']).weekday()
                        if createdDOW == editDOW:
                            chiMatrix[createdDOW] += 1
                        else:
                            chiMatrix[createdDOW+7] += 1
                            pass
                        pass
                    pass
                pass
            pass
        pass
    result = r('chisq.test')(chiMatrix, p=r.c(1/7,6/7))
    return (result, chiMatrix)
Example #3
0
def calc_virtual_midnight(user, window=6, before=None, after=None):
    allLogs = ActivityLog.objects.filter(owner=user,
                                         action__in=[
                                             'note-add', 'note-edit',
                                             'note-save', 'note-delete',
                                             'sidebar-open', 'sidebar-close',
                                             'significant-scroll',
                                             'notes-reordered'
                                         ])
    ## Bin by hour
    hourlyActions = [0 for i in range(24)]
    for log in allLogs:
        if before is not None and int(log.when) > before:
            continue
        elif after is not None and int(log.when) < after:
            continue
        else:
            hourlyActions[wUtil.msecToDate(log.when).hour] += 1
        pass
    hourlyActions.extend(
        hourlyActions
    )  ## --> creates a double! hr = [0,1,...23,24,1,2,3...] making window code easier!
    ## Determine min k-hour window of activity
    minHour, minSum = 0, sum(
        hourlyActions[0:window])  ## initialize to some valid choice
    for hr in xrange(0, 25):  ## 0-5, 1-6, ...,23-3, 24-4 ##
        currSum = sum(hourlyActions[hr:hr + window])
        if currSum < minSum:
            minSum = currSum
            minHour = hr
        pass
    ## Report what the center of the minimum window is!
    print "The center of a", window, "hour window is: ", (minHour +
                                                          (window / 2.0)) % 24
    return (minHour + (window / 2.0)) % 24
Example #4
0
def bTime(filename, user, title='title'):
    COL_SEGMENTS, ROW_GROUPS, GROUP_TYPES = 3, 24, 1
    aveSize = lambda a,b: int(float(a)/float(b)) if b != 0 else 0  ## a=quantity of something per how many b elts, if no b, return 0
    ##msecToDate = lambda msec : dd.fromtimestamp(float(msec)/1000.0)
    dtToDayMsec = lambda dt: int((((dt.weekday()*24+dt.hour)*60+dt.minute)*60+dt.second)*1000 + float(dt.microsecond)/1000.0)
    dtToHourMsec = lambda dt: int(((dt.hour*60+dt.minute)*60+dt.second)*1000 + float(dt.microsecond)/1000.0)
    hrsToIndex = lambda colSeg, group, hrOfDay : colSeg + (hrOfDay*GROUP_TYPES+group)*COL_SEGMENTS  # for group in [0,GROUP_TYPES-1]
    notes = user.note_owner.all()
    allLogs = ActivityLog.objects.filter(owner=user, action__in=['note-add','note-save','note-delete'])
    ##points = {'note-add':[0 for i in xrange(0,ROW_GROUPS*COL_SEGMENTS)],'note-save':[0 for i in xrange(0,24)],'note-delete':[0 for i in xrange(0,24)]}
    data = r.matrix(0,nrow=COL_SEGMENTS, ncol=ROW_GROUPS*GROUP_TYPES)
    for log in allLogs:
        noteArr = notes.filter(jid=log.noteid)
        if len(noteArr) < 1:  ## Processing logs for which we still
            continue          ## have the note (deleted or not)
        note = noteArr[0]
        aDate, bDate = wUtil.msecToDate(log.when), wUtil.msecToDate(note.created)
        aDay, aHour = aDate.weekday(), aDate.hour
        ##dIndex = hrsToIndex(0, aHour)
        ##points[log.action][dIndex] += 1
        ##data[dIndex] += 1
        if (log.action == 'note-add'):      ## Record Add
            data[hrsToIndex(0,0,aHour)] += 1
        elif (log.action == 'note-save'):   ## Record Save: Split (edit on day of note.created vs not)
            data[hrsToIndex(1,0,aHour)] += 1
        elif (log.action == 'note-delete'): ## Record Death
            data[hrsToIndex(2,0,aHour)] += 1
        ##xTime = dtToDayMsec(aDate)
        ##yTime = dtToHourMsec(aDate)##dd(year=1,month=1,day=1, hour=aDate.hour,minute=aDate.minute,microsecond=aDate.microsecond))
        pass
    r.png(file=cap.make_filename(filename), w=2000,h=1000)
    if title == 'title':
        title = "#Notes:#Logs:Email:ID -- " + str(notes.count()) + ":" + str(allLogs.count()) + ":" + user.email + ":" + str(user.id)
    dayNames = ["Mon","Tues","Wed","Thur","Fri","Sat","Sun","Mon"]
    hourNames = ['midnight', '1am','2am','3am','4am','5am','6am','7am','8am','9am','10am','11am','noon']
    hourNames.extend(['1pm','2pm','3pm','4pm','5pm','6pm','7pm','8pm','9pm','10pm','11pm'])
    ##names = []
    ##[names.extend([hrName, '','']) for hrName in hourNames]
    r.barplot(data, main=title,ylab='# Action Logs',beside=False, names=hourNames, col=r.c('green', 'orange', 'black'))##, width=c(widths), col=colors)
    ##
    ##r.barplot(points['note-add'], cex=8.0,col = "green", pch='o',xlab="Day Of Week", ylab="Hour of Day",main=title, axes=False, xlim=r.c(0,7*24*3600*1000), ylim=r.c(0,24*3600*1000))
    ##r.points(points['note-save'], cex=4.0,col = "purple", pch=17)
    ##r.points(points['note-delete'], cex=4.0,col = "dark red", pch='x')
    ##r.axis(1, at=c([float(x*24*60*60*1000.0) for x in range(0,8)]), labels=c([x for x in dayNames]), tck=1)
    ##r.axis(2, at=c([float(y*60*60*1000.0) for y in range(0,25)]), labels=c([x for x in hourNames]), tck=1)
    devoff()
Example #5
0
def col_of_edit(initText,type,when=0):
  if type == 'outer':
    if ca.str_n_urls(initText) >= 1: return 'blue'
    elif ca.str_n_emails(initText) >= 1: return 'orange'
    return 'black'
  elif type == 'inner':  ## Color of triangle hard to see!
    ## Fill in for inner edit: purple if 1 line note, red if multi-line
    day = wUtil.msecToDate(when).weekday()
    return ['red','orange','yellow','green','blue','gray', 'brown'][day]
    ##return 'blue' if wUtil.str_sum_lines(initText) <= 1 else 'red'
  else:
    print 'invalid type given to col_of_edit in wFunc.py'
Example #6
0
def col_of_edit(initText, type, when=0):
    if type == 'outer':
        if ca.str_n_urls(initText) >= 1: return 'blue'
        elif ca.str_n_emails(initText) >= 1: return 'orange'
        return 'black'
    elif type == 'inner':  ## Color of triangle hard to see!
        ## Fill in for inner edit: purple if 1 line note, red if multi-line
        day = wUtil.msecToDate(when).weekday()
        return ['red', 'orange', 'yellow', 'green', 'blue', 'gray',
                'brown'][day]
        ##return 'blue' if wUtil.str_sum_lines(initText) <= 1 else 'red'
    else:
        print 'invalid type given to col_of_edit in wFunc.py'
Example #7
0
def calc_virtual_midnight(user, window=6, before=None,after=None):
    allLogs = ActivityLog.objects.filter(owner=user, action__in = ['note-add','note-edit','note-save','note-delete','sidebar-open','sidebar-close','significant-scroll','notes-reordered'])
    ## Bin by hour
    hourlyActions = [0 for i in range(24)]
    for log in allLogs: 
        if before is not None and int(log.when) > before:
            continue
        elif after is not None and int(log.when) < after:
            continue
        else:
            hourlyActions[wUtil.msecToDate(log.when).hour] += 1
        pass
    hourlyActions.extend(hourlyActions) ## --> creates a double! hr = [0,1,...23,24,1,2,3...] making window code easier!
    ## Determine min k-hour window of activity
    minHour, minSum = 0,  sum(hourlyActions[0:window])  ## initialize to some valid choice
    for hr in xrange(0,25): ## 0-5, 1-6, ...,23-3, 24-4 ## 
        currSum = sum(hourlyActions[hr:hr+window])
        if currSum < minSum:
            minSum = currSum
            minHour = hr
        pass
    ## Report what the center of the minimum window is!
    print "The center of a", window, "hour window is: ", (minHour + (window/2.0)) % 24
    return (minHour + (window/2.0)) % 24
Example #8
0
def compareEditsBinWeek(filename, notes, title='Note Lifelines', color_function=one_or_no_url_redblk, YMAG=0.5):
  allLogs = ActivityLog.objects.filter(owner=notes[0].owner, action__in=['note-add','note-delete'])
  ##firstBirth = float(min([x[0] for x in notes.values_list('created')]))
  global firstBirth
  title = "%s -- %s %s %s  " % (title,str(notes.count()),notes[0].owner.email,notes[0].owner.id)
  ## Meta-data for points
  points = {'note-add':r.c(), 'note-delete':r.c()} ## Shortened to just the two, since note-edit added later
  births, deaths = {}, {},
  today = time.time()*1000.0
  print "saving to %s " % cap.make_filename(filename)
  r.png(file=cap.make_filename(filename), w=3200,h=1600) ## 3200x1600, 9600x4800, 12.8x6.4
  colors=dict([(n.jid,color_function(n)) for n in notes])
  
  ## 7 creation bins, each with 7 edit bins inside
  ## Each of 49 bins holds entries detailing when edits happen - plot these on line for that bin!
  creationBins = [[r.c() for i in xrange(7)] for i in xrange(7)] 

  # order notes by creation
  jids = [id[0] for id in notes.order_by("created").values_list('jid')]
  jid2idx = dict([(jids[i],i) for i in xrange(len(jids))])
  for log in allLogs:
     noteArr = notes.filter(jid=log.noteid)
     if len(noteArr) < 1:
        continue
     note = noteArr[0]
     births[note.jid] = jid2idx[note.jid]
     if not note.deleted and note.jid not in deaths:
        deaths[note.jid] = today
        pass
     if (log.action == 'note-delete'):
        deaths[note.jid] = float(log.when-note.created)
     points[log.action] = r.rbind(points[log.action],c([jid2idx[note.jid],float(log.when-note.created)]))
     pass

  # compute the edits, compile the colors
  edits_ = ca.note_edits_for_user(notes[0].owner)
  points['note-edit'] = r.c()
  edit_dir, edit_delta, colors_r = r.c(), r.c(), r.c(),
  ymax = 0

  points['bin-edits'] = r.c()
  weekdayColors = ['red','orange','yellow','green','blue','grey','brown']
  edit_col = r.c()
  maxDelta = 0


  for n in notes:  ## wCom: not all notes eval'd here may have note-add events !!
    colors_r = r.c(colors_r,colors[n.jid])
    if n.jid in edits_:
      for edit_action in edits_[n.jid]:
        ## Add edit to creationBin
        noteDOW = wUtil.msecToDate(n.created).weekday()
        editDOW = wUtil.msecToDate(edit_action['when']).weekday()
        timeDelta = edit_action['when'] - n.created 
        ##if edit_action['when'] != None and edit_action['when'] > firstBirth and edit_action['when'] < ca.DATABASE_SNAPSHOT_TIME:
          ## Current y-val plots time between edit and creation -- second y-val in comment plots absolute time of edit
        if timeDelta > 1000*3600*24*7:
          points['bin-edits'] = r.rbind(points['bin-edits'], c([int(noteDOW*7+editDOW), float(timeDelta)]))
          maxDelta = max(float(timeDelta), maxDelta)
        
          ##KEEP: points['bin-edits'] = r.rbind(points['bin-edits'], c([int(noteDOW*7+editDOW), float(edit_action['when'])]) )        
          edit_col = r.c(edit_col, weekdayColors[editDOW])
        pass
  xl,yl="Created Date", "Action Date"
  
  #maxDelta = 1000*3600*24*60

  r.plot(points['bin-edits'], cex=3.0, col=edit_col, pch='x' ,xlab=xl,ylab=yl, main=title, xlim=r.c(0, 48), ylim=c([0, float(maxDelta)]), axes=False )
  ##KEEP: , axes=False)#, ylim=c([firstBirth, ca.DATABASE_SNAPSHOT_TIME]) )
  
  ## 1 below, 2 left
  r.axis(1, at=c(range(0,49,7)), labels=c(['Mon','Tue','Wed','Thur','Fri','Sat','Sun']), col='grey' )  

  ## Something wrong here  #help!#
  ## Graph is coming up with tiny y-axis plotting # of days between note-create and edit event
  yTicks, yNames, ySep = [], [], 1000*3600*24 # daily ticks
  for tickTime in range(0,maxDelta, ySep): # daily ticks
    yTicks.append(tickTime)
    yNames.append(float(tickTime)/ySep)
    pass
  r.axis(2, at=c(yTicks), labels=c(yNames))
  #help!#
  
  ##r.points(points['note-edit'], col=colors_r, pch=edit_dir,  cex=edit_delta)  
  ##r.points(points['note-delete'], cex=2.0,col = colors_r, pch='x')
  yWeeks = [int(y) for y in range(int(msecToWeek(firstBirth)), int(msecToWeek(time.time()*1000)), 1)]

  for x in births.keys():
     if x in deaths:
       stillalive = (today-deaths[x] < 0.001)
       color = colors[x] # ('green' if stillalive else 'black')
       ##r.lines( c( [float(births[x])]*2)     ,c([ float(births[x]),float(deaths[x]-births[x]) ]), col=color)
       pass
     pass
  for i in range(0,49,7):
    r.abline(v=float(i)-0.5, col='purple')
    pass
  for i in range(0,49):
    r.abline(v=float(i)-0.5, col='gray')
  devoff()
Example #9
0
def compareEditsBinWeek(filename,
                        notes,
                        title='Note Lifelines',
                        color_function=one_or_no_url_redblk,
                        YMAG=0.5):
    allLogs = ActivityLog.objects.filter(
        owner=notes[0].owner, action__in=['note-add', 'note-delete'])
    ##firstBirth = float(min([x[0] for x in notes.values_list('created')]))
    global firstBirth
    title = "%s -- %s %s %s  " % (title, str(
        notes.count()), notes[0].owner.email, notes[0].owner.id)
    ## Meta-data for points
    points = {
        'note-add': r.c(),
        'note-delete': r.c()
    }  ## Shortened to just the two, since note-edit added later
    births, deaths = {}, {},
    today = time.time() * 1000.0
    print "saving to %s " % cap.make_filename(filename)
    r.png(file=cap.make_filename(filename), w=3200,
          h=1600)  ## 3200x1600, 9600x4800, 12.8x6.4
    colors = dict([(n.jid, color_function(n)) for n in notes])

    ## 7 creation bins, each with 7 edit bins inside
    ## Each of 49 bins holds entries detailing when edits happen - plot these on line for that bin!
    creationBins = [[r.c() for i in xrange(7)] for i in xrange(7)]

    # order notes by creation
    jids = [id[0] for id in notes.order_by("created").values_list('jid')]
    jid2idx = dict([(jids[i], i) for i in xrange(len(jids))])
    for log in allLogs:
        noteArr = notes.filter(jid=log.noteid)
        if len(noteArr) < 1:
            continue
        note = noteArr[0]
        births[note.jid] = jid2idx[note.jid]
        if not note.deleted and note.jid not in deaths:
            deaths[note.jid] = today
            pass
        if (log.action == 'note-delete'):
            deaths[note.jid] = float(log.when - note.created)
        points[log.action] = r.rbind(
            points[log.action],
            c([jid2idx[note.jid],
               float(log.when - note.created)]))
        pass

    # compute the edits, compile the colors
    edits_ = ca.note_edits_for_user(notes[0].owner)
    points['note-edit'] = r.c()
    edit_dir, edit_delta, colors_r = r.c(), r.c(), r.c(),
    ymax = 0

    points['bin-edits'] = r.c()
    weekdayColors = [
        'red', 'orange', 'yellow', 'green', 'blue', 'grey', 'brown'
    ]
    edit_col = r.c()
    maxDelta = 0

    for n in notes:  ## wCom: not all notes eval'd here may have note-add events !!
        colors_r = r.c(colors_r, colors[n.jid])
        if n.jid in edits_:
            for edit_action in edits_[n.jid]:
                ## Add edit to creationBin
                noteDOW = wUtil.msecToDate(n.created).weekday()
                editDOW = wUtil.msecToDate(edit_action['when']).weekday()
                timeDelta = edit_action['when'] - n.created
                ##if edit_action['when'] != None and edit_action['when'] > firstBirth and edit_action['when'] < ca.DATABASE_SNAPSHOT_TIME:
                ## Current y-val plots time between edit and creation -- second y-val in comment plots absolute time of edit
                if timeDelta > 1000 * 3600 * 24 * 7:
                    points['bin-edits'] = r.rbind(
                        points['bin-edits'],
                        c([int(noteDOW * 7 + editDOW),
                           float(timeDelta)]))
                    maxDelta = max(float(timeDelta), maxDelta)

                    ##KEEP: points['bin-edits'] = r.rbind(points['bin-edits'], c([int(noteDOW*7+editDOW), float(edit_action['when'])]) )
                    edit_col = r.c(edit_col, weekdayColors[editDOW])
                pass
    xl, yl = "Created Date", "Action Date"

    #maxDelta = 1000*3600*24*60

    r.plot(points['bin-edits'],
           cex=3.0,
           col=edit_col,
           pch='x',
           xlab=xl,
           ylab=yl,
           main=title,
           xlim=r.c(0, 48),
           ylim=c([0, float(maxDelta)]),
           axes=False)
    ##KEEP: , axes=False)#, ylim=c([firstBirth, ca.DATABASE_SNAPSHOT_TIME]) )

    ## 1 below, 2 left
    r.axis(1,
           at=c(range(0, 49, 7)),
           labels=c(['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']),
           col='grey')

    ## Something wrong here  #help!#
    ## Graph is coming up with tiny y-axis plotting # of days between note-create and edit event
    yTicks, yNames, ySep = [], [], 1000 * 3600 * 24  # daily ticks
    for tickTime in range(0, maxDelta, ySep):  # daily ticks
        yTicks.append(tickTime)
        yNames.append(float(tickTime) / ySep)
        pass
    r.axis(2, at=c(yTicks), labels=c(yNames))
    #help!#

    ##r.points(points['note-edit'], col=colors_r, pch=edit_dir,  cex=edit_delta)
    ##r.points(points['note-delete'], cex=2.0,col = colors_r, pch='x')
    yWeeks = [
        int(y) for y in range(int(msecToWeek(firstBirth)),
                              int(msecToWeek(time.time() * 1000)), 1)
    ]

    for x in births.keys():
        if x in deaths:
            stillalive = (today - deaths[x] < 0.001)
            color = colors[x]  # ('green' if stillalive else 'black')
            ##r.lines( c( [float(births[x])]*2)     ,c([ float(births[x]),float(deaths[x]-births[x]) ]), col=color)
            pass
        pass
    for i in range(0, 49, 7):
        r.abline(v=float(i) - 0.5, col='purple')
        pass
    for i in range(0, 49):
        r.abline(v=float(i) - 0.5, col='gray')
    devoff()