How to use set_file method in avocado

Best Python code snippet using avocado_python

catia.py

Source:catia.py Github

copy

Full Screen

1###############################################################################2# #3# Copyright (C) 2013-2014 Edward d'Auvergne #4# #5# This file is part of the program relax (http://www.nmr-relax.com). #6# #7# This program is free software: you can redistribute it and/or modify #8# it under the terms of the GNU General Public License as published by #9# the Free Software Foundation, either version 3 of the License, or #10# (at your option) any later version. #11# #12# This program is distributed in the hope that it will be useful, #13# but WITHOUT ANY WARRANTY; without even the implied warranty of #14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #15# GNU General Public License for more details. #16# #17# You should have received a copy of the GNU General Public License #18# along with this program. If not, see <http://www.gnu.org/licenses/>. #19# #20###############################################################################21# Module docstring.22"""Functions for interfacing with Flemming Hansen's CATIA program."""23# Dependencies.24import dep_check25# Python module imports.26from os import F_OK, access, chdir, getcwd, sep27PIPE, Popen = None, None28if dep_check.subprocess_module:29 from subprocess import PIPE, Popen30import sys31# relax module imports.32from lib.dispersion.variables import EXP_TYPE_CPMG_SQ33from lib.errors import RelaxError, RelaxDirError34from lib.io import mkdir_nofail, open_write_file, test_binary35from pipe_control.mol_res_spin import check_mol_res_spin_data, spin_loop36from pipe_control.pipes import check_pipe37from specific_analyses.relax_disp.checks import check_model_type, check_spectra_id_setup38from specific_analyses.relax_disp.data import loop_frq, loop_offset_point, return_param_key_from_data39def catia_execute(file='Fit.catia', dir=None, binary=None):40 """Create the CATIA input files.41 @keyword file: The main CATIA execution file.42 @type file: str43 @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory.44 @type dir: str or None45 @keyword binary: The name of the CATIA binary file. This can include the path to the binary.46 @type binary: str47 """48 # The current directory.49 orig_dir = getcwd()50 # Test the binary file string corresponds to a valid executable.51 test_binary(binary)52 # The directory.53 if dir == None:54 dir = orig_dir55 if not access(dir, F_OK):56 raise RelaxDirError('CATIA', dir)57 # Change to the directory with the CATIA input files.58 chdir(dir)59 # Catch failures and return to the correct directory.60 try:61 # Execute CATIA.62 cmd = binary + ' < Fit.catia'63 pipe = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=False)64 # Close the pipe.65 pipe.stdin.close()66 # Write to stdout.67 for line in pipe.stdout.readlines():68 # Decode Python 3 byte arrays.69 if hasattr(line, 'decode'):70 line = line.decode()71 # Write.72 sys.stdout.write(line)73 # Write to stderr.74 for line in pipe.stderr.readlines():75 # Decode Python 3 byte arrays.76 if hasattr(line, 'decode'):77 line = line.decode()78 # Write.79 sys.stderr.write(line)80 # Failure.81 except:82 # Change back to the original directory.83 chdir(orig_dir)84 # Reraise the error.85 raise86 # Change back to the original directory.87 chdir(orig_dir)88def catia_input(file='Fit.catia', dir=None, output_dir='output', force=False):89 """Create the CATIA input files.90 @keyword file: The main CATIA execution file.91 @type file: str92 @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory.93 @type dir: str or None94 @keyword output_dir: The CATIA output directory, located within the directory specified by the dir argument. This directory will be created.95 @type output_dir: str96 @keyword force: A flag which if True will cause all pre-existing files to be overwritten.97 @type force: Bool98 """99 # Data checks.100 check_pipe()101 check_mol_res_spin_data()102 check_spectra_id_setup()103 check_model_type()104 # Check that this is CPMG data.105 for id in cdp.spectrum_ids:106 if cdp.exp_type[id] != 'SQ CPMG':107 raise RelaxError("Only CPMG type data is supported.")108 # Directory creation.109 if dir != None:110 mkdir_nofail(dir, verbosity=0)111 # Create the R2eff files.112 write_r2eff_files(input_dir='input_r2eff', base_dir=dir, force=force)113 # Create the parameter files.114 write_param_files(global_file="ParamGlobal.inp", set_file="ParamSet1.inp", dir=dir, force=force)115 # Create the main execution file.116 write_main_file(file=file, dir=dir, output_dir=output_dir, force=force)117 # Create the output directory as needed by CATIA (it does not create it itself).118 mkdir_nofail(dir + sep + output_dir, verbosity=0)119def write_main_file(file=None, dir=None, output_dir=None, f_tol=1e-25, max_iter=10000000, r1=False, force=False):120 """Create the main CATIA execution file.121 @keyword file: The main CATIA execution file.122 @type file: str123 @keyword dir: The directory to place the files into.124 @type dir: str or None125 @keyword output_dir: The CATIA output directory, located within the directory specified by the dir argument. This directory will be created.126 @type output_dir: str127 @keyword r1: A flag which if True will cause the R1 data to be used for off-resonance effects.128 @type r1: bool129 @keyword force: A flag which if True will cause a pre-existing file to be overwritten.130 @type force: bool131 """132 # The file.133 catia_in = open_write_file(file_name=file, dir=dir, force=force)134 # The R2eff input sets.135 for frq in loop_frq():136 frq_label = int(frq*1e-6)137 file_name = "data_set_%i.inp" % frq_label138 catia_in.write("ReadDataset(%s)\n" % file_name)139 # Write out the data.140 catia_in.write("ReadParam_Global(ParamGlobal.inp)\n")141 catia_in.write("ReadParam_Local(ParamSet1.inp)\n")142 catia_in.write("\n")143 # The R1 data for off-resonance effects.144 if r1:145 catia_in.write("ReadParam(Omega;%s;0;1)\n" % shift_file)146 for frq in loop_frq():147 frq_label = int(frq*1e-6)148 param = "R1iph_%s" % frq_label149 r1_file = "R1_%s.out" % frq_label150 catia_in.write("ReadParam(%s;%s;0;1)\n" % (param, r1_file))151 catia_in.write("\n")152 # Fix these off-resonance parameters.153 catia_in.write("FreeLocalParam(all;Omega;false)\n")154 for frq in loop_frq():155 frq_label = int(frq*1e-6)156 param = "R1iph_%s" % frq_label157 catia_in.write("FreeLocalParam(all;%s;false)\n" % param)158 catia_in.write("\n")159 # Minimisation.160 catia_in.write("Minimize(print=y;tol=%s;maxiter=%i)\n" % (f_tol, max_iter))161 catia_in.write("\n")162 # Plotting.163 catia_in.write("PrintParam(%s/GlobalParam.fit;global)\n" % output_dir)164 catia_in.write("PrintParam(%s/DeltaOmega.fit;DeltaO)\n" % output_dir)165 catia_in.write("PrintData(%s/)\n" % output_dir)166 catia_in.write("\n")167 # Calculate the chi-squared value (not sure why, it's calculated in the minimisation).168 catia_in.write("ChiSq(all;all)\n")169 # Exit the program.170 catia_in.write("exit(0)\n")171 # Close the file.172 catia_in.close()173def write_param_files(global_file=None, kex=1500.0, pA=0.95, set_file=None, dir=None, r1=False, force=False):174 """Create the CATIA parameter files.175 @keyword global_file: The name of the global parameter file.176 @type global_file: str177 @keyword set_file: The name of the parameter set file.178 @type set_file: str179 @keyword dir: The base directory to place the files into.180 @type dir: str181 @keyword r1: A flag which if True will cause the R1 data to be used for off-resonance effects.182 @type r1: bool183 @keyword force: A flag which if True will cause a pre-existing file to be overwritten.184 @type force: bool185 """186 # Open the global parameter file.187 param_file = open_write_file(file_name=global_file, dir=dir, force=force)188 # Set the starting values.189 param_file.write("kex = %s\n" % kex)190 param_file.write("pb = %s\n" % (1.0-pA))191 # Close the file.192 param_file.close()193 # Open the 1st parameter set file.194 set_file = open_write_file(file_name=set_file, dir=dir, force=force)195 # The parameter format.196 params = ['Delta0']197 values = [0.5]198 if r1:199 for frq in loop_frq():200 params.append("R1iph_%s" % frq_label(frq))201 values.append(1.5)202 for frq in loop_frq():203 params.append("R0iph_%s" % frq_label(frq))204 values.append(5.0)205 if r1:206 params.append("Omega")207 values.append(0.0)208 # Write out the format.209 set_file.write("format = (")210 for i in range(len(params)):211 if i != 0:212 set_file.write(';')213 set_file.write(params[i])214 set_file.write(")\n")215 # Um?!? The average values maybe?216 set_file.write("* = (")217 for i in range(len(values)):218 if i != 0:219 set_file.write(';')220 set_file.write("%s" % values[i])221 set_file.write(")\n")222 # Close the file.223 set_file.close()224def frq_label(frq):225 """Convert the frequency in Hz to a label in MHz.226 @param frq: The frequency to convert.227 @type frq: float228 @return: The frequency in MHz as a label.229 @rtype: str230 """231 # Convert and return.232 return str(int(frq*1e-6))233def write_r2eff_files(input_dir=None, base_dir=None, force=False):234 """Create the CATIA R2eff input files.235 @keyword input_dir: The special directory for the R2eff input files.236 @type input_dir: str237 @keyword base_dir: The base directory to place the files into.238 @type base_dir: str239 @keyword force: A flag which if True will cause a pre-existing file to be overwritten.240 @type force: bool241 """242 # Create the directory for the R2eff files for each field and spin.243 dir = base_dir + sep + input_dir244 mkdir_nofail(dir, verbosity=0)245 # Determine the isotope information.246 isotope = None247 for spin in spin_loop(skip_desel=True):248 if hasattr(spin, 'isotope'):249 if isotope == None:250 isotope = spin.isotope251 elif spin.isotope != isotope:252 raise RelaxError("CATIA only supports one spin type.")253 if isotope == None:254 raise RelaxError("The spin isotopes have not been specified.")255 # Isotope translation.256 if isotope == '1H':257 isotope = 'H1'258 elif isotope == '13C':259 isotope = 'C13'260 elif isotope == '15N':261 isotope = 'N15'262 # Loop over the frequencies.263 for frq, mi in loop_frq(return_indices=True):264 # The frequency string in MHz.265 frq_string = int(frq*1e-6)266 # The set files.267 file_name = "data_set_%i.inp" % frq_string268 set_file = open_write_file(file_name=file_name, dir=base_dir, force=force)269 id = frq_string270 set_file.write("ID=%s\n" % id)271 set_file.write("Sfrq = %s\n" % frq_string)272 set_file.write("Temperature = %s\n" % 0.0)273 set_file.write("Nucleus = %s\n" % isotope)274 set_file.write("Couplednucleus = %s\n" % 'H1')275 set_file.write("Time_equil = %s\n" % 0.0)276 set_file.write("Pwx_cp = %s\n" % 0.0)277 set_file.write("Taub = %s\n" % 0.0)278 set_file.write("Time_T2 = %s\n"% cdp.relax_time_list[0])279 set_file.write("Xcar = %s\n" % 0.0)280 set_file.write("Seqfil = %s\n" % 'CW_CPMG')281 set_file.write("Minerror = %s\n" % "(2.%;0.5/s)")282 set_file.write("Basis = (%s)\n" % "Iph_7")283 set_file.write("Format = (%i;%i;%i)\n" % (0, 1, 2))284 set_file.write("DataDirectory = %s\n" % (dir+sep))285 set_file.write("Data = (\n")286 # Loop over the spins.287 for spin, mol_name, res_num, res_name, spin_id in spin_loop(full_info=True, return_id=True, skip_desel=True):288 # The file.289 file_name = "spin%s_%i.cpmg" % (spin_id.replace('#', '_').replace(':', '_').replace('@', '_'), frq_string)290 spin_file = open_write_file(file_name=file_name, dir=dir, force=force)291 # Write the header.292 spin_file.write("# %18s %20s %20s\n" % ("nu_cpmg(Hz)", "R2(1/s)", "Esd(R2)"))293 # Loop over the dispersion points.294 for offset, point, oi, di in loop_offset_point(exp_type=EXP_TYPE_CPMG_SQ, frq=frq, return_indices=True):295 # The key.296 key = return_param_key_from_data(exp_type=EXP_TYPE_CPMG_SQ, frq=frq, offset=offset, point=point)297 # No data.298 if key not in spin.r2eff:299 continue300 # Write out the data.301 spin_file.write("%20.15f %20.15f %20.15f\n" % (point, spin.r2eff[key], spin.r2eff_err[key]))302 # Close the file.303 spin_file.close()304 # Add the file name to the set.305 catia_spin_id = "%i%s" % (res_num, spin.name)306 set_file.write(" [%s;%s];\n" % (catia_spin_id, file_name))307 # Terminate the set file.308 set_file.write(")\n")...

Full Screen

Full Screen

parsemetsv2folia.py

Source:parsemetsv2folia.py Github

copy

Full Screen

1#!/usr/bin/env python32import argparse3import sys4import os5try:6 from pynlpl.formats import folia7except ImportError:8 print("ERROR: PyNLPl not found, please install pynlpl (pip install pynlpl)",file=sys.stderr)9 sys.exit(2)10#sys.path.append(os.path.dirname(__file__))11#import tsvlib12#import tsv2folia.tsvlib as tsvlib # USE THIS LINE ON FLAT SERVER but comment it out when testing locally13import tsvlib # USE THIS LINE WHEN TESTING LOCALLY but do not forget to comment this line out when committing14EMPTY = ['', '_']15POS_SET_URL = "https://github.com/proycon/parseme-support/raw/master/parseme-pos.foliaset.xml"16# The TSV file shouls have the following fields:17# 0: token ID (restarting from 1 at every sentence)18# 1: surface form19# 2: nsp (optional) -> the string 'nsp' if the current token should be appended to the previous without space 20# 3: MWEs (optional) -> the list of MWEs this word belongs to in the format 1[:MWE_type1]; 2[:MWE_type2]; ...21def convert(filename, targetfilename, rtl, lang_set_file):22 doc = folia.Document(id=os.path.basename(filename.replace('.tsv','')))23 if rtl:24 doc.metadata['direction'] = 'rtl'25 doc.metadata['status'] = 'untouched'26 doc.declare(folia.Entity, lang_set_file) #ENTITY-SET definition 27 doc.declare(folia.AnnotationType.POS, set=POS_SET_URL) #POS-SET definition 28 text = doc.append(folia.Text)29 with open(filename,'r',encoding='utf-8') as f:30 for tsv_sentence in tsvlib.iter_tsv_sentences(f):31 folia_sentence = folia.Sentence(doc, generate_id_in=text)32 text.append(folia_sentence)33 for tsv_word in tsv_sentence:34 folia_word = folia.Word(doc, text=tsv_word.surface, space=(not tsv_word.nsp), generate_id_in=folia_sentence)35 folia_sentence.append(folia_word)36 if tsv_word.pos:37 folia_word.append(folia.PosAnnotation(doc, cls=tsv_word.pos, annotator="auto", annotatortype=folia.AnnotatorType.AUTO))38 mwe_infos = tsv_sentence.mwe_infos()39 if mwe_infos:40 folia_mwe_list = folia.EntitiesLayer(doc)41 folia_sentence.append(folia_mwe_list)42 for mweid, mweinfo in mwe_infos.items():43 assert mweinfo.category, "Conversion to FoLiA requires all MWEs to have a category" # checkme44 folia_words = [folia_sentence[i] for i in mweinfo.word_indexes]45 folia_mwe_list.append(folia.Entity, *folia_words, cls=mweinfo.category, annotatortype=folia.AnnotatorType.MANUAL)46 doc.save(targetfilename)47#This function can be called directly by FLAT48def flat_convert(filename, targetfilename, *args, **kwargs):49 if 'rtl' in kwargs and kwargs['rtl']:50 rtl=True51 else:52 rtl = False53 setdefinition = kwargs['flatconfiguration']['annotationfocusset']54 try:55 convert(filename, targetfilename, rtl, setdefinition)56 except Exception as e:57 return False, e.__class__.__name__ + ': ' + str(e)58 return True59set_path = 'https://github.com/proycon/parseme-support/raw/master/'60set_options = {61 #"parseme": {62 # 'set_file': set_path + "parseme-mwe.foliaset.xml",63 # 'rtl': False64 #}65 "english": {66 'set_file': set_path + "parseme-mwe-en.foliaset.xml",67 'rtl': False68 },69 "farsi": {70 'set_file': set_path + "parseme-mwe-farsi.foliaset.xml",71 'rtl': True72 },73 "germanic": {74 'set_file': set_path + "parseme-mwe-germanic.foliaset.xml",75 'rtl': False76 }, 77 "hebrew": {78 'set_file': set_path + "parseme-mwe-hebrew.foliaset.xml",79 'rtl': True80 },81 "lithuanian": {82 'set_file': set_path + "parseme-mwe-lt.foliaset.xml",83 'rtl': False84 },85 "other": {86 'set_file': set_path + "parseme-mwe-other.foliaset.xml",87 'rtl': False88 },89 "romance": {90 'set_file': set_path + "parseme-mwe-romance.foliaset.xml",91 'rtl': False92 },93 "italian": {94 'set_file': set_path + "parseme-mwe-it.foliaset.xml",95 'rtl': False96 },97 "portuguese": {98 'set_file': set_path + "parseme-mwe-pt.foliaset.xml",99 'rtl': False100 },101 "slavic": {102 'set_file': set_path + "parseme-mwe-slavic.foliaset.xml",103 'rtl': False104 },105 "yiddish": {106 'set_file': set_path + "parseme-mwe-yiddish.foliaset.xml",107 'rtl': True108 },109 "maltese": {110 'set_file': set_path + "parseme-mwe-maltese.foliaset.xml",111 'rtl': False112 },113}114def valid_input_file(file_name):115 if not file_name.endswith('.tsv') and not file_name.endswith('.parsemetsv'):116 raise argparse.ArgumentTypeError("File name must end with '.tsv'")117 return file_name118parser = argparse.ArgumentParser(description="Convert from TSV to FoLiA XML.")119parser.add_argument("FILE", type=valid_input_file, help="An input TSV file")120parser.add_argument("--stdout", action="store_true", help="Output data in stdout")121parser.add_argument("--language", type = str.lower, choices = set_options.keys(), help="The input language", required=True) 122 123class Main(object):124 def __init__(self, args):125 self.args = args126 def run(self):127 sys.excepthook = tsvlib.excepthook128 lang_options = set_options[self.args.language]129 filename = self.args.FILE130 targetfilename = "/dev/stdout" if self.args.stdout else filename.rsplit(".",1)[0] + '.folia.xml'131 rtl = lang_options['rtl']132 lang_set_file = lang_options['set_file'] 133 convert(filename, targetfilename, rtl, lang_set_file)134def main():135 Main(parser.parse_args()).run()136#####################################################137if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful