Accessing Geometric Image Analysis Results Using Python

  • 4 February 2021
  • 5 replies
  • 613 views

Badge +2

The results of a Geometric Image Analysis may be accessed using:



gia = TheSystem.Analyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.GeometricImageAnalysis)
gia_setting = gia.GetSettings()
gia.ApplyAndWaitForCompletion()
gia_results = gia.GetResults()
gia.ApplyAndWaitForCompletion()
gia_data = zos.DoubleToNumpy(gia_results.GetDataGrid(0).Values).reshape(-1, gia_setting.NumberOfPixels)

where zos.DoubleToNumpy is tucked into the boilerplate PythonStandaloneApplication class.



def DoubleToNumpy(self, data):
'''Provided by Sandrine Auriol, Zemax'''
if 'numpy' not in sys.modules:
print('You have not imported numpy into this file')
return False
else:
src_hndl = GCHandle.Alloc(data, GCHandleType.Pinned)
try:
src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
cbuf = (ctypes.c_double * len(data)).from_address(src_ptr)
npData = np.frombuffer(cbuf, dtype=np.float64)
finally:
if src_hndl.IsAllocated: src_hndl.Free()
return npData

 


5 replies

Badge +2

Also, if you want to analyze the results using ImageJ or some other tool, the values can be saved to a tiff.



from PIL import Image


Image.fromarray(gia_data).save('gia_data.tif')

I am getting error “DoubleToNumpy() missing 1 required positional argument: 'data’ … one arguemtn is missing in you code? 

 

Hi Steve,

 

I am unable to get the data values from the GIA analysis. I have directly copied your sample code into my program and there does not seem to be a ‘Values’ attribute. Here is my error message:

 

gia_data = zos.DoubleToNumpy(gia_results.GetDataGrid(0).Values).reshape(-1, gia_setting.NumberOfPixels)
AttributeError: 'NoneType' object has no attribute 'Values'

 

Do you perhaps have a working example I could trial?

 

Regards,

Mark

From what I can tell, this only applies to false color and grey scale data output. Is there a way to get the spot diagram data?

Userlevel 6
Badge +2

Hi Mark,

Unfortunately, the GetDataGrid() only applies to a data structure that is exactly NxN.  Internally the output for GIA as a data grid is different than GIA as Spot Diagram (which provides data as a 9xM matrix).  Therefore, the only way to access the Spot Diagram data is to use GetTextResults() and manually parse the data. 

The other option would be to use IBatchRayTrace and trace the rays in Python.  The Ex22 and Ex23 in Zemax\ZOS-API\ZOS-API Sample Code\Python shows how to use IBatchRayTrace.  If you need to use an IMA file with a Field Size > 0, then you will need to recreate the pixelation of the object.  Even though pixelating the object will be easier as a Direct Ray Trace, I would suggest still using a Normalized Ray Trace for 2 reasons:

  1. It’s easier to fully sample the pupil/Stop
  2. Normalized Ray Trace works with Ray Aiming (if needed); a Direct Ray Trace bypasses the Ray Aiming algorithm

Reply