def parse_args(): parser = get_parser() parser.add_argument("hermes_build_dir", type=str, nargs="?", default="build") parser.add_argument("--icu", type=str, dest="icu_root", default="") parser.add_argument("--fbsource", type=str, dest="fbsource_dir", default="") parser.add_argument("--opcode-stats", dest="opcode_stats", action="store_true") parser.add_argument( "--basic-block-profiler", dest="basic_block_profiler", action="store_true" ) parser.add_argument( "--warnings-as-errors", dest="warnings_as_errors", action="store_true" ) parser.add_argument("--static-link", dest="static_link", action="store_true") args = parser.parse_args() args.hermes_build_dir = os.path.realpath(args.hermes_build_dir) if args.icu_root: args.icu_root = os.path.realpath(args.icu_root) if args.fbsource_dir: args.fbsource_dir = os.path.realpath(args.fbsource_dir) args.build_type = args.build_type or ("MinSizeRel" if args.distribute else "Debug") suffix = build_dir_suffix(args) args.hermes_build_dir += suffix # Guess the ICU directory based on platform. if not args.icu_root and platform.system() == "Linux": icu_prefs = [ "/mnt/gvfs/third-party2/icu/4e8f3e00e1c7d7315fd006903a9ff7f073dfc02b/53.1/gcc-5-glibc-2.23/9bc6787", "/mnt/gvfs/third-party2/icu/4e8f3e00e1c7d7315fd006903a9ff7f073dfc02b/53.1/gcc-4.8.1-glibc-2.17/c3f970a/", ] for pref in icu_prefs: if os.path.exists(pref): args.icu_root = pref break return args
def manage_response(self, data): if "local_connection_token" in data: spkserver.local_connection_token = data["local_connection_token"] if "channel_api_token" in data: self.channel_thread.connect(data["channel_api_token"]) if "command" in data: if data["command"] == "re_configure": common.set_id(None) self.channel_thread.disconnect() #common.restart_please() segun yo no es necesario if data["command"] == "invalidate_config_info": print u"Parece que hay un problema con la configuración, el servidor contestó: %s" % data[ "reason"] if data["command"] == "update_id": if "new_id" in data: common.set_id(data["new_id"]) elif data["command"] == "pause" or data["command"] == "unpause": if self.routine != None: self.routine.job.buffer_manager.send_line( "!" if data["command"] == "pause" else "~") elif data["command"] == "send_gcode": if self.routine is None: try: code = data.get("code", None) if code is None: parser = common.get_parser() code = data.get(parser, None) if code is not None: code = code.strip() + "\n" ThreadsafeSerial.get().write(code) if "success_message" in data: message = data["success_message"] try: self.show_message( "success", message.get("title", ""), message.get("description", ""), message.get("timeout", 5000)) except: pass except DeadSerialException: self.show_message( "error", "El puerto serial está desconectado") elif data["command"] == "stop_job": if self.routine != None: self.routine.status.stop_please() elif data["command"] == "run_queue": if "job_data" in data: self.start_routine("cut", job_id=data["job_data"]["job_id"], arguments=data["job_data"]["arguments"]) elif data["command"] == "do_homing": self.start_routine("home", arguments=data.get("arguments", None)) elif data["command"] == "do_probing": self.start_routine("probe", arguments=data.get("arguments", None))
def main(): #constants indices_to_use = [[1, 2499], [2549, 2999], [3049, 4999], [5049, 14999], [15049, 19999], [20049, 29999], [30049, 64999], [65049, 69999], [70049, -1]] self.starting_parameters = [ 2.26E-04, 0.0699, 3.45E-05, 0.05462, 0.0873, 8.92E-03, 5.150E-3, 0.03158, 0.1524 ] plt.rcParams['axes.axisbelow'] = True # Check input arguments parser = argparse.ArgumentParser( description='Plot sensitivities of the Beattie model') parser = get_parser(data_reqd=True) parser.add_argument("-n", "--no_chains", default=5, help="number of chains to use") parser.add_argument("-l", "--chain_length", default=1000, help="length of chain to use") args = parser.parse_args() data = pd.read_csv(args.data_file_path, delim_whitespace=True) times = data["time"] data = data["current"].values print("outputting to {}".format(args.output)) # Create output directory if not os.path.exists(args.output): os.mkdir(args.output) if not os.path.exists(args.data_file_path): print("Input file not provided. Doing nothing.") return par = Params() skip = int(par.timestep / 0.1) dat = extract_times(data, indices_to_use, skip) model = PintsWrapper(data, par, args, times) mcmc = pints.MCMCController(model, args.no_chains, args.no_chains * [starting_parameters], method=pints.HaarioBardenetACMC) mcmc.set_max_iterations(args.chain_length) chains = mcmc.run() print(chains)
def run(self): self.status.set_can_stop(True) try: usb= ThreadsafeSerial.get() usb.flushInput() usb.flushOutput() except DeadSerialException: raise Exception("El puerto serial está desconectado") self.parser=common.get_parser() self.buffer_manager = BufferManager(self.parser) self.prepare_cnc() if self.routine_type=="cut": self.status.set_fase('downloading') g_code= urllib2.urlopen(self.arguments["gcode_url"]).read() #descargamos el código G lines=clean_lines(g_code.split("\n"))#lista de líneas de código self.exit_if_requested() with PeriodicQuestion(self.parser): speed=self.arguments["speed"] if speed!= 1: lines=slow_down(lines, speed) start_line=self.arguments["start_line"] if start_line>len(lines): raise Exception("La línea inicial es %d, pero el archivo sólo tiene %d líneas."%(start_line,len(lines))) if start_line>0: previous_lines=lines[:start_line+1] #es mas uno para incluir la actual en las busqedas lines=lines[start_line:] previous_lines.reverse() last_G=extract_first_match(previous_lines,["G0","G1","G00","G01"]) last_X=extract_float(previous_lines, "X") last_Y=extract_float(previous_lines, "Y") last_Z=extract_float(previous_lines, "Z") last_F=extract_float(previous_lines, "F") last_S=extract_float(previous_lines, "S") for h in [HALF_FILE_CODE_FIRST]: header=h[:] header=replace_variable(header,"LAST_X","X"+last_X if last_X else "") header=replace_variable(header,"LAST_Y","Y"+last_Y if last_Y else "") header=replace_variable(header,"LAST_Z","Z"+last_Z if last_Z else "") header=replace_variable(header,"LAST_F","F"+last_F if last_F else "") header=replace_variable(header,"LAST_S","S"+last_S if last_S else "") header=replace_variable(header,"LAST_G",last_G if last_G else "") header=replace_variable(header,"SEC_PLANE",str(get_variable("security_plane"))) self.send_gcode(clean_lines(header),"preparing") self.send_gcode(lines,"cutting",start_line=start_line) elif self.routine_type=="home": self.home() elif self.routine_type=="probe": self.probe()
def main(): parser = get_parser() parser.add_argument('--list', action='store_true') parser.add_argument('--annotated', action='store_true') parser.add_argument('--color-by') args = get_args(__file__, parser) if args.list: print_list() else: plot(args)
def parse_args(): parser = get_parser() parser.add_argument("llvm_src_dir", type=str, nargs="?", default="llvm") parser.add_argument("llvm_build_dir", type=str, nargs="?", default="llvm_build") parser.add_argument("--target", "-t", action="append", default=[]) args = parser.parse_args() args.llvm_src_dir = os.path.realpath(args.llvm_src_dir) args.llvm_build_dir = os.path.realpath(args.llvm_build_dir) args.build_type = args.build_type or ("MinSizeRel" if args.distribute else "Debug") args.llvm_build_dir += build_dir_suffix(args) return args
def manage_response(self,data): if "local_connection_token" in data: spkserver.local_connection_token=data["local_connection_token"] if "channel_api_token" in data: self.channel_thread.connect(data["channel_api_token"]) if "command" in data: if data["command"]=="re_configure": common.set_id(None) self.channel_thread.disconnect() #common.restart_please() segun yo no es necesario if data["command"]=="invalidate_config_info": print u"Parece que hay un problema con la configuración, el servidor contestó: %s"%data["reason"] if data["command"]=="update_id": if "new_id" in data: common.set_id(data["new_id"]) elif data["command"]=="pause" or data["command"]=="unpause": if self.routine!=None: self.routine.job.buffer_manager.send_line("!" if data["command"]=="pause" else "~") elif data["command"]=="send_gcode": if self.routine is None: try: code=data.get("code",None) if code is None: parser=common.get_parser() code=data.get(parser,None) if code is not None: code=code.strip()+"\n" ThreadsafeSerial.get().write(code) if "success_message" in data: message= data["success_message"] try: self.show_message("success",message.get("title",""),message.get("description",""),message.get("timeout",5000)) except: pass except DeadSerialException: self.show_message("error","El puerto serial está desconectado") elif data["command"]=="stop_job": if self.routine!=None: self.routine.status.stop_please() elif data["command"]=="run_queue": if "job_data" in data: self.start_routine("cut",job_id=data["job_data"]["job_id"],arguments=data["job_data"]["arguments"]) elif data["command"]=="do_homing": self.start_routine("home",arguments=data.get("arguments",None)) elif data["command"]=="do_probing": self.start_routine("probe",arguments=data.get("arguments",None))
def parse_args(): def default_build_command(args): # Choose a default based on the build system chosen if args.build_system == "Ninja": return "ninja" elif args.build_system == "Unix Makefiles": return "make" elif "Visual Studio" in args.build_system: return "MSBuild.exe LLVM.sln -target:build -maxcpucount -verbosity:normal" else: raise Exception("Unrecognized build system: {}".format( args.build_system)) parser = get_parser() parser.add_argument("llvm_src_dir", type=str, nargs="?", default="llvm") parser.add_argument("llvm_build_dir", type=str, nargs="?", default="llvm_build") parser.add_argument( "--build-command", type=str, dest="build_command", default=None, help="Command to run once cmake finishes", ) parser.add_argument("--cross-compile-only", dest="cross_compile_only", action="store_true") args = parser.parse_args() args.llvm_src_dir = os.path.realpath(args.llvm_src_dir) args.llvm_build_dir = os.path.realpath(args.llvm_build_dir) if not args.build_command: args.build_command = default_build_command(args) if args.cross_compile_only: args.build_command += " " + os.path.join("bin", "llvm-tblgen") args.build_type = args.build_type or ("MinSizeRel" if args.distribute else "Debug") args.llvm_build_dir += build_dir_suffix(args) return args
if args.plot: plt.show() for i in range(0, par.n_params): for j in range(i + 1, par.n_params): parameters_to_view = np.array([i, j]) sub_cov = cov[parameters_to_view[:, None], parameters_to_view] eigen_val, eigen_vec = np.linalg.eigh(sub_cov) eigen_val = eigen_val.real if eigen_val[0] > 0 and eigen_val[1] > 0: print("COV_{},{} : well defined".format(i, j)) cov_ellipse(sub_cov, q=[0.75, 0.9, 0.99]) plt.ylabel("parameter {}".format(i)) plt.xlabel("parameter {}".format(j)) if args.plot: plt.show() else: plt.savefig( os.path.join( output_dir, "covariance_for_parameters_{}_{}".format(i, j))) plt.clf() else: print("COV_{},{} : negative eigenvalue".format(i, j)) if __name__ == "__main__": parser = get_parser(data_reqd=False) args = parser.parse_args() simulate_sine_wave_sensitivities(args)
def main(): # Check input arguments parser = get_parser() args = parser.parse_args() par = Params() plot_dir = os.path.join(args.output, "staircase") if not os.path.exists(plot_dir): os.makedirs(plot_dir) # Choose parameters (make sure conductance is the last parameter) para = np.array([ 2.07E-3, 7.17E-2, 3.44E-5, 6.18E-2, 4.18E-1, 2.58E-2, 4.75E-2, 2.51E-2, 3.33E-2 ]) # Compute resting potential for 37 degrees C reversal_potential = calculate_reversal_potential(temp=37) par.Erev = reversal_potential print("reversal potential is {}".format(reversal_potential)) # Create symbols for symbolic functions p, y, v = CreateSymbols(par) k = se.symbols('k1, k2, k3, k4') # Define system equations and initial conditions k1 = p[0] * se.exp(p[1] * v) k2 = p[2] * se.exp(-p[3] * v) k3 = p[4] * se.exp(p[5] * v) k4 = p[6] * se.exp(-p[7] * v) # Notation is consistent between the two papers A = se.Matrix([[-k1 - k3 - k4, k2 - k4, -k4], [k1, -k2 - k3, k4], [-k1, k3 - k1, -k2 - k4 - k1]]) B = se.Matrix([k4, 0, k1]) current_limit = (p[-1] * (par.holding_potential - reversal_potential) * k1 / (k1 + k2) * k4 / (k3 + k4)).subs( v, par.holding_potential) print("{} Current limit computed as {}".format( __file__, current_limit.subs(p, para).evalf())) sens_inf = [ float(se.diff(current_limit, p[j]).subs(p, para).evalf()) for j in range(0, par.n_params) ] print("{} sens_inf calculated as {}".format(__file__, sens_inf)) protocol = pd.read_csv( os.path.join( os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "protocols", "protocol-staircaseramp.csv")) times = 1000 * protocol["time"].values voltages = protocol["voltage"].values spikes = 1000 * detect_spikes(protocol["time"], protocol["voltage"]) staircase_protocol = scipy.interpolate.interp1d(times, voltages, kind="linear") staircase_protocol_safe = lambda t: staircase_protocol(t) if t < times[ -1] else par.holding_potential funcs = GetSensitivityEquations(par, p, y, v, A, B, para, times, voltage=staircase_protocol_safe) ret = funcs.SimulateForwardModelSensitivities(para), current = ret[0][0] S1 = ret[0][1] S1n = S1 * np.array(para)[None, :] sens_inf_N = sens_inf * np.array(para)[None, :] param_labels = ['S(p' + str(i + 1) + ',t)' for i in range(par.n_params)] [ plt.plot(funcs.times, sens, label=param_labels[i]) for i, sens in enumerate(S1n.T) ] [plt.axhline(s) for s in sens_inf_N[0, :]] plt.legend() plt.xlabel("time /ms") plt.ylabel("dI(t)/dp") if args.plot: plt.show() else: plt.savefig(os.path.join(plot_dir, "sensitivities_plot")) state_variables = funcs.GetStateVariables(para) state_labels = ['C', 'O', 'I', 'IC'] param_labels = ['S(p' + str(i + 1) + ',t)' for i in range(par.n_params)] fig = plt.figure(figsize=(8, 8), dpi=args.dpi) ax1 = fig.add_subplot(411) ax1.plot(funcs.times, funcs.GetVoltage()) ax1.grid(True) ax1.set_xticklabels([]) ax1.set_ylabel('Voltage (mV)') [ax1.axvline(spike, color='red') for spike in spikes] ax2 = fig.add_subplot(412) ax2.plot(funcs.times, funcs.SimulateForwardModel(para)) ax2.grid(True) ax2.set_xticklabels([]) ax2.set_ylabel('Current (nA)') ax3 = fig.add_subplot(413) for i in range(par.n_state_vars + 1): ax3.plot(funcs.times, state_variables[:, i], label=state_labels[i]) ax3.legend(ncol=4) ax3.grid(True) ax3.set_xticklabels([]) ax3.set_ylabel('State occupancy') ax4 = fig.add_subplot(414) for i in range(par.n_params): ax4.plot(funcs.times, S1n[:, i], label=param_labels[i]) ax4.legend(ncol=3) ax4.grid(True) ax4.set_xlabel('Time (ms)') ax4.set_ylabel('Sensitivities') plt.tight_layout() if not args.plot: plt.savefig( os.path.join(plot_dir, 'ForwardModel_SW_{}.png'.format(args.sine_wave))) # Only take every 100th point # S1n = S1n[0:-1:10] H = np.dot(S1n.T, S1n) print(H) eigvals = np.linalg.eigvals(H) #Sigma2 - the observed variance. 1885 is the value taken from a fit sigma2 = 1885 / (len(funcs.times) - 1) print('Eigenvalues of H:\n{}'.format(eigvals.real)) # Plot the eigenvalues of H, shows the condition of H fig = plt.figure(figsize=(6, 6), dpi=args.dpi) ax = fig.add_subplot(111) for i in eigvals: ax.axhline(y=i, xmin=0.25, xmax=0.75) ax.set_yscale('log') ax.set_xticks([]) ax.grid(True) if not args.plot: plt.savefig( os.path.join(plot_dir, 'Eigenvalues_SW_{}.png'.format(args.sine_wave))) if args.plot: plt.show() cov = np.linalg.inv(H * sigma2) for j in range(0, par.n_params): for i in range(j + 1, par.n_params): parameters_to_view = np.array([i, j]) # sub_sens = S1n[:,[i,j]] sub_cov = cov[parameters_to_view[:, None], parameters_to_view] # sub_cov = np.linalg.inv(np.dot(sub_sens.T, sub_sens)*sigma2) eigen_val, eigen_vec = np.linalg.eigh(sub_cov) eigen_val = eigen_val.real if eigen_val[0] > 0 and eigen_val[1] > 0: print("COV_{},{} : well defined".format(i, j)) cov_ellipse(sub_cov, q=[0.75, 0.9, 0.99], offset=para[[i, j]]) plt.ylabel("parameter {}".format(i + 1)) plt.xlabel("parameter {}".format(j + 1)) if args.plot: plt.show() else: plt.savefig( os.path.join( output_dir, "covariance_for_parameters_{}_{}".format( j + 1, i + 1))) plt.clf() else: print("COV_{},{} : negative eigenvalue: {}".format( i, j, eigen_val))
plt.clf() else: print("COV_{},{} : negative eigenvalue".format(i,j)) print('Eigenvalues of FIM:\n{}'.format(eigvals)) print("Covariance matrix is: \n{}".format(cov)) plt.plot(data["time"], data["current"], label="averaged data") plt.plot(times, current, label="current") plt.legend() if args.plot: plt.show() else: plt.savefig(os.path.join(output_dir, "fit")) plt.clf() return times, found_parameters if __name__ == "__main__": spike_removal_durations=[0,50,100,150,200,1000] parser = get_parser(data_reqd=True) parser.add_argument("-r", "--remove", default=spike_removal_durations, help="ms of data to ignore after each capacitive spike", nargs='+', type=int) args = parser.parse_args () data = pd.read_csv(args.data_file_path, delim_whitespace=True) for val in args.remove: output_dir = "{}ms_removed".format(val) times, params = main(args, output_dir, val) simulate_sine_wave_sensitivities(args, times, dirname=output_dir, para=params, data=data) print("done")
def main(): # Check input arguments parser = get_parser() args = parser.parse_args() par = Params() plot_dir = os.path.join(args.output, "AP-protocol") if not os.path.exists(plot_dir): os.makedirs(plot_dir) # Choose parameters (make sure conductance is the last parameter) para = np.array([ 2.07E-3, 7.17E-2, 3.44E-5, 6.18E-2, 4.18E-1, 2.58E-2, 4.75E-2, 2.51E-2, 3.33E-2 ]) # Create symbols for symbolic functions p, y, v = CreateSymbols(par) k = se.symbols('k1, k2, k3, k4') # Define system equations and initial conditions k1 = p[0] * se.exp(p[1] * v) k2 = p[2] * se.exp(-p[3] * v) k3 = p[4] * se.exp(p[5] * v) k4 = p[6] * se.exp(-p[7] * v) # Notation is consistent between the two papers A = se.Matrix([[-k1 - k3 - k4, k2 - k4, -k4], [k1, -k2 - k3, k4], [-k1, k3 - k1, -k2 - k4 - k1]]) B = se.Matrix([k4, 0, k1]) rhs = np.array(A * y + B) protocol = pd.read_csv( os.path.join( os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "protocols", "AP-protocol.txt")) times = 1E3 * protocol["time"].values voltages = protocol["voltage"].values # 10*times to correct units staircase_protocol = scipy.interpolate.interp1d(times, voltages, kind="linear") staircase_protocol_safe = lambda t: staircase_protocol(t) if t < times[ -1] else par.holding_potential funcs = GetSensitivityEquations(par, p, y, v, A, B, para, times, voltage=staircase_protocol_safe) ret = funcs.SimulateForwardModelSensitivities(para), current = ret[0][0] S1 = ret[0][1] S1n = S1 * np.array(para)[None, :] state_variables = funcs.GetStateVariables(para) state_labels = ['C', 'O', 'I', 'IC'] param_labels = ['S(p' + str(i + 1) + ',t)' for i in range(par.n_params)] fig = plt.figure(figsize=(8, 8), dpi=args.dpi) ax1 = fig.add_subplot(411) ax1.plot(funcs.times, funcs.GetVoltage()) ax1.grid(True) ax1.set_xticklabels([]) ax1.set_ylabel('Voltage (mV)') ax2 = fig.add_subplot(412) ax2.plot(funcs.times, funcs.SimulateForwardModel(para)) ax2.grid(True) ax2.set_xticklabels([]) ax2.set_ylabel('Current (nA)') ax3 = fig.add_subplot(413) for i in range(par.n_state_vars + 1): ax3.plot(funcs.times, state_variables[:, i], label=state_labels[i]) ax3.legend(ncol=4) ax3.grid(True) ax3.set_xticklabels([]) ax3.set_ylabel('State occupancy') ax4 = fig.add_subplot(414) for i in range(par.n_params): ax4.plot(funcs.times, S1n[:, i], label=param_labels[i]) ax4.legend(ncol=3) ax4.grid(True) ax4.set_xlabel('Time (ms)') ax4.set_ylabel('Sensitivities') plt.tight_layout() if not args.plot: plt.savefig( os.path.join(plot_dir, 'ForwardModel_SW_{}.png'.format(args.sine_wave))) H = np.dot(S1n.T, S1n) print(H) eigvals = np.linalg.eigvals(H) print('Eigenvalues of H:\n{}'.format(eigvals.real)) # Plot the eigenvalues of H, shows the condition of H fig = plt.figure(figsize=(6, 6), dpi=args.dpi) ax = fig.add_subplot(111) for i in eigvals: ax.axhline(y=i, xmin=0.25, xmax=0.75) ax.set_yscale('log') ax.set_xticks([]) ax.grid(True) if not args.plot: plt.savefig( os.path.join(plot_dir, 'Eigenvalues_SW_{}.png'.format(args.sine_wave))) if args.plot: plt.show()
def parse_args(): parser = get_parser() parser.add_argument("hermes_build_dir", type=str, nargs="?", default="build") parser.add_argument("--icu", type=str, dest="icu_root", default="") parser.add_argument("--fbsource", type=str, dest="fbsource_dir", default="") parser.add_argument("--opcode-stats", dest="opcode_stats", action="store_true") parser.add_argument("--basic-block-profiler", dest="basic_block_profiler", action="store_true") parser.add_argument("--warnings-as-errors", dest="warnings_as_errors", action="store_true") parser.add_argument("--static-link", dest="static_link", action="store_true") parser.add_argument( "--wasm", action="store_true", help="Build Hermes as WebAssembly instead of a native binary", ) parser.add_argument( "--emscripten-root", dest="emscripten_root", help="Path to the root of emscripten. Use emsdk to download", ) parser.add_argument( "--emscripten-platform", dest="emscripten_platform", choices=("upstream", "fastcomp"), default="fastcomp", help="Use either the upstream emscripten backend based on LLVM or the " "fastcomp backend", ) args = parser.parse_args() args.hermes_build_dir = os.path.realpath(args.hermes_build_dir) if args.icu_root: args.icu_root = os.path.realpath(args.icu_root) if args.fbsource_dir: args.fbsource_dir = os.path.realpath(args.fbsource_dir) if args.emscripten_root: args.emscripten_root = os.path.realpath(args.emscripten_root) if args.wasm: # Check that if wasm is specified, that emscripten_root is also specified. if not args.emscripten_root: raise ValueError( "WASM build requested, but emscripten-root not given") if not os.path.exists(args.emscripten_root): raise ValueError( "WASM build requested, but emscripten-root doesn't exist: " + args.emscripten_root) if not args.build_type: if args.distribute: # WASM doesn't need to be built to be small. args.build_type = "Release" if args.wasm else "MinSizeRel" else: args.build_type = "Debug" args.hermes_build_dir += build_dir_suffix(args) # Guess the ICU directory based on platform. if not args.icu_root and platform.system() == "Linux": icu_prefs = [ "/mnt/gvfs/third-party2/icu/4e8f3e00e1c7d7315fd006903a9ff7f073dfc02b/53.1/gcc-5-glibc-2.23/9bc6787", "/mnt/gvfs/third-party2/icu/4e8f3e00e1c7d7315fd006903a9ff7f073dfc02b/53.1/gcc-4.8.1-glibc-2.17/c3f970a/", ] for pref in icu_prefs: if os.path.exists(pref): args.icu_root = pref break return args
def main(): parser = get_parser() parser.add_argument('--first', default=3, type=int) parser.add_argument('--second', default=4, type=int) parser.add_argument('--annotated', action='store_true') parser.add_argument('--iterations', default=10, type=int) parser.add_argument('--color-by') args = get_args(__file__, parser) columns = [args.first, args.second] df = pd.DataFrame(read_table(args.table)) data = df[(df.lid == args.first) | (df.lid == args.second)]\ .pivot(index='bid', columns='lid', values='pos')\ .sort_values(by=args.first)\ .fillna(0)\ .reindex_axis(columns, axis=1) # assure column order # Reindex by position meta = Metadata(index='pos', dfs=[df[df.lid == args.first], df[df.lid == args.second]]) palette_name = None title = 'Orden/Orden inventarios {} y {}'\ .format(to_letter(args.first), to_letter(args.second)) if not args.color_by: # Color based on wether theyre in both inventaries or missing data['color'] = data.apply( lambda row: any(not row[c] for c in columns), 1) else: variable_name = variable_names.get(args.color_by, args.color_by) title += ' variable "{}"'.format(variable_name) data['color'] = data.apply( lambda row: meta.get_field(args.color_by, * [row[c] for c in columns]), 1) # Group numerical values in 5 bins/categories color_sorter = None if args.color_by in ['area', 'height']: palette_name = 'YlOrRd' # yellow to red bins = 10 if args.color_by == 'height' else 5 data['color'] = pd.cut(data['color'], bins, precision=0) def color_sorter(e): return float(str(e).strip('(').strip(']').split(', ', 1)[0]) # Assure repeteable colors by setting category-color map # before lmplot does it randomly on each run and confuse us values = sorted(data['color'].unique(), key=color_sorter) colors = sns.color_palette(palette=palette_name, n_colors=len(values)) palette = dict(zip(values, colors)) # Use str as column names, otherwise lmplot goes wild columns = list(map(str, columns)) data.columns = columns + ['color'] p = sns.lmplot(*columns, data=data, hue='color', palette=palette, legend=False, legend_out=True, fit_reg=False, size=7, aspect=1.3) # Set top title and space for it plt.suptitle(title) p.fig.subplots_adjust(top=0.92) p.set(ylim=(0, None), xlim=(0, None)) # Set legend outside graph at center right if args.color_by: p.fig.subplots_adjust(right=0.85) variable_name = variable_names.get(args.color_by, args.color_by) plt.legend(bbox_to_anchor=(1.18, 0.7), borderaxespad=0., title=variable_name) if args.annotated: texts = [ p.ax.text( first, second, meta.get_field('short', first, second), fontsize=8, ) for first, second, color in data.values ] # for first, second, na in data.values: # # plt.annotate( # # meta.get(first, second)['short'], # # #str((first, second)), # # xy=(first, second), # # xytext=(first + 1, second + 1), # # fontsize=8, # # ) adjust_text(texts, force_points=1.5, lim=args.iterations, arrowprops=dict(arrowstyle="-", color='r', alpha=0.8)) plotting(plt, args)
def do_POST(self): # if self.path=="/send_web": # # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # self.send_response(200) # self.end_headers() # textout = ["Nombre red: %s", # "Pswd: %s\n", # "Tu raspberry se esta conectando...", # "Si cometiste algun error, conectate al cable de Ethernet recarga la pagina y repite los pasos.", # ] # # self.wfile.write("SPKClient:. \n\n" + "\n".join(textout) % (form["ssid"].value,form["psk"].value)) # # if not spkwifi.set_wifi_network(form["ssid"].value.strip(), form["psk"].value.strip()): # self.wfile.write("Algo salio mal...") # # # return # if self.path=="/send_cncid": # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # # cncid= form["cncid"].value # # try: # if cncid.startswith("sudo"): # cncid = cncid.split("\"")[1] # # f = open(main.NEWCNCIDFILENAME,"w") # f.write(cncid) # f.close # # self.send_response(200) # self.end_headers() # # textout = ["ID: %s\n", # "Recarga la pagina en unos momentos.", # "Tu raspberry se esta reiniciando...", # "Revisa tu nueva maquina en el taller correspondiente." # ] # self.wfile.write("SPKClient:. \n\n" + "\n".join(textout) % (cncid)) # # common.restart_please() # #system("reboot") # #execl(sys.executable, *([sys.executable]+sys.argv)) # # except: # self.send_response(200) # self.end_headers() # self.wfile.write("Error, revisa el contenido de tu id") # # # return # if self.path=="/reset_client": # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # # # self.send_response(200) # self.end_headers() # self.wfile.write("El script de spkclient se esta reiniciando, ve a la pagina principal y recargala.") # common.restart_please() # return # if self.path=="/reset_pi": # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # # # self.send_response(200) # self.end_headers() # self.wfile.write("La raspberrypi se reiniciara...") # system("reboot") # return # if self.path=="/spkwizardsetup": data = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], }) action= data.getfirst("action", None) origin= self.headers.get("Origin",None) if not origin in ALLOWED_CONFIG_ORIGINS: self.send_response(403) return self.send_response(200) self.send_header('Content-type',"application/json") self.send_header('Access-Control-Allow-Origin',origin) self.end_headers() if action=="marco": if common.get_id()!=None: self.wfile.write(json.dumps({"status":"already_configured"})) else: self.wfile.write(json.dumps({"status":"polo","current_network":spkwifi.get_current_connected_network()})) elif action=="get_network_status": self.wfile.write(json.dumps({"status":"success","available_networks":[n.ssid for n in spkwifi.get_available_networks()],"current_network":spkwifi.get_current_connected_network()})) elif action=="set_wifi_network": ssid= data.getfirst("ssid", None) password= data.getfirst("password", None) if spkwifi.set_wifi_network(ssid, password): self.wfile.write(json.dumps({"status":"success","current_network":spkwifi.get_current_connected_network()})) else: self.wfile.write(json.dumps({"status":"error","current_network":spkwifi.get_current_connected_network()})) elif action=="send_workshop": config_data= json.loads(data.getfirst("config_data", "{}")) response=common.send_config_info(config_data) if "extra_info" in response: self.wfile.write(json.dumps({"status":"success","extra_info":response["extra_info"]})) else: self.wfile.write(json.dumps({"status":"error","reason":response.get("reason","")})) else: self.wfile.write(json.dumps({"status":"error"})) elif self.path=="/spkcnclocalcommandreceiver": data = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], }) origin= self.headers.get("Origin",None) local_token= data.getfirst("_local_token", None) prev_local_token= data.getfirst("_prev_local_token", None) if not origin in ALLOWED_CONFIG_ORIGINS or local_token==None or (local_token!=local_connection_token!=prev_local_token): self.send_response(403) return self.send_response(200) self.send_header('Content-type',"application/json") self.send_header('Access-Control-Allow-Origin',origin) self.end_headers() message= json.loads(data.getfirst("message", None)) command=message.get("command",None) if command=="checking_same_network": self.wfile.write(json.dumps({"status":"sure :)"})) elif command=="lifeline": time.sleep(60) self.wfile.write(json.dumps({"status":"sure :)"})) elif command=="get_network_status": self.wfile.write(json.dumps({"status":"success","available_networks":[n.ssid for n in spkwifi.get_available_networks()],"current_network":spkwifi.get_current_connected_network()})) elif command=="get_current_network": self.wfile.write(json.dumps({"status":"success","current_network":spkwifi.get_current_connected_network()})) elif command=="set_wifi_network": ssid= message.get("ssid", None) password= message.get("password", None) if spkwifi.set_wifi_network(ssid, password): self.wfile.write(json.dumps({"status":"success","current_network":spkwifi.get_current_connected_network()})) else: self.wfile.write(json.dumps({"status":"error","current_network":spkwifi.get_current_connected_network()})) elif command=="get_script_status_change": while True: if common.internet_thread.routine is None or common.internet_thread.routine.status.as_dict()!=message.get("last_status",None): break else: time.sleep(.01) if common.internet_thread.routine is not None: self.wfile.write(json.dumps({"status":common.internet_thread.routine.status.as_dict()})) else: self.wfile.write(json.dumps({"status":common.internet_thread.last_job_status.as_dict() if common.internet_thread.last_job_status is not None else None})) elif command=="send_console_line": if common.internet_thread.routine is None: try: self.console_manager.writeline(message.get("line","")) except: pass self.wfile.write(json.dumps({})) elif command=="console_line_poll": try: lines=None lastid=message.get("lastlineid",None) counter=0 response={"status":"success"} while common.internet_thread.routine is None and lines is None and counter <3: if message.get("i_think_there_is_no_serial",False): response["parser"]=common.get_parser() break #para que se quite el mensaje de que no hay serial lastid,lines=self.console_manager.readlines(lastid) counter+=1 response["lines"]=lines response["lastlineid"]=lastid self.wfile.write(json.dumps(response)) except DeadSerialException: self.wfile.write(json.dumps({"status":"error","reason":"no-serial"})) except: self.wfile.write(json.dumps({"status":"error"})) elif command =="jog_pressed_key": global current_jogging global jogging_cancel_timer try: if common.internet_thread.routine is None: key=message.get("key",None) pressed=message.get("pressed",None) possibles={38:"Y",40:"Y-",37:"X-",39:"X",33:"Z",34:"Z-"} #http://keycode.info/ usb=ThreadsafeSerial.get() parser=common.get_parser() if key in possibles and pressed is not None: if jogging_cancel_timer is not None: jogging_cancel_timer.cancel() if not pressed or (current_jogging is not None and current_jogging!=key): if parser=="tiny-g": usb.write("!%\n") #grbl mandar (! y ctrl x? cuando se frene?) current_jogging=None else: if message.get("new_pressed",False) and current_jogging is None: if parser=="tiny-g": usb.write("G91 G0 "+possibles[key]+"1000\nG90\n") #grbl mandar lo mismo? current_jogging=key jogging_cancel_timer=threading.Timer(0.5,lambda: usb.write("!%\n" if parser=="tiny-g" else "")) jogging_cancel_timer.start() usb.flushInput() except DeadSerialException: pass self.wfile.write(json.dumps({})) else: common.internet_thread.manage_response(message) self.wfile.write(json.dumps({}))
from pprint import pprint from time import sleep import logging import praw from common import get_parser, log_in import logging logging.basicConfig(format='%(asctime)s - %(name)s - %(message)s', level=logging.INFO) subreddits = Counter() if __name__ == "__main__": args = get_parser() if args.debug: handler = logging.StreamHandler() prawlogger = logging.getLogger('prawcore') prawlogger.addHandler(handler) prawlogger.setLevel(logging.DEBUG) #logger.setLevel(logging.DEBUG) reddit = log_in() seed = reddit.subreddit(args.seed_subreddit) also_post_in = list() logging.info(f"Checking where users from /r/{seed} also post...") for post in getattr(seed, args.subreddit_mode)(limit=args.post_limit): user = reddit.redditor(post.author.name) logging.info(f"Checking where /u/{post.author.name} posts..") for comment in user.comments.new():
def do_POST(self): # if self.path=="/send_web": # # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # self.send_response(200) # self.end_headers() # textout = ["Nombre red: %s", # "Pswd: %s\n", # "Tu raspberry se esta conectando...", # "Si cometiste algun error, conectate al cable de Ethernet recarga la pagina y repite los pasos.", # ] # # self.wfile.write("SPKClient:. \n\n" + "\n".join(textout) % (form["ssid"].value,form["psk"].value)) # # if not spkwifi.set_wifi_network(form["ssid"].value.strip(), form["psk"].value.strip()): # self.wfile.write("Algo salio mal...") # # # return # if self.path=="/send_cncid": # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # # cncid= form["cncid"].value # # try: # if cncid.startswith("sudo"): # cncid = cncid.split("\"")[1] # # f = open(main.NEWCNCIDFILENAME,"w") # f.write(cncid) # f.close # # self.send_response(200) # self.end_headers() # # textout = ["ID: %s\n", # "Recarga la pagina en unos momentos.", # "Tu raspberry se esta reiniciando...", # "Revisa tu nueva maquina en el taller correspondiente." # ] # self.wfile.write("SPKClient:. \n\n" + "\n".join(textout) % (cncid)) # # common.restart_please() # #system("reboot") # #execl(sys.executable, *([sys.executable]+sys.argv)) # # except: # self.send_response(200) # self.end_headers() # self.wfile.write("Error, revisa el contenido de tu id") # # # return # if self.path=="/reset_client": # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # # # self.send_response(200) # self.end_headers() # self.wfile.write("El script de spkclient se esta reiniciando, ve a la pagina principal y recargala.") # common.restart_please() # return # if self.path=="/reset_pi": # form = cgi.FieldStorage( # fp=self.rfile, # headers=self.headers, # environ={'REQUEST_METHOD':'POST', # 'CONTENT_TYPE':self.headers['Content-Type'], # }) # # # self.send_response(200) # self.end_headers() # self.wfile.write("La raspberrypi se reiniciara...") # system("reboot") # return # if self.path == "/spkwizardsetup": data = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': self.headers['Content-Type'], }) action = data.getfirst("action", None) origin = self.headers.get("Origin", None) if not origin in ALLOWED_CONFIG_ORIGINS: self.send_response(403) return self.send_response(200) self.send_header('Content-type', "application/json") self.send_header('Access-Control-Allow-Origin', origin) self.end_headers() if action == "marco": if common.get_id() != None: self.wfile.write( json.dumps({"status": "already_configured"})) else: self.wfile.write( json.dumps({ "status": "polo", "current_network": spkwifi.get_current_connected_network() })) elif action == "get_network_status": self.wfile.write( json.dumps({ "status": "success", "available_networks": [n.ssid for n in spkwifi.get_available_networks()], "current_network": spkwifi.get_current_connected_network() })) elif action == "set_wifi_network": ssid = data.getfirst("ssid", None) password = data.getfirst("password", None) if spkwifi.set_wifi_network(ssid, password): self.wfile.write( json.dumps({ "status": "success", "current_network": spkwifi.get_current_connected_network() })) else: self.wfile.write( json.dumps({ "status": "error", "current_network": spkwifi.get_current_connected_network() })) elif action == "send_workshop": config_data = json.loads(data.getfirst("config_data", "{}")) response = common.send_config_info(config_data) if "extra_info" in response: self.wfile.write( json.dumps({ "status": "success", "extra_info": response["extra_info"] })) else: self.wfile.write( json.dumps({ "status": "error", "reason": response.get("reason", "") })) else: self.wfile.write(json.dumps({"status": "error"})) elif self.path == "/spkcnclocalcommandreceiver": data = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': self.headers['Content-Type'], }) origin = self.headers.get("Origin", None) local_token = data.getfirst("_local_token", None) prev_local_token = data.getfirst("_prev_local_token", None) if not origin in ALLOWED_CONFIG_ORIGINS or local_token == None or ( local_token != local_connection_token != prev_local_token): self.send_response(403) return self.send_response(200) self.send_header('Content-type', "application/json") self.send_header('Access-Control-Allow-Origin', origin) self.end_headers() message = json.loads(data.getfirst("message", None)) command = message.get("command", None) if command == "checking_same_network": self.wfile.write(json.dumps({"status": "sure :)"})) elif command == "lifeline": time.sleep(60) self.wfile.write(json.dumps({"status": "sure :)"})) elif command == "get_network_status": self.wfile.write( json.dumps({ "status": "success", "available_networks": [n.ssid for n in spkwifi.get_available_networks()], "current_network": spkwifi.get_current_connected_network() })) elif command == "get_current_network": self.wfile.write( json.dumps({ "status": "success", "current_network": spkwifi.get_current_connected_network() })) elif command == "set_wifi_network": ssid = message.get("ssid", None) password = message.get("password", None) if spkwifi.set_wifi_network(ssid, password): self.wfile.write( json.dumps({ "status": "success", "current_network": spkwifi.get_current_connected_network() })) else: self.wfile.write( json.dumps({ "status": "error", "current_network": spkwifi.get_current_connected_network() })) elif command == "get_script_status_change": while True: if common.internet_thread.routine is None or common.internet_thread.routine.status.as_dict( ) != message.get("last_status", None): break else: time.sleep(.01) if common.internet_thread.routine is not None: self.wfile.write( json.dumps({ "status": common.internet_thread.routine.status.as_dict() })) else: self.wfile.write( json.dumps({ "status": common.internet_thread.last_job_status.as_dict() if common.internet_thread.last_job_status is not None else None })) elif command == "send_console_line": if common.internet_thread.routine is None: try: self.console_manager.writeline(message.get("line", "")) except: pass self.wfile.write(json.dumps({})) elif command == "console_line_poll": try: lines = None lastid = message.get("lastlineid", None) counter = 0 response = {"status": "success"} while common.internet_thread.routine is None and lines is None and counter < 3: if message.get("i_think_there_is_no_serial", False): response["parser"] = common.get_parser() break #para que se quite el mensaje de que no hay serial lastid, lines = self.console_manager.readlines(lastid) counter += 1 response["lines"] = lines response["lastlineid"] = lastid self.wfile.write(json.dumps(response)) except DeadSerialException: self.wfile.write( json.dumps({ "status": "error", "reason": "no-serial" })) except: self.wfile.write(json.dumps({"status": "error"})) elif command == "jog_pressed_key": global current_jogging global jogging_cancel_timer try: if common.internet_thread.routine is None: key = message.get("key", None) pressed = message.get("pressed", None) possibles = { 38: "Y", 40: "Y-", 37: "X-", 39: "X", 33: "Z", 34: "Z-" } #http://keycode.info/ usb = ThreadsafeSerial.get() parser = common.get_parser() if key in possibles and pressed is not None: if jogging_cancel_timer is not None: jogging_cancel_timer.cancel() if not pressed or (current_jogging is not None and current_jogging != key): if parser == "tiny-g": usb.write("!%\n") #grbl mandar (! y ctrl x? cuando se frene?) current_jogging = None else: if message.get( "new_pressed", False) and current_jogging is None: if parser == "tiny-g": usb.write("G91 G0 " + possibles[key] + "1000\nG90\n") #grbl mandar lo mismo? current_jogging = key jogging_cancel_timer = threading.Timer( 0.5, lambda: usb.write("!%\n" if parser == "tiny-g" else "")) jogging_cancel_timer.start() usb.flushInput() except DeadSerialException: pass self.wfile.write(json.dumps({})) else: common.internet_thread.manage_response(message) self.wfile.write(json.dumps({}))
def run(self): self.status.set_can_stop(True) try: usb = ThreadsafeSerial.get() usb.flushInput() usb.flushOutput() except DeadSerialException: raise Exception("El puerto serial está desconectado") self.parser = common.get_parser() self.buffer_manager = BufferManager(self.parser) self.prepare_cnc() if self.routine_type == "cut": self.status.set_fase('downloading') g_code = urllib2.urlopen( self.arguments["gcode_url"]).read() #descargamos el código G lines = clean_lines(g_code.split("\n")) #lista de líneas de código self.exit_if_requested() with PeriodicQuestion(self.parser): speed = self.arguments["speed"] if speed != 1: lines = slow_down(lines, speed) start_line = self.arguments["start_line"] if start_line > len(lines): raise Exception( "La línea inicial es %d, pero el archivo sólo tiene %d líneas." % (start_line, len(lines))) if start_line > 0: previous_lines = lines[:start_line + 1] #es mas uno para incluir la actual en las busqedas lines = lines[start_line:] previous_lines.reverse() last_G = extract_first_match(previous_lines, ["G0", "G1", "G00", "G01"]) last_X = extract_float(previous_lines, "X") last_Y = extract_float(previous_lines, "Y") last_Z = extract_float(previous_lines, "Z") last_F = extract_float(previous_lines, "F") last_S = extract_float(previous_lines, "S") for h in [HALF_FILE_CODE_FIRST]: header = h[:] header = replace_variable( header, "LAST_X", "X" + last_X if last_X else "") header = replace_variable( header, "LAST_Y", "Y" + last_Y if last_Y else "") header = replace_variable( header, "LAST_Z", "Z" + last_Z if last_Z else "") header = replace_variable( header, "LAST_F", "F" + last_F if last_F else "") header = replace_variable( header, "LAST_S", "S" + last_S if last_S else "") header = replace_variable(header, "LAST_G", last_G if last_G else "") header = replace_variable( header, "SEC_PLANE", str(get_variable("security_plane"))) self.send_gcode(clean_lines(header), "preparing") self.send_gcode(lines, "cutting", start_line=start_line) elif self.routine_type == "home": self.home() elif self.routine_type == "probe": self.probe()
ssl_keyfile= '/container/certs/ca/kafka/certs/source-endpoint-mockup/key.pem') logger.info('Topic for the source is: {}'.format(source_id)) while True: ch_num = random.randint(0, channels - 1) ch_id = '{}{}'.format(source_id[:-10], ch_num) doc_size = round(random.gauss(mean_doc_size, sigma_doc_size)) doc = ''.join('*' * doc_size) logger.debug( 'Sending new message of size {} to channel {} on topic {}'.format( sys.getsizeof(doc), ch_id, source_id)) p.send(source_id, key=ch_id.encode('utf-8'), value=doc.encode('utf-8')) sleep_time = random.expovariate(exp_lambda) logger.debug('Sleeping {} seconds'.format(sleep_time)) time.sleep(sleep_time) if __name__ == '__main__': parser = common.get_parser() args = parser.parse_args() avg_events_sec = common.get_avg_events_sec(args.avg_events, args.time_unit) signal.signal(signal.SIGINT, exit_gracefully) logger.info('avg_events_sec %s', avg_events_sec) client = docker.from_env() container = int( client.containers.get(os.environ['HOSTNAME']).name.split('_')[-1]) run(args.doc_size, args.doc_size_sigma, avg_events_sec, container)
workers = workerpool.WorkerPool(size=opts.num_users, worker_factory=_daemon_worker_factory) for user_num in xrange(1, opts.num_users + 1): query_list = copy.deepcopy(queries) random.shuffle(query_list) workers.put(workerpool.SimpleJob(results, _run_queries_for_user, [opts, results_dir, run_num, user_num, query_list])) print 'waiting for jobs to complete in run ', run_num workers.join() workers.shutdown() print 'finished run', run_num if __name__ == '__main__': parser = get_parser('Run impala queries') parser.add_argument('--interactive', action='store_true', help='Run interactive queries') parser.add_argument('--reporting', action='store_true', help='Run reporting queries') parser.add_argument('--analytic', action='store_true', help='Run analytic queries') parser.add_argument('--num-users', type=int, help='Number of users to simulate', default=1) parser.add_argument('--num-runs', type=int, help='Number of runs for each test', default=3) parser.add_argument('--results-dir', dest='base_results_dir', default=os.path.join(os.path.dirname(__file__), '../results/'), help='Where to store results') parser.add_argument('--query-dir', default=os.path.join(os.path.dirname(__file__), '../queries-sql92-modified/queries/'), help='path to directory where queries are located') opts = parser.parse_args() run_queries(opts)