### Load and Use Data Sources from an Intake Catalog Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Import the intake library, open a catalog file, and access data sources by their defined names. This example shows loading 'tiff' and 'fwf' drivers. ```python import intake catalog = intake.open_catalog('catalog.yml') coffee_data = catalog.coffee.read() table_data = catalog.table.read() ``` -------------------------------- ### Example Usage of FWFDataSource with DaskDataFrameAdapter Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Demonstrates how to use a custom FWFDataSource and read its data into a pandas DataFrame or a dask DataFrame using the DaskDataFrameAdapter. ```python import my_fwf_package data_source = my_fwf_package.FWFDataSource('example_data/table.txt') df = data_source.read() # pandas DataFrame dask_df = data_source.to_dask() # dask DataFrame ``` -------------------------------- ### TIFFReader Example for dask.array Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Implement the Reader interface for TIFF files to return a dask array. This example demonstrates handling multiple TIFF files and pages, and correctly closing resources. ```python import dask.array import tifffile class TIFFReader: """Example Reader for TIFF image files.""" container = 'dask.array.core.Array' def __init__(self, file): if isinstance(file, str): import os if os.path.isfile(file): self._tiff_files = [tifffile.TiffFile(file)] else: import glob self._tiff_files = [tifffile.TiffFile(f) for f in glob.glob(file)] else: self._tiff_files = [tifffile.TiffFile(file)] self._file = file self._closed = False def read(self): """Return a dask array from the TIFF file(s).""" if self._closed: raise Exception(f"{self} is closed") stack = [] for tf in self._tiff_files: series = tf.series[0] for page in series.pages: stack.append(dask.array.from_delayed( dask.delayed(page.asarray)(), shape=page.shape, dtype=series.dtype)) return dask.array.stack(stack) def close(self): """Release resources.""" self._closed = True for tf in self._tiff_files: tf.close() # Create intake-compatible DataSource from reader_adapter import adapt TIFFDataSource = adapt(TIFFReader, 'TIFFDataSource') ``` -------------------------------- ### Declare Intake Driver Entry Point in setup.py Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Define an 'intake.drivers' entry point in your setup.py file to register your custom data source driver with intake. ```python from setuptools import setup setup( name='my_fwf_package', packages=['my_fwf_package'], entry_points={ 'intake.drivers': [ 'fwf = my_fwf_package:FWFDataSource' ] }, install_requires=['dask[dataframe]'], ) ``` -------------------------------- ### Create and Use TIFFDataSource Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Instantiate a TIFFDataSource, discover its schema, read the entire dataset into memory as a numpy array, or convert it to a dask array for lazy evaluation. You can also iterate over partitions or read a specific partition. Remember to close the data source when done. ```python data_source = my_tiff_package.TIFFDataSource('example_data/coffee.tif') schema = data_source.discover() array = data_source.read() dask_array = data_source.to_dask() for chunk in data_source.read_chunked(): process(chunk) partition_0 = data_source.read_partition(0) data_source.close() ``` -------------------------------- ### DataSource API - Available Methods After Adaptation Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Details the methods provided by an intake DataSource once a Reader has been adapted using `reader_adapter.adapt()`. These methods align with the standard intake DataSource API. ```APIDOC ## DataSource API ### Description After a Reader class is adapted using `reader_adapter.adapt()`, the resulting DataSource object exposes the full suite of methods available in the standard intake DataSource API. This allows for seamless integration with the intake ecosystem. ### Available Methods - **discover()**: Returns schema information about the data, such as dtype, shape, and number of partitions. - **read()**: Reads and returns the entire dataset, typically computed into a concrete format like a pandas DataFrame or numpy array. - **read_chunked()**: Reads the data in chunks, suitable for large datasets that cannot fit into memory entirely. - **read_partition(i)**: Reads and returns a specific partition of the data, identified by its index `i`. - **to_dask()**: Returns the data as a dask collection (e.g., dask DataFrame or dask Array), allowing for lazy computation. - **close()**: Closes the underlying resources associated with the data source. ### Example Usage ```python # Assuming MyDataSource is an adapted DataSource class from reader_adapter import adapt # ... (Define and adapt a Reader class as shown previously) ... # Instantiate the adapted DataSource data_source = MyDataSource('path/to/your/data') # Use the intake DataSource API data_source.discover() computed_data = data_source.read() dask_data = data_source.to_dask() data_source.close() ``` ``` -------------------------------- ### Adapt a Reader to DataSource Source: https://github.com/danielballan/reader-intake-adapter/blob/master/README.md Use the adapter to convert a custom Reader class into a fully-compliant intake DataSource. ```python MyDataSource = reader_adapter.adapt(MyReader, 'MyDataSource') ``` -------------------------------- ### Adapt Reader to DataSource Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Use `adapt()` to convert a custom Reader class into an intake-compatible DataSource. Ensure the Reader class has a `container` attribute specifying the return type of `read()` and implements `read()` and `close()` methods. ```python from reader_adapter import adapt # Define a simple Reader class class MyReader: container = 'dask.dataframe.core.DataFrame' # or 'dask.array.core.Array' def __init__(self, file): self._filepath = file self._closed = False def read(self): if self._closed: raise Exception("Reader is closed") import dask.dataframe return dask.dataframe.read_csv(self._filepath) def close(self): self._closed = True # Convert Reader to DataSource MyDataSource = adapt(MyReader, 'MyDataSource') # Use the DataSource with full intake API data_source = MyDataSource('data.csv') data_source.discover() # Returns schema info: dtype, shape, npartitions data_source.read() # Returns computed pandas DataFrame data_source.to_dask() # Returns dask DataFrame data_source.close() ``` -------------------------------- ### Register Custom Container Adapter Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Extend `reader_adapter` to support new container types by creating a custom `Adapter` subclass and registering it. The custom adapter must define `container`, `_get_schema`, `_get_partition`, and optionally `to_dask` and `read` methods. ```python from reader_adapter._registry import register from reader_adapter._base import Adapter from reader_adapter._vendored import Schema class XArrayAdapter(Adapter): container = 'xarray' _EXPECTED_CONTAINER = 'xarray.core.dataset.Dataset' def _get_schema(self): reading = self._adapter_read() return Schema( datashape=None, dtype=str(reading.dtypes), shape=reading.dims, npartitions=1, extra_metadata={}) def _get_partition(self, i): return self._adapter_read() def to_dask(self): return self._adapter_read().to_dask_dataframe() def read(self): return self._adapter_read() # Register the adapter register('xarray.core.dataset.Dataset', XArrayAdapter) ``` -------------------------------- ### Reference Custom Driver in Intake Catalog YAML Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Use the 'driver' key in your catalog.yml file to specify the custom driver for a data source, along with its file path and persistence settings. ```yaml sources: coffee: driver: "tiff" persist: "never" args: file: "example_data/coffee.tif" table: driver: "fwf" persist: "never" args: file: "example_data/table.txt" ``` -------------------------------- ### Reader Interface - Required API for Custom Readers Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Defines the minimal interface that any custom Reader class must implement to be compatible with the `adapt()` function. This includes `read()`, `close()`, and a `container` attribute. ```APIDOC ## Reader Interface ### Description This section outlines the required interface for custom Reader classes to be compatible with the `reader_adapter.adapt()` function. A Reader must implement the `read()` and `close()` methods, and possess a `container` attribute. ### Required Methods - **read()**: This method should perform the actual reading of the data and return an object representing the data container (e.g., a dask array, pandas DataFrame). - **close()**: This method should release any resources held by the reader, such as open file handles. ### Required Attribute - **container** (str): A string representing the fully-qualified class name of the object returned by the `read()` method. Examples include 'dask.array.core.Array' or 'dask.dataframe.core.DataFrame'. ### Example Implementation ```python import dask.array import tifffile class TIFFReader: """Example Reader for TIFF image files.""" container = 'dask.array.core.Array' def __init__(self, file): # ... initialization logic ... self._closed = False def read(self): """Return a dask array from the TIFF file(s).""" if self._closed: raise Exception(f"{self} is closed") # ... logic to read TIFF and return dask array ... return dask.array.stack([]) # Placeholder def close(self): """Release resources.""" self._closed = True # ... logic to close file handles ... ``` ``` -------------------------------- ### Define a Reader Class Source: https://github.com/danielballan/reader-intake-adapter/blob/master/README.md Implement this class structure to define a reader that can be automatically adapted into an intake DataSource. ```python class MyReader: container = '...' # fully-qualified class name of type(self.read()) def read(self): """ Return any type that reader_adapter knows about. This currently includes dask.array.core.Array and dask.dataframe.core.DataFrame, but could grow to include numpy.ndarray, pandas.DataFrame, and xarray types. """ ... def close(self): ... ``` -------------------------------- ### reader_adapter.register() - Register Custom Container Adapters Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt Allows for the registration of custom adapters for new container types not supported by default. This extends the library's compatibility to other data structures. ```APIDOC ## POST reader_adapter.register() ### Description Registers a custom adapter for a new container type, extending reader_adapter's support beyond built-in types like dask arrays and dataframes. ### Method POST ### Endpoint /reader_adapter.register ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **container_type** (str) - Required - The fully-qualified class name of the container (e.g., 'xarray.core.dataset.Dataset'). - **adapter_class** (class) - Required - The custom Adapter class that handles the specified container type. ### Request Example ```json { "container_type": "xarray.core.dataset.Dataset", "adapter_class": "XArrayAdapter" } ``` ### Response #### Success Response (200) - **message** (str) - Confirmation message indicating successful registration. #### Response Example ```json { "message": "Adapter for xarray.core.dataset.Dataset registered successfully." } ``` ``` -------------------------------- ### reader_adapter.adapt() - Convert a Reader to a DataSource Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt The primary function to transform a custom Reader class into a fully-compliant intake DataSource. It handles validation and selects the appropriate adapter based on the container type specified in the Reader. ```APIDOC ## POST reader_adapter.adapt() ### Description Converts a simple Reader class into a fully-compliant intake DataSource. It validates the Reader's interface and selects the appropriate adapter based on the container type. ### Method POST ### Endpoint /reader_adapter.adapt ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **ReaderClass** (class) - Required - The custom Reader class to adapt. - **DataSourceName** (str) - Required - The desired name for the new intake DataSource. ### Request Example ```json { "ReaderClass": "MyReader", "DataSourceName": "MyDataSource" } ``` ### Response #### Success Response (200) - **DataSource** (class) - The newly created intake-compatible DataSource class. #### Response Example ```json { "DataSource": "MyDataSource" } ``` ``` -------------------------------- ### DaskArrayAdapter Behavior for Dask Arrays Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt The DaskArrayAdapter is automatically used for dask.array.core.Array containers. It extracts schema information like dtype, shape, and partitions directly from the dask array and provides methods to read partitions or the entire array. ```python from reader_adapter._adapters import DaskArrayAdapter # DaskArrayAdapter is automatically used when container is 'dask.array.core.Array' # Key behavior: # _get_schema() extracts: # - dtype from dask array # - shape from dask array # - npartitions from dask array # - chunks from dask array # _get_partition(i) returns: # - Computed numpy array for block index i (tuple indexing) # read() returns: # - Fully computed numpy array (calls .compute()) # to_dask() returns: # - Original dask array for lazy computation ``` -------------------------------- ### DaskDataFrameAdapter Behavior for Dask DataFrames Source: https://context7.com/danielballan/reader-intake-adapter/llms.txt The DaskDataFrameAdapter is automatically used for dask.dataframe.core.DataFrame containers. It extracts schema information including column dtypes and shape, and provides methods to read partitions or the entire DataFrame. ```python from reader_adapter._adapters import DaskDataFrameAdapter # DaskDataFrameAdapter is automatically used when container is 'dask.dataframe.core.DataFrame' # Key behavior: # _get_schema() extracts: # - dtype as dict mapping column names to dtypes # - shape from dask dataframe # - npartitions from dask dataframe # _get_partition(i) returns: # - Computed pandas DataFrame for partition i # read() returns: # - Fully computed pandas DataFrame (calls .compute()) # to_dask() returns: # - Original dask DataFrame for lazy computation ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.