desihub / specter

A toolkit for simulating multi-object spectrographs
Other
8 stars 7 forks source link

fixing bug in _gh by replacing with custom hermitenorm function #73

Closed lastephey closed 6 years ago

lastephey commented 6 years ago

This PR addresses the bug in GH-72. This bug was introduced during PR #71 and broke the _gh function. We have changed the previous self._hermitenorm to our custom numba-ized function custom_hermitenorm. We have also moved this function outside of the class since passing in self is no longer necessary. We have followed the same procedure used in optimizing the _pgh function in PR #71 .

Since this is now a JIT-compiled function there is likely some speedup, although we haven't made any measurements.

julienguy commented 6 years ago

another issue there:

raceback (most recent call last):
  File "/global/common/software/desi/users/jguy/teststand/bin/plot_psf_comparison_using_specter.py", line 64, in <module>
    fpix1=psf1._value(x+xy1[0],y+xy1[1],args.fiber,args.wavelength)
  File "/global/common/software/desi/users/jguy/specter/py/specter/psf/gausshermite.py", line 320, in _value
    xfunc1 = [self.gh(x, i, xc, sigma=sigx1) for i in range(degx1+1)]
  File "/global/common/software/desi/users/jguy/specter/py/specter/psf/gausshermite.py", line 320, in <listcomp>
    xfunc1 = [self.gh(x, i, xc, sigma=sigx1) for i in range(degx1+1)]
AttributeError: 'GaussHermitePSF' object has no attribute 'gh'
julienguy commented 6 years ago
Traceback (most recent call last):                                                                                                                                              
  File "/global/common/software/desi/users/jguy/teststand/bin/plot_psf_comparison_using_specter.py", line 64, in <module>                                                       
    fpix1=psf1._value(x+xy1[0],y+xy1[1],args.fiber,args.wavelength)                                                                                                             
  File "/global/common/software/desi/users/jguy/specter/py/specter/psf/gausshermite.py", line 320, in _value                                                                    
    xfunc1 = [gh(x, i, xc, sigma=sigx1) for i in range(degx1+1)]                                                                                                                
  File "/global/common/software/desi/users/jguy/specter/py/specter/psf/gausshermite.py", line 320, in <listcomp>                                                                
    xfunc1 = [gh(x, i, xc, sigma=sigx1) for i in range(degx1+1)]                                                                                                                
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/dispatcher.py", line 330, in _compile_for_args                
    raise e                                                                                                                                                                     
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/dispatcher.py", line 307, in _compile_for_args                
    return self.compile(tuple(argtypes))                                                                                                                                        
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/dispatcher.py", line 579, in compile                          
    cres = self._compiler.compile(args, return_type)                                                                                                                            
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/dispatcher.py", line 80, in compile                           
    flags=flags, locals=self.locals)                                                                                                                                            
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 766, in compile_extra                      
    return pipeline.compile_extra(func)                                                                                                                                         
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 362, in compile_extra                      
    return self._compile_bytecode()                                                                                                                                             
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 725, in _compile_bytecode                  
    return self._compile_core()                                                                                                                                                 
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 712, in _compile_core                      
    res = pm.run(self.status)                                                                                                                                                   
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 248, in run                                
    raise patched_exception                                                                                                                                                     
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 240, in run                                
    stage()                                                                                                                                                                     
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 454, in stage_nopython_frontend            
    self.locals)                                                                                                                                                                
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/compiler.py", line 867, in type_inference_stage               
    infer.build_constraint()                                                                                                                                                    
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/typeinfer.py", line 800, in build_constraint                  
    self.constrain_statement(inst)
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/typeinfer.py", line 959, in constrain_statement
    self.typeof_assign(inst)
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/typeinfer.py", line 1021, in typeof_assign
    self.typeof_global(inst, inst.target, value)
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/typeinfer.py", line 1117, in typeof_global
    typ = self.resolve_value_type(inst, gvar.value)
  File "/global/common/software/desi/edison/desiconda/20180130-1.2.4-spec/conda/lib/python3.6/site-packages/numba/typeinfer.py", line 1040, in resolve_value_type
    raise TypingError(msg, loc=inst.loc)
numba.errors.TypingError: Failed at nopython (nopython frontend)
Untyped global name 'n': cannot determine Numba type of <class 'object'>
File "../../../../../../../common/software/desi/users/jguy/specter/py/specter/psf/gausshermite.py", line 398
lastephey commented 6 years ago

@julienguy Thanks for the error messages. Can you send me the script or command you're using so I can just test myself?

julienguy commented 6 years ago

I simply try to use the function psf._value(x,y,fiber,wavelength)

The full command I am trying to run at NERSC on an edison interactive node is the following:

/global/common/software/desi/users/jguy/teststand/bin/plot_psf_comparison_using_specter.py --psf1  /project/projectdirs/desi/users/jguy/teststand/20181023/psf-b2-00010318-init.fits  --psf2 /project/projectdirs/desi/users/jguy/teststand/ccd_calibration_data/20180403/psf-b1.fits --fiber 10 --wavelength 4000
sbailey commented 6 years ago

While fixing this, please add a simple unit test that confirms that psf._value(x,y,fiber,wavelength) doesn't crash.

lastephey commented 6 years ago

Thanks for the info @julienguy

I have implemented a unit test and will push it and the fix momentarily @sbailey

This was the problem: _gh calls hermitenorm with a scalar value of u instead of an array. Our new custom_hermitenorm function cannot handle scalar values of u-- thus the failure. It might be worth considering adapting custom_hermitenorm to handle scalars in the future depending on how often _gh gets used.

I returned the previous functionality and put _gh and self._hermitenorm back to the way they were.

julienguy commented 6 years ago

Thank you Laurie for fixing this. I'm merging.