-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTDataUtils.java
More file actions
195 lines (156 loc) · 4.67 KB
/
Copy pathSTDataUtils.java
File metadata and controls
195 lines (156 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package data;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import net.imglib2.FinalInterval;
import net.imglib2.FinalRealInterval;
import net.imglib2.Interval;
import net.imglib2.IterableRealInterval;
import net.imglib2.KDTree;
import net.imglib2.RealCursor;
import net.imglib2.RealInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.neighborsearch.KNearestNeighborSearch;
import net.imglib2.neighborsearch.KNearestNeighborSearchOnKDTree;
import net.imglib2.util.Util;
public class STDataUtils
{
static class DistanceStats
{
public double avgDist, medianDist, minDist, maxDist;
}
public static List< String > commonGeneNames( final List< String > names0, final List< String > names1 )
{
final ArrayList< String > commonGeneNames = new ArrayList<>();
final HashSet< String > names0Hash = new HashSet<>( names0 );
for ( final String name1 : names1 )
if ( names0Hash.contains( name1 ) )
commonGeneNames.add( name1 );
return commonGeneNames;
}
public static Interval getCommonInterval( final STData stDataA, final STData stDataB )
{
final ArrayList< STData > list = new ArrayList<>();
list.add( stDataA );
list.add( stDataB );
return getCommonInterval( list );
}
public static Interval getCommonIterableInterval( final Collection< ? extends RealInterval > datasets )
{
double[] min = null, max = null;
for ( final RealInterval dataset : datasets )
{
if ( min == null )
{
min = new double[ dataset.numDimensions() ];
max = new double[ dataset.numDimensions() ];
dataset.realMin( min );
dataset.realMax( max );
}
else
{
for ( int d = 0; d < min.length; ++d )
{
min[ d ] = Math.min( min[ d ], dataset.realMin( d ) );
max[ d ] = Math.max( max[ d ], dataset.realMax( d ) );
}
}
}
if ( min != null )
{
final long[] minL = new long[ min.length ];
final long[] maxL = new long[ max.length ];
for ( int d = 0; d < minL.length; ++d )
{
minL[ d ] = Math.round( Math.floor( min[ d ] ) );
maxL[ d ] = Math.round( Math.ceil( max[ d ] ) );
}
return new FinalInterval( minL, maxL );
}
else
return null;
}
public static Interval getIterableInterval( final RealInterval dataset )
{
final long[] minL = new long[ dataset.numDimensions() ];
final long[] maxL = new long[ dataset.numDimensions() ];
for ( int d = 0; d < minL.length; ++d )
{
minL[ d ] = Math.round( Math.floor( dataset.realMin( d ) ) );
maxL[ d ] = Math.round( Math.ceil( dataset.realMax( d ) ) );
}
return new FinalInterval( minL, maxL );
}
public static Interval getCommonInterval( final Collection< STData > datasets )
{
long[] min = null, max = null;
for ( final STData dataset : datasets )
{
if ( min == null )
{
min = new long[ dataset.getRenderInterval().numDimensions() ];
max = new long[ dataset.getRenderInterval().numDimensions() ];
dataset.getRenderInterval().min( min );
dataset.getRenderInterval().max( max );
}
else
{
for ( int d = 0; d < min.length; ++d )
{
min[ d ] = Math.min( min[ d ], dataset.getRenderInterval().min( d ) );
max[ d ] = Math.max( max[ d ], dataset.getRenderInterval().max( d ) );
}
}
}
if ( min != null )
return new FinalInterval( min, max );
else
return null;
}
public static < R extends RealLocalizable > DistanceStats distanceStats( final KDTree< R > tree )
{
final KNearestNeighborSearch< R > search = new KNearestNeighborSearchOnKDTree<>( tree, 2 );
final double[] values = new double[ (int)tree.size() ];
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
int i = 0;
for ( final R p : tree )
{
search.search( p );
values[ i ] = search.getDistance( 1 );
min = Math.min( values[ i ], min );
max = Math.max( values[ i ], max );
++i;
}
final DistanceStats stats = new DistanceStats();
stats.medianDist = Util.median( values );
stats.avgDist = Util.average( values );
stats.minDist = min;
stats.maxDist = max;
return stats;
}
public static RealInterval computeRealInterval( final IterableRealInterval< ? > coord )
{
if ( coord.size() == 0 )
return null;
final int n = coord.numDimensions();
final RealCursor< ? > cursor = coord.localizingCursor();
cursor.fwd();
final double[] min = new double[ n ];
final double[] max = min.clone();
cursor.localize( min );
cursor.localize( max );
while( cursor.hasNext() )
{
cursor.fwd();
for ( int d = 0; d < n; ++d )
{
final double pos = cursor.getDoublePosition( d );
min[ d ] = Math.min( pos, min[ d ] );
max[ d ] = Math.max( pos, max[ d ] );
}
}
return new FinalRealInterval( min, max );
}
}