New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing DataArrays to files keep throwing TypeError #184
Comments
We would need to see a full example including link to data and the code to: |
`import geowombat as gw cl = Pipeline([ ('scaler', StandardScaler()), fig, ax = plt.subplots(figsize=(5,5)) Fit_predict unsupervised classifierwith gw.open(['Sen_clip2.tif', 'Sen_clip3.tif', 'Sen_clip4.tif'], stack_dim='band') as src: plt.tight_layout(pad=1) This is the code. Any .tif file I use doesn't work. |
Ok yeah its a bit quirky at the moment, but you need to select the time period (its set up for multiperiod prediction at the moment). The following example should help. I will update the docs. #%%
import geowombat as gw
from geowombat.data import l8_224078_20200518, l8_224078_20200518_polygons
from geowombat.ml import fit, predict, fit_predict
import geopandas as gpd
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.decomposition import PCA
from sklearn.naive_bayes import GaussianNB
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
cl = Pipeline([ ('scaler', StandardScaler()),
('pca', PCA()),
('clf', KMeans(n_clusters=12, random_state=0))])
# %%
from sklearn.cluster import KMeans
cl = Pipeline([ ('clf', KMeans(n_clusters=6, random_state=0))])
fig, ax = plt.subplots(dpi=200,figsize=(5,5))
# Fit_predict unsupervised classifier
with gw.config.update(ref_res=150):
with gw.open(l8_224078_20200518) as src:
y= fit_predict(src, cl)
y.plot(robust=True, ax=ax)
plt.tight_layout(pad=1)
y.sel(time='t1').gw.to_raster('Unsup.tif') |
It is still not working |
So the example I just sent you isn't working? or with your data? |
Both keep giving that error when writing out. |
I think I was able to resolve it by doing this instead. y.sel(time='t1').rio.to_raster('Unsup1.tif') Thanks for your time! |
Ok good to hear. We are testing .save() soon. Hopefully it will resolve this. |
Yep. I hope so too |
Hi, =====
====== TypeError Traceback (most recent call last) File ~\Miniconda3\envs\gwenv\lib\site-packages\geowombat\core\geoxarray.py:786, in GeoWombatAccessor.to_raster(self, filename, readxsize, readysize, separate, use_dask_store, out_block_type, keep_blocks, verbose, overwrite, gdal_cache, scheduler, n_jobs, n_workers, n_threads, n_chunks, overviews, resampling, use_client, address, total_memory, driver, nodata, blockxsize, blockysize, tags, **kwargs) File ~\Miniconda3\envs\gwenv\lib\site-packages\geowombat\core\io.py:902, in to_raster(data, filename, readxsize, readysize, use_dask_store, separate, out_block_type, keep_blocks, verbose, overwrite, gdal_cache, scheduler, n_jobs, n_workers, n_threads, n_chunks, use_client, address, total_memory, processes, padding, tags, tqdm_kwargs, **kwargs) TypeError: type object argument after ** must be a mapping, not NoneType I try to add the sel call for bands, but this does not work. Although maybe I am setting it up incorrectly. Thanks for any advice. |
Hello, you can use |
Hi, thanks for responding. I forgot to mention, I tried this, but there is no t1 data or time series, as they are just different bands of the same multispectral image. So I get a key error about t1. I tried to sel the bands I wanted to, but that also did not work. When I try
I get a key error: KeyError Traceback (most recent call last) File ~\Miniconda3\envs\gwenv\lib\site-packages\pandas_libs\index.pyx:136, in pandas._libs.index.IndexEngine.get_loc() File ~\Miniconda3\envs\gwenv\lib\site-packages\pandas_libs\index.pyx:144, in pandas._libs.index.IndexEngine.get_loc() File pandas_libs\index_class_helper.pxi:41, in pandas._libs.index.Int64Engine._check_type() KeyError: 't1' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) File ~\Miniconda3\envs\gwenv\lib\site-packages\xarray\core\dataarray.py:1329, in DataArray.sel(self, indexers, method, tolerance, drop, **indexers_kwargs) File ~\Miniconda3\envs\gwenv\lib\site-packages\xarray\core\dataset.py:2501, in Dataset.sel(self, indexers, method, tolerance, drop, **indexers_kwargs) File ~\Miniconda3\envs\gwenv\lib\site-packages\xarray\core\coordinates.py:421, in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs) File ~\Miniconda3\envs\gwenv\lib\site-packages\xarray\core\indexing.py:121, in remap_label_indexers(data_obj, indexers, method, tolerance) File ~\Miniconda3\envs\gwenv\lib\site-packages\xarray\core\indexes.py:241, in PandasIndex.query(self, labels, method, tolerance) File ~\Miniconda3\envs\gwenv\lib\site-packages\pandas\core\indexes\base.py:3623, in Index.get_loc(self, key, method, tolerance) KeyError: 't1 |
Try this |
Output: 22:04:37:INFO:813:io.to_raster: Writing data to file ... 22:04:37:INFO:837:io.to_raster: Windows 1--48 of 48 ...TypeError Traceback (most recent call last) File ~\Miniconda3\envs\gwenv\lib\site-packages\geowombat\core\geoxarray.py:786, in GeoWombatAccessor.to_raster(self, filename, readxsize, readysize, separate, use_dask_store, out_block_type, keep_blocks, verbose, overwrite, gdal_cache, scheduler, n_jobs, n_workers, n_threads, n_chunks, overviews, resampling, use_client, address, total_memory, driver, nodata, blockxsize, blockysize, tags, **kwargs) File ~\Miniconda3\envs\gwenv\lib\site-packages\geowombat\core\io.py:902, in to_raster(data, filename, readxsize, readysize, use_dask_store, separate, out_block_type, keep_blocks, verbose, overwrite, gdal_cache, scheduler, n_jobs, n_workers, n_threads, n_chunks, use_client, address, total_memory, processes, padding, tags, tqdm_kwargs, **kwargs) TypeError: type object argument after ** must be a mapping, not NoneType ============================================
Output: 22:09:01:INFO:813:io.to_raster: Writing data to file ... 22:09:01:INFO:837:io.to_raster: Windows 1--48 of 48 ...
TypeError Traceback (most recent call last) File ~\Miniconda3\envs\gwenv\lib\site-packages\geowombat\core\geoxarray.py:786, in GeoWombatAccessor.to_raster(self, filename, readxsize, readysize, separate, use_dask_store, out_block_type, keep_blocks, verbose, overwrite, gdal_cache, scheduler, n_jobs, n_workers, n_threads, n_chunks, overviews, resampling, use_client, address, total_memory, driver, nodata, blockxsize, blockysize, tags, **kwargs) File ~\Miniconda3\envs\gwenv\lib\site-packages\geowombat\core\io.py:902, in to_raster(data, filename, readxsize, readysize, use_dask_store, separate, out_block_type, keep_blocks, verbose, overwrite, gdal_cache, scheduler, n_jobs, n_workers, n_threads, n_chunks, use_client, address, total_memory, processes, padding, tags, tqdm_kwargs, **kwargs) TypeError: type object argument after ** must be a mapping, not NoneType |
@jgrss Just wondering if we can push your |
Just do this and It will work.
Don't forget to |
I am out until next week and then can take a look at this thread in more detail. @mmann1123 The |
Using the following code worked, thanks @willieseun
|
You're welcome |
@mmann1123 @pjhav @willieseun I reopened this because the above solution was a temporary fix using another library. See PR #187 for fixes. With the changes in #187, both of the following snippets work for me. import geowombat as gw
from geowombat.data import l8_224078_20200518, l8_224078_20200518_polygons
from geowombat.ml import fit_predict
from sklearn.pipeline import Pipeline
from sklearn.cluster import KMeans
cl = Pipeline(
[
('clf', KMeans(n_clusters=6, random_state=0))
]
)
# Fit_predict unsupervised classifier
with gw.config.update(ref_res=150):
with gw.open(l8_224078_20200518) as src:
y = fit_predict(src, cl)
y.sel(time='t1').gw.to_raster('Unsup.tif')
# GeoWombat expects a 3d (time, y, x | band, y, x) or 2d (y, x) array to write to file, so the following also works
# y.squeeze().gw.to_raster('Unsup.tif')
# Note that if the 'time' dimension is > 1, neither of the above would work because of the coordinate order. Instead, do
# y.transpose('time', 'y', 'x').gw.to_raster('Unsup.tif') @mmann1123 in the above snippet, the output of import geowombat as gw
from geowombat.data import l8_224077_20200518_B2, l8_224077_20200518_B3
with gw.open(
[l8_224077_20200518_B2, l8_224077_20200518_B3],
stack_dim='band',
band_names=["B2", "B3"]
) as src:
# Xarray drops attributes
attrs = src.attrs.copy()
# Apply operations on the DataArray
src = src * 10.0
src.attrs = attrs
src.attrs.update({'scales':(1,1)})
src.attrs.update({'offsets':(0,0)})
# Write the data to a GeoTiff
src.gw.to_raster('output.tif') |
Still not working. |
@willieseun Did you install the |
Just want to be clear that PR #187 has the fix but it has not yet been merged. It needs approval by @mmann1123. |
Ok. I am using pip with git. |
I just installed the library all over again. I guess it didn't work since it hasn't been merged. |
Thanks for clarifying. If you want to test the fixes before they are merged into git clone https://github.com/jgrss/geowombat.git
cd geowombat/
git fetch origin jgrss/to_raster:jgrss/to_raster
git checkout jgrss/to_raster
pip install . Otherwise, I will ping you as soon as it is merged and there is a new release. |
|
Alright |
I can confirm it works now. Thanks for your help. |
Sorry for the delay, I have been on holidays. For future reference, I did have to |
Hello, I tried writing data arrays using the to_raster function. But, I am getting an error 'TypeError: tqdm.std.tqdm() argument after ** must be a mapping, not NoneType'. I don't know what I am doing wrong so I appreciate if you can look into it.
The text was updated successfully, but these errors were encountered: