Guest User

Untitled

a guest
Oct 5th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         Dim Reader As New StreamReader("counter.csv")
  2.         Dim Datapoints As New List(Of Datapoint)
  3.  
  4.  
  5.         ' Read in csv data
  6.        While Reader.Peek > -1
  7.  
  8.             Dim Line As String = Reader.ReadLine
  9.             Dim LineSplit() As String = Line.split(",")
  10.  
  11.             Dim Datapoint As New Datapoint
  12.             Datapoint.Time = New Date(CLng(LineSplit(0)))
  13.             Datapoint.Value = CLng(LineSplit(1))
  14.             Datapoints.Add(Datapoint)
  15.  
  16.         End While
  17.  
  18.  
  19.  
  20.         ' Sanity checks
  21.        For Each datapoint In Datapoints
  22.  
  23.             Dim CollectionDate As Date = New Date(2016, 10, 5)
  24.             Dim MinAcceptableCounterVal As Long = 57000
  25.             Dim MaxAcceptableCounterVal As Long = 100000
  26.             Dim AcceptibleDateWindowDays = 2
  27.  
  28.             If datapoint.Value < MinAcceptableCounterVal Or datapoint.Value > MaxAcceptableCounterVal Then
  29.                 Throw New Exception("Date out of bounds")
  30.             End If
  31.  
  32.             If datapoint.Time < CollectionDate.AddDays(-AcceptibleDateWindowDays) Or datapoint.Time > CollectionDate.AddDays(AcceptibleDateWindowDays) Then
  33.                 Throw New Exception("Counter value out of bounds")
  34.             End If
  35.  
  36.         Next
  37.  
  38.  
  39.  
  40.         ' Write a sub-sampled representation of the counter data (10x less data points to plot, excel friendly)
  41.        Using Writer As New StreamWriter("counter.thin.csv")
  42.             For i As Integer = 0 To Datapoints.Count - 1 Step 10
  43.                 Writer.WriteLine(Datapoints(i).Time.Ticks & "," & Datapoints(i).Value)
  44.             Next
  45.         End Using
  46.  
  47.  
  48.  
  49.         ' Generate list of counter maxima (approximate due to jitter in counter values)
  50.        Dim Maxima As New List(Of Long)
  51.  
  52.         For i As Integer = 1 To Datapoints.Count - 1
  53.             If Datapoints(i - 1).Value > (Datapoints(i).Value + 20) Then
  54.                 Maxima.Add(Datapoints(i - 1).Value)
  55.             End If
  56.         Next
  57.  
  58.         Using Writer As New StreamWriter("counter.maxima.csv")
  59.             For Each Value In Maxima
  60.                 Writer.WriteLine(Value)
  61.             Next
  62.         End Using
  63.  
  64.  
  65.  
  66.         ' Maxima histogram
  67.        Dim BucketSize As Integer = 1000
  68.         Dim FirstBucket As Long = 57000
  69.         Dim LastBucket As Long = 100000
  70.         Dim BucketCount As Integer = (LastBucket - FirstBucket) / BucketSize
  71.         Dim Buckets(BucketCount - 1) As Integer
  72.  
  73.         For Each Value In Maxima
  74.             Dim BucketIdx As Integer = Math.Floor((Value - FirstBucket) / BucketSize)
  75.             Buckets(BucketIdx) += 1
  76.         Next
  77.  
  78.         Using Writer As New StreamWriter("counter.maxima.histogram.csv")
  79.             For i As Integer = 0 To Buckets.GetUpperBound(0)
  80.                 Writer.WriteLine(Buckets(i))
  81.             Next
  82.         End Using
Advertisement
Add Comment
Please, Sign In to add comment