---
title: "Multidimensional HSDSDataset Vignette"
author: "Sam"
date: "9/6/2018"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Multidimensional verification}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
Sys.setenv('RETICULATE_PYTHON'=Sys.getenv("RETICULATE_PYTHON"))
library(rhdf5client)
library(reticulate)
```

## Access to multi-dimensional dataset:

### Get result with `rhdf5client`

```{r}
src <- HSDSSource('http://hsdshdflab.hdfgroup.org')
rd <- HSDSDataset(HSDSFile(src, '/home/spollack/testone.h5'), '/group0/group1/group2/data4d')
A <- getData(rd, list(3:4, 8:9, 5:6, 2:3))
sum(A)
```

### Get result with `reticulate` and `h5pyd`

```{r, eval=FALSE}
h5pyd <- import("h5pyd")
builtins <- import_builtins()

endpoint <- 'http://hsdshdflab.hdfgroup.org'
domain <- '/home/spollack/testone.h5'
f <- h5pyd$File(endpoint=endpoint, mode='r', domain=domain)
pd <- f$`__getitem__`('/group0/group1/group2/data4d')
scs <- tuple(builtins$slice(2L, 4L, 1L), builtins$slice(7L, 9L, 1L), 
             builtins$slice(4L, 6L, 1L), builtins$slice(1L, 3L, 1L))
A <- pd$`__getitem__`(scs)
sum(A)   
```

## Special Case: subset of array (single-width slices)

The dimensionality of the http response from the server is somewhat non-intuitive,
so we need to test a case in which the single-width slices are dispersed:

```{r, eval=FALSE}
BR <- getData(rd, list(c(4), 1:11, c(5), 1:3))
scs <- tuple(builtins$slice(3L, 4L, 1L), builtins$slice(0L, 12L, 1L),
             builtins$slice(4L, 5L, 1L), builtins$slice(0L, 3L, 1L))
BP <- pd$`__getitem__`(scs)
print(BR)
all(BP == BR)
```


```{r, eval=FALSE}
BR <- getData(rd, list(c(5), 1:11, 1:7, c(2)))
scs <- tuple(builtins$slice(4L, 5L, 1L), builtins$slice(0L, 12L, 1L),
             builtins$slice(0L, 7L, 1L), builtins$slice(1L, 2L, 1L))
BP <- pd$`__getitem__`(scs)
print(BR)
all(BP == BR)
```

## Multiple-slice fetches

### Check: multiple slices in two dimensions is OK

With a two-dimensional array, non-adjacent slices work:

```{r}
f <- HSDSFile(src, '/home/spollack/testtwo.h5')
d <- HSDSDataset(f, '/grpB/grpC/dataC')
print(getData(d, list(1:9, 1:11)))
print(getData(d, list(c(2, 3, 5, 7), c(1, 2, 4, 6, 8, 10))))
```

### Check: single slices in multiple dimensions is OK

```{r}
d <- HSDSDataset(HSDSFile(src, '/home/spollack/testone.h5'), '/group0/group1/dataR')
X <- getData(d, list(c(5), 3:6, c(2), 2:4))
print(X)
```


### Check: multiple slices in multiple dimensions OK

```{r}
d <- HSDSDataset(HSDSFile(src, '/home/spollack/testone.h5'), '/group0/group1/dataR')
BR <- getData(d, list(c(4), 2:6, c(5), 1:3))
print(BR)
BT <- getData(d, list(c(4), c(2, 3, 5, 6), c(5), 1:3))
print(BT)
BR <- getData(d, list(c(4), 2:6, c(5), 1:5))
print(BR)
BT <- getData(d, list(c(4), c(2, 3, 5, 6), c(5), c(1, 2, 4, 5)))
print(BT)
```

### Special case: single-datum fetch

```{r}
x <- getData(d, list(c(4), c(5), c(3), c(2)))
print(x)
```



















