Source:
<code>@Override public Map<String, Map<String, Integer>> computeLevelCounts(String[] columnNames) throws DDFException { if (columnNames.length > 0) { List<Integer> columnIndexes = new ArrayList<Integer>(); List<Schema.ColumnType> columnTypes = new ArrayList<Schema.ColumnType>(); for (String columnName : columnNames) { this.setAsFactor(columnName); columnIndexes.add(this.getColumnIndex(columnName)); columnTypes.add(this.getColumn(columnName).getType()); } Map<Integer, Map<String, Integer>> listLevelCounts; IHandleRepresentations repHandler = this.getDDF().getRepresentationHandler(); try { if (repHandler.has(RDD.class, Object[].class)) { RDD<Object[]> rdd = ((SparkDDF) this.getDDF()).getRDD(Object[].class); listLevelCounts = GetMultiFactor.getFactorCounts(rdd, columnIndexes, columnTypes, Object[].class); } else { RDD<Object[]> rdd = ((SparkDDF) this.getDDF()).getRDD(Object[].class); if (rdd == null) { throw new DDFException("RDD is null"); } listLevelCounts = GetMultiFactor.getFactorCounts(rdd, columnIndexes, columnTypes, Object[].class); } } catch (DDFException e) { throw new DDFException("Error getting factor level counts", e); } if (listLevelCounts == null) { throw new DDFException("Error getting factor levels counts"); } Map<String, Map<String, Integer>> listLevelCountsWithName = new HashMap<String, Map<String, Integer>>(); for (Integer columnIndex : columnIndexes) { String colName = this.getDDF().getColumnName(columnIndex); <START> if(listLevelCounts.get(columnIndex) != null) { <END> listLevelCountsWithName.put(colName, listLevelCounts.get(columnIndex)); } else { listLevelCountsWithName.put(colName, new HashMap<String, Integer>()); } } return listLevelCountsWithName; } else { return new HashMap<String, Map<String, Integer>>(); } }</code><technical_language>is this check ddf is cached, factors available</technical_language>

Target:
@Override public Map<String, Map<String, Integer>> computeLevelCounts(String[] columnNames) throws DDFException { if (columnNames.length > 0) { List<Integer> columnIndexes = new ArrayList<Integer>(); List<Schema.ColumnType> columnTypes = new ArrayList<Schema.ColumnType>(); for (String columnName : columnNames) { this.setAsFactor(columnName); columnIndexes.add(this.getColumnIndex(columnName)); columnTypes.add(this.getColumn(columnName).getType()); } IHandleRepresentations repHandler = this.getDDF().getRepresentationHandler(); DataFrame df = (DataFrame) repHandler.get(DataFrame.class); Map<String, Map<String, Integer>> listLevelCountsWithName = new HashMap<String, Map<String, Integer>>(); if(df != null) { for(String colName: columnNames) { DataFrame groupedDF = df.groupBy(colName).count(); DataFrame groupedDF1 = groupedDF. withColumn(colName, groupedDF.col(colName).cast("string")). withColumn("count", groupedDF.col("count").cast("integer")); if(groupedDF.count() > Factor.getMaxLevelCounts()) { throw new DDFException(String.format("Number of unique values for column %s is larger than %s", colName, Factor.getMaxLevelCounts())); } Row[] rows = groupedDF1.collect(); Map<String, Integer> valueCounts = new HashMap<String, Integer>(); for(Row row: rows) { if(row.getAs(colName) != null) { String value = (String) row.getAs(colName); int count = (int) row.getAs("count"); valueCounts.put(value, count); } } listLevelCountsWithName.put(colName, valueCounts); } } else { throw new DDFException("No Spark DataFrame in DDF"); } return listLevelCountsWithName; } else { return new HashMap<String, Map<String, Integer>>(); } }