Beispiel #1
0
    def summary_row(self, field_name, query_set, headers, count):

        resolvers = {'Field Name': lambda field, query: field_name,
                     'Explanation': lambda field, query: explain(field_name),
                     'Mean': lambda field, query: query.aggregate(Avg(field)).popitem()[1],  # grabs value
                     'StdDev': std_dev,
                     'Low': lambda field, query: query.aggregate(Min(field)).popitem()[1],  # grabs value
                     'High': lambda field, query: query.aggregate(Max(field)).popitem()[1],  # grabs value
                     'p5': lambda field, query:  django_percentile(field, query, 5, count),
                     'p25': lambda field, query: django_percentile(field, query, 25, count),
                     'p50': lambda field, query: django_percentile(field, query, 50, count),
                     'p75': lambda field, query: django_percentile(field, query, 75, count),
                     'p95': lambda field, query: django_percentile(field, query, 95, count),
        }
        row = {}
        for column in headers:
            if column in resolvers.keys():
                row[column] = resolvers[column](field_name, query_set)
            else:
                raise NotImplemented("The column name " + column + " does not have a function associated with it.")

        return row
Beispiel #2
0
    def get_summary_data_table(self):
        """Generates a python data structure with all the information for the CSV file.  Structured in a list[row][column] 2D array that mimics the spreadsheet
        layout and follows the column order.  This could be changed to an OrderedDict( OrderedDict<column, value> ) if you want more flexibility in storage
        and retrieval, but it's simplest to just calculate and store them in order."""
        from Results.models import DailyByProductionType
        headers = ['Field Name', 'Explanation', 'Mean', 'Low', 'High', 'p5', 'p25', 'p50', 'p75', 'p95']
        data = []  # 2D

        fields_of_interest = [field for field, val in DailyByProductionType() if 'Cumulative' in explain(field)]  # only cumulative, last day fields in DailyByProductionType for all production types
        grab_pt = 'infcUIni infcAIni infcUAir infcAAir infcUDir infcADir infcUInd infcAInd expcUDir expcADir expcUInd expcAInd detcUClin detcAClin detcUTest detcATest descUIni descAIni descUDet descADet descUDirFwd descADirFwd descUIndFwd descAIndFwd descUDirBack descADirBack descUIndBack descAIndBack descURing descARing vaccUIni vaccAIni vaccURing vaccARing vacwUMax vacwAMax vacwUMaxDay vacwAMaxDay vacwUTimeMax vacwUTimeAvg exmcUDirFwd exmcADirFwd exmcUIndFwd exmcAIndFwd exmcUDirBack exmcADirBack exmcUIndBack exmcAIndBack tstcUDirFwd tstcADirFwd tstcUIndFwd tstcAIndFwd tstcUDirBack tstcADirBack tstcUIndBack tstcAIndBack tstcUTruePos tstcUTrueNeg tstcUFalsePos tstcUFalseNeg firstDetection lastDetection firstVaccination firstDestruction tsdUSusc tsdASusc tsdULat tsdALat tsdUSubc tsdASubc tsdUClin tsdAClin tsdUNImm tsdANImm tsdUVImm tsdAVImm tsdUDest tsdADest'.split()
        query_set = DailyByProductionType.objects.filter(last_day=True, production_type__isnull=True, )
        count = query_set.count()  # only needs to be evaluated once per model
        for field_name in set().union(grab_pt, fields_of_interest):
            data.append(self.summary_row(field_name, query_set, headers, count))

        grab_controls = 'deswUMax deswAMax deswUMaxDay deswAMaxDay deswUTimeMax deswUTimeAvg deswUDaysInQueue deswADaysInQueue detOccurred firstDetUInf firstDetAInf vaccOccurred destrOccurred diseaseDuration outbreakDuration'.split()
        query_set = DailyControls.objects.filter(last_day=True)
        count = query_set.count()  # only needs to be evaluated once per model
        for field_name in grab_controls:
            data.append(self.summary_row(field_name, query_set, headers, count))

        # TODO: These are field names mentioned in the original NAADSM file that I have not yet accounted for
        unaccounted_for = 'infcUAll infcAAll expcUAll expcAAll trcUDirFwd trcADirFwd trcUIndFwd trcAIndFwd trcUDirpFwd trcADirpFwd trcUIndpFwd trcAIndpFwd trcUDirBack trcADirBack trcUIndBack trcAIndBack trcUDirpBack trcADirpBack trcUIndpBack trcAIndpBack trcUDirAll trcADirAll trcUIndAll trcAIndAll trcUAll trcAAll tocUDirFwd tocUIndFwd tocUDirBack tocUIndBack tocUDirAll tocUIndAll tocUAll detcUAll detcAAll descUAll descAAll vaccUAll vaccAAll exmcUDirAll exmcADirAll exmcUIndAll exmcAIndAll exmcUAll exmcAAll tstcUDirAll tstcADirAll tstcUIndAll tstcAIndAll tstcUAll tstcAAll tstcATruePos tstcATrueNeg tstcAFalsePos tstcAFalseNeg zoncFoci diseaseEnded outbreakEnded'.split()

        return headers, data