Skip to content

Commit a75d653

Browse files
authored
Add Param and methods to Spark.ML (#586)
1 parent 01433ca commit a75d653

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed

.ionide/symbolCache.db

28 KB
Binary file not shown.

src/csharp/Microsoft.Spark.E2ETest/IpcTests/ML/Feature/BucketizerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using Microsoft.Spark.ML.Feature;
8+
using Microsoft.Spark.ML.Feature.Param;
89
using Microsoft.Spark.Sql;
910
using Microsoft.Spark.UnitTest.TestUtils;
1011
using Xunit;
@@ -58,6 +59,19 @@ public void TestBucketizer()
5859
Bucketizer loadedBucketizer = Bucketizer.Load(savePath);
5960
Assert.Equal(bucketizer.Uid(), loadedBucketizer.Uid());
6061
}
62+
63+
Assert.NotEmpty(bucketizer.ExplainParams());
64+
65+
Param handleInvalidParam = bucketizer.GetParam("handleInvalid");
66+
Assert.NotEmpty(handleInvalidParam.Doc);
67+
Assert.NotEmpty(handleInvalidParam.Name);
68+
Assert.Equal(handleInvalidParam.Parent, bucketizer.Uid());
69+
70+
Assert.NotEmpty(bucketizer.ExplainParam(handleInvalidParam));
71+
bucketizer.Set(handleInvalidParam, "keep");
72+
Assert.Equal("keep", bucketizer.GetHandleInvalid());
73+
74+
Assert.Equal("error", bucketizer.Clear(handleInvalidParam).GetHandleInvalid());
6175
}
6276

6377
[Fact]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Spark.ML.Feature.Param;
6+
using Microsoft.Spark.Sql;
7+
using Xunit;
8+
9+
namespace Microsoft.Spark.E2ETest.IpcTests.ML.ParamTests
10+
{
11+
[Collection("Spark E2E Tests")]
12+
public class ParamTests
13+
{
14+
private readonly SparkSession _spark;
15+
16+
public ParamTests(SparkFixture fixture)
17+
{
18+
_spark = fixture.Spark;
19+
}
20+
21+
[Fact]
22+
public void Test()
23+
{
24+
const string expectedParent = "parent";
25+
const string expectedName = "name";
26+
const string expectedDoc = "doc";
27+
28+
var param = new Param(expectedParent, expectedName, expectedDoc);
29+
30+
Assert.Equal(expectedParent, param.Parent);
31+
Assert.Equal(expectedDoc, param.Doc);
32+
Assert.Equal(expectedName, param.Name);
33+
}
34+
}
35+
}

src/csharp/Microsoft.Spark/ML/Feature/FeatureBase.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,49 @@ internal FeatureBase(JvmObjectReference jvmObject)
5656
public T Save(string path) =>
5757
WrapAsType((JvmObjectReference)_jvmObject.Invoke("save", path));
5858

59-
private T WrapAsType(JvmObjectReference reference)
59+
/// <summary>
60+
/// Clears any value that was previously set for this <see cref="Param"/>. The value is
61+
/// reset to the default value.
62+
/// </summary>
63+
/// <param name="param">The <see cref="Param"/> to set back to its original value</param>
64+
/// <returns>Object reference that was used to clear the <see cref="Param"/></returns>
65+
public T Clear(Param.Param param) =>
66+
WrapAsType((JvmObjectReference)_jvmObject.Invoke("clear", param));
67+
68+
/// <summary>
69+
/// Returns a description of how a specific <see cref="Param"/> works and is currently set.
70+
/// </summary>
71+
/// <param name="param">The <see cref="Param"/> to explain</param>
72+
/// <returns>Description of the <see cref="Param"/></returns>
73+
public string ExplainParam(Param.Param param) =>
74+
(string)_jvmObject.Invoke("explainParam", param);
75+
76+
/// <summary>
77+
/// Returns a description of how all of the <see cref="Param"/>'s that apply to this object
78+
/// work and how they are currently set.
79+
/// </summary>
80+
/// <returns>Description of all the applicable <see cref="Param"/>'s</returns>
81+
public string ExplainParams() => (string)_jvmObject.Invoke("explainParams");
82+
83+
/// <summary>
84+
/// Retrieves a <see cref="Param"/> so that it can be used to set the value of the
85+
/// <see cref="Param"/> on the object.
86+
/// </summary>
87+
/// <param name="paramName">The name of the <see cref="Param"/> to get.</param>
88+
/// <returns><see cref="Param"/> that can be used to set the actual value</returns>
89+
public Param.Param GetParam(string paramName) =>
90+
new Param.Param((JvmObjectReference)_jvmObject.Invoke("getParam", paramName));
91+
92+
/// <summary>
93+
/// Sets the value of a specific <see cref="Param"/>.
94+
/// </summary>
95+
/// <param name="param"><see cref="Param"/> to set the value of</param>
96+
/// <param name="value">The value to use</param>
97+
/// <returns>The object that contains the newly set <see cref="Param"/></returns>
98+
public T Set(Param.Param param, object value) =>
99+
WrapAsType((JvmObjectReference)_jvmObject.Invoke("set", param, value));
100+
101+
private static T WrapAsType(JvmObjectReference reference)
60102
{
61103
ConstructorInfo constructor = typeof(T)
62104
.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Microsoft.Spark.Interop;
7+
using Microsoft.Spark.Interop.Ipc;
8+
9+
namespace Microsoft.Spark.ML.Feature.Param
10+
{
11+
/// <summary>
12+
/// A <see cref="Param"/> with self-contained documentation and optionally default value.
13+
///
14+
/// A <see cref="Param"/> references an individual parameter that includes documentation, the
15+
/// name of the parameter and optionally a default value. Params can either be set using the
16+
/// generic <see cref="Param"/> methods or by using explicit methods. For example
17+
/// <see cref="Bucketizer"/> has <c>SetHandleInvalid</c> or you can call
18+
/// <c>GetParam("handleInvalid")</c>and then <see cref="Bucketizer"/>. Set using the
19+
/// <see cref="Param"/> and the value you want to use.
20+
/// </summary>
21+
public class Param : IJvmObjectReferenceProvider
22+
{
23+
private static readonly string s_ParamClassName =
24+
"org.apache.spark.ml.param.Param";
25+
26+
private readonly JvmObjectReference _jvmObject;
27+
28+
/// <summary>
29+
/// Creates a new instance of a <see cref="Param"/> which will be attached to the parent
30+
/// specified. The most likely use case for a <see cref="Param"/> is being read from a
31+
/// parent object such as <see cref="Bucketizer"/> rather than independently
32+
/// <param name="parent">The parent object to assign the <see cref="Param"/> to</param>
33+
/// <param name="name">The name of this <see cref="Param"/></param>
34+
/// <param name="doc">The documentation for this <see cref="Param"/></param>
35+
/// </summary>
36+
public Param(Identifiable parent, string name, string doc)
37+
: this(SparkEnvironment.JvmBridge.CallConstructor(
38+
s_ParamClassName, parent.Uid(), name, doc))
39+
{
40+
}
41+
42+
/// <summary>
43+
/// Creates a new instance of a <see cref="Param"/> which will be attached to the parent
44+
/// with the UID specified. The most likely use case for a <see cref="Param"/> is being
45+
/// read from a parent object such as <see cref="Bucketizer"/> rather than independently
46+
/// <param name="parent">
47+
/// The UID of the parent object to assign the <see cref="Param"/> to
48+
/// </param>
49+
/// <param name="name">The name of this <see cref="Param"/></param>
50+
/// <param name="doc">The documentation for this <see cref="Param"/></param>
51+
/// </summary>
52+
public Param(string parent, string name, string doc)
53+
: this(SparkEnvironment.JvmBridge.CallConstructor(s_ParamClassName, parent, name, doc))
54+
{
55+
}
56+
57+
internal Param(JvmObjectReference jvmObject)
58+
{
59+
_jvmObject = jvmObject;
60+
}
61+
62+
JvmObjectReference IJvmObjectReferenceProvider.Reference => _jvmObject;
63+
64+
/// <summary>
65+
/// The description of what the <see cref="Param"/> does and how it works including any
66+
/// defaults and the current value
67+
/// </summary>
68+
/// <returns>A description of how the <see cref="Param"/> works</returns>
69+
public string Doc => (string)_jvmObject.Invoke("doc");
70+
71+
/// <summary>
72+
/// The name of the <see cref="Param"/>
73+
/// </summary>
74+
/// <returns>The name of the <see cref="Param"/></returns>
75+
public string Name => (string)_jvmObject.Invoke("name");
76+
77+
/// <summary>
78+
/// The object that contains the <see cref="Param"/>
79+
/// </summary>
80+
/// <returns>The UID of the parent oject that this <see cref="Param"/> belongs to</returns>
81+
public string Parent => (string)_jvmObject.Invoke("parent");
82+
}
83+
}

0 commit comments

Comments
 (0)