Skip to content

Commit

Permalink
fix: Fix crash caused by values being overwritten with NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrimpCryptid committed Oct 29, 2024
1 parent 34a9479 commit 8f68f43
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/state/image-dataset/csv-dataset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CsvRequest implements ImageDataset {
cellIdToData: Record<string, CsvData>;
featureKeys: string[];
featureDefs: Map<string, MeasuredFeatureDef>;
featureData: Record<string, number[]>;
featureData: Record<string, (number | null)[]>;

constructor() {
// CSV parsing library?
Expand Down Expand Up @@ -126,7 +126,7 @@ class CsvRequest implements ImageDataset {
private parseDiscreteFeature(
key: string,
data: string[]
): { def: DiscreteMeasuredFeatureDef; data: number[] } {
): { def: DiscreteMeasuredFeatureDef; data: (number | null)[] } {
// Treat as discrete feature, create options objects
const options: Record<string, MeasuredFeaturesOption> = {};
const uniqueValuesSet = new Set<string>(data as string[]);
Expand All @@ -145,7 +145,7 @@ class CsvRequest implements ImageDataset {
};
}

const mappedData = data.map((val) => valueNameToIndex.get(val) || -1);
const mappedData = data.map((val) => valueNameToIndex.get(val) ?? null);

return {
def: {
Expand All @@ -164,7 +164,7 @@ class CsvRequest implements ImageDataset {
const featureKeys = this.getNonReservedFeatureColumns();
const featureDefs: Map<string, MeasuredFeatureDef> = new Map();
const rawFeatureData = this.getFeatureDataAsColumns(rawCsvData, featureKeys);
const newFeatureData: Record<string, number[]> = {};
const newFeatureData: Record<string, (number | null)[]> = {};

for (const key of featureKeys) {
const data = rawFeatureData.get(key);
Expand Down Expand Up @@ -269,23 +269,15 @@ class CsvRequest implements ImageDataset {
getFeatureData(): Promise<DataForPlot | void> {
const indices = this.rawCsvData.map((row) => Number.parseInt(row[CELL_ID_KEY]));

const values: Record<string, number[]> = this.featureData;
const values: Record<string, (number | null)[]> = this.featureData;
const labels: PerCellLabels = {
thumbnailPaths: [],
cellIds: [],
};

for (let i = 0; i < indices.length; i++) {
// TODO: Calculate in advance
const row = this.rawCsvData[i];

// Copy feature values
for (const key of this.featureKeys) {
if (!values[key]) {
values[key] = [];
}
values[key].push(Number.parseFloat(row[key]));
}

// Copy label data
labels.cellIds.push(row[CELL_ID_KEY]);
labels.thumbnailPaths.push(row[THUMBNAIL_PATH] || "");
Expand Down

0 comments on commit 8f68f43

Please sign in to comment.