Skip to content

Commit a84b68b

Browse files
committed
Update BinSampler.vb
1 parent bb712c0 commit a84b68b

1 file changed

Lines changed: 74 additions & 61 deletions

File tree

  • Data_science/Mathematica/SignalProcessing/SignalProcessing/Sampler

Data_science/Mathematica/SignalProcessing/SignalProcessing/Sampler/BinSampler.vb

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
11
#Region "Microsoft.VisualBasic::29521ef3388eb234bac5313fe88b3b7f, Data_science\Mathematica\SignalProcessing\SignalProcessing\Sampler\BinSampler.vb"
22

3-
' Author:
4-
'
5-
' asuka (amethyst.asuka@gcmodeller.org)
6-
' xie (genetics@smrucc.org)
7-
' xieguigang (xie.guigang@live.com)
8-
'
9-
' Copyright (c) 2018 GPL3 Licensed
10-
'
11-
'
12-
' GNU GENERAL PUBLIC LICENSE (GPL3)
13-
'
14-
'
15-
' This program is free software: you can redistribute it and/or modify
16-
' it under the terms of the GNU General Public License as published by
17-
' the Free Software Foundation, either version 3 of the License, or
18-
' (at your option) any later version.
19-
'
20-
' This program is distributed in the hope that it will be useful,
21-
' but WITHOUT ANY WARRANTY; without even the implied warranty of
22-
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23-
' GNU General Public License for more details.
24-
'
25-
' You should have received a copy of the GNU General Public License
26-
' along with this program. If not, see <http://www.gnu.org/licenses/>.
27-
28-
29-
30-
' /********************************************************************************/
31-
32-
' Summaries:
33-
34-
35-
' Code Statistics:
36-
37-
' Total Lines: 53
38-
' Code Lines: 32 (60.38%)
39-
' Comment Lines: 11 (20.75%)
40-
' - Xml Docs: 90.91%
41-
'
42-
' Blank Lines: 10 (18.87%)
43-
' File Size: 1.84 KB
44-
45-
46-
' Class BinSampler
47-
'
48-
' Properties: Range
49-
'
50-
' Constructor: (+2 Overloads) Sub New
51-
' Function: AggregateSignal, GetTicks
52-
'
53-
' /********************************************************************************/
3+
' Author:
4+
'
5+
' asuka (amethyst.asuka@gcmodeller.org)
6+
' xie (genetics@smrucc.org)
7+
' xieguigang (xie.guigang@live.com)
8+
'
9+
' Copyright (c) 2018 GPL3 Licensed
10+
'
11+
'
12+
' GNU GENERAL PUBLIC LICENSE (GPL3)
13+
'
14+
'
15+
' This program is free software: you can redistribute it and/or modify
16+
' it under the terms of the GNU General Public License as published by
17+
' the Free Software Foundation, either version 3 of the License, or
18+
' (at your option) any later version.
19+
'
20+
' This program is distributed in the hope that it will be useful,
21+
' but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
' GNU General Public License for more details.
24+
'
25+
' You should have received a copy of the GNU General Public License
26+
' along with this program. If not, see <http://www.gnu.org/licenses/>.
27+
28+
29+
30+
' /********************************************************************************/
31+
32+
' Summaries:
33+
34+
35+
' Code Statistics:
36+
37+
' Total Lines: 53
38+
' Code Lines: 32 (60.38%)
39+
' Comment Lines: 11 (20.75%)
40+
' - Xml Docs: 90.91%
41+
'
42+
' Blank Lines: 10 (18.87%)
43+
' File Size: 1.84 KB
44+
45+
46+
' Class BinSampler
47+
'
48+
' Properties: Range
49+
'
50+
' Constructor: (+2 Overloads) Sub New
51+
' Function: AggregateSignal, GetTicks
52+
'
53+
' /********************************************************************************/
5454

5555
#End Region
5656

57+
Imports System.Runtime.CompilerServices
5758
Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
59+
Imports Microsoft.VisualBasic.ComponentModel.TagData
5860
Imports Microsoft.VisualBasic.Math.Distributions.BinBox
5961

6062
''' <summary>
@@ -64,34 +66,46 @@ Public Class BinSampler
6466

6567
Dim signal As Signal
6668

69+
''' <summary>
70+
''' the signal x axis range
71+
''' </summary>
72+
''' <returns></returns>
6773
Public ReadOnly Property Range As DoubleRange
68-
Get
69-
Return New DoubleRange(signal.times)
70-
End Get
71-
End Property
7274

7375
Sub New(signal As Signal)
7476
Me.signal = signal
77+
Me.Range = New DoubleRange(signal.times)
7578
End Sub
7679

7780
Sub New(time As Double(), intensity As Double())
78-
Me.signal = New Signal(time.Select(Function(ti, i) New TimeSignal(ti, intensity(i))))
81+
Call Me.New(New Signal(time.Select(Function(ti, i) New TimeSignal(ti, intensity(i)))))
7982
End Sub
8083

8184
''' <summary>
8285
'''
8386
''' </summary>
84-
''' <param name="dt"></param>
87+
''' <param name="dt">
88+
''' the signal x axis resolution
89+
''' </param>
8590
''' <param name="aggregate">
8691
''' default is sum intensity data
8792
''' </param>
8893
''' <returns></returns>
8994
Public Function AggregateSignal(dt As Double, Optional aggregate As Func(Of IEnumerable(Of Double), Double) = Nothing) As Signal
9095
Static sum As Func(Of IEnumerable(Of Double), Double) = AddressOf Enumerable.Sum
91-
Return New Signal(GetTicks(dt, If(aggregate, sum)))
96+
Return New Signal(GetTicks(dt, Function(t, i) New TimeSignal(t, i), If(aggregate, sum)))
9297
End Function
9398

94-
Private Iterator Function GetTicks(dt As Double, aggregate As Func(Of IEnumerable(Of Double), Double)) As IEnumerable(Of TimeSignal)
99+
Public Function AggregateSignal(n As Integer, Optional aggregate As Func(Of IEnumerable(Of Double), Double) = Nothing) As Signal
100+
Return AggregateSignal(Range.Length / n, aggregate)
101+
End Function
102+
103+
<MethodImpl(MethodImplOptions.AggressiveInlining)>
104+
Public Function AggregateSignal(Of T As ITimeSignal)(n As Integer, [new] As Func(Of Double, Double, T), Optional aggregate As Func(Of IEnumerable(Of Double), Double) = Nothing) As IEnumerable(Of T)
105+
Return GetTicks(Range.Length / n, [new], If(aggregate, New Func(Of IEnumerable(Of Double), Double)(AddressOf Enumerable.Sum)))
106+
End Function
107+
108+
Private Iterator Function GetTicks(Of T As ITimeSignal)(dt As Double, [new] As Func(Of Double, Double, T), aggregate As Func(Of IEnumerable(Of Double), Double)) As IEnumerable(Of T)
95109
For Each box In CutBins.FixedWidthBins(signal, width:=dt, range:=Range, eval:=Function(ti) ti.time)
96110
If box.Count = 0 Then
97111
Continue For
@@ -101,8 +115,7 @@ Public Class BinSampler
101115
Dim into As Double = aggregate(From ti As TimeSignal
102116
In box.Raw
103117
Select ti.intensity)
104-
105-
Yield New TimeSignal(time, into)
118+
Yield [new](time, into)
106119
Next
107120
End Function
108121

0 commit comments

Comments
 (0)