emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Mario Frasca <mario@anche.no>
To: emacs-orgmode@gnu.org
Cc: schulte.eric@gmail.com
Subject: [PATCH] [FEATURE] Re: `with` as a list.
Date: Thu, 28 May 2020 08:34:57 -0500	[thread overview]
Message-ID: <bbfa1dc8-54fb-a916-bcd8-6564e8d812d8@anche.no> (raw)
In-Reply-To: <656b815f-9e38-e68c-b7a3-091f6f3d36bf@anche.no>

[-- Attachment #1: Type: text/plain, Size: 1970 bytes --]

I have added a couple of unit tests to the suite, describing the two 
functions I added.  I have no unexpectedly failing tests now.

I'm explicitly cc-ing Eric Schulte because he's in the header for 
org-plot.el, and —missing the unit tests for that source— I hope he can 
assist me not breaking what he wrote.  … I guess I can write also test 
cases for what already exists, but I will very likely need assistance.

best regards,
Mario Frasca

On 22/05/2020 11:07, Mario Frasca wrote:
> good day to you all
>
> now and then I use emacs to make graphs.  now recently I was plotting 
> point data, and a running average "fit", so I wanted to have points, 
> and lines, which I know it's possible in `gnuplot` but now how do I do 
> that from org-plot …
>
> I wrote a small patch for org-plot.el, I'm not a Lisp programmer so 
> I'm sure the patch looks terrible, but it does allow me to do this:
>
> #+PLOT: ind:1 deps:(3 6 4 7) with:(points lines points lines)
>
> it's two additions:
>
> 1. it lets me specify the order in which the dependent columns should 
> be considered.
>
> 2. it lets me specify a different `with` for each column, in the same 
> order.
>
> if you leave the `with` away, you get "lines" for all columns.
>
> if you specify only one `with` value, that value is used for all columns.
>
> if you specify more `deps` than `with`, the ones not specified will 
> get "lines".
>
> if you specify more `with` than `deps`, they are ignored.
>
> I ran the tests, and I get two failing ones, quite unrelated according 
> to me:
>
> 2 unexpected results:
>    FAILED  ob-exp/evaluate-all-executables-in-order
>    FAILED  ob-exp/export-call-line-information
>
> I have not defined test cases for the new behaviour, I'm willing to do 
> that (learning the way this test environment works), but I don't find 
> the location of the other tests related to the area of the program, 
> which I'm tweaking.
>
> best regards all,
>
> Mario Frasca
>

[-- Attachment #2: plot-with-list.patch --]
[-- Type: text/x-patch, Size: 5263 bytes --]

diff --git a/lisp/org-plot.el b/lisp/org-plot.el
index a23195d2a..524615d98 100644
--- a/lisp/org-plot.el
+++ b/lisp/org-plot.el
@@ -179,6 +179,28 @@ and dependent variables."
 	  (setf back-edge "") (setf front-edge ""))))
     row-vals))
 
+(defun org-plot/zip-deps-with (num-cols ind deps with)
+  "describe each column to be plotted as (col . with)"
+  ;; make 'deps explicit
+  (unless deps
+    (setf deps (let (r)
+		 (dotimes (i num-cols r)
+		   (unless (eq num-cols (+ ind i))
+		     (setq r (cons (- num-cols i) r)))))))
+  ;; make sure 'with matches 'deps
+  (unless with
+    (setf with "lines"))
+  (unless (listp with)
+    (setf with (make-list (length deps) with)))
+  ;; invoke zipping function on converted data
+  (org-plot/zip deps with))
+
+(defun org-plot/zip (xs ys)
+  (unless
+      (null xs)
+    (cons (cons (car xs) (or (car ys) "lines"))
+	  (org-plot/zip (cdr xs) (cdr ys)))))
+
 (defun org-plot/gnuplot-script (data-file num-cols params &optional preface)
   "Write a gnuplot script to DATA-FILE respecting the options set in PARAMS.
 NUM-COLS controls the number of columns plotted in a 2-d plot.
@@ -240,22 +262,24 @@ manner suitable for prepending to a user-specified script."
 			       "%Y-%m-%d-%H:%M:%S") "\"")))
     (unless preface
       (pcase type			; plot command
-	(`2d (dotimes (col num-cols)
-	       (unless (and (eq type '2d)
-			    (or (and ind (equal (1+ col) ind))
-				(and deps (not (member (1+ col) deps)))))
-		 (setf plot-lines
-		       (cons
-			(format plot-str data-file
-				(or (and ind (> ind 0)
-					 (not text-ind)
-					 (format "%d:" ind)) "")
-				(1+ col)
-				(if text-ind (format ":xticlabel(%d)" ind) "")
-				with
-				(or (nth col col-labels)
-				    (format "%d" (1+ col))))
-			plot-lines)))))
+	(`2d (dolist
+		 (col-with
+		  (org-plot/zip-deps-with num-cols ind deps with))
+	       (setf plot-lines
+		     (cons
+		      (format plot-str data-file
+			      (or (and ind (> ind 0)
+				       (not text-ind)
+				       (format "%d:" ind)) "")
+			      (car col-with)
+			      (if text-ind (format ":xticlabel(%d)" ind) "")
+			      (cdr col-with)
+			      (apply (lambda (x)
+				       (if (= 0 (length x))
+					   (format "%d" (car col-with))
+					 x))
+				     (list (nth (1- (car col-with)) col-labels))))
+		      plot-lines))))
 	(`3d
 	 (setq plot-lines (list (format "'%s' matrix with %s title ''"
 					data-file with))))
@@ -310,7 +334,8 @@ line directly before or after the table."
 				table data-file params)))
 		 (when y-labels (plist-put params :ylabels y-labels)))))
       ;; Check for timestamp ind column.
-      (let ((ind (1- (plist-get params :ind))))
+      (let ((ind (1- (plist-get params :ind)))
+	    (with (plist-get params :with)))
 	(when (and (>= ind 0) (eq '2d (plist-get params :plot-type)))
 	  (if (= (length
 		  (delq 0 (mapcar
@@ -320,7 +345,7 @@ line directly before or after the table."
 		 0)
 	      (plist-put params :timeind t)
 	    ;; Check for text ind column.
-	    (if (or (string= (plist-get params :with) "hist")
+	    (if (or (and (stringp with) (string= with "hist"))
 		    (> (length
 			(delq 0 (mapcar
 				 (lambda (el)
diff --git a/testing/lisp/test-org-plot.el b/testing/lisp/test-org-plot.el
new file mode 100644
index 000000000..4ed8c15b4
--- /dev/null
+++ b/testing/lisp/test-org-plot.el
@@ -0,0 +1,62 @@
+;;; test-org-plot.el --- Tests for org-plot.el
+
+;; Copyright (C) 2020  Mario Frasca
+
+;; Author: Mario Frasca <mario at anche dot no>
+
+;; Released under the GNU General Public License version 3
+;; see: http://www.gnu.org/licenses/gpl-3.0.html
+
+;;;; Comments
+
+
+\f
+;;; Code:
+
+(require 'org-plot)
+
+(ert-deftest test-org-plot/zip ()
+  "Test `org-plot/zip' specifications."
+  ;; zipping two equal length lists
+  (should
+   (equal '((1 . "a") (2 . "b") (3 . "c"))
+	  (org-plot/zip '(1 2 3) '("a" "b" "c"))))
+  ;; if second is shorter, fill in with "lines"
+  (should
+   (equal '((1 . "a") (2 . "b") (3 . "lines"))
+	  (org-plot/zip '(1 2 3) '("a" "b"))))
+  ;; if first is shorter, stop there
+  (should
+   (equal '((1 . "a") (2 . "b"))
+	  (org-plot/zip '(1 2) '("a" "b" "c")))))
+
+(ert-deftest test-org-plot/zip-deps-with ()
+  "Test `org-plot/zip-deps-with' specifications."
+  ;; no deps, no with. defaults to all except ind, and "lines"
+  (should
+   (equal (org-plot/zip-deps-with 3 1 nil nil)
+	  '((2 . "lines") (3 . "lines"))))
+  ;; no deps, single with. defaults to all except ind, and repeated with
+  (should
+   (equal (org-plot/zip-deps-with 3 1 nil "hist")
+	  '((2 . "hist") (3 . "hist"))))
+  ;; no deps, explicit with
+  (should
+   (equal (org-plot/zip-deps-with 3 1 nil '("points" "hist"))
+	  '((2 . "points") (3 . "hist"))))
+  ;; explicit with, same length as deps
+  (should
+   (equal (org-plot/zip-deps-with 5 1 '(2 4) '("points" "hist"))
+	  '((2 . "points") (4 . "hist"))))
+  ;; same as above, but different order
+  (should
+   (equal (org-plot/zip-deps-with 5 1 '(4 2) '("points" "hist"))
+	  '((4 . "points") (2 . "hist"))))
+  ;; fills in with "lines"
+  (should
+   (equal (org-plot/zip-deps-with 5 1 '(4 2 3) '("points"))
+	  '((4 . "points") (2 . "lines") (3 . "lines")))))
+  
+     
+(provide 'test-org-plot)
+;;; test-org-plot.el end here

  reply	other threads:[~2020-05-28 13:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-22 16:07 `with` as a list Mario Frasca
2020-05-28 13:34 ` Mario Frasca [this message]
2020-05-30  6:22 ` Kyle Meyer
2020-05-30 14:23   ` Mario Frasca
2020-05-30 14:38     ` Mario Frasca
2020-05-30 20:23     ` Kyle Meyer
2020-05-30 21:29       ` Mario Frasca
2020-05-31 20:18         ` Mario Frasca
2020-06-01  0:19           ` Kyle Meyer
2020-06-01  1:47             ` Mario Frasca
2020-06-01  2:31               ` Kyle Meyer
2020-06-03 15:09                 ` Mario Frasca
2020-06-03 15:13                   ` Bastien
2020-06-03 15:18                     ` Mario Frasca
2020-06-03 15:29                       ` Bastien
2020-06-03 17:08                         ` Mario Frasca
2020-06-03 22:15                         ` Mario Frasca
2020-06-03 15:25                     ` Mario Frasca
2020-06-03 15:30                       ` Bastien
2020-05-30 16:01   ` Mario Frasca
2020-05-30 20:25     ` Kyle Meyer
2020-05-30 21:36       ` Mario Frasca
2020-05-31  0:36         ` Kyle Meyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bbfa1dc8-54fb-a916-bcd8-6564e8d812d8@anche.no \
    --to=mario@anche.no \
    --cc=emacs-orgmode@gnu.org \
    --cc=schulte.eric@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).