From b766ed4c016b243d46fed8d615b26f771e3d1d45 Mon Sep 17 00:00:00 2001 From: Anton Antonov Date: Mon, 16 Nov 2015 21:28:15 -0500 Subject: [PATCH] Added a Dataset object conversion to a RSparseMatrix object. --- Misc/RSparseMatrix.m | 56 +++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/Misc/RSparseMatrix.m b/Misc/RSparseMatrix.m index ae7e76f9..2ffd6e1c 100644 --- a/Misc/RSparseMatrix.m +++ b/Misc/RSparseMatrix.m @@ -92,7 +92,7 @@ Note that assignment (with Set[__]) is not implemented. "The dimension names `1` are expected to be a list of two strings."; ToRSparseMatrix::arg1 = - "The first argument is expected to a sparse array or a RSparseMatrix object."; + "The first argument is expected to be a sparse array, a dataset with two dimensions, or a RSparseMatrix object"; Options[MakeRSparseMatrix] = {"RowNames" -> None, "ColumnNames" -> None, "DimensionNames" -> None}; @@ -141,6 +141,30 @@ Note that assignment (with Set[__]) is not implemented. If[dnames === None, None, AssociationThread[dnames, {1, 2}]]|>] ]; +ToRSparseMatrix[ds_Dataset, opts : OptionsPattern[]] := + Block[{rows, dsRownames, dsColnames, vals, res}, + rows = Normal[ds]; + If[AssociationQ[rows], + dsRownames = Keys[rows]; + rows = rows /@ dsRownames, + (*ELSE*) + dsRownames = None; + ]; + If[AssociationQ[rows[[1]]], + dsColnames = Keys[rows[[1]]]; + vals = Map[Values, rows], + (*ELSE*) + dsColnames = None; + vals = rows; + ]; + + res = ToRSparseMatrix[SparseArray[vals], "RowNames" -> dsRownames, "ColumnNames" -> dsColnames]; + + If[ Length[{opts}] == 0, res, + ToRSparseMatrix[ res, opts ] + ] + ] /; Length[Dimensions[ds]] == 2; + ToRSparseMatrix[___] := Message[ToRSparseMatrix::arg1]; SparseArray[rmat_RSparseMatrix] ^:= rmat[[1]]["sparseArray"]; @@ -419,22 +443,26 @@ There are three solutions (1) using array rules, (2) using matrix padding, Array (*F_[rmat_RSparseMatrix, args___] ^:=*) (*Block[{res = F[SparseArray[rmat], args]},*) - (*Print["RSparseMatrix decoration::F=",F];*) - (*Print["RSparseMatrix decoration::res=",res];*) + (*Print["RSparseMatrix decoration::F=",F];*) + (*Print["RSparseMatrix decoration::res=",res];*) (*If[MatrixQ[res],*) (*RSparseMatrix[*) (*Join[<|"sparseArray" -> SparseArray[res]|>, Rest[rmat[[1]]]]],*) (*res*) (*]*) - (*] /; !*) - (*MemberQ[{SparseArray, ToRSparseMatrix,*) - (*RowNames, ColumnNames, DimensionNames,*) - (*SetRowNames, SetColumnNames, SetDimensionNames,*) - (*MatrixForm, MatrixPlot,*) - (*Dimensions, ArrayRules,*) - (*Total, RowSums, ColumnSums, RowsCount, ColumnsCount,*) - (*Dot, Plus, Times, Part,*) - (*RowBind, ColumnBind,*) - (*Head, Format, Print*) - (*}, F];*) + (*] /;*) + (*! MemberQ[*) + (*Join[{"SparseArray", "ToRSparseMatrix",*) + (*"RowNames", "ColumnNames", "DimensionNames",*) + (*"SetRowNames", "SetColumnNames", "SetDimensionNames",*) + (*"MatrixForm", "MatrixPlot",*) + (*"Dimensions", "ArrayRules",*) + (*"Total", "RowSums", "ColumnSums", "RowsCount", "ColumnsCount",*) + (*"Dot", "Plus", "Times", "Part",*) + (*"RowBind", "ColumnBind",*) + (*"Head", "Format", "Print"},*) + (*Names["System`*Hold*"],*) + (*Names["System`Inactiv*"],*) + (*Names["System`Activ*"]*) + (*], SymbolName[F] ];*)