Python numpy.fromiter() Examples

The following are 30 code examples of numpy.fromiter(). 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: __init__.py    From python-in-practice with GNU General Public License v3.0 8 votes vote down vote up
def create_array(width, height, background=None):
    """returns an array.array or numpy.array of the correct size and
    with the given background color"""
    if numpy is not None:
        if background is None:
            return numpy.zeros(width * height, dtype=numpy.uint32)
        else:
            iterable = (background for _ in range(width * height))
            return numpy.fromiter(iterable, numpy.uint32)
    else:
        # Use the smallest typecode that can store a 32-bit unsigned integer
        typecode = "I" if array.array("I").itemsize >= 4 else "L"
        background = (background if background is not None else
                      ColorForName["transparent"])
        return array.array(typecode, [background] * width * height)


# Taken from rgb.txt and converted to ARGB (with the addition of
# transparent). Default is solid black. 
Example #2
Source File: plyfile.py    From pointnet-registration-framework with MIT License 7 votes vote down vote up
def make2d(array, cols=None, dtype=None):
    '''
    Make a 2D array from an array of arrays.  The `cols' and `dtype'
    arguments can be omitted if the array is not empty.

    '''
    if (cols is None or dtype is None) and not len(array):
        raise RuntimeError("cols and dtype must be specified for empty "
                           "array")

    if cols is None:
        cols = len(array[0])

    if dtype is None:
        dtype = array[0].dtype

    return _np.fromiter(array, [('_', dtype, (cols,))],
                        count=len(array))['_'] 
Example #3
Source File: util.py    From Computable with MIT License 6 votes vote down vote up
def cartesian_product(X):
    '''
    Numpy version of itertools.product or pandas.compat.product.
    Sometimes faster (for large inputs)...

    Examples
    --------
    >>> cartesian_product([list('ABC'), [1, 2]])
    [array(['A', 'A', 'B', 'B', 'C', 'C'], dtype='|S1'),
 	array([1, 2, 1, 2, 1, 2])]

    '''

    lenX = np.fromiter((len(x) for x in X), dtype=int)
    cumprodX = np.cumproduct(lenX)

    a = np.roll(cumprodX, 1)
    a[0] = 1

    b = cumprodX[-1] / cumprodX

    return [np.tile(np.repeat(x, b[i]), 
                    np.product(a[i]))
               for i, x in enumerate(X)] 
Example #4
Source File: modeller.py    From InSilicoSeq with MIT License 6 votes vote down vote up
def divide_qualities_into_bins(qualities, n_bins=4):
    """Divides the raw quality scores in bins according to the mean phred
    quality of the sequence they come from

    Args:
        qualities (list): raw count of all the phred scores and mean sequence
            quality
        n_bins (int): number of bins to create (default: 4)

    Returns:
        list: a list of lists containing the binned quality scores
    """
    logger = logging.getLogger(__name__)
    logger.debug('Dividing qualities into mean clusters')
    bin_lists = [[] for _ in range(n_bins)]  # create list of `n_bins` list
    ranges = np.split(np.array(range(40)), n_bins)
    for quality in qualities:
        mean = int(quality[0][1])  # mean is at 1 and same regardless of b pos
        which_array = 0
        for array in ranges:
            if mean in array:
                read = np.fromiter((q[0] for q in quality), dtype=np.float)
                bin_lists[which_array].append(read)
            which_array += 1
    return bin_lists 
Example #5
Source File: sorting.py    From recruit with Apache License 2.0 6 votes vote down vote up
def decons_obs_group_ids(comp_ids, obs_ids, shape, labels, xnull):
    """
    reconstruct labels from observed group ids

    Parameters
    ----------
    xnull: boolean,
        if nulls are excluded; i.e. -1 labels are passed through
    """

    if not xnull:
        lift = np.fromiter(((a == -1).any() for a in labels), dtype='i8')
        shape = np.asarray(shape, dtype='i8') + lift

    if not is_int64_overflow_possible(shape):
        # obs ids are deconstructable! take the fast route!
        out = decons_group_index(obs_ids, shape)
        return out if xnull or not lift.any() \
            else [x - y for x, y in zip(out, lift)]

    i = unique_label_indices(comp_ids)
    i8copy = lambda a: a.astype('i8', subok=False, copy=True)
    return [i8copy(lab[i]) for lab in labels] 
Example #6
Source File: test_array.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_constructor_object_dtype(self):
        # GH 11856
        arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object)
        assert arr.dtype == np.object
        assert np.isnan(arr.fill_value)

        arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object,
                          fill_value='A')
        assert arr.dtype == np.object
        assert arr.fill_value == 'A'

        # GH 17574
        data = [False, 0, 100.0, 0.0]
        arr = SparseArray(data, dtype=np.object, fill_value=False)
        assert arr.dtype == np.object
        assert arr.fill_value is False
        arr_expected = np.array(data, dtype=np.object)
        it = (type(x) == type(y) and x == y for x, y in zip(arr, arr_expected))
        assert np.fromiter(it, dtype=np.bool).all() 
Example #7
Source File: test_array.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_constructor_object_dtype(self):
        # GH 11856
        arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object)
        assert arr.dtype == SparseDtype(np.object)
        assert np.isnan(arr.fill_value)

        arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object,
                          fill_value='A')
        assert arr.dtype == SparseDtype(np.object, 'A')
        assert arr.fill_value == 'A'

        # GH 17574
        data = [False, 0, 100.0, 0.0]
        arr = SparseArray(data, dtype=np.object, fill_value=False)
        assert arr.dtype == SparseDtype(np.object, False)
        assert arr.fill_value is False
        arr_expected = np.array(data, dtype=np.object)
        it = (type(x) == type(y) and x == y for x, y in zip(arr, arr_expected))
        assert np.fromiter(it, dtype=np.bool).all() 
Example #8
Source File: sorting.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def decons_obs_group_ids(comp_ids, obs_ids, shape, labels, xnull):
    """
    reconstruct labels from observed group ids

    Parameters
    ----------
    xnull: boolean,
        if nulls are excluded; i.e. -1 labels are passed through
    """

    if not xnull:
        lift = np.fromiter(((a == -1).any() for a in labels), dtype='i8')
        shape = np.asarray(shape, dtype='i8') + lift

    if not is_int64_overflow_possible(shape):
        # obs ids are deconstructable! take the fast route!
        out = decons_group_index(obs_ids, shape)
        return out if xnull or not lift.any() \
            else [x - y for x, y in zip(out, lift)]

    i = unique_label_indices(comp_ids)
    i8copy = lambda a: a.astype('i8', subok=False, copy=True)
    return [i8copy(lab[i]) for lab in labels] 
Example #9
Source File: data_util_hdf5.py    From BERT with Apache License 2.0 6 votes vote down vote up
def load_word2vec(word2vec_model_path,embed_size):
    """
    load pretrained word2vec in txt format
    :param word2vec_model_path:
    :return: word2vec_dict. word2vec_dict[word]=vector
    """
    #word2vec_object = codecs.open(word2vec_model_path,'r','utf-8') #open(word2vec_model_path,'r')
    #lines=word2vec_object.readlines()
    #word2vec_dict={}
    #for i,line in enumerate(lines):
    #    if i==0: continue
    #    string_list=line.strip().split(" ")
    #    word=string_list[0]
    #    vector=string_list[1:][0:embed_size]
    #    word2vec_dict[word]=vector
    ######################
    word2vec_dict = {}
    with open(word2vec_model_path, errors='ignore') as f:
        meta = f.readline()
        for line in f.readlines():
            items = line.split(' ')
            #if len(items[0]) > 1 and items[0] in vocab:
            word2vec_dict[items[0]] = np.fromiter(items[1:][0:embed_size], dtype=float)
    return word2vec_dict 
Example #10
Source File: plyfile.py    From Pointnet_Pointnet2_pytorch with MIT License 6 votes vote down vote up
def make2d(array, cols=None, dtype=None):
    '''
    Make a 2D array from an array of arrays.  The `cols' and `dtype'
    arguments can be omitted if the array is not empty.
    '''
    if (cols is None or dtype is None) and not len(array):
        raise RuntimeError("cols and dtype must be specified for empty "
                           "array")

    if cols is None:
        cols = len(array[0])

    if dtype is None:
        dtype = array[0].dtype

    return _np.fromiter(array, [('_', dtype, (cols,))],
                        count=len(array))['_'] 
Example #11
Source File: stl.py    From pymesh with MIT License 5 votes vote down vote up
def __load_ascii(fh, header):
        return numpy.fromiter(Stl.__ascii_reader(fh, header), dtype=Stl.stl_dtype) 
Example #12
Source File: test_numeric.py    From Computable with MIT License 5 votes vote down vote up
def test_lengths(self):
        expected = array(list(self.makegen()))
        a = fromiter(self.makegen(), int)
        a20 = fromiter(self.makegen(), int, 20)
        self.assertTrue(len(a) == len(expected))
        self.assertTrue(len(a20) == 20)
        self.assertRaises(ValueError, fromiter,
                          self.makegen(), int, len(expected) + 10) 
Example #13
Source File: test_regression.py    From Computable with MIT License 5 votes vote down vote up
def test_mem_fromiter_invalid_dtype_string(self, level=rlevel):
        x = [1, 2, 3]
        self.assertRaises(ValueError,
                              np.fromiter, [xi for xi in x], dtype='S') 
Example #14
Source File: test_numeric.py    From Computable with MIT License 5 votes vote down vote up
def test_2592(self):
        # Test iteration exceptions are correctly raised.
        count, eindex = 10, 5
        self.assertRaises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count) 
Example #15
Source File: test_numeric.py    From Computable with MIT License 5 votes vote down vote up
def test_2592_edge(self):
        # Test iter. exceptions, edge case (exception at end of iterator).
        count = 10
        eindex = count-1
        self.assertRaises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count) 
Example #16
Source File: test_numeric.py    From Computable with MIT License 5 votes vote down vote up
def test_values(self):
        expected = array(list(self.makegen()))
        a = fromiter(self.makegen(), int)
        a20 = fromiter(self.makegen(), int, 20)
        self.assertTrue(alltrue(a == expected, axis=0))
        self.assertTrue(alltrue(a20 == expected[:20], axis=0)) 
Example #17
Source File: test_numeric.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_2592_edge(self):
        # Test iter. exceptions, edge case (exception at end of iterator).
        count = 10
        eindex = count-1
        assert_raises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count) 
Example #18
Source File: test_numeric.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_2592(self):
        # Test iteration exceptions are correctly raised.
        count, eindex = 10, 5
        assert_raises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count) 
Example #19
Source File: test_numeric.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_values(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        self.assertTrue(np.alltrue(a == expected, axis=0))
        self.assertTrue(np.alltrue(a20 == expected[:20], axis=0)) 
Example #20
Source File: test_numeric.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_values(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        assert_(np.alltrue(a == expected, axis=0))
        assert_(np.alltrue(a20 == expected[:20], axis=0)) 
Example #21
Source File: Globals.py    From python-in-practice with GNU General Public License v3.0 5 votes vote down vote up
def create_array(width, height, background=None):
    """returns anumpy.array of the correct size and with the given
    background color"""
    if background is None:
        return numpy.zeros(width * height, dtype=numpy.uint32)
    iterable = (background for _ in range(width * height))
    return numpy.fromiter(iterable, numpy.uint32) 
Example #22
Source File: test_numeric.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_2592(self):
        # Test iteration exceptions are correctly raised.
        count, eindex = 10, 5
        self.assertRaises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count) 
Example #23
Source File: test_numeric.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_2592_edge(self):
        # Test iter. exceptions, edge case (exception at end of iterator).
        count = 10
        eindex = count-1
        self.assertRaises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count) 
Example #24
Source File: test_regression.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_mem_fromiter_invalid_dtype_string(self, level=rlevel):
        x = [1, 2, 3]
        self.assertRaises(ValueError,
                              np.fromiter, [xi for xi in x], dtype='S') 
Example #25
Source File: test_numeric.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_lengths(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        assert_(len(a) == len(expected))
        assert_(len(a20) == 20)
        assert_raises(ValueError, np.fromiter,
                          self.makegen(), int, len(expected) + 10) 
Example #26
Source File: test_regression.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_fromiter_bytes(self):
        # Ticket #1058
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) 
Example #27
Source File: test_regression.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_fromiter_comparison(self, level=rlevel):
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) 
Example #28
Source File: test_numeric.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_types(self):
        ai32 = np.fromiter(self.makegen(), np.int32)
        ai64 = np.fromiter(self.makegen(), np.int64)
        af = np.fromiter(self.makegen(), float)
        assert_(ai32.dtype == np.dtype(np.int32))
        assert_(ai64.dtype == np.dtype(np.int64))
        assert_(af.dtype == np.dtype(float)) 
Example #29
Source File: terrain_correction.py    From pyeo with GNU General Public License v3.0 5 votes vote down vote up
def generate_latlon(x, y,geotransform, transformer):
    x_geo, y_geo = cm.pixel_to_point_coordinates([y,x], geotransform)
    lon, lat, _ = transformer.TransformPoint(x_geo, y_geo)
    return np.fromiter((lat, lon),np.float) 
Example #30
Source File: test_regression.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_duplicate_field_names_assign(self):
        ra = np.fromiter(((i*3, i*2) for i in range(10)), dtype='i8,f8')
        ra.dtype.names = ('f1', 'f2')
        repr(ra)  # should not cause a segmentation fault
        assert_raises(ValueError, setattr, ra.dtype, 'names', ('f1', 'f1'))