Exemple #1
0
    def get_statistics(self, tclass, users=[]):
        from aiida.orm.querybuilder import QueryBuilder as QB
        from aiida.orm import User
        from collections import Counter

        def count_statistics(dataset):
            def get_statistics_dict(dataset):
                results = {}
                for count, typestring in sorted(
                    (v, k) for k, v in dataset.iteritems())[::-1]:
                    results[typestring] = count
                return results

            count_dict = {}

            types = Counter([r[3] for r in dataset])
            count_dict["types"] = get_statistics_dict(types)

            ctimelist = [r[1].strftime("%Y-%m") for r in dataset]
            ctime = Counter(ctimelist)
            count_dict["ctime_by_month"] = get_statistics_dict(ctime)

            ctimelist = [r[1].strftime("%Y-%m-%d") for r in dataset]
            ctime = Counter(ctimelist)
            count_dict["ctime_by_day"] = get_statistics_dict(ctime)

            mtimelist = [r[2].strftime("%Y-%m") for r in dataset]
            mtime = Counter(ctimelist)
            count_dict["mtime_by_month"] = get_statistics_dict(mtime)

            mtimelist = [r[1].strftime("%Y-%m-%d") for r in dataset]
            mtime = Counter(ctimelist)
            count_dict["mtime_by_day"] = get_statistics_dict(mtime)

            return count_dict

        statistics = {}

        q = QB()
        q.append(tclass, project=['id', 'ctime', 'mtime', 'type'], tag='node')
        q.append(User, creator_of='node', project='email')
        qb_res = q.all()

        # total count
        statistics["total"] = len(qb_res)

        node_users = Counter([r[4] for r in qb_res])
        statistics["users"] = {}

        if isinstance(users, basestring):
            users = [users]
        if len(users) == 0:
            users = node_users

        for user in users:
            user_data = [r for r in qb_res if r[4] == user]
            # statistics for user data
            statistics["users"][user] = count_statistics(user_data)
            statistics["users"][user]["total"] = node_users[user]

        # statistics for node data
        statistics.update(count_statistics(qb_res))

        return statistics
Exemple #2
0
    def get_creation_statistics(self, user_email=None):
        """
        Return a dictionary with the statistics of node creation, summarized by day.

        :note: Days when no nodes were created are not present in the returned `ctime_by_day` dictionary.

        :param user_email: If None (default), return statistics for all users.
            If an email is specified, return only the statistics for the given user.

        :return: a dictionary as
            follows::

                {
                   "total": TOTAL_NUM_OF_NODES,
                   "types": {TYPESTRING1: count, TYPESTRING2: count, ...},
                   "ctime_by_day": {'YYYY-MMM-DD': count, ...}

            where in `ctime_by_day` the key is a string in the format 'YYYY-MM-DD' and the value is
            an integer with the number of nodes created that day.
        """
        from aiida.orm.querybuilder import QueryBuilder as QB
        from aiida.orm import User, Node
        from collections import Counter
        import datetime

        def count_statistics(dataset):
            def get_statistics_dict(dataset):
                results = {}
                for count, typestring in sorted(
                    (v, k) for k, v in dataset.iteritems())[::-1]:
                    results[typestring] = count
                return results

            count_dict = {}

            types = Counter([r[2] for r in dataset])
            count_dict["types"] = get_statistics_dict(types)

            ctimelist = [r[1].strftime("%Y-%m-%d") for r in dataset]
            ctime = Counter(ctimelist)

            if len(ctimelist) > 0:

                # For the way the string is formatted, we can just sort it alphabetically
                firstdate = datetime.datetime.strptime(
                    sorted(ctimelist)[0], '%Y-%m-%d')
                lastdate = datetime.datetime.strptime(
                    sorted(ctimelist)[-1], '%Y-%m-%d')

                curdate = firstdate
                outdata = {}

                while curdate <= lastdate:
                    curdatestring = curdate.strftime('%Y-%m-%d')
                    outdata[curdatestring] = ctime.get(curdatestring, 0)
                    curdate += datetime.timedelta(days=1)
                count_dict["ctime_by_day"] = outdata

            else:
                count_dict["ctime_by_day"] = {}

            return count_dict

        statistics = {}

        q = QB()
        q.append(Node, project=['id', 'ctime', 'type'], tag='node')
        if user_email is not None:
            q.append(User,
                     creator_of='node',
                     project='email',
                     filters={'email': user_email})
        qb_res = q.all()

        # total count
        statistics["total"] = len(qb_res)
        statistics.update(count_statistics(qb_res))

        return statistics
Exemple #3
0
def extract_structure_info(keys, structures=None):
    """
    A method that collects a bunch of information (specified in keys) from
    structures (default whole db, or provided node list) in the database and
    returns that information as a dict, which could be used for further evalation
    #keys = ['uuid', 'formula', 'pk', 'symmetry', 'pbc', 'volume', 'total_energy',
    'child_nodes', 'natoms', 'group', extras', 'label', 'description', 'cif_file',
    'cif_number', 'cif_uuid', 'cif_ref', 'calcfunctions', 'band', 'dos', 'eos',
    'init_cls', 'corehole', primitive]

    """
    StructureData = DataFactory('structure')
    structure_list = []

    from aiida_fleur.tools.StructureData_util import get_spacegroup, is_structure
    from aiida_fleur.tools.StructureData_util import is_primitive

    if not structures:
        StructureData = DataFactory('structure')
        #t = time.time()
        qb = QB()
        qb.append(StructureData)
        structures = qb.all()
        #elapsed = time.time() - t
        # print "Total number of structures: {} (retrieved in {} s.)".format(len(structures), elapsed)
        #t = time.time()

    # for structure in structures:
    #    structure_dict = {}
    #    struc = structure[0]
    #    for key in keys:
    #        structure_dict[key] = get_methods(key)(struc)

    # get information
    for structure in structures:
        structure_dict = {}

        if isinstance(structure, list):
            struc = structure[0]
        else:
            struc = is_structure(structure)

        if 'formula' in keys:
            structure_dict['formula'] = struc.get_formula()
        if 'pk' in keys:
            structure_dict['pk'] = struc.pk
        if 'uuid' in keys:
            structure_dict['uuid'] = str(struc.uuid)
        if 'natoms' in keys:
            structure_dict['natoms'] = len(struc.sites)
        if 'cell' in keys:
            structure_dict['cell'] = str(struc.cell)
        if 'pbc' in keys:
            structure_dict['pbc'] = str(struc.pbc)
        if 'label' in keys:
            structure_dict['label'] = struc.label
        if 'description' in keys:
            structure_dict['description'] = struc.description
        if 'extras' in keys:
            extras = struc.extras
            structure_dict['extras'] = str(extras)
        if 'symmetry' in keys:
            symmetry = get_spacegroup(struc)
            structure_dict['symmetry'] = str(symmetry)
        if 'volume' in keys:
            volume = struc.get_cell_volume()
            structure_dict['volume'] = volume
        if 'child_nodes' in keys:
            child_nodes = len(struc.get_outgoing().all())
            structure_dict['child_nodes'] = child_nodes
        if 'primitive' in keys:
            prim = is_primitive(struc)
            structure_dict['primitive'] = prim

        if 'cif_file' in keys:
            cif_file = get_cif_file(struc)
            structure_dict['cif_file'] = cif_file
        '''
        if 'cif_number' in keys:
            cif_number = get_cif_number(struc)
            structure_dict['cif_number'] = cif_number
        if 'cif_uuid' in keys:
            cif_uuid = get_cif_uuid(struc)
            structure_dict['cif_uuid'] = cif_uuid
        if 'cif_ref' in keys:
            cif_ref = get_cif_ref(struc)
            structure_dict['cif_ref'] = cif_ref
        if 'total_energy' in keys:
            total_energy = get_total_energy(struc)
            structure_dict['total_energy'] = total_energy
        '''
        if 'group' in keys:
            group = group_member(struc)
            structure_dict['group'] = group
        if 'scf' in keys:
            scf = input_of_workcal('fleur_scf_wc', struc)
            structure_dict['scf'] = scf
        if 'band' in keys:
            band = input_of_workcal('fleur_band_wc', struc)
            structure_dict['band'] = band
        if 'dos' in keys:
            dos = input_of_workcal('fleur_dos_wc', struc)
            structure_dict['dos'] = dos
        if 'eos' in keys:
            eos = input_of_workcal('fleur_eos_wc', struc)
            structure_dict['eos'] = eos
        if 'init_cls' in keys:
            init_cls = input_of_workcal('fleur_initial_cls_wc', struc)
            structure_dict['init_cls'] = init_cls
        if 'corehole' in keys:
            corehole = input_of_workcal('fleur_corehole_wc', struc)
            structure_dict['corehole'] = corehole
        if 'calcfunctions' in keys:
            calcfunctions_uuid, calcfunctions_name = input_of_calcfunctions(
                struc)
            structure_dict['calcfunctions'] = [
                calcfunctions_uuid, calcfunctions_name
            ]

        structure_list.append(structure_dict)

    #elapsed = time.time() - t
    # print "(needed {} s.!!!)".format(elapsed)

    return structure_list