def test_stog_plot_df(self, mock_show): df = pd.DataFrame(np.random.randn(10, 2), index=np.arange(10), columns=list('XY')) stog = StoG() stog._plot_df(df, 'x', 'y', 'title', None) mock_show.assert_called_with()
def test_stog_real_space_function_setter(self): stog = StoG() stog.real_space_function = "GK(r)" self.assertEqual(stog.real_space_function, "GK(r)") self.assertEqual(stog.gr_title, "GK(r) Merged") self.assertEqual(stog.real_space_function, "GK(r)") self.assertEqual(stog.gr_ft_title, "GK(r) FT") self.assertEqual(stog.gr_lorch_title, "GK(r) FT Lorched")
def test_stog_read_all_data_for_files_length(self): # Load S(Q) for Argon from test data stog = StoG() stog.files = self.kwargs_for_files['Files'] stog.read_all_data() # Check S(Q) data against targets self.assertEqual(len(stog.files), len(self.kwargs_for_files['Files']))
def test_stog_add_dataset_default_reciprocal_space_function(self): # Checks the default reciprocal space function is S(Q) and the index is # set stog = StoG() index = 300 info = {'data': pd.DataFrame({'x': self.q, 'y': self.sq})} stog.add_dataset(info, index=index) self.assertEqual(list(stog.df_individuals.columns.values), ['S(Q)_%d' % index])
def test_stog_plotting_kwargs_setter(self): stog = StoG() new_kwargs = { 'figsize': (4, 4), 'style': 'o', 'ms': 2, 'lw': 2, } stog.plotting_kwargs = new_kwargs self.assertEqual(stog.plotting_kwargs, new_kwargs)
def setUp(self): super(TestStogPlottingDataFrameMethods, self).setUp() stog = StoG(**self.kwargs_for_stog_input) stog.files = self.kwargs_for_files['Files'] stog.read_all_data() stog.merge_data() stog.transform_merged() self.stog = stog
def test_stog_transform_merged_for_nan_after_filter(self): # Load S(Q) for Argon from test data stog = StoG(**self.kwargs_for_stog_input) stog.files = self.kwargs_for_files['Files'] stog.real_space_function = "GK(r)" stog.read_all_data() stog.merge_data() stog.transform_merged() self.assertFalse( np.isnan(stog.df_sq_master[stog.sq_title].values).any()) self.assertFalse( np.isnan(stog.df_gr_master[stog.gr_title].values).any())
def test_stog_add_dataset_wrong_reciprocal_space_function_exception(self): # Check qmin and qmax apply cropping stog = StoG() index = 0 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.sq }), 'ReciprocalFunction': 'ABCDEFG(Q)' } with self.assertRaises(ValueError): stog.add_dataset(info, index=index)
def test_stog_add_dataset_xoffset(self): # Offset Q from 1.96 -> 2.14 stog = StoG() index = 0 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.sq }), 'ReciprocalFunction': 'S(Q)', 'X': { 'Offset': 0.2 } } stog.add_dataset(info, index=index) self.assertEqual(stog.df_individuals.iloc[self.first].name, 2.14)
def test_stog_read_all_data_assertion(self): stog = StoG() with self.assertRaises(AssertionError): stog.read_all_data() stog.files = list() with self.assertRaises(AssertionError): stog.read_all_data()
def test_stog_fourier_filter_before_transform_merged_call(self): # Number of decimal places for precision places = 1 # Load S(Q) for Argon from test data stog = StoG(**self.kwargs_for_stog_input) stog.files = self.kwargs_for_files['Files'] stog.plot_flag = False stog.read_all_data() stog.merge_data() stog.fourier_filter() # Check g(r) data against targets self.assertAlmostEqual( stog.df_gr_master.iloc[self.real_space_first].name, self.real_xtarget, places=places) self.assertAlmostEqual( stog.df_gr_master.iloc[self.real_space_first][stog.gr_ft_title], self.gofr_ff_target[0], places=places)
def test_stog_add_dataset_yoffset(self): # Offset S(Q) and make sure it does not equal original target values stog = StoG() index = 0 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.sq }), 'ReciprocalFunction': 'S(Q)', 'Y': { 'Offset': 2.0 } } stog.add_dataset(info, index=index) self.assertNotEqual( stog.df_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0]) self.assertNotEqual( stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0])
def test_stog_transform_merged_GKofR(self): # Number of decimal places for precision places = 2 # Load S(Q) for Argon from test data stog = StoG(**self.kwargs_for_stog_input) stog.files = self.kwargs_for_files['Files'] stog.real_space_function = "GK(r)" stog.read_all_data() stog.merge_data() stog.transform_merged() # Check GK(r) data against targets self.assertAlmostEqual( stog.df_gr_master.iloc[self.real_space_first].name, self.real_xtarget, places=places) self.assertAlmostEqual( stog.df_gr_master.iloc[self.real_space_first][stog.gr_title], self.GKofR_target[0], places=places)
def test_stog_read_all_data(self): # Number of decimal places for precision places = 5 # Load S(Q) for Argon from test data stog = StoG() stog.files = self.kwargs_for_files['Files'] stog.read_all_data() # Check S(Q) data against targets self.assertEqual(stog.df_individuals.iloc[self.first].name, self.reciprocal_xtarget) for index in range(len(stog.files)): self.assertAlmostEqual( stog.df_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0], places=places) self.assertAlmostEqual( stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0], places=places)
def test_stog_read_dataset(self): # Number of decimal places for precision places = 5 # Load S(Q) for Argon from test data stog = StoG( **{ '<b_coh>^2': self.kwargs['<b_coh>^2'], '<b_tot^2>': self.kwargs['<b_tot^2>'] }) info = { 'Filename': get_data_path(self.material.reciprocal_space_filename), 'ReciprocalFunction': 'S(Q)', 'Qmin': 0.02, 'Qmax': 35.2, 'Y': { 'Offset': 0.0, 'Scale': 1.0 }, 'X': { 'Offset': 0.0 } } info['index'] = 0 stog.read_dataset(info) # Check S(Q) data against targets self.assertEqual(stog.df_individuals.iloc[self.first].name, self.reciprocal_xtarget) self.assertAlmostEqual( stog.df_individuals.iloc[self.first]['S(Q)_%d' % info['index']], self.sq_target[0], places=places) self.assertAlmostEqual( stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % info['index']], self.sq_target[0], places=places)
def test_stog_merge_data_qsq_opts_offset(self): # Number of decimal places for precision places = 5 # Load S(Q) for Argon from test data stog = StoG() stog.files = self.kwargs_for_files['Files'] stog.read_all_data() qsq_opts = {'Y': {'Offset': 1.0}} # Test Q[S(Q)-1] scale stog.merged_opts['Q[S(Q)-1]'] = qsq_opts stog.merge_data() self.assertAlmostEqual( stog.df_sq_master.iloc[self.first][stog.qsq_minus_one_title], qsq_opts['Y']['Offset'] + self.fq_target[0], places=places)
def test_stog_merge_data(self): # Number of decimal places for precision places = 5 # Load S(Q) for Argon from test data stog = StoG() stog.files = self.kwargs_for_files['Files'] stog.read_all_data() stog.merge_data() # Check S(Q) data against targets self.assertEqual(stog.df_sq_master.iloc[self.first].name, self.reciprocal_xtarget) self.assertAlmostEqual( stog.df_sq_master.iloc[self.first][stog.sq_title], self.sq_target[0], places=places)
def test_stog_init(self): stog = StoG() self.assertEqual(stog.xmin, 100) self.assertEqual(stog.xmax, 0) self.assertEqual(stog.qmin, None) self.assertEqual(stog.qmax, None) self.assertEqual(stog.files, None) self.assertEqual(stog.sq_title, "S(Q) Merged") self.assertEqual(stog.qsq_minus_one_title, "Q[S(Q)-1] Merged") self.assertEqual(stog.sq_ft_title, "S(Q) FT") self.assertEqual(stog.fq_title, "F(Q) Merged") self.assertEqual(stog.real_space_function, "g(r)") self.assertEqual(stog.gr_title, "g(r) Merged") self.assertEqual(stog.gr_ft_title, "g(r) FT") self.assertEqual(stog.gr_lorch_title, "g(r) FT Lorched") self.assertEqual(stog.GKofR_title, "G(r) (Keen Version)") self.assertEqual(stog.rmin, 0.0) self.assertEqual(stog.rmax, 50.0) self.assertEqual(stog.rdelta, 0.01) self.assertEqual(stog.density, 1.0) self.assertEqual(stog.bcoh_sqrd, 1.0) self.assertEqual(stog.btot_sqrd, 1.0) self.assertEqual(stog.low_q_correction, False) self.assertEqual(stog.lorch_flag, False) self.assertEqual(stog.fourier_filter_cutoff, None) self.assertEqual(stog.plot_flag, True) self.assertEqual(stog.plotting_kwargs, { 'figsize': (16, 8), 'style': '-', 'ms': 1, 'lw': 1, }) self.assertEqual(stog.merged_opts, {"Y": { "Offset": 0.0, "Scale": 1.0 }}) self.assertEqual(stog.stem_name, "out") self.assertTrue(stog.df_individuals.empty) self.assertTrue(stog.df_sq_master.empty) self.assertTrue(stog.df_sq_individuals.empty) self.assertTrue(stog.df_gr_master.empty)
def test_stog_add_dataset_qmin_qmax_crop(self): # Check qmin and qmax apply cropping stog = StoG() index = 0 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.sq }), 'ReciprocalFunction': 'S(Q)' } stog.qmin = 1.5 stog.qmax = 12.0 stog.add_dataset(info, index=index) self.assertEqual(stog.df_individuals.iloc[0].name, stog.qmin) self.assertEqual(stog.df_individuals.iloc[-1].name, stog.qmax)
def test_stog_fourier_filter_for_nan_after_filter(self): # Load S(Q) for Argon from test data stog = StoG(**self.kwargs_for_stog_input) stog.files = self.kwargs_for_files['Files'] stog.plot_flag = False stog.read_all_data() stog.merge_data() stog.transform_merged() stog.fourier_filter() self.assertFalse( np.isnan(stog.df_gr_master[stog.gr_title].values).any()) self.assertFalse( np.isnan(stog.df_sq_master[stog.sq_title].values).any()) self.assertFalse( np.isnan(stog.df_sq_master[stog._ft_title].values).any()) self.assertFalse( np.isnan(stog.df_sq_master[stog.sq_ft_title].values).any())
def test_stog_apply_lorch_GofR(self): # Number of decimal places for precision places = 5 # Load S(Q) for Argon from test data stog = StoG(**self.kwargs_for_stog_input) stog.files = self.kwargs_for_files['Files'] stog.plot_flag = False stog.real_space_function = "G(r)" stog.read_all_data() stog.merge_data() stog.transform_merged() q, sq, r, gr = stog.fourier_filter() stog.apply_lorch(q, sq, r) self.assertAlmostEqual( stog.df_gr_master.iloc[self.real_space_first][stog.gr_lorch_title], self.GofR_lorch_target[0], places=places)
def test_stog_low_q_correction_exception(self): stog = StoG() with self.assertRaises(TypeError): stog.low_q_correction = 1.0
def test_stog_fourier_filter_with_plot_flag(self, mock_show): # Load S(Q) for Argon from test data stog = StoG(**self.kwargs_for_stog_input) stog.files = self.kwargs_for_files['Files'] stog.plot_flag = True stog.read_all_data() stog.merge_data() stog.transform_merged() stog.fourier_filter() mock_show.assert_called_with()
def test_stog_real_space_function_exception(self): stog = StoG() with self.assertRaises(ValueError): stog.real_space_function = "Dog"
def test_stog_plot_flag_exception(self): stog = StoG() with self.assertRaises(TypeError): stog.plot_flag = 1.0
def test_stog_df_sq_individuals_setter(self): stog = StoG() stog.df_sq_individuals = self.df_target self.assertTrue(stog.df_sq_individuals.equals(self.df_target))
def test_stog_df_gr_master_setter(self): stog = StoG() stog.df_gr_master = self.df_target self.assertTrue(stog.df_gr_master.equals(self.df_target))
def test_stog_append_file(self): stog = StoG(**{'Files': ['file1.txt', 'file2.txt']}) stog.append_file('file3.txt') self.assertEqual(stog.files, ['file1.txt', 'file2.txt', 'file3.txt'])
def test_stog_extend_file_list(self): stog = StoG(**{'Files': ['file1.txt', 'file2.txt']}) stog.extend_file_list(['file3.txt', 'file4.txt']) self.assertEqual(stog.files, ['file1.txt', 'file2.txt', 'file3.txt', 'file4.txt'])
def test_stog_add_dataset(self): # Number of decimal places for precision places = 5 # Initialize with material info for Argon stog = StoG( **{ '<b_coh>^2': self.kwargs['<b_coh>^2'], '<b_tot^2>': self.kwargs['<b_tot^2>'] }) # Add the S(Q) data set and check values against targets index = 0 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.sq }), 'ReciprocalFunction': 'S(Q)' } stog.add_dataset(info, index=index) self.assertEqual(stog.df_individuals.iloc[self.first].name, self.reciprocal_xtarget) self.assertAlmostEqual(stog.df_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0], places=places) self.assertAlmostEqual( stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0], places=places) # Add the Q[S(Q)-1] data set and check values for it and S(Q) against # targets index = 1 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.fq }), 'ReciprocalFunction': 'Q[S(Q)-1]' } stog.add_dataset(info, index=index) self.assertAlmostEqual( stog.df_individuals.iloc[self.first]['Q[S(Q)-1]_%d' % index], self.fq_target[0], places=places) self.assertAlmostEqual( stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0], places=places) # Add the FK(Q) data set and check values for it and S(Q) against # targets index = 2 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.fq_keen }), 'ReciprocalFunction': 'FK(Q)' } stog.add_dataset(info, index=index) self.assertAlmostEqual( stog.df_individuals.iloc[self.first]['FK(Q)_%d' % index], self.fq_keen_target[0], places=places) self.assertAlmostEqual( stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0], places=places) # Add the DCS(Q) data set and check values for it and S(Q) against # targets index = 3 info = { 'data': pd.DataFrame({ 'x': self.q, 'y': self.dcs }), 'ReciprocalFunction': 'DCS(Q)' } stog.add_dataset(info, index=index) self.assertAlmostEqual( stog.df_individuals.iloc[self.first]['DCS(Q)_%d' % index], self.dcs_target[0], places=places) self.assertAlmostEqual( stog.df_sq_individuals.iloc[self.first]['S(Q)_%d' % index], self.sq_target[0], places=places)