stan_data, t0 = ncs.get_stan_data(csv, args) if stan_data is None: print("No data for %s; skipping fit." % args.roi) sys.exit(0) if args.n_data_only: print(ncs.get_n_data(stan_data)) sys.exit(0) init_fun = ncs.get_init_fun(args, stan_data) model_path = Path(args.models_path) / ('%s.stan' % args.model_name) model_path = model_path.resolve() assert model_path.is_file(), "No such .stan file: %s" % model_path control = {'adapt_delta': args.adapt_delta} stanrunmodel = ncs.load_or_compile_stan_model( args.model_name, args.models_path, force_recompile=args.force_recompile) # Fit Stan # fit = stanrunmodel.sampling(data=stan_data, init=init_fun, control=control, # chains=args.n_chains, # chain_id=np.arange(args.n_chains), # warmup=args.n_warmups, iter=args.n_iter, # thin=args.n_thin) fit = stanrunmodel.vb(data=stan_data, init=init_fun) # Uncomment to print fit summary print(fit) # Save fit save_dir = Path(args.fits_path) save_dir.mkdir(parents=True, exist_ok=True)
if not args.model_names: args.model_names = ncs.list_models(args.models_path) assert len(args.model_names),\ ("No such model files matching: *.stan' at %s" % (args.models_path)) model_paths = [ Path(args.models_path) / ('%s.stan' % model_name) for model_name in args.model_names ] for model_path in model_paths: assert model_path.is_file(), "No such .stan file: %s" % model_path run_script_path = Path(__file__).parent / 'run.py' run_flags = [('--%s' % key.replace('_', '-'), value) for key, value in args.__dict__.items() if key not in ['model_names', 'rois']] # This next section will be run in serial since this is only a reference # implementation. Parallel implementations are possible with multiprocessing # or the library of your choice. Best performance comes from parallelizing # run.py on a cluster. iterator = tqdm(args.model_names, desc='Model') for model_name in iterator: iterator.set_description("Compiling %s" % model_name) ncs.load_or_compile_stan_model(model_name, models_path=args.models_path, force_recompile=True) print("Finished compiling all models")