class SubstitutionPredictorTransformation(AbstractTransformation): """ This transformation takes a structure and uses the structure prediction module to find likely site substitutions. """ def __init__(self, threshold=1e-2, **kwargs): """ Args: kwargs: args for SubstitutionProbability class lambda_table, alpha """ self._kwargs = kwargs self._threshold = threshold self._substitutor = Substitutor(threshold, **kwargs) def apply_transformation(self, structure, return_ranked_list=False): if not return_ranked_list: raise ValueError("SubstitutionPredictorTransformation doesn't" " support returning 1 structure") preds = self._substitutor.pred_from_comp(structure.composition) preds.sort(key=lambda x: x['probability'], reverse=True) outputs = [] for pred in preds: st = SubstitutionTransformation(pred['substitutions']) output = {'structure': st.apply_transformation(structure), 'probability': pred['probability'], 'threshold': self._threshold, 'substitutions': {}} #dictionary keys have to be converted to strings for JSON for key, value in pred['substitutions'].items(): output['substitutions'][str(key)] = str(value) outputs.append(output) return outputs def __str__(self): return "SubstitutionPredictorTransformation" def __repr__(self): return self.__str__() @property def inverse(self): return None @property def is_one_to_many(self): return True @property def to_dict(self): d = {"name": self.__class__.__name__, "version": __version__, "init_args": self._kwargs, "@module": self.__class__.__module__, "@class": self.__class__.__name__} d["init_args"]["threshold"] = self._threshold return d
class SubstitutorTest(unittest.TestCase): def setUp(self): self.s = Substitutor(threshold=1e-3, lambda_table=get_table(), alpha=-5.) def test_substitutor(self): s_list = [Specie('O', -2), Specie('Li', 1)] subs = self.s.pred_from_list(s_list) self.assertEqual(len(subs), 4, 'incorrect number of substitutions') c = Composition({'O2-': 1, 'Li1+': 2}) subs = self.s.pred_from_comp(c) self.assertEqual(len(subs), 4, 'incorrect number of substitutions') def test_to_dict(self): Substitutor.from_dict(self.s.to_dict)
class SubstitutorTest(unittest.TestCase): def setUp(self): self.s = Substitutor(threshold=1e-3, lambda_table=get_table(), alpha= -5.) def test_substitutor(self): s_list = [Specie('O', -2), Specie('Li', 1)] subs = self.s.pred_from_list(s_list) self.assertEqual(len(subs), 4 , 'incorrect number of substitutions') c = Composition({'O2-': 1, 'Li1+': 2}) subs = self.s.pred_from_comp(c) self.assertEqual(len(subs), 4 , 'incorrect number of substitutions') def test_as_dict(self): Substitutor.from_dict(self.s.as_dict())
class SubstitutionPredictorTransformation(AbstractTransformation): """ This transformation takes a structure and uses the structure prediction module to find likely site substitutions. """ def __init__(self, threshold=1e-2, **kwargs): """ Args: kwargs: args for SubstitutionProbability class lambda_table, alpha """ self._kwargs = kwargs self._threshold = threshold self._substitutor = Substitutor(threshold, **kwargs) def apply_transformation(self, structure, return_ranked_list=False): if not return_ranked_list: raise ValueError("SubstitutionPredictorTransformation doesn't" " support returning 1 structure") preds = self._substitutor.pred_from_comp(structure.composition) preds.sort(key=lambda x: x['probability'], reverse=True) outputs = [] for pred in preds: st = SubstitutionTransformation(pred['substitutions']) output = { 'structure': st.apply_transformation(structure), 'probability': pred['probability'], 'threshold': self._threshold, 'substitutions': {} } #dictionary keys have to be converted to strings for JSON for key, value in pred['substitutions'].items(): output['substitutions'][str(key)] = str(value) outputs.append(output) return outputs def __str__(self): return "SubstitutionPredictorTransformation" def __repr__(self): return self.__str__() @property def inverse(self): return None @property def is_one_to_many(self): return True @property def to_dict(self): d = { "name": self.__class__.__name__, "version": __version__, "init_args": self._kwargs, "@module": self.__class__.__module__, "@class": self.__class__.__name__ } d["init_args"]["threshold"] = self._threshold return d