Skip to content

map_partition performing calculations on metadata #7377

Open

Description

@achapkowski

When I call map_partitions on my accessor, get some unexpected issues.

The first partition has data that is unrelated to the rest of my data. This is causing issues performing type specific data. How do I do I account for this first group of data? It appears the first group is the set of data from the registration of the make_nonempty.register call.

@make_array_nonempty.register(GeoType)
def _(dtype):
    a = np.array([_default_geometry(), _default_geometry()], dtype=object)
    return _from_geometry(a)

Is there a tag stating this is metadata? I don't want map_partitions to look at this chunk.

     SHAPE                                                        fielda  b
0  {"x": 0, "y": 0, "spatialReference": {"wkid": ...       1  1
1  {"x": 0, "y": 0, "spatialReference": {"wkid": ...       1  1
                                               SHAPE                             fielda  b
8  {"curveRings": [[[-35.09392053986361, 7.573204...    2729  2
9  {"curveRings": [[[7.505218982136703, 18.358753...    5387  2
                                               SHAPE                             fielda  b
2  {"curveRings": [[[35.51113767361919, 12.957382...    9575  2
3  {"curveRings": [[[-18.30138894072785, -16.9425...    9151  2
                                               SHAPE                             fielda  b
6  {"curveRings": [[[-21.068573113681047, 33.0624...    2846  2
7  {"curveRings": [[[-36.53534927307355, 7.872075...    4623  2
                                               SHAPE                             fielda  b
0  {"curveRings": [[[-21.43621100432236, 1.310434...    1708  2
1  {"curveRings": [[[-11.702427410357348, 30.1368...    9153  2
                                               SHAPE                            fielda  b
4  {"curveRings": [[[33.74060870013736, 40.225838...     828  2
5  {"curveRings": [[[-1.1111195840167558, -20.320...    4718  2

I need the operation to skip this snippet below:

     SHAPE                                                        fielda  b
0  {"x": 0, "y": 0, "spatialReference": {"wkid": ...       1  1
1  {"x": 0, "y": 0, "spatialReference": {"wkid": ...       1  1

I think a solution could be to use get_partitions but I think this would prevent the distribution of code to fail. Any solutions welcome.

UPDATE 1:

This only occurs is meta isn't specified on the map_partitions, but the custom type can't be understood...

Thank you

Activity

  1. achapkowski commented on Mar 12, 2021

    @achapkowski
    Author

    The custom dtype is defined as:

    class GeoType(ExtensionDtype):
        type = Geometry
        name = "geometry"
        na_value = None
    
        @classmethod
        def construct_from_string(cls, string):
            if string == cls.name:
                return cls()
            else:
                raise TypeError(
                    "Cannot construct a '{}' from '{}'".format(cls.__name__, string)
                )
    
        @classmethod
        def construct_array_type(cls):
            return GeoArray

    Where Geometry is a base class that all other Geometries inherit from.

    When given to meta on map_partitions, I get the following errors now:

      File "c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-dev\lib\site-packages\dask\array\core.py", line 1116, in __new__
        meta = meta_from_array(meta, dtype=dtype)
      File "c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-dev\lib\site-packages\dask\array\utils.py", line 70, in meta_from_array
        x = x(shape=(0,) * (ndim or 0), dtype=dtype)
    TypeError: Cannot interpret '<arcgis.features.geo._array.GeoType object at 0x000002613B28E608>' as a data type
    
    

    What needs to be added to the Panda's registered data type for it to work in Dask?

  2. jsignell commented on Mar 15, 2021

    @jsignell
    Member

    What happens if you pass in a dataframe that looks like the expected output ? So for meta in map_partitions set something like df.head() or df.partitions[1].head() if that is more representative.

  3. jsignell commented on Mar 15, 2021

    @jsignell
    Member

    Actually I'm wondering if this is a manifestation of #7375

  4. achapkowski commented on Mar 15, 2021

    @achapkowski
    Author

    If you pass what the data should look like, and it doesn't match the input, it will fail. One solution I found was to set enforce_metadata=False.

  5. gjoseph92 commented on Mar 15, 2021

    @gjoseph92
    Collaborator

    @jsignell yes, I believe this is related to #7375. Basically right now, dask does not support map_partitions with a function that returns a pandas ExtensionArray. If it's going to return an array-like, it must return a NumPy array.

    Note that #7375 will not resolve this in general; it'll just make .values on such a Series with an ExtensionDtype work. The fix for the issue in generality should only be a few lines, but it breaks other tests in weird ways, so I'd like to handle it separately.

    @achapkowski, it seems here that you're trying to turn your dask DataFrame into a dask Array, where the chunks are GeoArrays? If you could share the function you're passing to map_partitions, that could be helpful. I'm having trouble interpreting the output you've posted. Also, is that output what you see when you call compute, or what you see when you call map_partitions (as your function gets called on the meta array), or some of both?

  6. achapkowski commented on Mar 15, 2021

    @achapkowski
    Author

    The output is the partitions of data being send to each client. The first chunk is a Point dataset which is derived from this:

    #-------------------------------------------------------------------------
    @make_array_nonempty.register(GeoType)
    def _(dtype):
        a = np.array([_default_geometry(), _default_geometry()], dtype=GeoType)
        return _from_geometry(a)
    

    Where the default geometry is:

    Point({'x' : 0, 'y' : 0, 'spatialReference' : {'wkid' : 4326}})

    The output for these calls is a DataFrame. I am performing operations like spatial joins or performing overlay operations like select by location (intersection operation). In the case of the spatial join, there are cases where DataFrame A and DataFrame B can have the same schema. So I allow for a _left and _right tags. This modifies the schema and the output can be different than the input.

  7. gjoseph92 commented on Mar 15, 2021

    @gjoseph92
    Collaborator

    The output is the partitions of data being send to each client.

    How are you seeing this? Print statements in the function you're passing to map_partitions? Again, if you could share the code for the function you're passing to map_partitions, that would be very helpful.

    You may already know this, but map_partitions works by first calling your function on a "dummy" pandas DataFrame—in this case, the one produced by your @make_array_nonempty.register(GeoType) function. This happens immediately when you call map_partitions, not when you call compute.

    So basically, if you have a print statement in your mapper function, which is printing out each partition, it's expected behavior to see it getting called on the Point({'x' : 0, 'y' : 0, 'spatialReference' : {'wkid' : 4326}}) "dummy" values. Just because you see that printed doesn't mean that the Point is actually data in your DataFrame.

    The docs on DataFrame metadata might be helpful here: https://docs.dask.org/en/latest/dataframe-design.html#metadata


    It sounds like the real issue here is that your mapper function can't handle the "dummy" data you've specified (perhaps you're using a function which can't handle Points, only CurveRings?):

    This is causing issues performing type specific data. How do I do I account for this first group of data?

    There's nothing dask can really do to help able to handle this data. I'd say you should either a) add some if statements to your mapper function to be able to handle the different data types correctly, or b) change your make_array_nonempty function to return something more representative of the type of data you'll actually be using (put a CurveRing in it instead of a Point, for example).

    Alternatively, as you saw, you can pass a meta. It sounds to me like you tried passing GeoType or a GeoArray instance as meta, and that broke (this is the bug related to #7375). However, if the output is supposed to be a DataFrame, then a GeoArray is actually the wrong meta type, since that implies the result should be an Array. Instead, your meta should be a pandas DataFrame, where some of the columns have the dtype GeoType. Or, if your function doesn't change the columns/types at all, you can just pass the original DataFrame for convenience: ddf.map_partitions(my_mapper, ..., meta=ddf). See the make_meta and map_partitions documentation for more details here.

  8. achapkowski commented on Mar 16, 2021

    @achapkowski
    Author
    results = self._data.map_partitions(
                lambda part : self._sfn_method(
                    part.spatial,
                    'join',
                    **{
                        "right_df" : right_df,
                        "how": how,
                        "op" : op,
                        "left_tag" : left_tag,
                        "right_tag" : right_tag
                       }
                ),
                meta=meta,
                enforce_metadata=False
            )

    Where part would be a pd.DataFrame, part.spatial is a custom Pandas' accessor, and self._sfn_method would be:

    self._sfn_method = lambda a, sop, **kwargs: getattr(a, sop)(**kwargs)

    This allows me to perform a spatial join on each partition for the dask dataframe, or at least that is the thought.

  9. gjoseph92 commented on Mar 19, 2021

    @gjoseph92
    Collaborator

    What is right_df in this case? A pd.DataFrame or a dask DataFrame? (I don't think a dask DataFrame will work.)

    I'm still unclear on what the issue is.

    It sounds like the real issue here is that your mapper function can't handle the "dummy" data you've specified (perhaps you're using a function which can't handle Points, only CurveRings?)

    Is this accurate? Is the problem that join is failing on your dummy data that contains Points?

  10. added
    needs info
    on Mar 25, 2021
  11. added
    needs attention
    on Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrayneeds attentionneeds info

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions