Skip to content

Commit

Permalink
only allowed 3D when 2D is done
Browse files Browse the repository at this point in the history
  • Loading branch information
ndittren committed Apr 23, 2024
1 parent b775091 commit 44b5ca2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
31 changes: 17 additions & 14 deletions media/js/src/simulationOne/scatterPlot.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
const x = Math.round(rng() * 100);
const y = Math.round(correlation * x + Math.sqrt(
1 - Math.pow(correlation, 2)) * rng() * 100);
const z = Math.round(correlation * y + Math.sqrt(
1 - Math.pow(correlation, 2)) * rng() * 100);

generatedData.push({ x, y, z });
if (plotType === '3d') {
const z = Math.round(correlation * y + Math.sqrt(
1 - Math.pow(correlation, 2)) * rng() * 100);
generatedData.push({ x, y, z });
} else {
generatedData.push({ x, y });
}
}
}

Expand Down Expand Up @@ -52,10 +55,11 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
: undefined;

try {
let response;
if (plotType === '3d') {

response = await axios.post('/calculate_multiple_regression/', {
if (plotType === '3d' && z_values[0]) {

// eslint-disable-next-line max-len
const response = await axios.post('/calc_multi_regression/', {
x1_values: x_values,
x2_values: y_values,
y_values: z_values,
Expand Down Expand Up @@ -85,11 +89,12 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
});

} else {
response = await axios.post('/calculate_regression/', {
const response = await axios.post('/calc_regression/', {
x_values,
y_values,
});
const { slope, intercept, stderr, rvalue } = response.data;

setSlope(slope);
setIntercept(intercept);
setStderror(stderr);
Expand All @@ -101,7 +106,8 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
mode: 'lines',
x: [Math.min(...x_values), Math.max(...x_values)],
// eslint-disable-next-line max-len
y: [slope * Math.min(...x_values) + intercept, slope * Math.max(...x_values) + intercept],
y: [slope * Math.min(...x_values) + intercept, slope * Math.max(
...x_values) + intercept],
marker: { color: 'red' },
});
}
Expand Down Expand Up @@ -134,7 +140,7 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
if(N) {
setData(generateData());
}
}, [N, correlation, seed]);
}, [N, correlation, seed, plotType]);

useEffect(() => {
if(data.length > 0) {
Expand Down Expand Up @@ -203,10 +209,7 @@ ScatterPlot.propTypes = {
setSlope: PropTypes.func,
setIntercept: PropTypes.func,
setStderror: PropTypes.func,
slope: PropTypes.oneOfType([
PropTypes.number,
PropTypes.arrayOf(PropTypes.number)
]),
slope: PropTypes.number,
stderror: PropTypes.number,
intercept: PropTypes.number,
plotType: PropTypes.oneOf(['2d', '3d']).isRequired,
Expand Down
10 changes: 8 additions & 2 deletions media/js/src/simulationOne/simulationOne.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SimulationOne = () => {
const [plotType, setPlotType] = useState('2d');
const [slopes, setSlopes] = useState([]);
const [stderrs, setStderrs] = useState([]);
const [is2DCompleted, setIs2DCompleted] = useState(false);


const saveGraphData = async() => {
Expand Down Expand Up @@ -315,7 +316,9 @@ export const SimulationOne = () => {
tvalue={tvalue}
hypothesizedSlope={hypothesizedSlope}
n={N}
appRvalue={appRvalue} />
appRvalue={appRvalue}
is2DCompleted={is2DCompleted}
setIs2DCompleted={setIs2DCompleted} />
)}
</>
)}
Expand All @@ -335,7 +338,10 @@ export const SimulationOne = () => {
<a className={
plotType === '3d'
? 'active nav-link'
: 'nav-link'}
: (!is2DCompleted
? 'nav-link disabled'
: 'nav-link')
}
onClick={() => handlePlotTypeChange('3d')}
href='#'>3D</a>
</li>
Expand Down
21 changes: 17 additions & 4 deletions media/js/src/simulationOne/simulationOneQuiz.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { Katex } from '../katexComponent';
import { HypothesisTest } from './hypothesisTest';
import { MultipleChoiceQuestion } from './multipleChoiceQuestion';
import PropTypes from 'prop-types';

export const SimulationOneQuiz = ({
appRvalue, tvalue, hypothesizedSlope, n
appRvalue, tvalue, hypothesizedSlope, n, setIs2DCompleted,
is2DCompleted
}) => {
const [selectedOption, setSelectedOption] = useState(null);
const [completedChoices, setCompletedChoices] = useState([]);

const allChoicesCompleted = ['A', 'B', 'C'].every(
choice => completedChoices.includes(choice));

const handleChoiceCompletion = () => {
setCompletedChoices([...completedChoices, selectedOption]);
setSelectedOption(null);
};

const allChoicesCompleted = ['A', 'B', 'C'].every(
choice => completedChoices.includes(choice));
const is2dCompleted = () => {
if (allChoicesCompleted) {
setIs2DCompleted(true);
}
};

useEffect(() => {
is2dCompleted();
},[completedChoices, allChoicesCompleted]);

return (
<>
Expand Down Expand Up @@ -122,4 +133,6 @@ SimulationOneQuiz.propTypes = {
tvalue: PropTypes.string.isRequired,
hypothesizedSlope: PropTypes.any.isRequired,
n: PropTypes.any.isRequired,
setIs2DCompleted: PropTypes.func.isRequired,
is2DCompleted: PropTypes.bool.isRequired,
};
4 changes: 2 additions & 2 deletions metricsmentor/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@

re_path(r'^course/(?P<pk>\d+)/$', views.CourseDetailView.as_view(),
name='course-detail-view'),
path('calculate_regression/', views.calculate_regression,
path('calc_regression/', views.calculate_regression,
name='calculate_regression'),
path('calculate_multiple_regression/', views.calculate_multiple_regression,
path('calc_multi_regression/', views.calculate_multiple_regression,
name='calculate_multiple_regression'),
path('calculate_pvalue/', views.calculate_pvalue,
name='calculate_pvalue'),
Expand Down

0 comments on commit 44b5ca2

Please sign in to comment.