Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/UIComponents/ColumnDetailsCategorical.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {connect} from 'react-redux';
import {RootState} from '../redux';
import {colors, styles} from '../constants';
import {Bar} from 'react-chartjs-2';
import {getLocalizedValue} from '../helpers/valueDetails';
import {getCategoricalColumnDetails} from '../selectors/currentColumnSelectors';
import I18n from '../i18n';
import {CategoricalColumnDetails} from '../types';

interface ColumnDetailsCategoricalProps {
columnDetails: CategoricalColumnDetails;
datasetId: string;
}

const chartOptions = {
Expand All @@ -27,11 +29,15 @@ const chartOptions = {

const ColumnDetailsCategorical = ({
columnDetails,
datasetId,
}: ColumnDetailsCategoricalProps) => {
const {id, uniqueOptions, frequencies} = columnDetails;
const labels = uniqueOptions && Object.values(uniqueOptions);
const localizedLabels = labels.map(option =>
getLocalizedValue(option, datasetId),
);
const barData = {
labels,
localizedLabels,
datasets: [
{
label: id,
Expand Down Expand Up @@ -70,6 +76,7 @@ const ColumnDetailsCategorical = ({
export default connect(
(state: RootState) => ({
columnDetails: getCategoricalColumnDetails(state),
datasetId: state.metadata?.name || 'unknown',
}),
{},
)(ColumnDetailsCategorical);
9 changes: 6 additions & 3 deletions src/UIComponents/CrossTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {RootState} from '../redux';
import {getCrossTabData} from '../selectors/visualizationSelectors';
import {styles} from '../constants';
import ScrollableContent from './ScrollableContent';
import {getLocalizedValue} from '../helpers/valueDetails';
import I18n from '../i18n';
import {CrossTabData} from '../types';

interface CrossTabProps {
crossTabData: CrossTabData | null;
datasetId: string;
}

const CrossTab = ({crossTabData}: CrossTabProps) => {
const CrossTab = ({crossTabData, datasetId}: CrossTabProps) => {
const getCellStyle = useCallback((percent: number) => {
return {
...(styles as Record<string, React.CSSProperties>)[
Expand Down Expand Up @@ -91,7 +93,7 @@ const CrossTab = ({crossTabData}: CrossTabProps) => {
(uniqueLabelValue, index) => {
return (
<td key={index} style={styles.tableCell}>
{uniqueLabelValue}
{getLocalizedValue(uniqueLabelValue, datasetId)}
</td>
);
},
Expand All @@ -104,7 +106,7 @@ const CrossTab = ({crossTabData}: CrossTabProps) => {
(featureValue, featureIndex) => {
return (
<td key={featureIndex} style={styles.tableCell}>
{featureValue}
{getLocalizedValue(featureValue, datasetId)}
</td>
);
},
Expand Down Expand Up @@ -137,4 +139,5 @@ const CrossTab = ({crossTabData}: CrossTabProps) => {

export default connect((state: RootState) => ({
crossTabData: getCrossTabData(state),
datasetId: state.metadata?.name || 'unknown',
}))(CrossTab);
13 changes: 7 additions & 6 deletions src/UIComponents/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
import {Dispatch} from 'redux';
import {styles} from '../constants';
import {getLocalizedColumnName} from '../helpers/columnDetails';
import {getLocalizedValue} from '../helpers/valueDetails';
import {DataRow} from '../types';

interface DataTableProps {
currentPanel: string;
data: DataRow[];
datasetId?: string;
labelColumn?: string;
datasetId: string;
labelColumn: string;
selectedFeatures: string[];
setCurrentColumn: (column?: string) => void;
setHighlightColumn: (column?: string) => void;
Expand Down Expand Up @@ -143,7 +144,7 @@ const DataTable = ({
onMouseEnter={() => handleSetHighlightColumn(columnId)}
onMouseLeave={() => handleSetHighlightColumn(undefined)}
>
{getLocalizedColumnName(datasetId!, columnId)}
{getLocalizedColumnName(datasetId, columnId)}
</th>
);
})}
Expand All @@ -168,7 +169,7 @@ const DataTable = ({
{startingRow !== undefined && index <= startingRow ? (
<span>&nbsp;</span>
) : (
row[columnId]
getLocalizedValue(row[columnId], datasetId)
)}
</td>
);
Expand All @@ -184,8 +185,8 @@ const DataTable = ({
export default connect(
(state: RootState, props: {useResultsData?: boolean}) => ({
data: getTableData(state, !!props.useResultsData),
datasetId: state.metadata && state.metadata.name,
labelColumn: state.labelColumn,
datasetId: state.metadata?.name || 'unknown',
labelColumn: state.labelColumn || 'unknown',
selectedFeatures: state.selectedFeatures,
currentColumn: state.currentColumn,
highlightColumn: state.highlightColumn,
Expand Down
8 changes: 6 additions & 2 deletions src/UIComponents/ModelCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Statement from './Statement';
import aiBotBorder from '@public/images/ai-bot/ai-bot-border.png';
import I18n from '../i18n';
import {getLocalizedColumnName} from '../helpers/columnDetails';
import {getLocalizedValue} from '../helpers/valueDetails';
import {
ModelCardColumn,
TrainedModelDetailsSave,
Expand All @@ -21,6 +22,7 @@ interface ModelCardProps {
label: ModelCardColumn;
features: ModelCardColumn[];
datasetDetails: DatasetDetails;
datasetId: string;
}

const ModelCard = ({
Expand All @@ -30,6 +32,7 @@ const ModelCard = ({
label,
features,
datasetDetails,
datasetId,
}: ModelCardProps) => {
const localizedLabel = getLocalizedColumnName(datasetDetails.name, label.id);
const localizedFeatures = selectedFeatures.map(feature =>
Expand Down Expand Up @@ -131,7 +134,7 @@ const ModelCard = ({
<p style={styles.modelCardDetails}>
{I18n.t('modelCardPossibleValues')}
<br />
{label.values.join(' ')}
{label.values.map((value) => getLocalizedValue(value, datasetId)).join(' ')}
</p>
)}
</div>
Expand All @@ -158,7 +161,7 @@ const ModelCard = ({
<p>
{I18n.t('modelCardPossibleValues')}
<br />
{feature.values.join(' ')}
{feature.values.map((value) => getLocalizedValue(value, datasetId)).join(' ')}
</p>
)}
</div>
Expand All @@ -178,4 +181,5 @@ export default connect((state: RootState) => ({
label: getLabelToSave(state),
features: getFeaturesToSave(state),
datasetDetails: getDatasetDetails(state),
datasetId: state.metadata?.name || 'unknown',
}))(ModelCard);
21 changes: 10 additions & 11 deletions src/UIComponents/Predict.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import aiBotBorder from '@public/images/ai-bot/ai-bot-border.png';
import ScrollableContent from './ScrollableContent';
import I18n from '../i18n';
import {getLocalizedColumnName} from '../helpers/columnDetails';
import {getLocalizedValue} from '../helpers/valueDetails';

interface PredictProps {
labelColumn: string | undefined;
labelColumn: string;
selectedCategoricalFeatures: string[];
selectedNumericalFeatures: string[];
uniqueOptionsByColumn: Record<string, string[]>;
Expand All @@ -28,7 +29,7 @@ interface PredictProps {
predictedLabel: string | number;
getPredictAvailable: boolean;
extremaByColumn: Record<string, {min: number; max: number}>;
datasetId: string | undefined;
datasetId: string;
}

const Predict = ({
Expand Down Expand Up @@ -66,7 +67,7 @@ const Predict = ({
return (
<div style={styles.cardRow} key={index}>
<label>
{getLocalizedColumnName(datasetId!, feature)}
{getLocalizedColumnName(datasetId, feature)}
&nbsp;
<input
type="number"
Expand All @@ -86,7 +87,7 @@ const Predict = ({
{selectedCategoricalFeatures.map((feature, index) => {
return (
<div style={styles.cardRow} key={index}>
<div>{getLocalizedColumnName(datasetId!, feature)}&nbsp;</div>
<div>{getLocalizedColumnName(datasetId, feature)}&nbsp;</div>
<div>
<select
onChange={event => handleChange(event, feature)}
Expand All @@ -98,7 +99,7 @@ const Predict = ({
.map((option, index) => {
return (
<option key={index} value={option}>
{option}
{getLocalizedValue(option, datasetId)}
</option>
);
})}
Expand Down Expand Up @@ -132,10 +133,8 @@ const Predict = ({
</div>
<div style={styles.predictBotRight}>
<div style={styles.statement}>{I18n.t('predictAIBotPredicts')}</div>
<div>{getLocalizedColumnName(datasetId!, labelColumn!)}</div>
<div>
{getLocalizedColumnName(datasetId!, String(predictedLabel))}
</div>
<div>{getLocalizedColumnName(datasetId, labelColumn)}</div>
<div>{getLocalizedValue(predictedLabel, datasetId)}</div>
</div>
</div>
)}
Expand All @@ -147,13 +146,13 @@ export default connect(
(state: RootState) => ({
testData: state.testData,
predictedLabel: getConvertedPredictedLabel(state),
labelColumn: state.labelColumn,
labelColumn: state.labelColumn || 'unknown',
selectedNumericalFeatures: getSelectedNumericalFeatures(state),
selectedCategoricalFeatures: getSelectedCategoricalFeatures(state),
uniqueOptionsByColumn: getUniqueOptionsByColumn(state),
getPredictAvailable: getPredictAvailable(state),
extremaByColumn: getExtremaByColumn(state),
datasetId: state.metadata && state.metadata.name,
datasetId: state.metadata?.name || 'unknown',
}),
(dispatch: Dispatch) => ({
setTestData(feature: string, value: string | number) {
Expand Down
24 changes: 14 additions & 10 deletions src/UIComponents/ResultsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import {Dispatch} from 'redux';
import {styles, colors, REGRESSION_ERROR_TOLERANCE} from '../constants';
import I18n from '../i18n';
import {getLocalizedColumnName} from '../helpers/columnDetails';
import {getLocalizedValue} from '../helpers/valueDetails';
import {ResultsData} from '../types';

interface ResultsTableProps {
selectedFeatures: string[];
labelColumn: string | undefined;
labelColumn: string;
results: ResultsData;
isRegression: boolean;
setResultsHighlightRow: (row: number | undefined) => void;
resultsHighlightRow: number | undefined;
datasetId: string | undefined;
datasetId: string;
}

const ResultsTable = ({
Expand Down Expand Up @@ -90,7 +91,7 @@ const ResultsTable = ({
}}
key={index}
>
{getLocalizedColumnName(datasetId!, feature)}
{getLocalizedColumnName(datasetId, feature)}
</th>
);
})}
Expand All @@ -101,7 +102,7 @@ const ResultsTable = ({
...styles.resultsTableSecondHeader,
}}
>
{getLocalizedColumnName(datasetId!, labelColumn!)}
{getLocalizedColumnName(datasetId, labelColumn)}
</th>
<th
style={{
Expand All @@ -110,7 +111,7 @@ const ResultsTable = ({
...styles.resultsTableSecondHeader,
}}
>
{getLocalizedColumnName(datasetId!, labelColumn!)}
{getLocalizedColumnName(datasetId, labelColumn)}
</th>
</tr>
</thead>
Expand All @@ -125,15 +126,18 @@ const ResultsTable = ({
{examples.map((example, i) => {
return (
<td style={getRowCellStyle(index)} key={i}>
{example}
{getLocalizedValue(example, datasetId)}
</td>
);
})}
<td style={getRowCellStyle(index)}>
{results.labels[index]}
{getLocalizedValue(results.labels[index], datasetId)}
</td>
<td style={getRowCellStyle(index)}>
{results.predictedLabels[index]}
{getLocalizedValue(
results.predictedLabels[index],
datasetId,
)}
</td>
</tr>
);
Expand All @@ -148,10 +152,10 @@ const ResultsTable = ({
export default connect(
(state: RootState) => ({
selectedFeatures: state.selectedFeatures,
labelColumn: state.labelColumn,
labelColumn: state.labelColumn || 'unknown',
isRegression: isRegression(state),
resultsHighlightRow: state.resultsHighlightRow,
datasetId: state.metadata && state.metadata.name,
datasetId: state.metadata?.name || 'unknown',
}),
(dispatch: Dispatch) => ({
setResultsHighlightRow(column: number | undefined) {
Expand Down
21 changes: 21 additions & 0 deletions src/helpers/valueDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import I18n from '../i18n';

export function getLocalizedValue(
value: string | number,
datasetId: string,
): string | number {
if (typeof value === 'number') {
return value;
}

if (Number.isFinite(+(value || NaN))) {
return value;
}

return (
I18n.t(value, {
scope: ['datasets', datasetId, 'values'],
default: value,
}) || value
);
}
16 changes: 16 additions & 0 deletions test/unit/columnDetails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
isColumnNumerical,
filterColumnsByType,
getUniqueOptions,
getLocalizedColumnName,
tooManyUniqueOptions,
isColumnReadOnly,
getExtrema,
Expand All @@ -14,6 +15,7 @@ import {
allNumericalState,
regressionState,
premadeDatasetState,
premadeDatasetColumnNameLocalized,
mosquitoCountMax,
mosquitoCountMin,
mosquitoDescription,
Expand Down Expand Up @@ -151,3 +153,17 @@ describe('getColumnDescription', () => {
expect(result).toEqual(null);
});
});

describe('getLocalizedColumnName', () => {
test('returns the fallback column name', async () => {
const result = getLocalizedColumnName('dataset-id', 'column-id');
expect(result).toEqual('column-id');
});

test('returns the localized column name', async () => {
I18n.reset();
I18n.initI18n(premadeDatasetTranslations);
const result = getLocalizedColumnName('bats_eat_mozzies', 'mosquitoCount');
expect(result).toEqual(premadeDatasetColumnNameLocalized);
});
});
Loading
Loading