Skip to content

Commit 95734e2

Browse files
committed
Finish forgotten utils since I apparently got distracted
1 parent 1898bab commit 95734e2

File tree

18 files changed

+62
-48
lines changed

18 files changed

+62
-48
lines changed

src/Microsoft.ML.Core/Utilities/ReservoirSampler.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1313
/// The sample is created in one pass by calling <see cref="Sample"/> for every data point in the stream. Implementations should have
1414
/// a delegate for getting the next data point, which is invoked if the current data point should go into the reservoir.
1515
/// </summary>
16-
public interface IReservoirSampler<T>
16+
[BestFriend]
17+
internal interface IReservoirSampler<T>
1718
{
1819
/// <summary>
1920
/// If the number of elements sampled is less than the reservoir size, this should return the number of elements sampled.
@@ -49,7 +50,8 @@ public interface IReservoirSampler<T>
4950
/// for every data point in the stream. In case the next data point does not get 'picked' into the reservoir, the delegate is not invoked.
5051
/// Sampling is done according to the algorithm in this paper: <a href="https://epubs.siam.org/doi/pdf/10.1137/1.9781611972740.53">https://epubs.siam.org/doi/pdf/10.1137/1.9781611972740.53</a>.
5152
/// </summary>
52-
public sealed class ReservoirSamplerWithoutReplacement<T> : IReservoirSampler<T>
53+
[BestFriend]
54+
internal sealed class ReservoirSamplerWithoutReplacement<T> : IReservoirSampler<T>
5355
{
5456
// This array contains a cache of the elements composing the reservoir.
5557
private readonly T[] _cache;
@@ -122,7 +124,8 @@ public IEnumerable<T> GetSample()
122124
/// for every data point in the stream. In case the next data point does not get 'picked' into the reservoir, the delegate is not invoked.
123125
/// Sampling is done according to the algorithm in this paper: <a href="https://epubs.siam.org/doi/pdf/10.1137/1.9781611972740.53">https://epubs.siam.org/doi/pdf/10.1137/1.9781611972740.53</a>.
124126
/// </summary>
125-
public sealed class ReservoirSamplerWithReplacement<T> : IReservoirSampler<T>
127+
[BestFriend]
128+
internal sealed class ReservoirSamplerWithReplacement<T> : IReservoirSampler<T>
126129
{
127130
// This array contains pointers to the elements in the _cache array that are currently in the reservoir (may contain duplicates).
128131
private readonly int[] _reservoir;

src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1616
/// This class takes care of downloading resources needed by ML.NET components. Resources are located in
1717
/// a pre-defined location, that can be overridden by defining Environment variable <see cref="CustomResourcesUrlEnvVariable"/>.
1818
/// </summary>
19-
public sealed class ResourceManagerUtils
19+
[BestFriend]
20+
internal sealed class ResourceManagerUtils
2021
{
2122
private static volatile ResourceManagerUtils _instance;
2223
public static ResourceManagerUtils Instance
@@ -301,7 +302,9 @@ public static ResourceDownloadResults GetErrorMessage(out string errorMessage, p
301302
return errorResult;
302303
}
303304

305+
#pragma warning disable IDE1006
304306
[DllImport("libc", SetLastError = true)]
305307
private static extern int chmod(string pathname, int mode);
308+
#pragma warning restore IDE1006
306309
}
307310
}

src/Microsoft.ML.Core/Utilities/Stats.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1111
/// <summary>
1212
/// A class containing common statistical functions
1313
/// </summary>
14-
public static class Stats
14+
[BestFriend]
15+
internal static class Stats
1516
{
1617
/// <summary>
1718
/// Returns a number uniformly sampled from 0...(rangeSize-1)

src/Microsoft.ML.Core/Utilities/SubsetStream.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
2222
/// subset stream, the underlying stream will always remain open and
2323
/// undisposed.
2424
/// </summary>
25-
public sealed class SubsetStream : Stream
25+
[BestFriend]
26+
internal sealed class SubsetStream : Stream
2627
{
2728
private readonly Stream _stream;
2829
// The position of the stream.

src/Microsoft.ML.Core/Utilities/SummaryStatistics.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Microsoft.ML.Runtime.Internal.Utilities
88
{
9-
public abstract class SummaryStatisticsBase
9+
internal abstract class SummaryStatisticsBase
1010
{
1111
// Sum of squared difference from the current mean.
1212
protected double M2;
@@ -152,7 +152,8 @@ public void Add(SummaryStatisticsBase s)
152152
}
153153
}
154154

155-
public sealed class SummaryStatisticsUpToSecondOrderMoments : SummaryStatisticsBase
155+
[BestFriend]
156+
internal sealed class SummaryStatisticsUpToSecondOrderMoments : SummaryStatisticsBase
156157
{
157158
/// <summary>
158159
/// A convenient way to combine the observations of two Stats objects
@@ -177,7 +178,8 @@ public sealed class SummaryStatisticsUpToSecondOrderMoments : SummaryStatisticsB
177178
/// https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
178179
/// All quantities are weighted, except for <c>RawCount</c>.
179180
/// </summary>
180-
public sealed class SummaryStatistics : SummaryStatisticsBase
181+
[BestFriend]
182+
internal sealed class SummaryStatistics : SummaryStatisticsBase
181183
{
182184
// Sum of cubed difference from the current mean.
183185
private double _m3;

src/Microsoft.ML.Core/Utilities/SupervisedBinFinder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
2020
/// The class can be used several times sequentially, it is stateful and not thread-safe.
2121
/// Both Single and Double precision processing is implemented, and is identical.
2222
/// </summary>
23-
public sealed class SupervisedBinFinder
23+
[BestFriend]
24+
internal sealed class SupervisedBinFinder
2425
{
2526
private readonly struct ValuePair<T> : IComparable<ValuePair<T>>
2627
where T : IComparable<T>

src/Microsoft.ML.Core/Utilities/TextReaderStream.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1414
/// compensates by inserting <c>\n</c> line feed characters at the end of every
1515
/// input line, including the last one.
1616
/// </summary>
17-
public sealed class TextReaderStream : Stream
17+
[BestFriend]
18+
internal sealed class TextReaderStream : Stream
1819
{
1920
private readonly TextReader _baseReader;
2021
private readonly Encoding _encoding;

src/Microsoft.ML.Core/Utilities/ThreadUtils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static Thread CreateForegroundThread(ThreadStart start)
5959
/// that the workers have finished by its own means, will call <see cref="ThrowIfSet"/> to throw
6060
/// the set exception as an inner exception, in the wrapping thread.
6161
/// </summary>
62-
public sealed class ExceptionMarshaller : IDisposable
62+
[BestFriend]
63+
internal sealed class ExceptionMarshaller : IDisposable
6364
{
6465
private readonly CancellationTokenSource _ctSource;
6566
private readonly object _lock;
@@ -130,7 +131,8 @@ public void ThrowIfSet(IExceptionContext ectx)
130131
/// Provides a task scheduler that ensures a maximum concurrency level while
131132
/// running on top of the ThreadPool.
132133
/// </summary>
133-
public sealed class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
134+
[BestFriend]
135+
internal sealed class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
134136
{
135137
// Whether the current thread is processing work items.
136138
[ThreadStatic]

src/Microsoft.ML.Core/Utilities/Tree.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1717
/// </summary>
1818
/// <typeparam name="TKey">Children are keyed with values of this type</typeparam>
1919
/// <typeparam name="TValue">The type of the node value</typeparam>
20-
public sealed class Tree<TKey, TValue> : IDictionary<TKey, Tree<TKey, TValue>>
20+
[BestFriend]
21+
internal sealed class Tree<TKey, TValue> : IDictionary<TKey, Tree<TKey, TValue>>
2122
{
2223
// The key of this node in the parent, assuming this is a child node at all.
2324
// This back reference is necessary to complete any "remove" operations.

src/Microsoft.ML.Core/Utilities/VBufferUtils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1414
/// <summary>
1515
/// Convenience utilities for vector operations on <see cref="VBuffer{T}"/>.
1616
/// </summary>
17-
public static class VBufferUtils
17+
[BestFriend]
18+
internal static class VBufferUtils
1819
{
1920
private const float SparsityThreshold = 0.25f;
2021

src/Microsoft.ML.Data/DataLoadSave/Binary/Zlib/Zlib.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal static class Zlib
1212
{
1313
public const string DllPath = "zlib.dll";
1414

15+
#pragma warning disable IDE1006
1516
[DllImport(DllPath), SuppressUnmanagedCodeSecurity]
1617
private static extern unsafe Constants.RetCode deflateInit2_(ZStream* strm, int level, int method, int windowBits,
1718
int memLevel, Constants.Strategy strategy, byte* version, int streamSize);
@@ -44,6 +45,7 @@ public static unsafe Constants.RetCode InflateInit2(ZStream* strm, int windowBit
4445

4546
[DllImport(DllPath), SuppressUnmanagedCodeSecurity]
4647
public static extern unsafe Constants.RetCode inflateEnd(ZStream* strm);
48+
#pragma warning restore IDE1006
4749
}
4850

4951
[StructLayout(LayoutKind.Sequential)]

src/Microsoft.ML.Data/Evaluators/AucAggregator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.ML.Runtime.Data
1111
{
1212
public abstract partial class EvaluatorBase<TAgg>
1313
{
14-
protected abstract class AucAggregatorBase
14+
internal abstract class AucAggregatorBase
1515
{
1616
protected Single Score;
1717
protected Single Label;
@@ -30,7 +30,7 @@ public void ProcessRow(Single label, Single score, Single weight = 1)
3030
public abstract Double ComputeWeightedAuc(out Double unweighted);
3131
}
3232

33-
protected abstract class AucAggregatorBase<T> : AucAggregatorBase
33+
internal abstract class AucAggregatorBase<T> : AucAggregatorBase
3434
{
3535
private readonly ReservoirSamplerWithoutReplacement<T> _posReservoir;
3636
private readonly ReservoirSamplerWithoutReplacement<T> _negReservoir;
@@ -117,7 +117,7 @@ public override Double ComputeWeightedAuc(out Double unweighted)
117117
protected abstract Double ComputeWeightedAucCore(out double unweighted);
118118
}
119119

120-
protected sealed class UnweightedAucAggregator : AucAggregatorBase<Single>
120+
internal sealed class UnweightedAucAggregator : AucAggregatorBase<Single>
121121
{
122122
public UnweightedAucAggregator(IRandom rand, int reservoirSize)
123123
: base(rand, reservoirSize)
@@ -210,7 +210,7 @@ protected override void AddExample(List<Single> examples)
210210
}
211211
}
212212

213-
protected sealed class WeightedAucAggregator : AucAggregatorBase<WeightedAucAggregator.AucInfo>
213+
internal sealed class WeightedAucAggregator : AucAggregatorBase<WeightedAucAggregator.AucInfo>
214214
{
215215
public struct AucInfo
216216
{
@@ -345,7 +345,7 @@ protected override void AddExample(List<AucInfo> examples)
345345
}
346346
}
347347

348-
public abstract class AuPrcAggregatorBase
348+
internal abstract class AuPrcAggregatorBase
349349
{
350350
protected Single Score;
351351
protected Single Label;
@@ -364,7 +364,7 @@ public void ProcessRow(Single label, Single score, Single weight = 1)
364364
public abstract Double ComputeWeightedAuPrc(out Double unweighted);
365365
}
366366

367-
protected abstract class AuPrcAggregatorBase<T> : AuPrcAggregatorBase
367+
private protected abstract class AuPrcAggregatorBase<T> : AuPrcAggregatorBase
368368
{
369369
protected readonly ReservoirSamplerWithoutReplacement<T> Reservoir;
370370

@@ -393,7 +393,7 @@ public override Double ComputeWeightedAuPrc(out Double unweighted)
393393
protected abstract Double ComputeWeightedAuPrcCore(out Double unweighted);
394394
}
395395

396-
protected sealed class UnweightedAuPrcAggregator : AuPrcAggregatorBase<UnweightedAuPrcAggregator.Info>
396+
private protected sealed class UnweightedAuPrcAggregator : AuPrcAggregatorBase<UnweightedAuPrcAggregator.Info>
397397
{
398398
public struct Info
399399
{
@@ -466,7 +466,7 @@ protected override ValueGetter<Info> GetSampleGetter()
466466
}
467467
}
468468

469-
protected sealed class WeightedAuPrcAggregator : AuPrcAggregatorBase<WeightedAuPrcAggregator.Info>
469+
private protected sealed class WeightedAuPrcAggregator : AuPrcAggregatorBase<WeightedAuPrcAggregator.Info>
470470
{
471471
public struct Info
472472
{

src/Microsoft.ML.Data/Evaluators/BinaryClassifierEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ private struct RocInfo
535535
public readonly List<Double> WeightedRecall;
536536
public readonly List<Double> WeightedFalsePositiveRate;
537537

538-
public readonly AuPrcAggregatorBase AuPrcAggregator;
538+
internal readonly AuPrcAggregatorBase AuPrcAggregator;
539539
public double WeightedAuPrc;
540540
public double UnweightedAuPrc;
541541

src/Microsoft.ML.Data/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Legacy" + PublicKey.Value)]
1212
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Maml" + PublicKey.Value)]
13+
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.ResultProcessor" + PublicKey.Value)]
1314

1415
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Data" + PublicKey.Value)]
1516
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Api" + PublicKey.Value)]

src/Microsoft.ML.Data/Utilities/StreamUtils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
namespace Microsoft.ML.Runtime.Internal.Utilities
1010
{
1111
// REVIEW: Implement properly on CoreCLR.
12-
public static class StreamUtils
12+
[BestFriend]
13+
internal static class StreamUtils
1314
{
1415
public static Stream OpenInStream(string fileName)
1516
{

src/Microsoft.ML.Data/Utilities/TimerScope.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using Microsoft.ML.Runtime;
7-
using Microsoft.ML.Runtime.Data;
86

97
namespace Microsoft.ML.Runtime.Internal.Utilities
108
{
119
using Stopwatch = System.Diagnostics.Stopwatch;
1210

1311
/// <summary>
14-
/// A timer scope class that starts a Stopwatch when created, calculates and prints elapsed time, physical and virtual memory usages before sending these to the telemetry when disposed.
12+
/// A timer scope class that starts a <see cref="Stopwatch"/> when created, calculates and prints elapsed time, physical and virtual memory usages before sending these to the telemetry when disposed.
1513
/// </summary>
16-
public sealed class TimerScope : IDisposable
14+
[BestFriend]
15+
internal sealed class TimerScope : IDisposable
1716
{
1817
// Note that this class does not own nor dispose of this channel.
1918
private readonly IChannel _ch;

src/Microsoft.ML.Data/Utils/IntSequencePool.cs renamed to src/Microsoft.ML.Data/Utils/SequencePool.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
7-
using System.Collections;
86
using System.IO;
9-
using System.Linq;
10-
using System.Threading;
11-
using System.Text;
12-
using Microsoft.ML.Runtime.Model;
137

148
namespace Microsoft.ML.Runtime.Internal.Utilities
159
{
@@ -19,7 +13,8 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1913
/// A dictionary of uint sequences of variable length. Stores the sequences as
2014
/// byte sequences encoded with LEB128. Empty sequences (or null) are also valid.
2115
/// </summary>
22-
public sealed class SequencePool
16+
[BestFriend]
17+
internal sealed class SequencePool
2318
{
2419
// uint sequences are hashed into _mask+1 buckets. _buckets contains the ID of the first
2520
// sequence that falls in it (or -1 if it is empty).

src/Microsoft.ML.PipelineInference/DatasetFeaturesInference.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ public static class DatasetFeatureInference
2121
{
2222
public sealed class Stats
2323
{
24-
[JsonIgnore] public SummaryStatistics Statistics;
24+
[JsonIgnore] private SummaryStatistics _statistics;
2525

2626
[JsonIgnore] public double Sum;
2727

2828
public Stats()
2929
{
30-
Statistics = new SummaryStatistics();
30+
_statistics = new SummaryStatistics();
3131
}
3232

3333
public void Add(double x)
3434
{
3535
Sum += x;
36-
Statistics.Add(x);
36+
_statistics.Add(x);
3737
}
3838

3939
public void Add(IEnumerable<int> x)
@@ -43,31 +43,31 @@ public void Add(IEnumerable<int> x)
4343
}
4444

4545
[JsonProperty]
46-
public long Count => Statistics.RawCount;
46+
public long Count => _statistics.RawCount;
4747

4848
[JsonProperty]
49-
public double? NonZeroValueCount => Statistics.RawCount > 20 ? (double?)Statistics.Nonzero : null;
49+
public double? NonZeroValueCount => _statistics.RawCount > 20 ? (double?)_statistics.Nonzero : null;
5050

5151
[JsonProperty]
52-
public double? Variance => Statistics.RawCount > 20 ? (double?)Statistics.SampleVariance : null;
52+
public double? Variance => _statistics.RawCount > 20 ? (double?)_statistics.SampleVariance : null;
5353

5454
[JsonProperty]
55-
public double? StandardDeviation => Statistics.RawCount > 20 ? (double?)Statistics.SampleStdDev : null;
55+
public double? StandardDeviation => _statistics.RawCount > 20 ? (double?)_statistics.SampleStdDev : null;
5656

5757
[JsonProperty]
58-
public double? Skewness => Statistics.RawCount > 20 ? (double?)Statistics.Skewness : null;
58+
public double? Skewness => _statistics.RawCount > 20 ? (double?)_statistics.Skewness : null;
5959

6060
[JsonProperty]
61-
public double? Kurtosis => Statistics.RawCount > 20 ? (double?)Statistics.Kurtosis : null;
61+
public double? Kurtosis => _statistics.RawCount > 20 ? (double?)_statistics.Kurtosis : null;
6262

6363
[JsonProperty]
64-
public double? Mean => Statistics.RawCount > 20 ? (double?)Statistics.Mean : null;
64+
public double? Mean => _statistics.RawCount > 20 ? (double?)_statistics.Mean : null;
6565

6666
[JsonIgnore]
67-
public double Min => Statistics.Min;
67+
public double Min => _statistics.Min;
6868

6969
[JsonIgnore]
70-
public double Max => Statistics.Max;
70+
public double Max => _statistics.Max;
7171
}
7272

7373
public sealed class Column

0 commit comments

Comments
 (0)