Files
MeltMind/tools/run_s3b_v04_part.py

110 lines
6.7 KiB
Python

"""One immutable geometry-only v0.4 measurement. Never reads labels."""
import argparse
import json
import os
from pathlib import Path
import time
import traceback
import numpy as np
from s2_recovery_supervisor import save
from s3b_v04_runtime import contract, checked, read, sha, restrict_prediction_io
from s3b_graph import edge_geometry, distal_regions
from s3b_v04_math import PARAMETERS, fill_batches
import resource
def run(root, index, method):
c = contract(root); spec = c['ten_parts'][index]
dest = root/'candidates'/method/'parts'/f'part-{index:02d}'/'output'; dest.mkdir(exist_ok=False)
save(dest/'started.json', dict(pid=os.getpid(), case=spec['case'], index=index, unix=time.time(),
solver=method, parameters=PARAMETERS, authorizing_ruling=c['authorizing_ruling'], labels_read=False))
allowed = list(spec['inputs'])
restrict_prediction_io(allowed, dest)
start = time.monotonic(); cpu = time.process_time(); latest = None
cost = {}; geometry = None; last_phase = 'STARTING'
def progress(stage, **kw):
nonlocal last_phase
last_phase=stage
save(dest/'progress.json', dict(stage=stage, case=spec['case'], pid=os.getpid(), unix=time.time(),
worker_CPU_s=time.process_time()-cpu, wall_s=time.monotonic()-start,
peak_RSS_bytes=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024, **kw))
try:
progress('VERIFY_GEOMETRY_ALLOWLIST')
for p, h in spec['inputs'].items(): checked(p, h)
source = Path(spec['preparation_root'])
metadata = read(source/'result.json')
if metadata['base_columns'][5] != 'geodesic_over_Rg':
raise ValueError('Not the original unrestricted geodesic column')
with np.load(source/'geometry.npz', allow_pickle=False) as z:
x=z['points_raw']; tetra=z['tetra']; faces=z['boundary_triangles']
edges=z['edges']; mass=z['node_volume_raw']; raw=z['raw_node_ids']; gate=z['gate_location_raw']
dw = np.load(source/'boundary_distance_raw.npy', allow_pickle=False)
base = np.load(source/'base.npy', allow_pickle=False)
distance = base[:, 5]*metadata['scales']['Rg']; del base
if np.any(np.diff(raw) <= 0): raise ValueError('Original IDs must be increasing')
seed = int(np.argmin(np.linalg.norm(x-gate, axis=1)))
if seed != metadata['base_gate_node']: raise ValueError('Gate source identity differs')
branch_start = time.process_time()
regions = distal_regions(distance, mass, edges, raw)
save(dest/'regions.json', dict(case=spec['case'], definition='strict-geodesic-distal-v0.3',
total_volume=float(mass.sum()), geometry_inputs=spec['inputs'], **regions))
cost['regions_CPU_s'] = time.process_time()-branch_start
def midpoint_progress(done, total):
progress('EXACT_MIDPOINTS', completed=done, total=total)
progress('GEOMETRY_EDGE_CAPACITY')
geometry = edge_geometry(x, tetra, faces, dw, edges, mass, midpoint_progress)
cost.update({k:geometry[k] for k in ['geometry_CPU_s','midpoint_CPU_s']})
np.savez_compressed(dest/'conductance.npz', edges=edges, edge_volume=geometry['edge_volume'],
midpoint_distance=geometry['midpoint'], g=geometry['conductance'],
exact_query_edge_ids=geometry['exact_query_edge_ids'], boundary_edges=geometry['boundary_edges'])
save(dest/'geometry-freeze.json', dict(case=spec['case'], nodes=len(x), total_volume=float(mass.sum()),
exact_midpoint_queries=len(geometry['exact_query_edge_ids']), seed=seed, raw_seed=int(raw[seed]),
boundary_edges=int(geometry['boundary_edges'].sum()), positive_edges=int((geometry['conductance']>0).sum()),
regions_sha256=sha(dest/'regions.json'), conductance_sha256=sha(dest/'conductance.npz'), costs=cost))
progress('BATCH_INITIALIZING', nodes=len(x), events=0, wetted_nodes=1)
last_progress = -1.
event_start = time.process_time()
with (dest/'events.jsonl').open('x', buffering=1) as stream, (dest/'pressure.jsonl').open('x', buffering=1) as pressure_stream:
def event(row):
stream.write(json.dumps(row, allow_nan=False)+'\n')
def state(row, remaining, arrival):
nonlocal latest, last_progress
latest = (row, remaining, arrival)
def stage(phase, **row):
nonlocal last_progress
now=time.monotonic()
if phase != last_phase or now-last_progress>=1:
progress(phase, **row)
last_progress=now
def pressure(row):
row['peak_RSS_bytes']=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024
pressure_stream.write(json.dumps(row, allow_nan=False)+'\n')
arrival, remaining, result = fill_batches(edges, geometry['conductance'], mass, seed, method,
event_callback=event, state_callback=state, stage_callback=stage, pressure_callback=pressure)
cost['events_CPU_s'] = time.process_time()-event_start
np.savez_compressed(dest/'event-field.npz', raw_ids=raw, arrival=arrival, remaining=remaining, mass=mass)
save(dest/'result.json', dict(case=spec['case'], index=index, nodes=len(x),
exact_midpoint_queries=len(geometry['exact_query_edge_ids']), **result, costs=cost,
labels_read=False, GPU=False, new_fits=0, event_batching=True, solver=method, parameters=PARAMETERS,
worker_CPU_s=time.process_time()-cpu, wall_s=time.monotonic()-start,
output_sha256={p.name:sha(p) for p in dest.iterdir() if p.is_file() and p.name!='progress.json'}))
progress(result['status'], event_state=result)
return 0
except Exception as exc:
if latest is not None:
state, remaining, arrival = latest
np.savez_compressed(dest/'last-trusted-state.npz', remaining=remaining, arrival=arrival)
else: state = None
save(dest/'failure.json', dict(status='NUMERICAL_OR_INPUT_FAILURE', case=spec['case'],
type=type(exc).__name__, phase=last_phase, message=str(exc), traceback=traceback.format_exc(),
last_trusted_state=state, remaining_volume=state['remaining_volume'] if state else None,
remaining_state='KNOWN_LAST_STATE' if state else 'UNKNOWN', costs=cost,
worker_CPU_s=time.process_time()-cpu, wall_s=time.monotonic()-start, retry=False))
raise
if __name__ == '__main__':
parser=argparse.ArgumentParser();parser.add_argument('root', type=Path);parser.add_argument('index', type=int)
parser.add_argument('method',choices=['splu','gmres_ilu'])
args=parser.parse_args();raise SystemExit(run(args.root,args.index,args.method))