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

LoadH5ADobs Missing Labels #214

Open
sylvia-science opened this issue Mar 21, 2024 · 0 comments
Open

LoadH5ADobs Missing Labels #214

sylvia-science opened this issue Mar 21, 2024 · 0 comments

Comments

@sylvia-science
Copy link

Hi,

I'm not very familiar with Anndata objects so I don't know if this is a common problem, but the one I was working with had missing observable values labeled with -1. Because the LoadH5ADobs tried to convert the column to a factor but is missing a label for these -1 values, the function can throw this error:

Error in factor(x = obs_value_i, labels = hfile_cate[[obs.i]][]) :
invalid 'labels'; length 24 should be 1 or 25

Which says that I have 24 labels for the 25 values with an extra -1 value.

My simple fix for this was checking if the number of unique values obs_value_i matches the length of the labels. Obviously this will fail if there is more than one missing value or if it's not -1.

if ('__categories' %in% names(x = hfile_obs)) {
  hfile_cate <- hfile_obs[['__categories']]
  for (i in seq_along(obs_groups)) {
    obs.i <- obs_groups[i]
    obs_value_i <- hfile_obs[[obs.i]][]
    if (obs.i %in% names(x = hfile_cate)){
      # If the observable has missing values, they're labeled as -1.
      # Then you add to add -1 to the categories
      if (length(unique(obs_value_i)) == length(hfile_cate[[obs.i]][])){
        obs_value_i <- factor(x = obs_value_i, labels =  hfile_cate[[obs.i]][])
      }else{
        labels = c('-1', hfile_cate[[obs.i]][])
        obs_value_i <- factor(x = obs_value_i, labels =  labels)
      }
    }
    matrix[,i] <- obs_value_i
  }
}
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

1 participant