def main_random(args): locations = sorted(glob(os.path.join(args.in_path, args.glob))) locations = [os.path.abspath(loc) for loc in locations] pool = multiprocessing.Pool(args.nproc) bar = ProgressBar(maxval=args.rnd_num, widgets=[Timer(), " ", SimpleProgress(), " ", Percentage(), " ", Bar(), " ", ETA()]) rewire_name = "grn_rewired_{0:.1f}.pkl".format(args.prob) switch_name = "grn_switched_{0:d}.pkl".format(args.flip_num) for path in locations: filename = os.path.join(path, "trn.pkl") if not os.path.exists(filename): continue ver = version_from_path(path) base_path = os.path.join(args.out_path, ver) if not os.path.isdir(base_path): os.makedirs(base_path) LOGGER.info(ver) trn = pyorg.read_pickle(filename) # we consider null-models on the projected level since that is the level # on which we evaluate topological quantities net = pyreg.to_simple(trn.to_grn()) if args.run_rewire: LOGGER.info("Rewiring with probability %.1f", args.prob) tasks = [(net, args.prob)] * args.rnd_num res_it = pool.imap_unordered(rewire, tasks) rands = list() bar.start() for rnd in res_it: rands.append(rnd) bar += 1 bar.finish() pyorg.write_pickle(rands, os.path.join(base_path, rewire_name)) if args.run_switch: LOGGER.info("Switch-randomizing each edge %d times", args.flip_num) tasks = [(net, args.flip_num)] * args.rnd_num res_it = pool.imap_unordered(switch, tasks) success = list() rands = list() bar.start() for (rnd, rate) in res_it: rands.append(rnd) success.append(rate) bar += 1 bar.finish() pyorg.write_pickle(rands, os.path.join(base_path, switch_name)) LOGGER.info("mean flip success rate: %.3G +/- %.3G", np.mean(success), np.std(success)) pool.close()
def main_analysis(args): locations = sorted(glob(os.path.join(args.in_path, args.glob))) locations = [os.path.abspath(loc) for loc in locations] tasks = list() if args.run_rewire: tasks.extend([(loc, "rewired", args.choose, args.prob) for loc in locations]) if args.run_switch: tasks.extend([(loc, "switch", args.choose) for loc in locations]) out_name = os.path.join(args.out_path, args.file) if not os.path.isdir(args.out_path): os.makedirs(args.out_path) if os.path.exists(out_name): result = pd.read_csv(out_name, sep=str(";"), dtype={"version": str}, encoding=args.encoding) else: result = pd.DataFrame(columns=["version", "num_components", "largest_component", "feed_forward", "feedback", "cycles", "regulated_in_deg", "regulating_out_deg", "null_model"]) pool = multiprocessing.Pool(args.nproc) rewire_name = "grn_rewired_{0:.1f}.pkl".format(args.prob) rewire_descr = "rewired {0:.1f}".format(args.prob) switch_name = "grn_switched_{0:d}.pkl".format(args.flip_num) switch_descr = "switch {0:d}".format(args.flip_num) for path in locations: ver = version_from_path(path) LOGGER.info(ver) if args.run_rewire: filename = os.path.join(path, rewire_name) if os.path.exists(filename): LOGGER.info("Analyzing rewired networks (probability %.1f)", args.prob) nets = pyorg.read_pickle(filename) if args.choose is not None: chosen_num = min(args.choose, len(nets)) LOGGER.info("Using %d/%d random networks", chosen_num, len(nets)) nets = random.sample(nets, chosen_num) tasks = [(net, ver, rewire_descr) for net in nets] res_it = pool.imap_unordered(stats, tasks) frames = list() bar = ProgressBar(maxval=len(tasks), widgets=[Timer(), " ", SimpleProgress(), " ", Percentage(), " ", Bar(), " ", ETA()]).start() for df in res_it: frames.append(df) bar += 1 bar.finish() result = result.append(pd.concat(frames, ignore_index=True), ignore_index=True) result.to_csv(out_name, header=True, index=False, sep=str(";"), encoding=args.encoding) if args.run_switch: filename = os.path.join(path, switch_name) if os.path.exists(filename): LOGGER.info("Analyzing switched networks (flip number %d)", args.flip_num) nets = pyorg.read_pickle(filename) if args.choose is not None: chosen_num = min(args.choose, len(nets)) LOGGER.info("Using %d/%d random networks", chosen_num, len(nets)) nets = random.sample(nets, chosen_num) tasks = [(net, ver, switch_descr) for net in nets] res_it = pool.imap_unordered(stats, tasks) frames = list() bar = ProgressBar(maxval=len(tasks), widgets=[Timer(), " ", SimpleProgress(), " ", Percentage(), " ", Bar(), " ", ETA()]).start() for df in res_it: frames.append(df) bar += 1 bar.finish() result = result.append(pd.concat(frames, ignore_index=True), ignore_index=True) result.to_csv(out_name, header=True, index=False, sep=str(";"), encoding=args.encoding)