-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from flatironinstitute/bmw/analysis.R
Consolidate script editors, add analysis.R
- Loading branch information
Showing
34 changed files
with
935 additions
and
890 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { FunctionComponent, RefObject, useCallback, useMemo } from "react"; | ||
import { StanRun } from "@SpStanSampler/useStanSampler"; | ||
import { FileNames } from "@SpCore/FileMapping"; | ||
import { ProjectKnownFiles } from "@SpCore/ProjectDataModel"; | ||
import useTemplatedFillerText from "@SpScripting/useTemplatedFillerText"; | ||
import { | ||
clearOutputDivs, | ||
writeConsoleOutToDiv, | ||
} from "@SpScripting/OutputDivUtils"; | ||
import usePyodideWorker from "@SpScripting/pyodide/usePyodideWorker"; | ||
import PlottingScriptEditor from "@SpScripting/PlottingScriptEditor"; | ||
import useAnalysisState from "./useAnalysisState"; | ||
|
||
import analysisPyTemplate from "./analysis_template.py?raw"; | ||
|
||
export type GlobalDataForAnalysis = { | ||
draws: number[][]; | ||
paramNames: string[]; | ||
numChains: number; | ||
}; | ||
|
||
type AnalysisWindowProps = { | ||
latestRun: StanRun; | ||
}; | ||
|
||
const AnalysisPyWindow: FunctionComponent<AnalysisWindowProps> = ({ | ||
latestRun, | ||
}) => { | ||
const { | ||
consoleRef, | ||
imagesRef, | ||
spData, | ||
status, | ||
onStatus, | ||
runnable, | ||
notRunnableReason, | ||
} = useAnalysisState(latestRun); | ||
|
||
const callbacks = useMemo( | ||
() => ({ | ||
onStdout: (x: string) => writeConsoleOutToDiv(consoleRef, x, "stdout"), | ||
onStderr: (x: string) => writeConsoleOutToDiv(consoleRef, x, "stderr"), | ||
onImage: (b64: string) => addImageToDiv(imagesRef, b64), | ||
onStatus, | ||
}), | ||
[consoleRef, imagesRef, onStatus], | ||
); | ||
|
||
const { run } = usePyodideWorker(callbacks); | ||
|
||
const handleRun = useCallback( | ||
(code: string) => { | ||
clearOutputDivs(consoleRef, imagesRef); | ||
run(code, spData, { | ||
loadsDraws: true, | ||
showsPlots: true, | ||
producesData: false, | ||
}); | ||
}, | ||
[consoleRef, imagesRef, run, spData], | ||
); | ||
|
||
const contentOnEmpty = useTemplatedFillerText( | ||
"Use the draws object to access the samples. ", | ||
analysisPyTemplate, | ||
ProjectKnownFiles.ANALYSISPYFILE, | ||
); | ||
|
||
return ( | ||
<PlottingScriptEditor | ||
filename={FileNames.ANALYSISPYFILE} | ||
dataKey={ProjectKnownFiles.ANALYSISPYFILE} | ||
language="python" | ||
status={status} | ||
onRun={handleRun} | ||
runnable={runnable} | ||
notRunnableReason={notRunnableReason} | ||
imagesRef={imagesRef} | ||
consoleRef={consoleRef} | ||
contentOnEmpty={contentOnEmpty} | ||
/> | ||
); | ||
}; | ||
|
||
const addImageToDiv = (imagesRef: RefObject<HTMLDivElement>, b64: string) => { | ||
const imageUrl = `data:image/png;base64,${b64}`; | ||
|
||
const img = document.createElement("img"); | ||
img.style.width = "100%"; | ||
img.src = imageUrl; | ||
|
||
const divElement = document.createElement("div"); | ||
divElement.appendChild(img); | ||
imagesRef.current?.appendChild(divElement); | ||
}; | ||
|
||
export default AnalysisPyWindow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { FunctionComponent, useCallback } from "react"; | ||
import { StanRun } from "@SpStanSampler/useStanSampler"; | ||
import { FileNames } from "@SpCore/FileMapping"; | ||
import { ProjectKnownFiles } from "@SpCore/ProjectDataModel"; | ||
import PlottingScriptEditor from "@SpScripting/PlottingScriptEditor"; | ||
import runR from "@SpScripting/webR/runR"; | ||
import useTemplatedFillerText from "@SpScripting/useTemplatedFillerText"; | ||
import { clearOutputDivs } from "@SpScripting/OutputDivUtils"; | ||
import loadDrawsCode from "@SpScripting/webR/sp_load_draws.R?raw"; | ||
import useAnalysisState from "./useAnalysisState"; | ||
|
||
import analysisRTemplate from "./analysis_template.R?raw"; | ||
|
||
type AnalysisWindowProps = { | ||
latestRun: StanRun; | ||
}; | ||
|
||
const AnalysisRWindow: FunctionComponent<AnalysisWindowProps> = ({ | ||
latestRun, | ||
}) => { | ||
const { | ||
consoleRef, | ||
imagesRef, | ||
spData, | ||
status, | ||
onStatus, | ||
runnable, | ||
notRunnableReason, | ||
} = useAnalysisState(latestRun); | ||
|
||
const handleRun = useCallback( | ||
async (userCode: string) => { | ||
clearOutputDivs(consoleRef, imagesRef); | ||
const code = loadDrawsCode + userCode; | ||
await runR({ | ||
code, | ||
imagesRef, | ||
consoleRef, | ||
onStatus, | ||
spData, | ||
}); | ||
}, | ||
[consoleRef, imagesRef, onStatus, spData], | ||
); | ||
|
||
const contentOnEmpty = useTemplatedFillerText( | ||
"Use the draws object (a posterior::draws_array) to access the samples. ", | ||
analysisRTemplate, | ||
ProjectKnownFiles.ANALYSISRFILE, | ||
); | ||
|
||
return ( | ||
<PlottingScriptEditor | ||
filename={FileNames.ANALYSISRFILE} | ||
dataKey={ProjectKnownFiles.ANALYSISRFILE} | ||
language="r" | ||
status={status} | ||
onRun={handleRun} | ||
runnable={runnable} | ||
notRunnableReason={notRunnableReason} | ||
imagesRef={imagesRef} | ||
consoleRef={consoleRef} | ||
contentOnEmpty={contentOnEmpty} | ||
/> | ||
); | ||
}; | ||
|
||
export default AnalysisRWindow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
library(posterior) | ||
print(summary(draws)) | ||
|
||
install.packages("bayesplot") | ||
library(bayesplot) | ||
|
||
mcmc_hist(draws, pars=c("lp__")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import matplotlib.pyplot as plt | ||
|
||
# Print the draws object | ||
print(draws) | ||
|
||
# Print parameter names | ||
print(draws.parameter_names) | ||
|
||
# plot the lp parameter | ||
samples = draws.get("lp__") | ||
print(samples.shape) | ||
plt.hist(samples.ravel(), bins=30) | ||
plt.title("lp__") | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
InterpreterStatus, | ||
isInterpreterBusy, | ||
} from "@SpScripting/InterpreterTypes"; | ||
import { clearOutputDivs } from "@SpScripting/OutputDivUtils"; | ||
import { StanRun } from "@SpStanSampler/useStanSampler"; | ||
import { useEffect, useMemo, useRef, useState } from "react"; | ||
|
||
export type GlobalDataForAnalysis = { | ||
draws: number[][]; | ||
paramNames: string[]; | ||
numChains: number; | ||
}; | ||
|
||
// A custom hook to share logic between the Python and R analysis windows | ||
// This contains the output div refs, the interpreter state, and the data from | ||
// the latest run. | ||
const useAnalysisState = (latestRun: StanRun) => { | ||
const consoleRef = useRef<HTMLDivElement | null>(null); | ||
const imagesRef = useRef<HTMLDivElement | null>(null); | ||
|
||
useEffect(() => { | ||
clearOutputDivs(consoleRef, imagesRef); | ||
}, [latestRun.draws]); | ||
|
||
const { draws, paramNames, samplingOpts, status: samplerStatus } = latestRun; | ||
const numChains = samplingOpts?.num_chains; | ||
const spData = useMemo(() => { | ||
if (samplerStatus === "completed" && draws && numChains && paramNames) { | ||
return { | ||
draws, | ||
paramNames, | ||
numChains, | ||
}; | ||
} else { | ||
return undefined; | ||
} | ||
}, [samplerStatus, draws, numChains, paramNames]); | ||
|
||
const [status, setStatus] = useState<InterpreterStatus>("idle"); | ||
|
||
const [runnable, setRunnable] = useState<boolean>(true); | ||
const [notRunnableReason, setNotRunnableReason] = useState<string>(""); | ||
|
||
const isDataDefined = useMemo(() => spData !== undefined, [spData]); | ||
|
||
useEffect(() => { | ||
if (!isDataDefined) { | ||
setRunnable(false); | ||
setNotRunnableReason("Run sampler first."); | ||
} else if (isInterpreterBusy(status)) { | ||
setRunnable(false); | ||
setNotRunnableReason(""); | ||
} else { | ||
setRunnable(true); | ||
setNotRunnableReason(""); | ||
} | ||
}, [isDataDefined, status]); | ||
|
||
return { | ||
consoleRef, | ||
imagesRef, | ||
spData, | ||
status, | ||
onStatus: setStatus, | ||
runnable, | ||
notRunnableReason, | ||
}; | ||
}; | ||
|
||
export default useAnalysisState; |
Oops, something went wrong.