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
Developing Expertise in PI System and Related Technologies: A Comprehensive Training Roadmap
This blog outlines a comprehensive training roadmap for developing expertise in the PI System and related technologies. Structured over four weeks, the program covers essential technologies like the PI System, Asset Framework, and various APIs, providing a strong foundation for data management and analytics.
Roshan Soni
Traversing an AF Database Hierarchy to Count All Elements Using OSIsoft AF SDK
Learn how to use the OSIsoft AF SDK in C# to traverse an AF database and count all elements within its hierarchy. This blog post provides a comprehensive guide with code examples for connecting, traversing, and counting AF elements.
Roshan Soni
A Beginner's Guide to Learning the OSIsoft PI System
Unlock the power of real-time data management and analytics with OSIsoft PI System. This beginner's guide provides a structured learning path and key resources to help you effectively learn the PI System.
Roshan Soni