Publish source-linked engineering workbench with verified RST and original UR7e data
6d15d6c verified Download app.js from devkumarhf/Engineering-Workbench: direct link, hf CLI and curl.
- Browser
- Download file 9.84 kB
-
https://huggingface.co/spaces/devkumarhf/Engineering-Workbench/resolve/main/app.js
- Command line
-
hf download hf://spaces/devkumarhf/Engineering-Workbench/app.js
-
curl -L -o app.js https://huggingface.co/spaces/devkumarhf/Engineering-Workbench/resolve/main/app.js
9.84 kB
| import {design,response,rollingMean} from './control.js'; | |
| const $=s=>document.querySelector(s),all=s=>[...document.querySelectorAll(s)]; | |
| const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(cls)n.className=cls;return n;}; | |
| function link(label,url){const a=el('a',label);a.href=url;a.target='_blank';a.rel='noopener';return a;} | |
| let projects=[],training,currentProject,currentTab='studies'; | |
| function setTab(id){if(!['studies','control','data'].includes(id))id='studies';currentTab=id;all('[role=tabpanel]').forEach(p=>p.hidden=p.id!==id);all('[role=tab]').forEach(b=>{b.setAttribute('aria-selected',String(b.dataset.tab===id));b.tabIndex=b.dataset.tab===id?0:-1;});history.replaceState(null,'','#'+id);requestAnimationFrame(()=>{if(id==='control')renderControl();if(id==='data')renderData();});} | |
| all('[role=tab]').forEach((b,i)=>{b.addEventListener('click',()=>setTab(b.dataset.tab));b.addEventListener('keydown',e=>{if(['ArrowLeft','ArrowRight','Home','End'].includes(e.key)){e.preventDefault();const tabs=all('[role=tab]'),j=e.key==='Home'?0:e.key==='End'?2:(i+(e.key==='ArrowRight'?1:2))%3;setTab(tabs[j].dataset.tab);tabs[j].focus();}});}); | |
| $('.brand').addEventListener('click',()=>setTab('studies')); | |
| function renderProjects(){const q=$('#search').value.toLowerCase().trim(),domain=$('#domain').value;const filtered=projects.filter(p=>(domain==='all'||p.domain===domain)&&[p.title,p.tags,p.question,p.method,p.evidence,p.authors].join(' ').toLowerCase().includes(q));$('#project-count').textContent=filtered.length+' / '+projects.length+' projects';$('#empty').hidden=!!filtered.length;$('.project-layout').hidden=!filtered.length;$('#project-list').replaceChildren();for(const p of filtered){const b=el('button',undefined,'project-item');b.type='button';b.dataset.project=p.id;b.setAttribute('aria-pressed',String(currentProject===p.id));b.append(el('span',String(projects.indexOf(p)+1).padStart(2,'0'),'project-number'),el('small',p.domain),el('strong',p.title));b.addEventListener('click',()=>showProject(p));$('#project-list').append(b);}if(filtered.length)showProject(filtered.find(p=>p.id===currentProject)||filtered[0]);} | |
| function showProject(p){currentProject=p.id;all('.project-item').forEach(b=>b.setAttribute('aria-pressed',String(b.dataset.project===p.id)));const a=el('div',undefined,'detail');a.append(el('p',p.domain+' / PROJECT STUDY','eyebrow'),el('h2',p.title),el('p',p.tags,'tags'),el('p',p.question,'question'));const grid=el('div',undefined,'detail-grid');for(const [label,value]of [['Method',p.method],['Available evidence',p.evidence]]){const d=el('div');d.append(el('h3',label,'detail-label'),el('p',value));grid.append(d);}a.append(grid);const scope=el('div',undefined,'scope-box');scope.append(el('strong','Validation scope'),el('span',p.scope));a.append(scope);const sources=el('div',undefined,'sources');sources.append(link('Explore the complete repository ↗',p.repository));for(const [title,items]of [['PINNED IMPLEMENTATION',p.sources],['TECHNICAL REPORT',p.reports],['EXECUTABLE NOTEBOOKS',p.notebooks]])if(items.length){sources.append(el('span',title,'source-title'));for(const item of items)sources.append(link(item.label+' ↗',item.url));}a.append(sources);const attr=el('div',undefined,'attribution');attr.append(el('p',p.authors),el('p','Licence: '+p.license),link('Audited source snapshot: '+p.commit.slice(0,12),p.repository+'/tree/'+p.commit));attr.lastChild.className='commit';a.append(attr);$('#project-detail').replaceChildren(a);} | |
| $('#search').addEventListener('input',renderProjects);$('#domain').addEventListener('change',renderProjects); | |
| function chartBase(id,xmin,xmax,ymin,ymax,xlabel,ylabel,xformat=x=>x.toFixed(0),yformat=x=>Number(x.toPrecision(3)).toString()){ | |
| const c=$(id),ctx=c.getContext('2d'),rect=c.getBoundingClientRect(),w=Math.max(250,rect.width),h=rect.height||280,dpr=Math.min(2,window.devicePixelRatio||1),L=w<500?48:66,R=20,T=25,B=45;c.width=Math.round(w*dpr);c.height=Math.round(h*dpr);ctx.scale(dpr,dpr);ctx.clearRect(0,0,w,h);ctx.font=(w<500?'10':'12')+'px monospace';ctx.lineWidth=1;ctx.fillStyle='#a1ada2'; | |
| const x=v=>L+(v-xmin)/(xmax-xmin)*(w-L-R),y=v=>h-B-(v-ymin)/(ymax-ymin)*(h-T-B); | |
| for(let i=0;i<=4;i++){const vx=xmin+(xmax-xmin)*i/4,vy=ymin+(ymax-ymin)*i/4;ctx.strokeStyle='#303a32';ctx.beginPath();ctx.moveTo(x(vx),T);ctx.lineTo(x(vx),h-B);ctx.moveTo(L,y(vy));ctx.lineTo(w-R,y(vy));ctx.stroke();ctx.textAlign='center';ctx.fillText(xformat(vx),x(vx),h-B+20);ctx.textAlign='right';ctx.fillText(yformat(vy),L-9,y(vy)+4);} | |
| ctx.textAlign='center';ctx.fillText(xlabel,(w+L-R)/2,h-4);ctx.textAlign='left';ctx.fillText(ylabel,L,T-9); | |
| return {ctx,x,y,w,h,L,R,T,B}; | |
| } | |
| function line(p,xs,ys,color,width=2){const {ctx,x,y}=p;ctx.save();ctx.beginPath();ctx.rect(p.L,p.T,p.w-p.L-p.R,p.h-p.T-p.B);ctx.clip();ctx.strokeStyle=color;ctx.lineWidth=width;ctx.beginPath();xs.forEach((v,i)=>{if(i===0)ctx.moveTo(x(v),y(ys[i]));else ctx.lineTo(x(v),y(ys[i]));});ctx.stroke();ctx.restore();} | |
| function renderControl(){const length=+$('#length').value,beta=+$('#beta').value,gamma=+$('#gamma').value,omega=+$('#omega').value,d=design(length,beta,gamma);$('#length-value').textContent=length.toFixed(2)+' m';$('#beta-value').textContent=beta.toFixed(1)+' s⁻¹';$('#gamma-value').textContent=gamma+' s⁻¹';$('#omega-value').textContent=omega.toFixed(2)+' rad/s';const H=response(d,omega);$('#identity-error').textContent=d.residual.toExponential(1);$('#gain').textContent=Math.hypot(...H).toFixed(4);$('#phase').textContent=(Math.atan2(H[1],H[0])*180/Math.PI).toFixed(2)+'°'; | |
| const p=chartBase('#poles',-Math.max(gamma+3,d.a+3),d.a+2,-1.7,1.7,'Real part / s⁻¹','Imaginary part / s⁻¹'),{ctx,x,y}=p;ctx.strokeStyle='#6b776d';ctx.beginPath();ctx.moveTo(x(0),y(-1.7));ctx.lineTo(x(0),y(1.7));ctx.stroke();for(const [re,im]of d.poles){ctx.strokeStyle='#9bdfb8';ctx.lineWidth=2;ctx.beginPath();ctx.moveTo(x(re)-5,y(im)-5);ctx.lineTo(x(re)+5,y(im)+5);ctx.moveTo(x(re)+5,y(im)-5);ctx.lineTo(x(re)-5,y(im)+5);ctx.stroke();}ctx.strokeStyle='#e0a78e';for(const re of [d.a,-d.a])ctx.strokeRect(x(re)-7,y(0)-7,14,14); | |
| const frequencies=Array.from({length:400},(_,i)=>-2+4*i/399),db=frequencies.map(v=>20*Math.log10(Math.max(1e-12,Math.hypot(...response(d,10**v))))),min=Math.min(-40,Math.floor(Math.min(...db)/20)*20),max=Math.max(5,Math.ceil(Math.max(...db)/5)*5);const bp=chartBase('#bode',-2,2,min,max,'Angular frequency / rad s⁻¹ (log scale)','Magnitude / dB',v=>(10**v).toPrecision(1));line(bp,frequencies,db,'#9bdfb8');bp.ctx.fillStyle='#b4a4e4';const mark=omega===0?null:Math.log10(omega);if(mark!==null){bp.ctx.beginPath();bp.ctx.arc(bp.x(mark),bp.y(20*Math.log10(Math.hypot(...H))),5,0,2*Math.PI);bp.ctx.fill();} | |
| $('#coefficients').textContent=['Coefficients descend in s. Internal-model frequency ω₀ = 1 rad/s.','',...['R','S','T','desired','actual'].map(k=>k+' = ['+d[k].map(v=>v.toPrecision(8)).join(', ')+']'),'','Relative coefficient residual = '+d.residual.toExponential(5),'At each slider change, the controller is redesigned for that nominal L.'].join('\n'); | |
| } | |
| for(const id of ['length','beta','gamma','omega'])$('#'+id).addEventListener('input',renderControl); | |
| function preset(b,g){$('#beta').value=b;$('#gamma').value=g;renderControl();}$('#initial-design').addEventListener('click',()=>preset(1,10));$('#retuned-design').addEventListener('click',()=>preset(.5,30)); | |
| let dataPlot; | |
| function renderData(){if(!training)return;const key=$('#metric').value,w=+$('#window').value,raw=training.rows.map(r=>Number(r[key])),smooth=rollingMean(raw,w),xs=raw.map((_,i)=>i+1),lo=Math.min(...raw),hi=Math.max(...raw),pad=Math.max((hi-lo)*.1,.01);$('#window-value').textContent=w+' episodes';$('#metric-title').textContent=$('#metric').selectedOptions[0].textContent;dataPlot=chartBase('#training-chart',1,raw.length,lo-pad,hi+pad,'Training episode (sequential order)',$('#metric-title').textContent);line(dataPlot,xs,raw,'#67766a',.8);line(dataPlot,xs,smooth,'#9bdfb8',2.3);$('#success-count').textContent=training.rows.filter(r=>r.is_success).length+' / '+raw.length;} | |
| $('#metric').addEventListener('change',renderData);$('#window').addEventListener('input',renderData);$('#training-chart').addEventListener('pointermove',e=>{if(!dataPlot)return;const rect=e.currentTarget.getBoundingClientRect(),px=e.clientX-rect.left,i=Math.max(0,Math.min(519,Math.round((px-dataPlot.L)/(dataPlot.w-dataPlot.L-dataPlot.R)*519))),r=training.rows[i];$('#hover-readout').textContent=`Episode ${i+1} · return ${r.r.toFixed(3)} · progress ${r.progress.toFixed(3)} · recent RMSE ${(1000*r.recent_rmse).toFixed(2)} mm · success flag ${r.is_success}`;}); | |
| function download(body,name,type){const a=document.createElement('a'),u=URL.createObjectURL(new Blob([body],{type}));a.href=u;a.download=name;a.click();setTimeout(()=>URL.revokeObjectURL(u),1000);} | |
| window.addEventListener('resize',()=>{if(currentTab==='control')renderControl();if(currentTab==='data')renderData();}); | |
| $('#download-csv').addEventListener('click',()=>{const a=document.createElement('a');a.href='monitor.csv';a.download='ur7e-original-monitor.csv';a.click();});$('#download-provenance').addEventListener('click',()=>{const {rows,...provenance}=training;download(JSON.stringify({...provenance,episodes:rows.length,license:'MIT',analysis:'Descriptive only; single simulated training run'},null,2),'ur7e-provenance.json','application/json');}); | |
| try{const data=await Promise.all(['projects.json','training-log.json'].map(async url=>{const r=await fetch(url);if(!r.ok)throw Error('Local data fetch failed');return r.json();}));[projects,training]=data;renderProjects();$('#provenance').textContent='Unchanged source SHA-256: '+training.sha256;$('#raw-source').href=training.source;setTab(location.hash.slice(1));}catch(e){$('#load-error').hidden=false;console.error(e);} | |