28 Replies Latest reply on Feb 5, 2015 4:54 PM by Dan Fishman

    Calculate event frame average with AF analysis

    carpentercr

      Instead of using a CDR, I would like to be able to calculate the average value of an event frame attribute over the time span of an event frame.  I was hoping to use the new AF analysis capability.  How can I obtain the start and end time of the event frame in the expression portion of an analysis in AF?  After obtaining the start and end times of the event frame, I would like to be able to valid the times for time span and then add 5 seconds to the start time and subtract 5 seconds from the end time.  Using the new start and end time, I want to calculate the average over that time span.

        • Re: Calculate event frame average with AF analysis
          Camille Metzinger

          Hi Charles,

           

          That's an interesting question. Typically, to calculate the average value of an event frame attribute over the time span of the event, you would set it up as an attribute within the event frame template.

          For example, you would use a PI Point Data Reference, select the appropriate attribute, and then pick average under the Value retrieval methods > By Time Range Option as shown in the screenshot below.

          Event Frame Average.JPG

          Based on the additional information you provided in your question, it looks like you are seeking a different approach. I was curious about your interest in calculating an average for a time range slightly offset from the event frame. Are you trying to do a correction? If so I wonder then, if it would make sense to try to adjust the start and end time when the event frame is actually triggered. For example, if you are using the event frame generation analysis there is a start trigger true option. Again, I am just trying to understand the motivation here which would dictate what type of approach would be the best. You may have already considered these other options I asked about and have determined why they don't fit with your requirements.

           

           

          Thanks,

          Camille

            • Re: Calculate event frame average with AF analysis
              carpentercr

              Camille,

               

              Thanks for the reply and request for clarification.  I have and do use the averaging capability on the attribute as you suggested.  To further elaborate why we want to use an offset, a simple example.  This is one of several needs we see.  Many of our applications have processing steps that may last from a few seconds to a few minutes.  We are interested in the section of data after the processing step starts and we go from unsteady state to steady state.  This may take just a few seconds.  See inserted image.  We want to exclude the beginning and ending portions of the data and only consider the steady state portion.  The event below is triggered by the blue line.  The data we want to average or do other things to is the blue line.  This is easy to do with ACE, but we are hoping to move away from ACE and use the new analytics.  The other option was to use a CDR. Seems like having access to the start and end data of the event frame would be a pretty common request when doing analytics.  I was hoping someone know a way to get the times.  Certainly open to other suggestions.  Another desire is to capture the midpoint value of an event frame. Again, easy in ACE, but we would like to be able to do this in AF Analytics.

               

              Force.PNG

                • Re: Calculate event frame average with AF analysis
                  Camille Metzinger

                  Hi Charles,

                   

                  Thanks for the additional information. Steve can certainly provide the best insight for cleanest ways to do this and the possibilities that will be available in upcoming releases. In the meantime, if I understand your case correctly, this is something that I think would work using the AF expression analytics:

                   

                  I have an event frame for downtime triggered by the change in the equipment status tag and compute the line production average during the downtime with the traditional methods we discussed at the beginning.

                  To compute a corrected average based off of a subset of the event frame duration, I first set up expression analysis to determine the corrected start time and end time of the event.

                  Last Downtime Start Time Adjusted: If'Status' = "downtime" And PrevVal('Status','*') = "running" then TimeStamp('Status')+5 Else NoOutput()

                  Last Downtime End Time Adjusted: If'Status' = "running" And PrevVal('Status','*') = "downtime" then TimeStamp('Status')-5 Else NoOutput()

                   

                  Notice that I am working off of the same logic that is used to trigger my event frame ('status' = 'downtime' from 'status' = 'running')

                   

                  Then, I computed the average with the adjusted times in a separate expression analysis where my scheduling is event-triggered off of changes to Last Downtime End Time Adjusted.

                  Corrected Downtime Duration Production Average: TagAvg('Production Rate','Last Downtime Start Time Adjusted','Last Downtime End Time Adjusted')

                   

                  With these analyses running I was able to get this-

                   

                  EventFramCorrections.JPG

                   

                  Let me know if you have any questions on this.


                  Thanks,
                  Camille

              • Re: Calculate event frame average with AF analysis
                Dan Fishman

                Charles, you are correct that you would need a CDR currently to do this.  Only way I can think of is to use another EF with the correct time frame.  Ideally, a child EF.  Perhaps use ACE to provide the correct timestamps or one could even generate the EF in ACE.    I believe the next versions of Asset-Based Analytics will allow you to set timetamps so this will make it easier to create the necessary EF.

                 

                I'll pass your question on to my colleagues as it would be great not to have to develop a CDR for this and have all the data within the same object.

                 

                Regards,

                Dan

                • Re: Calculate event frame average with AF analysis
                  skwan

                  Unfortunately there is no easy way to do this right now.  Your use case has been brought up before and it's in our backlog.  I can think of a way you can do it with the next release but it is still a workaround.  When do you need this?

                   

                  --

                  Steve

                  AF Product Manager

                  • Re: Calculate event frame average with AF analysis
                    Tom Albrecht

                    There is a way, but I think it is thwarted by a bug . The method is to calculate [1] the average from the EF(endtime) to EF(adjusted start time) and [2] the average from the EF(endtime) to EF(adjusted end time). Then compute your own weighted average ( [1] * time1 - [2] * time2 ) / (time1-time2).

                     

                    In an Event Frame template, define the point tag reference and under Value retrieval methods, select "Time Range Override" and in the Relative Time box, you'll need -5s for 5 seconds from the end of the event frame. This can be done automatically from another attribute turned into a string (e.g. have an attribute with a value of 5, and a second attribute that concatenates "-" & 1st_attribute & "s" to get -5s) and you can put that second attribute into the Time Range Override as %@2nd_attribute%. However, calculating the duration of the event frame using %EndTime% - %StartTime% is where the bug is (explained below)! All the points are tabulated below:

                         

                    NameAttributeTypeAttributeDefaultValueAttributeDataReferenceAttributeConfigString
                    Average BeginningDouble0PI Point|PI Point Sinusoid;TimeMethod=TimeRangeOverride;RelativeTime=%@EF Time Range Beginning%;TimeRangeMethod=Average
                    Average EndDouble0PI Point|PI Point Sinusoid;TimeMethod=TimeRangeOverride;RelativeTime=%@EF Time Range End%;TimeRangeMethod=Average
                    Average for Adjusted RangeDouble0FormulaA=Average Beginning;C=Average End;D=Before-After in Seconds;E=Beginning Rel Time Int;[(A*E-C*D)/(E-D)]
                    Before-After in SecondsDouble5
                    Beginning Rel Time IntInt320FormulaA=EF Start;B=EF End;C=Before-After in Seconds;[B-A-C]
                    EF EndDateTime0001-01-01 00:00:00String Builder"%EndTime%";
                    EF StartDateTime0001-01-01 00:00:00String Builder"%StartTime%";
                    EF Time Range BeginningString0String Builder"-";'Beginning Rel Time Int';"s";
                    EF Time Range EndString0String Builder"-";'Before-After in Seconds';"s";
                    PI Point SinusoidDouble0PI Point\\%Server%\sinusoid

                     

                    The BUG is when "Beginning Rel Time Int" is calculated. The value displayed is the correct number of seconds, and the relative time string shows the correct value, but the number that is passed to the Averaging Element is wrong. However, if the correct number is typed in for the "Beginning Rel Time Int" formula, everything works! I think the problem is that, in the formula, it thinks B-A = 0, despite showing a different number! Here are two pictures, the first with the formula as B-A-C, and the second with the number just typed in (notice the difference on the right side of the image, and how it affects the rest of the calculations in the EF):

                     

                    A1.PNGA2.PNG

                      • Re: Calculate event frame average with AF analysis
                        Dan Fishman

                        Tom, nice work and great find!!!!

                        I obtained the duration using the count of a PI Point and then used a formula to subtract the offset.  I then used string builder to convert to the EF Time Range Beginning as you have done.  After that I saw the same issue you faced!   There is definitely something going on with the string builder reading from an AF formula.  More to think about next week.  Thanks for sharing!

                          • Re: Calculate event frame average with AF analysis
                            Dan Fishman

                            Tom Albrecht, try [(B-C-A)*-1] and then remove the "-" in your string builder.  I'm not sure why this works but it appears when we parse the AFValue into an AFTimeSpan there is some unexpected behavior.  I'll email the AF SDK team about this one!  Nice find!

                             

                            Regards,

                             

                            Dan

                              • Re: Calculate event frame average with AF analysis
                                Tom Albrecht

                                That [(B-C-A)*-1] did not work for me. However, I do not understand what you did when you said:

                                I obtained the duration using the count of a PI Point and then used a formula to subtract the offset.

                                and maybe that makes a difference?

                                 

                                In tracing down what I think is the problem, I evaluated the DateTime values as integers. They both display different integers different by the number of seconds (as expected). However, in the (B-C-A) calculation, I think the "StartTime" value is calculated the same as EndTime - whether it's taking is value from the attribute timestamp or from the Event Frame [end] timestamp. Thus, B-C returns 0.

                              • Re: Calculate event frame average with AF analysis
                                carpentercr

                                When the new AF 2014 analytics came out, we tested some of the functionality and found issues using the time ranges.  The case number was 565117.  I attached the original document I sent with the case.  It is also on the case at the beginning or that transcript.  The attached document has a pretty good example of what we wanted to do.  Perhaps this is a related issue. Was supposed to get on the "fix" list.

                                 

                                I like all of the proposed solutions to what we would like to do.  The fact that we would probably do this type of correction hundreds of times make we wonder if the CDR is still the best option.  Our desire is to have these corrected averages as attributes on the event frames and then collect the data from the event frames for use in a data cube. We collect this data via OLEDB. So, we need all the associated event frame data with the event frame.  So, as far as a CDR, maybe you can help me understand how I would proceed with the CDR for this problem with event frames.  I have create a CDR for a normal attribute on an element and it works fine.  I am not sure the correct objects and methods to use with the CDR when applied to an attribute to get the start and end time of the event frame where the attribute is defined.  I can use the FindEventFrame method and find the last event frame in getvalue, but that does not seem efficient.  What would be the correct way to obtain the event frame properties (start and end time, parent,...) within the getvalue or getvalues methods for the event frame that just occurred.  At that point, using some of the built in methods for average, SD, min, max,... would be pretty easy.

                                 

                                Thanks,

                                Charley

                                  • Re: Calculate event frame average with AF analysis
                                    Dan Fishman

                                    Thanks. I'll take a look at the document. I took at look at CDR to do exactly what you wanted about 5 months back!  In fact, it was actually related to a question that came from your company .  I believe the CDR could just use the timeConext  in the AFValue GetValue override and this gave me the start and end time.  Basically, it seemed straightforward.  I never really completed the CDR and if I get time I should finish it up! Let me know if that is enough or I can try to find time to dig into this.

                                     

                                    For me, the best solution is the one that you can maintain and support into the future.

                                     

                                    Regards,

                                    Dan

                                      • Re: Calculate event frame average with AF analysis
                                        carpentercr

                                        Dan,

                                         

                                        I will test the timeContext object in the getvalue to see if the start and end times are there.  That would make it very easy to calculate whatever is desired.  I will setup a test scenario for the event frames to test it.  Right now, our event frames are turned off due to some other issues we are fighting.  Will let you know how it turns out.

                                         

                                        Thanks,

                                        Charley

                                          • Re: Calculate event frame average with AF analysis
                                            Dan Fishman

                                            Charley

                                            I took a CDR, applied it to an EF and attached the debugger and definitely saw the correct start and end times passed in! I would hope so, right?

                                             

                                            Looking at the AF SDK help, take a look at AFDataReference.Getvalue Method.  The timeConext is an AFTime object or an AFtimeRange object.  Pretty cool..looks like we took care of you on this one .  Let us know how this works out.

                                             

                                            Thanks,

                                            Dan

                                              • Re: Calculate event frame average with AF analysis
                                                carpentercr

                                                Good.  I hope to test that today or tomorrow.  That was certainly a good heads up on where to get the times.  Originally, I just could not think of where the overridden methods for the CDR would provide the event frame data.  Certainly makes sense that the data is there.  Will let you know how it works out.  This is still probably the best long term solution.  I really hate to develop a bunch of stuff in either ACE or with CDRs, but they are there for these higher end issues.

                                                • Re: Calculate event frame average with AF analysis
                                                  carpentercr

                                                  Hello Dan,

                                                   

                                                  I do have a CDR that works fine for an attribute on an element.  When I use it on an event frame, I am having some problems.  I have to debug the CDR remotely because the AF server that has the new analytics with AF 2014 does not have visual studio.  That is ok, I can debug the CDR on the element attribute without any issue.  When I tried the CDR on an event frame I get errors in the debugger such as "An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll and wasn't handled before a managed/native boundary".  If I do not use a form to build the config string with the CDR I still get the error, but it will breakpoint inside of the GetValue function.  The timeContext shows nothing in the debugger.  Sort of at a loss at this point.  I did this in VB instead of C# and am not sure what is happening.  Do you by chance have an example of a CDR that works on an event frame.  That would be most helpful.  The event frame does return the values in the event frame search, I just can get the times inside the GetValue method.  Though I was home free after our last email, but I seem to have hit a snag.  Thanks very much.  Charley

                                                    • Re: Calculate event frame average with AF analysis
                                                      Dan Fishman

                                                      That is odd.  I took an existing CDR and modified it to support EF by adding the below code for the GetValue override.  I can't really explain that message but maybe sharing my code will help.

                                                       

                                                       

                                                        public override AFValue GetValue(object context, object timeContext, AFAttributeList inputAttributes, AFValues inputValues)
                                                              {
                                                                  //check to see if it an EF by the timeConext, else continue
                                                      
                                                      
                                                                  if (timeContext != null)
                                                                  {
                                                      
                                                      
                                                                      AFTimeRange r = (AFTimeRange)timeContext;
                                                                      String st = r.StartTime.ToString();
                                                                      AFValue result2 = new AFValue(st, r.StartTime);
                                                                      return result2;
                                                                  }
                                                      

                                                       

                                                                 

                                                      also, I support all Contexts.

                                                       

                                                       

                                                       

                                                      public override AFDataReferenceContext SupportedContexts
                                                              {
                                                                  get
                                                                  {          
                                                                      return AFDataReferenceContext.All;
                                                                  }
                                                              }
                                                      
                                                        • Re: Calculate event frame average with AF analysis
                                                          carpentercr

                                                          Dan,

                                                           

                                                          I added the SupportedContext property and can now see the timerange for the event frames.  Did you realize I needed that.  This level of programming is a still over my head.  It is not completely clear ☺.  Thanks for your help.  This should get me moving on the event frames portion.  I am hoping that the error messages in the debugger are due to the need to use the debugger in a remote mode.  It will probably be a little while before I can get back to this.  I will let you know how it turns out…

                                                           

                                                          Charley

                                                            • Re: Calculate event frame average with AF analysis
                                                              Dan Fishman

                                                              I tossed in the support all Contexts above because I realized that will make it work with EF (and other items). Wasn't quite sure if that was it.  I guess a little luck by me to share that .  I also don't get to build these often so I'm really glad you are asking these questions.  Forces me to keep up with our customers!!! I would recommend watching the custom data references videos found here: https://pisquare.osisoft.com/community/Master-PI/developing+applications+with+PI+AF+SDK.  I thought the CDR videos that Brandon Perry made rock.

                                                                • Re: Calculate event frame average with AF analysis
                                                                  carpentercr

                                                                  Dan,

                                                                   

                                                                  Sorry to be a pest about this CDR, but I seem to still have issues with getting the actual pinpoint data from within the getvalue method.  My plan was to access data from the archive and use the pipoint.summary method to get the statistical data over the timerange from within the getvalue method in the CDR.  I don’t seem to be able to create the pipoint instances or access the methods for the pipoint.  I am starting to feel slightly inferior in this endeavor.   It seems like it should be fairly simple.  I can do all this in a console program, but this CDR is being difficult.  Do you think you could provide an example of how to access data for a pinpoint in the archive from within the getvalue method for a CDR?

                                                                   

                                                                  Is this the wrong forum to ask these questions?

                                                                   

                                                                  Thanks again.

                                                                  Charley

                                                                    • Re: Calculate event frame average with AF analysis
                                                                      Dan Fishman

                                                                      Sure.  I would not take my code and just convert it to VB without thinking about what I did and modifying it to deal with special cases.  Depending on how you do this you do not need to use a PIPoint.

                                                                       

                                                                      I basically truncate the time range on either side by 60 seconds.  Think what will happen if the time range is less than 1 minute...hmm.  For testing, 60 seconds was more exciting to me than 5 seconds.

                                                                      Attribute 2 is using the customer data reference...I stole my "string test" CDR.

                                                                       

                                                                      CDR_result.jpg

                                                                       

                                                                      I hope this code will get you started.

                                                                       

                                                                      Have a good weekend!

                                                                       

                                                                              public override AFValue GetValue(object context, object timeContext, AFAttributeList inputAttributes, AFValues inputValues)
                                                                              {
                                                                                  //check to see if it an EF by the timeConext, else continue
                                                                      
                                                                      
                                                                                  if (timeContext != null)
                                                                                  {              
                                                                                     
                                                                                      AFTimeRange range = (AFTimeRange)timeContext;
                                                                                      //check for end time
                                                                                      if (range.EndTime == AFTime.MaxValue)
                                                                                      {
                                                                                          AFValue value = new AFValue();
                                                                                          value.Status = AFValueStatus.Bad;
                                                                                          value.Value = new Exception("No endtime");
                                                                                          return value;
                                                                                      }
                                                                                      range.StartTime = new AFTime(range.StartTime.UtcSeconds + 60);
                                                                                      range.EndTime = new AFTime(range.EndTime.UtcSeconds - 60);
                                                                              IDictionary<AFSummaryTypes,AFValue> average=  inputAttributes[0].Data.Summary(range, AFSummaryTypes.Average, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto);
                                                                                  return  average[AFSummaryTypes.Average];
                                                                                  }
                                                                      
                                                                      
                                                                        • Re: Calculate event frame average with AF analysis
                                                                          carpentercr

                                                                          Dan,

                                                                           

                                                                          I have the all the parts I need to finish.  We will add several options to this CDR and will provide a very useful tool for a lot of the types of data collection that we like to do.  Some of my confusion tends to be around the general use of the AFSDK and what to use to do what.  It is very powerful.  So, to be a pest one more time.  We have also been asked about the ability to write the results back to pi tags to access the results in the historian using some of the other tools.  Would you have an example of the best way to store values to a pi tag from within the getvalue method.

                                                                           

                                                                          You have been extremely helpful and I certainly appreciate all the help.

                                                                           

                                                                          Charley

                                                                            • Re: Calculate event frame average with AF analysis
                                                                              Dan Fishman

                                                                              Thanks!  Very interesting question and usually I would recommend the Setvalue method for an attribute.  I guess we need to understand if you are using a PI Point defined in the configuration string or where this PI Point is.  Is it on an attribute on the EF?  It seems like your config string will be getting a little longer.

                                                                               

                                                                              I am wondering why you want send a value to PI because this gets interesting. This Getvalue method is called whenever you obtain the value of the attribute and the Event Frame has not had captured values.  Basically, hitting refresh 10 times when viewing this EF will send 10 values to PI!

                                                                               

                                                                               

                                                                              Of course you can check to see if it has sent a value to PI or potentially update the configuration string with a flag that means it has already been sent to PI.  Before you return the average you could set the config string to indicate that there is no need to send a value to PI, and then return.   Another option is a tool that just reads from EF and sends to PI. Overall, very fun little project!

                                                                               

                                                                              Let me know if this all makes sense and if not I can post examples and discuss a little more when I have time.

                                                                               

                                                                              Regards,

                                                                              Dan

                                                                                • Re: Calculate event frame average with AF analysis
                                                                                  carpentercr

                                                                                  Dan,

                                                                                   

                                                                                  Excellent points about writing the data to the historian.  I believe that given that issue, we will not do that.  But just to answer the question of how to do that, what classes and methods would you use to read/write a pi tag with the archive from the getvalue method?  The setvalue method is for attributes, not pinpoints??

                                                                                   

                                                                                  As usual, thanks again.  I have pretty much complete the CDR that we wanted.  Just a little more to do on the form.

                                                                                    • Re: Calculate event frame average with AF analysis
                                                                                      Dan Fishman

                                                                                      Great.  I did get the CDR to only do an action on first time it has been read after the EF has closed.  Maybe I'll work on it a little more but rather academic in nature for me.  Sorry about not getting back to you.  I've been out on vacation and I see our full time moderators didn't get to this yet.  Allows me to play more so I'm happy .

                                                                                       

                                                                                      Regarding, which is the best way to read/write to a PI Tag I would say it depends if you are using either pure PI Points outside of AF or if you are using AF Attributes.  In the case of a standalone application that is not using AF you can use our PI Point class and use all of the methods such as Snapshot, Recorded value and of course UpdateValue to send a value to PI.

                                                                                       

                                                                                      For AFData we actually have nearly the exact same methods.  In the AFSDK help, see under AF SDK Reference> PI SDK Equivalents.  I started here my first few times using the AF SDK when reading and writing to PI.

                                                                                       

                                                                                      Also, I had suggested using GetValue and Setvalue.  Yeah, SetValue is only for AFAttributes.  For SetValue method of an AFAttribute note that "if the data reference is a PI Point then this call is equivalent to UpdateValue Overload with AFUpdateOption set to Replace"

                                                                                      For GetValue, it works in a similar manner.  I know you are working in the Context of AF, so to send a value to PI given an Attribute with a PI Point Data Reference I would use SetValue.  If I needed different overloads,  I would use AFAttribute.Data.UpdateValue or UpdateValues...

                                                                                      If I only had a PI Point object I would use PIPoint.Data.UpdateValue. 


                                                                                      As always, let us know if you have questions.