Python numpy.genfromtxt() Examples

The following are 30 code examples of numpy.genfromtxt(). 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. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: test_io.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_names_with_usecols_bug1636(self):
        # Make sure we pick up the right names w/ usecols
        data = "A,B,C,D,E\n0,1,2,3,4\n0,1,2,3,4\n0,1,2,3,4"
        ctrl_names = ("A", "C", "E")
        test = np.genfromtxt(TextIO(data),
                             dtype=(int, int, int), delimiter=",",
                             usecols=(0, 2, 4), names=True)
        assert_equal(test.dtype.names, ctrl_names)
        #
        test = np.genfromtxt(TextIO(data),
                             dtype=(int, int, int), delimiter=",",
                             usecols=("A", "C", "E"), names=True)
        assert_equal(test.dtype.names, ctrl_names)
        #
        test = np.genfromtxt(TextIO(data),
                             dtype=int, delimiter=",",
                             usecols=("A", "C", "E"), names=True)
        assert_equal(test.dtype.names, ctrl_names) 
Example #2
Source File: test_io.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_utf8_file(self):
        utf8 = b"\xcf\x96"
        with temppath() as path:
            with open(path, "wb") as f:
                f.write((b"test1,testNonethe" + utf8 + b",test3\n") * 2)
            test = np.genfromtxt(path, dtype=None, comments=None,
                                 delimiter=',', encoding="UTF-8")
            ctl = np.array([
                     ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"],
                     ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"]],
                     dtype=np.unicode)
            assert_array_equal(test, ctl)

            # test a mixed dtype
            with open(path, "wb") as f:
                f.write(b"0,testNonethe" + utf8)
            test = np.genfromtxt(path, dtype=None, comments=None,
                                 delimiter=',', encoding="UTF-8")
            assert_equal(test['f0'], 0)
            assert_equal(test['f1'], "testNonethe" + utf8.decode("UTF-8")) 
Example #3
Source File: sequence_folders.py    From SfmLearner-Pytorch with MIT License 6 votes vote down vote up
def crawl_folders(self, sequence_length):
        sequence_set = []
        demi_length = (sequence_length-1)//2
        shifts = list(range(-demi_length, demi_length + 1))
        shifts.pop(demi_length)
        for scene in self.scenes:
            intrinsics = np.genfromtxt(scene/'cam.txt').astype(np.float32).reshape((3, 3))
            imgs = sorted(scene.files('*.jpg'))
            if len(imgs) < sequence_length:
                continue
            for i in range(demi_length, len(imgs)-demi_length):
                sample = {'intrinsics': intrinsics, 'tgt': imgs[i], 'ref_imgs': []}
                for j in shifts:
                    sample['ref_imgs'].append(imgs[i+j])
                sequence_set.append(sample)
        random.shuffle(sequence_set)
        self.samples = sequence_set 
Example #4
Source File: platereader.py    From assaytools with GNU Lesser General Public License v2.1 6 votes vote down vote up
def read_emission_spectra_text(filename):
    """
    Read text-formatted emission spectra.

    Parameters
    ----------
    filename : str
       The Tecan Infinite output filen to be read.

    Returns
    -------
    SRC_280 : numpy.array
    SRC_280_x : numpy.array
    SRC_280_x_num : numpy.array

    Examples
    --------

    """

    SRC_280 = np.genfromtxt(filename, dtype='str')
    SRC_280_x = SRC_280[0,:]
    SRC_280_x_num = re.findall(r'\d+', str(SRC_280_x )[1:-1])

    return [SRC_280, SRC_280_x, SRC_280_x_num] 
Example #5
Source File: test_io.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_dtype_with_object(self):
        # Test using an explicit dtype with an object
        data = """ 1; 2001-01-01
                   2; 2002-01-31 """
        ndtype = [('idx', int), ('code', object)]
        func = lambda s: strptime(s.strip(), "%Y-%m-%d")
        converters = {1: func}
        test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype,
                             converters=converters)
        control = np.array(
            [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
            dtype=ndtype)
        assert_equal(test, control)

        ndtype = [('nest', [('idx', int), ('code', object)])]
        with assert_raises_regex(NotImplementedError,
                                 'Nested fields.* not supported.*'):
            test = np.genfromtxt(TextIO(data), delimiter=";",
                                 dtype=ndtype, converters=converters) 
Example #6
Source File: plot.py    From rl_graph_generation with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_results(file):
    if not os.path.exists(file):
        return None
    with open(file, 'r') as f:
        lines = [line for line in f]
    if len(lines) < 2:
        return None
    keys = [name.strip() for name in lines[0].split(',')]
    data = np.genfromtxt(file, delimiter=',', skip_header=1, filling_values=0.)
    if data.ndim == 1:
        data = data.reshape(1, -1)
    assert data.ndim == 2
    assert data.shape[-1] == len(keys)
    result = {}
    for idx, key in enumerate(keys):
        result[key] = data[:, idx]
    return result 
Example #7
Source File: test_io.py    From lambda-packs with MIT License 6 votes vote down vote up
def test_commented_header(self):
        # Check that names can be retrieved even if the line is commented out.
        data = TextIO("""
#gender age weight
M   21  72.100000
F   35  58.330000
M   33  21.99
        """)
        # The # is part of the first name and should be deleted automatically.
        test = np.genfromtxt(data, names=True, dtype=None)
        ctrl = np.array([('M', 21, 72.1), ('F', 35, 58.33), ('M', 33, 21.99)],
                        dtype=[('gender', '|S1'), ('age', int), ('weight', float)])
        assert_equal(test, ctrl)
        # Ditto, but we should get rid of the first element
        data = TextIO(b"""
# gender age weight
M   21  72.100000
F   35  58.330000
M   33  21.99
        """)
        test = np.genfromtxt(data, names=True, dtype=None)
        assert_equal(test, ctrl) 
Example #8
Source File: depth_evaluation_utils.py    From SfmLearner-Pytorch with MIT License 6 votes vote down vote up
def get_displacements_from_speed(root, date, scene, indices, tgt_index):
    """get displacement magnitudes by integrating over speed values.
    Might be a good alternative if the GPS is not good enough"""
    if len(indices) == 0:
        return []
    oxts_root = root/date/scene/'oxts'
    with open(oxts_root/'timestamps.txt') as f:
        timestamps = np.array([datetime.datetime.strptime(ts[:-3], "%Y-%m-%d %H:%M:%S.%f").timestamp() for ts in f.read().splitlines()])
    speeds = np.zeros((len(indices), 3))
    for i, index in enumerate(indices):
        oxts_data = np.genfromtxt(oxts_root/'data'/'{:010d}.txt'.format(index))
        speeds[i] = oxts_data[[6,7,10]]
    displacements = np.zeros((len(indices), 3))
    # Perform the integration operation, using trapezoidal method
    for i0, (i1, i2) in enumerate(zip(indices, indices[1:])):
        displacements[i0 + 1] = displacements[i0] + 0.5*(speeds[i0] + speeds[i0 + 1]) * (timestamps[i1] - timestamps[i2])
    # Set the origin of displacements at tgt_index
    displacements -= displacements[tgt_index]
    # Finally, get the displacement magnitude relative to tgt and discard the middle value (which is supposed to be 0)
    displacements_mag = np.linalg.norm(displacements, axis=1)
    return np.concatenate([displacements_mag[:tgt_index], displacements_mag[tgt_index + 1:]]) 
Example #9
Source File: test_io.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_skip_footer_with_invalid(self):
        with suppress_warnings() as sup:
            sup.filter(ConversionWarning)
            basestr = '1 1\n2 2\n3 3\n4 4\n5  \n6  \n7  \n'
            # Footer too small to get rid of all invalid values
            assert_raises(ValueError, np.genfromtxt,
                          TextIO(basestr), skip_footer=1)
    #        except ValueError:
    #            pass
            a = np.genfromtxt(
                TextIO(basestr), skip_footer=1, invalid_raise=False)
            assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
            #
            a = np.genfromtxt(TextIO(basestr), skip_footer=3)
            assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
            #
            basestr = '1 1\n2  \n3 3\n4 4\n5  \n6 6\n7 7\n'
            a = np.genfromtxt(
                TextIO(basestr), skip_footer=1, invalid_raise=False)
            assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]]))
            a = np.genfromtxt(
                TextIO(basestr), skip_footer=3, invalid_raise=False)
            assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]])) 
Example #10
Source File: test_io.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_skip_footer(self):
        data = ["# %i" % i for i in range(1, 6)]
        data.append("A, B, C")
        data.extend(["%i,%3.1f,%03s" % (i, i, i) for i in range(51)])
        data[-1] = "99,99"
        kwargs = dict(delimiter=",", names=True, skip_header=5, skip_footer=10)
        test = np.genfromtxt(TextIO("\n".join(data)), **kwargs)
        ctrl = np.array([("%f" % i, "%f" % i, "%f" % i) for i in range(41)],
                        dtype=[(_, float) for _ in "ABC"])
        assert_equal(test, ctrl) 
Example #11
Source File: test_io.py    From lambda-packs with MIT License 6 votes vote down vote up
def test_dtype_with_object(self):
        # Test using an explicit dtype with an object
        data = """ 1; 2001-01-01
                   2; 2002-01-31 """
        ndtype = [('idx', int), ('code', np.object)]
        func = lambda s: strptime(s.strip(), "%Y-%m-%d")
        converters = {1: func}
        test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype,
                             converters=converters)
        control = np.array(
            [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
            dtype=ndtype)
        assert_equal(test, control)

        ndtype = [('nest', [('idx', int), ('code', np.object)])]
        try:
            test = np.genfromtxt(TextIO(data), delimiter=";",
                                 dtype=ndtype, converters=converters)
        except NotImplementedError:
            pass
        else:
            errmsg = "Nested dtype involving objects should be supported."
            raise AssertionError(errmsg) 
Example #12
Source File: test_io.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_replace_space(self):
        # Test the 'replace_space' option
        txt = "A.A, B (B), C:C\n1, 2, 3.14"
        # Test default: replace ' ' by '_' and delete non-alphanum chars
        test = np.genfromtxt(TextIO(txt),
                             delimiter=",", names=True, dtype=None)
        ctrl_dtype = [("AA", int), ("B_B", int), ("CC", float)]
        ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
        assert_equal(test, ctrl)
        # Test: no replace, no delete
        test = np.genfromtxt(TextIO(txt),
                             delimiter=",", names=True, dtype=None,
                             replace_space='', deletechars='')
        ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", float)]
        ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
        assert_equal(test, ctrl)
        # Test: no delete (spaces are replaced by _)
        test = np.genfromtxt(TextIO(txt),
                             delimiter=",", names=True, dtype=None,
                             deletechars='')
        ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", float)]
        ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
        assert_equal(test, ctrl) 
Example #13
Source File: ply_from_array.py    From pypoisson with MIT License 6 votes vote down vote up
def points_normals_from(filename):
    array = np.genfromtxt(filename)
    return array[:,0:3], array[:,3:6] 
Example #14
Source File: io.py    From jwalk with Apache License 2.0 6 votes vote down vote up
def load_edges(fpath, delimiter=None, has_header=False):
    """Load edges in CSV format as numpy ndarray of strings.

    Args:
        fpath (str): edges file
        delimiter (str): alternative argument name for sep (default=None)
        has_header (bool): True if has header row

    Returns:
        np.ndarray: array of edges
    """
    if PANDAS_INSTALLED:
        header = 'infer' if has_header else None
        df = pd.read_csv(fpath, delimiter=delimiter, header=header)
        edges = df.values
    else:
        logger.warning("Pandas not installed. Using numpy to load csv, which "
                       "is slower.")
        header = 1 if has_header else 0
        edges = np.genfromtxt(fpath, delimiter=delimiter, skip_header=header,
                              dtype=object)
    return edges.astype('str') 
Example #15
Source File: plot.py    From HardRLWithYoutube with MIT License 6 votes vote down vote up
def load_results(file):
    if not os.path.exists(file):
        return None
    with open(file, 'r') as f:
        lines = [line for line in f]
    if len(lines) < 2:
        return None
    keys = [name.strip() for name in lines[0].split(',')]
    data = np.genfromtxt(file, delimiter=',', skip_header=1, filling_values=0.)
    if data.ndim == 1:
        data = data.reshape(1, -1)
    assert data.ndim == 2
    assert data.shape[-1] == len(keys)
    result = {}
    for idx, key in enumerate(keys):
        result[key] = data[:, idx]
    return result 
Example #16
Source File: malware.py    From trees with Apache License 2.0 6 votes vote down vote up
def read_data(labelsname, distancename):
    ## Extract labels
    rawlabels = np.genfromtxt(labelsname, delimiter=',', dtype=None)
    labelmap = {}
    row_len = 0
    for row in rawlabels:
        row_len = max(row_len, len(row)-1)
        name = row[0]
        labelmap[name] = list(row)[1:]

    ## Extract distances
    rawdistances = np.genfromtxt(distancename, delimiter=',', dtype=None)
    names = rawdistances[0][1:]
    distances = np.array(rawdistances[1:, 1:], dtype=float)
    labels = np.zeros((len(names), row_len))
    
    for i, name in enumerate(names):
        labels[i, 0:(len(row))] = labelmap[name]

    del labelmap
    return distances, labels, names 
Example #17
Source File: plot.py    From lirpg with MIT License 6 votes vote down vote up
def load_results(file):
    if not os.path.exists(file):
        return None
    with open(file, 'r') as f:
        lines = [line for line in f]
    if len(lines) < 2:
        return None
    keys = [name.strip() for name in lines[0].split(',')]
    data = np.genfromtxt(file, delimiter=',', skip_header=1, filling_values=0.)
    if data.ndim == 1:
        data = data.reshape(1, -1)
    assert data.ndim == 2
    assert data.shape[-1] == len(keys)
    result = {}
    for idx, key in enumerate(keys):
        result[key] = data[:, idx]
    return result 
Example #18
Source File: test_io.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_replace_space_known_dtype(self):
        # Test the 'replace_space' (and related) options when dtype != None
        txt = "A.A, B (B), C:C\n1, 2, 3"
        # Test default: replace ' ' by '_' and delete non-alphanum chars
        test = np.genfromtxt(TextIO(txt),
                             delimiter=",", names=True, dtype=int)
        ctrl_dtype = [("AA", int), ("B_B", int), ("CC", int)]
        ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
        assert_equal(test, ctrl)
        # Test: no replace, no delete
        test = np.genfromtxt(TextIO(txt),
                             delimiter=",", names=True, dtype=int,
                             replace_space='', deletechars='')
        ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", int)]
        ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
        assert_equal(test, ctrl)
        # Test: no delete (spaces are replaced by _)
        test = np.genfromtxt(TextIO(txt),
                             delimiter=",", names=True, dtype=int,
                             deletechars='')
        ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", int)]
        ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
        assert_equal(test, ctrl) 
Example #19
Source File: pose_evaluation_utils.py    From SfmLearner-Pytorch with MIT License 6 votes vote down vote up
def read_scene_data(data_root, sequence_set, seq_length=3, step=1):
    data_root = Path(data_root)
    im_sequences = []
    poses_sequences = []
    indices_sequences = []
    demi_length = (seq_length - 1) // 2
    shift_range = np.array([step*i for i in range(-demi_length, demi_length + 1)]).reshape(1, -1)

    sequences = set()
    for seq in sequence_set:
        corresponding_dirs = set((data_root/'sequences').dirs(seq))
        sequences = sequences | corresponding_dirs

    print('getting test metadata for theses sequences : {}'.format(sequences))
    for sequence in tqdm(sequences):
        poses = np.genfromtxt(data_root/'poses'/'{}.txt'.format(sequence.name)).astype(np.float64).reshape(-1, 3, 4)
        imgs = sorted((sequence/'image_2').files('*.png'))
        # construct 5-snippet sequences
        tgt_indices = np.arange(demi_length, len(imgs) - demi_length).reshape(-1, 1)
        snippet_indices = shift_range + tgt_indices
        im_sequences.append(imgs)
        poses_sequences.append(poses)
        indices_sequences.append(snippet_indices)
    return im_sequences, poses_sequences, indices_sequences 
Example #20
Source File: test.py    From CalculiX-Examples with MIT License 5 votes vote down vote up
def run():
    etypes = ("qu4", "qu8", "qu8r")
    ctypes = ("tie", "equ", "pc-ns", "pc-ss")
    r=open("Results.md",'w')
    r.write("Elem   | Contact |        1 |        2 |        3 |        4 |        5 |        6 |        7 |        8 |        9 |       10\n")
    r.write(":--    | :--     | --:      | --:      | --:      | --:      | --:      | --:      | --:      | --:      | --:      | --:\n")
    for ctyp in ctypes:
        for etyp in etypes:
            # cleanup and create sim dir
            simPath = etyp + "_" + ctyp
            if os.path.exists(simPath):
                shutil.rmtree(simPath)
            os.mkdir(simPath)
            
            # get the command files
            shutil.copyfile("run.fbd",os.path.join(simPath,"run.fbd"))
            shutil.copyfile(ctyp+".inp",os.path.join(simPath,ctyp+".inp"))
            
            # generate parameter file
            with open(os.path.join(simPath, "values.fbd"), "w") as f:
                f.write("valu etyp " + etyp + "\n")
                f.write("valu ctyp " + ctyp + "\n")
                f.write("valu last quit \n")
            
            # run the simulation
            os.chdir(simPath)
            os.system("cgx -bg run.fbd")
            # extract frequencies
            os.system("dat2txt.py " + ctyp)
            freq=numpy.genfromtxt("Eigenvalues_1.txt",skip_header=1)
            os.chdir("..")
            
            # write frequencies to results file
            r.write("{0:6} | {1:6} ".format(etyp,ctyp))
            for i in range(10):
                # the frequency is the third last column (freq can have 4 or 5 columns)
                fcol=len(freq[0])-2
                r.write(" | " + "{0:8.3g}".format(freq[i,fcol]))
            r.write("\n")
    r.close() 
Example #21
Source File: LSDMappingTools.py    From LSDMappingTools with MIT License 5 votes vote down vote up
def read_ascii_raster(ascii_raster_file):
    import numpy as np
    
    with open(ascii_raster_file) as f:
        header_data = [float(f.next().split()[1]) for x in xrange(6)] #read the first 6 lines
         
    raster_data = np.genfromtxt(ascii_raster_file, delimiter=' ', skip_header=6)
    raster_data = raster_data.reshape(header_data[1], header_data[0]) #rows, columns
    
    return raster_data, header_data

# this gets the extent of the asc for use with plotting
# It returns a list with 4 elements, x_min, x_max, y_min,y_max 
Example #22
Source File: raster_plotter_2d_ascii_chanfile_version.py    From LSDMappingTools with MIT License 5 votes vote down vote up
def read_ascii_raster(ascii_raster_file):
    import numpy as np
    
    with open(ascii_raster_file) as f:
        header_data = [float(f.next().split()[1]) for x in xrange(6)] #read the first 6 lines
         
    raster_data = np.genfromtxt(ascii_raster_file, delimiter=' ', skip_header=6)
    raster_data = raster_data.reshape(header_data[1], header_data[0]) #rows, columns
    
    return raster_data, header_data 
Example #23
Source File: imagenet.py    From tensornets with MIT License 5 votes vote down vote up
def get_files(data_dir, data_name, max_rows=None):
    """Reads a \`data_name.txt\` (e.g., \`val.txt\`) from
    http://www.image-net.org/challenges/LSVRC/2012/
    """
    files, labels = np.split(
        np.genfromtxt("%s/%s.txt" % (data_dir, data_name),
                      dtype=np.str, max_rows=max_rows),
        [1], axis=1)
    files = files.flatten()
    labels = np.asarray(labels.flatten(), dtype=np.int)
    return files, labels 
Example #24
Source File: sigma_database_parser.py    From gmpe-smtk with GNU Affero General Public License v3.0 5 votes vote down vote up
def parse_spectra(self):
        """
        Parses the Spectra to an instance of the database dictionary
        """
        
        damping_list = ["damping_02", "damping_05", "damping_07", 
                        "damping_10", "damping_20", "damping_30"]
        sm_record = OrderedDict([
            ("X", {"Scalar": {}, "Spectra": {"Response": {}}}), 
            ("Y", {"Scalar": {}, "Spectra": {"Response": {}}}), 
            ("V", {"Scalar": {}, "Spectra": {"Response": {}}})])
        target_names = list(sm_record)
        for iloc, ifile in enumerate(self.input_files):
            if not os.path.exists(ifile):
                continue
            data = np.genfromtxt(ifile, skip_header=1)
            per = data[:-1, 0]
            spec_acc = data[:-1, 1:]
            pgv = 100.0 * data[-1, 1]
            num_per = len(per)

            sm_record[target_names[iloc]]["Scalar"]["PGA"] =\
                {"Value": 100.0  * spec_acc[0, 0], "Units": "cm/s/s"}
            sm_record[target_names[iloc]]["Scalar"]["PGV"] =\
                {"Value": pgv, "Units": "cm/s"}
            sm_record[target_names[iloc]]["Spectra"]["Response"] = {
                "Periods": per,
                "Number Periods" : num_per,
                "Acceleration" : {"Units": "cm/s/s"},
                "Velocity" : None,
                "Displacement" : None,
                "PSA" : None,
                "PSV" : None}
            for jloc, damping in enumerate(damping_list):
                sm_record[target_names[iloc]]["Spectra"]["Response"]\
                    ["Acceleration"][damping] = 100.0 * data[:-1, jloc + 1]
        return sm_record 
Example #25
Source File: simple_flatfile_parser.py    From gmpe-smtk with GNU Affero General Public License v3.0 5 votes vote down vote up
def _parse_time_history(self, ifile):
        """
        Parses the time history from the file and returns a dictionary of
        time-series properties
        """
        output = {}
        accel = np.genfromtxt(ifile, skip_header=1)
        output["Acceleration"] = convert_accel_units(accel, self.units)
        nvals, time_step = (getline(ifile, 1).rstrip("\n")).split()
        output["Time-step"] = float(time_step)
        output["Number Steps"] = int(nvals)
        output["Units"] = "cm/s/s"
        output["PGA"] = np.max(np.fabs(output["Acceleration"]))
        return output 
Example #26
Source File: esm_database_parser.py    From gmpe-smtk with GNU Affero General Public License v3.0 5 votes vote down vote up
def _parse_time_history(self, ifile):
        """
        Parses the time history
        """
        # Build the metadata dictionary again
        metadata = _get_metadata_from_file(ifile)
        self.number_steps = _to_int(metadata["NDATA"])
        self.time_step = _to_float(metadata["SAMPLING_INTERVAL_S"])
        self.units = metadata["UNITS"]
        # Get acceleration data
        accel = np.genfromtxt(ifile, skip_header=64)
        if "DIS" in ifile:
            pga = None
            pgd = np.fabs(_to_float(metadata["PGD_" +
                          metadata["UNITS"].upper()]))
        else:
            pga = np.fabs(_to_float(
                          metadata["PGA_" + metadata["UNITS"].upper()]))
            pgd = None
            if "s^2" in self.units:
                self.units = self.units.replace("s^2", "s/s")
        
        output = {
            # Although the data will be converted to cm/s/s internally we can
            # do it here too
            "Acceleration": convert_accel_units(accel, self.units),
            "Time": get_time_vector(self.time_step, self.number_steps),
            "Time-step": self.time_step,
            "Number Steps": self.number_steps,
            "Units": self.units,
            "PGA": pga,
            "PGD": pgd
        }
        return output 
Example #27
Source File: simple_flatfile_parser_sara.py    From gmpe-smtk with GNU Affero General Public License v3.0 5 votes vote down vote up
def _parse_time_history(self, ifile, units="cm/s/s"):
        """
        Parses the time history from the file and returns a dictionary of
        time-series properties
        """
        output = {}
        accel = np.genfromtxt(ifile, skip_header=1)
        output["Acceleration"] = convert_accel_units(accel, self.units)
        nvals, time_step = (getline(ifile, 1).rstrip("\n")).split()
        output["Time-step"] = float(time_step)
        output["Number Steps"] = int(nvals)
        output["Units"] = units
        output["PGA"] = np.max(np.fabs(output["Acceleration"]))
        return output 
Example #28
Source File: luigi-targets.py    From notes with Creative Commons Attribution 4.0 International 5 votes vote down vote up
def read(self):
        return numpy.genfromtxt(str(self.path), delimiter=',') 
Example #29
Source File: common.py    From typhon with MIT License 5 votes vote down vote up
def cmap_from_txt(file, name=None, N=-1, comments='%'):
    """Import colormap from txt file.

    Reads colormap data (RGB/RGBA) from an ASCII file.
    Values have to be given in [0, 1] range.

    Parameters:
        file (str): Path to txt file.
        name (str): Colormap name. Defaults to filename without extension.
        N (int): Number of colors.
            ``-1`` means all colors (i.e., the complete file).
        comments (str): Character to start comments with.

    Returns:
        LinearSegmentedColormap.
    """
    # Extract colormap name from filename.
    if name is None:
        name = os.path.splitext(os.path.basename(file))[0]

    # Read binary file and determine number of colors
    rgb = np.genfromtxt(file, comments=comments)
    if N == -1:
        N = np.shape(rgb)[0]

    if np.min(rgb) < 0 or np.max(rgb) > 1:
        raise Exception('RGB value out of range: [0, 1].')

    # Create and register colormap...
    cmap = LinearSegmentedColormap.from_list(name, rgb, N=N)
    plt.register_cmap(cmap=cmap)

    # ... and the reversed colormap.
    cmap_r = LinearSegmentedColormap.from_list(
            name + '_r', np.flipud(rgb), N=N)
    plt.register_cmap(cmap=cmap_r)

    return cmap 
Example #30
Source File: shifted_sequence_folders.py    From SfmLearner-Pytorch with MIT License 5 votes vote down vote up
def crawl_folders(self, sequence_length):
        sequence_set = []
        img_sequences = []
        demi_length = (sequence_length-1)//2
        for scene in self.scenes:
            imgs = sorted(scene.files('*.jpg'))
            if len(imgs) < sequence_length:
                continue

            shifts_file = scene/'shifts.json'
            if shifts_file.isfile():
                with open(shifts_file, 'r') as f:
                    shifts = json.load(f)
            else:
                prior_shifts = list(range(-demi_length, 0))
                post_shifts = list(range(1, sequence_length - demi_length))
                shifts = [[prior_shifts[:], post_shifts[:]] for i in imgs]

            img_sequences.append(imgs)
            sequence_index = len(img_sequences) - 1
            intrinsics = np.genfromtxt(scene/'cam.txt').astype(np.float32).reshape((3, 3))
            for i in range(demi_length, len(imgs)-demi_length):
                sample = {'intrinsics': intrinsics,
                          'tgt': i,
                          'prior_shifts': shifts[i][0],
                          'post_shifts': shifts[i][1],
                          'sequence_index': sequence_index}
                sequence_set.append(sample)
        random.shuffle(sequence_set)
        self.samples = sequence_set
        self.img_sequences = img_sequences