count_samples_in_group <- function(tmp_DT, main_DT, just_iTunesDA = TRUE, byCols = "upc", sample_size.colname="sample_size") {
  # byCols ::  What to count by

  cols <- extract_fromto(tmp_DT)
  setattr(cols, "names", rm_fromto(cols))
  uCols <- unique(names(cols))
  selfname_(uCols)

  if (!length(cols))
    stop ("could not find from/to_ columns in tmp_DT")

  tmp_DT[, (sample_size.colname) := 
  {
    uVals <- lapply(uCols, function(u) unlist(.SD[, cols[names(cols) == u], with=FALSE], use.names=FALSE))
    # uVals <- unlist(.SD[, cols[names(cols) == u], with=FALSE], use.names=FALSE)
    main_DT[(is_iTunesDA | !just_iTunesDA)][, 
    all(sapply(uCols, function(u) uVals[[u]]  %in%  get(u), USE.NAMES=FALSE))
    , by=byCols][(V1), .N]
    # main_DT[(is_iTunesDA | !just_iTunesDA)][, Reduce("&", lapply(uCols, function(u) 
    #     uVals[[u]]  %in%  get(u)
    #   )), by=byCols][(V1), .N]
  } , by=seq(nrow(tmp_DT))]

  setcolorderpt(tmp_DT, unique(c(key(tmp_DT), cols, sample_size.colname)))

  return(invisible(tmp_DT))
}


count_samples_in_group <- function(tmp_DT, main_DT, just_iTunesDA = TRUE, byCols = "upc", sample_size.colname="sample_size") {
  # byCols ::  What to count by

stop("this function is incomplete")
  cols <- extract_fromto(tmp_DT)
  setattr(cols, "names", rm_fromto(cols))
  uCols <- unique(names(cols))
  selfname_(uCols)

  if (!length(cols))
    stop ("could not find from/to_ columns in tmp_DT")

  fromCols <- extract("from", cols)
  toCols   <- extract("to",   cols)
# ?   tmp_DT[, paste0(sample_size.colname, c(".from", ".to")) := 
# ?   {
# ?     main_DT[(is_iTunesDA | !just_iTunesDA)][, 
# ?         {.SD; browser()}
# ?         .SD[, names(fromCols), with=FALSE]
# ?     , by=byCols][(V1), .N]
# ? 
# ? # .... left off here -- need to separate from from to
# ? 
# ?     uVals <- lapply(uCols, function(u) unlist(.SD[, cols[names(cols) == u], with=FALSE], use.names=FALSE))
# ?     # uVals <- unlist(.SD[, cols[names(cols) == u], with=FALSE], use.names=FALSE)
# ?     main_DT[(is_iTunesDA | !just_iTunesDA)][, 
# ?     all(sapply(uCols, function(u) uVals[[u]]  %in%  get(u), USE.NAMES=FALSE))
# ?     , by=byCols][(V1), .N]
# ?     # main_DT[(is_iTunesDA | !just_iTunesDA)][, Reduce("&", lapply(uCols, function(u) 
# ?     #     uVals[[u]]  %in%  get(u)
# ?     #   )), by=byCols][(V1), .N]
# ?   } , by=seq(nrow(tmp_DT))]

  setcolorderpt(tmp_DT, unique(c(key(tmp_DT), cols, sample_size.colname)))

  return(invisible(tmp_DT))
}


if (FALSE) {

  tmp_DT[, sample_size := NULL]
  tmp_DT[1, from_TreatmentWk1 := "Poop"]
  (count_samples_in_group(tmp_DT, DT.EU_aggd.delta))
  Q

}
