TutteInstitute / datamapplot

Creating beautiful plots of data maps
MIT License
759 stars 49 forks source link

'ValueError: array of sample points is empty' with certain randomly generated coordinates #33

Open zilch42 opened 2 months ago

zilch42 commented 2 months ago

Hi there,

I'm having a weird issue where some sets of coordinates produce a ValueError and other sets of coordinates don't. I'm not exactly sure what the difference between these two coordinate examples is.

I've hard coded two examples of coordinates generated from np.random.rand() below. dummy_coords1 is an example that works, and dummy_coords2 is an example that does not. Both were produced by np.random.rand(12, 2)

datamapplot v0.3.0

import numpy as np
import datamapplot

dummy_labels = [['T1', 'Unlabelled', 'T1', 'T1', 'T2', 'T2', 'T2', 'T2', 'Unlabelled', 'Unlabelled', 'T2', 'T2'],
                ['T3', 'Unlabelled', 'T4', 'T4', 'T3', 'T3', 'T4', 'T5', 'Unlabelled', 'Unlabelled', 'T5', 'T5']]
dummy_label_array = np.array(dummy_labels)
dummy_hover = ['Text']*12

dummy_coords = np.random.rand(12, 2)

# works
dummy_coords1 = np.array([[0.36944978, 0.24840278],
       [0.05371874, 0.6797169 ],
       [0.73539839, 0.5694784 ],
       [0.37300726, 0.33794748],
       [0.09289142, 0.2358011 ],
       [0.95914631, 0.72673466],
       [0.67561212, 0.54775067],
       [0.54549458, 0.04819768],
       [0.88099899, 0.39860445],
       [0.26929064, 0.12571897],
       [0.84381725, 0.57393012],
       [0.74503466, 0.33412061]])

# doesn't work
dummy_coords2 = np.array([[0.90951533, 0.08958288],
       [0.98867221, 0.11019179],
       [0.01779863, 0.02374286],
       [0.80546908, 0.43450257],
       [0.09368194, 0.84672945],
       [0.89156692, 0.51185024],
       [0.68626036, 0.54685405],
       [0.94836897, 0.4673322 ],
       [0.98332563, 0.38787542],
       [0.32122166, 0.89713211],
       [0.00077888, 0.36843552],
       [0.0949498 , 0.40831818]])

datamapplot.create_interactive_plot(
    dummy_coords2, 
    *dummy_label_array, 
    hover_text=dummy_hover, 
    cluster_boundary_polygons=False
    )
{
    "name": "ValueError",
    "message": "array of sample points is empty",
    "stack": "---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[12], line 39
     25 # doesn't work
     26 dummy_coords2 = np.array([[0.90951533, 0.08958288],
     27        [0.98867221, 0.11019179],
     28        [0.01779863, 0.02374286],
   (...)
     36        [0.00077888, 0.36843552],
     37        [0.0949498 , 0.40831818]])
---> 39 datamapplot.create_interactive_plot(
     40     dummy_coords2, 
     41     *dummy_label_array, 
     42     hover_text=dummy_hover, 
     43     cluster_boundary_polygons=False
     44     )

File c:\\Users\\\[path]\\lib\\site-packages\\datamapplot\\create_plots.py:457, in create_interactive_plot(data_map_coords, hover_text, inline_data, noise_label, noise_color, color_label_text, label_wrap_width, label_color_map, width, height, darkmode, palette_hue_shift, palette_hue_radius_dependence, cmap, marker_size_array, marker_color_array, use_medoids, cluster_boundary_polygons, color_cluster_boundaries, polygon_alpha, *label_layers, **render_html_kwds)
    455 if label_color_map is None:
    456     if cmap is None:
--> 457         palette = palette_from_datamap(
    458             data_map_coords,
    459             label_dataframe[[\"x\", \"y\"]].values,
    460             hue_shift=palette_hue_shift,
    461             radius_weight_power=palette_hue_radius_dependence,
    462         )
    463     else:
    464         palette = palette_from_cmap_and_datamap(
    465             cmap,
    466             data_map_coords,
    467             label_dataframe[[\"x\", \"y\"]].values,
    468             radius_weight_power=palette_hue_radius_dependence,
    469         )

File c:\\Users\\[path]\\lib\\site-packages\\datamapplot\\palette_handling.py:65, in palette_from_datamap(umap_coords, label_locations, hue_shift, theta_range, radius_weight_power, min_lightness)
     58         chroma = (
     59             np.argsort(np.argsort(data_map_radii[r_mask])) / mask_size
     60         ) * 80 + 20
     61         lightness = (
     62             1.0 - (np.argsort(np.argsort(data_map_radii[r_mask])) / mask_size)
     63         ) * (80 - min_lightness) + min_lightness
     64         location_lightness.append(
---> 65             np.interp(
     66                 r,
     67                 np.sort(data_map_radii[r_mask]),
     68                 np.sort(lightness)[::-1],
     69             )
     70         )
     71         location_chroma.append(
     72             np.interp(r, np.sort(data_map_radii[r_mask]), np.sort(chroma))
     73         )
     74 else:

File c:\\Users\\[path]\\lib\\site-packages\
umpy\\lib\\function_base.py:1599, in interp(x, xp, fp, left, right, period)
   1596     xp = np.concatenate((xp[-1:]-period, xp, xp[0:1]+period))
   1597     fp = np.concatenate((fp[-1:], fp, fp[0:1]))
-> 1599 return interp_func(x, xp, fp, left, right)

ValueError: array of sample points is empty"
}
lmcinnes commented 2 months ago

I have some guesses, but I'll actually have to try it out and see what exactly is the issue and how easy it will be to fix. I'm not sure when I'll have enough time to set aside to do debugging, but hopefully soon.