def check_instance(self, data, data_kwargs={}): if isinstance(data, np.ndarray): self.data = np.ndaray(data, **data_kwargs) self.data_type = 'numpy.ndarray' elif isinstance(data, list): self.data = data self.data_type = 'list' else: # Check optional installed modules to see if data type matches try: from pandas import DataFrame if isinstance(data, DataFrame): self.data = DataFrame(data, **data_kwargs) self.data_type = 'pandas.core.frame.DataFrame' except ImportError: pass return self.data_type is None
def set_data(self, data=None, data_type=None, data_kwargs={}): """ Set the data for the given source based on the data type or a user specified type """ import toyz.utils.io self.data_type = None if data is None: if self.paths['data']['io_module']=='': raise ToyzDataError( 'You must supply a data object or file info to initialize a DataSource') else: self.data = toyz.utils.io.load_data(**self.paths['data']) if data_type is None: self.data_type = type(self.data).__module__+'.'+type(self.data).__name__ else: self.data_type = data_type else: if data_type is None: # Attempt to detect the data_type if isinstance(data, np.ndarray): self.data = np.ndaray(data, **data_kwargs) self.data_type = 'numpy.ndarray' elif isinstance(data, list): self.data = data self.data_type = 'list' else: # Check optional installed modules to see if data type matches try: from pandas import DataFrame pandas_installed=True except ImportError: pandas_installed=False if pandas_installed and isinstance(data, DataFrame): self.data = DataFrame(data, **data_kwargs) self.data_type = 'pandas.core.frame.DataFrame' else: self.data_type = data_type if data_type == 'pandas.core.frame.DataFrame': from pandas import DataFrame self.data = DataFrame(data, **data_kwargs) elif data_type == 'numpy.ndarray': self.data = numpy.ndarray(data, **data_kwargs) elif data_type == 'list': self.data = list(data) if 'columns' in data_kwargs: self.columns = data_kwargs['columns'] else: self.columns = ['col-'+n for n in range(len(self.data))] else: self.data_type = None # If the data_type was not found in the Toyz standard data types look in # user defined toyz if self.data_type is None: import toyz.utils.core user_modules = toyz.utils.core.get_all_user_modules( session_vars.toyz_settings, self.user_id) data_types = {} for module in user_modules: toy = toyz.utils.core.get_user_toyz( session_vars.toyz_settings, self.user_id, module) if hasattr(toy.config.data_types): for k,v in toy.config.data_types.items(): data_types[k] = v for dt, obj in data_types.items(): if (data_type is None and obj.check_instance(data) or data_type is not None and dt==data_type): obj.set_data(data, data_type, data_kwargs) self.data_type = dt break if self.data_type is None: raise ToyzDataError( "Could not find data type {0} in users approved modules".format(dt)) # Set the column names based on the data type self.name_columns()