Skip to content

Commit aaf226c

Browse files
LehontiLehonti Ramos
and
Lehonti Ramos
authored
File-scoped namespaces in files under Data (Microsoft.ML.Core) (#6789)
Co-authored-by: Lehonti Ramos <john@doe>
1 parent 34389b6 commit aaf226c

31 files changed

+5113
-5144
lines changed

src/Microsoft.ML.Core/Data/AnnotationBuilderExtensions.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@
44

55
using System;
66

7-
namespace Microsoft.ML.Data
7+
namespace Microsoft.ML.Data;
8+
9+
[BestFriend]
10+
internal static class AnnotationBuilderExtensions
811
{
9-
[BestFriend]
10-
internal static class AnnotationBuilderExtensions
11-
{
12-
/// <summary>
13-
/// Add slot names annotation.
14-
/// </summary>
15-
/// <param name="builder">The <see cref="DataViewSchema.Annotations.Builder"/> to which to add the slot names.</param>
16-
/// <param name="size">The size of the slot names vector.</param>
17-
/// <param name="getter">The getter delegate for the slot names.</param>
18-
public static void AddSlotNames(this DataViewSchema.Annotations.Builder builder, int size, ValueGetter<VBuffer<ReadOnlyMemory<char>>> getter)
19-
=> builder.Add(AnnotationUtils.Kinds.SlotNames, new VectorDataViewType(TextDataViewType.Instance, size), getter);
12+
/// <summary>
13+
/// Add slot names annotation.
14+
/// </summary>
15+
/// <param name="builder">The <see cref="DataViewSchema.Annotations.Builder"/> to which to add the slot names.</param>
16+
/// <param name="size">The size of the slot names vector.</param>
17+
/// <param name="getter">The getter delegate for the slot names.</param>
18+
public static void AddSlotNames(this DataViewSchema.Annotations.Builder builder, int size, ValueGetter<VBuffer<ReadOnlyMemory<char>>> getter)
19+
=> builder.Add(AnnotationUtils.Kinds.SlotNames, new VectorDataViewType(TextDataViewType.Instance, size), getter);
2020

21-
/// <summary>
22-
/// Add key values annotation.
23-
/// </summary>
24-
/// <typeparam name="TValue">The value type of key values.</typeparam>
25-
/// <param name="builder">The <see cref="DataViewSchema.Annotations.Builder"/> to which to add the key values.</param>
26-
/// <param name="size">The size of key values vector.</param>
27-
/// <param name="valueType">The value type of key values. Its raw type must match <typeparamref name="TValue"/>.</param>
28-
/// <param name="getter">The getter delegate for the key values.</param>
29-
public static void AddKeyValues<TValue>(this DataViewSchema.Annotations.Builder builder, int size, PrimitiveDataViewType valueType, ValueGetter<VBuffer<TValue>> getter)
30-
=> builder.Add(AnnotationUtils.Kinds.KeyValues, new VectorDataViewType(valueType, size), getter);
31-
}
21+
/// <summary>
22+
/// Add key values annotation.
23+
/// </summary>
24+
/// <typeparam name="TValue">The value type of key values.</typeparam>
25+
/// <param name="builder">The <see cref="DataViewSchema.Annotations.Builder"/> to which to add the key values.</param>
26+
/// <param name="size">The size of key values vector.</param>
27+
/// <param name="valueType">The value type of key values. Its raw type must match <typeparamref name="TValue"/>.</param>
28+
/// <param name="getter">The getter delegate for the key values.</param>
29+
public static void AddKeyValues<TValue>(this DataViewSchema.Annotations.Builder builder, int size, PrimitiveDataViewType valueType, ValueGetter<VBuffer<TValue>> getter)
30+
=> builder.Add(AnnotationUtils.Kinds.KeyValues, new VectorDataViewType(valueType, size), getter);
3231
}

src/Microsoft.ML.Core/Data/AnnotationUtils.cs

Lines changed: 402 additions & 403 deletions
Large diffs are not rendered by default.

src/Microsoft.ML.Core/Data/ColumnTypeExtensions.cs

Lines changed: 146 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -5,165 +5,164 @@
55
using System;
66
using Microsoft.ML.Runtime;
77

8-
namespace Microsoft.ML.Data
8+
namespace Microsoft.ML.Data;
9+
10+
/// <summary>
11+
/// Extension methods related to the ColumnType class.
12+
/// </summary>
13+
[BestFriend]
14+
internal static class ColumnTypeExtensions
915
{
1016
/// <summary>
11-
/// Extension methods related to the ColumnType class.
17+
/// Whether this type is a standard scalar type completely determined by its <see cref="DataViewType.RawType"/>
18+
/// (not a <see cref="KeyDataViewType"/> or <see cref="StructuredDataViewType"/>, etc).
19+
/// </summary>
20+
public static bool IsStandardScalar(this DataViewType columnType) =>
21+
(columnType is NumberDataViewType) || (columnType is TextDataViewType) || (columnType is BooleanDataViewType) ||
22+
(columnType is RowIdDataViewType) || (columnType is TimeSpanDataViewType) ||
23+
(columnType is DateTimeDataViewType) || (columnType is DateTimeOffsetDataViewType);
24+
25+
/// <summary>
26+
/// Zero return means it's not a key type.
27+
/// </summary>
28+
public static ulong GetKeyCount(this DataViewType columnType) => (columnType as KeyDataViewType)?.Count ?? 0;
29+
30+
/// <summary>
31+
/// Sometimes it is necessary to cast the Count to an int. This performs overflow check.
32+
/// Zero return means it's not a key type.
1233
/// </summary>
13-
[BestFriend]
14-
internal static class ColumnTypeExtensions
34+
public static int GetKeyCountAsInt32(this DataViewType columnType, IExceptionContext ectx = null)
1535
{
16-
/// <summary>
17-
/// Whether this type is a standard scalar type completely determined by its <see cref="DataViewType.RawType"/>
18-
/// (not a <see cref="KeyDataViewType"/> or <see cref="StructuredDataViewType"/>, etc).
19-
/// </summary>
20-
public static bool IsStandardScalar(this DataViewType columnType) =>
21-
(columnType is NumberDataViewType) || (columnType is TextDataViewType) || (columnType is BooleanDataViewType) ||
22-
(columnType is RowIdDataViewType) || (columnType is TimeSpanDataViewType) ||
23-
(columnType is DateTimeDataViewType) || (columnType is DateTimeOffsetDataViewType);
24-
25-
/// <summary>
26-
/// Zero return means it's not a key type.
27-
/// </summary>
28-
public static ulong GetKeyCount(this DataViewType columnType) => (columnType as KeyDataViewType)?.Count ?? 0;
29-
30-
/// <summary>
31-
/// Sometimes it is necessary to cast the Count to an int. This performs overflow check.
32-
/// Zero return means it's not a key type.
33-
/// </summary>
34-
public static int GetKeyCountAsInt32(this DataViewType columnType, IExceptionContext ectx = null)
35-
{
36-
ulong count = columnType.GetKeyCount();
37-
ectx.Check(count <= int.MaxValue, nameof(KeyDataViewType) + "." + nameof(KeyDataViewType.Count) + " exceeds int.MaxValue.");
38-
return (int)count;
39-
}
36+
ulong count = columnType.GetKeyCount();
37+
ectx.Check(count <= int.MaxValue, nameof(KeyDataViewType) + "." + nameof(KeyDataViewType.Count) + " exceeds int.MaxValue.");
38+
return (int)count;
39+
}
4040

41-
/// <summary>
42-
/// For non-vector types, this returns the column type itself (i.e., return <paramref name="columnType"/>).
43-
/// For vector types, this returns the type of the items stored as values in vector.
44-
/// </summary>
45-
public static DataViewType GetItemType(this DataViewType columnType) => (columnType as VectorDataViewType)?.ItemType ?? columnType;
46-
47-
/// <summary>
48-
/// Zero return means either it's not a vector or the size is unknown.
49-
/// </summary>
50-
public static int GetVectorSize(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 0;
51-
52-
/// <summary>
53-
/// For non-vectors, this returns one. For unknown size vectors, it returns zero.
54-
/// For known sized vectors, it returns size.
55-
/// </summary>
56-
public static int GetValueCount(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 1;
57-
58-
/// <summary>
59-
/// Whether this is a vector type with known size. Returns false for non-vector types.
60-
/// Equivalent to <c><see cref="GetVectorSize"/> &gt; 0</c>.
61-
/// </summary>
62-
public static bool IsKnownSizeVector(this DataViewType columnType) => columnType.GetVectorSize() > 0;
63-
64-
/// <summary>
65-
/// Gets the equivalent <see cref="InternalDataKind"/> for the <paramref name="columnType"/>'s RawType.
66-
/// This can return default(<see cref="InternalDataKind"/>) if the RawType doesn't have a corresponding
67-
/// <see cref="InternalDataKind"/>.
68-
/// </summary>
69-
public static InternalDataKind GetRawKind(this DataViewType columnType)
70-
{
71-
columnType.RawType.TryGetDataKind(out InternalDataKind result);
72-
return result;
73-
}
41+
/// <summary>
42+
/// For non-vector types, this returns the column type itself (i.e., return <paramref name="columnType"/>).
43+
/// For vector types, this returns the type of the items stored as values in vector.
44+
/// </summary>
45+
public static DataViewType GetItemType(this DataViewType columnType) => (columnType as VectorDataViewType)?.ItemType ?? columnType;
7446

75-
/// <summary>
76-
/// Equivalent to calling Equals(ColumnType) for non-vector types. For vector type,
77-
/// returns true if current and other vector types have the same size and item type.
78-
/// </summary>
79-
public static bool SameSizeAndItemType(this DataViewType columnType, DataViewType other)
80-
{
81-
if (other == null)
82-
return false;
83-
84-
if (columnType.Equals(other))
85-
return true;
86-
87-
// For vector types, we don't care about the factoring of the dimensions.
88-
if (!(columnType is VectorDataViewType vectorType) || !(other is VectorDataViewType otherVectorType))
89-
return false;
90-
if (!vectorType.ItemType.Equals(otherVectorType.ItemType))
91-
return false;
92-
return vectorType.Size == otherVectorType.Size;
93-
}
47+
/// <summary>
48+
/// Zero return means either it's not a vector or the size is unknown.
49+
/// </summary>
50+
public static int GetVectorSize(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 0;
9451

95-
public static PrimitiveDataViewType PrimitiveTypeFromType(Type type)
96-
{
97-
if (type == typeof(ReadOnlyMemory<char>) || type == typeof(string))
98-
return TextDataViewType.Instance;
99-
if (type == typeof(bool))
100-
return BooleanDataViewType.Instance;
101-
if (type == typeof(TimeSpan))
102-
return TimeSpanDataViewType.Instance;
103-
if (type == typeof(DateTime))
104-
return DateTimeDataViewType.Instance;
105-
if (type == typeof(DateTimeOffset))
106-
return DateTimeOffsetDataViewType.Instance;
107-
if (type == typeof(DataViewRowId))
108-
return RowIdDataViewType.Instance;
109-
return NumberTypeFromType(type);
110-
}
52+
/// <summary>
53+
/// For non-vectors, this returns one. For unknown size vectors, it returns zero.
54+
/// For known sized vectors, it returns size.
55+
/// </summary>
56+
public static int GetValueCount(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 1;
11157

112-
public static PrimitiveDataViewType PrimitiveTypeFromKind(InternalDataKind kind)
113-
{
114-
if (kind == InternalDataKind.TX)
115-
return TextDataViewType.Instance;
116-
if (kind == InternalDataKind.BL)
117-
return BooleanDataViewType.Instance;
118-
if (kind == InternalDataKind.TS)
119-
return TimeSpanDataViewType.Instance;
120-
if (kind == InternalDataKind.DT)
121-
return DateTimeDataViewType.Instance;
122-
if (kind == InternalDataKind.DZ)
123-
return DateTimeOffsetDataViewType.Instance;
124-
if (kind == InternalDataKind.UG)
125-
return RowIdDataViewType.Instance;
126-
return NumberTypeFromKind(kind);
127-
}
58+
/// <summary>
59+
/// Whether this is a vector type with known size. Returns false for non-vector types.
60+
/// Equivalent to <c><see cref="GetVectorSize"/> &gt; 0</c>.
61+
/// </summary>
62+
public static bool IsKnownSizeVector(this DataViewType columnType) => columnType.GetVectorSize() > 0;
12863

129-
public static NumberDataViewType NumberTypeFromType(Type type)
130-
{
131-
InternalDataKind kind;
132-
if (type.TryGetDataKind(out kind))
133-
return NumberTypeFromKind(kind);
64+
/// <summary>
65+
/// Gets the equivalent <see cref="InternalDataKind"/> for the <paramref name="columnType"/>'s RawType.
66+
/// This can return default(<see cref="InternalDataKind"/>) if the RawType doesn't have a corresponding
67+
/// <see cref="InternalDataKind"/>.
68+
/// </summary>
69+
public static InternalDataKind GetRawKind(this DataViewType columnType)
70+
{
71+
columnType.RawType.TryGetDataKind(out InternalDataKind result);
72+
return result;
73+
}
13474

135-
Contracts.Assert(false);
136-
throw new InvalidOperationException($"Bad type in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromType)}: {type}");
137-
}
75+
/// <summary>
76+
/// Equivalent to calling Equals(ColumnType) for non-vector types. For vector type,
77+
/// returns true if current and other vector types have the same size and item type.
78+
/// </summary>
79+
public static bool SameSizeAndItemType(this DataViewType columnType, DataViewType other)
80+
{
81+
if (other == null)
82+
return false;
83+
84+
if (columnType.Equals(other))
85+
return true;
86+
87+
// For vector types, we don't care about the factoring of the dimensions.
88+
if (!(columnType is VectorDataViewType vectorType) || !(other is VectorDataViewType otherVectorType))
89+
return false;
90+
if (!vectorType.ItemType.Equals(otherVectorType.ItemType))
91+
return false;
92+
return vectorType.Size == otherVectorType.Size;
93+
}
13894

139-
private static NumberDataViewType NumberTypeFromKind(InternalDataKind kind)
95+
public static PrimitiveDataViewType PrimitiveTypeFromType(Type type)
96+
{
97+
if (type == typeof(ReadOnlyMemory<char>) || type == typeof(string))
98+
return TextDataViewType.Instance;
99+
if (type == typeof(bool))
100+
return BooleanDataViewType.Instance;
101+
if (type == typeof(TimeSpan))
102+
return TimeSpanDataViewType.Instance;
103+
if (type == typeof(DateTime))
104+
return DateTimeDataViewType.Instance;
105+
if (type == typeof(DateTimeOffset))
106+
return DateTimeOffsetDataViewType.Instance;
107+
if (type == typeof(DataViewRowId))
108+
return RowIdDataViewType.Instance;
109+
return NumberTypeFromType(type);
110+
}
111+
112+
public static PrimitiveDataViewType PrimitiveTypeFromKind(InternalDataKind kind)
113+
{
114+
if (kind == InternalDataKind.TX)
115+
return TextDataViewType.Instance;
116+
if (kind == InternalDataKind.BL)
117+
return BooleanDataViewType.Instance;
118+
if (kind == InternalDataKind.TS)
119+
return TimeSpanDataViewType.Instance;
120+
if (kind == InternalDataKind.DT)
121+
return DateTimeDataViewType.Instance;
122+
if (kind == InternalDataKind.DZ)
123+
return DateTimeOffsetDataViewType.Instance;
124+
if (kind == InternalDataKind.UG)
125+
return RowIdDataViewType.Instance;
126+
return NumberTypeFromKind(kind);
127+
}
128+
129+
public static NumberDataViewType NumberTypeFromType(Type type)
130+
{
131+
InternalDataKind kind;
132+
if (type.TryGetDataKind(out kind))
133+
return NumberTypeFromKind(kind);
134+
135+
Contracts.Assert(false);
136+
throw new InvalidOperationException($"Bad type in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromType)}: {type}");
137+
}
138+
139+
private static NumberDataViewType NumberTypeFromKind(InternalDataKind kind)
140+
{
141+
switch (kind)
140142
{
141-
switch (kind)
142-
{
143-
case InternalDataKind.I1:
144-
return NumberDataViewType.SByte;
145-
case InternalDataKind.U1:
146-
return NumberDataViewType.Byte;
147-
case InternalDataKind.I2:
148-
return NumberDataViewType.Int16;
149-
case InternalDataKind.U2:
150-
return NumberDataViewType.UInt16;
151-
case InternalDataKind.I4:
152-
return NumberDataViewType.Int32;
153-
case InternalDataKind.U4:
154-
return NumberDataViewType.UInt32;
155-
case InternalDataKind.I8:
156-
return NumberDataViewType.Int64;
157-
case InternalDataKind.U8:
158-
return NumberDataViewType.UInt64;
159-
case InternalDataKind.R4:
160-
return NumberDataViewType.Single;
161-
case InternalDataKind.R8:
162-
return NumberDataViewType.Double;
163-
}
164-
165-
Contracts.Assert(false);
166-
throw new InvalidOperationException($"Bad data kind in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromKind)}: {kind}");
143+
case InternalDataKind.I1:
144+
return NumberDataViewType.SByte;
145+
case InternalDataKind.U1:
146+
return NumberDataViewType.Byte;
147+
case InternalDataKind.I2:
148+
return NumberDataViewType.Int16;
149+
case InternalDataKind.U2:
150+
return NumberDataViewType.UInt16;
151+
case InternalDataKind.I4:
152+
return NumberDataViewType.Int32;
153+
case InternalDataKind.U4:
154+
return NumberDataViewType.UInt32;
155+
case InternalDataKind.I8:
156+
return NumberDataViewType.Int64;
157+
case InternalDataKind.U8:
158+
return NumberDataViewType.UInt64;
159+
case InternalDataKind.R4:
160+
return NumberDataViewType.Single;
161+
case InternalDataKind.R8:
162+
return NumberDataViewType.Double;
167163
}
164+
165+
Contracts.Assert(false);
166+
throw new InvalidOperationException($"Bad data kind in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromKind)}: {kind}");
168167
}
169168
}

0 commit comments

Comments
 (0)