Python Code Examples for read input file

18 Python code examples are found related to "read input file". These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Project: TEPIC   Author: SchulzLab   File: findBackground.py    License: MIT License 6 votes vote down vote up
def read_input_file(fpath):
    """
    :param fpath:
    :return:
    """
    foreground = col.defaultdict(list)
    with open(fpath, 'r') as inputfile:
        rows = csv.reader(inputfile, delimiter='\t')
        try:
            for idx, row in enumerate(rows, start=1):
                region = {'name': idx, 'chrom': row[0],
                          'start': int(row[1]), 'end': int(row[2]),
                          'length': int(row[2]) - int(row[1])}
                foreground[row[0]].append(region)
        except (ValueError, IndexError, TypeError) as err:
            sys.stderr.write('\nError processing line {} in input file {}\nRecord: {}\n'.format(idx, fpath, row))
            raise err
    assert foreground, 'No input regions read from file {}'.format(fpath)
    return foreground 
Example 2
Project: Wordlister   Author: 4n4nk3   File: wordlister.py    License: GNU General Public License v3.0 6 votes vote down vote up
def read_input_file(file_path: str) -> set:
    """
    Read input file and return a set of words.

    :param file_path: the path to the file with the user supplied wordlist
    :type file_path: str
    :return _: set
    """

    try:
        with open(file_path, 'r') as input_file:
            data = input_file.read().splitlines()
    except FileNotFoundError:
        print('Input file not found!\nExiting...')
        exit(1)
    # Apply capitalize mutagen and upper mutagen if needed
    input_list = set(data)
    for word in data:
        if args.cap is True:
            input_list.add(word.capitalize())
        if args.up is True:
            input_list.add(word.upper())
    return input_list 
Example 3
Project: Intelligent-Projects-Using-Python   Author: PacktPublishing   File: MachineTranslation_word2vec.py    License: MIT License 6 votes vote down vote up
def read_input_file(self,path,num_samples=10e13):
        input_texts = []
        target_texts = []
        input_words = set()
        target_words = set()

        
        
        with codecs.open(path, 'r', encoding='utf-8') as f:
            lines = f.read().split('\n')

        for line in lines[: min(num_samples, len(lines) - 1)]:
            input_text, target_text = line.split('\t')  # \t as the start of sequence 
            target_text = '\t ' + target_text + ' \n'   # \n as the end  of sequence
            input_texts.append(input_text)
            target_texts.append(target_text)
            for word in input_text.split(" "):
                if word not in input_words:
                    input_words.add(word)
            for word in target_text.split(" "):
                if word not in target_words:
                    target_words.add(word)

        return input_texts,target_texts,input_words,target_words 
Example 4
Project: Menotexport   Author: Xunius   File: latex2text.py    License: GNU General Public License v3.0 6 votes vote down vote up
def read_input_file(self, fn):
        fnfull = os.path.realpath(os.path.join(self.tex_input_directory, fn))
        if self.strict_input:
            # make sure that the input file is strictly within dirfull, and didn't escape with
            # '../..' tricks or via symlinks.
            dirfull = os.path.realpath(self.tex_input_directory)
            if not fnfull.startswith(dirfull):
                logger.warning("Can't access path '%s' leading outside of mandated directory [strict input mode]",
                               fn)
                return ''

        if not os.path.exists(fnfull) and os.path.exists(fnfull + '.tex'):
            fnfull = fnfull + '.tex'
        if not os.path.exists(fnfull) and os.path.exists(fnfull + '.latex'):
            fnfull = fnfull + '.latex'
        if not os.path.isfile(fnfull):
            logger.warning(u"Error, file doesn't exist: '%s'", fn)
            return ''
        
        try:
            with open(fnfull) as f:
                return f.read()
        except IOError as e:
            logger.warning(u"Error, can't access '%s': %s", fn, e)
            return '' 
Example 5
Project: flee-release   Author: djgroen   File: coupling.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def readInputFromFile(self, t):
    """
    Returns a dictionary with key <coupling name> and value <number of agents>.
    """
    in_fname = "%s.%s.csv" % (self.inputfilename, t)

    print("Couple: searching for", in_fname, file=sys.stderr)
    while not os.path.exists(in_fname):
      time.sleep(0.1)
    time.sleep(0.001)

    csv_string = ""
    with open("%s.%s.csv" % (self.inputfilename, t)) as csvfile:
      csv_string = csvfile.read().split('\n')

    return self.extractNewAgentsFromCSVString(csv_string) 
Example 6
Project: OLive   Author: microsoft   File: check_model.py    License: MIT License 5 votes vote down vote up
def readInputFromFile(full_path):
    t = onnx.TensorProto()
    with open(full_path, 'rb') as f:
        t.ParseFromString(f.read())
    return t


# Generate a {input/output_name: input/output_arr} dictionary 
Example 7
Project: adam_qas   Author: 5hirish   File: question_classifier_trainer.py    License: GNU General Public License v3.0 5 votes vote down vote up
def read_input_file(raw_data_file, training_data_path, en_nlp):

    with open(training_data_path, 'a', newline='') as csv_fp:
        csv_fp_writer = csv.writer(csv_fp, delimiter='|')
        for row in raw_data_file:
            list_row = row.split(" ")
            question_class_list = list_row[0].split(":")
            question = " ".join(list_row[1:len(list_row)])
            question = question.strip("\n")
            question_class = question_class_list[0]

            process_question(question, question_class, en_nlp, training_data_path, csv_fp_writer)

        csv_fp.close() 
Example 8
Project: nlp-architect   Author: NervanaSystems   File: prepare_data.py    License: Apache License 2.0 5 votes vote down vote up
def read_inference_input_examples_file(input_examples_file):
    """
    read inference input examples file

    Args:
        input_examples_file(str): inference input file containing a vector of target word

    Returns:
        list(str): target word vector

    """
    with open(input_examples_file, "rU", encoding="utf-8") as file:
        reader = csv.reader((line.replace("\0", "") for line in file))
        cntr2 = 0
        # 1. read csv file
        target_word_vec1 = []
        header_line_flag = True
        for line in reader:
            if line is not None:
                if header_line_flag:  # skip header line
                    header_line_flag = False
                    continue
                target_word_vec1.insert(cntr2, line[0])

                cntr2 = cntr2 + 1

    return target_word_vec1 
Example 9
Project: phageParser   Author: phageParser   File: addRepeatAndSpacersToDB.py    License: MIT License 5 votes vote down vote up
def read_input_file(filename):
    fin = open(filename, 'r')
    flines = fin.readlines()
    file_dict = dict()
    for f in flines:
        f = f.replace('\n', '')
        if f[0] == '>':
            accessions = f[1:].split('|')
        else:
            file_dict[f] = accessions
    return file_dict 
Example 10
Project: JointBERT   Author: monologg   File: predict.py    License: Apache License 2.0 5 votes vote down vote up
def read_input_file(pred_config):
    lines = []
    with open(pred_config.input_file, "r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            words = line.split()
            lines.append(words)

    return lines 
Example 11
Project: JetPack   Author: dsp-jetpack   File: gen_ini_file.py    License: Apache License 2.0 5 votes vote down vote up
def read_input_file(input_file):
    idata = {}
    with open(input_file, 'r') as f:
        next(f)  # skip first line
        for line in f:
            line = line.strip()
            if len(line) > 2:
                fields = line.split("|")
                if len(fields) == 2:
                    idata[fields[0]] = fields[1]
    return idata 
Example 12
Project: KoBERT-NER   Author: monologg   File: predict.py    License: Apache License 2.0 5 votes vote down vote up
def read_input_file(pred_config):
    lines = []
    with open(pred_config.input_file, "r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            words = line.split()
            lines.append(words)

    return lines 
Example 13
Project: anvio   Author: merenlab   File: genomedescriptions.py    License: GNU General Public License v3.0 5 votes vote down vote up
def read_paths_from_input_file(self):
        """Reads metagenome files, populates self.metagenomes"""

        columns = utils.get_columns_of_TAB_delim_file(self.input_file_for_metagenomes)

        if 'profile_db_path' in columns:
            fields_for_metagenomes_input = ['name', 'contigs_db_path', 'profile_db_path']
            self.profile_dbs_available = True
        else:
            fields_for_metagenomes_input = ['name', 'contigs_db_path']
            self.profile_dbs_available = False

        self.metagenomes_dict = utils.get_TAB_delimited_file_as_dictionary(self.input_file_for_metagenomes, expected_fields=fields_for_metagenomes_input) if self.input_file_for_metagenomes else {} 
Example 14
Project: genmon   Author: jgyates   File: modbus_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def ReadInputFileThread(self):

        while True:
            if self.IsStopSignaled("ReadInputFileThread"):
                break
            self.ReadInputFile(self.InputFile)
            if not self.AdjustInputData():
                self.LogInfo("Error parsing input data")
            time.sleep(5)

    #-------------ModbusBase::ProcessMasterSlaveWriteTransaction---------------- 
Example 15
Project: NeMo   Author: NVIDIA   File: lasertagger_preprocessor.py    License: Apache License 2.0 5 votes vote down vote up
def read_input_file(
    args, input_file, output_arbitrary_targets_for_infeasible_examples=False, save_tokens=False, infer=False
):
    '''Reads in Tab Separated Value file and converts to training/infernece-ready examples.

    Args:
        args: Parsed args returned by the parse_args().
        input_file: Path to the TSV input file.
        output_arbitrary_targets_for_infeasible_examples: Set this to True when preprocessing 
            the development set. Determines whether to output a TF example also for sources 
            that can not be converted to target via the available tagging operations. In these 
            cases, the target ids will correspond to the tag sequence KEEP-DELETE-KEEP-DELETE... 
            which should be very unlikely to be predicted by chance. This will be useful for 
            getting more accurate eval scores during training.
        save_tokens: To save tokens required in example.task, only needs to be True for testing.
        infer: Whether test files or not.

    Returns:
        examples: List of converted examples(features and Editing Tasks).
        saved_tokens: List of additional out-of-vocab special tokens in test files.
    '''

    label_map = utils.read_label_map(args.label_map_file)
    converter = tagging_converter.TaggingConverter(
        tagging_converter.get_phrase_vocabulary_from_label_map(label_map), True
    )
    builder = bert_example.BertExampleBuilder(
        label_map, args.pretrained_model_name, args.max_seq_length, False, converter
    )
    examples = deque()
    for i, (sources, target) in enumerate(utils.yield_sources_and_targets(input_file)):
        if len(examples) % 1000 == 0:
            logging.info("{} examples processed.".format(len(examples)))
        example = builder.build_bert_example(
            sources, target, output_arbitrary_targets_for_infeasible_examples, save_tokens, infer
        )
        if example is None:
            continue
        examples.append(example)
    logging.info(f'Done. {len(examples)} examples converted.')
    return examples, builder.get_special_tokens_and_ids() 
Example 16
Project: pylatexenc   Author: phfaist   File: __init__.py    License: MIT License 4 votes vote down vote up
def read_input_file(self, fn):
        """
        This method may be overridden to implement a custom lookup mechanism when
        encountering ``\\input`` or ``\\include`` directives.

        The default implementation looks for a file of the given name relative
        to the directory set by :py:meth:`set_tex_input_directory()`.  If
        `strict_input=True` was set, we ensure strictly that the file resides in
        a subtree of the reference input directory (after canonicalizing the
        paths and resolving all symlinks).

        If `set_tex_input_directory()` was not called, or if it was called with
        a value of `None`, then no file system access is attempted an an empty
        string is returned.

        You may override this method to obtain the input data in however way you
        see fit.  In that case, a call to `set_tex_input_directory()` may not be
        needed as that function simply sets properties which are used by the
        default implementation of `read_input_file()`.

        This function accepts the referred filename as argument (the argument to
        the ``\\input`` macro), and should return a string with the file
        contents (or generate a warning or raise an error).
        """

        if self.tex_input_directory is None:
            return ''

        fnfull = os.path.realpath(os.path.join(self.tex_input_directory, fn))
        if self.strict_input:
            # make sure that the input file is strictly within dirfull, and
            # didn't escape with '../..' tricks or via symlinks.
            dirfull = os.path.realpath(self.tex_input_directory)
            if not fnfull.startswith(dirfull):
                logger.warning(
                    "Can't access path '%s' leading outside of mandated directory "
                    "[strict input mode]",
                    fn
                )
                return ''

        if not os.path.exists(fnfull) and os.path.exists(fnfull + '.tex'):
            fnfull = fnfull + '.tex'
        if not os.path.exists(fnfull) and os.path.exists(fnfull + '.latex'):
            fnfull = fnfull + '.latex'
        if not os.path.isfile(fnfull):
            logger.warning(u"Error, file doesn't exist: '%s'", fn)
            return ''

        logger.debug("Reading input file %r", fnfull)

        try:
            with open(fnfull) as f:
                return f.read()
        except IOError as e:
            logger.warning(u"Error, can't access '%s': %s", fn, e)
            return '' 
Example 17
Project: DynaPhoPy   Author: abelcarreras   File: __init__.py    License: MIT License 4 votes vote down vote up
def read_parameters_from_input_file(file_name, number_of_dimensions=3):

    input_parameters = {'structure_file_name_poscar': 'POSCAR'}

    # Check file exists
    if not os.path.isfile(file_name):
        print (file_name + ' file does not exist')
        exit()

    input_file = open(file_name, "r").readlines()
    for i, line in enumerate(input_file):
        if line[0] == '#':
            continue

        if "STRUCTURE FILE OUTCAR" in line:
            input_parameters.update({'structure_file_name_outcar': input_file[i+1].replace('\n','').strip()})

        if "STRUCTURE FILE POSCAR" in line:
            input_parameters.update({'structure_file_name_poscar': input_file[i+1].replace('\n','').strip()})

        if "FORCE SETS" in line:
            input_parameters.update({'force_sets_file_name': input_file[i+1].replace('\n','').strip()})

        if "FORCE CONSTANTS" in line:
            input_parameters.update({'force_constants_file_name': input_file[i+1].replace('\n','').strip()})
            # print('Warning!: FORCE CONSTANTS label in input has changed. Please use FORCE SETS instead')
            # exit()

        if "PRIMITIVE MATRIX" in line:
            primitive_matrix = [input_file[i+j+1].replace('\n','').split() for j in range(number_of_dimensions)]
            input_parameters.update({'_primitive_matrix': np.array(primitive_matrix, dtype=float)})

        if "SUPERCELL MATRIX" in line:
            super_cell_matrix = [input_file[i+j+1].replace('\n','').split() for j in range(number_of_dimensions)]

            super_cell_matrix = np.array(super_cell_matrix, dtype=int)
            input_parameters.update({'supercell_phonon': np.array(super_cell_matrix, dtype=int)})

        if "BANDS" in line:
            bands = []
            labels = []
            while i < len(input_file)-1:
                line = input_file[i + 1].replace('\n', '')
                try:
                    labels.append(line.split(':')[1].replace('\n','').split(','))
                    line = line.split(':')[0]
                except:
                    pass
                try:
                    band = np.array(line.replace(',',' ').split(), dtype=float).reshape((2,3))
                except IOError:
                    break
                except ValueError:
                    break
                i += 1
                bands.append(band)
            labels = [(label[0].replace(' ',''), label[1].replace(' ','')) for label in labels]

            if labels != []:
                input_parameters.update({'_band_ranges': {'ranges': bands,
                                                          'labels': labels}})
            else:
                input_parameters.update({'_band_ranges': {'ranges':bands}})

        if "MESH PHONOPY" in line:
            input_parameters.update({'_mesh_phonopy': np.array(input_file[i+1].replace('\n','').split(),dtype=int)})

    return input_parameters 
Example 18
Project: genmon   Author: jgyates   File: modbus_file.py    License: GNU General Public License v2.0 4 votes vote down vote up
def ReadInputFile(self, FileName):

        REGISTERS = 0
        STRINGS = 1
        FILE_DATA = 2

        Section  = REGISTERS
        if not len(FileName):
            self.LogError("Error in  ReadInputFile: No Input File")
            return False

        if self.ReadJSONFile(FileName):
            return True

        try:

            with open(FileName,"r") as InputFile:   #opens file

                for line in InputFile:
                    line = line.strip()             # remove beginning and ending whitespace

                    if not len(line):
                        continue
                    if line[0] == "#":              # comment?
                        continue
                    if "Strings :"in line:
                        Section = STRINGS
                    elif "FileData :" in line:
                        Section = FILE_DATA
                    if Section == REGISTERS:
                        line = line.replace('\t', ' ')
                        line = line.replace(' : ', ':')
                        Items = line.split(" ")
                        for entry in Items:
                            RegEntry = entry.split(":")
                            if len(RegEntry) == 2:
                                if len(RegEntry[0])  and len(RegEntry[1]):
                                    try:
                                        if Section == REGISTERS:
                                            HexVal = int(RegEntry[0], 16)
                                            HexVal = int(RegEntry[1], 16)
                                            #self.LogError("REGISTER: <" + RegEntry[0] + ": " + RegEntry[1] + ">")
                                            self.Registers[RegEntry[0]] = RegEntry[1]

                                    except:
                                        continue
                    elif Section == STRINGS:
                        Items = line.split(" : ")
                        if len(Items) == 2:
                            #self.LogError("STRINGS: <" + Items[0] + ": " + Items[1] + ">")
                            self.Strings[Items[0]] = Items[1]
                        else:
                            pass
                            #self.LogError("Error in STRINGS: " + str(Items))
                    elif Section == FILE_DATA:
                        Items = line.split(" : ")
                        if len(Items) == 2:
                            #self.LogError("FILEDATA: <" + Items[0] + ": " + Items[1] + ">")
                            self.FileData[Items[0]] = Items[1]
                        else:
                            pass
                            #self.LogError("Error in FILEDATA: " + str(Items))

            return True

        except Exception as e1:
            self.LogErrorLine("Error in  ReadInputFile: " + str(e1))
            return False

    # ---------- ModbusBase::GetCommStats---------------------------------------