Permalink
Browse files

Head out N rows of a Frame. Write a Frame out as a tab delimited file.

  • Loading branch information...
1 parent ba6705c commit abe6055638b99b3e3c48960840ee1d4380d6342d @RayRacine committed Mar 4, 2013
Showing with 55 additions and 8 deletions.
  1. +55 −8 rpr/frame/frame-print.rkt
@@ -1,20 +1,23 @@
#lang typed/racket/base
(provide:
+ [ frame-write-tab (Frame Output-Port [#:heading Boolean] -> Void)]
[frame-head (case-> (Frame -> Void)
(Frame (Option Index) -> Void))])
(require
(only-in prelude/type/format
~a ~r)
+ (only-in "types.rkt"
+ Dim Dim-rows)
(only-in "indexed-series.rkt"
Label)
(only-in "series-description.rkt"
Series series-type)
(only-in "frame.rkt"
Frame new-frame frame-names
Column Columns column-heading column-series
- frame-cseries frame-explode
+ frame-cseries frame-explode frame-dim
FrameDescription FrameDescription-series
show-frame-description frame-description)
(only-in "integer-series.rkt"
@@ -37,7 +40,7 @@
(~a (symbol->string heading)
#:width WIDTH
#:align 'center))
-
+
(for ([col cols])
(let ((heading (column-heading col))
(series (column-series col)))
@@ -63,9 +66,12 @@
(: format-nseries (NSeries Index -> String))
(define (format-nseries nseries row)
- (~r (nseries-ref nseries row)
- #:precision '(= 4)
- #:min-width WIDTH))
+ (let ((n (nseries-ref nseries row)))
+ (if (rational? n)
+ (~r n
+ #:precision '(= 4)
+ #:min-width WIDTH)
+ "+nan.0")))
(: format-iseries (ISeries Index -> String))
(define (format-iseries iseries row)
@@ -88,8 +94,8 @@
(display (format-iseries a-series row)))
(else
(error 'frame-head "Unknown series types ~s"
- (series-type a-series))))))
- (display " ")
+ (series-type a-series)))))
+ (display " "))
(newline)))
(define default-head-rows 10)
@@ -104,5 +110,46 @@
;; (show-frame-description (frame-description frame))
(display-heading cols)
- (let ((count (if (not count) default-head-rows count)))
+ (let ((count (min (Dim-rows (frame-dim frame))
+ (if (not count) default-head-rows count))))
(display-frame-row series (in-range count))))
+
+(: frame-write-tab (Frame Output-Port [#:heading Boolean] -> Void))
+(define (frame-write-tab frame outp #:heading [heading #t])
+
+ (define: cols : Columns (frame-explode frame))
+ (define: headings : (Listof Label) (map column-heading cols))
+ (define: series : (Vectorof Series) (list->vector (map column-series cols)))
+ (define: row-num : Index (Dim-rows (frame-dim frame)))
+ (define: col-num : Index (vector-length series))
+
+ (: write-frame-row (Index -> Void))
+ (define (write-frame-row row)
+ (for ([col (in-range col-num)])
+ (let ((a-series (vector-ref series col)))
+ (cond
+ ((NSeries? a-series)
+ (let ((n (nseries-ref a-series row)))
+ (display n outp)))
+ ((CSeries? a-series)
+ (display (cseries-ref a-series row) outp))
+ ((ISeries? a-series)
+ (display (iseries-ref a-series row) outp))
+ (else
+ (error 'frame-head "Unknown series types ~s"
+ (series-type a-series)))))
+ (display "\t" outp)))
+
+ (: write-heading (Columns -> Void))
+ (define (write-heading cols)
+ (for ([col cols])
+ (display (car col) outp)
+ (display "\t" outp))
+ (displayln "" outp))
+
+ (when heading
+ (write-heading cols))
+
+ (for ([row (in-range row-num)])
+ (write-frame-row (assert row index?))
+ (displayln "" outp)))

0 comments on commit abe6055

Please sign in to comment.