|
5 | 5 | using System;
|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.IO;
|
| 8 | +using System.Linq; |
8 | 9 | using Microsoft.ML.Data;
|
9 | 10 | using Microsoft.ML.Model;
|
10 | 11 | using Microsoft.ML.RunTests;
|
@@ -795,5 +796,97 @@ public void TestTextLoaderKeyTypeBackCompat()
|
795 | 796 | Assert.True(result.Schema[featureIdx].Type is KeyDataViewType keyType && keyType.Count == typeof(uint).ToMaxInt());
|
796 | 797 | }
|
797 | 798 | }
|
| 799 | + |
| 800 | + private class IrisNoFields |
| 801 | + { |
| 802 | + } |
| 803 | + |
| 804 | + private class IrisPrivateFields |
| 805 | + { |
| 806 | + [LoadColumn(0)] |
| 807 | + private float SepalLength; |
| 808 | + |
| 809 | + [LoadColumn(1)] |
| 810 | + private float SepalWidth { get; } |
| 811 | + |
| 812 | + public float GetSepalLenght() |
| 813 | + => SepalLength; |
| 814 | + |
| 815 | + public void SetSepalLength(float sepalLength) |
| 816 | + { |
| 817 | + SepalLength = sepalLength; |
| 818 | + } |
| 819 | + } |
| 820 | + private class IrisPublicGetProperties |
| 821 | + { |
| 822 | + [LoadColumn(0)] |
| 823 | + public float SepalLength { get; } |
| 824 | + |
| 825 | + [LoadColumn(1)] |
| 826 | + public float SepalWidth { get; } |
| 827 | + } |
| 828 | + |
| 829 | + private class IrisPublicFields |
| 830 | + { |
| 831 | + public IrisPublicFields(float sepalLength, float sepalWidth) |
| 832 | + { |
| 833 | + SepalLength = sepalLength; |
| 834 | + SepalWidth = sepalWidth; |
| 835 | + } |
| 836 | + |
| 837 | + [LoadColumn(0)] |
| 838 | + public readonly float SepalLength; |
| 839 | + |
| 840 | + [LoadColumn(1)] |
| 841 | + public float SepalWidth; |
| 842 | + } |
| 843 | + |
| 844 | + private class IrisPublicProperties |
| 845 | + { |
| 846 | + [LoadColumn(0)] |
| 847 | + public float SepalLength { get; set; } |
| 848 | + |
| 849 | + [LoadColumn(1)] |
| 850 | + public float SepalWidth { get; set; } |
| 851 | + } |
| 852 | + |
| 853 | + [Fact] |
| 854 | + public void TestTextLoaderNoFields() |
| 855 | + { |
| 856 | + var dataPath = GetDataPath(TestDatasets.irisData.trainFilename); |
| 857 | + var mlContext = new MLContext(); |
| 858 | + |
| 859 | + // Class with get property only. |
| 860 | + var dataIris = mlContext.Data.CreateTextLoader<IrisPublicGetProperties>(separatorChar: ',').Load(dataPath); |
| 861 | + var oneIrisData = mlContext.Data.CreateEnumerable<IrisPublicProperties>(dataIris, false).First(); |
| 862 | + Assert.True(oneIrisData.SepalLength != 0 && oneIrisData.SepalWidth != 0); |
| 863 | + |
| 864 | + // Class with read only fields. |
| 865 | + dataIris = mlContext.Data.CreateTextLoader<IrisPublicFields>(separatorChar: ',').Load(dataPath); |
| 866 | + oneIrisData = mlContext.Data.CreateEnumerable<IrisPublicProperties>(dataIris, false).First(); |
| 867 | + Assert.True(oneIrisData.SepalLength != 0 && oneIrisData.SepalWidth != 0); |
| 868 | + |
| 869 | + // Class with no fields. |
| 870 | + try |
| 871 | + { |
| 872 | + dataIris = mlContext.Data.CreateTextLoader<IrisNoFields>(separatorChar: ',').Load(dataPath); |
| 873 | + Assert.False(true); |
| 874 | + } |
| 875 | + catch (Exception ex) |
| 876 | + { |
| 877 | + Assert.StartsWith("Should define at least one public, readable field or property in TInput.", ex.Message); |
| 878 | + } |
| 879 | + |
| 880 | + // Class with no public readable fields. |
| 881 | + try |
| 882 | + { |
| 883 | + dataIris = mlContext.Data.CreateTextLoader<IrisPrivateFields>(separatorChar: ',').Load(dataPath); |
| 884 | + Assert.False(true); |
| 885 | + } |
| 886 | + catch (Exception ex) |
| 887 | + { |
| 888 | + Assert.StartsWith("Should define at least one public, readable field or property in TInput.", ex.Message); |
| 889 | + } |
| 890 | + } |
798 | 891 | }
|
799 | 892 | }
|
0 commit comments