File size: 6,650 Bytes
6d08d46 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | import React, { useRef, useState } from 'react'
import {
Title,
Text,
Button,
Anchor,
Slider,
Switch,
Divider,
Stack,
Box,
Loader,
Select,
NumberInput,
Badge,
Alert,
Group,
} from '@mantine/core'
import { IconCloudUpload, IconChartBar, IconAlertCircle } from '@tabler/icons-react'
import { useXRD } from '../context/XRDContext'
import ExampleDataPanel from './ExampleDataPanel'
const Controls = () => {
const {
rawData,
isLoading,
detectedWavelength,
userWavelength,
setUserWavelength,
wavelengthSource,
dataWarnings,
baselineCorrection,
setBaselineCorrection,
interpolationEnabled,
setInterpolationEnabled,
scalingEnabled,
setScalingEnabled,
interpolationStrategy,
setInterpolationStrategy,
handleFileUpload,
runInference,
MODEL_WAVELENGTH,
MODEL_MIN_2THETA,
MODEL_MAX_2THETA,
} = useXRD()
const fileInputRef = useRef(null)
const [showExamples, setShowExamples] = useState(true)
const handleFileChange = async (event) => {
const file = event.target.files?.[0]
if (file) {
const success = await handleFileUpload(file)
if (success) setShowExamples(false)
// Reset file input so the same file can be uploaded again
if (fileInputRef.current) {
fileInputRef.current.value = ''
}
}
}
const handleUploadClick = () => {
fileInputRef.current?.click()
}
return (
<Stack gap="md">
{/* File Upload */}
<Box>
<input
ref={fileInputRef}
type="file"
accept=".xy,.csv,.txt,.cif,.dif"
onChange={handleFileChange}
style={{ display: 'none' }}
/>
<Button
fullWidth
leftSection={<IconCloudUpload size={20} />}
onClick={handleUploadClick}
size="md"
>
Upload XRD Data
</Button>
<Group justify="space-between" mt="xs">
<Text size="sm" c="dimmed">
Formats: .xy, .cif, .dif
</Text>
<Anchor
size="sm"
component="button"
type="button"
onClick={() => setShowExamples((v) => !v)}
>
{showExamples ? 'Hide samples' : 'Try samples'}
</Anchor>
</Group>
</Box>
{showExamples && <ExampleDataPanel onSelect={() => setShowExamples(false)} />}
{rawData && (
<>
<Divider />
{/* Data Info */}
<Box p="md" style={{ backgroundColor: '#f8f9fa', borderRadius: '8px' }}>
<Stack gap="xs">
<Text size="sm" c="dimmed">
Data Points: {rawData.x.length}
</Text>
<Text size="sm" c="dimmed">
Range: {Math.min(...rawData.x).toFixed(2)}° - {Math.max(...rawData.x).toFixed(2)}°
</Text>
</Stack>
</Box>
{/* Warnings */}
{dataWarnings.length > 0 && (
<Alert icon={<IconAlertCircle size={16} />} color="yellow" variant="light">
<Stack gap="xs">
{dataWarnings.map((warning, idx) => (
<Text key={idx} size="xs">{warning}</Text>
))}
</Stack>
</Alert>
)}
<Divider />
{/* Wavelength Configuration */}
<Title order={4}>Wavelength</Title>
{detectedWavelength && (
<Badge color="blue" variant="light" size="sm" mb="xs">
Detected: {detectedWavelength.toFixed(4)} Å
</Badge>
)}
<NumberInput
label="Wavelength (Å)"
description="Cu Kα=1.5406, Mo Kα=0.7107, Synch=0.6199"
value={userWavelength}
onChange={(value) => setUserWavelength(value || MODEL_WAVELENGTH)}
min={0.1}
max={3.0}
step={0.0001}
precision={4}
size="sm"
decimalScale={4}
/>
<Group gap="xs" mt="xs">
<Button size="xs" variant="light" onClick={() => setUserWavelength(1.5406)}>Cu Kα</Button>
<Button size="xs" variant="light" onClick={() => setUserWavelength(0.7107)}>Mo Kα</Button>
<Button size="xs" variant="light" onClick={() => setUserWavelength(0.6199)}>Synch</Button>
</Group>
<Box mt="md" p="md" style={{ backgroundColor: '#e7f5ff', borderRadius: '8px', border: '1px solid #91a7ff' }}>
<Text size="sm" fw={600} c="#1864ab" mb={4}>
Training Data Specs:
</Text>
<Text size="sm" c="#364fc7" style={{ lineHeight: 1.6 }}>
• λ: {MODEL_WAVELENGTH} Å (20 keV)<br/>
• 2θ: {MODEL_MIN_2THETA}°-{MODEL_MAX_2THETA}° (8192 pts)<br/>
• Intensity: 0-100 scaled
</Text>
</Box>
<Divider />
{/* Preprocessing Controls */}
<Title order={4}>Preprocessing</Title>
<Switch
label="Baseline Correction"
checked={baselineCorrection}
onChange={(event) => setBaselineCorrection(event.currentTarget.checked)}
/>
<Switch
label="Signal Scaling (0-100)"
description="Normalize to model input range"
checked={scalingEnabled}
onChange={(event) => setScalingEnabled(event.currentTarget.checked)}
/>
<Select
label="Interpolation Strategy"
description="Resampling method for 8192 points"
value={interpolationStrategy}
onChange={(value) => {
if (value) setInterpolationStrategy(value)
}}
data={[
{ value: 'linear', label: 'Linear' },
{ value: 'cubic', label: 'Cubic Spline' },
]}
size="sm"
allowDeselect={false}
/>
<Divider />
</>
)}
{/* Analysis Button - Always visible when data loaded */}
{rawData && (
<Button
fullWidth
color="violet"
leftSection={isLoading ? <Loader size={18} color="white" /> : <IconChartBar size={20} />}
onClick={runInference}
disabled={isLoading}
size="lg"
style={{ marginTop: 'auto' }}
>
{isLoading ? 'Analyzing...' : 'Run Analysis'}
</Button>
)}
</Stack>
)
}
export default Controls
|