Skip to content

Commit 1dcc17e

Browse files
committed
update code gen and make param appear in the doc
1 parent f66ba0c commit 1dcc17e

File tree

3 files changed

+132
-21
lines changed

3 files changed

+132
-21
lines changed

python/pyspark/ml/param/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __str__(self):
3939
return str(self.parent) + "_" + self.name
4040

4141
def __repr__(self):
42-
return str(self.parent) + "_" + self.name
42+
return "Param(parent=%r, name=%r, doc=%r, defaultValue=%r)" % \
43+
(self.parent, self.name, self.doc, self.defaultValue)
4344

4445

4546
class Params(Identifiable):
@@ -69,3 +70,12 @@ def _merge_params(self, params):
6970
paramMap = self.paramMap.copy()
7071
paramMap.update(params)
7172
return paramMap
73+
74+
@staticmethod
75+
def _dummy():
76+
"""
77+
Returns a dummy Params instance used as a placeholder to generate docs.
78+
"""
79+
dummy = Params()
80+
dummy.uid = "undefined"
81+
return dummy

python/pyspark/ml/param/_gen_shared_params.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,42 @@ def _gen_param_code(name, doc, defaultValue):
4242
:param defaultValue: string representation of the param
4343
:return: code string
4444
"""
45-
upperCamelName = name[0].upper() + name[1:]
46-
return """class Has%s(Params):
45+
# TODO: How to correctly inherit instance attributes?
46+
template = '''class Has$Name(Params):
47+
"""
48+
Params with $name.
49+
"""
50+
51+
# a placeholder to make it appear in the generated doc
52+
$name = Param(Params._dummy(), "$name", "$doc", $defaultValue)
4753
4854
def __init__(self):
49-
super(Has%s, self).__init__()
50-
#: %s
51-
self.%s = Param(self, "%s", "%s", %s)
55+
super(Has$Name, self).__init__()
56+
#: param for $doc
57+
self.$name = Param(self, "$name", "$doc", $defaultValue)
5258
53-
def set%s(self, value):
54-
self.paramMap[self.%s] = value
59+
def set$Name(self, value):
60+
"""
61+
Sets the value of :py:attr:`$name`.
62+
"""
63+
self.paramMap[self.$name] = value
5564
return self
5665
57-
def get%s(self):
58-
if self.%s in self.paramMap:
59-
return self.paramMap[self.%s]
66+
def get$Name(self):
67+
"""
68+
Gets the value of $name or its default value.
69+
"""
70+
if self.$name in self.paramMap:
71+
return self.paramMap[self.$name]
6072
else:
61-
return self.%s.defaultValue""" % (
62-
upperCamelName, upperCamelName, doc, name, name, doc, defaultValue, upperCamelName, name,
63-
upperCamelName, name, name, name)
73+
return self.$name.defaultValue'''
74+
75+
upperCamelName = name[0].upper() + name[1:]
76+
return template \
77+
.replace("$name", name) \
78+
.replace("$Name", upperCamelName) \
79+
.replace("$doc", doc) \
80+
.replace("$defaultValue", defaultValue)
6481

6582
if __name__ == "__main__":
6683
print header

python/pyspark/ml/param/shared.py

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,125 +21,209 @@
2121

2222

2323
class HasMaxIter(Params):
24+
"""
25+
Params with maxIter.
26+
"""
27+
28+
# a placeholder to make it appear in the generated doc
29+
maxIter = Param(Params._dummy(), "maxIter", "max number of iterations", 100)
2430

2531
def __init__(self):
2632
super(HasMaxIter, self).__init__()
27-
#: max number of iterations
33+
#: param for max number of iterations
2834
self.maxIter = Param(self, "maxIter", "max number of iterations", 100)
2935

3036
def setMaxIter(self, value):
37+
"""
38+
Sets the value of :py:attr:`maxIter`.
39+
"""
3140
self.paramMap[self.maxIter] = value
3241
return self
3342

3443
def getMaxIter(self):
44+
"""
45+
Gets the value of maxIter or its default value.
46+
"""
3547
if self.maxIter in self.paramMap:
3648
return self.paramMap[self.maxIter]
3749
else:
3850
return self.maxIter.defaultValue
3951

4052

4153
class HasRegParam(Params):
54+
"""
55+
Params with regParam.
56+
"""
57+
58+
# a placeholder to make it appear in the generated doc
59+
regParam = Param(Params._dummy(), "regParam", "regularization constant", 0.1)
4260

4361
def __init__(self):
4462
super(HasRegParam, self).__init__()
45-
#: regularization constant
63+
#: param for regularization constant
4664
self.regParam = Param(self, "regParam", "regularization constant", 0.1)
4765

4866
def setRegParam(self, value):
67+
"""
68+
Sets the value of :py:attr:`regParam`.
69+
"""
4970
self.paramMap[self.regParam] = value
5071
return self
5172

5273
def getRegParam(self):
74+
"""
75+
Gets the value of regParam or its default value.
76+
"""
5377
if self.regParam in self.paramMap:
5478
return self.paramMap[self.regParam]
5579
else:
5680
return self.regParam.defaultValue
5781

5882

5983
class HasFeaturesCol(Params):
84+
"""
85+
Params with featuresCol.
86+
"""
87+
88+
# a placeholder to make it appear in the generated doc
89+
featuresCol = Param(Params._dummy(), "featuresCol", "features column name", 'features')
6090

6191
def __init__(self):
6292
super(HasFeaturesCol, self).__init__()
63-
#: features column name
93+
#: param for features column name
6494
self.featuresCol = Param(self, "featuresCol", "features column name", 'features')
6595

6696
def setFeaturesCol(self, value):
97+
"""
98+
Sets the value of :py:attr:`featuresCol`.
99+
"""
67100
self.paramMap[self.featuresCol] = value
68101
return self
69102

70103
def getFeaturesCol(self):
104+
"""
105+
Gets the value of featuresCol or its default value.
106+
"""
71107
if self.featuresCol in self.paramMap:
72108
return self.paramMap[self.featuresCol]
73109
else:
74110
return self.featuresCol.defaultValue
75111

76112

77113
class HasLabelCol(Params):
114+
"""
115+
Params with labelCol.
116+
"""
117+
118+
# a placeholder to make it appear in the generated doc
119+
labelCol = Param(Params._dummy(), "labelCol", "label column name", 'label')
78120

79121
def __init__(self):
80122
super(HasLabelCol, self).__init__()
81-
#: label column name
123+
#: param for label column name
82124
self.labelCol = Param(self, "labelCol", "label column name", 'label')
83125

84126
def setLabelCol(self, value):
127+
"""
128+
Sets the value of :py:attr:`labelCol`.
129+
"""
85130
self.paramMap[self.labelCol] = value
86131
return self
87132

88133
def getLabelCol(self):
134+
"""
135+
Gets the value of labelCol or its default value.
136+
"""
89137
if self.labelCol in self.paramMap:
90138
return self.paramMap[self.labelCol]
91139
else:
92140
return self.labelCol.defaultValue
93141

94142

95143
class HasPredictionCol(Params):
144+
"""
145+
Params with predictionCol.
146+
"""
147+
148+
# a placeholder to make it appear in the generated doc
149+
predictionCol = Param(Params._dummy(), "predictionCol", "prediction column name", 'prediction')
96150

97151
def __init__(self):
98152
super(HasPredictionCol, self).__init__()
99-
#: prediction column name
153+
#: param for prediction column name
100154
self.predictionCol = Param(self, "predictionCol", "prediction column name", 'prediction')
101155

102156
def setPredictionCol(self, value):
157+
"""
158+
Sets the value of :py:attr:`predictionCol`.
159+
"""
103160
self.paramMap[self.predictionCol] = value
104161
return self
105162

106163
def getPredictionCol(self):
164+
"""
165+
Gets the value of predictionCol or its default value.
166+
"""
107167
if self.predictionCol in self.paramMap:
108168
return self.paramMap[self.predictionCol]
109169
else:
110170
return self.predictionCol.defaultValue
111171

112172

113173
class HasInputCol(Params):
174+
"""
175+
Params with inputCol.
176+
"""
177+
178+
# a placeholder to make it appear in the generated doc
179+
inputCol = Param(Params._dummy(), "inputCol", "input column name", 'input')
114180

115181
def __init__(self):
116182
super(HasInputCol, self).__init__()
117-
#: input column name
183+
#: param for input column name
118184
self.inputCol = Param(self, "inputCol", "input column name", 'input')
119185

120186
def setInputCol(self, value):
187+
"""
188+
Sets the value of :py:attr:`inputCol`.
189+
"""
121190
self.paramMap[self.inputCol] = value
122191
return self
123192

124193
def getInputCol(self):
194+
"""
195+
Gets the value of inputCol or its default value.
196+
"""
125197
if self.inputCol in self.paramMap:
126198
return self.paramMap[self.inputCol]
127199
else:
128200
return self.inputCol.defaultValue
129201

130202

131203
class HasOutputCol(Params):
204+
"""
205+
Params with outputCol.
206+
"""
207+
208+
# a placeholder to make it appear in the generated doc
209+
outputCol = Param(Params._dummy(), "outputCol", "output column name", 'output')
132210

133211
def __init__(self):
134212
super(HasOutputCol, self).__init__()
135-
#: output column name
213+
#: param for output column name
136214
self.outputCol = Param(self, "outputCol", "output column name", 'output')
137215

138216
def setOutputCol(self, value):
217+
"""
218+
Sets the value of :py:attr:`outputCol`.
219+
"""
139220
self.paramMap[self.outputCol] = value
140221
return self
141222

142223
def getOutputCol(self):
224+
"""
225+
Gets the value of outputCol or its default value.
226+
"""
143227
if self.outputCol in self.paramMap:
144228
return self.paramMap[self.outputCol]
145229
else:

0 commit comments

Comments
 (0)