示例#1
0
def test_to_pandas():
    """A test to ensure that data sets are convertable to pandas objects."""

    source = CalculationSource('Dummy', {})

    pure_substance = Substance.from_components('C')
    binary_substance = Substance.from_components('C', 'O')

    data_set = PhysicalPropertyDataSet()

    data_set.properties[pure_substance.identifier] = []
    data_set.properties[binary_substance.identifier] = []

    for temperature in [298 * unit.kelvin, 300 * unit.kelvin, 302 * unit.kelvin]:

        thermodynamic_state = ThermodynamicState(temperature=temperature, pressure=1.0 * unit.atmosphere)

        density_property = Density(thermodynamic_state=thermodynamic_state,
                                   phase=PropertyPhase.Liquid,
                                   substance=pure_substance,
                                   value=1 * unit.gram / unit.milliliter,
                                   uncertainty=0.11 * unit.gram / unit.milliliter,
                                   source=source)

        dielectric_property = DielectricConstant(thermodynamic_state=thermodynamic_state,
                                                 phase=PropertyPhase.Liquid,
                                                 substance=pure_substance,
                                                 value=1 * unit.dimensionless,
                                                 uncertainty=0.11 * unit.dimensionless,
                                                 source=source)

        data_set.properties[pure_substance.identifier].append(density_property)
        data_set.properties[pure_substance.identifier].append(dielectric_property)

    for temperature in [298 * unit.kelvin, 300 * unit.kelvin, 302 * unit.kelvin]:

        thermodynamic_state = ThermodynamicState(temperature=temperature, pressure=1.0 * unit.atmosphere)

        enthalpy_property = EnthalpyOfMixing(thermodynamic_state=thermodynamic_state,
                                             phase=PropertyPhase.Liquid,
                                             substance=binary_substance,
                                             value=1 * unit.kilojoules / unit.mole,
                                             uncertainty=0.11 * unit.kilojoules / unit.mole,
                                             source=source)

        excess_property = ExcessMolarVolume(thermodynamic_state=thermodynamic_state,
                                            phase=PropertyPhase.Liquid,
                                            substance=binary_substance,
                                            value=1 * unit.meter**3 / unit.mole,
                                            uncertainty=0.11 * unit.meter**3 / unit.mole,
                                            source=source)

        data_set.properties[binary_substance.identifier].append(enthalpy_property)
        data_set.properties[binary_substance.identifier].append(excess_property)

    data_set_pandas = data_set.to_pandas()

    assert data_set_pandas is not None
    assert len(data_set_pandas) == 6
def save_results(force_field_key, results):
    """Saves the estimated results to disk.

    Parameters
    ----------
    force_field_key: str
        The key of the force field which these results were
        estimated for.
    results: PropertyEstimatorResult
        The results of an estimation request.
    """

    with open(f'{force_field_key} results.json', 'w') as file:

        json.dump(results,
                  file,
                  sort_keys=True,
                  indent=2,
                  separators=(',', ': '),
                  cls=TypedJSONEncoder)

    # Save the estimated and unsuccessful properties in separate data sets.
    estimated_data_set = PhysicalPropertyDataSet()
    unsuccessful_data_set = PhysicalPropertyDataSet()

    # Gather up the successfully estimated properties.
    for substance_id in results.estimated_properties:

        estimated_properties = results.estimated_properties[substance_id]

        for estimated_property in estimated_properties:

            if substance_id not in estimated_data_set.properties:
                estimated_data_set.properties[substance_id] = []

            estimated_property.source.provenance = {}
            estimated_data_set.properties[substance_id].append(
                estimated_property)

    estimated_data_set.to_pandas().to_csv(f'{force_field_key}.csv')

    with open(f'{force_field_key}.json', 'w') as file:
        json.dump(estimated_data_set,
                  file,
                  sort_keys=True,
                  indent=2,
                  separators=(',', ': '),
                  cls=TypedJSONEncoder)

    # Gather up the properties which could not be estimated.
    for substance_id in results.unsuccessful_properties:

        unsuccessful_properties = results.unsuccessful_properties[
            substance_id][0]

        for unsuccessful_property in unsuccessful_properties:

            if substance_id not in unsuccessful_data_set.properties:
                unsuccessful_data_set.properties[substance_id] = []

            unsuccessful_property.source.provenance = None
            unsuccessful_data_set.properties[substance_id].append(
                unsuccessful_property)

    with open(f'{force_field_key} unsuccessful.json', 'w') as file:
        json.dump(unsuccessful_data_set,
                  file,
                  sort_keys=True,
                  indent=2,
                  separators=(',', ': '),
                  cls=TypedJSONEncoder)

    # Save any exceptions that occured in a more human readable file.
    with open(f'{force_field_key} exceptions.txt', 'w') as file:

        for index, exception in enumerate(results.exceptions):

            file.write(f'\n{exception.directory}\n')
            file.write(exception.message.replace('\\n', '\n'))