Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Bug fix: the subset Schema now checks for instances of int Or float #64

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions CAT/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,17 @@ def prep_core(core_df: SettingsDataFrame) -> SettingsDataFrame:
# Checks the if the dummy is a string (atomic symbol) or integer (atomic number)
formula = core.get_formula()

# Returns the indices and Atoms of all dummy atom ligand placeholders in the core
# Returns the indices of all dummy atom ligand placeholders in the core
if not core.properties.dummies:
at_idx = np.array([i for i, atom in enumerate(core) if atom.atnum == dummy])
else:
at_idx = np.array([i for i in core.properties.dummies]) - 1
dummies = core.properties.dummies
at_idx = np.fromiter(dummies, count=len(dummies), dtype=int)
at_idx -= 1
if subset:
at_idx = distribute_idx(core, at_idx, **subset)

# Convert atomic indices into atoms
# Convert atomic indices into Atoms
at_idx += 1
at_idx.sort()
core.properties.dummies = dummies = [core[i] for i in at_idx]
Expand All @@ -241,7 +243,9 @@ def prep_core(core_df: SettingsDataFrame) -> SettingsDataFrame:
# Delete all core dummy atoms
for at in dummies:
core.delete_atom(at)
idx_tuples.append((formula, ' '.join(at_idx.astype(str))))
idx_tuples.append(
(formula, ' '.join(at_idx.astype(str)))
)

# Create and return a new dataframe
idx = pd.MultiIndex.from_tuples(idx_tuples, names=['formula', 'anchor'])
Expand Down
7 changes: 5 additions & 2 deletions CAT/data_handling/validation_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,16 @@ def _get_crsjob() -> type:
#: Schema for validating the ``['optional']['core']['subset']`` block.
subset_schema: Schema = Schema({
'p':
And(int, float, lambda n: 0.0 < n <= 1.0, Use(float)),
Or(
And(int, lambda n: 0 < n <= 1, Use(float)),
And(float, lambda n: 0.0 < n <= 1.0)
),

Optional_('mode', default='uniform'):
And(str, lambda n: n.lower() in {'uniform', 'random', 'cluster'}, Use(str.lower)),

Optional_('start', default=None):
And(None, int)
Or(None, int)
})

_db_names = ('core', 'ligand', 'qd')
Expand Down
51 changes: 50 additions & 1 deletion tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from CAT.utils import get_template
from CAT.data_handling.validation_schemas import (
mol_schema, core_schema, ligand_schema, qd_schema, database_schema,
mongodb_schema, bde_schema, qd_opt_schema, crs_schema
mongodb_schema, bde_schema, qd_opt_schema, crs_schema, subset_schema
)

PATH = join('tests', 'test_files')
Expand Down Expand Up @@ -440,3 +440,52 @@ def test_bde_schema() -> None:
assertion.eq(bde_schema.validate(bde_dict)[s], ref)
bde_dict[s] = join(PATH, 'settings.yaml')
assertion.eq(bde_schema.validate(bde_dict)[s], ref)


def test_subset_schema() -> None:
"""Test :data:`CAT.data_handling.validation_schemas.subset_schema`."""
subset_dict = {'p': 0.5}
ref = {
'p': 0.5,
'mode': 'uniform',
'start': None
}

assertion.eq(subset_schema.validate(subset_dict), ref)

subset_dict['p'] = 'bob' # Exception: incorrect type
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['p'] = -1 # Exception: incorrect value
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['p'] = 0 # Exception: incorrect value
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['p'] = 1.5 # Exception: incorrect value
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['p'] = 0.33
assertion.eq(subset_schema.validate(subset_dict)['p'], 0.33)
subset_dict['p'] = 1
assertion.eq(subset_schema.validate(subset_dict)['p'], 1.0)

subset_dict['mode'] = 1 # Exception: incorrect type
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['mode'] = 'bob' # Exception: incorrect value
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['mode'] = 'random'
assertion.eq(subset_schema.validate(subset_dict)['mode'], 'random')
subset_dict['mode'] = 'uniform'
assertion.eq(subset_schema.validate(subset_dict)['mode'], 'uniform')
subset_dict['mode'] = 'cluster'
assertion.eq(subset_schema.validate(subset_dict)['mode'], 'cluster')

subset_dict['start'] = 1.0 # Exception: incorrect type
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['start'] = 'bob' # Exception: incorrect type
assertion.assert_(subset_schema.validate, subset_dict, exception=SchemaError)
subset_dict['start'] = None
assertion.is_(subset_schema.validate(subset_dict)['start'], None)
subset_dict['start'] = 42
assertion.eq(subset_schema.validate(subset_dict)['start'], 42)
subset_dict['start'] = -5
assertion.eq(subset_schema.validate(subset_dict)['start'], -5)
subset_dict['start'] = 0
assertion.eq(subset_schema.validate(subset_dict)['start'], 0)