Python numpy.str_() Examples
The following are 30
code examples of numpy.str_().
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_sequence.py From kipoiseq with MIT License | 6 votes |
def test_fasta_based_dataset(intervals_file, fasta_file): # just test the functionality dl = StringSeqIntervalDl(intervals_file, fasta_file) ret_val = dl[0] assert isinstance(ret_val["inputs"], np.ndarray) assert ret_val["inputs"].shape == () # # test with set wrong seqlen: # dl = StringSeqIntervalDl(intervals_file, fasta_file, required_seq_len=3) # with pytest.raises(Exception): # dl[0] dl = StringSeqIntervalDl(intervals_file, fasta_file, label_dtype="str") ret_val = dl[0] assert isinstance(ret_val['targets'][0], np.str_) dl = StringSeqIntervalDl(intervals_file, fasta_file, label_dtype="int") ret_val = dl[0] assert isinstance(ret_val['targets'][0], np.int_) dl = StringSeqIntervalDl(intervals_file, fasta_file, label_dtype="bool") ret_val = dl[0] assert isinstance(ret_val['targets'][0], np.bool_) vals = dl.load_all() assert vals['inputs'][0] == 'GT'
Example #2
Source File: test_uvflag.py From pyuvdata with BSD 2-Clause "Simplified" License | 6 votes |
def test_collapse_pol_flag(): uvf = UVFlag(test_f_file) uvf.to_flag() assert uvf.weights_array is None uvf2 = uvf.copy() uvf2.polarization_array[0] = -4 uvf.__add__(uvf2, inplace=True, axis="pol") # Concatenate to form multi-pol object uvf2 = uvf.copy() uvf2.collapse_pol() assert len(uvf2.polarization_array) == 1 assert uvf2.polarization_array[0] == np.str_( ",".join(map(str, uvf.polarization_array)) ) assert uvf2.mode == "metric" assert hasattr(uvf2, "metric_array") assert hasattr(uvf2, "flag_array") assert uvf2.flag_array is None
Example #3
Source File: test_uvflag.py From pyuvdata with BSD 2-Clause "Simplified" License | 6 votes |
def test_to_waterfall_bl_multi_pol(): uvf = UVFlag(test_f_file) uvf.weights_array = np.ones_like(uvf.weights_array) uvf2 = uvf.copy() uvf2.polarization_array[0] = -4 uvf.__add__(uvf2, inplace=True, axis="pol") # Concatenate to form multi-pol object uvf2 = uvf.copy() # Keep a copy to run with keep_pol=False uvf.to_waterfall() assert uvf.type == "waterfall" assert uvf.metric_array.shape == ( len(uvf.time_array), len(uvf.freq_array), len(uvf.polarization_array), ) assert uvf.weights_array.shape == uvf.metric_array.shape assert len(uvf.polarization_array) == 2 # Repeat with keep_pol=False uvf2.to_waterfall(keep_pol=False) assert uvf2.type == "waterfall" assert uvf2.metric_array.shape == (len(uvf2.time_array), len(uvf.freq_array), 1) assert uvf2.weights_array.shape == uvf2.metric_array.shape assert len(uvf2.polarization_array) == 1 assert uvf2.polarization_array[0] == np.str_( ",".join(map(str, uvf.polarization_array)) )
Example #4
Source File: pyanitools.py From deepchem with MIT License | 6 votes |
def store_data(self, store_loc, **kwargs): """Put arrays to store """ #print(store_loc) g = self.store.create_group(store_loc) for k, v, in kwargs.items(): #print(type(v[0])) #print(k) if type(v) == list: if len(v) != 0: if type(v[0]) is np.str_ or type(v[0]) is str: v = [a.encode('utf8') for a in v] g.create_dataset( k, data=v, compression=self.clib, compression_opts=self.clev)
Example #5
Source File: test_uvflag.py From pyuvdata with BSD 2-Clause "Simplified" License | 6 votes |
def test_collapse_pol_or(): uvf = UVFlag(test_f_file) uvf.to_flag() assert uvf.weights_array is None uvf2 = uvf.copy() uvf2.polarization_array[0] = -4 uvf.__add__(uvf2, inplace=True, axis="pol") # Concatenate to form multi-pol object uvf2 = uvf.copy() uvf2.collapse_pol(method="or") assert len(uvf2.polarization_array) == 1 assert uvf2.polarization_array[0] == np.str_( ",".join(map(str, uvf.polarization_array)) ) assert uvf2.mode == "flag" assert hasattr(uvf2, "flag_array") assert hasattr(uvf2, "metric_array") assert uvf2.metric_array is None
Example #6
Source File: test_utils.py From argos with GNU General Public License v3.0 | 5 votes |
def setUp(self): pass self.b_lit = b'bytes literal' self.s_lit = 'literal literal' self.u_lit = u'unicode literal' self.np_b_lit = np.bytes_('numpy bytes literal') self.np_s_lit = np.str_('numpy unicode literal') self.np_u_lit = np.unicode_('numpy unicode literal')
Example #7
Source File: components_test.py From batchflow with Apache License 2.0 | 5 votes |
def fest_getitem_getattr(self, source, indices_type): index, full, a12_68, a25_48, a32_42, a35_40, a37_39, a38 = source(indices_type) assert (full[index[38]].labels == 138).all() assert (a12_68[index[38]].labels == 138).all() assert (a25_48[index[38]].labels == 138).all() assert (a32_42[index[38]].labels == 138).all() assert (a35_40[index[38]].labels == 138).all() assert (a37_39[index[38]].labels == 138).all() if index.dtype.type is np.str_: assert (a38[index[38]].labels == 138).all() else: with pytest.raises(TypeError): assert (a38[index[38]].labels == 138).all()
Example #8
Source File: test_arrayprint.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_0d_arrays(self): unicode = type(u'') assert_equal(unicode(np.array(u'café', '<U4')), u'café') if sys.version_info[0] >= 3: assert_equal(repr(np.array('café', '<U4')), "array('café', dtype='<U4')") else: assert_equal(repr(np.array(u'café', '<U4')), "array(u'caf\\xe9', dtype='<U4')") assert_equal(str(np.array('test', np.str_)), 'test') a = np.zeros(1, dtype=[('a', '<i4', (3,))]) assert_equal(str(a[0]), '([0, 0, 0],)') assert_equal(repr(np.datetime64('2005-02-25')[...]), "array('2005-02-25', dtype='datetime64[D]')") assert_equal(repr(np.timedelta64('10', 'Y')[...]), "array(10, dtype='timedelta64[Y]')") # repr of 0d arrays is affected by printoptions x = np.array(1) np.set_printoptions(formatter={'all':lambda x: "test"}) assert_equal(repr(x), "array(test)") # str is unaffected assert_equal(str(x), "1") # check `style` arg raises assert_warns(DeprecationWarning, np.array2string, np.array(1.), style=repr) # but not in legacy mode np.array2string(np.array(1.), style=repr, legacy='1.13') # gh-10934 style was broken in legacy mode, check it works np.array2string(np.array(1.), legacy='1.13')
Example #9
Source File: __init__.py From LearningApacheSpark with MIT License | 5 votes |
def _can_convert_to_string(value): vtype = type(value) return isinstance(value, basestring) or vtype in [np.unicode_, np.string_, np.str_]
Example #10
Source File: tests.py From LearningApacheSpark with MIT License | 5 votes |
def test_string(self): lr = LogisticRegression() for col in ['features', u'features', np.str_('features')]: lr.setFeaturesCol(col) self.assertEqual(lr.getFeaturesCol(), 'features') self.assertRaises(TypeError, lambda: LogisticRegression(featuresCol=2.3))
Example #11
Source File: Preprocessing.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def get_data(lst,preproc): data = [] result = [] for path in lst: f = dicom.read_file(path) img = preproc(f.pixel_array.astype(float) / np.max(f.pixel_array)) dst_path = path.rsplit(".", 1)[0] + ".64x64.jpg" scipy.misc.imsave(dst_path, img) result.append(dst_path) data.append(img) data = np.array(data, dtype=np.uint8) data = data.reshape(data.size) data = np.array(data, dtype=np.str_) data = data.reshape(data.size) return [data,result]
Example #12
Source File: testing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def rands_array(nchars, size, dtype='O'): """Generate an array of byte strings.""" retval = (np.random.choice(RANDS_CHARS, size=nchars * np.prod(size)) .view((np.str_, nchars)).reshape(size)) if dtype is None: return retval else: return retval.astype(dtype)
Example #13
Source File: test_inference.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_is_scalar_numpy_array_scalars(self): assert is_scalar(np.int64(1)) assert is_scalar(np.float64(1.)) assert is_scalar(np.int32(1)) assert is_scalar(np.object_('foobar')) assert is_scalar(np.str_('foobar')) assert is_scalar(np.unicode_(u('foobar'))) assert is_scalar(np.bytes_(b'foobar')) assert is_scalar(np.datetime64('2014-01-01')) assert is_scalar(np.timedelta64(1, 'h'))
Example #14
Source File: test_constructors.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_constructor_empty_with_string_dtype(self): # GH 9428 expected = DataFrame(index=[0, 1], columns=[0, 1], dtype=object) df = DataFrame(index=[0, 1], columns=[0, 1], dtype=str) tm.assert_frame_equal(df, expected) df = DataFrame(index=[0, 1], columns=[0, 1], dtype=np.str_) tm.assert_frame_equal(df, expected) df = DataFrame(index=[0, 1], columns=[0, 1], dtype=np.unicode_) tm.assert_frame_equal(df, expected) df = DataFrame(index=[0, 1], columns=[0, 1], dtype='U5') tm.assert_frame_equal(df, expected)
Example #15
Source File: test_scalarmath.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_scalar_comparison_to_none(self): # Scalars should just return False and not give a warnings. # The comparisons are flagged by pep8, ignore that. with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) assert_(not np.float32(1) == None) assert_(not np.str_('test') == None) # This is dubious (see below): assert_(not np.datetime64('NaT') == None) assert_(np.float32(1) != None) assert_(np.str_('test') != None) # This is dubious (see below): assert_(np.datetime64('NaT') != None) assert_(len(w) == 0) # For documentation purposes, this is why the datetime is dubious. # At the time of deprecation this was no behaviour change, but # it has to be considered when the deprecations are done. assert_(np.equal(np.datetime64('NaT'), None)) #class TestRepr(object): # def test_repr(self): # for t in types: # val = t(1197346475.0137341) # val_repr = repr(val) # val2 = eval(val_repr) # assert_equal( val, val2 )
Example #16
Source File: test_regression.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_object_array_to_fixed_string(self): # Ticket #1235. a = np.array(['abcdefgh', 'ijklmnop'], dtype=np.object_) b = np.array(a, dtype=(np.str_, 8)) assert_equal(a, b) c = np.array(a, dtype=(np.str_, 5)) assert_equal(c, np.array(['abcde', 'ijklm'])) d = np.array(a, dtype=(np.str_, 12)) assert_equal(a, d) e = np.empty((2, ), dtype=(np.str_, 8)) e[:] = a[:] assert_equal(a, e)
Example #17
Source File: test_arrayprint.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_0d_arrays(self): unicode = type(u'') assert_equal(unicode(np.array(u'café', '<U4')), u'café') if sys.version_info[0] >= 3: assert_equal(repr(np.array('café', '<U4')), "array('café', dtype='<U4')") else: assert_equal(repr(np.array(u'café', '<U4')), "array(u'caf\\xe9', dtype='<U4')") assert_equal(str(np.array('test', np.str_)), 'test') a = np.zeros(1, dtype=[('a', '<i4', (3,))]) assert_equal(str(a[0]), '([0, 0, 0],)') assert_equal(repr(np.datetime64('2005-02-25')[...]), "array('2005-02-25', dtype='datetime64[D]')") assert_equal(repr(np.timedelta64('10', 'Y')[...]), "array(10, dtype='timedelta64[Y]')") # repr of 0d arrays is affected by printoptions x = np.array(1) np.set_printoptions(formatter={'all':lambda x: "test"}) assert_equal(repr(x), "array(test)") # str is unaffected assert_equal(str(x), "1") # check `style` arg raises assert_warns(DeprecationWarning, np.array2string, np.array(1.), style=repr) # but not in legacy mode np.array2string(np.array(1.), style=repr, legacy='1.13') # gh-10934 style was broken in legacy mode, check it works np.array2string(np.array(1.), legacy='1.13')
Example #18
Source File: test_gp.py From flare with MIT License | 5 votes |
def dumpcompare(obj1, obj2): '''this source code comes from http://stackoverflow.com/questions/15785719/how-to-print-a-dictionary-line-by-line-in-python''' assert isinstance(obj1, type( obj2)), "the two objects are of different types" if isinstance(obj1, dict): assert len(obj1.keys()) == len( obj2.keys()), f"key1 {list(obj1.keys())}, \n key2 {list(obj2.keys())}" for k1, k2 in zip(sorted(obj1.keys()), sorted(obj2.keys())): assert k1 == k2, f"key {k1} is not the same as {k2}" if (k1 != "name"): assert dumpcompare(obj1[k1], obj2[k2] ), f"value {k1} is not the same as {k2}" elif isinstance(obj1, (list, tuple)): assert len(obj1) == len(obj2) for k1, k2 in zip(obj1, obj2): assert dumpcompare(k1, k2), f"list elements are different" elif isinstance(obj1, np.ndarray): assert obj1.shape == obj2.shape if (not isinstance(obj1[0], np.str_)): assert np.equal(obj1, obj2).all(), "ndarray is not all the same" else: for xx, yy in zip(obj1, obj2): assert dumpcompare(xx, yy) else: assert obj1 == obj2 return True
Example #19
Source File: unischema.py From petastorm with Apache License 2.0 | 5 votes |
def _numpy_to_spark_mapping(): """Returns a mapping from numpy to pyspark.sql type. Caches the mapping dictionary inorder to avoid instantiation of multiple objects in each call.""" # Refer to the attribute of the function we use to cache the map using a name in the variable instead of a 'dot' # notation to avoid copy/paste/typo mistakes cache_attr_name = 'cached_numpy_to_pyspark_types_map' if not hasattr(_numpy_to_spark_mapping, cache_attr_name): import pyspark.sql.types as T setattr(_numpy_to_spark_mapping, cache_attr_name, { np.int8: T.ByteType(), np.uint8: T.ShortType(), np.int16: T.ShortType(), np.uint16: T.IntegerType(), np.int32: T.IntegerType(), np.int64: T.LongType(), np.float32: T.FloatType(), np.float64: T.DoubleType(), np.string_: T.StringType(), np.str_: T.StringType(), np.unicode_: T.StringType(), np.bool_: T.BooleanType(), }) return getattr(_numpy_to_spark_mapping, cache_attr_name) # TODO: Changing fields in this class or the UnischemaField will break reading due to the schema being pickled next to # the dataset on disk
Example #20
Source File: astropy_py3compat.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def encode_ascii(s): if isinstance(s, str): return s.encode('ascii') elif isinstance(s, numpy.ndarray) and \ issubclass(s.dtype.type, numpy.str_): ns = numpy.char.encode(s, 'ascii').view(type(s)) if ns.dtype.itemsize != s.dtype.itemsize / 4: ns = ns.astype((numpy.bytes_, s.dtype.itemsize / 4)) return ns return s
Example #21
Source File: vasprun.py From pyiron with BSD 3-Clause "New" or "Revised" License | 5 votes |
def clean_key(a, remove_char=" "): """ Replaces blanck spaces from a string for a dictionary key with "_" Args: a (str): String to be cleaned remove_char (str): Character to be replaced Returns: str: The clean string """ if isinstance(a, (str, np.str, np.str_)): return a.replace(remove_char, "_") else: return a
Example #22
Source File: vasprun.py From pyiron with BSD 3-Clause "New" or "Revised" License | 5 votes |
def clean_character(a, remove_char=" "): """ Args: a (str): String to be cleaned remove_char (str): Character to be replaced Returns: str: The clean string """ if isinstance(a, (str, np.str, np.str_)): return a.replace(remove_char, "") else: return a
Example #23
Source File: sparse_list.py From pyiron with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __getitem__(self, item): new_dict = {} if isinstance(item, int): for key, value in self._lists.items(): if value[item] is not None: new_dict[key] = value[item] return SparseArrayElement(**new_dict) elif isinstance(item, (str, np.str, np.str_)): return self._lists[item] elif isinstance(item, (list, np.ndarray)): # print("key(__getitem__) len, type, item[0]: ", len(item), type(item), item[0]) if len(item) == len(self): if isinstance(item[0], (np.bool_, bool)): item = np.arange(len(item))[item] for key, value in self._lists.items(): # print ('key: ', key, type(value)) if isinstance(item, slice): new_dict[key] = value[item] else: if isinstance(value, (list, tuple)): new_dict[key] = [value[i] for i in item] else: if len(value) > 0: try: new_dict[key] = value[item] except IndexError: print("Index error:: ", key, item, value) # else: # new_dict[key] = [] # print ("new_dict: ", new_dict, self.__class__) return self.__class__(**new_dict)
Example #24
Source File: test_uvflag.py From pyuvdata with BSD 2-Clause "Simplified" License | 5 votes |
def test_collapse_pol(test_outfile): uvf = UVFlag(test_f_file) uvf.weights_array = np.ones_like(uvf.weights_array) uvf2 = uvf.copy() uvf2.polarization_array[0] = -4 uvf.__add__(uvf2, inplace=True, axis="pol") # Concatenate to form multi-pol object uvf2 = uvf.copy() uvf2.collapse_pol() assert len(uvf2.polarization_array) == 1 assert uvf2.polarization_array[0] == np.str_( ",".join(map(str, uvf.polarization_array)) ) assert uvf2.mode == "metric" assert hasattr(uvf2, "metric_array") assert hasattr(uvf2, "flag_array") assert uvf2.flag_array is None # test check passes just to be sure assert uvf2.check() # test writing it out and reading in to make sure polarization_array has # correct type uvf2.write(test_outfile, clobber=True) with h5py.File(test_outfile, "r") as h5: assert h5["Header/polarization_array"].dtype.type is np.string_ uvf = UVFlag(test_outfile) assert uvf._polarization_array.expected_type == str assert uvf._polarization_array.acceptable_vals is None assert uvf == uvf2 os.remove(test_outfile)
Example #25
Source File: aipy_extracts.py From pyuvdata with BSD 2-Clause "Simplified" License | 5 votes |
def __init__(self, filename, status="old", corrmode="r"): """ Initialize from a miriad file. Parameters ---------- filename : str filename to initialize from status : str options are: 'old', 'new', 'append' corrmode : str options are 'r' (float32 data storage) or 'j' (int16 with shared exponent) """ assert status in ["old", "new", "append"] assert corrmode in ["r", "j"] # when reading mutliple files we may get a numpy array of file names # numpy casts arrays as np.str_ and cython does not like this _miriad.UV.__init__(self, str(filename), status, corrmode) self.status = status self.nchan = _miriad.MAXCHAN if status == "old": self.vartable = self._gen_vartable() self.read() self.rewind() # Update variables for the user try: self.nchan = self["nchan"] except KeyError: pass else: self.vartable = {"corr": corrmode}
Example #26
Source File: _encoders.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def _check_X(self, X): """ Perform custom check_array: - convert list of strings to object dtype - check for missing values for object dtype data (check_array does not do that) - return list of features (arrays): this list of features is constructed feature by feature to preserve the data types of pandas DataFrame columns, as otherwise information is lost and cannot be used, eg for the `categories_` attribute. """ if not (hasattr(X, 'iloc') and getattr(X, 'ndim', 0) == 2): # if not a dataframe, do normal check_array validation X_temp = check_array(X, dtype=None) if (not hasattr(X, 'dtype') and np.issubdtype(X_temp.dtype, np.str_)): X = check_array(X, dtype=np.object) else: X = X_temp needs_validation = False else: # pandas dataframe, do validation later column by column, in order # to keep the dtype information to be used in the encoder. needs_validation = True n_samples, n_features = X.shape X_columns = [] for i in range(n_features): Xi = self._get_feature(X, feature_idx=i) Xi = check_array(Xi, ensure_2d=False, dtype=None, force_all_finite=needs_validation) X_columns.append(Xi) return X_columns, n_samples, n_features
Example #27
Source File: test_scalarmath.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_scalar_comparison_to_none(self): # Scalars should just return False and not give a warnings. # The comparisons are flagged by pep8, ignore that. with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) assert_(not np.float32(1) == None) assert_(not np.str_('test') == None) # This is dubious (see below): assert_(not np.datetime64('NaT') == None) assert_(np.float32(1) != None) assert_(np.str_('test') != None) # This is dubious (see below): assert_(np.datetime64('NaT') != None) assert_(len(w) == 0) # For documentation purposes, this is why the datetime is dubious. # At the time of deprecation this was no behaviour change, but # it has to be considered when the deprecations are done. assert_(np.equal(np.datetime64('NaT'), None)) #class TestRepr(object): # def test_repr(self): # for t in types: # val = t(1197346475.0137341) # val_repr = repr(val) # val2 = eval(val_repr) # assert_equal( val, val2 )
Example #28
Source File: Preprocessing.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def get_data(lst,preproc): data = [] result = [] for path in lst: f = dicom.read_file(path) img = preproc(f.pixel_array.astype(float) / np.max(f.pixel_array)) dst_path = path.rsplit(".", 1)[0] + ".64x64.jpg" scipy.misc.imsave(dst_path, img) result.append(dst_path) data.append(img) data = np.array(data, dtype=np.uint8) data = data.reshape(data.size) data = np.array(data, dtype=np.str_) data = data.reshape(data.size) return [data,result]
Example #29
Source File: test_arrayprint.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_0d_arrays(self): unicode = type(u'') assert_equal(unicode(np.array(u'café', '<U4')), u'café') if sys.version_info[0] >= 3: assert_equal(repr(np.array('café', '<U4')), "array('café', dtype='<U4')") else: assert_equal(repr(np.array(u'café', '<U4')), "array(u'caf\\xe9', dtype='<U4')") assert_equal(str(np.array('test', np.str_)), 'test') a = np.zeros(1, dtype=[('a', '<i4', (3,))]) assert_equal(str(a[0]), '([0, 0, 0],)') assert_equal(repr(np.datetime64('2005-02-25')[...]), "array('2005-02-25', dtype='datetime64[D]')") assert_equal(repr(np.timedelta64('10', 'Y')[...]), "array(10, dtype='timedelta64[Y]')") # repr of 0d arrays is affected by printoptions x = np.array(1) np.set_printoptions(formatter={'all':lambda x: "test"}) assert_equal(repr(x), "array(test)") # str is unaffected assert_equal(str(x), "1") # check `style` arg raises assert_warns(DeprecationWarning, np.array2string, np.array(1.), style=repr) # but not in legacy mode np.array2string(np.array(1.), style=repr, legacy='1.13') # gh-10934 style was broken in legacy mode, check it works np.array2string(np.array(1.), legacy='1.13')
Example #30
Source File: test_draw.py From synthpop with BSD 3-Clause "New" or "Revised" License | 5 votes |
def index(): return np.array(['v', 'w', 'x', 'y', 'z'], dtype=np.str_)