@@ -181,6 +181,64 @@ def stretch(x, y, stretch):
181181
182182 assert res < err
183183
184+ def test_refine_grid_bad (self , user_filesystem ):
185+ grid = numpy .arange (2 )
186+ func = numpy .sin (grid )
187+ grid1 , func1 , grid2 , func2 = grid , func , grid , func
188+ config = {
189+ "stretch" : 0.005 ,
190+ "scale" : 1.0 ,
191+ "smear" : 0 ,
192+ }
193+ chain = MorphChain (config )
194+ refiner = Refiner (chain , grid1 , func1 , grid2 , func2 )
195+ refpars = ["stretch" , "scale" , "smear" ]
196+ expected_error_message = (
197+ "\n Number of shared grid points: 2\n "
198+ "Number of parameters: 3\n "
199+ "Not enough shared grid points between morphed function "
200+ "between morphed function and target function to fit "
201+ "the chosen parameters.\n "
202+ "Please make sure the overlapping domain between the morphed "
203+ "function and the target function is sufficiently large, or "
204+ "reduce the number of parameters."
205+ )
206+ with pytest .raises (
207+ ValueError ,
208+ ) as error :
209+ refiner .refine (* refpars )
210+ actual_error_message = str (error .value )
211+ assert actual_error_message == expected_error_message
212+
213+ # call from command line
214+ import subprocess
215+
216+ data_dir_path = user_filesystem / "cwd_dir"
217+ morph_file = data_dir_path / "morph_data"
218+ morph_data_text = [
219+ str (grid1 [i ]) + " " + str (func1 [i ]) for i in range (len (grid1 ))
220+ ]
221+ morph_data_text = "\n " .join (morph_data_text )
222+ morph_file .write_text (morph_data_text )
223+ target_file = data_dir_path / "target_data"
224+ target_data_text = [
225+ str (grid2 [i ]) + " " + str (func2 [i ]) for i in range (len (grid2 ))
226+ ]
227+ target_data_text = "\n " .join (target_data_text )
228+ target_file .write_text (target_data_text )
229+ run_cmd = ["diffpy.morph" ]
230+ for key , value in config .items ():
231+ run_cmd .append (f"--{ key } " )
232+ run_cmd .append (f"{ value } " )
233+ run_cmd .extend ([str (morph_file ), str (target_file )])
234+ run_cmd .append ("-n" )
235+ result = subprocess .run (run_cmd , capture_output = True , text = True )
236+ expected_error_message = (
237+ "diffpy.morph: error: " + expected_error_message
238+ )
239+ actual_error_message = result .stderr .strip ()
240+ assert actual_error_message == expected_error_message
241+
184242
185243# End of class TestRefine
186244
0 commit comments