Skip to content

Commit

Permalink
#669: fixed correlation code export
Browse files Browse the repository at this point in the history
  • Loading branch information
aschonfeld committed May 24, 2022
1 parent ddff3c8 commit 39d8922
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 16 deletions.
16 changes: 13 additions & 3 deletions dtale/correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import dtale.global_state as global_state
from dtale.code_export import build_code_export
from dtale.utils import classify_type
from dtale.utils import classify_type, dict_merge


def get_col_groups(data_id, data):
Expand All @@ -28,7 +28,7 @@ def get_col_groups(data_id, data):
return valid_corr_cols, valid_str_corr_cols, valid_date_cols


def build_matrix(data_id, data, cols):
def build_matrix(data_id, data, cols, code_formatting_vars=None):
if data[cols].isnull().values.any():
data = data.corr(method="pearson")
code = build_code_export(data_id)
Expand All @@ -40,6 +40,10 @@ def build_matrix(data_id, data, cols):
"corr_data = df[corr_cols]\n"
"{str_encodings}"
"corr_data = corr_data.corr(method='pearson')"
).format(
**dict_merge(
{"corr_cols": "", "str_encodings": ""}, code_formatting_vars
)
)
)
else:
Expand All @@ -59,6 +63,10 @@ def build_matrix(data_id, data, cols):
"{str_encodings}"
"corr_data = np.corrcoef(corr_data.values, rowvar=False)\n"
"corr_data = pd.DataFrame(corr_data, columns=[corr_cols], index=[corr_cols])"
).format(
**dict_merge(
{"corr_cols": "", "str_encodings": ""}, code_formatting_vars
)
)
)

Expand All @@ -69,7 +77,9 @@ def build_matrix(data_id, data, cols):
def get_analysis(data_id):
df = global_state.get_data(data_id)
valid_corr_cols, _, _ = get_col_groups(data_id, df)
corr_matrix, _ = build_matrix(data_id, df, valid_corr_cols)
corr_matrix, _ = build_matrix(
data_id, df, valid_corr_cols, {"corr_cols": "", "str_encodings": ""}
)
corr_matrix = corr_matrix.abs()

# Select upper triangle of correlation matrix
Expand Down
13 changes: 7 additions & 6 deletions dtale/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2744,12 +2744,13 @@ def build_correlations_matrix(data_id, is_pps=False, encode_strings=False, image

data, pps_data = get_ppscore_matrix(data[valid_corr_cols])
else:
data, matrix_code = correlations.build_matrix(data_id, data, valid_corr_cols)
code = [
matrix_code.format(
corr_cols=corr_cols_str, str_encodings=str_encodings_code
)
]
data, matrix_code = correlations.build_matrix(
data_id,
data,
valid_corr_cols,
{"corr_cols": corr_cols_str, "str_encodings": str_encodings_code},
)
code = [matrix_code]

code.append(
"corr_data.index.name = str('column')\ncorr_data = corr_data.reset_index()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const CorrelationScatterStats: React.FC<CorrelationScatterStatsProps & WithTrans
<div className="pt-5">
<dl className="property-pair inline">
<dt>
<b>{`${col0} ${t('vs.', { ns: 'correlations' })} ${col1}${date ?? ''}`}</b>;
<b>{`${col0} ${t('vs.', { ns: 'correlations' })} ${col1}${date ?? ''}`}</b>
</dt>
</dl>
<dl className="property-pair inline">
Expand Down
1 change: 1 addition & 0 deletions frontend/static/popups/correlations/Correlations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export const Correlations: React.FC = () => {
if (scatterUrl === updatedScatterUrl) {
return;
}
setSelectedCols(cols);
scatterBouncer.current?.style.setProperty('display', 'block');
scatterCanvas.current?.style.setProperty('display', 'none');
CorrelationsRepository.loadScatter(updatedScatterUrl).then((response) => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/static/popups/correlations/CorrelationsGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const CorrelationsGrid: React.FC<CorrelationsGridProps & WithTranslation> = ({ c
const { dataId, sidePanel, theme } = useSelector((state: AppState) => state);
const columnOptions: Array<BaseOption<string>> = React.useMemo(
() => columns.map((column) => ({ value: column })),
columns,
[columns],
);
const [col1, setCol1] = React.useState<BaseOption<string> | undefined>(
props.col1 ? { value: props.col1 } : undefined,
Expand Down Expand Up @@ -171,7 +171,7 @@ const CorrelationsGrid: React.FC<CorrelationsGridProps & WithTranslation> = ({ c
onClick={() => window.open(buildCorrelationsUrl(dataId, props.encodeStrings, props.isPPS, true), '_blank')}
>
<i className="fas fa-file-code pr-3" />
<span className="align-middle">{t('Export Image', { ns: 'side' })}</span>
<span className="align-middle">{t('Export Image', { ns: 'correlations' })}</span>
</button>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion frontend/static/translations/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@
"Correlations Timeseries": "Correlations Timeseries",
"Predictive Power Score for ": "Predictive Power Score for ",
"Encode Strings": "Encode Strings",
"encode_strings_tt": "Checking this box will OneHotEncode (pd.get_dummies) any string column with no more then 50 unique values and include them in the correlation matrix. Those columns are:"
"encode_strings_tt": "Checking this box will OneHotEncode (pd.get_dummies) any string column with no more then 50 unique values and include them in the correlation matrix. Those columns are:",
"Export Image": "Export Image"
},
"xarray": {
"count": "count",
Expand Down
3 changes: 2 additions & 1 deletion frontend/static/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@
"Correlations Timeseries": "Correlations Timeseries",
"Predictive Power Score for ": "Predictive Power Score for ",
"Encode Strings": "Encode Strings",
"encode_strings_tt": "Checking this box will OneHotEncode (pd.get_dummies) any string column with no more then 50 unique values and include them in the correlation matrix. Those columns are:"
"encode_strings_tt": "Checking this box will OneHotEncode (pd.get_dummies) any string column with no more then 50 unique values and include them in the correlation matrix. Those columns are:",
"Export Image": "Export Image"
},
"xarray": {
"count": "count",
Expand Down
3 changes: 2 additions & 1 deletion frontend/static/translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@
"Correlations Timeseries": "Correlações de Séries Temporais",
"Predictive Power Score for ": "Power Score Preditivo para ",
"Encode Strings": "Encode Strings",
"encode_strings_tt": "Marcando esta caixa usará OneHotEncode (pd.get_dummies) em qualquer coluna de string com até 50 valores únicos para incluí-los na matriz de correlação. Essas colunas são:"
"encode_strings_tt": "Marcando esta caixa usará OneHotEncode (pd.get_dummies) em qualquer coluna de string com até 50 valores únicos para incluí-los na matriz de correlação. Essas colunas são:",
"Export Image": "Export Image"
},
"xarray": {
"count": "contas",
Expand Down
11 changes: 10 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
lz4<=2.2.1; python_version < '3.0'
lz4; python_version > '3.0'
certifi==2021.10.8; python_version == '2.7'
certifi; python_version > '3.0'
cycler; python_version > '3.0'
cycler==0.10.0; python_version == '2.7'
dash==1.21.0; python_version == '2.7'
Expand Down Expand Up @@ -41,7 +43,14 @@ matplotlib==3.3.4; python_version == '3.6'
matplotlib; python_version > '3.6'
missingno<=0.4.2
networkx==2.2; python_version < '3.0'
networkx; python_version >= '3.0'
networkx==2.8; python_version == '3.1'
networkx==2.8; python_version == '3.2'
networkx==2.8; python_version == '3.3'
networkx==2.8; python_version == '3.4'
networkx==2.8; python_version == '3.5'
networkx==2.8; python_version == '3.6'
networkx==2.8; python_version == '3.7'
networkx; python_version > '3.7'
numpy==1.16.6; python_version < '3.0'
numpy; python_version >= '3.0'
openpyxl==2.6.4; python_version < '3.0'
Expand Down

0 comments on commit 39d8922

Please sign in to comment.