Python read csv

60 Python code examples are found related to " read csv". 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
Source File: multi.py    From btgym with GNU Lesser General Public License v3.0 9 votes vote down vote up
def read_csv(self, data_filename=None, force_reload=False):
        # Load:
        indexes = []
        for stream in self.data.values():
            stream.read_csv(force_reload=force_reload)
            indexes.append(stream.data.index)

        # Get indexes intersection:
        if len(indexes) > 1:
            idx_intersected = indexes[0]
            for i in range(1, len(indexes)):
                idx_intersected = idx_intersected.intersection(indexes[i])

            # Truncate data to common index:
            for stream in self.data.values():
                stream.data = stream.data.loc[idx_intersected] 
Example 2
Source File: YelpRestaurantClassification2.py    From TensorflowProjects with MIT License 6 votes vote down vote up
def read_csv_files():
    def create_labels(index_string):
        label = np.zeros((1, NUM_CLASSES), dtype=np.float32)
        index_split = index_string.split(" ")
        if index_string and index_split[0]:
            indexes = map(int, index_split)
            label[:, indexes] = 1
        return label

    with open(os.path.join(FLAGS.train_data_dir, "train_photo_to_biz_ids.csv"), 'r') as photo_map_file:
        photo_map_file.next()
        photo_dict = dict((x[0], x[1]) for x in csv.reader(photo_map_file))

    with open(os.path.join(FLAGS.train_data_dir, "train.csv"), 'r') as biz_map_file:
        biz_map_file.next()
        biz_dict = dict((x[0], create_labels(x[1])) for x in csv.reader(biz_map_file))

    return photo_dict, biz_dict 
Example 3
Source File: make_preCluster_from_existing_csv.py    From cDNA_Cupcake with BSD 3-Clause Clear License 6 votes vote down vote up
def read_seq_csv(csv_filename):
    # sanity check that "seqid" and "stat" are two valid column headers
    header_checked = False
    orphans = set()
    pCS = preClusterSet2()

    for r in DictReader(open(csv_filename), delimiter=','):
        if not header_checked:
            if 'seqid' not in r or 'stat' not in r:
                print("{0} must have the fields 'seqid' and 'stat'! Abort".format(csv_filename), file=sys.stderr)
                sys.exit(-1)
        header_checked = True
        if r['stat']=='orphan':
            orphans.add(r['seqid'])
        else:
            cid = int(r['stat'])
            if cid not in pCS.S: pCS.S[cid] = preCluster(cid=cid)
            pCS.add_seqid_to_cluster_by_cid(r['seqid'], cid)
    return pCS, orphans 
Example 4
Source File: graph_util.py    From train-procgen with MIT License 6 votes vote down vote up
def read_csv(filename, key_name):
    with open(filename) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        key_index = -1

        values = []

        for line_num, row in enumerate(csv_reader):
            row = [x.lower() for x in row]
            if line_num == 0:
                idxs = [i for i, val in enumerate(row) if val == key_name]
                key_index = idxs[0]
            else:
                values.append(row[key_index])

    return np.array(values, dtype=np.float32) 
Example 5
Source File: run_stats.py    From groupme_stats with MIT License 6 votes vote down vote up
def readCsv(fname, process_msg_func=None):
	f = open(fname, 'rU')
	reader = csv.reader(f)
	count = 0
	d = {}
	for row in reader:
		if len(row) < 3:
			raise IOError("CSV file missing columns.")
		group_name = row[0]
		timestamp = row[1]
		user = row[2]
		text = row[3]
		if user not in d:
			d[user] = []
		if process_msg_func is None:
			d[user].append(1)
		else:
			data = process_msg_func(user, text)
			d[user].append(data)
	return d 
Example 6
Source File: voc.py    From wildcat.pytorch with MIT License 6 votes vote down vote up
def read_object_labels_csv(file, header=True):
    images = []
    num_categories = 0
    print('[dataset] read', file)
    with open(file, 'r') as f:
        reader = csv.reader(f)
        rownum = 0
        for row in reader:
            if header and rownum == 0:
                header = row
            else:
                if num_categories == 0:
                    num_categories = len(row) - 1
                name = row[0]
                labels = (np.asarray(row[1:num_categories + 1])).astype(np.float32)
                labels = torch.from_numpy(labels)
                item = (name, labels)
                images.append(item)
            rownum += 1
    return images 
Example 7
Source File: task_runner.py    From fileflow with Apache License 2.0 6 votes vote down vote up
def read_upstream_pandas_csv(self, data_dependency_key, dag_id=None, encoding='utf-8'):
        """
        Reads a csv file from upstream into a pandas DataFrame. Specifically
        reads a csv into memory as a pandas dataframe in a standard
        manner. Reads the data in from a file output by a previous task.

        :param str data_dependency_key: The key (business logic name) for the
            upstream dependency. This will get the value from the
            self.data_dependencies dictionary to determine the file to read
            from.
        :param str dag_id: Defaults to the current DAG id.
        :param str encoding: The file encoding to use. Defaults to 'utf-8'.
        :return: The pandas dataframe.
        :rtype: :py:obj:`pd.DataFrame`
        """
        # Read the upstream file as a stream, abstracting away storage concerns
        input_stream = self.get_upstream_stream(data_dependency_key, dag_id)

        return read_and_clean_csv_to_dataframe(
            filename_or_stream=input_stream,
            encoding=encoding
        ) 
Example 8
Source File: sqlite_bro.py    From sqlite_bro with MIT License 6 votes vote down vote up
def read_this_csv(csv_file, encoding, delimiter , quotechar, header, decim):
    """yield csv data records from a file """
    # handle Python 2/3
    try:
        reader = csv.reader(open(csv_file, 'r', encoding=encoding),
                            delimiter=delimiter, quotechar=quotechar)
    except:  # minimal hack for 2.7
        reader = csv.reader(open(csv_file, 'r'),
                            delimiter=str(delimiter), quotechar=str(quotechar))
    # handle header
    if header:
        next(reader)
    # otherwise handle special decimal treatment
    for row in reader:
        if decim != "." and not isinstance(row, (type('e'), type(u'e'))):
                for i in range(len(row)):
                    row[i] = row[i].replace(decim, ".")
        yield(row) 
Example 9
Source File: utils.py    From luminol with Apache License 2.0 6 votes vote down vote up
def read_csv(csv_name):
    """
    Read data from a csv file into a dictionary.
    :param str csv_name: path to a csv file.
    :return dict: a dictionary represents the data in file.
    """
    data = {}
    if int(sys.version[0]) == 2:
        str_types = (str, unicode)
    else:
        str_types = (bytes, str)
    if not isinstance(csv_name, str_types):
        raise exceptions.InvalidDataFormat('luminol.utils: csv_name has to be a string!')
    with open(csv_name, 'r') as csv_data:
        reader = csv.reader(csv_data, delimiter=',', quotechar='|')
        for row in reader:
            try:
                key = to_epoch(row[0])
                value = float(row[1])
                data[key] = value
            except ValueError:
                pass
    return data 
Example 10
Source File: wdl_functions.py    From toil with Apache License 2.0 6 votes vote down vote up
def read_csv(f):
    '''
    Take a csv filepath and return an array; e.g. [[],[],[]].

    For example, a file containing:

    1,2,3
    4,5,6
    7,8,9

    would return the array: [['1','2','3'], ['4','5','6'], ['7','8','9']]

    :param csv_filepath:
    :return: csv_array
    '''
    return read_tsv(f, delimiter=",") 
Example 11
Source File: script.py    From pyRevit with GNU General Public License v3.0 6 votes vote down vote up
def read_csv_typed_data(csv_file):
    """Read Revit property data from the given CSV file."""
    # open file
    with codecs.open(csv_file, 'rb', encoding='utf-8') as csvfile:
        # read lines
        csv_lines = list(csv.reader(csvfile, delimiter=',', quotechar='\"'))
        # grab the first line, extract field names
        # if field definition include the type, grab the associated
        # DB.ParameterType as well
        # https://www.apidocs.co/apps/revit/2019/f38d847e-207f-b59a-3bd6-ebea80d5be63.htm
        # https://support.spatialkey.com/providing-data-types-in-csv-headers/
        field_defs = []
        for field_def in csv_lines[0]:
            parts = field_def.split('|')
            parts_count = len(parts)
            if parts_count == 1:
                if parts[0]:
                    field_defs.append((parts[0], DB.ParameterType.Text))
            elif parts_count == 2:
                field_defs.append((parts[0],
                                   coreutils.get_enum_value(DB.ParameterType,
                                                            parts[1])))
    # return field definitions, and data
    return (field_defs, csv_lines[1:]) 
Example 12
Source File: api.py    From open-context-py with GNU General Public License v3.0 6 votes vote down vote up
def get_read_csv(self, url):
        """
        gets json daa from a geonames_uri
        """
        if self.delay_before_request > 0:
            # default to sleep BEFORE a request is sent, to
            # give the remote service a break.
            sleep(self.delay_before_request)
        try:
            gapi = GeneralAPI()
            r = requests.get(url,
                             timeout=240,
                             headers=gapi.client_headers)
            r.raise_for_status()
            csvfile = r.text.split('\n')
            self.csv_data = csv.reader(csvfile)
        except:
            self.csv_data = False
        return self.csv_data 
Example 13
Source File: run_deid_lib.py    From healthcare-deid with Apache License 2.0 6 votes vote down vote up
def read_csv(p, csv_filename):
  """Read csv file to the row format expected by deid()."""
  rows = []
  with open(csv_filename) as f:
    spamreader = unicodecsv.UnicodeReader(f)
    headers = []
    for row in spamreader:
      if not headers:
        headers = row
        continue
      rowmap = {}
      for i in range(len(headers)):
        val = ''
        if i < len(row):
          val = row[i]
        rowmap[headers[i]] = val
      rows.append([rowmap])
  return p | beam.Create(rows) 
Example 14
Source File: data.py    From CTGAN with MIT License 6 votes vote down vote up
def read_csv(csv_filename, meta_filename=None, header=True, discrete=None):

    data = pd.read_csv(csv_filename, header='infer' if header else None)

    if meta_filename:
        with open(meta_filename) as meta_file:
            metadata = json.load(meta_file)

        discrete_columns = [
            column['name']
            for column in metadata['columns']
            if column['type'] != 'continuous'
        ]

    elif discrete:
        discrete_columns = discrete.split(',')
        if not header:
            discrete_columns = [int(i) for i in discrete_columns]

    else:
        discrete_columns = []

    return data, discrete_columns 
Example 15
Source File: filter_dataset.py    From hwrt with MIT License 6 votes vote down vote up
def read_csv(filepath: str) -> Sequence[Dict[Any, Any]]:
    """
    Read a CSV into a list of dictionarys. The first line of the CSV determines
    the keys of the dictionary.

    Parameters
    ----------
    filepath : str

    Returns
    -------
    symbols : List[Dict]
    """
    symbols = []
    with open(filepath) as csvfile:
        spamreader = csv.DictReader(csvfile, delimiter=",", quotechar='"')
        for row in spamreader:
            symbols.append(row)
    return symbols 
Example 16
Source File: solution2.py    From tiny_python_projects with MIT License 6 votes vote down vote up
def read_csv(fh):
    """Read the CSV input"""

    exercises = []
    for row in csv.DictReader(fh, delimiter=','):
        name, reps = row.get('exercise'), row.get('reps')
        if name and reps:
            match = re.match(r'(\d+)-(\d+)', reps)
            if match:
                low, high = map(int, match.groups())
                exercises.append((name, low, high))

    return exercises


# -------------------------------------------------- 
Example 17
Source File: read.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_csv(
    filename: Union[PathLike, Iterator[str]],
    delimiter: Optional[str] = ",",
    first_column_names: Optional[bool] = None,
    dtype: str = "float32",
) -> AnnData:
    """\
    Read `.csv` file.

    Same as :func:`~anndata.read_text` but with default delimiter `','`.

    Parameters
    ----------
    filename
        Data file.
    delimiter
        Delimiter that separates data within text file.
        If `None`, will split at arbitrary number of white spaces,
        which is different from enforcing splitting at single white space `' '`.
    first_column_names
        Assume the first column stores row names.
    dtype
        Numpy data type.
    """
    return read_text(filename, delimiter, first_column_names, dtype) 
Example 18
Source File: edgar.py    From edgar-10k-mda with MIT License 6 votes vote down vote up
def read_url_from_combined_csv(csv_path):
    """ Reads url from csv file
    Args:
        csv_path (str): path to index file
    Returns
        urls: urls in combined csv
    """
    urls = []
    with open(csv_path, 'r') as fin:
        reader = csv.reader(fin, delimiter=",",
                            quotechar='\"', quoting=csv.QUOTE_ALL)
        # Skip header
        next(reader)
        for row in reader:
            url = row[-1]
            urls.append(url)
    return urls 
Example 19
Source File: generator_utils.py    From pycsvw with Apache License 2.0 6 votes vote down vote up
def read_csv(handle):
    """ Read CSV file
    :param handle: File-like object of the CSV file
    :return: csv.reader object
    """

    # These functions are to handle unicode in Python 2 as described in:
    # https://docs.python.org/2/library/csv.html#examples
    def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
        """ csv.py doesn't do Unicode; encode temporarily as UTF-8."""
        csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
                                dialect=dialect, **kwargs)
        for row in csv_reader:
            # decode UTF-8 back to Unicode, cell by cell:
            yield [unicode(cell, 'utf-8') for cell in row]

    def utf_8_encoder(unicode_csv_data):
        """ Encode with UTF-8."""
        for line in unicode_csv_data:
            yield line.encode('utf-8')

    return unicode_csv_reader(handle) if PY2 else csv.reader(handle) 
Example 20
Source File: file_interface.py    From evo with GNU General Public License v3.0 6 votes vote down vote up
def read_euroc_csv_trajectory(file_path):
    """
    parses ground truth trajectory from EuRoC MAV state estimate .csv
    :param file_path: <sequence>/mav0/state_groundtruth_estimate0/data.csv
    :return: trajectory.PoseTrajectory3D object
    """
    raw_mat = csv_read_matrix(file_path, delim=",", comment_str="#")
    error_msg = ("EuRoC MAV state ground truth must have 17 entries per row "
                 "and no trailing delimiter at the end of the rows (comma)")
    if len(raw_mat) > 0 and len(raw_mat[0]) != 17:
        raise FileInterfaceException(error_msg)
    try:
        mat = np.array(raw_mat).astype(float)
    except ValueError:
        raise FileInterfaceException(error_msg)
    stamps = np.divide(mat[:, 0], 1e9)  # n x 1  -  nanoseconds to seconds
    xyz = mat[:, 1:4]  # n x 3
    quat = mat[:, 4:8]  # n x 4
    logger.debug("Loaded {} stamps and poses from: {}".format(
        len(stamps), file_path))
    return PoseTrajectory3D(xyz, quat, stamps) 
Example 21
Source File: convert_luna_to_npy.py    From CASED-Tensorflow with MIT License 6 votes vote down vote up
def read_csv(filename):
    lines = []
    with open(filename, 'r') as f:
        csvreader = csv.reader(f)
        for line in csvreader:
            lines.append(line)

    lines = lines[1:] # remove csv headers
    annotations_dict = {}
    for i in lines:
        series_uid, x, y, z, diameter = i
        value = {'position':[float(x),float(y),float(z)],
                 'diameter':float(diameter)}
        if series_uid in annotations_dict.keys():
            annotations_dict[series_uid].append(value)
        else:
            annotations_dict[series_uid] = [value]

    return annotations_dict 
Example 22
Source File: utils.py    From pipeline with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_fingerprints_csv():
    with open("fingerprints.csv", newline="", encoding="utf-8") as f:
        reader = csv.reader(f)
        fingerprints = {}
        for row in reader:
            num, cc, body_match, header_name, header_prefix, header_full = row
            if cc not in fingerprints:
                fingerprints[cc] = []

            d = {}
            if body_match:
                d["body_match"] = body_match
            else:
                d["header_name"] = header_name
                if header_full:
                    d["header_full"] = header_full
                else:
                    d["header_prefix"] = header_prefix
            fingerprints[cc].append(d)
        print(fingerprints) 
Example 23
Source File: data_reading.py    From coiltraine with MIT License 6 votes vote down vote up
def read_summary_csv(control_csv_file):

    f = open(control_csv_file, "rU")
    header = f.readline()
    header = header.split(',')
    header[-1] = header[-1][:-2]
    f.close()

    data_matrix = np.loadtxt(control_csv_file, delimiter=",", skiprows=1)
    summary_dict = {}

    if len(data_matrix) == 0:
        return None

    if len(data_matrix.shape) == 1:
        data_matrix = np.expand_dims(data_matrix, axis=0)

    count = 0
    for _ in header:
        summary_dict.update({header[count]: data_matrix[:, count]})
        count += 1

    return summary_dict 
Example 24
Source File: config_bad_1.py    From dagster with Apache License 2.0 6 votes vote down vote up
def read_csv(context, csv_path):
    csv_path = os.path.join(os.path.dirname(__file__), csv_path)
    with open(csv_path, 'r') as fd:
        lines = [
            row
            for row in csv.DictReader(
                fd,
                delimiter=',',
                doublequote=False,
                escapechar='\\',
                quotechar='"',
                quoting=csv.QUOTE_MINIMAL,
                skipinitialspace=False,
                strict=False,
            )
        ]

    context.log.info('Read {n_lines} lines'.format(n_lines=len(lines)))

    return lines 
Example 25
Source File: InputGeography_food.py    From flee-release with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def ReadClosuresFromCSV(self, csv_name):
    """
    Read the closures.csv file. Format is:
    closure_type,name1,name2,closure_start,closure_end
    """
    self.closures = []

    with open(csv_name, newline='') as csvfile:
      values = csv.reader(csvfile)

      for row in values:
        if row[0][0] == "#":
          pass
        else:
          #print(row)
          self.closures.append(row) 
Example 26
Source File: InputGeography_food.py    From flee-release with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def ReadLocationsFromCSV(self,csv_name, columns=["name","region","country","gps_x","gps_y","location_type","conflict_date","pop/cap"]):
    """
    Converts a CSV file to a locations information table
    """
    self.locations = []

    c = {} #column map

    c["location_type"] = 0
    c["conflict_date"] = 0
    c["country"] = 0
    c["region"] = 0

    for i in range(0, len(columns)):
      c[columns[i]] = i

    with open(csv_name, newline='') as csvfile:
      values = csv.reader(csvfile)

      for row in values:
        if row[0][0] == "#":
          pass
        else:
          #print(row)
          self.locations.append([row[c["name"]], row[c["pop/cap"]], row[c["gps_x"]], row[c["gps_y"]], row[c["location_type"]], row[c["conflict_date"]], row[c["region"]], row[c["country"]]]) 
Example 27
Source File: data_generator.py    From mentornet with Apache License 2.0 6 votes vote down vote up
def read_from_csv(input_csv_file):
  """Reads Data from an input CSV file.

  Args:
    input_csv_file: the path of the CSV file.

  Returns:
    a numpy array with different data at each index:
  """
  data = {}
  with open(input_csv_file, 'r') as csv_file_in:
    reader = csv.reader(csv_file_in)
    for row in reader:
      for (_, cell) in enumerate(row):
        rdata = cell.strip().split(' ')
        rid = rdata[0]
        rdata = [float(t) for t in rdata[1:]]
        data[rid] = rdata
    csv_file_in.close()
  return data 
Example 28
Source File: visual_util.py    From neural_graph_evolution with MIT License 6 votes vote down vote up
def read_csv(filepath, skipfirst=True, convert2num=True):
    ''' read a csv file and return its content
    '''
    data = []
    with open(filepath, newline='') as csvfile:
        reader = csv.reader(csvfile)

        for row in reader:
            data.append( row )

    if skipfirst: start = 1
    else: start = 0
    data = data[start:]

    if convert2num:
        for i, row in enumerate(data):
            data[i] = [float(x) for x in row]

    return data 
Example 29
Source File: concoct_csv_to_fasta.py    From EdwardsLab with MIT License 6 votes vote down vote up
def read_csv(csvf, verbose=False):
    """
    Read the csv list and return a hash of contigs->bins and the number of bins
    :param csvf: csv file to read
    :param verbose: more output
    :return: a dict of contigs -> bins and an int of the largest bin number
    """
    bins={}
    x=0
    with open(csvf, 'r') as f:
        for l in f:
            p = l.strip().split(",")
            bins[p[0]]=int(p[1])
            if int(p[1]) > x:
                x = int(p[1])
    return bins, x 
Example 30
Source File: csv_reader.py    From asreview with Apache License 2.0 6 votes vote down vote up
def read_csv(data_fp):
    """CVS file reader.

    Parameters
    ----------
    fp: str, pathlib.Path
        File path to the CSV file.

    Returns
    -------
    list:
        List with entries.

    """
    try:
        df = pd.read_csv(data_fp)
    except UnicodeDecodeError:
        df = pd.read_csv(data_fp, encoding="ISO-8859-1")
    return standardize_dataframe(df) 
Example 31
Source File: utils.py    From simcoin with MIT License 6 votes vote down vote up
def read_csv(file_name):
    if os.path.isfile(file_name):
        with open(file_name, 'r') as file:
            try:
                reader = csv.reader(file)
                Object = namedtuple("Object", next(reader))
                objects = []
                for line in reader:
                    for i, var in enumerate(line):
                        try:
                            line[i] = literal_eval(var)
                        except ValueError:
                            pass
                        except SyntaxError:
                            pass
                    objects.append(Object._make(line))
                return objects
            except StopIteration:
                logging.debug('File={} has not enough lines'.format(config.args_csv))
                return []
    else:
        return [] 
Example 32
Source File: credentials.py    From opinel with GNU General Public License v2.0 6 votes vote down vote up
def read_creds_from_csv(filename):
    """
    Read credentials from a CSV file

    :param filename:
    :return:
    """
    key_id = None
    secret = None
    mfa_serial = None
    secret_next = False
    with open(filename, 'rt') as csvfile:
        for i, line in enumerate(csvfile):
            values = line.split(',')
            for v in values:
                if v.startswith('AKIA'):
                    key_id = v.strip()
                    secret_next = True
                elif secret_next:
                    secret = v.strip()
                    secret_next = False
                elif re_mfa_serial_format.match(v):
                    mfa_serial = v.strip()
    return key_id, secret, mfa_serial 
Example 33
Source File: utils.py    From geoplotlib with MIT License 6 votes vote down vote up
def read_csv(fname):
    """
    Read a csv file into a DataAccessObject

    :param fname: filename
    """
    values = defaultdict(list)
    with open(fname) as f:
        reader = csv.DictReader(f)
        for row in reader:
            for (k,v) in row.items():
                values[k].append(v)
    npvalues = {k: np.array(values[k]) for k in values.keys()}
    for k in npvalues.keys():
        for datatype in [np.int, np.float]:
            try:
                npvalues[k][:1].astype(datatype)
                npvalues[k] = npvalues[k].astype(datatype)
                break
            except:
                pass
    dao = DataAccessObject(npvalues)
    return dao 
Example 34
Source File: mainContainer.py    From blink-docker with MIT License 6 votes vote down vote up
def readCsvFile(path):
        #########
        # Format
        # 1 - Name of font/plugin
        # 2 - Name of file
        # 3 - Weight
        #########
        l = []
        with open(path, newline='') as csvFile:
            reader = csv.reader(csvFile, delimiter=',')
            for row in reader:
                l.append((row[0],row[1],int(row[2])))
        return l


############### Main 
Example 35
Source File: chem.py    From reinvent-scaffold-decorator with MIT License 6 votes vote down vote up
def read_csv_file(file_path, ignore_invalid=True, num=-1, num_fields=0):
    """
    Reads a SMILES file.
    :param file_path: Path to a CSV file.
    :param ignore_invalid: Ignores invalid lines (empty lines)
    :param num: Parse up to num rows.
    :param num_fields: Number of fields to extract (from left to right).
    :return: An iterator with the rows.
    """
    with open_file(file_path, "rt") as csv_file:
        for i, row in enumerate(csv_file):
            if i == num:
                break
            fields = row.rstrip().split("\t")
            if fields:
                if num_fields > 0:
                    fields = fields[0:num_fields]
                yield fields
            elif not ignore_invalid:
                yield None 
Example 36
Source File: epacems.py    From pudl with MIT License 6 votes vote down vote up
def read_cems_csv(filename):
    """
    Read a CEMS CSV file, compressed or not, into a :class:`pandas.DataFrame`.

    Note that some columns are not read. See
    :mod:`pudl.constants.epacems_columns_to_ignore`. Data types for the columns
    are specified in :mod:`pudl.constants.epacems_csv_dtypes` and names of the
    output columns are set by :mod:`pudl.constants.epacems_rename_dict`.

    Args:
        filename (str): The name of the file to be read

    Returns:
        pandas.DataFrame: A DataFrame containing the contents of the
        CSV file.

    """
    df = pd.read_csv(
        filename,
        index_col=False,
        usecols=lambda col: col not in pc.epacems_columns_to_ignore,
        dtype=pc.epacems_csv_dtypes,
    ).rename(columns=pc.epacems_rename_dict)
    return df 
Example 37
Source File: Evaluation_FROC.py    From NCRF with Apache License 2.0 6 votes vote down vote up
def readCSVContent(csvDIR):
    """Reads the data inside CSV file
    
    Args:
        csvDIR:    The directory including all the .csv files containing the results.
        Note that the CSV files should have the same name as the original image
        
    Returns:
        Probs:      list of the Probabilities of the detected lesions
        Xcorr:      list of X-coordinates of the lesions
        Ycorr:      list of Y-coordinates of the lesions
    """
    Xcorr, Ycorr, Probs = ([] for i in range(3))
    csv_lines = open(csvDIR,"r").readlines()
    for i in range(len(csv_lines)):
        line = csv_lines[i]
        elems = line.rstrip().split(',')
        Probs.append(float(elems[0]))
        Xcorr.append(int(elems[1]))
        Ycorr.append(int(elems[2]))
    return Probs, Xcorr, Ycorr 
Example 38
Source File: plot_threshold_vs_success.py    From pcrnet with MIT License 6 votes vote down vote up
def read_csv(folder_name):
	data = []
	# Each folder having results contain test.csv file with all the log.
	# Read all data from the csv file.
	with open(os.path.join(folder_name, 'test.csv')) as csvfile:
		csvreader = csv.reader(csvfile)
		for row in csvreader:
			row = [float(x) for x in row]
			data.append(row)

	rot_err, trans_err = [], []

	# Log stored is as per following sequence in csv files:
	# Sr. No. [0], time taken [1], number of iterations [2], translation error [3], rotation error [4].
	if folder_name[5:9]=='PNLK':
		for data_i in data:
			rot_err.append(data_i[2])
			trans_err.append(data_i[1])
	else:	
		for data_i in data:
			rot_err.append(data_i[4])
			trans_err.append(data_i[3])
	return rot_err, trans_err

# It will count the total number of test cases having rotation error below certain threshold. 
Example 39
Source File: read.py    From climatelearn with GNU General Public License v2.0 6 votes vote down vote up
def read_csv(path, sep=None, feat_drop=None, date_key=None, dropna=False, drop_axis=None):
    """
    Wraps the pandas.read_csv function adding extra features.

    :path: string
        Path to the csv file to be read.
    :sep: char
        Same argument as pandas.read_csv function.
    :feat_drop: list of strings
        Features to drop
    :date_key: string
        The name of the key representing the date in the dataset.
    :returns: Pandas DataFrame
        The pandas dataframe created from the data.
    """
    df = pd.read_csv(path, sep=sep, index_col=date_key)
    if dropna:
        for d in drop_axis:
            df = df.dropna(axis=d)
    if feat_drop is not None:
        df = df.drop(feat_drop, axis=1)
    return df 
Example 40
Source File: utils.py    From geoist with MIT License 6 votes vote down vote up
def read_csv(csv_name):
    """
    Read data from a csv file into a dictionary.
    :param str csv_name: path to a csv file.
    :return dict: a dictionary represents the data in file.
    """
    data = {}
    if int(sys.version[0]) == 2:
        str_types = (str, unicode)
    else:
        str_types = (bytes, str)
    if not isinstance(csv_name, str_types):
        raise exceptions.InvalidDataFormat('geoist.snoopy.utils: csv_name has to be a string!')
    with open(csv_name, 'r') as csv_data:
        reader = csv.reader(csv_data, delimiter=',', quotechar='|')
        for row in reader:
            try:
                key = to_epoch(row[0])
                value = float(row[1])
                data[key] = value
            except ValueError:
                pass
    return data 
Example 41
Source File: calculate_model.py    From football_predictions with Apache License 2.0 6 votes vote down vote up
def read_csv(path, base_weight):
  reader = csv.DictReader(file(path))
  games = list(reader)
  sum_diff = 0.0
  sum_count = 0
  year = 2016
  prev_month = None
  now = datetime.datetime.now()
  for game in games:
    goals1, goals2 = map(int, game['result'].split(':'))
    game['goals1'] = goals1
    game['goals2'] = goals2
    day, month, _ = game['date'].split('.')
    day = int(day)
    month = int(month)
    if prev_month and prev_month < month:
      year -= 1
    prev_month = month
    game['weight'] = base_weight / max((now - datetime.datetime(year, month, day)).days / 180.0, 1.0)
    game['date'] = '%d-%02d-%02d' % (year, month, day)
    sum_count += 1
    if 'Netherlands' in (game['team1'], game['team2']):
      print '%(team1)s - %(team2)s  %(goals1)d - %(goals2)d   (%(weight)2.2f)' % game
  return games, sum_diff, sum_count 
Example 42
Source File: lib.py    From prjxray with ISC License 6 votes vote down vote up
def read_root_csv(root_dir):
    """ Reads root.csv from raw db directory.

  This should only be used during database generation.

  """
    tiles = {}
    nodes = []

    with open(os.path.join(root_dir, 'root.csv')) as f:
        for d in csv.DictReader(f):
            if d['filetype'] == 'tile':
                if d['subtype'] not in tiles:
                    tiles[d['subtype']] = []

                tiles[d['subtype']].append(
                    os.path.join(root_dir, d['filename']))
            elif d['filetype'] == 'node':
                nodes.append(os.path.join(root_dir, d['filename']))

    return tiles, nodes 
Example 43
Source File: preprocess_h36m.py    From spl with GNU General Public License v3.0 6 votes vote down vote up
def read_csv_as_float(filename):
    """
    Borrowed from SRNN code. Reads a csv and returns a float matrix.
    https://github.com/asheshjain399/NeuralModels/blob/master/neuralmodels/utils.py#L34

    Args
      filename: string. Path to the csv file
    Returns
      returnArray: the read data in a float32 matrix
    """
    out_array = []
    lines = open(filename).readlines()
    for line in lines:
        line = line.strip().split(',')
        if len(line) > 0:
            out_array.append(np.array([np.float32(x) for x in line]))

    return np.array(out_array) 
Example 44
Source File: __init__.py    From h2o4gpu with Apache License 2.0 6 votes vote down vote up
def readTensorFromCSV(datasetFileName, allowOneColumn=False):
    dataSource = FileDataSource(datasetFileName,
                                DataSourceIface.doAllocateNumericTable,
                                DataSourceIface.doDictionaryFromContext)
    dataSource.loadDataBlock()

    nt = dataSource.getNumericTable()
    size = nt.getNumberOfRows()
    block = BlockDescriptor()
    nt.getBlockOfRows(0, size, readOnly, block)
    blockData = block.getArray().flatten()

    dims = [size]
    if nt.getNumberOfColumns() > 1 or allowOneColumn:
        dims.append(nt.getNumberOfColumns())
        size *= dims[1]

    tensorData = np.array(blockData, dtype=np.float32)

    nt.releaseBlockOfRows(block)

    tensorData.shape = dims
    tensor = HomogenTensor(tensorData, ntype=np.float32)

    return tensor 
Example 45
Source File: functions.py    From MHWorldData with MIT License 6 votes vote down vote up
def read_csv(location, fieldnames=None):
    "Reads a csv file as an object list without additional processing"
    with open(location, encoding="utf-8") as f:
        reader = csv.DictReader(f, fieldnames=fieldnames)
        items = list(reader)

        # CSV does not distinguish between empty string and null
        # Set empties to null
        for item in items:
            for key, value in item.items():
                if value == '':
                    item[key] = None

        validate_csv(items, location)
            
        return items 
Example 46
Source File: scoring.py    From waldo with Apache License 2.0 6 votes vote down vote up
def read_csv_as_dict(csv_file):
    """
    This function accepts a csv file and returns a run-length encoding (rle) dictionary, where
    the key is the image_id and the value is a matrix. Each row in this matrix is the rle of 
    an object.
    """
    rle_dict = {}
    with open(csv_file, 'r') as csv_fh:
        csv_reader = csv.reader(csv_fh)
        for row in csv_reader:  # each row represents an object
            image_id = row[0]
            if image_id == 'ImageId':  # skip header row
                continue
            encoded_pixels = row[1].split()
            encoded_pixels = list(map(int, encoded_pixels))
            if image_id not in rle_dict:
                rle_dict[image_id] = [encoded_pixels]
            else:
                rle_dict[image_id].append(encoded_pixels)

    return rle_dict 
Example 47
Source File: input_data_gen.py    From DeepMoon with MIT License 6 votes vote down vote up
def ReadHeadCraterCSV(filename="catalogues/HeadCraters.csv", sortlat=True):
    """Reads Head et al. 2010 >= 20 km diameter crater catalogue.

    Parameters
    ----------
    filename : str, optional
        Filepath and name of Head et al. csv file.  Defaults to the one in
        the current folder.
    sortlat : bool, optional
        If `True` (default), order catalogue by latitude.

    Returns
    -------
    craters : pandas.DataFrame
        Craters data frame.
    """
    craters = pd.read_csv(filename, header=0,
                          names=['Long', 'Lat', 'Diameter (km)'])
    if sortlat:
        craters.sort_values(by='Lat', inplace=True)
        craters.reset_index(inplace=True, drop=True)

    return craters 
Example 48
Source File: input_data_gen.py    From DeepMoon with MIT License 6 votes vote down vote up
def ReadLROCCraterCSV(filename="catalogues/LROCCraters.csv", sortlat=True):
    """Reads LROC 5 - 20 km crater catalogue CSV.

    Parameters
    ----------
    filename : str, optional
        Filepath and name of LROC csv file.  Defaults to the one in the current
        folder.
    sortlat : bool, optional
        If `True` (default), order catalogue by latitude.

    Returns
    -------
    craters : pandas.DataFrame
        Craters data frame.
    """
    craters = pd.read_csv(filename, header=0, usecols=list(range(2, 6)))
    if sortlat:
        craters.sort_values(by='Lat', inplace=True)
        craters.reset_index(inplace=True, drop=True)

    return craters 
Example 49
Source File: chem.py    From reinvent-randomized with MIT License 6 votes vote down vote up
def read_csv_file(file_path, ignore_invalid=True, num=-1):
    """
    Reads a SMILES file.
    :param file_path: Path to a CSV file.
    :param ignore_invalid: Ignores invalid lines (empty lines)
    :param num: Parse up to num rows.
    :return: An iterator with the rows.
    """
    with open_file(file_path, "rt") as csv_file:
        for i, row in enumerate(csv_file):
            if i == num:
                break
            fields = row.rstrip().split("\t")
            if fields:
                yield fields
            elif not ignore_invalid:
                yield None 
Example 50
Source File: __init__.py    From robotframework-CSVLibrary with Apache License 2.0 6 votes vote down vote up
def read_csv_file_to_associative(self, filename, delimiter=',', fieldnames=None, **kwargs):
        """Read CSV file and return its content as a Python list of dictionaries.
        
        - ``filename``:  name of csv file
        - ``delimiter``: Default: `,`
        - ``fieldnames``: list of column names
        - ``line_numbers``: List of linenumbers to read. Default None
        - ``quoting`` (int):
          _0_: QUOTE_MINIMAL
          _1_: QUOTE_ALL
          _2_: QUOTE_NONNUMERIC
          _3_: QUOTE_NONE
        """
        csv_dict = self._open_csv_file_for_read(
            filename,
            csv_reader=csv.DictReader,
            delimiter=str(delimiter),
            fieldnames=fieldnames,
            **kwargs
        )
        return [item for item in csv_dict] 
Example 51
Source File: simple_reader_knowledge_flexible.py    From OpenBookQA with Apache License 2.0 6 votes vote down vote up
def read_csv_file_to_json_flexible(file_name):
    """Reads a csv file to json
    See https://docs.python.org/3/library/csv.html for options and formats, etc.
    """
    import csv
    with open(file_name, newline='') as csvfile:
        reader = csv.DictReader(csvfile)

        for row in reader:
            yield row 
Example 52
Source File: __init__.py    From robotframework-CSVLibrary with Apache License 2.0 6 votes vote down vote up
def read_csv_file_to_list(self, filename, delimiter=',', **kwargs):
        """Read CSV file and return its content as a Python list of tuples.
        
        - ``filename``:  name of csv file
        - ``delimiter``: Default: `,`
        - ``line_numbers``: List of linenumbers to read. Default None
        - ``quoting`` (int):
          _0_: QUOTE_MINIMAL
          _1_: QUOTE_ALL
          _2_: QUOTE_NONNUMERIC
          _3_: QUOTE_NONE
        """
        csv_list = self._open_csv_file_for_read(
            filename,
            csv_reader=csv.reader,
            delimiter=str(delimiter),
            **kwargs
        )
        return [tuple(row) for row in csv_list] 
Example 53
Source File: reader_helper.py    From graph-partition-neural-network-samples with MIT License 6 votes vote down vote up
def read_csv_file(file_name):
  with open(file_name, "r") as ff:
    count = 0

    for line in ff:
      line_str = line.rstrip().split(",")

      if count == 0:
        num_col = len(line_str)
        results = [[] for _ in xrange(num_col)]

      for ii, xx in enumerate(line_str):
        results[ii] += [int(xx)]

      count += 1

  return results 
Example 54
Source File: LabelIdMapping.py    From BlenderProc with GNU General Public License v3.0 6 votes vote down vote up
def read_csv_mapping(path):
		""" Loads an idset mapping from a csv file, assuming the rows are sorted by their ids.
		
		:param path: Path to csv file
		"""

		with open(path, 'r') as csvfile:
				reader = csv.DictReader(csvfile)
				new_id_label_map = []
				new_label_id_map = {}

				for row in reader:
					new_id_label_map.append(row["name"])
					new_label_id_map[row["name"]] = int(row["id"])

				return new_id_label_map, new_label_id_map 
Example 55
Source File: utils.py    From pyDataverse with MIT License 6 votes vote down vote up
def read_file_csv(filename):
    """Read in CSV file.

    See more at `csv.reader() <https://docs.python.org/3.5/library/csv.html>`_.

    Parameters
    ----------
    filename : string
        Full filename with path of file.

    Returns
    -------
    reader
        Reader object, which can be iterated over.

    """
    try:
        with open(filename, newline='') as csvfile:
            return csv.reader(csvfile, delimiter=',', quotechar='"')
    except Exception as e:
        raise e
    finally:
        csvfile.close() 
Example 56
Source File: PlottingHelpers.py    From LSDMappingTools with MIT License 6 votes vote down vote up
def ReadDisorderCSV(DataDirectory, fname_prefix):
    """
    Function to read in the CSV from the chi disorder
    analysis
    Args:
        DataDirectory: the data directory
        fname_prefix: the file name prefix

    Returns:
        pandas dataframe with the csv file

    Author: FJC
    """
    # get the csv filename
    mc_points_suffix = '_disorder_movernstats_disorder_basinstats.csv'
    fname = fname_prefix+mc_points_suffix
    # read in the dataframe using pandas
    df = pd.read_csv(DataDirectory+fname)

    return df 
Example 57
Source File: PlottingHelpers.py    From LSDMappingTools with MIT License 6 votes vote down vote up
def ReadChainCSV(DataDirectory, fname_prefix, basin_key):
    """
    This function reads in the file with the suffix '_BasinX_chain.csv'
    to a pandas dataframe, where X is the basin key

    Args:
        DataDirectory: the data directory
        fname_prefix: the file name prefix
        basin_key: the basin key

    Returns:
        pandas dataframe with the csv file

    Author: FJC
    """
    # get the csv filename
    chain_suffix = '_Basin%s_chain.csv' %str(basin_key)
    fname = fname_prefix+chain_suffix
    # read in the dataframe using pandas
    df = pd.read_csv(DataDirectory+fname)

    return df 
Example 58
Source File: PlottingHelpers.py    From LSDMappingTools with MIT License 6 votes vote down vote up
def ReadBasinStatsCSV(DataDirectory, fname_prefix):
    """
    This function reads in the file with the suffix '_disorder_basinstats.csv'
    to a pandas dataframe

    Args:
        DataDirectory: the data directory
        fname_prefix: the file name prefix

    Returns:
        pandas dataframe with the csv file

    Author: FJC
    """
    
    # get the csv filename
    basin_stats_suffix = '_disorder_basinstats.csv'
    fname = fname_prefix+basin_stats_suffix
    # read in the dataframe using pandas
    df = pd.read_csv(DataDirectory+fname)

    return df