def test_common_converters(raw_structure, RAW_STRUCTURES):
    """Test common converters"""
    structure = Structure(raw_structure)

    assert structure.as_json == StructureResource(**raw_structure).json()
    assert structure.as_dict == StructureResource(**raw_structure).dict()

    # Since calling .dict() and .json() will return also all default-valued properties,
    # the raw structure should at least be a sub-set of the resource's full list of properties.
    for raw_structure in RAW_STRUCTURES:
        raw_structure_property_set = set(raw_structure.keys())
        resource_property_set = set(Structure(raw_structure).as_dict.keys())
        assert raw_structure_property_set.issubset(resource_property_set)
Exemplo n.º 2
0
def get_single_cif(request: Request,
                   entry_id: str,
                   params: SingleEntryQueryParams = Depends()):
    from optimade.adapters import Structure

    response = get_single_entry(
        collection=structures_coll,
        entry_id=entry_id,
        request=request,
        params=params,
        response=StructureResponseOne,
    )

    adapter = Structure(response.data.dict())
    filename = entry_id.replace("/", "_") + ".cif"
    headers = {
        "Content-Type": "text/plain",
        "Content-Disposition": f"attachment;filename={filename};",
    }
    cif_list = adapter.as_cif.split("\n")
    num_lines = len(cif_list)
    cif_generator = (f"{line}\n" if ind != num_lines - 1 else line
                     for ind, line in enumerate(cif_list))
    return StreamingResponse(
        cif_generator,
        media_type="text/plain",
        headers=headers,
    )
    def _update_structures(self, data: list):
        """Update structures dropdown from response data"""
        structures = []

        for entry in data:
            # XXX: THIS IS TEMPORARY AND SHOULD BE REMOVED ASAP
            entry["attributes"]["chemical_formula_anonymous"] = None

            structure = Structure(self._check_species_mass(entry))

            formula = structure.attributes.chemical_formula_descriptive
            if formula is None:
                formula = structure.attributes.chemical_formula_reduced
            if formula is None:
                formula = structure.attributes.chemical_formula_anonymous
            if formula is None:
                formula = structure.attributes.chemical_formula_hill
            if formula is None:
                raise BadResource(
                    resource=structure,
                    fields=[
                        "chemical_formula_descriptive",
                        "chemical_formula_reduced",
                        "chemical_formula_anonymous",
                        "chemical_formula_hill",
                    ],
                    msg="At least one of the following chemical formula fields "
                    "should have a valid value",
                )

            entry_name = f"{formula} (id={structure.id})"
            structures.append((entry_name, {"structure": structure}))

        # Update list of structures in dropdown widget
        self.structure_drop.set_options(structures)
Exemplo n.º 4
0
def test_special_species(SPECIAL_SPECIES_STRUCTURES):
    """Make sure vacancies and non-chemical symbols ("X") are handled"""
    for special_structure in SPECIAL_SPECIES_STRUCTURES:
        structure = Structure(special_structure)

        with pytest.raises(
            ConversionError,
            match=r"(ASE cannot handle structures with partial occupancies)|"
            r"(ASE cannot handle structures with unknown \('X'\) chemical symbols)",
        ):
            get_ase_atoms(structure)
Exemplo n.º 5
0
def test_special_species(SPECIAL_SPECIES_STRUCTURES):
    """Make sure vacancies and non-chemical symbols ("X") are handled"""
    for special_structure in SPECIAL_SPECIES_STRUCTURES:
        structure = Structure(special_structure)

        # Since all the special species structure only have a single species, this works fine.
        if len(structure.species[0].chemical_symbols) > 1:
            # If the structure is disordered (has partial occupancies of any kind),
            # jarvis-tools cannot convert the structure
            with pytest.raises(
                ConversionError,
                match="jarvis-tools cannot handle structures with partial occupancies",
            ):
                get_jarvis_atoms(structure)
        else:
            # No partial occupancies, just special/non-standard species.
            # jarvis-tools should convert these structure fine enough.
            assert isinstance(get_jarvis_atoms(structure), Atoms)
Exemplo n.º 6
0
def test_special_species(SPECIAL_SPECIES_STRUCTURES):
    """Make sure vacancies and non-chemical symbols ("X") are handled"""
    for special_structure in SPECIAL_SPECIES_STRUCTURES:
        structure = Structure(special_structure)

        aiida_structure = get_aiida_structure_data(structure)

        assert isinstance(aiida_structure, StructureData)

        if "vacancy" in structure.attributes.species[0].chemical_symbols:
            assert aiida_structure.has_vacancies
            assert not aiida_structure.is_alloy
        elif len(structure.attributes.species[0].chemical_symbols) > 1:
            assert not aiida_structure.has_vacancies
            assert aiida_structure.is_alloy
        else:
            assert not aiida_structure.has_vacancies
            assert not aiida_structure.is_alloy
def test_special_species(SPECIAL_SPECIES_STRUCTURES):
    """Make sure vacancies and non-chemical symbols ("X") are handled"""
    from optimade.adapters.warnings import ConversionWarning

    for special_structure in SPECIAL_SPECIES_STRUCTURES:
        structure = Structure(special_structure)

        if structure.attributes.species[0].mass:
            aiida_structure = get_aiida_structure_data(structure)
        else:
            with pytest.warns(
                    ConversionWarning,
                    match=r".*will default to setting mass to 1\.0\.$"):
                aiida_structure = get_aiida_structure_data(structure)

        assert isinstance(aiida_structure, StructureData)

        # Test species.chemical_symbols
        if "vacancy" in structure.attributes.species[0].chemical_symbols:
            assert aiida_structure.has_vacancies
            assert not aiida_structure.is_alloy
        elif len(structure.attributes.species[0].chemical_symbols) > 1:
            assert not aiida_structure.has_vacancies
            assert aiida_structure.is_alloy
        else:
            assert not aiida_structure.has_vacancies
            assert not aiida_structure.is_alloy

        # Test species.mass
        if structure.attributes.species[0].mass:
            if len(structure.attributes.species[0].mass) > 1:
                assert aiida_structure.kinds[0].mass == sum([
                    conc * mass for conc, mass in zip(
                        structure.attributes.species[0].concentration,
                        structure.attributes.species[0].mass,
                    )
                ])
            else:
                assert (aiida_structure.kinds[0].mass ==
                        structure.attributes.species[0].mass[0])
        else:
            assert aiida_structure.kinds[0].mass == 1.0
def test_setting_entry(caplog, RAW_STRUCTURES):
    """Make sure entry can only be set once"""
    structure = Structure(RAW_STRUCTURES[0])
    structure.entry = RAW_STRUCTURES[1]
    assert "entry can only be set once and is already set." in caplog.text
def test_instantiate(RAW_STRUCTURES):
    """Try instantiating Structure for all raw test structures"""
    for structure in RAW_STRUCTURES:
        new_Structure = Structure(structure)
        assert isinstance(new_Structure.entry, StructureResource)
Exemplo n.º 10
0
def test_special_species(SPECIAL_SPECIES_STRUCTURES):
    """Make sure vacancies and non-chemical symbols ("X") are handled"""
    for special_structure in SPECIAL_SPECIES_STRUCTURES:
        structure = Structure(special_structure)

        assert isinstance(get_cif(structure), str)
Exemplo n.º 11
0
def test_successful_conversion(RAW_STRUCTURES):
    """Make sure its possible to convert"""
    for structure in RAW_STRUCTURES:
        assert isinstance(get_cif(Structure(structure)), str)
def test_successful_conversion(RAW_STRUCTURES):
    """Make sure its possible to convert"""
    for structure in RAW_STRUCTURES:
        assert isinstance(get_pymatgen(Structure(structure)),
                          (PymatgenStructure, Molecule))
Exemplo n.º 13
0
 def test_setting_entry(self, capfd, RAW_STRUCTURES):
     """Make sure entry can only be set once"""
     structure = Structure(RAW_STRUCTURES[0])
     structure.entry = RAW_STRUCTURES[1]
     captured = capfd.readouterr()
     assert "entry can only be set once and is already set." in captured.out