Skip to main content
Question

Help me sort out the PythonCaller syntax from where AI left me to fend for myself


aron
Supporter
Forum|alt.badge.img+16
  • Supporter

I don’t have an programming background, so please bear with me. 

I need to find out the circular standard deviation of the rotation attributes from a set of features in degrees.

The built in AI in the PythonCaller has taken me as far as the code below. I have tried many different prompts, they use somewhat different methods to do the calculations, but all throw errors about missing methods or incorrect classes. It seems that the AI is unable to initialize the script “The FME way”, or I have not figured out the correct prompt.

What do I need to ad to the script in order to get the below working? My desired output is the circular_standard_deviation as a new attribute.

Another nooby question, what should I write in the box: Class to process features?
 

import math
import numpy as np

# Function to calculate circular standard deviation
def circular_standard_deviation(angles_deg):
    angles_rad = np.deg2rad(angles_deg)
    sin_sum = np.sum(np.sin(angles_rad))
    cos_sum = np.sum(np.cos(angles_rad))
    mean_angle = math.atan2(sin_sum, cos_sum)
    R = np.sqrt(sin_sum**2 + cos_sum**2) / len(angles_deg)
    deviation_rad = np.sqrt(-2 * np.log(R))
    return deviation_rad

# FME function to initialize and calculate circular standard deviation
def fme_function(fme_feature):
    angles_deg = fme_feature.getAttribute('angles_deg')
    deviation_rad = circular_standard_deviation(angles_deg)
    fme_feature.setAttribute('circular_standard_deviation', deviation_rad)

 

3 replies

birgit
Influencer
Forum|alt.badge.img+16
  • Influencer
  • May 9, 2025

Is this everything that is in your pythoncaller? Since your are using the AI in the pythoncaller I take it that your FME version is probably 2024+? In that case you need to use classes instead of just functions. I cant test if it works but I would expect something like this:

import fme
import fmeobjects
import math
import numpy as np


class FeatureProcessor(object):


    def __init__(self):

        pass

    def input(self, feature):
        angles_deg = fme_feature.getAttribute('angles_deg')
        deviation_rad = circular_standard_deviation(angles_deg)
        fme_feature.setAttribute('circular_standard_deviation', deviation_rad)
        self.pyoutput(feature)

    def circular_standard_deviation(angles_deg):
        angles_rad = np.deg2rad(angles_deg)
        sin_sum = np.sum(np.sin(angles_rad))
        cos_sum = np.sum(np.cos(angles_rad))
        mean_angle = math.atan2(sin_sum, cos_sum)
        R = np.sqrt(sin_sum**2 + cos_sum**2) / len(angles_deg)
        deviation_rad = np.sqrt(-2 * np.log(R))
        return deviation_rad

Do note that I haven't actually taken a look at the functionality of the provided code, I did some copy pasting into the format that FME wants. 


liamfez
Influencer
Forum|alt.badge.img+34
  • Influencer
  • May 9, 2025

Adding to what ​@birgit said, but specifically for your last question: “...what should I write in the box: Class to process features?”
The Class to Process Features should be whatever is listed as your defined class. That first object you define that you want to process. In this case it would be “FeatureProcessor”. When using the PythonCreator the default is FeatureCreator and for the PythonCaller it is FeatureProcessor.


aron
Supporter
Forum|alt.badge.img+16
  • Author
  • Supporter
  • May 9, 2025

Thanks all!

I got it working with the pointers above and some more digging in the documentation.

(I should ad that the final script does not look much like the one in my example, so don't count on the code above to be useful.)


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings