|
| 1 | +import { randomUUID } from "crypto" |
| 2 | +import Image from "next/image" |
| 3 | +import { InputText } from "primereact/inputtext" |
| 4 | +import { useContext, useEffect, useState } from "react" |
| 5 | +import { Button, Card, Stack } from "react-bootstrap" |
| 6 | +import { FaAlignJustify } from "react-icons/fa" |
| 7 | +import { MdDashboard } from "react-icons/md" |
| 8 | +import { TbZoom } from "react-icons/tb" |
| 9 | +import med3paLogo from "../../../resources/MED3paLogo.png" |
| 10 | +import myimage from "../../../resources/medomics_transparent_bg.png" |
| 11 | +import { LayoutModelContext } from "../layout/layoutContext" |
| 12 | +import { insertMEDDataObjectIfNotExists } from "../mongoDB/mongoDBUtils" |
| 13 | +import { DataContext } from "../workspace/dataContext" |
| 14 | +import { MEDDataObject } from "../workspace/NewMedDataObject" |
| 15 | + |
| 16 | + |
| 17 | +// Variable used to store some modularity information about the module |
| 18 | +const typeInfo = { |
| 19 | + title: "Evaluation", |
| 20 | + extension: "medeval", |
| 21 | + internalFolders: [] |
| 22 | +} |
| 23 | + |
| 24 | +export default function EvaluationLandingPage() { |
| 25 | + const [nameEval, setNameEval] = useState("") |
| 26 | + const [experimentList, setExperimentList] = useState([]) // List of .medeval files |
| 27 | + const [loading, setLoading] = useState(false) |
| 28 | + const [pendingOpenId, setPendingOpenId] = useState(null) |
| 29 | + const { dispatchLayout, setLayoutRequestQueue } = useContext(LayoutModelContext) |
| 30 | + const { globalData } = useContext(DataContext) |
| 31 | + |
| 32 | + // We use the useEffect hook to update the experiment list state when the workspace changes |
| 33 | + useEffect(() => { |
| 34 | + let localExperimentList = [] |
| 35 | + if (!globalData["EXPERIMENTS"]) return |
| 36 | + for (const experimentId of globalData["EXPERIMENTS"].childrenIDs) { |
| 37 | + if (globalData[experimentId]) { |
| 38 | + localExperimentList.push(globalData[experimentId].name) |
| 39 | + } |
| 40 | + } |
| 41 | + setExperimentList(localExperimentList) |
| 42 | + }, [globalData]) // We log the workspace when it changes |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (!pendingOpenId || !globalData[pendingOpenId]) return |
| 46 | + |
| 47 | + const medObject = globalData[pendingOpenId] |
| 48 | + const openItem = { |
| 49 | + index: pendingOpenId, |
| 50 | + canMove: true, |
| 51 | + isFolder: false, |
| 52 | + children: medObject.childrenIDs || [], |
| 53 | + data: medObject.name, |
| 54 | + canRename: true, |
| 55 | + type: medObject.type || "medeval", |
| 56 | + inWorkspace: medObject.inWorkspace ?? false, |
| 57 | + path: medObject.path ?? null, |
| 58 | + isLocked: medObject.isLocked ?? null, |
| 59 | + usedIn: medObject.usedIn ?? null |
| 60 | + } |
| 61 | + dispatchLayout({ type: "openInEvaluationModule", payload: openItem }) |
| 62 | + if (setLayoutRequestQueue) { |
| 63 | + setLayoutRequestQueue((prev) => [...prev, { type: "DELETE_TAB", payload: { id: "evaluationLandingPage" } }]) |
| 64 | + } else { |
| 65 | + dispatchLayout({ type: "remove", payload: { name: "Evaluation Module" } }) |
| 66 | + } |
| 67 | + setPendingOpenId(null) |
| 68 | + }, [dispatchLayout, globalData, pendingOpenId, setLayoutRequestQueue]) |
| 69 | + |
| 70 | + const createSceneContent = async (sceneName) => { |
| 71 | + setLoading(true) |
| 72 | + |
| 73 | + // Create custom zip file |
| 74 | + const sceneId = randomUUID() |
| 75 | + const sceneFileName = `${sceneName}.medeval` |
| 76 | + let sceneObject = new MEDDataObject({ |
| 77 | + id: sceneId, |
| 78 | + name: sceneFileName, |
| 79 | + type: "medeval", |
| 80 | + parentID: "EXPERIMENTS", |
| 81 | + childrenIDs: [], |
| 82 | + inWorkspace: false |
| 83 | + }) |
| 84 | + let sceneObjectId = await insertMEDDataObjectIfNotExists(sceneObject) |
| 85 | + |
| 86 | + // Create hidden metadata file |
| 87 | + const emptyScene = [{ useMedStandard: false }] |
| 88 | + const metadataId = randomUUID() |
| 89 | + let metadataObject = new MEDDataObject({ |
| 90 | + id: metadataId, |
| 91 | + name: "metadata.json", |
| 92 | + type: "json", |
| 93 | + parentID: sceneObjectId, |
| 94 | + childrenIDs: [], |
| 95 | + inWorkspace: false |
| 96 | + }) |
| 97 | + await insertMEDDataObjectIfNotExists(metadataObject, null, emptyScene) |
| 98 | + |
| 99 | + // Create internal folders |
| 100 | + const childrenIds = [metadataId] |
| 101 | + for (const folder of typeInfo.internalFolders) { |
| 102 | + const folderId = randomUUID() |
| 103 | + let medObject = new MEDDataObject({ |
| 104 | + id: folderId, |
| 105 | + name: folder, |
| 106 | + type: "directory", |
| 107 | + parentID: sceneObjectId, |
| 108 | + childrenIDs: [], |
| 109 | + inWorkspace: false |
| 110 | + }) |
| 111 | + await insertMEDDataObjectIfNotExists(medObject) |
| 112 | + childrenIds.push(folderId) |
| 113 | + } |
| 114 | + |
| 115 | + // Load everything in globalData |
| 116 | + MEDDataObject.updateWorkspaceDataObject() |
| 117 | + |
| 118 | + setPendingOpenId(sceneObjectId || sceneId) |
| 119 | + |
| 120 | + // Update loading state |
| 121 | + setLoading(false) |
| 122 | + } |
| 123 | + |
| 124 | + function choosePage(event, name) { |
| 125 | + event.stopPropagation() |
| 126 | + console.log(`Double clicked ${name}`, event, `open${name}Module`) |
| 127 | + dispatchLayout({ type: `open${name}Module`, payload: { pageId: name } }) |
| 128 | + } |
| 129 | + |
| 130 | + const trimmedName = nameEval.trim() |
| 131 | + const isValidName = /^[A-Za-z0-9_-]+$/.test(trimmedName) |
| 132 | + const existingNames = new Set(experimentList) |
| 133 | + const hasConflict = |
| 134 | + trimmedName !== "" && (existingNames.has(trimmedName) || existingNames.has(`${trimmedName}.medeval`)) |
| 135 | + const nameError = trimmedName === "" |
| 136 | + ? "" |
| 137 | + : !isValidName |
| 138 | + ? "Use only letters, numbers, hyphens, or underscores." |
| 139 | + : hasConflict |
| 140 | + ? "This evaluation session already exists." |
| 141 | + : "" |
| 142 | + const isStartDisabled = loading || trimmedName === "" || !isValidName || hasConflict |
| 143 | + |
| 144 | + return ( |
| 145 | + <div className="h-100 w-100"> |
| 146 | + <h1 className="text-center fw-bold text-secondary mt-5" style={{ fontSize: "3rem", letterSpacing: "1px" }}> |
| 147 | + Evaluation Module |
| 148 | + </h1> |
| 149 | + |
| 150 | + <div className="mx-auto text-center my-4" > |
| 151 | + <Image className="text-center" src={myimage} alt="" style={{ height: "30px", width: "30px" }} /> |
| 152 | + </div> |
| 153 | + |
| 154 | + {/* Description of the Evaluation Module */} |
| 155 | + <div className="mx-auto text-center" style={{ maxWidth: "860px", marginBottom: "40px" }}> |
| 156 | + <h5 className="lh-lg" style={{ fontSize: "1.1rem" }}> |
| 157 | + The Evaluation Module is as a quality-check workspace that tests completed AI models on fresh data, |
| 158 | + uses interactive dashboards to explain how they make decisions, and flags unreliable predictions to |
| 159 | + ensure no patient group is left behind. |
| 160 | + </h5> |
| 161 | + </div> |
| 162 | + |
| 163 | + <div style={{ display: "flex", flexDirection: "vertical", flexGrow: "10", width: "100%", margin: "auto" }}> |
| 164 | + {/* Main Title and Subtitle */} |
| 165 | + <div className="h-100 w-100 d-flex justify-content-center align-items-center"> |
| 166 | + <Stack direction="horizontal" gap={4} className="w-100"> |
| 167 | + {/* Explainer Dashboard Card */} |
| 168 | + <Card className="flex-fill shadow-sm border-primary h-100 w-50 hover-border-success" style={{ cursor: "pointer" }}> |
| 169 | + <Card.Header className="bg-danger text-white d-flex align-items-center"> |
| 170 | + <TbZoom className="me-2" color="white"/> |
| 171 | + <h5 className="text-white mb-0">Explainer Dashboard</h5> |
| 172 | + </Card.Header> |
| 173 | + <Card.Body className="d-flex flex-column justify-content-center align-items-center p-4"> |
| 174 | + <MdDashboard className="me-6" size={100} color="red"/> |
| 175 | + <Card.Text className="mt-3 text-center"> |
| 176 | + Build interactive dashboards to analyze and explain the predictions of your models. |
| 177 | + </Card.Text> |
| 178 | + <div |
| 179 | + className="p-inputgroup w-full my-3" |
| 180 | + style={{ margin: "5px", fontSize: "1rem", marginTop: "20px", maxWidth: "300px" }} |
| 181 | + > |
| 182 | + <InputText placeholder="Session Name" value={nameEval} onChange={(e) => setNameEval(e.target.value)} /> |
| 183 | + <span className="p-inputgroup-addon">.medeval</span> |
| 184 | + </div> |
| 185 | + <Button variant="danger" onClick={() => createSceneContent(trimmedName)} disabled={isStartDisabled}> |
| 186 | + Start Evaluation |
| 187 | + </Button> |
| 188 | + |
| 189 | + {nameError && ( |
| 190 | + <div className="text-danger small mt-1">{nameError}</div> |
| 191 | + )} |
| 192 | + </Card.Body> |
| 193 | + </Card> |
| 194 | + |
| 195 | + {/* MED3pa Card */} |
| 196 | + <Card className="flex-fill shadow-sm border-success h-100 w-50" style={{ cursor: "pointer" }}> |
| 197 | + <Card.Header className="bg-success text-white d-flex align-items-center"> |
| 198 | + <FaAlignJustify className="me-2" color="white"/> |
| 199 | + <h5 className="text-white mb-0">MED3pa</h5> |
| 200 | + </Card.Header> |
| 201 | + <Card.Body className="d-flex flex-column justify-content-center align-items-center p-4"> |
| 202 | + <Image src={med3paLogo} alt="MED3pa" width={120} height={120} /> |
| 203 | + <Card.Text className="mt-3 text-center"> |
| 204 | + Evaluate models stability and performance and flag unreliable predictions to ensure no patient group is left behind. |
| 205 | + </Card.Text> |
| 206 | + <Button variant="success" onClick={(e) => choosePage(e, "MED3pa")}> |
| 207 | + Start Analysis |
| 208 | + </Button> |
| 209 | + </Card.Body> |
| 210 | + </Card> |
| 211 | + </Stack> |
| 212 | + </div> |
| 213 | + </div> |
| 214 | + </div> |
| 215 | + ) |
| 216 | +} |
0 commit comments