File: BBLog.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (190 lines) | stat: -rwxr-xr-x 5,562 bytes parent folder | download | duplicates (3)
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
package cardinality;

import java.util.concurrent.atomic.AtomicLongArray;

import shared.Parser;
import shared.Tools;
import structures.LongList;

/**
 * @author Brian Bushnell
 * @date Feb 20, 2020
 *
 */
public final class BBLog extends CardinalityTracker {
	
	/*--------------------------------------------------------------*/
	/*----------------        Initialization        ----------------*/
	/*--------------------------------------------------------------*/
	
	/** Create a LogLog with default parameters */
	BBLog(){
		this(2048, 31, -1, 0);
	}
	
	/** Create a LogLog with parsed parameters */
	BBLog(Parser p){
		super(p);
		maxArrayA=(atomic ? new AtomicLongArray(buckets) : null);
		maxArray=(atomic ? null : new long[buckets]);
		counts=(trackCounts ? new int[buckets] : null);
	}
	
	/**
	 * Create a LogLog with specified parameters
	 * @param buckets_ Number of buckets (counters)
	 * @param k_ Kmer length
	 * @param seed Random number generator seed; -1 for a random seed
	 * @param minProb_ Ignore kmers with under this probability of being correct
	 */
	BBLog(int buckets_, int k_, long seed, float minProb_){
		super(buckets_, k_, seed, minProb_);
		maxArrayA=(atomic ? new AtomicLongArray(buckets) : null);
		maxArray=(atomic ? null : new long[buckets]);
		counts=(trackCounts ? new int[buckets] : null);
	}
	
	/*--------------------------------------------------------------*/
	/*----------------           Methods            ----------------*/
	/*--------------------------------------------------------------*/
	
	@Override
	public final long cardinality(){
		double difSum=0;
		double estLogSum=0;
		int count=0;
		LongList list=new LongList(buckets);
		//assert(atomic);
		if(atomic){
			for(int i=0; i<maxArrayA.length(); i++){
				long val=maxArrayA.get(i);
				if(val>0){
//					System.err.println("val="+val);
					long dif=Long.MAX_VALUE-val;
					difSum+=dif;
					count++;
					double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
					estLogSum+=Math.log(est);
					list.add(dif);
				}
			}
		}else{
			for(int i=0; i<maxArray.length; i++){
				long val=maxArray[i];
				if(val>0){
					long dif=Long.MAX_VALUE-val;
					difSum+=dif;
					count++;
					double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
					estLogSum+=Math.log(est);
					list.add(dif);
				}
			}
		}
		int div=count;//Could also be count be that causes problems
		final double mean=difSum/Tools.max(div, 1);
		final double estimatePerSet=2*(Long.MAX_VALUE/mean)*SKIPMOD;
		final double total=estimatePerSet*div*((count+buckets)/(float)(buckets+buckets));

		final double estSum=div*Math.exp(estLogSum/(Tools.max(div, 1)));
		list.sort();
		long median=list.median();
		double medianEst=2*(Long.MAX_VALUE/(double)median)*SKIPMOD*div;
		
//		new Exception().printStackTrace();
		
//		System.err.println(maxArray);
//		//Overall, it looks like "total" is the best, then "estSum", then "medianEst" is the worst, in terms of variance.
//		System.err.println("difSum="+difSum+", count="+count+", mean="+mean+", est="+estimatePerSet+", total="+(long)total);
//		System.err.println("estSum="+(long)estSum+", median="+median+", medianEst="+(long)medianEst);
		
		long cardinality=(long)(total);
		lastCardinality=cardinality;
		return cardinality;
	}

	@Override
	public int[] getCounts(){
		return counts;
	}
	
	@Override
	public final void add(CardinalityTracker log){
		assert(log.getClass()==this.getClass());
		add((BBLog)log);
	}
	
	public void add(BBLog log){
		if(atomic && maxArrayA!=log.maxArrayA){
			for(int i=0; i<buckets; i++){
				maxArrayA.set(i, Tools.max(maxArrayA.get(i), log.maxArrayA.get(i)));
			}
		}else 
		if(maxArray!=log.maxArray){
			if(counts==null){
				for(int i=0; i<buckets; i++){
					maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]);
				}
			}else{
				for(int i=0; i<buckets; i++){
					final long a=maxArray[i], b=log.maxArray[i];
					if(a==b){
						counts[i]+=log.counts[i];
					}else if(b>a){
						maxArray[i]=b;
						counts[i]=log.counts[i];
					}
				}
			}
		}
	}
	
	@Override
	public void hashAndStore(final long number){
//		if(number%SKIPMOD!=0){return;}
//		final long key=hash(number, tables[((int)number)&numTablesMask]);
		final long key=Tools.hash64shift(number);
		
//		if(key<minKey){return;}
		final int bucket=(int)(key&bucketMask);
		
		if(atomic){
			long x=maxArrayA.get(bucket);
			while(key>x){
				boolean b=maxArrayA.compareAndSet(bucket, x, key);
				if(b){x=key;}
				else{x=maxArrayA.get(bucket);}
			}
		}else{
			if(trackCounts){
				if(key>maxArray[bucket]){
					maxArray[bucket]=key;
					counts[bucket]=1;
				}else if(key==maxArray[bucket]){
					counts[bucket]++;
				}
			}else{
				maxArray[bucket]=Tools.max(key, maxArray[bucket]);
			}
		}
	}
	
	@Override
	public final float[] compensationFactorLogBucketsArray(){
		return null;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------            Fields            ----------------*/
	/*--------------------------------------------------------------*/

	/** Maintains state.  These are the actual buckets. */
	private final long[] maxArray;
	/** Atomic version of maxArray. */
	private final AtomicLongArray maxArrayA;
	/** Counts associated with values in the buckets. */
	private final int[] counts;
	
//	private static long minKey=(long)(0.75f*Long.MAX_VALUE); //non-atomic 15% faster without this
	
}