Skip to content

Displaying the records that have been loaded using TextLoader #2466

Closed
@sdg002

Description

@sdg002

System information

  • OS version/distro: Windows 10
  • .NET Version (eg., dotnet --info): .NET 4.5

Issue

I was trying to get a better understanding of loading records from a flat file. All I wanted to do was to access the records that have been loaded sequentially and display them.

  • What did you do?
  • What happened?
  • What did you expect?
    The code in the while block below is my attempt. This worked well. However, I am wondering if this is the right way to iterate over a cursor? Is there anything simpler to get the individual column values? Feels a bit onerous.

Source code / logs

    [TestMethod]
    public void TestMethod1()
    {
        string datafile = @"Data\Dummy2.csv";
        string pathFull = System.IO.Path.Combine(Util.GetProjectDir2(), datafile);
        var argsLoader = new Microsoft.ML.Data.TextLoader.Arguments();
        try
        {
            argsLoader.HasHeader = true;
            argsLoader.Separators = new char[] { '|' };
            argsLoader.Column = new Microsoft.ML.Data.TextLoader.Column[]
            {
                new Microsoft.ML.Data.TextLoader.Column("id", Microsoft.ML.Data.DataKind.I4,0),
            new Microsoft.ML.Data.TextLoader.Column("ht", Microsoft.ML.Data.DataKind.R4,1),
            new Microsoft.ML.Data.TextLoader.Column("wt", Microsoft.ML.Data.DataKind.R4,2),
            new Microsoft.ML.Data.TextLoader.Column("overwt", Microsoft.ML.Data.DataKind.Bool,3)
            };
            var mlContext = new Microsoft.ML.MLContext();
            var loader = mlContext.Data.CreateTextLoader(argsLoader);
            Microsoft.Data.DataView.IDataView dataView = loader.Read(pathFull);
            var schema = dataView.Schema;
            Microsoft.Data.DataView.RowCursor cur = dataView.GetRowCursor(schema);
            while (cur.MoveNext())
            {
                System.Diagnostics.Trace.WriteLine($"got a row, position={cur.Position}");
                ///
                /// Column 0
                ///
                Microsoft.Data.DataView.ValueGetter<int> getter = cur.GetGetter<int>(0);
                int id = 0;
                getter.Invoke(ref id);
                ///
                /// Column 1
                ///
                Microsoft.Data.DataView.ValueGetter<float> getterWt = cur.GetGetter<float>(1);
                float wt = 0;
                getterWt.Invoke(ref wt);
                ///
                /// Column 3
                ///
                Microsoft.Data.DataView.ValueGetter<bool> getterIsOverWt = cur.GetGetter<bool>(3);
                bool isOverWt = false;
                getterIsOverWt.Invoke(ref isOverWt);

                System.Diagnostics.Trace.WriteLine($"id={id}    wt={wt}    isOverWt={isOverWt}");
            }

        }
        catch (Exception ex)
        {
            System.Diagnostics.Trace.WriteLine(ex.ToString());
        }
    }

/*
id|wt|ht|overwt
01|30.0|4.0|False
02|35.0|4.5|False
03|40.0|5.0|False
10|33.0|4.0|True
11|38.0|4.5|True
12|43.0|5.0|True

*/

Please paste or attach the code or logs or traces that would be helpful to diagnose the issue you are reporting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions