src/ldt/ldt/ldt_utils/stat.py
author verrierj
Fri, 27 Jan 2012 10:27:52 +0100
changeset 468 d1ff0694500b
parent 467 a1e9f5e91791
child 474 5e7942f049a6
permissions -rw-r--r--
Add polemic properties to contents
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
     1
from django.conf import settings
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     2
from ldt.ldt_utils.models import Segment
354
ecd4d57d0b40 Last annotated contents can be retrieved using the field content.last_annotated
verrierj
parents: 351
diff changeset
     3
import datetime
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
     4
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     5
def update_stat_content(content):
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     6
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     7
    nb_division = settings.DIVISIONS_FOR_STAT_ANNOTATION
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     8
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     9
    segments = Segment.objects.filter(content=content)
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    10
    buckets = [0] * nb_division 
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    11
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    12
    size_division = content.duration / nb_division
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    13
    limits = [x * size_division for x in range(nb_division+1)]
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    14
    nb_annotation = len(segments)
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    15
    
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    16
    polemic_positive = 0
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    17
    polemic_negative = 0
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    18
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    19
    for segment in segments:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    20
        
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    21
        begin = segment.start_ts
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    22
        end = segment.start_ts + segment.duration
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    23
        
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    24
        if segment.is_polemic('OK'):
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    25
            polemic_positive += 1
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    26
        if segment.is_polemic('KO'):
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    27
            polemic_negative += 1
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    28
        
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    29
        buckets = find_buckets(buckets, limits, begin, end)
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    30
        
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    31
    content.annotation_volume = buckets
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    32
    content.nb_annotation = nb_annotation
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    33
    content.polemic_positive = polemic_positive
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    34
    content.polemic_negative = polemic_negative
420
38e46b1d0bd3 Small bugfixes
verrierj
parents: 415
diff changeset
    35
    
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    36
    content.save()     
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    37
 
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    38
   
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    39
def update_stat_project(project, contents_only=[]):
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    40
        if project.state != 2:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    41
            return None
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    42
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    43
        if contents_only:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    44
            contents = contents_only
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    45
        else:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    46
            contents = project.contents.all()
351
74f898bd0983 Fix bug when computing stat on a project where the media is referenced in the xml but is not included in the contents field
verrierj
parents: 343
diff changeset
    47
            
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    48
        for c in contents:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    49
            update_stat_content(c)
420
38e46b1d0bd3 Small bugfixes
verrierj
parents: 415
diff changeset
    50
            
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    51
        return True
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    52
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    53
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    54
def add_annotation_to_stat(content, begin, end):
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    55
    
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    56
    if not content.nb_annotation or not content.stat_annotation:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    57
        content.nb_annotation = 1
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    58
        content.stat_annotation = get_empty_stat_vector()
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    59
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    60
    buckets = find_buckets(content.stat_annotation, begin, end)
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    61
    content.annotation_volume = buckets
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    62
    content.last_annotated = datetime.datetime.now()
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    63
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    64
    content.save()        
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    65
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    66
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    67
def get_string_from_buckets(buckets):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    68
    s = "%s" % buckets
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    69
    s = s[1:-1].replace(' ', '')
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    70
    return s
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    71
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    72
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    73
def get_buckets_from_string(string):
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    74
    return [int(x) for x in string.split(',')]
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    75
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    76
def get_empty_stat_vector():
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    77
    return [0] * (settings.DIVISIONS_FOR_STAT_ANNOTATION + 4) # + 4 for polemic syntax
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    78
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    79
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    80
def find_buckets(buckets, limits, begin, end):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    81
    
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    82
    if len(buckets)+1 != len(limits):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    83
        raise ValueError("There should be as many buckets as those defined by limits.")
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    84
    
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    85
    has_started = False  
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    86
        
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    87
    for i in range(len(limits)-1):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    88
        if not has_started:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    89
            if limits[i] <= begin and begin <= limits[i+1]:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    90
                buckets[i] += 1
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    91
                has_started = True
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    92
                #print "Starts after timecode %s" % limits[i]
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    93
        elif limits[i] > end:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    94
            #print "Ends before timecode %s" % limits[i]
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    95
            break
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    96
        else:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    97
            buckets[i] += 1
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    98
            
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    99
    return buckets
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   100