Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error in optimize_embedding #22

Open
dkassler opened this issue Sep 7, 2023 · 3 comments
Open

Type error in optimize_embedding #22

dkassler opened this issue Sep 7, 2023 · 3 comments

Comments

@dkassler
Copy link

dkassler commented Sep 7, 2023

When running umap, I get the following error:

Error in eval(ei, envir) : 
  Not compatible with requested type: [type=character; target=double].

I've traced this to umap:::optimize_embedding, but because I don't speak C well I can't follow the .Call inside any further. I've checked all the arguments that are getting passed on to the C code, though, and none of them are character. The only thing I can see that might be out of place is that abg = c(NA, NA, 1, 0), and I don't know if those NAs are expected.

Running umap with its default settings does not produce the error; the settings I have are as follows:

umap configuration parameters
           n_neighbors: 15
          n_components: 2
                metric: euclidean
              n_epochs: 200
                 input: data
                  init: spectral
              min_dist: 0.1
      set_op_mix_ratio: 1
    local_connectivity: 1
             bandwidth: 1
                 alpha: 1
                 gamma: 1
  negative_sample_rate: 5
                     a: NA
                     b: NA
                spread: 1
          random_state: NA
       transform_state: NA
                   knn: NA
           knn_repeats: 1
               verbose: FALSE
       umap_learn_args: NA

Is this happening because a and b are NA? The function does warn me that

1: umap: parameters 'spread', 'a', 'b' are set to non-default values;
 parameter 'spread' will be ignored.
 (Embedding will be controlled via 'a' and 'b')

Which now that I write this seems suspicious. But umap.defaults also has a and b set to NA.

@tkonopka
Copy link
Owner

tkonopka commented Sep 8, 2023

Thanks for reporting. That config seems to match the default (a=NA, b=NA, spread=1) so am not sure why you are seeing the warning. Could you confirm that is indeed the configuration, please?

It is helpful that you traced to optimize_embedding and that none of the inputs are character, but the root cause doesn't spring to mind. Would you be able to share a minimal example, especially your config settings (see above). And just to be sure, are you using version 0.2.10?

Sorry this is not immediately helpful, but let's investigate further.

@dkassler
Copy link
Author

Yes, I'm using umap 2.10. Did a little more detective work, and this is what I've found:

The umap config settings I'm using (umap_config) appear to have the same values as umap.defaults when printed, but that's only because of the print method for class umap.config. In actuality, my config file had "NA" instead of NA and 1L instead of 1 because it was (incorrectly) parsed from a YAML file.

> cbind(umap.defaults, sapply(umap.defaults, class), umap_config, sapply(umap_config, class))
                     umap.defaults             umap_config            
n_neighbors          15            "numeric"   15          "integer"  
n_components         2             "numeric"   2           "integer"  
metric               "euclidean"   "character" "euclidean" "character"
n_epochs             200           "numeric"   200         "integer"  
input                "data"        "character" "data"      "character"
init                 "spectral"    "character" "spectral"  "character"
min_dist             0.1           "numeric"   0.1         "numeric"  
set_op_mix_ratio     1             "numeric"   1           "integer"  
local_connectivity   1             "numeric"   1           "integer"  
bandwidth            1             "numeric"   1           "integer"  
alpha                1             "numeric"   1           "integer"  
gamma                1             "numeric"   1           "integer"  
negative_sample_rate 5             "numeric"   5           "integer"  
a                    NA            "logical"   "NA"        "character"
b                    NA            "logical"   "NA"        "character"
spread               1             "numeric"   1           "integer"  
random_state         NA            "logical"   "NA"        "character"
transform_state      NA            "logical"   "NA"        "character"
knn                  NA            "logical"   "NA"        "character"
knn_repeats          1             "numeric"   1           "integer"  
verbose              FALSE         "logical"   FALSE       "logical"  
umap_learn_args      NA            "logical"   "NA"        "character"

If you run the two different configs you get:

> umap(mtcars)
umap embedding of 32 items in 2 dimensions
object components: layout, data, knn, config
> umap(mtcars, config = umap_config)
Error: Not compatible with requested type: [type=character; target=double].
In addition: Warning messages:
1: umap: parameters 'spread', 'a', 'b' are set to non-default values;
 parameter 'spread' will be ignored.
 (Embedding will be controlled via 'a' and 'b')
 
2: In umap.prep.config(config, ...) : NAs introduced by coercion
3: In umap.prep.config(config, ...) : NAs introduced by coercion

To reproduce in a minimal example, I'm attaching a dput of my config file.

umap_config <- structure(list(n_neighbors = 15L, n_components = 2L, metric = "euclidean", 
    n_epochs = 200L, input = "data", init = "spectral", min_dist = 0.1, 
    set_op_mix_ratio = 1L, local_connectivity = 1L, bandwidth = 1L, 
    alpha = 1L, gamma = 1L, negative_sample_rate = 5L, a = "NA", 
    b = "NA", spread = 1L, random_state = "NA", transform_state = "NA", 
    knn = "NA", knn_repeats = 1L, verbose = FALSE, umap_learn_args = "NA"), class = "umap.config")

As for why in my initial debug I found abg = c(NA, NA, 1, 0) instead of showing as character, I don't know because I can't reproduce that. Must have been something else I did to that environment, because reproducing it with a minimal example in a fresh environment just shows abg = c("NA", "NA", "1", "0") which is obviously the character vector the function is having problems with.

So case closed, this is user error on my part. That said, it might be nice for the function to do some type checking early on and throw more verbose error messages about that?

@tkonopka
Copy link
Owner

Thanks for the detailed investigation. Glad you found the root cause and can work around this for now.

As you suggest, additional input validation would be useful. Most inputs should be numeric values, apart from 'metric', 'input', 'init', and 'umap_learn_args'. If you'd like to make a PR, I'd be happy to incorporate into a new version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants