public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* imagej - a clojure example
@ 2015-09-18 23:38 David Pirotte
  2015-09-24 18:31 ` David Pirotte
  0 siblings, 1 reply; 5+ messages in thread
From: David Pirotte @ 2015-09-18 23:38 UTC (permalink / raw)
  To: kawa


[-- Attachment #1.1: Type: text/plain, Size: 1142 bytes --]

Hello,

1]

For info, here is the tutorial I used to start to try to use imagej from clojure:

	http://imagej.net/Clojure_Scripting


	[ Note that section 1.1 is obsolate, if you read, start at section 1.2
	[ for a [terribly basic] repl, I do:
	[
	[	java -cp clojure.jar:ij-core.jar:cljs clojure.main
	[
	[ 	where clj is a symlink to the path of .clj files

Any similar document, even for another library? How to do these things in kawa
would be fantastic for a zero/null java guy like me :), see subsections:

	- You can specify imports in Clojure in a few ways
	- Calling methods and variables on a java object
	- ...
	- Calling static fields and methods: namespace syntax
	- ...
	- Creating objects: invoking constructors

2]

	attached a very first module I should port to kawa, for info as well

	[  I do not ask to do my job here, but any hint(s) to get me me of the ground
	[  more then welcome

	java -cp clojure.jar:ij-core.jar:cljs clojure.main
	Clojure 1.6.0
	user=> (import '(ij IJ)
        		'(ij ImagePlus))
	user=> (def bf (ij-open "bf.png"))
	user=> (ij-show bf)
	...

Cheers,
David

[-- Attachment #1.2: ij_support.clj --]
[-- Type: application/octet-stream, Size: 5293 bytes --]

;; -*- mode: clojure; coding: utf-8 -*-

;;;;
;;;; Copyright (C) 2015
;;;; David Pirotte <david at altosw dot be>

;;;; Get-particles is part of a set of image processing scripts.

;;;; Get-particles is free software: you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License as
;;;; published by the Free Software Foundation, either version 3 of
;;;; the License, or (at your option) any later version.

;;;; It is distributed in the hope that it will be useful, but WITHOUT
;;;; ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; General Public License for more details.

;;;; You should have received a copy of the GNU General Public License
;;;; along with this s/w.  If not, see <http://www.gnu.org/licenses/>.
;;;;

;;; Commentary:

;;   <- stands for ->
;; bb		bounding-box
;; iplus	ImagePlus
;; iproc	ImageProcessor
;; istack 	ImageStack
;; icalc	ImageCalculator

;;; Code:


(ns ij-support
  "imagej support"
  (:import (ij IJ)
           (ij ImagePlus)
           (ij ImageStack)
           (ij.measure Measurements)
           (ij.measure ResultsTable)
           (ij.gui Roi)
           (ij.process ImageProcessor)
           (ij.plugin ImageCalculator)
           (ij.plugin.frame RoiManager)
           (ij.plugin.filter EDM)
           (ij.plugin.filter Analyzer)
           (ij.plugin.filter PlugInFilter)
           (java.io File)))


(defn ij-open [filename]
  (IJ/openImage filename))

(defn ij-save [iplus filename]
  (IJ/save iplus filename))

(defn ij-show [iplus]
  (.show iplus))

(defn ij-set-scale-pixel [iplus]
  (IJ/run iplus "Set Scale..." "distance=0 known=0 pixel=1 unit=pixel"))

(defn ij-clear-results [ & [rt]]
  (let [rtable (if rt rt (ResultsTable/getResultsTable))]
    ;; (ResultsTable/getResultsWindow)  <- these
    ;; (.show "Results") 		<- 2 lines do not work, will write our own
    (.reset rtable)))

(defn ij-get-bb-values [ & [rt]]
  (let [rtable (if rt rt (ResultsTable/getResultsTable))
        nb-rows (.getCounter rtable)]
    [nb-rows
     (doall
      (for [i (range nb-rows)]
        [(.getValue rtable "BX" i)
         (.getValue rtable "BY" i)
         (.getValue rtable "Width" i)
         (.getValue rtable "Height" i)]))]))

(defn ij-8bit [iplus]
  (IJ/run iplus "8-bit" ""))

(defn ij-16bit [iplus]
  (IJ/run iplus "8-bit" ""))

(defn ij-make-binary [iplus]
  (IJ/run iplus "Make Binary" ""))

(defn ij-set-threshold [iplus inf sup & [mode]]
  (if mode
    (IJ/setThreshold iplus inf sup mode)
    (IJ/setThreshold iplus inf sup ""))
  (IJ/run iplus "Convert to Mask" ""))

(defn ij-invert-lut [iplus]
  (IJ/run iplus "Invert LUT" ""))

(defn ij-scrap-inf [iplus min-area]
  (let [analyze (str "size=" min-area "-Infinity show=Masks in_situ")]
    (IJ/run iplus "Set Measurements..." "area redirect=None decimal=3")
    (IJ/run iplus "Analyze Particles..." analyze)))

(defn ij-get-bounding-boxes [iplus]
  (IJ/run iplus "Set Measurements..." "bounding redirect=None decimal=3")
  (IJ/run iplus "Analyze Particles..." "show=Nothing clear")
  (ij-get-bb-values))

(defn ij-clean-bounding-box [iplus]
  (let [b (bean iplus)
        i-width (* 1.0 (:width b))
        i-height (* 1.0 (:height b))]
    (ij-set-threshold iplus 1 255 "Red")
    ;; add stands for add to overlay [see overlays in imagej's doc]
    (IJ/run iplus "Set Measurements..." "bounding add redirect=None decimal=3")
    (IJ/run iplus "Analyze Particles..." "show=[Count Masks] clear in_situ")
    (let [[nb-objs results] (ij-get-bb-values)]
      (loop [i 0]
        (if (< i nb-objs)
          (let [[x y width height] (nth results i)]
            ;; (println ["i: " i x y width height])
            (if (and (= x 0.0)
                     (= y 0.0)
                     (= width i-width)
                     (= height i-height))
              (let [pos (inc i)]
                (ij-set-threshold iplus (- pos 0.1) (+ pos 0.1)))
              (recur (inc i)))))))))

(defn ij-get-max-area [iplus]
  (IJ/run iplus "Set Measurements..." "area redirect=None decimal=3")
  (IJ/run iplus "Analyze Particles..." "show=Nothing clear")
  ;; (IJ/run iplus "Invert LUT" "")
  (let [rt (ResultsTable/getResultsTable)
        nb-objs (.getCounter rt)]
    (reduce max 0
            (doall (for [i (range nb-objs)] (.getValue rt "Area" i))))))

(defn ij-set-roi [iplus x y width height]
  (let [iproc (.getProcessor iplus)
        roi (new Roi x y width height)]
    (.setRoi iproc roi)))

(defn ij-crop [iplus x y width height]
  (let [iproc (.getProcessor iplus)]
    (ij-set-roi iplus x y width height)
    (new ImagePlus "" (.crop iproc))))

(defn ij-duplicate [iplus title]
  (new ImagePlus title (.duplicate (.getProcessor iplus))))

(defn ij-and [iplus1 iplus2]
  (let [icalc (new ImageCalculator)]
    (.run icalc "AND create" iplus1 iplus2)))


;;;
;;; Testing
;;;

(comment
  (import '(ij IJ)
          '(ij ImagePlus)
          '(ij.measure Measurements)
          '(ij.measure ResultsTable)
          '(ij.gui Roi)
          '(ij.process ImageProcessor)
          '(ij.plugin ImageCalculator)
          '(ij.plugin.frame RoiManager)
          '(ij.plugin.filter EDM)
          '(ij.plugin.filter Analyzer)
          '(ij.plugin.filter PlugInFilter)))

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: imagej - a clojure example
  2015-09-18 23:38 imagej - a clojure example David Pirotte
@ 2015-09-24 18:31 ` David Pirotte
  2015-09-24 21:02   ` Per Bothner
  0 siblings, 1 reply; 5+ messages in thread
From: David Pirotte @ 2015-09-24 18:31 UTC (permalink / raw)
  To: kawa

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

Heya,

david@capac:~/lpdi/projects/kawa/jars 61 $ ls -ls
total 2076
1944 -rw-rw-r-- 1 david alto 1986620 Sep 24 15:11 ij-1.49v.jar
   0 lrwxrwxrwx 1 david alto      12 Sep 24 15:11 ij.jar -> ij-1.49v.jar
 132 -rw-rw-r-- 1 david alto  132846 Sep 24 15:12 mpicbg_-1.0.1.jar
   0 lrwxrwxrwx 1 david alto      17 Sep 24 15:13 mpicbg.jar -> mpicbg_-1.0.1.jar

david@capac:~/lpdi/projects/kawa/jars 62 $ kawa
#|kawa:1|# (import (ij IJ))
/dev/stdin:1:9: unknown library (ij IJ)
#|kawa:2|# 


^^ Of course it is a stupid quizz for someone who knows java, I don't and I don't
want to.  What is the scheme procedure and/or variable, like %load-path in guile,
that I need to set! so that kawa knows where to look for when I do

	(import (ij IJ))		[ if that is correct, I don't know
					[ if not please correct me

Also, How do I introspect a module?  Like once yu tll me how to make these
ij-1.49v.jar, mpicbg_-1.0.1.jar, .. known to java, how can I list the public
interface of these modules, its classes, methods ...

Many thanks,
David

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: imagej - a clojure example
  2015-09-24 18:31 ` David Pirotte
@ 2015-09-24 21:02   ` Per Bothner
  2015-09-24 23:52     ` David Pirotte
  0 siblings, 1 reply; 5+ messages in thread
From: Per Bothner @ 2015-09-24 21:02 UTC (permalink / raw)
  To: David Pirotte, kawa



On 09/24/2015 11:30 AM, David Pirotte wrote:
> Heya,
>
> david@capac:~/lpdi/projects/kawa/jars 61 $ ls -ls
> total 2076
> 1944 -rw-rw-r-- 1 david alto 1986620 Sep 24 15:11 ij-1.49v.jar
>     0 lrwxrwxrwx 1 david alto      12 Sep 24 15:11 ij.jar -> ij-1.49v.jar
>   132 -rw-rw-r-- 1 david alto  132846 Sep 24 15:12 mpicbg_-1.0.1.jar
>     0 lrwxrwxrwx 1 david alto      17 Sep 24 15:13 mpicbg.jar -> mpicbg_-1.0.1.jar
>
> david@capac:~/lpdi/projects/kawa/jars 62 $ kawa
> #|kawa:1|# (import (ij IJ))
> /dev/stdin:1:9: unknown library (ij IJ)
> #|kawa:2|#

Is there an actual class ij.IJ in the classpath?  Otherwise what you're doing
won't work.

Try typing ij.IJ directly at the REPL and you should see if it exists.  For example:

#|kawa:5|# java.util.Map
interface java.util.Map
#|kawa:6|#

> ^^ Of course it is a stupid quizz for someone who knows java, I don't and I don't
> want to.  What is the scheme procedure and/or variable, like %load-path in guile,
> that I need to set! so that kawa knows where to look for when I do
>
> 	(import (ij IJ))		[ if that is correct, I don't know
> 					[ if not please correct me

You need set the classpath before you start up the Java VM - typically
on the java command line or a CLASSPATH environment variable.
For example:
$ CLASSPATH=.:ij.jar kawa

or:

$ java -cp .:ij.jar kawa.repl

It would be possible for Kawa to use a ClassLoader that can change its
search path on the fly.  I don't think we'd want to do that by default,
because we want to keep Kawa "close to the [JVM] metal".  With a strong use
case we could add it as an optional feature.

> Also, How do I introspect a module?  Like once yu tll me how to make these
> ij-1.49v.jar, mpicbg_-1.0.1.jar, .. known to java, how can I list the public
> interface of these modules, its classes, methods ...

Introspection is a complex feature.  Kawa doesn't really have an introspection
API - you can use the Java reflection API or the model API, but these are nontrivial;
certainly more than a quick reply here.  What is your specific use-case?

If you're just trying the learn what's available in a libray, the usual
method is to look at the javadoc-generated API documentation.  For example
for imagej look here:

http://javadoc.imagej.net/Fiji/overview-summary.html
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: imagej - a clojure example
  2015-09-24 21:02   ` Per Bothner
@ 2015-09-24 23:52     ` David Pirotte
  2015-09-25  1:24       ` David Pirotte
  0 siblings, 1 reply; 5+ messages in thread
From: David Pirotte @ 2015-09-24 23:52 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

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


> > 	(import (ij IJ))		[ if that is correct, I don't know
> > 					[ if not please correct me

> You need set the classpath before you start up the Java VM - typically
> on the java command line or a CLASSPATH environment variable.
> For example:
> $ CLASSPATH=.:ij.jar kawa

Thanks, someone kindly answered on #kawa.  I will mail the example I got so other
may benefit:  I did not know a module was auto define based on the name of the
file... and other things jhope explained me on #irc

> > Also, How do I introspect a module?  Like once yu tll me how to make these
> > ij-1.49v.jar, mpicbg_-1.0.1.jar, .. known to java, how can I list the public
> > interface of these modules, its classes, methods ...

> Introspection is a complex feature.  Kawa doesn't really have an introspection
> API - you can use the Java reflection API or the model API, but these are
> nontrivial; certainly more than a quick reply here.  What is your specific
> use-case?

Everything I can do when using clos I guess.

> If you're just trying the learn what's available in a libray, the usual
> method is to look at the javadoc-generated API documentation.  For example
> for imagej look here:

Sure, but how do you debug? 

	Maybe the second jar must be in a specific subdirectory, if that so, how
	would I know?

Cheers,
David

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: imagej - a clojure example
  2015-09-24 23:52     ` David Pirotte
@ 2015-09-25  1:24       ` David Pirotte
  0 siblings, 0 replies; 5+ messages in thread
From: David Pirotte @ 2015-09-25  1:24 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

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


> > > Also, How do I introspect a module?  Like once yu tll me how to make these
> > > ij-1.49v.jar, mpicbg_-1.0.1.jar, .. known to java, how can I list the public
> > > interface of these modules, its classes, methods ...
> 
> > Introspection is a complex feature.  Kawa doesn't really have an introspection
> > API - you can use the Java reflection API or the model API, but these are
> > nontrivial; certainly more than a quick reply here.  What is your specific
> > use-case?
> 
> Everything I can do when using clos I guess.

I should have said clos _and_ the module system itself here: I did ask because in
guile we have a substantial amount of procedures related to introspection of modules
themselves, not related to goops [which is itself a separate module, guile's
implementation of clos, not full but almost]:

	 I could ask to get the list of public symbols [procedures, variables,
	 getters, setters, accessors and methods defined within a module

This said, both jhope and Per pointed me to the command line java commands which
lists the classes of a jar... but that did not help: I still don't know how to debug
this basic situation though, 2 jars, they work when used through Fiji [the imagej
interface], but not using [yet] kawa, neither clojure:  I guess there is something
wrong in the design of imagej, by the way, not in kawa, but I hope java/kawa 
experts can help.

I will write another email, time for a new thread, couple of things were solved
today thnks to jhope on irc, but the major problem, which I faced in clojure as
well, unfortunately also occurs when using kawa. But I still have hope to get
through this using kawa.

Cheers,
David

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-09-25  1:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-18 23:38 imagej - a clojure example David Pirotte
2015-09-24 18:31 ` David Pirotte
2015-09-24 21:02   ` Per Bothner
2015-09-24 23:52     ` David Pirotte
2015-09-25  1:24       ` David Pirotte

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).