Skip to main content
Solved

Use attribute in attribute creator adjacent fields settings


bromp
Contributor
Forum|alt.badge.img+5
  • Contributor

Hi there,

I am trying to calculate a rolling average across a dynamic number of features for a certain attribute. I saw this post here explaining that you can do it using the adjacent features parameter in the attribute creator:

Calculating moving average (3 rolling points) in FME. | Community

However my issue is that the number of features that the rolling average needs to be calculated over is dynamic, it could change depending on the requirement of the incoming data. This number of features required is stored in an attribute called ‘consec_avg_requirement’. I therefore need to be able to set the number of prior features to use in the rolling average to be whatever the value is in that field -1. The only option shown here is to use a user parameter… but is it possible to assign an attribute to a user parameter that could vary with each run?
 

 

Best answer by nielsgerrits

One way to solve this is to use Python. Not that I am a Python expert, but ChatGPT can help with that. Attached working sample.

import fmeobjects

class FeatureProcessor(object):
    def __init__(self):
        self.values = []

    def input(self, feature):
        # Get the attribute and period values
        value = feature.getAttribute('value')
        period = feature.getAttribute('period')
        
        # Convert value to integer if possible
        try:
            value = int(value)
            self.values.append(value)
            
            # If we have at least as much values as the period is, calculate the rolling average
            if len(self.values) >= period:
                rolling_avg = sum(self.values[-period:]) / period
                feature.setAttribute('rolling_average', rolling_avg)
        except (TypeError, ValueError):
            # Handle cases where conversion to integer fails
            feature.setAttribute('rolling_average', None)
        
        # Output the feature
        self.pyoutput(feature)

    def close(self):
        pass

View original
Did this help you find an answer to your question?

3 replies

nielsgerrits
VIP
Forum|alt.badge.img+54
  • Best Answer
  • June 27, 2025

One way to solve this is to use Python. Not that I am a Python expert, but ChatGPT can help with that. Attached working sample.

import fmeobjects

class FeatureProcessor(object):
    def __init__(self):
        self.values = []

    def input(self, feature):
        # Get the attribute and period values
        value = feature.getAttribute('value')
        period = feature.getAttribute('period')
        
        # Convert value to integer if possible
        try:
            value = int(value)
            self.values.append(value)
            
            # If we have at least as much values as the period is, calculate the rolling average
            if len(self.values) >= period:
                rolling_avg = sum(self.values[-period:]) / period
                feature.setAttribute('rolling_average', rolling_avg)
        except (TypeError, ValueError):
            # Handle cases where conversion to integer fails
            feature.setAttribute('rolling_average', None)
        
        # Output the feature
        self.pyoutput(feature)

    def close(self):
        pass


crutledge
Influencer
Forum|alt.badge.img+33
  • Influencer
  • June 27, 2025

Hi ​@bromp 

The only option shown here is to use a user parameter… but is it possible to assign an attribute to a user parameter that could vary with each run?

I don’t know if this is possible another way (like ​@nielsgerrits  suggests to use in python) but it seems to me like this would be a great suggestion in ideas: To be able to use an attribute for a variable value in the Adjacent Features function.


bromp
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • June 30, 2025

Thanks ​@nielsgerrits and ​@crutledge ,

I have implemented the python solution and that works nicely, I’ll also submit an idea to allow the attribute selection in the adjacent features section.

 


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