1{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
2{- A dataframe is a strongly-typed, ordered list of named tuples. A dataframe differs from a relation in that its tuples are ordered.-}
3module ProjectM36.DataFrame where
5import ProjectM36.Attribute as A
7import qualified ProjectM36.Relation as R
8import ProjectM36.Relation.Show.Term
9import qualified ProjectM36.Relation.Show.HTML as RelHTML
10import ProjectM36.DataTypes.Sorting
11import ProjectM36.AtomType
13import qualified Data.Vector as V
15import qualified Data.List as L
16import qualified Data.Set as S
18import qualified Data.Text as T
23data AttributeOrderExpr = AttributeOrderExpr AttributeName Order deriving (Show, Generic, Binary)
24data AttributeOrder = AttributeOrder AttributeName Order deriving (Show, Generic, Binary)
25data Order = AscendingOrder | DescendingOrder deriving (Eq, Show, Generic, Binary)
36data DataFrame = DataFrame {
37 orders :: [AttributeOrder],
38 attributes :: Attributes,
39 tuples :: [DataFrameTuple]
40 } deriving (Show, Generic, Binary)
42data DataFrameTuple = DataFrameTuple Attributes (V.Vector Atom) deriving (Eq, Show, Generic, Binary)
44sortDataFrameBy :: [AttributeOrder] -> DataFrame -> Either RelationalError DataFrame
45sortDataFrameBy attrOrders frame = do
46 attrs <- mapM (\(AttributeOrder nam _) -> A.attributeForName nam (attributes frame)) attrOrders
47 mapM_ (\attr -> if not (isSortableAtomType (atomType attr)) then
48 Left (AttributeNotSortableError attr)
51 pure $ DataFrame attrOrders (attributes frame) (sortTuplesBy (compareTupleByAttributeOrders attrOrders) (tuples frame))
53sortTuplesBy :: (DataFrameTuple -> DataFrameTuple -> Ordering) -> [DataFrameTuple] -> [DataFrameTuple]
54sortTuplesBy = L.sortBy
56compareTupleByAttributeOrders :: [AttributeOrder] -> DataFrameTuple -> DataFrameTuple -> Ordering
57compareTupleByAttributeOrders attributeOrders tup1 tup2 =
58 let compare' (AttributeOrder attr order) = if order == DescendingOrder
59 then compareTupleByOneAttributeName attr tup2 tup1
60 else compareTupleByOneAttributeName attr tup1 tup2
61 res = map compare' attributeOrders in
62 fromMaybe EQ (L.find (/= EQ) res)
64compareTupleByOneAttributeName :: AttributeName -> DataFrameTuple -> DataFrameTuple -> Ordering
65compareTupleByOneAttributeName attr tuple1 tuple2 =
66 let eAtom1 = atomForAttributeName attr tuple1
67 eAtom2 = atomForAttributeName attr tuple2 in
69 Left err -> error (show err)
72 Left err -> error (show err)
73 Right atom2 -> compareAtoms atom1 atom2
75atomForAttributeName :: AttributeName -> DataFrameTuple -> Either RelationalError Atom
76atomForAttributeName attrName (DataFrameTuple tupAttrs tupVec) = case V.findIndex (\attr -> attributeName attr == attrName) tupAttrs of
77 Nothing -> Left (NoSuchAttributeNamesError (S.singleton attrName))
78 Just index -> case tupVec V.!? index of
79 Nothing -> Left (NoSuchAttributeNamesError (S.singleton attrName))
80 Just atom -> Right atom
82take' :: Integer -> DataFrame -> DataFrame
83take' n df = df { tuples = take (fromInteger n) (tuples df) }
85drop' :: Integer -> DataFrame -> DataFrame
86drop' n df = df { tuples = drop (fromInteger n) (tuples df) }
88toDataFrame :: Relation -> DataFrame
89toDataFrame (Relation attrs (RelationTupleSet tuples')) = DataFrame [] attrs (map (\(RelationTuple tupAttrs tupVec) -> DataFrameTuple tupAttrs tupVec) tuples')
91fromDataFrame :: DataFrame -> Either RelationalError Relation
92fromDataFrame df = R.mkRelation (attributes df) (RelationTupleSet tuples')
94 tuples' = map (\(DataFrameTuple attrs' tupVec) -> RelationTuple attrs' tupVec) (tuples df)
96showDataFrame :: DataFrame -> T.Text
97showDataFrame = renderTable . dataFrameAsTable
100dataFrameAsTable :: DataFrame -> Table
101dataFrameAsTable df = (header, body)
103 oAttrNames = orderedAttributeNames (attributes df)
104 oAttrs = orderedAttributes (attributes df)
105 header = "DF" : map dfPrettyAttribute oAttrs
106 dfPrettyAttribute attr = prettyAttribute attr <> case L.find (\(AttributeOrder nam _) -> nam == attributeName attr) (orders df) of
108 Just (AttributeOrder _ AscendingOrder) -> ascending
109 Just (AttributeOrder _ DescendingOrder) -> descending
110 body = snd (L.foldl' tupleFolder (1 :: Int,[]) (tuples df))
111 tupleFolder (count, acc) tuple = (count + 1,
112 acc ++ [T.pack (show count) : map (\attrName -> case atomForAttributeName attrName tuple of
114 Right atom -> showAtom 0 atom
117-- | A Relation can be converted to a DataFrame for sorting, limits, and offsets.
118data DataFrameExpr = DataFrameExpr {
119 convertExpr :: RelationalExpr,
120 orderExprs :: [AttributeOrderExpr],
121 offset :: Maybe Integer,
122 limit :: Maybe Integer
123 } deriving (Show, Binary, Generic)
125dataFrameAsHTML :: DataFrame -> T.Text
126-- web browsers don't display tables with empty cells or empty headers, so we have to insert some placeholders- it's not technically the same, but looks as expected in the browser
128 | length (tuples df) == 1 && L.null (attributes df) = style <>
130 "<tr><th></th></tr>" <>
131 "<tr><td></td></tr>" <>
132 tablefooter <> "</table>"
133 | L.null (tuples df) && L.null (attributes df) = style <>
135 "<tr><th></th></tr>" <>
138 | otherwise = style <>
140 attributesAsHTML (attributes df) (orders df) <>
141 tuplesAsHTML (tuples df) <>
145 cardinality = T.pack (show (length (tuples df)))
146 style = "<style>.pm36dataframe {empty-cells: show;} .pm36dataframe tbody td, .pm36relation th { border: 1px solid black;}</style>"
147 tablefooter = "<tfoot><tr><td colspan=\"100%\">" <> cardinality <> " tuples</td></tr></tfoot>"
148 tablestart = "<table class=\"pm36dataframe\"\">"
150tuplesAsHTML :: [DataFrameTuple] -> T.Text
151tuplesAsHTML = foldr folder ""
153 folder tuple acc = acc <> tupleAsHTML tuple
155tupleAssocs :: DataFrameTuple -> [(AttributeName, Atom)]
156tupleAssocs (DataFrameTuple attrVec tupVec) = V.toList $ V.map (first attributeName) (V.zip attrVec tupVec)
159tupleAsHTML :: DataFrameTuple -> T.Text
160tupleAsHTML tuple = "<tr>" <> T.concat (L.map tupleFrag (tupleAssocs tuple)) <> "</tr>"
162 tupleFrag tup = "<td>" <> atomAsHTML (snd tup) <> "</td>"
163 atomAsHTML (RelationAtom rel) = RelHTML.relationAsHTML rel
164 atomAsHTML atom = atomToText atom
166attributesAsHTML :: Attributes -> [AttributeOrder] -> T.Text
167attributesAsHTML attrs orders' = "<tr>" <> T.concat (map oneAttrHTML (V.toList attrs)) <> "</tr>"
169 oneAttrHTML attr = "<th>" <> prettyAttribute attr <> ordering (attributeName attr) <> "</th>"
170 ordering attrName = " " <> case L.find (\(AttributeOrder nam _) -> nam == attrName) orders' of
172 Just (AttributeOrder _ AscendingOrder) -> "(asc)"
173 Just (AttributeOrder _ DescendingOrder) -> "(desc)"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list