Is there a way to measure uniformness of points in a 2D square? Questions : Say I have 1000 points, that are distributed in a $[0,1]\times[0,1]$ square. It is not uniformly distributed. For example I might have "clusters" of points within the square, and conversely, there may be some empty patches of space within the square. I want to get a measure of uniformness in the distribution of the points. But, I don't think it can be the $\chi^2$ test for a uniform distribution for what I want to do. The measure should be continuous with respect to the point locations.
Context: I have a dataset of variables, that are closely linked to the interaction positions within a $[0,1]\times[0,1]$ square. I do not know those points a priori, but I do know that when I map those variables to the points in the square, it has to be somewhat uniformly distributed. (It should look as if they were sampled from a 2d uniform distribution.)
I want to learn the mapping between the variables and the point locations, so I am trying to setup a neural network model that can learn to map those variables. Loss function will be a measure of uniformness, so that if the learned mapping does not give a uniformly distribution of points, it will be penalized and the model will be corrected (through backpropagation). Now I've been thinking about what could be used for such measure of uniformness. With some Google search, I learned that the $\chi^2$ test can be used to measure the uniformness of points, but I don't think it can be used for the neural network, because the method is based on dividing the square into many small squares, and  counting the number of points in each small squares. The problem is, the gradient cannot be calculated, because an infinitismal change in the point locations won't change the $\chi^2$ test results. So there is no gradient and the model cannot be trained.
So I need a measure of uniformness, with which I can calculate the gradient w.r.t. point locations. So it has to be a continuous measure. Does anyone know of such a measure? Thank you.

The solution depends on the possible alternatives to uniformity you need to measure.
One attractive approach that immediately yields several solutions is to use a measure of the distance between the empirical copula and the independence copula (which describes the uniform distribution on the unit square). For instance, you might use the $L^2$ norm of the difference between the copulae.  See the code at the end of this post for details.
Here are the results of a simulation study of the distribution of this norm when $n=100$ points are created with iid Beta$(1,\beta)$ coordinates. When $\beta=1$ this is uniform and as $\beta$ increases the points start favoring locations near the origin.

The first row plots the Beta density functions.  The second row plots sample data.  The third row plots the distribution of 500 values of this norm from 500 independent sets of sample data.
The distribution for $\beta=1.5$ is clearly separated from the distribution for $\beta=1,$ showing this approach has high power to discriminate the third or fourth columns from the left column.
A subtler way in which a distribution might fail to be uniform is by local clustering (or anticlustering).  To explore that, I created uniform samples of the square and snapped the results to a grid of $h+1$ equally spaced values.

This method has only a little power to distinguish $h=10$ from no clustering with $n=100$ points, but is very powerful at identifying the $h=5$ clustering at the right.
Because you are interested in datasets with about $n=1000$ points, here is a similar study for simulated data of that size.  It exhibits reasonable power to distinguish the middle plot ($\beta=1.05$) from uniformity and high power to distinguish the right plot ($\beta=1.1$).  I cannot visually discern any difference in uniformity among these cases, suggesting the $L^2$ norm works well.


If you are concerned about identifying local clustering, instead use a local measure of non-uniformity, such as the mean nearest-neighbor distance or (edge-adjusted) mean numbers of neighbors within a moving window of fixed size.

This R code snippet simulates an array of points in the unit square and (using a $O(n^2)$ brute force algorithm) computes the $L^2$ distance to the independence copula.
uv <- matrix(runif(2*n), 2)       # Coordinates in columns
y <- apply(uv, 2, function(xy) {
  z <- uv - xy                    # Vector between `xy` and a point
  mean(z[1,] <= 0 & z[2,] <= 0)   # Value of the copula at `xy`
})
sqrt(mean((uv[1,]*uv[2,] - y)^2)) # L^2 distance between the reference and the copula

A more efficient method to compute the empirical copula would use a lexicographic sort on the coordinates or an equivalent efficient spatial search structure (e.g., a quadtree).
