<commit_before>
#  IATI Data Quality, tools for Data QA on IATI-formatted  publications
#  by <NAME>, <NAME>, <NAME> and <NAME>
#
#  Copyright (C) 2013  Publish What You Fund
#
#  This programme is free software; you may redistribute and/or modify
#  it under the terms of the GNU Affero General Public License v3.0

import json
import urllib2

from iatidq import db
import models

def aggregate_percentages(data):
    # Aggregates results data for a specific runtime.

    packages = set(map(lambda x: (x[4]), data))
    hierarchies = set(map(lambda x: (x[2]), data))
    tests = set(map(lambda x: (x[0].id,), data))

    d = dict(map(lambda x: ((x[0].id,x[1],x[2],x[4]),(x[3])), data))
    out = []
    for p in packages:
        for t in tests:
            for h in hierarchies:
                try: fail = d[(t[0],0,h,p)]
                except: fail = 0
                try: success = d[(t[0],1,h,p)]
                except: success = 0
                try:
                    percentage = int((float(success)/(fail+success)) * 100)
                except ZeroDivisionError:
                    # Don't return data to DB if there are no results
                    continue
                data = {}
                data = {
                    "test_id": t[0],
                    "percentage_passed": percentage,
                    "total_results": fail+success,
                    "hierarchy": h,
                    "package_id": p
                }
                out.append(data)
    return out

def aggregate_percentages_org(data):
    # Aggregates results data for a specific runtime.

    packages = set(map(lambda x: (x[4]), data))
    hierarchies = set(map(lambda x: (x[2]), data))
    tests = set(map(lambda x: (x[0].id,), data))
    organisations = set(map(lambda x: (x[5]), data))

    d = dict(map(lambda x: ((x[0].id,x[1],x[2],x[4],x[5]),(x[3])), data))
    out = []
    for p in packages:
        for t in tests:
            for h in hierarchies:
                for o in organisations:
                    try: fail = d[(t[0],0,h,p,o)]
                    except: fail = 0
                    try: success = d[(t[0],1,h,p,o)]
                    except: success = 0
                    try:
                        percentage = int((float(success)/(fail+success)) * 100)
                    except ZeroDivisionError:
                        # Don't return data to DB if there are no results
                        continue
                    data = {}
                    data = {
                        "test_id": t[0],
                        "percentage_passed": percentage,
                        "total_results": fail+success,
                        "hierarchy": h,
                        "package_id": p,
                        "organisation_id": o
                        }
                    out.append(data)
    return out<commit_msg>don't use one-member tuple<commit_after>
#  IATI Data Quality, tools for Data QA on IATI-formatted  publications
#  by <NAME>, <NAME>, <NAME> and <NAME>
#
#  Copyright (C) 2013  Publish What You Fund
#
#  This programme is free software; you may redistribute and/or modify
#  it under the terms of the GNU Affero General Public License v3.0

import json
import urllib2

from iatidq import db
import models

def aggregate_percentages(data):
    # Aggregates results data for a specific runtime.

    packages = set(map(lambda x: (x[4]), data))
    hierarchies = set(map(lambda x: (x[2]), data))
    tests = set(map(lambda x: (x[0].id), data))

    d = dict(map(lambda x: ((x[0].id,x[1],x[2],x[4]),(x[3])), data))
    out = []
    for p in packages:
        for t in tests:
            for h in hierarchies:
                try: fail = d[(t,0,h,p)]
                except: fail = 0
                try: success = d[(t,1,h,p)]
                except: success = 0
                try:
                    percentage = int((float(success)/(fail+success)) * 100)
                except ZeroDivisionError:
                    # Don't return data to DB if there are no results
                    continue
                data = {}
                data = {
                    "test_id": t,
                    "percentage_passed": percentage,
                    "total_results": fail+success,
                    "hierarchy": h,
                    "package_id": p
                }
                out.append(data)
    return out

def aggregate_percentages_org(data):
    # Aggregates results data for a specific runtime.

    packages = set(map(lambda x: (x[4]), data))
    hierarchies = set(map(lambda x: (x[2]), data))
    tests = set(map(lambda x: (x[0].id), data))
    organisations = set(map(lambda x: (x[5]), data))

    d = dict(map(lambda x: ((x[0].id,x[1],x[2],x[4],x[5]),(x[3])), data))
    out = []
    for p in packages:
        for t in tests:
            for h in hierarchies:
                for o in organisations:
                    try: fail = d[(t,0,h,p,o)]
                    except: fail = 0
                    try: success = d[(t,1,h,p,o)]
                    except: success = 0
                    try:
                        percentage = int((float(success)/(fail+success)) * 100)
                    except ZeroDivisionError:
                        # Don't return data to DB if there are no results
                        continue
                    data = {}
                    data = {
                        "test_id": t,
                        "percentage_passed": percentage,
                        "total_results": fail+success,
                        "hierarchy": h,
                        "package_id": p,
                        "organisation_id": o
                        }
                    out.append(data)
    return out
{'fraction_non_alphanumeric': 0.11688989389459037, 'fraction_numerical': 0.014611236736823796, 'mean_word_length': 1.4395417904115402, 'pattern_counts': {'":': 22, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '473350', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1608, 'n_words': 480}