Skip to content

Commit be1fea4

Browse files
Copilotmaurever
andcommitted
Refactor: Extract validation logic to shared method
Co-authored-by: maurever <11465784+maurever@users.noreply.github.com>
1 parent 633d62e commit be1fea4

3 files changed

Lines changed: 43 additions & 56 deletions

File tree

h2o-algos/src/main/java/hex/tree/gbm/GBMModel.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -315,34 +315,8 @@ public TwoDimTable[][] getFeatureInteractionsTable(int maxInteractionDepth, int
315315

316316
@Override
317317
public double getFriedmanPopescusH(Frame frame, String[] vars) {
318-
// Validate vars parameter
319-
if (vars == null || vars.length == 0) {
320-
throw new IllegalArgumentException(
321-
"Calculating H statistics error: 'vars' parameter cannot be null or empty. " +
322-
"Please specify at least one variable.");
323-
}
324-
325-
// Validate that all specified columns exist in the frame and are numeric
326-
for (String varName : vars) {
327-
if (varName == null) {
328-
throw new IllegalArgumentException(
329-
"Calculating H statistics error: 'vars' parameter contains a null value.");
330-
}
331-
Vec col = frame.vec(varName);
332-
if (col == null) {
333-
throw new IllegalArgumentException(
334-
"Calculating H statistics error: column '" + varName + "' does not exist in the frame.");
335-
}
336-
if (!col.isNumeric()) {
337-
throw new IllegalArgumentException(
338-
"Calculating H statistics error: column '" + varName + "' is not numeric. " +
339-
"H statistics can only be calculated for numeric variables.");
340-
}
341-
if (col.isBad() || col.isConst()) {
342-
throw new IllegalArgumentException(
343-
"Calculating H statistics error: column '" + varName + "' contains only missing or constant values.");
344-
}
345-
}
318+
// Validate input parameters
319+
validateFriedmanPopescusHInput(frame, vars);
346320

347321
Frame adaptFrm = removeSpecialNNonNumericColumns(frame);
348322

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
package hex;
22

33
import water.fvec.Frame;
4+
import water.fvec.Vec;
45

56
/**
67
* Implementors of this interface have Friedman & Popescu's H calculation implemented.
78
*/
89
public interface FriedmanPopescusHCollector {
910

1011
double getFriedmanPopescusH(Frame frame, String[] vars);
12+
13+
/**
14+
* Validates the input parameters for Friedman Popescu's H statistic calculation.
15+
*
16+
* @param frame The input frame
17+
* @param vars The variables to calculate H statistic for
18+
* @throws IllegalArgumentException if validation fails
19+
*/
20+
default void validateFriedmanPopescusHInput(Frame frame, String[] vars) {
21+
// Validate vars parameter
22+
if (vars == null || vars.length == 0) {
23+
throw new IllegalArgumentException(
24+
"Calculating H statistics error: 'vars' parameter cannot be null or empty. " +
25+
"Please specify at least one variable.");
26+
}
27+
28+
// Validate that all specified columns exist in the frame and are numeric
29+
for (String varName : vars) {
30+
if (varName == null) {
31+
throw new IllegalArgumentException(
32+
"Calculating H statistics error: 'vars' parameter contains a null value.");
33+
}
34+
Vec col = frame.vec(varName);
35+
if (col == null) {
36+
throw new IllegalArgumentException(
37+
"Calculating H statistics error: column '" + varName + "' does not exist in the frame.");
38+
}
39+
if (!col.isNumeric()) {
40+
throw new IllegalArgumentException(
41+
"Calculating H statistics error: column '" + varName + "' is not numeric. " +
42+
"H statistics can only be calculated for numeric variables.");
43+
}
44+
if (col.isBad() || col.isConst()) {
45+
throw new IllegalArgumentException(
46+
"Calculating H statistics error: column '" + varName + "' contains only missing or constant values.");
47+
}
48+
}
49+
}
1150
}

h2o-extensions/xgboost/src/main/java/hex/tree/xgboost/XGBoostModel.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,34 +1036,8 @@ protected Frame removeSpecialNNonNumericColumns(Frame frame) {
10361036

10371037
@Override
10381038
public double getFriedmanPopescusH(Frame frame, String[] vars) {
1039-
// Validate vars parameter
1040-
if (vars == null || vars.length == 0) {
1041-
throw new IllegalArgumentException(
1042-
"Calculating H statistics error: 'vars' parameter cannot be null or empty. " +
1043-
"Please specify at least one variable.");
1044-
}
1045-
1046-
// Validate that all specified columns exist in the frame and are numeric
1047-
for (String varName : vars) {
1048-
if (varName == null) {
1049-
throw new IllegalArgumentException(
1050-
"Calculating H statistics error: 'vars' parameter contains a null value.");
1051-
}
1052-
Vec col = frame.vec(varName);
1053-
if (col == null) {
1054-
throw new IllegalArgumentException(
1055-
"Calculating H statistics error: column '" + varName + "' does not exist in the frame.");
1056-
}
1057-
if (!col.isNumeric()) {
1058-
throw new IllegalArgumentException(
1059-
"Calculating H statistics error: column '" + varName + "' is not numeric. " +
1060-
"H statistics can only be calculated for numeric variables.");
1061-
}
1062-
if (col.isBad() || col.isConst()) {
1063-
throw new IllegalArgumentException(
1064-
"Calculating H statistics error: column '" + varName + "' contains only missing or constant values.");
1065-
}
1066-
}
1039+
// Validate input parameters
1040+
validateFriedmanPopescusHInput(frame, vars);
10671041

10681042
Frame adaptFrm = removeSpecialNNonNumericColumns(frame);
10691043

0 commit comments

Comments
 (0)