Skip to main content

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?
 

 

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 = u]

    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


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.


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.