Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include sampling_opts.json in exported csv zip with draws #90

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 18 additions & 6 deletions gui/src/app/SamplerOutputView/SamplerOutputView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SmallIconButton } from "@fi-sci/misc"
import { Download } from "@mui/icons-material"
import JSZip from 'jszip'
import { FunctionComponent, useCallback, useMemo, useState } from "react"
import StanSampler from "../StanSampler/StanSampler"
import StanSampler, { SamplingOpts } from "../StanSampler/StanSampler"
import { useSamplerOutput } from "../StanSampler/useStanSampler"
import TabWidget from "../TabWidget/TabWidget"
import { triggerDownload } from "../util/triggerDownload"
Expand Down Expand Up @@ -30,6 +30,7 @@ const SamplerOutputView: FunctionComponent<SamplerOutputViewProps> = ({width, he
paramNames={paramNames}
numChains={numChains}
computeTimeSec={computeTimeSec}
samplingOpts={sampler.samplingOpts}
/>
)
}
Expand All @@ -41,6 +42,7 @@ type DrawsDisplayProps = {
numChains: number,
paramNames: string[]
computeTimeSec: number | undefined
samplingOpts: SamplingOpts | undefined // for including in exported zip
}

const tabs = [
Expand Down Expand Up @@ -70,7 +72,7 @@ const tabs = [
}
]

const DrawsDisplay: FunctionComponent<DrawsDisplayProps> = ({ width, height, draws, paramNames, numChains, computeTimeSec }) => {
const DrawsDisplay: FunctionComponent<DrawsDisplayProps> = ({ width, height, draws, paramNames, numChains, computeTimeSec, samplingOpts }) => {

const [currentTabId, setCurrentTabId] = useState('summary');

Expand Down Expand Up @@ -106,6 +108,7 @@ const DrawsDisplay: FunctionComponent<DrawsDisplayProps> = ({ width, height, dra
paramNames={paramNames}
drawChainIds={drawChainIds}
drawNumbers={drawNumbers}
samplingOpts={samplingOpts}
/>
<HistsView
width={0}
Expand All @@ -132,9 +135,10 @@ type DrawsViewProps = {
paramNames: string[]
drawChainIds: number[]
drawNumbers: number[]
samplingOpts: SamplingOpts | undefined // for including in exported zip
}

const DrawsView: FunctionComponent<DrawsViewProps> = ({ width, height, draws, paramNames, drawChainIds, drawNumbers }) => {
const DrawsView: FunctionComponent<DrawsViewProps> = ({ width, height, draws, paramNames, drawChainIds, drawNumbers, samplingOpts }) => {
const [abbreviatedToNumRows, setAbbreviatedToNumRows] = useState<number | undefined>(300);
const draws2 = useMemo(() => {
if (abbreviatedToNumRows === undefined) return draws;
Expand All @@ -147,15 +151,15 @@ const DrawsView: FunctionComponent<DrawsViewProps> = ({ width, height, draws, pa
const handleExportToMultipleCsvs = useCallback(async () => {
const uniqueChainIds = Array.from(new Set(drawChainIds));
const csvTexts = prepareMultipleCsvsText(draws, paramNames, drawChainIds, uniqueChainIds);
const blob = await createZipBlobForMultipleCsvs(csvTexts, uniqueChainIds);
const blob = await createZipBlobForMultipleCsvs(csvTexts, uniqueChainIds, samplingOpts);
const fileName = 'SP-draws.zip';
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
URL.revokeObjectURL(url);
}, [draws, paramNames, drawChainIds]);
}, [draws, paramNames, drawChainIds, samplingOpts]);
return (
<div style={{position: 'absolute', width, height, overflow: 'auto'}}>
<div>
Expand Down Expand Up @@ -237,14 +241,22 @@ const prepareMultipleCsvsText = (draws: number[][], paramNames: string[], drawCh
})
}

const createZipBlobForMultipleCsvs = async (csvTexts: string[], uniqueChainIds: number[]) => {
const createZipBlobForMultipleCsvs = async (csvTexts: string[], uniqueChainIds: number[], samplingOpts: SamplingOpts | undefined) => {
const zip = new JSZip();
// put them all in a folder called 'draws'
const folder = zip.folder('draws');
if (!folder) throw new Error('Failed to create folder');
csvTexts.forEach((text, i) => {
folder.file(`chain_${uniqueChainIds[i]}.csv`, text);
});
if (samplingOpts) {
const samplingOptsText = JSON.stringify(samplingOpts, null, 2);
folder.file('sampling_opts.json', samplingOptsText);
}
else {
// this should not happen, but just in case we'll log a warning
console.warn('Not including sampling options in zip file');
}
const blob = await zip.generateAsync({type: 'blob'});
return blob;
}
Expand Down
5 changes: 5 additions & 0 deletions gui/src/app/StanSampler/StanSampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class StanSampler {
#paramNames: string[] = [];
#numChains: number = 0;
#samplingStartTimeSec: number = 0;
#samplingOpts: SamplingOpts | undefined = undefined; // the sampling options used in the last sample call
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're storing numChains is redundant, correct?

I think we should set this to defaultSamplingOpts rather than undefined, and remove numChains

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


private constructor(private compiledUrl: string) {
this._initialize()
Expand Down Expand Up @@ -107,6 +108,7 @@ class StanSampler {
console.warn('Model not loaded yet')
return
}
this.#samplingOpts = samplingOpts;
this.#draws = [];
this.#paramNames = [];
this.#worker
Expand Down Expand Up @@ -149,6 +151,9 @@ class StanSampler {
get computeTimeSec() {
return this.#computeTimeSec;
}
get samplingOpts() {
return this.#samplingOpts;
}
}

const calculateReasonableRefreshRate = (samplingOpts: SamplingOpts) => {
Expand Down
Loading