All files / app analysisMenu.tsx

98.57% Statements 69/70
63.63% Branches 14/22
100% Functions 40/40
100% Lines 59/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307                                                                                        88x                   88x           200x     252x 252x   252x 252x   252x 252x 22x 22x   88x 88x 88x 44x   44x     88x     22x   252x 22x 22x 22x   252x 62x   198x                           306x   450x     397x 397x 397x 396x 112x 5x   112x   112x                             199x   58x             198x   54x                                 199x   58x                           199x   58x                         202x   70x                             199x   58x                 199x   58x                 200x   62x                 200x   62x                                   201x   66x               200x   62x               201x   66x                                  
import {
  Box,
  Button,
  Checkbox,
  FormControl,
  FormControlLabel,
  InputLabel,
  List,
  ListItem,
  ListItemButton,
  ListItemIcon,
  ListItemText,
  ListSubheader,
  MenuItem,
  Select,
  Stack,
  Switch,
  TextField,
  Toolbar,
  Tooltip,
  Typography,
} from '@mui/material'
import {useContext, useEffect, useMemo, useReducer, useState} from 'react'
import {AllCategories, BuildContext, type NumberObject} from './building'
import type {FixedTerm} from './term'
import Results from './analysisResults'
 
export type TermEntry = {host?: string; term: string; categories: {[index: string]: number}; processed: FixedTerm}
 
export type ProcessOptions = {
  include_fuzzy: boolean
  dense: boolean
  min_sim: number
}
export type PlotOptions = {
  type: 'graph' | 'scatter' | 'distribution'
  layout: 'none' | 'force' | 'forceAtlas2' | 'circular'
  size_by_value: boolean
  hide_zeros: boolean
  label_threshold: number
  repulsion: number
  gravity: number
  edge_length: number
}
const plotOptions: PlotOptions = {
  type: 'graph',
  layout: 'force',
  size_by_value: true,
  hide_zeros: false,
  label_threshold: 0,
  repulsion: 100,
  gravity: 0.1,
  edge_length: 40,
}
const processOptions: ProcessOptions = {
  include_fuzzy: false,
  dense: false,
  min_sim: 0.001,
}
function updateOptions<T>(state: T, action: {key: keyof T; value: boolean | string | number}) {
  return {...state, [action.key]: action.value}
}
export default function AnalyzeMenu() {
  const dict = useContext(BuildContext)
  const allCategories = useContext(AllCategories)
 
  const [plotOpts, setPlotOpts] = useReducer(updateOptions<PlotOptions>, plotOptions)
  const [procOpts, setProcOpts] = useReducer(updateOptions<ProcessOptions>, processOptions)
 
  const [selected, setSelected] = useState<string[]>([])
  const {allTerms, catCounts} = useMemo(() => {
    const catCounts: NumberObject = {}
    const allTerms = new Map(
      Object.keys(dict).map(id => {
        const termCats = Object.keys(dict[id].categories)
        ;(termCats.length ? termCats : ['no categories']).forEach(cat => {
          if (cat in catCounts) {
            catCounts[cat]++
          } else {
            catCounts[cat] = 1
          }
        })
        return [dict[id].term || id, dict[id]]
      })
    )
    return {catCounts, allTerms}
  }, [dict])
  const categories = useMemo(() => {
    const out = [...allCategories]
    Iif ('no categories' in catCounts && !out.includes('no categories')) out.push('no categories')
    return out
  }, [allCategories, catCounts])
  useEffect(() => setSelected(selected => selected.filter(cat => categories.includes(cat))), [categories])
  return (
    <Stack direction="row" sx={{height: '100%'}}>
      <Box
        sx={{
          height: '100%',
          minWidth: '250px',
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'space-between',
        }}
      >
        <Box sx={{pl: 1, pr: 1, height: '100%', display: 'flex', flexDirection: 'column'}}>
          <Box sx={{overflowY: 'auto'}}>
            <Typography variant="h6">Terms</Typography>
            <List dense sx={{overflowY: 'auto', overflowX: 'hidden', maxHeight: '300px', p: 0}}>
              {Object.keys(catCounts).map(cat => (
                <ListItem key={cat} disablePadding disableGutters>
                  <ListItemButton
                    disableGutters
                    aria-label="toggle category"
                    onClick={() => {
                      const newSelection = [...selected]
                      const index = newSelection.indexOf(cat)
                      Iif (index === -1) {
                        newSelection.push(cat)
                      I} else {
                        newSelection.splice(index, 1)
                      }
                      setSelected(newSelection)
                    }}
                  >
                    <ListItemIcon sx={{minWidth: '35px'}}>
                      <Checkbox sx={{p: 0}} checked={selected.includes(cat)} />
                    </ListItemIcon>
                    <ListItemText sx={{m: 0}} primary={cat} secondary={catCounts[cat] + ' terms'}></ListItemText>
                  </ListItemButton>
                </ListItem>
              ))}
            </List>
            <Toolbar sx={{justifyContent: 'space-between'}} variant="dense" disableGutters>
              <Stack direction="row">
                <Button
                  size="small"
                  sx={{minWidth: '1px'}}
                  onClick={() => {
                    setSelected([...categories])
                  }}
                >
                  All
                </Button>
                <Button
                  size="small"
                  sx={{minWidth: '1px'}}
                  onClick={() => {
                    setSelected([])
                  }}
                >
                  None
                </Button>
              </Stack>
              <Typography sx={{pr: 1, whiteSpace: 'nowrap'}}>
                {selected.length + ' / ' + categories.length + ' selected'}
              </Typography>
            </Toolbar>
            <Stack spacing={2}>
              <Tooltip title="Include all matches to fuzzy terms in the comparison." placement="right">
                <FormControlLabel
                  label="Fuzzy Matches"
                  labelPlacement="start"
                  control={
                    <Switch
                      size="small"
                      checked={procOpts.include_fuzzy}
                      onChange={() => setProcOpts({key: 'include_fuzzy', value: !procOpts.include_fuzzy})}
                    />
                  }
                />
              </Tooltip>
              <Tooltip
                title="Include second-order term relationships in similarity calculations, resulting in a denser network."
                placement="right"
              >
                <FormControlLabel
                  label="Secondary Connections"
                  labelPlacement="start"
                  control={
                    <Switch
                      size="small"
                      checked={procOpts.dense}
                      onChange={() => setProcOpts({key: 'dense', value: !procOpts.dense})}
                    />
                  }
                />
              </Tooltip>
              <Tooltip
                title="Will consider nodes with similarity equal to or less than this as unconnected."
                placement="right"
              >
                <TextField
                  value={procOpts.min_sim}
                  type="number"
                  size="small"
                  inputProps={{min: 0, max: 1, step: 0.01}}
                  label="Similarity Threshold"
                  onChange={e => setProcOpts({key: 'min_sim', value: e.target.value})}
                ></TextField>
              </Tooltip>
            </Stack>
          </Box>
          <Box sx={{overflowY: 'auto'}}>
            <Typography variant="h6" sx={{mt: 2}}>
              Plot Options
            </Typography>
            <Stack spacing={2}>
              <FormControlLabel
                label="Hide Zeros"
                labelPlacement="start"
                control={
                  <Switch
                    size="small"
                    checked={plotOpts.hide_zeros}
                    onChange={() => setPlotOpts({key: 'hide_zeros', value: !plotOpts.hide_zeros})}
                  />
                }
              />
              <FormControlLabel
                label="Size By Value"
                labelPlacement="start"
                control={
                  <Switch
                    size="small"
                    checked={plotOpts.size_by_value}
                    onChange={() => setPlotOpts({key: 'size_by_value', value: !plotOpts.size_by_value})}
                  />
                }
              />
              <Tooltip title="Will not show labels for nodes with values lower than this." placement="right">
                <TextField
                  value={plotOpts.label_threshold}
                  type="number"
                  size="small"
                  label="Label Threshold"
                  inputProps={{min: 0, step: 0.5}}
                  onChange={e => setPlotOpts({key: 'label_threshold', value: e.target.value})}
                ></TextField>
              </Tooltip>
              <FormControl fullWidth>
                <InputLabel id="graph_layout_select">Layout</InputLabel>
                <Select
                  labelId="graph_layout_select"
                  label="Layout"
                  size="small"
                  value={plotOpts.layout || 'force'}
                  onChange={e => {
                    setPlotOpts({key: 'layout', value: e.target.value})
                  }}
                >
                  <ListSubheader>Canvas</ListSubheader>
                  <MenuItem value="none">Random</MenuItem>
                  <MenuItem value="circular">Circular</MenuItem>
                  <MenuItem value="force">Force</MenuItem>
                  <ListSubheader>WebGL</ListSubheader>
                  <MenuItem value="forceAtlas2">ForceAtlas2</MenuItem>
                </Select>
              </FormControl>
              {plotOpts.layout === 'force' && (
                <>
                  <Tooltip title="Repulsion factor between nodes." placement="right">
                    <TextField
                      value={plotOpts.repulsion}
                      type="number"
                      size="small"
                      label="Repulsion"
                      inputProps={{min: 0}}
                      onChange={e => setPlotOpts({key: 'repulsion', value: e.target.value})}
                    ></TextField>
                  </Tooltip>
                  <Tooltip title="Nodes' strength of attraction to the center." placement="right">
                    <TextField
                      value={plotOpts.gravity}
                      type="number"
                      size="small"
                      label="Gravity"
                      inputProps={{min: 0, step: 0.1}}
                      onChange={e => setPlotOpts({key: 'gravity', value: e.target.value})}
                    ></TextField>
                  </Tooltip>
                  <Tooltip title="Base distance between nodes." placement="right">
                    <TextField
                      value={plotOpts.edge_length}
                      type="number"
                      size="small"
                      label="Edge Length"
                      inputProps={{min: 0}}
                      onChange={e => setPlotOpts({key: 'edge_length', value: e.target.value})}
                    ></TextField>
                  </Tooltip>
                </>
              )}
            </Stack>
          </Box>
        </Box>
      </Box>
      <Results
        allTerms={allTerms}
        catCounts={catCounts}
        selectedCategories={selected}
        options={procOpts}
        plotOptions={plotOpts}
      />
    </Stack>
  )
}