|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2024. Hopsworks AB |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * |
| 14 | + * See the License for the specific language governing permissions and limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.logicalclocks.hsfs.metadata; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 21 | +import lombok.AllArgsConstructor; |
| 22 | +import lombok.Getter; |
| 23 | +import lombok.NoArgsConstructor; |
| 24 | +import lombok.Setter; |
| 25 | +import org.json.JSONObject; |
| 26 | + |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 30 | +@NoArgsConstructor |
| 31 | +@AllArgsConstructor |
| 32 | +@Getter |
| 33 | +@Setter |
| 34 | +public class FeatureDescriptiveStatistics extends RestDto<FeatureDescriptiveStatistics> { |
| 35 | + private Integer id; |
| 36 | + private String featureType; |
| 37 | + private String featureName; |
| 38 | + |
| 39 | + // for any feature type |
| 40 | + private Long count; |
| 41 | + private Double completeness; |
| 42 | + private Long numNonNullValues; |
| 43 | + private Long numNullValues; |
| 44 | + private Long approxNumDistinctValues; |
| 45 | + |
| 46 | + // for numerical features |
| 47 | + private Double min; |
| 48 | + private Double max; |
| 49 | + private Double sum; |
| 50 | + private Double mean; |
| 51 | + private Double stddev; |
| 52 | + private Map<String, Double> percentiles; |
| 53 | + |
| 54 | + // with exact uniqueness |
| 55 | + private Double distinctness; |
| 56 | + private Double entropy; |
| 57 | + private Double uniqueness; |
| 58 | + private Long exactNumDistinctValues; |
| 59 | + |
| 60 | + // histogram, correlations, kll <- from hdfs file |
| 61 | + private String extendedStatistics; |
| 62 | + |
| 63 | + public static FeatureDescriptiveStatistics fromDeequStatisticsJson(JSONObject statsJson) { |
| 64 | + FeatureDescriptiveStatistics fds = new FeatureDescriptiveStatistics(); |
| 65 | + fds.setFeatureName(statsJson.getString("column")); |
| 66 | + |
| 67 | + if (statsJson.has("dataType")) { |
| 68 | + fds.setFeatureType(statsJson.getString("dataType")); |
| 69 | + } |
| 70 | + |
| 71 | + if (statsJson.has("count") && statsJson.getLong("count") == 0) { |
| 72 | + // if empty data, ignore the rest of statistics |
| 73 | + fds.setCount(0L); |
| 74 | + return fds; |
| 75 | + } |
| 76 | + |
| 77 | + // common for all data types |
| 78 | + if (statsJson.has("numRecordsNull")) { |
| 79 | + fds.setNumNullValues(statsJson.getLong("numRecordsNull")); |
| 80 | + } |
| 81 | + if (statsJson.has("numRecordsNonNull")) { |
| 82 | + fds.setNumNonNullValues(statsJson.getLong("numRecordsNonNull")); |
| 83 | + } |
| 84 | + if (statsJson.has("numRecordsNull") && statsJson.has("numRecordsNonNull")) { |
| 85 | + fds.setCount(Long.valueOf(statsJson.getInt("numRecordsNull") + statsJson.getInt("numRecordsNonNull"))); |
| 86 | + } |
| 87 | + if (statsJson.has("count")) { |
| 88 | + fds.setCount(statsJson.getLong("count")); |
| 89 | + } |
| 90 | + if (statsJson.has("completeness")) { |
| 91 | + fds.setCompleteness(statsJson.getDouble("completeness")); |
| 92 | + } |
| 93 | + if (statsJson.has("approximateNumDistinctValues")) { |
| 94 | + fds.setApproxNumDistinctValues(statsJson.getLong("approximateNumDistinctValues")); |
| 95 | + } |
| 96 | + |
| 97 | + // commmon for all data types if exact_uniqueness is enabled |
| 98 | + if (statsJson.has("uniqueness")) { |
| 99 | + fds.setUniqueness(statsJson.getDouble("uniqueness")); |
| 100 | + } |
| 101 | + if (statsJson.has("entropy")) { |
| 102 | + fds.setEntropy(statsJson.getDouble("entropy")); |
| 103 | + } |
| 104 | + if (statsJson.has("distinctness")) { |
| 105 | + fds.setDistinctness(statsJson.getDouble("distinctness")); |
| 106 | + } |
| 107 | + if (statsJson.has("exactNumDistinctValues")) { |
| 108 | + fds.setExactNumDistinctValues(statsJson.getLong("exactNumDistinctValues")); |
| 109 | + } |
| 110 | + |
| 111 | + // fractional / integral features |
| 112 | + if (statsJson.has("minimum")) { |
| 113 | + fds.setMin(statsJson.getDouble("minimum")); |
| 114 | + } |
| 115 | + if (statsJson.has("maximum")) { |
| 116 | + fds.setMax(statsJson.getDouble("maximum")); |
| 117 | + } |
| 118 | + if (statsJson.has("sum")) { |
| 119 | + fds.setSum(statsJson.getDouble("sum")); |
| 120 | + } |
| 121 | + if (statsJson.has("mean")) { |
| 122 | + fds.setMean(statsJson.getDouble("mean")); |
| 123 | + } |
| 124 | + if (statsJson.has("stdDev")) { |
| 125 | + fds.setStddev(statsJson.getDouble("stdDev")); |
| 126 | + } |
| 127 | + |
| 128 | + JSONObject extendedStatistics = new JSONObject(); |
| 129 | + if (statsJson.has("correlations")) { |
| 130 | + extendedStatistics.put("correlations", statsJson.getJSONArray("correlations")); |
| 131 | + } |
| 132 | + if (statsJson.has("histogram")) { |
| 133 | + extendedStatistics.put("histogram", statsJson.getJSONArray("histogram")); |
| 134 | + } |
| 135 | + if (statsJson.has("kll")) { |
| 136 | + extendedStatistics.put("kll", statsJson.get("kll")); |
| 137 | + } |
| 138 | + if (statsJson.has("unique_values")) { |
| 139 | + extendedStatistics.put("unique_values", statsJson.getJSONArray("unique_values")); |
| 140 | + } |
| 141 | + if (extendedStatistics.length() > 0) { |
| 142 | + fds.setExtendedStatistics(extendedStatistics.toString()); |
| 143 | + } |
| 144 | + |
| 145 | + return fds; |
| 146 | + } |
| 147 | +} |
0 commit comments