Skip to content

Commit 77c3467

Browse files
authored
Avoid using hidden classes defined in SampleUtils (#3563)
* Avoid hidden class * Use TT files
1 parent 3c168ce commit 77c3467

12 files changed

+498
-26
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/DataViewEnumerable.cs

+37-7
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
using System;
22
using System.Collections.Generic;
33
using Microsoft.ML;
4-
using Microsoft.ML.SamplesUtils;
54

65
namespace Samples.Dynamic
76
{
8-
/// <summary>
9-
/// Sample class showing how to use ShuffleRows.
10-
/// </summary>
117
public static class DataViewEnumerable
128
{
9+
// A simple case of creating IDataView from IEnumerable.
1310
public static void Example()
1411
{
1512
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
1613
// as a catalog of available operations and as the source of randomness.
1714
var mlContext = new MLContext();
1815

1916
// Get a small dataset as an IEnumerable.
20-
IEnumerable<DatasetUtils.SampleTemperatureData> enumerableOfData = DatasetUtils.GetSampleTemperatureData(5);
17+
IEnumerable<SampleTemperatureData> enumerableOfData = GetSampleTemperatureData(5);
2118

2219
// Load dataset into an IDataView.
2320
IDataView data = mlContext.Data.LoadFromEnumerable(enumerableOfData);
2421

2522
// We can now examine the records in the IDataView. We first create an enumerable of rows in the IDataView.
26-
var rowEnumerable = mlContext.Data.CreateEnumerable<DatasetUtils.SampleTemperatureData>(data, reuseRowObject: true);
23+
var rowEnumerable = mlContext.Data.CreateEnumerable<SampleTemperatureData>(data, reuseRowObject: true);
2724

2825
// SampleTemperatureDataWithLatitude has the definition of a Latitude column of type float.
2926
// We can use the parameter ignoreMissingColumns to true to ignore any missing columns in the IDataView.
3027
// The produced enumerable will have the Latitude field set to the default for the data type, in this case 0.
31-
var rowEnumerableIgnoreMissing = mlContext.Data.CreateEnumerable<DatasetUtils.SampleTemperatureDataWithLatitude>(data,
28+
var rowEnumerableIgnoreMissing = mlContext.Data.CreateEnumerable<SampleTemperatureDataWithLatitude>(data,
3229
reuseRowObject: true, ignoreMissingColumns: true);
3330

3431
Console.WriteLine($"Date\tTemperature");
@@ -55,5 +52,38 @@ public static void Example()
5552
// 1/5/2012 35 0
5653
// 1/6/2012 35 0
5754
}
55+
56+
private class SampleTemperatureData
57+
{
58+
public DateTime Date { get; set; }
59+
public float Temperature { get; set; }
60+
}
61+
62+
private class SampleTemperatureDataWithLatitude
63+
{
64+
public float Latitude { get; set; }
65+
public DateTime Date { get; set; }
66+
public float Temperature { get; set; }
67+
}
68+
69+
/// <summary>
70+
/// Get a fake temperature dataset.
71+
/// </summary>
72+
/// <param name="exampleCount">The number of examples to return.</param>
73+
/// <returns>An enumerable of <see cref="SampleTemperatureData"/>.</returns>
74+
private static IEnumerable<SampleTemperatureData> GetSampleTemperatureData(int exampleCount)
75+
{
76+
var rng = new Random(1234321);
77+
var date = new DateTime(2012, 1, 1);
78+
float temperature = 39.0f;
79+
80+
for (int i = 0; i < exampleCount; i++)
81+
{
82+
date = date.AddDays(1);
83+
temperature += rng.Next(-5, 5);
84+
yield return new SampleTemperatureData { Date = date, Temperature = temperature };
85+
}
86+
}
5887
}
5988
}
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<#@ include file="TemperatureAndLatitude.ttinclude"#>
2+
<#+
3+
string NameSpace = "Samples.Dynamic";
4+
string ClassName="DataViewEnumerable";
5+
string AddExtraClass = "true";
6+
string ExampleShortDoc = @"// A simple case of creating IDataView from IEnumerable.";
7+
string Example = @"// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
8+
// as a catalog of available operations and as the source of randomness.
9+
var mlContext = new MLContext();
10+
11+
// Get a small dataset as an IEnumerable.
12+
IEnumerable<SampleTemperatureData> enumerableOfData = GetSampleTemperatureData(5);
13+
14+
// Load dataset into an IDataView.
15+
IDataView data = mlContext.Data.LoadFromEnumerable(enumerableOfData);
16+
17+
// We can now examine the records in the IDataView. We first create an enumerable of rows in the IDataView.
18+
var rowEnumerable = mlContext.Data.CreateEnumerable<SampleTemperatureData>(data, reuseRowObject: true);
19+
20+
// SampleTemperatureDataWithLatitude has the definition of a Latitude column of type float.
21+
// We can use the parameter ignoreMissingColumns to true to ignore any missing columns in the IDataView.
22+
// The produced enumerable will have the Latitude field set to the default for the data type, in this case 0.
23+
var rowEnumerableIgnoreMissing = mlContext.Data.CreateEnumerable<SampleTemperatureDataWithLatitude>(data,
24+
reuseRowObject: true, ignoreMissingColumns: true);
25+
26+
Console.WriteLine($""Date\tTemperature"");
27+
foreach (var row in rowEnumerable)
28+
Console.WriteLine($""{row.Date.ToString(""d"")}\t{row.Temperature}"");
29+
30+
// Expected output:
31+
// Date Temperature
32+
// 1/2/2012 36
33+
// 1/3/2012 36
34+
// 1/4/2012 34
35+
// 1/5/2012 35
36+
// 1/6/2012 35
37+
38+
Console.WriteLine($""Date\tTemperature\tLatitude"");
39+
foreach (var row in rowEnumerableIgnoreMissing)
40+
Console.WriteLine($""{row.Date.ToString(""d"")}\t{row.Temperature}\t{row.Latitude}"");
41+
42+
// Expected output:
43+
// Date Temperature Latitude
44+
// 1/2/2012 36 0
45+
// 1/3/2012 36 0
46+
// 1/4/2012 34 0
47+
// 1/5/2012 35 0
48+
// 1/6/2012 35 0";
49+
#>

docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/FilterRowsByColumn.cs

+29-5
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44

55
namespace Samples.Dynamic
66
{
7-
/// <summary>
8-
/// Sample class showing how to use FilterRowsByColumn.
9-
/// </summary>
107
public static class FilterRowsByColumn
118
{
9+
// // Sample class showing how to filter out some rows in IDataView.
1210
public static void Example()
1311
{
1412
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
1513
// as a catalog of available operations and as the source of randomness.
1614
var mlContext = new MLContext();
1715

1816
// Get a small dataset as an IEnumerable.
19-
IEnumerable<Microsoft.ML.SamplesUtils.DatasetUtils.SampleTemperatureData> enumerableOfData = Microsoft.ML.SamplesUtils.DatasetUtils.GetSampleTemperatureData(10);
17+
var enumerableOfData = GetSampleTemperatureData(10);
2018
var data = mlContext.Data.LoadFromEnumerable(enumerableOfData);
2119

2220
// Before we apply a filter, examine all the records in the dataset.
@@ -43,7 +41,7 @@ public static void Example()
4341
var filteredData = mlContext.Data.FilterRowsByColumn(data, columnName: "Temperature", lowerBound: 34, upperBound: 37);
4442

4543
// Look at the filtered data and observe that values outside [34,37) have been dropped.
46-
var enumerable = mlContext.Data.CreateEnumerable<Microsoft.ML.SamplesUtils.DatasetUtils.SampleTemperatureData>(filteredData, reuseRowObject: true);
44+
var enumerable = mlContext.Data.CreateEnumerable<SampleTemperatureData>(filteredData, reuseRowObject: true);
4745
Console.WriteLine($"Date\tTemperature");
4846
foreach (var row in enumerable)
4947
{
@@ -59,5 +57,31 @@ public static void Example()
5957
// 1/6/2012 35
6058
// 1/9/2012 35
6159
}
60+
61+
private class SampleTemperatureData
62+
{
63+
public DateTime Date { get; set; }
64+
public float Temperature { get; set; }
65+
}
66+
67+
/// <summary>
68+
/// Get a fake temperature dataset.
69+
/// </summary>
70+
/// <param name="exampleCount">The number of examples to return.</param>
71+
/// <returns>An enumerable of <see cref="SampleTemperatureData"/>.</returns>
72+
private static IEnumerable<SampleTemperatureData> GetSampleTemperatureData(int exampleCount)
73+
{
74+
var rng = new Random(1234321);
75+
var date = new DateTime(2012, 1, 1);
76+
float temperature = 39.0f;
77+
78+
for (int i = 0; i < exampleCount; i++)
79+
{
80+
date = date.AddDays(1);
81+
temperature += rng.Next(-5, 5);
82+
yield return new SampleTemperatureData { Date = date, Temperature = temperature };
83+
}
84+
}
6285
}
6386
}
87+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<#@ include file="TemperatureAndLatitude.ttinclude"#>
2+
<#+
3+
string NameSpace = "Samples.Dynamic";
4+
string ClassName="FilterRowsByColumn";
5+
string AddExtraClass = null;
6+
string ExampleShortDoc = @"// // Sample class showing how to filter out some rows in IDataView.";
7+
string Example = @"// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
8+
// as a catalog of available operations and as the source of randomness.
9+
var mlContext = new MLContext();
10+
11+
// Get a small dataset as an IEnumerable.
12+
var enumerableOfData = GetSampleTemperatureData(10);
13+
var data = mlContext.Data.LoadFromEnumerable(enumerableOfData);
14+
15+
// Before we apply a filter, examine all the records in the dataset.
16+
Console.WriteLine($""Date\tTemperature"");
17+
foreach (var row in enumerableOfData)
18+
{
19+
Console.WriteLine($""{row.Date.ToString(""d"")}\t{row.Temperature}"");
20+
}
21+
Console.WriteLine();
22+
// Expected output:
23+
// Date Temperature
24+
// 1/2/2012 36
25+
// 1/3/2012 36
26+
// 1/4/2012 34
27+
// 1/5/2012 35
28+
// 1/6/2012 35
29+
// 1/7/2012 39
30+
// 1/8/2012 40
31+
// 1/9/2012 35
32+
// 1/10/2012 30
33+
// 1/11/2012 29
34+
35+
// Filter the data by the values of the temperature. The lower bound is inclusive, the upper exclusive.
36+
var filteredData = mlContext.Data.FilterRowsByColumn(data, columnName: ""Temperature"", lowerBound: 34, upperBound: 37);
37+
38+
// Look at the filtered data and observe that values outside [34,37) have been dropped.
39+
var enumerable = mlContext.Data.CreateEnumerable<SampleTemperatureData>(filteredData, reuseRowObject: true);
40+
Console.WriteLine($""Date\tTemperature"");
41+
foreach (var row in enumerable)
42+
{
43+
Console.WriteLine($""{row.Date.ToString(""d"")}\t{row.Temperature}"");
44+
}
45+
46+
// Expected output:
47+
// Date Temperature
48+
// 1/2/2012 36
49+
// 1/3/2012 36
50+
// 1/4/2012 34
51+
// 1/5/2012 35
52+
// 1/6/2012 35
53+
// 1/9/2012 35";#>

docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/ShuffleRows.cs

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
using System;
2+
using System.Collections.Generic;
23
using Microsoft.ML;
3-
using Microsoft.ML.SamplesUtils;
44

55
namespace Samples.Dynamic
66
{
7-
/// <summary>
8-
/// Sample class showing how to use ShuffleRows.
9-
/// </summary>
107
public static class ShuffleRows
118
{
9+
// Sample class showing how to shuffle rows in IDataView.
1210
public static void Example()
1311
{
1412
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
1513
// as a catalog of available operations and as the source of randomness.
1614
var mlContext = new MLContext();
1715

1816
// Get a small dataset as an IEnumerable.
19-
var enumerableOfData = DatasetUtils.GetSampleTemperatureData(5);
17+
var enumerableOfData = GetSampleTemperatureData(5);
2018
var data = mlContext.Data.LoadFromEnumerable(enumerableOfData);
2119

2220
// Before we apply a filter, examine all the records in the dataset.
@@ -38,7 +36,7 @@ public static void Example()
3836
var shuffledData = mlContext.Data.ShuffleRows(data, seed: 123);
3937

4038
// Look at the shuffled data and observe that the rows are in a randomized order.
41-
var enumerable = mlContext.Data.CreateEnumerable<DatasetUtils.SampleTemperatureData>(shuffledData, reuseRowObject: true);
39+
var enumerable = mlContext.Data.CreateEnumerable<SampleTemperatureData>(shuffledData, reuseRowObject: true);
4240
Console.WriteLine($"Date\tTemperature");
4341
foreach (var row in enumerable)
4442
{
@@ -52,5 +50,31 @@ public static void Example()
5250
// 1/3/2012 36
5351
// 1/6/2012 35
5452
}
53+
54+
private class SampleTemperatureData
55+
{
56+
public DateTime Date { get; set; }
57+
public float Temperature { get; set; }
58+
}
59+
60+
/// <summary>
61+
/// Get a fake temperature dataset.
62+
/// </summary>
63+
/// <param name="exampleCount">The number of examples to return.</param>
64+
/// <returns>An enumerable of <see cref="SampleTemperatureData"/>.</returns>
65+
private static IEnumerable<SampleTemperatureData> GetSampleTemperatureData(int exampleCount)
66+
{
67+
var rng = new Random(1234321);
68+
var date = new DateTime(2012, 1, 1);
69+
float temperature = 39.0f;
70+
71+
for (int i = 0; i < exampleCount; i++)
72+
{
73+
date = date.AddDays(1);
74+
temperature += rng.Next(-5, 5);
75+
yield return new SampleTemperatureData { Date = date, Temperature = temperature };
76+
}
77+
}
5578
}
5679
}
80+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<#@ include file="TemperatureAndLatitude.ttinclude"#>
2+
<#+
3+
string NameSpace = "Samples.Dynamic";
4+
string ClassName="ShuffleRows";
5+
string AddExtraClass = null;
6+
string ExampleShortDoc = @"// Sample class showing how to shuffle rows in IDataView.";
7+
string Example = @"// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
8+
// as a catalog of available operations and as the source of randomness.
9+
var mlContext = new MLContext();
10+
11+
// Get a small dataset as an IEnumerable.
12+
var enumerableOfData = GetSampleTemperatureData(5);
13+
var data = mlContext.Data.LoadFromEnumerable(enumerableOfData);
14+
15+
// Before we apply a filter, examine all the records in the dataset.
16+
Console.WriteLine($""Date\tTemperature"");
17+
foreach (var row in enumerableOfData)
18+
{
19+
Console.WriteLine($""{row.Date.ToString(""d"")}\t{row.Temperature}"");
20+
}
21+
Console.WriteLine();
22+
// Expected output:
23+
// Date Temperature
24+
// 1/2/2012 36
25+
// 1/3/2012 36
26+
// 1/4/2012 34
27+
// 1/5/2012 35
28+
// 1/6/2012 35
29+
30+
// Shuffle the dataset.
31+
var shuffledData = mlContext.Data.ShuffleRows(data, seed: 123);
32+
33+
// Look at the shuffled data and observe that the rows are in a randomized order.
34+
var enumerable = mlContext.Data.CreateEnumerable<SampleTemperatureData>(shuffledData, reuseRowObject: true);
35+
Console.WriteLine($""Date\tTemperature"");
36+
foreach (var row in enumerable)
37+
{
38+
Console.WriteLine($""{row.Date.ToString(""d"")}\t{row.Temperature}"");
39+
}
40+
// Expected output:
41+
// Date Temperature
42+
// 1/4/2012 34
43+
// 1/2/2012 36
44+
// 1/5/2012 35
45+
// 1/3/2012 36
46+
// 1/6/2012 35";#>

0 commit comments

Comments
 (0)