question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot access content of data with custom Sentinel Class

See original GitHub issue

Hello! Ive built a new class to work with Sentinel2 data that has been processed (cloud removal and rolled up to monthly)

The issue is that the dataloader can’t seem to sample data from it

Class:

class Sentinel_test(RasterDataset):
    """Abstract base class for all Sentinel datasets.

    `Sentinel <https://sentinel.esa.int/web/sentinel/home>`_ is a family of
    satellites launched by the `European Space Agency (ESA) <https://www.esa.int/>`_
    under the `Copernicus Programme <https://www.copernicus.eu/en>`_.

    If you use this dataset in your research, please cite it using the following format:

    * https://asf.alaska.edu/data-sets/sar-data-sets/sentinel-1/sentinel-1-how-to-cite/
    """


class Sentinel2_test(Sentinel_test):
    filename_glob = "*B0*.tif"

    filename_regex = "^(?P<date>\d{6})\S{4}(?P<band>B[018][\dA]).tif$"
    
    date_format = "%Y%m"

    # https://gisgeography.com/sentinel-2-bands-combinations/
    all_bands = [
     
        "B03",
        
        "B08",
        
    ]
    RGB_BANDS = ["B04", "B03", "B02"]

    separate_files = True
    def __init__(
        self,
        root: str = "data",
        crs: Optional[CRS] = None,
        res: Optional[float] = None,
        bands: Sequence[str] = [],
        transforms: Optional[Callable[[Dict[str, Any]], Dict[str, Any]]] = None,
        cache: bool = True,
    ) -> None:
        """Initialize a new Dataset instance.

        Args:
            root: root directory where dataset can be found
            crs: :term:`coordinate reference system (CRS)` to warp to
                (defaults to the CRS of the first file found)
            res: resolution of the dataset in units of CRS
                (defaults to the resolution of the first file found)
            bands: bands to return (defaults to all bands)
            transforms: a function/transform that takes an input sample
                and returns a transformed version
            cache: if True, cache file handle to speed up repeated sampling

        Raises:
            FileNotFoundError: if no files are found in ``root``
        """
        self.bands = bands if bands else self.all_bands

        super().__init__(root, crs, res, transforms, cache)


    def plot(  # type: ignore[override]
        self,
        sample: Dict[str, Tensor],
        show_titles: bool = True,
        suptitle: Optional[str] = None,
    ) -> plt.Figure:
            """Plot a sample from the dataset.

            Args:
                sample: a sample returned by :meth:`RasterDataset.__getitem__`
                show_titles: flag indicating whether to show titles above each panel
                suptitle: optional string to use as a suptitle

            Returns:
                a matplotlib Figure with the rendered sample

            Raises:
                ValueError: if the RGB bands are not included in ``self.bands``

            .. versionadded:: 0.3
            """
            rgb_indices = []
            for band in self.RGB_BANDS:
                if band in self.bands:
                    rgb_indices.append(self.bands.index(band))
                else:
                    raise ValueError("Dataset doesn't contain some of the RGB bands")

            image = sample["image"][rgb_indices].permute(1, 2, 0)
            image = torch.clamp(image / 3000, min=0, max=1)  # type: ignore[attr-defined]

            fig, ax = plt.subplots(1, 1, figsize=(4, 4))

            ax.imshow(image)
            ax.axis("off")

            if show_titles:
                ax.set_title("Image")

            if suptitle is not None:
                plt.suptitle(suptitle)

            return fig

Loading the data set works fine:

ds_test = Sentinel2_test(path, bands=['B03', 'B08'])
sampler_test = RandomGeoSampler(ds_test, size=1024, length=3)
dl_test = DataLoader(ds_test, sampler=sampler_test, collate_fn=stack_samples)
print(ds_test)
Sentinel2_test Dataset
    type: GeoDataset
    bbox: BoundingBox(minx=24.834824344768293, maxx=25.150582167136307, miny=52.28913605802911, maxy=53.044619211973625, mint=1554076799.999999, maxt=1556668799.999999)
    size: 2

But when I try to plot samples I get the error ‘Cannot choose from an empty sequence’

fig, ax = plt.subplots(5, figsize = (10,15) )
plt.tight_layout()

for sample in dl_test:
    image = sample['image']
    image = torch.squeeze(image)
    # # print(image)
    ax[0].imshow(image[0])
    ax[0].set_title('B3')
    
    
    ax[1].imshow(image[1])
    ax[1].set_title('B8')
IndexError                                Traceback (most recent call last)
[<ipython-input-121-cc724a83bbc8>](https://localhost:8080/#) in <module>()
      3 plt.tight_layout()
      4 
----> 5 for sample in dl_test:
      6     image = sample['image']
      7     image = torch.squeeze(image)

5 frames
[/usr/lib/python3.7/random.py](https://localhost:8080/#) in choice(self, seq)
    259             i = self._randbelow(len(seq))
    260         except ValueError:
--> 261             raise IndexError('Cannot choose from an empty sequence') from None
    262         return seq[i]
    263 

IndexError: Cannot choose from an empty sequence

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
graceebc9commented, Mar 15, 2022

ah thank you so much! patch was too big

1reaction
graceebc9commented, Mar 15, 2022

using 0.3.0.dev0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage access to Microsoft Sentinel data by resource
If your data is not an Azure resource, such as Syslog, CEF, or AAD data, or data collected by a custom collector, you'll...
Read more >
Error codes used to monitor Sentinel
You can monitor Sentinel logs (by default, Sentinel\log\sentinel.log) with a third-party tool that looks for error codes.
Read more >
Error Codes - Sentinel Product Documentation
Error Code Description Category 112 The input string is not valid. Activation 151 Payload required. Activation 623 Enter AID or License String Activation
Read more >
Guard against access to Optional<T>'s .value property when ...
(Using null or undefined as a sentinel value for "missing" is not desired, as that's precisely what this class is for--to avoid the...
Read more >
Sentinel Administration Guide - Micro Focus
Contents. 7. Raw Data Retention Policy . ... Cannot Set the Admin Role as the Search Proxy Role. ... Unable to Connect to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found