## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 8,
  fig.height = 4.5
)

## ----setup--------------------------------------------------------------------
library(BIDistances)

## ----method-count-------------------------------------------------------------
MethodCount = data.frame(
  AccessLayer = c(
    "Package-defined DistanceMatrix routes",
    "Predefined parallelDist methods",
    "Additional philentropy identifiers"
  ),
  CanonicalRoutes = c(19, 41, 34),
  Availability = c(
    "Core",
    "Core import",
    "Suggested package"
  ),
  stringsAsFactors = FALSE
)

knitr::kable(MethodCount, row.names = FALSE)

## ----unified-interface--------------------------------------------------------
data("Hepta")
Data = Hepta$Data

D_euclidean = DistanceMatrix(
  Data,
  method = "euclidean"
)

D_minkowski_p3 = DistanceMatrix(
  Data,
  method = "minkowski",
  p = 3,
  threads = 2
)

D_cosine = DistanceMatrix(
  Data,
  method = "Cosine_Distance"
)

c(
  EuclideanRows = nrow(D_euclidean),
  MinkowskiRows = nrow(D_minkowski_p3),
  CosineRows = nrow(D_cosine)
)

## ----distance-selection, message = FALSE, warning = FALSE, eval = FALSE-------
# set.seed(42)
# 
# Selection = DistanceDistributions(
#   Data = Data,
#   DistanceMethods = c(
#     "euclidean",
#     "manhattan",
#     "minkowski",
#     "chord"
#   ),
#   CosineNonParallel = TRUE,
#   CorrelationDist = TRUE,
#   PlotIt = FALSE,
#   PlotSampleSize = 5000
# )
# 
# Selection$DistanceChoice
# Selection$SelectionStatistics[, c(
#   "Distance", "DipStatistic", "DipPValue", "BimodalityAmplitude", "Status"
# )]

## ----distance-selection-plot, message = FALSE, warning = FALSE, eval = FALSE----
# Selection$ggobject

## ----properties---------------------------------------------------------------
Properties = DistanceProperties()
Relevant = grepl(
  "Minkowski|Fractional|Tfidf|DTW|Mahalanobis|Cosine",
  Properties$Function
)

knitr::kable(
  Properties[Relevant, c("Function", "Classification", "Conditions")],
  row.names = FALSE
)

## ----minkowski-cpu------------------------------------------------------------
MinkowskiData = as.matrix(iris[1:40, 1:4])
Weights = c(1, 2, 0.5, 1)

D_parallel = Minkowski_Distance(
  Data = MinkowskiData,
  p = 3,
  Weights = Weights,
  backend = "parallelDist",
  threads = 2
)

D_multicore = Minkowski_Distance(
  Data = MinkowskiData,
  p = 3,
  Weights = Weights,
  backend = "multicore",
  threads = 2
)

c(
  MaximumAbsoluteDifference = max(abs(D_parallel - D_multicore)),
  EqualWithinTolerance = isTRUE(
    all.equal(D_parallel, D_multicore, tolerance = 1e-10)
  )
)

## ----minkowski-dispatch-------------------------------------------------------
D_dispatch = DistanceMatrix(
  MinkowskiData,
  method = "Minkowski_Distance",
  p = 3,
  Weights = Weights,
  backend = "multicore",
  threads = 2
)

isTRUE(all.equal(D_dispatch, D_multicore, tolerance = 1e-10))

## ----minkowski-opencl, eval = FALSE-------------------------------------------
# D_opencl = Minkowski_Distance(
#   Data = MinkowskiData,
#   p = 3,
#   Weights = Weights,
#   backend = "opencl",
#   Mem = 2
# )
# 
# max(abs(D_opencl - D_multicore))

## ----memory-plan--------------------------------------------------------------
MemoryPlan = calculateMemoryDemandGPU(
  n = 70000,
  d = 784,
  mem = 6
)
MemoryPlan

## ----tfidf--------------------------------------------------------------------
data("Hearingloss_N109")
Gene2Term = Hearingloss_N109$FeatureMatrix_Gene2Term

GO_Result = Tfidf_Distance(
  Gene2Term,
  tf_fun = mean
)

c(
  Genes = nrow(Gene2Term),
  GOTerms = ncol(Gene2Term),
  DistanceRows = nrow(GO_Result$Distance)
)

GO_Result$TfidfWeights[1:5]
GO_Result$Distance[1:5, 1:5]

## ----tfidf-dispatch-----------------------------------------------------------
GO_Distance = DistanceMatrix(
  Gene2Term,
  method = "Tfidf_Distance",
  tf_fun = mean
)

isTRUE(all.equal(GO_Distance, GO_Result$Distance))

## ----time-series--------------------------------------------------------------
TimeSeries = cbind(
  SeriesA = c(0, 1, 2, 1, 0),
  SeriesB = c(0, 1, 1.5, 1, 0),
  SeriesC = c(2, 1, 0, 1, 2)
)

D_msmd = DistanceMatrix(
  TimeSeries,
  method = "MSMD_Distance",
  ParameterC = 1
)
D_msmd

## ----mixed-data, eval = FALSE-------------------------------------------------
# MixedData = data.frame(
#   expression = c(2.1, 1.7, 4.2, 3.8),
#   age = c(42, 38, 61, 58),
#   subtype = factor(c("A", "A", "B", "B"))
# )
# 
# D_mixed = DistanceMatrix(
#   MixedData,
#   method = "manydist",
#   preset = "gower"
# )

