+
    ¤ˆkc  ã                   ó€   € R t ^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIHtH	t	H
t
 ^ RIHtHt R t]R8X  d
   ]! 4        R# R# )zMPrepare a visual proposal-matching page using an independent reference field.N)ÚPath)ÚImageÚ	ImageDrawÚ
ImageChops)ÚdigestÚinsidec            #      ó\-  aC€ \         P                  ! \        R 7      p V P                  R\        RR7       V P                  R\        RR7       V P                  R\        RR7       V P                  R\        RR7       V P                  R\        RR7       V P                  4       p\        P                  ! VP                  P                  4       4      p\        P                  ! VP                  P                  4       4      p\        P                  ! VP                  P                  4       4      qAP                  R,          p\        P                  ! VP                  4       4      pVP                  R	4      R
8w  g©   VP                  R	4      R8w  g“   VP                  R4      \        VP                  4      8w  gj   VP                  R4      \        V4      8w  gK   VP                  R4      VP                  R4      8w  g&   VP                  R4      VP                  R4      8w  d   \        R4      hVP                  R4      pVe>   \!        V\"        4      '       d   V'       d   WwP%                  4       8w  d   \        R4      hVP                  R. 4       Uu. uF+  qˆP                  R4      VP                  R4      8X  g   K)  VNK-  	  p	p\'        V	4      ^8w  Eg:   VP                  R4      V	^ ,          P                  R4      8w  Eg   VP                  R	4      R8w  g÷   VP                  R4      \        VP                  4      8w  gÎ   VP                  R4      VP                  R4      8w  g©   VP                  R4      VP                  R4      8w  g„   VP                  R4      R8w  gn   VP                  R4      R8w  gX   \!        VP                  R4      \"        4      '       d3   VR,          P%                  4       '       d   VP                  R4      RJd   \        R4      h\)        VP                  VR ,          4      p
\        V
4      VP                  R!4      8w  g&   VP                  R!4      VP                  R!4      8w  d   \        R"4      h\*        P,                  ! V
4      ;_uu_ 4       q»P/                  R#4      pRRR4       VR$,          oCXP0                  SCSC38w  d   \        R%4      hVP                  R&,          p\        P                  ! VP                  4       4      p/ p/ pVP                  R	4      R'8w  gC   VP                  R4      VR,          8w  g&   \!        VP                  R(4      \2        4      '       g   \        R)4      hVP                  R*4      pVe‘   \!        V\4        4      '       dp   VP                  R	4      R+8w  gZ   VP                  R,4      \        V4      8w  g;   VP                  R4      VR,          8w  g   VP                  R!4      VR!,          8w  d   \        R-4      hVR(,           EF2  pVP                  R.4      p\7        V4      \8        Jg   V^ 8  g   VV9   d   \        R/4      h\)        VP                  VR0,          4      p\        V4      VP                  R14      8w  d   \        R24      h\*        P,                  ! V4      ;_uu_ 4       pVP/                  R#4      pVP0                  VP0                  8w  g:   VVR.,          8X  d7   \:        P<                  ! VV4      P?                  4       '       d   \        R34      hVVV&   \        V4      VV&   RRR4       EK5  	  VR.,          V9  d   \        R44      hVP                  R54      pVP                  RR64      pR7V9   d   R80M	\A        4       R9V9   d   R:R&0M	\A        4       ,          R;V9   d   R<0M	\A        4       ,          p\!        V\4        4      '       d   \A        V4      V8w  d   \        R=4      hVPC                  4        F9  w  ppVRŒ9  g$   \        VP                  V,          4      V8w  g   K0  \        R>4      h	  VP                  R?4      pVP                  R@4      p\!        V\2        4      '       d   \!        V\2        4      '       g   \        RA4      h\A        4       pV EFU  p\!        V\4        4      '       d   VP                  R4      MRp\!        V\4        4      '       d   VP                  RB4      MRp \!        V\4        4      '       d   VP                  R.4      MRp\!        V\"        4      '       d¥   V'       d�   VVP%                  4       8w  gˆ   VV9   g�   \!        V \2        4      '       dk   \'        V 4      ^8w  g[   \D        ;QJ d    VC3RC lV  4       F  '       g   K   RM	  RDM! VC3RC lV  4       4      '       g   \7        V4      \8        Jg   VV9  d   \        RE4      hVPG                  V4       EKX  	  \A        4       p!. p"\I        V^4       EFf  w  p#p$\!        V$\4        4      '       d   V$P                  R4      MRp\!        V$\4        4      '       d   V$P                  RF4      MRp%\!        V\"        4      '       d…   V'       d}   VVP%                  4       8w  gh   VV!9   ga   \!        V%\2        4      '       dK   \'        V%4      ^8w  g;   \D        ;QJ d    RG V% 4       F  '       g   K   RM	  RDM! RG V% 4       4      '       d   \        RH4      hV$P                  RIVR.,          4      p&\7        V&4      \8        Jg   V&V9  d   \        RJ4      hV$P                  RK4      p'V'VV&,          8w  d(   V&VR.,          8X  d   V'VR!,          8X  g   \        RL4      hV!PG                  V4       V%w  p(p)p*p+\K        ^ \L        PN                  ! V(4      4      p(\K        ^ \L        PN                  ! V)4      4      p)\Q        SC\L        PR                  ! V*4      4      p*\Q        SC\L        PR                  ! V+4      4      p+V(V*8¼  g   V)V+8¼  d   \        RM4      h\)        VP                  V$P                  RN4      4      p,\        V,4      V$P                  RO4      8w  d   \        RP4      h\*        P,                  ! V,4      ;_uu_ 4       q»P/                  R#4      p-RRR4       VV&,          PU                  V(V)V*V+34      p.X-P0                  V.P0                  8w  g,   \:        P<                  ! V-V.4      P?                  4       '       d   \        RQ4      hV$P                  RR4      '       d   \)        VP                  V$RR,          4      MRp/V/f   V$P                  RS4      e   \        RT4      hV/'       d+   \        V/4      V$P                  RS4      8w  d   \        RU4      hRVp0V/'       do   \*        P,                  ! V/4      ;_uu_ 4       p1V1P0                  SCSC38w  d   \        RW4      hV1P/                  RX4      P?                  4       '       d   RYMRZp0RRR4       V$P                  R[R\4      R\V039  d   \        R]4      hV"PW                  VV(V)V*V+3V/V&V034       EKi  	  VPX                  P[                  4       '       d5   \E        VPX                  P]                  4       4      '       d   \_        R^4      hVPX                  Pa                  RRR_7       . p2/ p3\c        VPC                  4       4       EFj  w  ppVPe                  4       p4\f        Ph                  ! V44      p5V" FQ  w  pp%  p6V5Pk                  V%R`^Ra7       V5Pm                  V%^ ,          ^,           V%^,          ^,           3VR`^RbRc7       KS  	  V F˜  pVR.,          V8w  d   K  VRB,          w  p7p8V5Po                  V7^,
          V8V7^,           V83Rd^Re7       V5Po                  V7V8^,
          V7V8^,           3Rd^Re7       V5Pm                  V7^,           V8^,           3VR,          Rd^RbRc7       Kš  	  RfVRg Rh2p9V4Pq                  VPX                  V9,          4       \        VPX                  V9,          4      V3V9&   EKm  	  \I        V"^4       EF„  w  p#w  pp%p/p&p0V%w  p(p)p*p+^0p:\K        ^ V(V:,
          4      \K        ^ V)V:,
          4      \Q        SCV*V:,           4      \Q        SCV+V:,           4      3p;Rp1V/'       dW   \*        P,                  ! V/4      ;_uu_ 4       p<V<P/                  RX4      p1RRR4       V1P0                  SCSC38w  d   \        RW4      h\c        VPC                  4       4       EFZ  w  ppVPU                  V;4      P/                  Ri4      p-V1es   VV&8X  dl   V1PU                  V;4      Ps                  Rj 4      p=\*        Pt                  ! RiV-P0                  R�4      p>V>Pw                  V=4       \*        Px                  ! V-V>4      p-\f        Ph                  ! V-4      p5V5Pk                  V(V;^ ,          ,
          V)V;^,          ,
          V*V;^ ,          ,
          V+V;^,          ,
          3R`^Ra7       V Fö  pVR.,          V8w  d   K  VRB,          w  p7p8V7V;^ ,          ,          p7V8V;^,          ,          p8^ T7u;8:  d   V-Pz                  8  g   KZ  M K^  ^ T8u;8:  d   V-P|                  8  g   Ky  M K}  V5Po                  V7^,
          V8V7^,           V83Rd^Re7       V5Po                  V7V8^,
          V7V8^,           3Rd^Re7       V5Pm                  V7^,           V8^,           3VR,          Rd^RbRc7       Kø  	  RkV#Rl RmVRg Rh2p9V-P/                  R#4      Pq                  VPX                  V9,          4       \        VPX                  V9,          4      V3V9&   EK]  	  V2PW                  Rn\~        P€                  ! V4       RoV& RpV0 RqV#Rl RrV#Rl RmVR.,          Rg RsV#^,
           Rt24       EK‡  	  R	RuR\        VP                  4      Rv\        VP                  4      Rw\        VP                  4      RVR,          RVR,          RxV" U7u. uF  p7V7^ ,          NK  	  up7R?V Uu. uF  pRVR,          R.VR.,          /NK  	  upRyVR.,          R,\        V4      R(\c        V4      RzT3RTR{V" U7u/ uF  p7V7^ ,          V7^,          bK  	  up7R|V" U7u/ uF  p7V7^ ,          V7^,          bK  	  up7/p?\        P‚                  ! V?RRŽR}7      P…                  R~R4      p@R€R6P‡                  V24      ,           pAVAP…                  R�VR.,          Rg 4      pATAP…                  R‚Rƒ\~        P€                  ! T;'       g    R„4      ,           R…,           4      pAR†pBVPX                  R‡,          P‰                  VAVBP…                  RˆX@4      ,           4       \‹        R‰\'        V4       RŠ\'        V4       R‹24       R# u upi   + '       g   i     EL‡; i  + '       g   i     EK)  ; i  + '       g   i     E	Lu; i  + '       g   i     EL; i  + '       g   i     EL@; iu up7i u upi u up7i u up7i )�)Údescriptionz
--protocolT)ÚtypeÚrequiredz--predictionsz--referencez--bundlez--outputzbundle.jsonÚformatzdiatom-frozen-field-protocol-v1z"diatom-frozen-field-predictions-v1Úprotocol_sha256Úbundle_sha256Ú	bundle_idÚcandidate_policyz'Prediction, protocol or bundle mismatchÚmask_rule_idNzInvalid frozen mask ruleÚfieldsÚidÚfield_idÚ	sample_idz%diatom-independent-field-reference-v1Úreference_statusÚexpert_adjudicatedÚreviewer_roleÚexpertÚreviewer_idÚ
exhaustivez0Reference declarations do not match frozen fieldÚfield_imageÚfield_image_sha256zField image hash mismatchÚRGBÚsizezInvalid field dimensionsúfocus-planes.jsonzdiatom-focus-planes-v1ÚplaneszInvalid focal-plane manifestÚdisplay_provenancezdiatom-reference-page-v1Úfocus_manifest_sha256z)Reference focal-plane provenance mismatchÚzzInvalid focal planeÚimageÚsha256zFocal plane hash mismatchz)Focal plane pixels or dimensions mismatchz'Detector plane missing from focal stackÚsource_manifest_sha256Ú Ú
_plus_tileútile-proposals.jsonÚ_plus_stackústack-proposals.jsonÚ_plus_splitúsplit-alternatives.jsonz"Invalid source manifest provenancez(Prediction source manifest hash mismatchÚ
referencesÚpredictionsz!Invalid references or predictionsÚfield_xyc              3   óà   <"  € T Fc  p\        V4      \        \        39  ;'       g?    \        P                  ! V4      '       * ;'       g    ^ Tu;8*  ;'       d    S8  Mu '       * x € Ke  	  R# 5i)é    N©r
   ÚintÚfloatÚmathÚisfinite)Ú.0Úxr   s   & €Ú(diatom_pilot/make_field_matching_page.pyÚ	<genexpr>Úmain.<locals>.<genexpr><   sN   øé € ÐfÑ`eÐ[\”4˜“7¤3¤u +Ñ-×VÐV´T·]²]À1Ó5EÔ1E×VÐVÈQÐPQÏYÌYÐRVÌYÌÔVÓ`eùs   ƒ#A.§"A.Á
A.ÁA.Fz'Invalid reference ID, position or planeÚboxc              3   ó˜   "  € T F@  p\        V4      \        \        39  ;'       g    \        P                  ! V4      '       * x € KB  	  R # 5i)Nr4   )r9   r:   s   & r;   r<   r=   B   s4   é € ÐSÉsÈ!”4˜“7¤3¤u +Ñ-×EÐE´T·]²]À1Ó5EÔ1EÔEËsùs
   ‚#A
¦$A
zInvalid prediction ID or boxÚplane_zzInvalid proposal source planeÚsource_plane_sha256z#Proposal source plane hash mismatchzInvalid proposal boundsÚcropÚsource_crop_sha256zProposal crop hash mismatchz)Proposal crop does not match source planeÚmaskÚsource_mask_sha256zMissing proposal maskzProposal mask hash mismatchÚmissingzInvalid mask dimensionsÚLÚnonemptyÚemptyÚmask_statusÚunknownzPrediction mask status mismatchzOutput directory must be empty)ÚparentsÚexist_okz#00a7ff)ÚoutlineÚwidthÚblack)ÚfillÚstroke_widthÚstroke_fillz#ffcc00)rQ   rO   zfield-overview-zÚ03dz.pngÚRGBAc                 ó&   € \        V R ,          4      # )gìQ¸…ëÑ?)Úround)r:   s   &r;   Ú<lambda>Úmain.<locals>.<lambda>k   s   € ´u¸Q¸s½U´|ó    z	proposal-Ú04dz-zz<section><h2>z!</h2><p>Proposal source plane: Z z; mask: z4.</p><img class="plane-image" data-prefix="proposal-z" src="proposal-zP.png" alt="Proposal and nearby reference points"><div class="match" data-index="z"></div></section>zdiatom-field-match-display-v1Úpredictions_sha256Úreference_sha256Úprediction_idsÚdisplay_plane_zÚasset_sha256Úmask_status_by_predictionÚproposal_plane_by_prediction)Úensure_asciiÚ
separatorsz</z<\/uÑ  <!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Match field proposals</title><style>body{font:16px/1.5 system-ui;color:#1c3835;max-width:1000px;margin:2em auto;padding:0 1em}section{border-top:1px solid #aaa;padding:1em 0}img{max-width:100%;height:auto}label{display:block;margin:.7em 0}select,input,button{font:inherit;padding:.3em}</style><h1>Match field proposals</h1><p>Reference points are yellow and appear only on their recorded plane; proposal boxes are blue and are approximate guides on other planes. Available masks are pink and appear only on their proposal source plane. The planes are not registered; consult the original stack when needed. Taxon predictions and reference names are hidden. Independent exhaustive reference annotation and a predetermined matching rule are required.</p><label>Focal plane <select id="focus"></select></label><img class="plane-image" data-prefix="field-overview" src="field-overview-zINITIALPLANE.png" alt="Full field, proposed boxes and independent reference points"><label>Matching reviewer ID <input id="reviewer"></label><label>Reviewer role <select id="role"><option value="unknown">Not specified</option><option value="nonexpert">Nonexpert / practice</option><option value="expert">Diatom expert</option></select></label><button id="recovery" hidden>Download browser recovery backup</button> <button id="download">Download matching decisions</button> <button id="download-progress">Download unfinished work</button> <button id="open">Open saved matches</button><input id="open-file" type="file" accept=".json,application/json" hidden> <span id="status" role="status">Ready.</span><p>Match every proposal or explicitly mark it unmatched. Reference specimens with no matched proposal remain misses. Decisions remain in this browser until downloaded. Download unfinished work to continue on another computer. Opening saved matches replaces this browserâ€™s current work for this field.</p>ÚINITIALPLANEz<p>Match every proposalz<p>Mask assessment rule: znot configuredzM. A nonempty mask is not automatically acceptable.</p><p>Match every proposaluT.  <script>"use strict";const payload=PAYLOAD,$=id=>document.getElementById(id),key='diatom-field-matching:'+payload.predictions_sha256+':'+payload.reference_sha256;let state,recoveryRaw=null,raw=null;function validStored(x){if(!x||typeof x.reviewer_id!=='string'||!['unknown','nonexpert','expert'].includes(x.reviewer_role)||!x.choices||typeof x.choices!=='object'||Array.isArray(x.choices)||canonical(x.display)!==canonical(payload))throw Error('Incompatible stored work');if(x.assessments===undefined)x.assessments={};if(x.merge_refs===undefined)x.merge_refs={};if(!validMergeMap(x.merge_refs))throw Error('Invalid stored merged references');if(!x.assessments||typeof x.assessments!=='object'||Array.isArray(x.assessments)||Object.entries(x.assessments).some(([id,v])=>!payload.prediction_ids.includes(id)||!['','acceptable','unacceptable','merged','unsure'].includes(v)))throw Error('Invalid stored mask assessments');for(const [id,v] of Object.entries(x.choices)){if(!payload.prediction_ids.includes(id)||typeof v!=='string'||(v&&!['u:duplicate','u:other','u:unknown'].includes(v)&&!payload.references.some((r,i)=>v==='r:'+i)))throw Error('Invalid stored choice')}return x}try{raw=localStorage.getItem(key)}catch(e){}if(raw!==null){try{state=validStored(JSON.parse(raw))}catch(e){recoveryRaw=raw}}if(!state)state={reviewer_id:'',reviewer_role:'unknown',choices:{},assessments:{},merge_refs:{},display:payload};function save(){if(recoveryRaw!==null)return;try{localStorage.setItem(key,JSON.stringify(state));$('status').textContent='Saved in this browser.'}catch(e){$('status').textContent='Browser storage unavailable; download decisions.'}}for(const z of payload.planes){const o=document.createElement('option');o.value=String(z);o.textContent='Z '+z+(z===payload.display_plane_z?' Â· detector plane':'');$('focus').append(o)}$('focus').value=String(payload.display_plane_z);$('focus').onchange=()=>{const z=Number($('focus').value);if(!payload.planes.includes(z))return;for(const image of document.querySelectorAll('.plane-image'))image.src=image.dataset.prefix+'-z'+String(z).padStart(3,'0')+'.png'};for(const [id,field] of [['reviewer','reviewer_id'],['role','reviewer_role']]){$(id).value=state[field]||(id==='role'?'unknown':'');$(id).oninput=$(id).onchange=()=>{state[field]=$(id).value;save()}}for(const c of document.querySelectorAll('.match')){const i=Number(c.dataset.index),id=payload.prediction_ids[i],label=document.createElement('label'),select=document.createElement('select');label.textContent='Match ';const options=[['','Not reviewed'],...payload.references.map((r,j)=>['r:'+j,'Reference '+r.id+' (Z '+r.z+')']),['u:duplicate','Unmatched: duplicate'],['u:other','Unmatched: other'],['u:unknown','Unmatched: unsure']];for(const [value,text] of options){const o=document.createElement('option');o.value=value;o.textContent=text;select.append(o)}select.value=state.choices[id]||'';const planeHint=document.createElement('p');function updatePlaneHint(){const ref=select.value.startsWith('r:')?payload.references[Number(select.value.slice(2))]:null,source=(payload.proposal_plane_by_prediction||{})[id]??payload.display_plane_z;planeHint.textContent=!ref?'':ref.z===source?'Reference and proposal were recorded on Z '+source+'. Mask quality still needs review.':'Reference was marked on Z '+ref.z+'; the proposal mask is from Z '+source+'. Planes are not registered; inspect the stack.'}select.onchange=()=>{state.choices[id]=select.value;updatePlaneHint();save()};label.append(select);c.append(label,planeHint);updatePlaneHint();const maskLabel=document.createElement('label'),maskSelect=document.createElement('select');maskLabel.textContent='Mask assessment under the frozen rule ';for(const [v,t] of [['','Not assessed'],['acceptable','Acceptable'],['unacceptable','Unacceptable'],['merged','Merged with another specimen'],['unsure','Unsure']]){const o=document.createElement('option');o.value=v;o.textContent=t;maskSelect.append(o)}maskSelect.value=state.assessments[id]||'';maskSelect.disabled=!payload.mask_rule_id||payload.mask_status_by_prediction[id]!=='nonempty';const extraLabel=document.createElement('label'),extra=document.createElement('select');extraLabel.textContent='Other reference specimens inside this merged mask (optional; use Ctrl/Command to select more than one) ';extra.multiple=true;extra.size=Math.min(5,Math.max(2,payload.references.length));for(const r of payload.references){const o=document.createElement('option');o.value=r.id;o.textContent=r.id+' (Z '+r.z+')';o.selected=(state.merge_refs[id]||[]).includes(r.id);extra.append(o)}extra.disabled=maskSelect.disabled||maskSelect.value!=='merged';extra.onchange=()=>{state.merge_refs[id]=Array.from(extra.selectedOptions,o=>o.value);save()};maskSelect.onchange=()=>{state.assessments[id]=maskSelect.value;extra.disabled=maskSelect.disabled||maskSelect.value!=='merged';save()};maskLabel.append(maskSelect);c.append(maskLabel);extraLabel.append(extra);c.append(extraLabel)}if(recoveryRaw!==null){for(const id of ['reviewer','role','download','download-progress'])$(id).disabled=true;for(const select of document.querySelectorAll('.match select'))select.disabled=true;$('recovery').hidden=false;$('status').textContent='Saved browser data is incompatible or damaged. Download a recovery backup, then open a valid saved match file to resume.'}$('recovery').onclick=()=>{if(recoveryRaw===null)return;const url=URL.createObjectURL(new Blob([recoveryRaw],{type:'application/json'})),link=document.createElement('a');link.href=url;link.download='matching-browser-recovery.json';link.click();setTimeout(()=>URL.revokeObjectURL(url),1000)};function canonical(x){if(Array.isArray(x))return '['+x.map(canonical).join(',')+']';if(x&&typeof x==='object')return '{'+Object.keys(x).sort().map(k=>JSON.stringify(k)+':'+canonical(x[k])).join(',')+'}';return JSON.stringify(x)}function validMergeMap(map){return !!map&&typeof map==='object'&&!Array.isArray(map)&&Object.entries(map).every(([id,list])=>payload.prediction_ids.includes(id)&&Array.isArray(list)&&new Set(list).size===list.length&&list.every(ref=>payload.references.some(r=>r.id===ref)))}function knownMaskAssessment(id,v){return payload.prediction_ids.includes(id)&&['','acceptable','unacceptable','merged','unsure'].includes(v)}function validateMatchFile(file){if(!file||!['diatom-field-matches-v1','diatom-field-match-progress-v1'].includes(file.format)||canonical(file.display)!==canonical(payload))throw Error('The field, references, predictions, or displayed images do not match');if(typeof file.reviewer_id!=='string'||!['unknown','nonexpert','expert'].includes(file.reviewer_role))throw Error('Invalid reviewer');if(file.format==='diatom-field-match-progress-v1'){if(!file.choices||typeof file.choices!=='object'||Array.isArray(file.choices))throw Error('Invalid unfinished work');const choices={};for(const [id,value] of Object.entries(file.choices)){if(!payload.prediction_ids.includes(id)||typeof value!=='string')throw Error('Unknown proposal or invalid choice');if(value&&!['u:duplicate','u:other','u:unknown'].includes(value)&&!payload.references.some((r,i)=>value==='r:'+i))throw Error('Invalid match choice');choices[id]=value}const merge_refs=file.merge_refs??{};if(!validMergeMap(merge_refs))throw Error('Invalid unfinished merged references');const assessments=file.assessments??{};if(!assessments||typeof assessments!=='object'||Array.isArray(assessments)||Object.entries(assessments).some(([id,v])=>!knownMaskAssessment(id,v)))throw Error('Invalid unfinished mask assessments');return {reviewer_id:file.reviewer_id,reviewer_role:file.reviewer_role,choices,assessments,merge_refs}}if(!Array.isArray(file.matches))throw Error('Invalid matching decisions');const choices={},assessments={},merge_refs={},used=new Set(),known=new Set(payload.prediction_ids);for(const m of file.matches){if(!m||typeof m.prediction_id!=='string'||!known.has(m.prediction_id)||Object.hasOwn(choices,m.prediction_id))throw Error('Unknown or duplicate proposal');let value;if(m.reference_id===null){if(!['duplicate','other','unknown'].includes(m.unmatched_type))throw Error('Invalid unmatched type');value='u:'+m.unmatched_type}else{const i=payload.references.findIndex(r=>r.id===m.reference_id);if(i<0||used.has(m.reference_id)||m.unmatched_type!=null)throw Error('Unknown or repeated reference match');used.add(m.reference_id);value='r:'+i}choices[m.prediction_id]=value;const assessment=m.mask_assessment??'not_reviewed';if(!['not_reviewed','acceptable','unacceptable','merged','unsure','unavailable','not_applicable'].includes(assessment))throw Error('Invalid mask assessment');if(['acceptable','unacceptable','merged','unsure'].includes(assessment)){if(!payload.mask_rule_id||payload.mask_status_by_prediction[m.prediction_id]!=='nonempty'||m.reference_id===null)throw Error('Mask assessment does not match the proposal');assessments[m.prediction_id]=assessment}const extra=m.merged_reference_ids??[];if(!validMergeMap({[m.prediction_id]:extra})||(extra.length&&(assessment!=='merged'||m.reference_id===null||extra.includes(m.reference_id))))throw Error('Invalid merged reference IDs');merge_refs[m.prediction_id]=extra}if(Object.keys(choices).length!==payload.prediction_ids.length)throw Error('Missing proposal matches');return {reviewer_id:file.reviewer_id,reviewer_role:file.reviewer_role,choices,assessments,merge_refs}}$('open').onclick=()=>$('open-file').click();$('open-file').onchange=async e=>{const file=e.target.files[0];e.target.value='';if(!file)return;try{const next=validateMatchFile(JSON.parse(await file.text()));if(!confirm('Replace this browserâ€™s current field matches? Download them first if you want to keep them.'))return;next.display=payload;localStorage.setItem(key,JSON.stringify(next));location.reload()}catch(err){$('status').textContent='Matches not opened: '+err.message}};$('download-progress').onclick=()=>{const result={format:'diatom-field-match-progress-v1',display:payload,reviewer_id:state.reviewer_id,reviewer_role:state.reviewer_role,choices:state.choices,assessments:state.assessments,merge_refs:state.merge_refs};const url=URL.createObjectURL(new Blob([JSON.stringify(result,null,2)],{type:'application/json'})),link=document.createElement('a');link.href=url;link.download='field-matching-progress.json';link.click();setTimeout(()=>URL.revokeObjectURL(url),1000)};$('download').onclick=()=>{const matches=[],used=new Set();for(const id of payload.prediction_ids){const v=state.choices[id]||'';if(!v){$('status').textContent='Match every proposal before downloading.';return}if(v.startsWith('r:')){const ref=payload.references[Number(v.slice(2))];if(!ref||used.has(ref.id)){$('status').textContent='A reference cannot be matched to multiple proposals; mark extra proposals as duplicates.';return}used.add(ref.id);const assessment=payload.mask_status_by_prediction[id]==='nonempty'?(payload.mask_rule_id?(state.assessments[id]||'not_reviewed'):'not_reviewed'):'unavailable',extra=assessment==='merged'?(state.merge_refs[id]||[]):[];if(extra.includes(ref.id)){$('status').textContent='Other references cannot include the primary matched reference.';return}matches.push({prediction_id:id,reference_id:ref.id,mask_assessment:assessment,merged_reference_ids:extra})}else if(['u:duplicate','u:other','u:unknown'].includes(v))matches.push({prediction_id:id,reference_id:null,unmatched_type:v.slice(2),mask_assessment:'not_applicable'});else{$('status').textContent='Invalid match.';return}}const result={format:'diatom-field-matches-v1',display:payload,reviewer_id:state.reviewer_id,reviewer_role:state.reviewer_role,matches};const url=URL.createObjectURL(new Blob([JSON.stringify(result,null,2)],{type:'application/json'})),link=document.createElement('a');link.href=url;link.download='field-matches.json';link.click();setTimeout(()=>URL.revokeObjectURL(url),1000)};</script></html>z
index.htmlÚPAYLOADzPrepared matching page for z predictions and z! declared independent references.>   r    r*   r,   r.   )éÿ   é@   éd   r3   )Ú,Ú:)FÚargparseÚArgumentParserÚ__doc__Úadd_argumentr   Ú
parse_argsÚjsonÚloadsÚprotocolÚ	read_textr0   Ú	referenceÚbundleÚgetr   Ú
ValueErrorÚ
isinstanceÚstrÚstripÚlenr   r   ÚopenÚconvertr   ÚlistÚdictr
   r5   r   Ú
differenceÚgetbboxÚsetÚitemsÚanyÚaddÚ	enumerateÚmaxr7   ÚfloorÚminÚceilrB   ÚappendÚoutputÚexistsÚiterdirÚFileExistsErrorÚmkdirÚsortedÚcopyr   ÚDrawÚ	rectangleÚtextÚlineÚsaveÚpointÚnewÚputalphaÚalpha_compositerO   ÚheightÚhtmlÚescapeÚdumpsÚreplaceÚjoinÚ
write_textÚprint)DÚpÚars   ÚpredÚrefÚbundle_filerv   Ú	mask_ruleÚfr   Ú
image_pathÚimÚfieldÚ
focus_fileÚfocusr!   Úplane_hashesÚdisplayÚplaner$   ÚpathÚframeÚ	manifestsÚpolicyÚexpected_namesÚnameÚexpected_hashr/   r0   Úref_idsÚrÚidentr™   ÚidsÚassetsÚiÚitemr>   r@   Úsource_hashÚx0Úy0Úx1Úy1Ú	crop_pathrB   ÚexpectedÚ	mask_pathrJ   rD   ÚcardsÚasset_hashesÚfullÚdrawÚ_r:   ÚyÚfilenameÚpadÚregionÚmÚalphaÚoverlayÚpayloadÚembeddedÚpageÚjsr   sD                                                                      @r;   ÚmainrÚ      sú  ø€ Ü×Ò¬'Ô2€AØ‡N�N�<¤T°4€NÔ8¸¿¹ÈÔ]aÐko¸Ô9pØ‡N�N�=¤d°D€NÔ9¸!¿.¹.ÈÔY]Ðgk¸.Ô:lÐmn×m{Ñm{ð  }Gô  MQð  [_Ðm{ô  n`Ø‡l�lƒn€AœdŸjšj¨¯©×)=Ñ)=Ó)?Ó@�XÄdÇjÂjÐQR×Q^ÑQ^×QhÑQhÓQjÓFkÀÔpt×pzÒpzÐ{|÷  |Gñ  |G÷  |Qñ  |Qó  |Só  qTÐlo÷  aiñ  aið  jwõ  awð  U`ô  C÷  Iò  Ið  JU÷  J_ñ  J_ó  Jaó  bð  x~Ø�‰�XÓÐ AÔAÀTÇXÁXÈhÓEWÐY}ÔE}Ø�8‰8Ð%Ó&¬¨q¯z©zÓ(:Ô:¸d¿h¹hÀÓ>WÔY_Ð`kÓYlÔ>lØ�8‰8�KÓ  &§*¡*¨[Ó"9Ô9¸T¿X¹XÐFXÓ=YÐ[c×[gÑ[gÐhzÓ[{Ô={ô  DNð  Oxó  Dyð  ~yØ�l‰l˜>Ó*€IØÒ¤j°¼3×&?Ò&?ÇyÐT]×_nÑ_nÓ_pÔTpô  yCð  D^ó  y_ð  s_Ø—|‘| H¨RÔ0ÓVÑ0�!·E±E¸$³KÀÇÁÈ*ÓAUÑ4U�AˆAÑ0€FÐVÜˆF‹�Q�˜$Ÿ(™( ;Ó/°¸µ·±¸{Ó1KÕKÈsÏwÉwÐW_ÓO`ð  cJô  PJØ�7‰7Ð$Ó%¤v¨a¯j©jÓ'9Ô9¸S¿W¹WÀZÓ=PÐRV×RZÑRZÐ[eÓRfÔ=fÐjm×jqÑjqÐr}Ój~ð  AE÷  AIñ  AIð  JUó  AVô  kVØ�7‰7Ð%Ó&Ð(<Ô<ÀÇÁÈÓ@XÐZbÔ@bÜ˜#Ÿ'™' -Ó0´×5Ò5¸SÀÕ=O×=UÑ=U×=WÒ=WÐ[^×[bÑ[bÐcoÓ[pÐx|Ó[|ô  EOð  PBó  ECð  CÜ�a—h‘h˜v mÕ4Ó5€JÜˆjÓ˜4Ÿ8™8Ð$8Ó9Ô9¸S¿W¹WÐEYÓ=ZÐ\`×\dÑ\dÐeyÓ\zÔ=zô  BLð  Mhó  Bið  |iÜ	�Š�J×	Ô	 2¯J©J°uÓ,= e×	Ø	��€DØ‡z�z�D˜�;Ô¤ZÐ0JÓ%KÐKØ�x‰xÐ+Õ+€J´$·*²*¸Z×=QÑ=QÓ=SÓ2T¨EÐ\^ÐU[ÐlnÐ_kØ‡y�y�ÓÐ4Ô4¸¿	¹	À+Ó8NÐPVÐWbÕPcÔ8cÔkuÐv{×vÑvð  AIó  wJô  KO÷  lPò  lPô  Wað  b@ó  WAð  QAØ�G‰GÐ(Ó)€GØÒ¤J¨w´t×$<Ò$<ÀÇÁÈHÓ@UÐWqÔ@qØ�;‰;Ð.Ó/´¸
Ó1CÔCÀwÇ{Á{ÐS^ÓG_ÐagÐhsÕatÔGtØ�;‰;Ð+Ó,¨dÐ3GÕ.HÔHÔPZð  \Gó  QHð  KHØ�x—‘ˆØ
�)‰)�C‹.ˆÜ�‹7œ#Ó  1¤¨¨V¬¼*ÐEZÓ:[Ð4[Ü�A—H‘H˜U 7�^Ó,ˆÜ�$‹<˜Ÿ™ 8Ó,Ô,´:Ð>YÓ3ZÐ-ZÜ�ZŠZ˜×Ô Ø—*‘*˜UÓ#ˆEØ�z‰z˜5Ÿ:™:Ô%¨!¨V°C­[¬.¼Z×=RÒ=RÐSXÐY^Ó=_×=gÑ=g×=iÒ=iÔq{ð  }hó  rið  liØˆF�1‰IÜ" 4›LˆL˜‰O÷	 Òñ !ð ˆc…{˜&Ô ¤zÐ2[Ó'\Ð!\Ø�h‰hÐ/Ó0€I¸¿¹ÐASÐTVÓ8W°Ø/;¸vÔ/EÐ*Ñ+Ì3Ë5ð  DQð  U[ô  D[ÐTjÐk~ÑSô  adó  afõ  gð  HUð  Y_ô  H_ð  jCñ  iDô  ehó  ejõ  k€NÜ�i¤×%Ò%¬¨Y«¸Ô)GÌjÐY}ÓN~ÐH~Ø'Ÿo™oÖ/Ñˆˆ]ØÐsÔsÔw}Ð~÷  Gñ  Gð  HLõ  Ló  xMð  O\ö  x\ÜÐGÓHÐHñ 0ð �w‰w�|Ó$€J°·±¸-Ó1H [Ü�j¤×&Ò&¬j¸ÄT×.JÒ.JÔQ[Ð\ó  RAð  LAÜ‹E€GÜˆÜ'¨¬$×/Ò/ˆa�e‰e�DŒk°TˆÔU_Ð`aÔbf×UgÒUgÀÇÁÀjÔ@QÐmq¸%ô  CMð  NOô  PT÷  CUò  CUÐtu×tyÑtyÐz}Ôt~ð  [_ÐrsÜ˜5¤×%Ò%¯U°e¸U¿[¹[»]Ô6JÈeÐW^ÔN^ÔfpÐqvÔw{×f|Òf|ô  ADð  EJó  AKð  MNô  ANß‹sÔfÑ`eÓf�s�sŠsÔfÑ`eÓf×fÒfÔjnÐopÓjqÔy|Ój|ð  ABð  JPô  APô  Xbð  cLó  XMð  RMØ�‰�E×ñ	 ô
 	‹€C�RˆfÜ˜K¨×*‰ˆˆ$Ü *¨4´× 5Ò 5ˆd�h‰h�tŒn¸4ˆÔWaÐbfÔgk×WlÒWlÀDÇHÁHÈUÄOÐrvÀÜ˜5¤×%Ò%¯U°e¸U¿[¹[»]Ô6JÈeÐWZÌlÔblÐmpÔqu×bvÒbvÔz}ð  Bó  {Cð  EFô  {Fß‹sÑSÉsÓS�s�sŠsÑSÉsÓS×SÒSÔ[eð  gEó  \Fð  VFØ—‘˜ 6¨#¥;Ó/ˆÜ�‹=¤Ó# w°fÔ'<Ä:ÐNmÓCnÐ=nØ—H‘HÐ2Ó3ˆØ˜ WÕ-Ô-°wÀÀsÅÔ7KÐP[Ð]aÐbvÕ]wÔPwÜÐBÓCÐCØ�‰�Œ 3‘{�r˜"˜R Üˆq”—’˜B“Ó ˆ¤C¨¬$¯*ª*°R«.Ó$9 ¼SÀÄdÇiÂiÐPRÃmÓ=T¸"ÔX[Ð\`Ôae×ajÒajÐkmÓanÓXoÐUWØˆrŒ6�R˜”V¤*Ð-FÓ"GÐGÜ˜Ÿ™ $§(¡(¨6Ó"2Ó3ˆ	Ü�)Ó˜dŸh™hÐ';Ó<Ô<Ä:ÐNkÓClÐ=lÜ�ZŠZ˜	×"Ô" b¯j©j¸Ó.?¨×"Ø˜•×%Ñ% r¨"¨R° mÓ4ˆØ�9‰9�h—m‘mÔ#¤z×'<Ò'<¸TÀ(Ó'K×'SÑ'S×'UÒ'UÔ\fð  hSó  ]Tð  WTØ37·8±8¸F×3CÒ3C”&˜Ÿ™ $ v¥,Ô/Èˆ	ØÒ §¡Ð*>Ó!?Ò!KÔR\Ð]tÓRuÐLußœ 	Ó*¨D¯H©HÐ5IÓ,JÔJÔQ[Ð\yÓQzÐKzØˆßÜ—’˜I×&Ô&¨$Ø—9‘9˜t D˜kÔ)´
Ð;TÓ0UÐ*UØ*.¯,©,°sÓ*;×*CÑ*C×*EÒ*E™JÈ7�÷ 'ð �8‰8�M )Ó,°Y¸{Ð4KÔKÔR\Ð]~ÓRÐLØ�‰�u˜b  B r˜]¨9°W¸[ÐI×Jñ5 +ð6 	‡x�x‡�×ÒœS §¡×!1Ñ!1Ó!3×4Ò4¼?ÐKkÓ;lÐ5lØ‡H�H‡N�N˜4¨€NÔ.°R¨uÀR¸Ü˜&Ÿ,™,›.×)‰ˆˆ%Ø�Z‰Z‹\ˆœyŸ~š~¨dÓ3˜$Û%‰OˆE�#�a˜˜! d§n¡n°SÀÐQR nÔ&SÐTX×T]ÑT]Ð_bÐcdÕ_eÐfgÕ_gÐhkÐlmÕhnÐopÕhpÐ^qÐrwð  ~Gð  UVð  cjÐT]ö  Uk™vÛˆAØ��v�qŒy™Ø�*•‰CˆAˆa˜dŸi™i¨¨1­¨Q¨q°­s°1¨¸9È1˜iÔMÈdÏiÉiÐYZÐ[\Ð]^Õ[^Ð_`ÐabÐcdÕadÐXeÐktÐ{|ÈiÔN}ð  C÷  Hñ  Hð  JKð  LMõ  JMð  NOð  PQõ  NQð  IRð  STð  UYõ  SZð  `ið  wxð  ELð  Hö  Mñ ð $ A c 7¨$Ð/ˆ°·	±	¸!¿(¹(À8Õ:KÔ0LÔdjÐkl×ksÑksÐt|Õk|Ód}È\ÐZbÔMcñ *ô 8AÀÈ×7JÑ3ˆÑ
3ˆ5��Y˜w {Ø‰ˆˆ2ˆb�˜B˜¤s¨1¨R°­V£}´S¸¸2¸c½6³]Ä3ÀtÈBÈsÍFÓCSÔTWÐX\Ð]_Ð`cÕ]cÓTdÐ&e˜vÐkoÐfjßÜ—’˜I×&Ô&¨!°·±¸3³¨D×&Ø�y‰y˜4 ˜+Ô%¬JÐ7PÓ,QÐ&QÜ˜fŸl™l›n×-‰GˆAˆeØ—‘˜FÓ#×+Ñ+¨FÓ3ˆDØÒ A w¤JØ—i‘i Ó'×-Ñ-Ñ.CÓD�ÌUÏYÊYÐW]Ð^b×^gÑ^gÐhvÓMwÀWÐx÷  yIñ  yIð  JOô  yPô  V[÷  Vkò  Vkð  lpð  qxó  Vyð  QUÜ—’ Ó%ˆD d§n¡n°b¸À½µlÀ2ÀfÈQÅiÅ<ÐPRÐSYÐZ[ÕS\ÕP\Ð]_Ð`fÐghÕ`iÕ]iÐ5jÐs|ð  DE nô  'FÛ�Ø�S•6˜1”9™XØ�j•M‘��! ! V¨A¥Y¥, !¨q°&¸µ)­|¨qØ�a–?˜Ÿ
™
—?’? q¨!Ö'7¨D¯K©K×'7Ò'7Ø—I‘I˜q �s 1 Q q¥S¨˜m°À�IÔCÀDÇIÁIÈqÐQRÐSTÕQTÐUVÐWXÐYZÕWZÈmÐajÐqrÀIÔDsÐtx×t}Ñt}ð  @Að  BCõ  @Cð  DEð  FGõ  DGð  Hð  IJð  KOõ  IPð  V_ð  mnð  {BÐt}ö  uCñ	  ð
 !  3  r¨!¨C¨°Ð5ˆH°d·l±lÀ5Ó6I×6NÑ6NÈqÏxÉxÐX`ÕO`Ô6aÔyð  AB÷  AIñ  AIð  JRõ  ARó  zSÐbnÐowÔbxñ .ð 	�‰�}¤T§[¢[°Ó%7Ð$8Ð8YÐZaÐYbÐbjÐkvÐjwð  xlð  mnð  orð  lsð  sCð  DEð  FIð  CJð  JLð  MSð  TWõ  MXð  Y\ð  L]ð  ]mð  noð  pqõ  nqð  mrð  rDð  E÷  	Fñ! 8Kð" Ð5Ð6GÌÈqÏzÉzÓHZÐ[oÔpvÐwx÷  xEñ  xEó  qFð  GYô  Z`ð  ab÷  alñ  aló  ZmØ˜˜ZÕ(¨°T¸+Õ5FÐGWÑgmÓXnÑgmÐbcÐYZÐ[\×Y]ÐY]ÑgmÑXnØ¹jÓI¹j¸˜D  4¥¨¨Q¨s­VÓ4¹jÑIÐJ[Ð\bÐcfÕ\gØ$¤V¨JÓ%7¸ÄÈÃÐP^Ð_kØ˜IÐ&AÑV\ÓB]ÑV\ÐQRÀ1ÀQÅ4ÈÈ!ÍÂ9ÑV\ÑB]Ø+ÁÓ,GÁ¸1¨Q¨q­T°!°Aµ$ªYÁÑ,GðI€Gô �ZŠZ˜¨T¸YÔG×OÑOÐPTÐU[Ó\€Hð 
að  bd÷  biñ  bið  joó  bpõ  
p€DØ	�‰�n¨¨s­°CÐ'8Ó	:€DØ	�‰Ð/Ð1LÌTÏ[Ê[ÐYb×YvÐYvÐfvÓMwÕ1wð  yHõ  2Hó  
I€Dð by€BØ‡X�XˆlÕ×&Ñ& t¨B¯J©J°yÀÓ,JÕ'JÔKÌEÐToÔpsÐtó  qAð  pBð  BSô  TWð  Xbó  Tcð  Sdð  dEð  SFö  MGùòU W÷ 
 ×	Ð	ú÷ ×Ñú÷F #×"Ð"ú÷ '×&Ð&ú÷" '×&Ð&üò YoùÚIùâB]ùÚ,GsŠ   Ê&AX0Ê?AX0Ò&AX5ÛA%AY	Ü>AY	ó!AYøAAY2ùAY2ÁD=AZÁRAZÁR/AZ"ÁS6AZ$:ÁTAZ)>ÁX5AY	ÁY	AYÁYAY/ÁY2AZÁZAZÚ__main__)rn   rl   rž   rq   r7   Úpathlibr   ÚPILr   r   r   Úingest_reviewr   r   rÚ   Ú__name__© rZ   r;   Ú<module>rá      s9   ðÙ SÛ Û Û Û Ý ß *Ñ *ß 'òtGðn ˆZÔ™žÑrZ   