def getdata(data, kind = 'np', columns = None, header = None, sep = ',', datatype = 'S', verbosity = True): """ Get data from csv-file or convert between pandas dataframe and numpy 2d-array. Args: :data: | - str with path to file containing data | - ndarray with data | - pandas.dataframe with data :kind: | str ['np','df'], optional | Determines type(:returns:), np: ndarray, df: pandas.dataframe :columns: | None or list[str] of column names for dataframe, optional :header: | None, optional | - None: no header in file | - 'infer': infer headers from file :sep: | ',' or '\t' or other char, optional | Column separator in data file :datatype': | 'S',optional | Specifies a type of data. | Is used when creating column headers (:column: is None). | -'S': light source spectrum | -'R': reflectance spectrum | or other. :verbosity: | True, False, optional | Print warning when inferring headers from file. Returns: :returns: | data as ndarray or pandas.dataframe """ if isinstance(data,str): datafile = data data = pd.read_csv(data,names=None,index_col = None,header = header,sep = sep) # Set column headers: if header == 'infer': if verbosity == True: warnings.warn('getdata(): Infering HEADERS from data file: {}!'.format(datafile)) columns = data.columns elif (columns is None): data.columns = ['{}{}'.format(datatype,x) for x in range(len(data.columns))] if columns is not None: data.columns = columns if isinstance(data,np.ndarray) & (kind == 'df'): if columns is None: columns = ['{}{}'.format(datatype,x) for x in range(data.shape[1])] data = pd.DataFrame(data, columns = columns) elif isinstance(data,pd.DataFrame) & (kind == 'np'): data = data.values return data
def read_csv_(self, file, header=None, sep=','): """ Reads spectral data from file. Args: :file: | filename :header: | None or 'infer', optional | If 'infer': headers will be inferred from file itself. | If None: no headers are expected from file. :sep: | ',', optional | Column separator. Returns: :returns: | ndarray with spectral data (first row are wavelengths) Note: Spectral data in file should be organized in columns with the first column containing the wavelengths. """ return pd.read_csv(file, names=None, index_col=None, header=header, sep=sep).values.T
def subsample_RFL_set(rfl, rflpath = '', samplefcn = 'rand', S = _CIE_ILLUMINANTS['E'], \ jab_ranges = None, jab_deltas = None, cieobs = _VF_CIEOBS, cspace = _VF_CSPACE, \ ax = np.arange(-_VF_MAXR,_VF_MAXR+_VF_DELTAR,_VF_DELTAR), \ bx = np.arange(-_VF_MAXR,_VF_MAXR+_VF_DELTAR,_VF_DELTAR), \ jx = None, limit_grid_radius = 0): """ Sub-samples a spectral reflectance set by pixelization of color space. Args: :rfl: | ndarray or str | Array with of str referring to a set of spectral reflectance functions to be subsampled. | If str to file: file must contain data as columns, with first column the wavelengths. :rflpath: | '' or str, optional | Path to folder with rfl-set specified in a str :rfl: filename. :samplefcn: | 'rand' or 'mean', optional | -'rand': selects a random sample from the samples within each pixel | -'mean': returns the mean spectral reflectance in each pixel. :S: | _CIE_ILLUMINANTS['E'], optional | Illuminant used to calculate the color coordinates of the spectral reflectance samples. :jab_ranges: | None or ndarray, optional | Specifies the pixelization of color space. (ndarray.shape = (3,3), with first axis: J,a,b, and second axis: min, max, delta) :jab_deltas: | float or ndarray, optional | Specifies the sampling range. | A float uses jab_deltas as the maximum Euclidean distance to select samples around each pixel center. A ndarray of 3 deltas, uses a city block sampling around each pixel center. :cspace: | _VF_CSPACE or dict, optional | Specifies color space. See _VF_CSPACE_EXAMPLE for example structure. :cieobs: | _VF_CIEOBS or str, optional | Specifies CMF set used to calculate color coordinates. :ax: | default ndarray or user defined ndarray, optional | default = np.arange(-_VF_MAXR,_VF_MAXR+_VF_DELTAR,_VF_DELTAR) :bx: | default ndarray or user defined ndarray, optional | default = np.arange(-_VF_MAXR,_VF_MAXR+_VF_DELTAR,_VF_DELTAR) :jx: | None, optional | Note that not-None :jab_ranges: override :ax:, :bx: and :jx input. :limit_grid_radius: | 0, optional | A value of zeros keeps grid as specified by axr,bxr. | A value > 0 only keeps (a,b) coordinates within :limit_grid_radius: Returns: :returns: | rflsampled, jabp | ndarrays with resp. the subsampled set of spectral reflectance functions and the pixel coordinate centers. """ # Testing effects of sample set, pixel size and gamut size: if type(rfl) == str: rfl = pd.read_csv(os.path.join(rflpath,rfl),header = None).get_values().T # Calculate Jab coordinates of samples: xyz,xyzw = spd_to_xyz(S, cieobs = cieobs, rfl = rfl.copy(), out = 2) cspace_pars = cspace.copy() cspace_pars.pop('type') cspace_pars['xyzw'] = xyzw jab = colortf(xyz,tf = cspace['type'],fwtf = cspace_pars) # Generate grid and get samples in each grid: gridp,idxp, jabp, pixelsamplenrs, pixelIDs = get_pixel_coordinates(jab, jab_ranges = jab_ranges, jab_deltas = jab_deltas, limit_grid_radius = limit_grid_radius) # Get rfls from set using sampling function (mean or rand): W = rfl[:1] R = rfl[1:] rflsampled = np.nan*np.ones((len(idxp),R.shape[1])) for i in range(len(idxp)): if samplefcn == 'mean': rfl_i = np.nanmean(rfl[pixelsamplenrs[i],:],axis = 0) else: samplenr_i = np.random.randint(len(pixelsamplenrs[i])) rfl_i = rfl[pixelsamplenrs[i][samplenr_i],:] rflsampled[i,:] = rfl_i rflsampled = np.vstack((W,rflsampled)) return rflsampled, jabp