Using Python to Filter and Calculate Average Data in OSIsoft PI with AFSDK
Learn how to use Python with AFSDK to compute average daily data from OSIsoft PI while filtering out negative values, leveraging dynamic AFAttributes.
Roshan Soni
Using Python to Filter and Calculate Average Data in OSIsoft PI with AFSDK
Working with OSIsoft PI for data analytics is a common task in industries requiring time-series data management. One essential activity in this context is computing averages across data points while applying specific filters, such as removing negative values.
In this guide, I’ll walk you through the process of calculating average daily data for multiple PI tags in Python, excluding any negative data points, using the power of AFSDK (Asset Framework Software Development Kit). The challenge is that the direct usage of FilteredSummaries for a PIPointList in the code may not function as expected. Here’s how you can address it by leveraging AF attributes.
Understanding the Functionality
When working with the PI System, a common requirement is to filter out certain values (e.g., negative values) before calculating summaries like averages. Although there is a syntax for filtering, it may not apply directly to a list of PI Points using PIPointList. Hence, an alternative approach using AF attributes is necessary.
Approach Using AFAttributes
To calculate an average while excluding negative values, we need to utilize AF Attributes that allow us to access the FilteredSummaries method properly. Here's how to do it:
-
Generate AFAttributes: Create AFAttributes from the existing PIPoints. These attributes should belong to an AFAttributeList instead of a PIPointList.
-
Dynamically Create and Use Attributes: Leverage the AFSDK library to dynamically build AFAttributes that aren’t tied to a base element, enabling the use of a self-referential filtering syntax.
-
Filter and Compute: Use the
FilteredSummariesmethod with the necessary filtering logic for the AFAttributes, which allows proper filtering and calculation of the average.
Step-by-Step Code Solution
Below is a modified version of the approach to tackle this problem using Python and AFSDK:
from OSIsoft.AF import AFAttributeList
from OSIsoft.AF.Data import *
from OSIsoft.AF.Asset import AFAttribute, AFAttributeTemplate
from OSIsoft.AF.Time import AFTimeSpan, AFTime
from OSIsoft.AF.PI import PIPoint
# Assume piPoints_list is a list of PIPoint objects
# time and timespan are defined as per the requirement
attributes = []
for point in piPoints_list:
attr = AFAttribute(PIPoint\_(point))
attributes.append(attr)
attribute_list = AFAttributeList(attributes)
time_range_start = AFTime('2023-01-01T00:00:00Z') # example start time
timeSpan = AFTimeSpan(Duration=86400) # 24 hours span for daily data
results = attribute_list.FilteredSummaries(
time_range_start, timeSpan,
". > 0",
AFSummaryTypes.Average,
AFCalculationBasis.TimeWeighted,
AFSampleType.ExpressionRecordedValues,
AFTimeSpan.Parse("10m"),
AFTimestampCalculation.EarliestTime
)
Conclusion
By embracing AF Attributes for filtering in combination with Python, this approach enables robust data manipulation and filtering on PIPoints, akin to advanced calculations possible in tools like PI DataLink. Dynamically creating non-element AFAttributes allows for seamless calculation of filtered summaries. This solution can be extended to include more complex filtering criteria as required by different industrial scenarios.
Remember, mastering such techniques improves your efficiency in managing large-scale PI System data operations, aiding in comprehensive data analysis and insightful decision-making.
Tags
About Roshan Soni
Expert in PI System implementation, industrial automation, and data management. Passionate about helping organizations maximize the value of their process data through innovative solutions and best practices.
No comments yet
Be the first to share your thoughts on this article.
Related Articles
Enhancing PI ProcessBook Trends with Banding and Zones: User Needs, Workarounds, and the Road Ahead
A look at the user demand for trend banding/zoning in OSIsoft PI ProcessBook, current VBA workarounds, UI challenges, and how future PI Vision releases aim to address these visualization needs.
Roshan Soni
Migrating PIAdvCalcFilVal Uptime Calculations from PI DataLink to PI OLEDB
Learn how to translate PI DataLink's PIAdvCalcFilVal advanced calculations—like counting uptime based on conditions—into efficient PI OLEDB SQL queries. Explore three practical approaches using PIAVG, PIINTERP, and PICOunt tables, and get tips for validation and accuracy.
Roshan Soni
Understanding PI Web API WebID Encoding: Can You Generate WebIDs Client-Side?
Curious about how PI Web API generates WebIDs and whether you can encode them client-side using GUIDs or paths? This article explores the encoding mechanisms, current documentation, and best practices for handling WebIDs in your applications.
Roshan Soni