def test_gaussian(self): g = Gaussian(center=4, width=4) self.assertArrayAlmostEqual([1, np.exp(-1)], g([4, 0]), decimal=4) self.assertAlmostEqual(g.volume(20), 2118, places=0) # Make sure the name makes sense name = g.name() self.assertIn('Gaussian', name) self.assertIn('width=4', name) self.assertIn('center=4', name)
def test_grdf(self): f1 = Gaussian(1, 0) f2 = Gaussian(1, 1) f3 = Gaussian(1, 5) s_tuples = [(self.sc, 0), (self.cscl, 0)] # test fit, transform, and featurize dataframe for both run modes GRDF mode grdf = GeneralizedRadialDistributionFunction(bins=[f1, f2, f3], mode='GRDF') grdf.fit(s_tuples) features = grdf.transform(s_tuples) self.assertArrayAlmostEqual( features, [[4.4807e-06, 0.00031, 0.02670], [3.3303e-06, 0.00026, 0.01753]], 3) features = grdf.featurize_dataframe(pd.DataFrame(s_tuples), [0, 1]) self.assertArrayEqual(list(features.columns.values), [ 0, 1, 'Gaussian center=0 width=1', 'Gaussian center=1 width=1', 'Gaussian center=5 width=1' ]) # pairwise GRDF mode grdf = GeneralizedRadialDistributionFunction(bins=[f1, f2, f3], mode='pairwise_GRDF') grdf.fit(s_tuples) features = grdf.transform(s_tuples) self.assertArrayAlmostEqual(features[0], [4.4807e-06, 3.1661e-04, 0.0267], 3) self.assertArrayAlmostEqual( features[1], [2.1807e-08, 6.1119e-06, 0.0142, 3.3085e-06, 2.5898e-04, 0.0032], 3) features = grdf.featurize_dataframe(pd.DataFrame(s_tuples), [0, 1]) self.assertArrayEqual(list(features.columns.values), [ 0, 1, 'site2 0 Gaussian center=0 width=1', 'site2 1 Gaussian center=0 width=1', 'site2 0 Gaussian center=1 width=1', 'site2 1 Gaussian center=1 width=1', 'site2 0 Gaussian center=5 width=1', 'site2 1 Gaussian center=5 width=1' ]) # test preset grdf = GeneralizedRadialDistributionFunction.from_preset('gaussian') grdf.featurize(self.sc, 0) self.assertArrayEqual([bin.name() for bin in grdf.bins], [ 'Gaussian center={} width=1.0'.format(i) for i in np.arange(10.0) ])
def from_preset(preset, width=0.5, spacing=0.5, cutoff=10): """ Preset bin functions for this featurizer. Example use: >>> AFS = AngularFourierSeries.from_preset('gaussian') >>> AFS.featurize(struct, idx) Args: preset (str): shape of bin (either 'gaussian' or 'histogram') width (float): bin width. std dev for gaussian, width for histogram spacing (float): the spacing between bin centers cutoff (float): maximum distance to look for neighbors """ # Generate bin functions if preset == "gaussian": bins = [] for center in np.arange(0., cutoff, spacing): bins.append(Gaussian(width, center)) elif preset == "histogram": bins = [] for start in np.arange(0, cutoff, spacing): bins.append(Histogram(start, width)) else: raise ValueError('Not a valid preset condition.') return AngularFourierSeries(bins, cutoff=cutoff)
def from_preset(preset, width=1.0, spacing=1.0, cutoff=10, mode='GRDF'): """ Preset bin functions for this featurizer. Example use: >>> GRDF = GeneralizedRadialDistributionFunction.from_preset('gaussian') >>> GRDF.featurize(struct, idx) Args: preset (str): shape of bin (either 'gaussian' or 'histogram') width (float): bin width. std dev for gaussian, width for histogram spacing (float): the spacing between bin centers cutoff (float): maximum distance to look for neighbors mode (str): featurizing mode. either 'GRDF' or 'pairwise_GRDF' """ # Generate bin functions if preset == "gaussian": bins = [] for center in np.arange(0., cutoff, spacing): bins.append(Gaussian(width, center)) elif preset == "histogram": bins = [] for start in np.arange(0, cutoff, spacing): bins.append(Histogram(start, width)) else: raise ValueError('Not a valid preset condition.') return GeneralizedRadialDistributionFunction(bins, cutoff=cutoff, mode=mode)
def test_afs(self): f1 = Gaussian(1, 0) f2 = Gaussian(1, 1) f3 = Gaussian(1, 5) s_tuples = [(self.sc, 0), (self.cscl, 0)] # test transform,and featurize dataframe afs = AngularFourierSeries(bins=[f1, f2, f3]) features = afs.transform(s_tuples) self.assertArrayAlmostEqual( features, [[ -1.0374e-10, -4.3563e-08, -2.7914e-06, -4.3563e-08, -1.8292e-05, -0.0011, -2.7914e-06, -0.0011, -12.7863 ], [ -1.7403e-11, -1.0886e-08, -3.5985e-06, -1.0886e-08, -6.0597e-06, -0.0016, -3.5985e-06, -0.0016, -3.9052 ]], 3) features = afs.featurize_dataframe(pd.DataFrame(s_tuples), [0, 1]) self.assertArrayEqual(list(features.columns.values), [ 0, 1, 'AFS (Gaussian center=0 width=1, Gaussian center=0 width=1)', 'AFS (Gaussian center=0 width=1, Gaussian center=1 width=1)', 'AFS (Gaussian center=0 width=1, Gaussian center=5 width=1)', 'AFS (Gaussian center=1 width=1, Gaussian center=0 width=1)', 'AFS (Gaussian center=1 width=1, Gaussian center=1 width=1)', 'AFS (Gaussian center=1 width=1, Gaussian center=5 width=1)', 'AFS (Gaussian center=5 width=1, Gaussian center=0 width=1)', 'AFS (Gaussian center=5 width=1, Gaussian center=1 width=1)', 'AFS (Gaussian center=5 width=1, Gaussian center=5 width=1)' ]) # test preset afs = AngularFourierSeries.from_preset('gaussian') afs.featurize(self.sc, 0) self.assertArrayEqual([bin.name() for bin in afs.bins], [ 'Gaussian center={} width=0.5'.format(i) for i in np.arange(0, 10, 0.5) ]) afs = AngularFourierSeries.from_preset('histogram') afs.featurize(self.sc, 0) self.assertArrayEqual([bin.name() for bin in afs.bins], [ 'Histogram start={} width=0.5'.format(i) for i in np.arange(0, 10, 0.5) ])