def __init__(self, complete_data): header_size = VariableHeader.struct_size header = complete_data[:header_size] self.vh = vh = VariableHeader(header) print "Blah:", hexbytes(vh.guid) assert vh.magic == self.HEADER_MAGIC, "bad magic 0x%x" % vh.magic total_length = vh.dsize + vh.nsize assert len(complete_data) >= total_length, "input not long enough" data = complete_data[header_size:] data = data[:total_length] nullterm = data.index("\x00\x00") + 1 strend = nullterm if nullterm < vh.nsize else vh.nsize self.name = data[:strend].decode("utf-16le") self.value = data[vh.nsize:total_length] # Set the checksum to 0, and the status to 0x7F fdata = substitute(header + data, header_size - 2, "\x00\x00") fdata = substitute(fdata, 2, "\x7F\x00") self.ccsum = self.checksum(fdata)
def plot_comparison_reconstruction(imgs: list, models: list, model_names: list, substitution_dict=None): if substitution_dict is None: substitution_dict = {} fig_width = COLUMN_WIDTH * INCHES_PER_PT fig_height = fig_width * GOLDEN_MEAN fig_size = [fig_width, fig_height] matplotlib.use("pgf") matplotlib.rcParams.update({ "pgf.texsystem": "pdflatex", 'font.family': 'serif', 'text.usetex': True, 'pgf.rcfonts': False, 'figure.figsize': fig_size }) fig, axs = plt.subplots(1, len(models) + 1, figsize=fig_size) for model, ax in zip([ModelMockup(imgs[0], 1)] + models, axs): plot_reconstruction_matrix(imgs, model, ax) axs[0].set_xlabel('Original') for ax, col in zip(axs[1:], model_names): if col in substitution_dict: col = substitute(col, substitution_dict) ax.set_xlabel(col) plt.tight_layout() plt.subplots_adjust(wspace=0.1, hspace=0.1) return fig, axs
def plot_comparison_disentanglement(imgs: list, models: list, model_names: list, feature_dict: dict, min_z=-6, max_z=6, num_steps=6, range_dict=None, substitution_dict=None): if substitution_dict is None: substitution_dict = {} if range_dict is None: range_dict = {} fig_width = COLUMN_WIDTH * 2 * INCHES_PER_PT fig_height = fig_width * GOLDEN_MEAN fig_size = [fig_width, fig_height] matplotlib.use("pgf") matplotlib.rcParams.update({ "pgf.texsystem": "pdflatex", 'font.family': 'serif', 'text.usetex': True, 'pgf.rcfonts': False, 'figure.figsize': fig_size }) fig, axs = plt.subplots(len(feature_dict), len(models), sharex='all', sharey='all', figsize=fig_size) for i, feature_name in enumerate(feature_dict.keys()): for j, (model, model_name) in enumerate(zip(models, model_names)): min_z_temp, max_z_temp = range_dict[ model_name] if model_name in range_dict else (min_z, max_z) plot_image_matrix(imgs, model, feature_dict[feature_name][j], axs[i, j], min_z=min_z_temp, max_z=max_z_temp, num_steps=num_steps) for ax, col in zip(axs[-1, :], model_names): min_z_temp, max_z_temp = range_dict[col] if col in range_dict else ( min_z, max_z) if col in substitution_dict: col = substitute(col, substitution_dict) ax.set_xlabel(f'{col} ({min_z_temp},{max_z_temp})') for ax, row in zip(axs[:, 0], feature_dict.keys()): ax.set_ylabel(row) plt.tight_layout() plt.subplots_adjust(wspace=0.1, hspace=0.1) return fig, axs
def cr_transform(transform, cr_next): """ transform coroutine pipe_in: dict_grep output or producer output: python list pipe_out: REST request """ while True: pipe_in = (yield) if cr_next: req_data = substitute(transform, *pipe_in) cr_next.send(req_data) # --> [REST request]
def pre_substitute_parameters(self): """Substitute model parameters into the variables of the model to improve speed. """ self.logger.debug('pre_substitute_parameters') self.sub_constants = False constants = self.constants self.M = self.M.subs(constants) self.tau_c = self.tau_c.subs(constants) self.tau_g = self.tau_g.subs(constants) self.tau_l = self.tau_l.subs(constants) self.tau_b = self.tau_b.subs(constants) self.f = self.f.subs(constants) self.lm = self.lm.subs(constants) self.lmd = self.lmd.subs(constants) self.lmdd = self.lmdd.subs(constants) self.R = self.R.subs(constants) self.RDot = self.RDot.subs(constants) self.RDotQDot = self.RDotQDot.subs(constants) self.RTDq = self.RTDq.subs(constants) self.ap = substitute(self.ap, constants) self.bp = substitute(self.bp, constants) self.bc = substitute(self.bc, constants) self.jc = substitute(self.jc, constants) self.ee = substitute(self.ee, constants) # self.L = self.L.subs(constants) # self.Lc = self.Lc.subs(constants) # self.Iz = self.Iz.subs(constants) self.xc = substitute(self.xc, constants) self.vc = substitute(self.vc, constants) self.Jc = substitute(self.Jc, constants)
def draw_model(self, q, in_deg, ax=None, scale=0.8, use_axis_limits=True, alpha=1.0, text=True): """Draws the 3D toy model. Parameters ---------- q: coordinate values in degrees or rad in_deg: True/False ax: axis 1D scale: if figure is small this helps in visualizing details use_axis_limits: use axis limits from max length """ if self.nd == 2: self.logger.debug('draw_model supports 3DoFs case') return constants = self.model_parameters(q=q, in_deg=in_deg) joints = substitute(self.jc, constants) muscle_a = substitute(self.ap, constants) muscle_b = substitute(self.bp, constants) end_effector = substitute(self.ee, constants) CoM = substitute(self.bc, constants) linewidth = 4 * scale gd_markersize = 14 * scale jc_markersize = 12 * scale mo_markersize = 7 * scale ef_markersize = 15 * scale fontsize = 12 * scale if ax == None: fig, ax = plt.subplots(1, 1, figsize=(5, 5)) # arm ax.plot([joints[0, 0], joints[1, 0]], [joints[0, 1], joints[1, 1]], 'r', linewidth=linewidth, alpha=alpha) # forearm ax.plot([muscle_b[5, 0], joints[2, 0]], [muscle_b[5, 1], joints[2, 1]], 'r', linewidth=linewidth, alpha=alpha) # hand ax.plot([muscle_b[7, 0], end_effector[0]], [muscle_b[7, 1], end_effector[1]], 'r', linewidth=linewidth, alpha=alpha) # muscles for i in range(0, muscle_a.shape[0]): ax.plot([muscle_a[i, 0], muscle_b[i, 0]], [muscle_a[i, 1], muscle_b[i, 1]], 'b', alpha=alpha) if text: ax.text(muscle_a[i, 0], muscle_a[i, 1], r'$a_' + str(i + 1) + '$', fontsize=fontsize, alpha=alpha) ax.text(muscle_b[i, 0], muscle_b[i, 1], r'$b_' + str(i + 1) + '$', fontsize=fontsize, alpha=alpha) ax.text((muscle_b[i, 0] + muscle_a[i, 0]) / 2.0, (muscle_b[i, 1] + muscle_a[i, 1]) / 2.0, r'$l_' + str(i + 1) + '$', fontsize=fontsize, alpha=alpha) # joint centers ax.plot(joints[:, 0], joints[:, 1], 'or', markersize=gd_markersize, alpha=alpha) if text: for i in range(0, joints.shape[0]): ax.text(joints[i, 0], joints[i, 1], r'$J_' + str(i + 1) + '$', fontsize=fontsize, alpha=alpha) # CoM ax.plot(CoM[:, 0], CoM[:, 1], 'oy', markersize=jc_markersize, alpha=alpha) if text: for i in range(0, CoM.shape[0]): ax.text(CoM[i, 0], CoM[i, 1], r'$Lc_' + str(i + 1) + '$', fontsize=fontsize, alpha=alpha) # end effector ax.plot(end_effector[0], end_effector[1], '<b', markersize=ef_markersize, alpha=alpha) # muscle origin ax.plot(muscle_a[:, 0], muscle_a[:, 1], 'dy', markersize=mo_markersize, alpha=alpha) # muscle insertion ax.plot(muscle_b[:, 0], muscle_b[:, 1], 'db', markersize=mo_markersize, alpha=alpha) ax.axis('equal') ax.set_title('Model Pose') ax.set_xlabel('$x \; (m)$') ax.set_ylabel('$y \; (m)$') # axis limits if use_axis_limits: L_max = self.constants[self.L[1]] + \ self.constants[self.L[2]] + self.constants[self.L[3]] ax.set_xlim([-L_max, L_max]) ax.set_ylim([-L_max / 2, 1.5 * L_max])
def build_contest_pdf(contest, problems, solutions=False, web=False): builddir = config.tmpdir / contest builddir.mkdir(parents=True, exist_ok=True) build_type = 'solution' if solutions else 'problem' main_file = 'solutions' if solutions else 'contest' main_file += '-web.tex' if web else '.tex' if solutions: ensure_symlink(builddir / 'solutions-base.tex', config.tools_root / 'latex/solutions-base.tex') ensure_symlink(builddir / 'bapc.cls', config.tools_root / 'latex/bapc.cls') ensure_symlink(builddir / 'images', config.tools_root / 'latex/images') ensure_symlink(builddir / main_file, config.tools_root / 'latex' / main_file) config_data = util.read_yaml(Path('contest.yaml')) config_data['testsession'] = '\\testsession' if config_data.get( 'testsession') else '' util.copy_and_substitute(config.tools_root / 'latex/contest-data.tex', builddir / 'contest_data.tex', config_data) ensure_symlink(builddir / 'logo.pdf', find_logo()) problems_data = '' # Link the solve stats directory if it exists. solve_stats = Path('solve_stats') if solve_stats.exists(): ensure_symlink(builddir / 'solve_stats', solve_stats) # include a header slide in the solutions PDF headertex = Path('solution_header.tex') if headertex.exists(): ensure_symlink(builddir / 'solution_header.tex', headertex) if solutions and headertex.exists(): problems_data += f'\\input{{{headertex}}}\n' per_problem_data = (config.tools_root / 'latex' / f'contest-{build_type}.tex').read_text() # Some logic to prevent duplicate problem IDs. for problem in problems: prepare_problem(problem) id_ok = True problems_data += util.substitute( per_problem_data, { 'problemlabel': problem.label, 'problemyamlname': problem.config['name'], 'problemauthor': problem.config.get('author'), 'timelimit': get_tl(problem.config), 'problemdir': config.tmpdir / problem.id, }) # include a statistics slide in the solutions PDF footer_tex = Path('solution_footer.tex') if footer_tex.exists(): ensure_symlink(builddir / 'solution_footer.tex', footer_tex) if solutions and footer_tex.exists(): problems_data += f'\\input{{{footer_tex}}}\n' (builddir / f'contest-{build_type}s.tex').write_text(problems_data) for i in range(3): ok, err, out = util.exec_command(PDFLATEX + [ '-output-directory', builddir, (builddir / main_file).with_suffix('.tex') ], 0, False, cwd=builddir, stdout=subprocess.PIPE) if ok is not True: print(f'{_c.red}Failure compiling pdf:{_c.reset}\n{out}') return False # link the output pdf output_pdf = Path(main_file).with_suffix('.pdf') ensure_symlink(output_pdf, builddir / output_pdf, True) print(f'{_c.green}Pdf written to {output_pdf}{_c.reset}') return True
def build_contest_pdf(contest, problems, tmpdir, solutions=False, web=False): builddir = tmpdir / contest builddir.mkdir(parents=True, exist_ok=True) build_type = 'solution' if solutions else 'problem' main_file = 'solutions' if solutions else 'contest' main_file += '-web.tex' if web else '.tex' default_config_data = { 'title': 'TITLE', 'subtitle': '', 'year': 'YEAR', 'author': 'AUTHOR', 'testsession': '', 'blank_page_text': '', } config_data = contest_yaml() for x in default_config_data: if x not in config_data: config_data[x] = default_config_data[x] config_data['testsession'] = '\\testsession' if config_data.get( 'testsession') else '' config_data['logofile'] = find_logo().as_posix() util.copy_and_substitute(config.tools_root / 'latex/contest-data.tex', builddir / 'contest_data.tex', config_data) problems_data = '' if solutions: # include a header slide in the solutions PDF headertex = Path('solution_header.tex') if headertex.exists(): problems_data += f'\\input{{{headertex}}}\n' per_problem_data = (config.tools_root / 'latex' / f'contest-{build_type}.tex').read_text() for problem in problems: if build_type == 'problem': prepare_problem(problem) if solutions: if not (problem.path / 'problem_statement/solution.tex').is_file(): warn(f'solution.tex not found for problem {problem.name}') continue problems_data += util.substitute( per_problem_data, { 'problemlabel': problem.label, 'problemyamlname': problem.settings.name.replace('_', ' '), 'problemauthor': problem.settings.author, 'timelimit': get_tl(problem), 'problemdir': problem.path.absolute().as_posix(), 'builddir': problem.tmpdir.as_posix(), }, ) if solutions: # include a statistics slide in the solutions PDF footer_tex = Path('solution_footer.tex') if footer_tex.exists(): problems_data += f'\\input{{{footer_tex}}}\n' (builddir / f'contest-{build_type}s.tex').write_text(problems_data) return build_latex_pdf(builddir, config.tools_root / 'latex' / main_file)
def build_contest_pdf(contest, problems, tmpdir, solutions=False, web=False): builddir = tmpdir / contest builddir.mkdir(parents=True, exist_ok=True) build_type = 'solution' if solutions else 'problem' main_file = 'solutions' if solutions else 'contest' main_file += '-web.tex' if web else '.tex' config_data = util.read_yaml(Path('contest.yaml')) config_data['testsession'] = '\\testsession' if config_data.get('testsession') else '' config_data['logofile'] = find_logo().as_posix() util.copy_and_substitute(config.tools_root / 'latex/contest-data.tex', builddir / 'contest_data.tex', config_data) problems_data = '' if solutions: # Link the solve stats directory if it exists. solve_stats = Path('solve_stats') # include a header slide in the solutions PDF headertex = Path('solution_header.tex') if headertex.exists(): problems_data += f'\\input{{{headertex}}}\n' per_problem_data = (config.tools_root / 'latex' / f'contest-{build_type}.tex').read_text() # Some logic to prevent duplicate problem IDs. for problem in problems: if build_type == 'problem': prepare_problem(problem) problems_data += util.substitute( per_problem_data, { 'problemlabel': problem.label, 'problemyamlname': problem.settings.name.replace('_', ' '), 'problemauthor': problem.settings.author, 'timelimit': problem.settings.timelimit, 'problemdir': problem.path.absolute().as_posix(), 'builddir': problem.tmpdir.as_posix(), }) if solutions: # include a statistics slide in the solutions PDF footer_tex = Path('solution_footer.tex') if footer_tex.exists(): problems_data += f'\\input{{{footer_tex}}}\n' (builddir / f'contest-{build_type}s.tex').write_text(problems_data) env = get_environment() for i in range(3): ret = util.exec_command( PDFLATEX + ['-output-directory', builddir, config.tools_root / 'latex' / main_file], 0, False, cwd=builddir, stdout=subprocess.PIPE, env=env, ) if ret.ok is not True: print(f'{Fore.RED}Failure compiling pdf:{Style.RESET_ALL}\n{ret.out}') return False # link the output pdf output_pdf = Path(main_file).with_suffix('.pdf') ensure_symlink(output_pdf, builddir / output_pdf, True) print(f'{Fore.GREEN}Pdf written to {output_pdf}{Style.RESET_ALL}') return True
def sub(s): return util.substitute(s, workflow.variables)