Code Monkey home page Code Monkey logo

quads's Introduction

quads

Documentation Status CI

A pure Python Quadtree implementation.

Quadtrees are a useful data structure for sparse datasets where the location/position of the data is important. They're especially good for spatial indexing & image processing.

An actual visualization of a quads.QuadTree:

quadtree_viz

Usage

Full documentation at https://quads.readthedocs.io/en/latest/

>>> import quads
>>> tree = quads.QuadTree(
...     (0, 0),  # The center point
...     10,  # The width
...     10,  # The height
... )

# You can choose to simply represent points that exist.
>>> tree.insert((1, 2))
True
# ...or include extra data at those points.
>>> tree.insert(quads.Point(4, -3, data="Samus"))
True

# You can search for a given point. It returns the point if found...
>>> tree.find((1, 2))
Point(1, 2)

# Or `None` if there's no match.
>>> tree.find((4, -4))
None

# You can also find all the points within a given region.
>>> bb = quads.BoundingBox(min_x=-1, min_y=-2, max_x=2, max_y=2)
>>> tree.within_bb(bb)
[Point(1, 2)]

# You can also search to find the nearest neighbors of a point, even
# if that point doesn't have data within the quadtree.
>>> tree.nearest_neighbors((0, 1), count=2)
[
    Point(1, 2),
    Point(4, -4),
]

# And if you have `matplotlib` installed (not required!), you can visualize
# the tree.
>>> quads.visualize(tree)

Installation

$ pip install quads

Requirements

  • Python 3.7+ (untested on older versions but may work)

Running Tests

$ git clone https://github.com/toastdriven/quads.git
$ cd quads
$ poetry install
$ poetry shell

# Just the tests.
$ pytest .

# With coverage.
$ pytest -s --cov=quads .
# And with pretty reports.
$ pytest -s --cov=quads . && coverage html

License

New BSD

quads's People

Contributors

toastdriven avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

quads's Issues

Numba Integration?

Looking through this library. Super legit I love the clarity.

Looks like it would be fairly easy to wrap numba around your library. Your only import is the math module which should be compatibly. Any interest in doing this? I'd be happy to contribute.

Insert() is not functional when data with properties inserted

def insert(self, point):
    """
    Inserts a `Point` into the quadtree.

    Args:
        point (Point|tuple|None): The point to insert.
        data (any): Optional. Corresponding data for that point. Default
            is `None`.

    Returns:
        bool: `True` if insertion succeeded, otherwise `False`.
    """
    pnt = self.convert_to_point(point)
    # pnt.data = data
    return self._root.insert(pnt)

In the insert() of class QuadTree, suggest to remove the "data=None" in the insert and delete "pnt.data=data".

Sorry for the code format and hope this helps!

Duplicate Points can be inserted

I've been looking into quads for some spatial indexing I need to do and I think it looks like it will do the job very nicely.

One thing I've noticed is that a point can be inserted more than once, eg. .insert(Point(1,2)) and .insert(Point(1,2)). It shows up with a recursion error when it is inserted more than 'capacity' times. I'm assuming that is expected that a point should only be inserted once but wanted to confirm before making any changes for a fix.

Docstrings

What it says on the tin. I need to flesh out docstrings across the board.

Add nearest neighbor support

What it says on the tin. This is one of the big benefits of using a Quadtree & absolutely required for what I need this library for.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.