<commit_before>from __future__ import division, unicode_literals
from mpcontribs.rest.rester import MPContribsRester
from mpcontribs.io.core.utils import get_short_object_id
from mpcontribs.io.archieml.mpfile import MPFile
from pandas import DataFrame

class MnO2PhaseSelectionRester(MPContribsRester):
    """MnO2_phase_selection-specific convenience functions to interact with MPContribs REST interface"""

    def get_contributions(self, phase=None):
        data = []
        phase_query_key = {'$exists': 1} if phase is None else phase
        columns = ['mp-id', 'contribution', 'formula']
        if phase is None:
            columns.append('phase')
        columns += ['dH (formation)', 'dH (hydration)', 'GS?', 'CIF']

        for doc in self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT','University of Kentucky']},
                'content.Phase': phase_query_key
            }, projection={'_id': 1, 'mp_cat_id': 1, 'content': 1}
        ):
            mpfile = MPFile.from_contribution(doc)
            mp_id = mpfile.ids[0]
            contrib = mpfile.hdata[mp_id]
            row = [mp_id, get_short_object_id(doc['_id']), contrib['Formula']]
            if phase is None:
                row.append(contrib['Phase'])
            row += [contrib['dHf'], contrib['dHh'], contrib['GS'], 'TODO']
            # TODO URLs for mp_id and cid
            data.append((mp_id, row))

        return DataFrame.from_items(data, orient='index', columns=columns)

    def get_provenance(self):
        for doc in self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT','University of Kentucky']}, 'content.authors': {'$exists': 1},
                'content.title': {'$exists': 1}, 'content.reference': {'$exists': 1}
            },
            projection={
                '_id': 1, 'mp_cat_id': 1, 'content.authors': 1,
                'content.reference': 1, 'content.title': 1
            }
        ):
            mpfile = MPFile.from_contribution(doc)
            mp_id = mpfile.ids[0]
            return mpfile.hdata[mp_id]

    def get_phases(self):
        phases = set()

        for doc in self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT','University of Kentucky']},
                'content.Phase': {'$exists': 1}
            }, projection={'_id': 0, 'content.Phase': 1}
        ):
            phases.add(doc['content']['Phase'])

        return list(phases)
<commit_msg>mno2.rester: catch no contribs found<commit_after>from __future__ import division, unicode_literals
from mpcontribs.rest.rester import MPContribsRester
from mpcontribs.io.core.utils import get_short_object_id
from mpcontribs.io.archieml.mpfile import MPFile
from pandas import DataFrame

class MnO2PhaseSelectionRester(MPContribsRester):
    """MnO2_phase_selection-specific convenience functions to interact with MPContribs REST interface"""

    def get_contributions(self, phase=None):
        data = []
        phase_query_key = {'$exists': 1} if phase is None else phase
        columns = ['mp-id', 'contribution', 'formula']
        if phase is None:
            columns.append('phase')
        columns += ['dH (formation)', 'dH (hydration)', 'GS?', 'CIF']

        docs = self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT', 'University of Kentucky']},
                'content.Phase': phase_query_key
            }, projection={'_id': 1, 'mp_cat_id': 1, 'content': 1}
        )
        if not docs:
            raise Exception('No contributions found for MnO2 Phase Selection Explorer!')

        for doc in docs:
            mpfile = MPFile.from_contribution(doc)
            mp_id = mpfile.ids[0]
            contrib = mpfile.hdata[mp_id]
            row = [mp_id, get_short_object_id(doc['_id']), contrib['Formula']]
            if phase is None:
                row.append(contrib['Phase'])
            row += [contrib['dHf'], contrib['dHh'], contrib['GS'], 'TODO']
            # TODO URLs for mp_id and cid
            data.append((mp_id, row))

        return DataFrame.from_items(data, orient='index', columns=columns)

    def get_provenance(self):
        docs = self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT','University of Kentucky']},
                'content.authors': {'$exists': 1},
                'content.title': {'$exists': 1},
                'content.reference': {'$exists': 1}
            },
            projection={
                '_id': 1, 'mp_cat_id': 1, 'content.authors': 1,
                'content.reference': 1, 'content.title': 1
            }
        )
        if not docs:
            raise Exception('No contributions found for MnO2 Phase Selection Explorer!')

        for doc in docs:
            mpfile = MPFile.from_contribution(doc)
            mp_id = mpfile.ids[0]
            return mpfile.hdata[mp_id]

    def get_phases(self):
        phases = set()
        docs = self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT','University of Kentucky']},
                'content.Phase': {'$exists': 1}
            }, projection={'_id': 0, 'content.Phase': 1}
        )
        if not docs:
            raise Exception('No contributions found for MnO2 Phase Selection Explorer!')

        for doc in docs:
            phases.add(doc['content']['Phase'])

        return list(phases)

{'fraction_non_alphanumeric': 0.13514005092761003, 'fraction_numerical': 0.007639141506002182, 'mean_word_length': 2.2461629279811097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4313818', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1579, 'n_words': 399}