simple macro coding question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

simple macro coding question

Cammer, Michael
Dear ImageJers,

I've been writing what I thought was an extremely simple function to throw into a bigger set of macros.  The goal is to extract numbers from a text window.  If I'm making a silly error, I'm sorry to bother you with it, but this just seems so simple and the results unpredictable.   I added the d2s() command when the results were first odd; the function works exactly the same with and without the d2s command.   Am I overlooking a dumb error in my coding or is there a problem with ImageJ?  Any help appreciated.  
Sincerely,
Michael

var rowwidth = 2;        // How many data points in each row.  
var threshold = 20.0;  // the minimum size object to count

macro "get numerical data from text files" {
  print(get_simple_sum_above_threshold());
} // macro

function get_simple_sum_above_threshold() {
  a = getInfo("window.contents");
  b = split(a);
  sumsizes = 0;
  for (i=0; i<b.length; i=i+rowwidth){
    if (d2s(b[i+1],3) > threshold) sumsizes = sumsizes + d2s(b[i+1],3);
    print (b[i], b[i+1], d2s(b[i+1],3), sumsizes);  // for debugging only
  } // for i
  return (sumsizes);
} // function



In this example I don't understand why line 3 isn't added to the sum and why line 6 is added to the sum. Why is line 7 not added to the sum but line 8 is?  Then line 10 is not added.

Raw data:
1 2.981
2 72.202
3 103.335
4 2.981
5 13.579
6 4.968
7 105.322
8 200.045
9 18.216
10 143.079
11 2.981

Result:
1 2.981 2.981 0
2 72.202 72.202 72.202
3 103.335 103.335 72.202
4 2.981 2.981 72.202
5 13.579 13.579 72.202
6 4.968 4.968 77.17
7 105.322 105.322 77.17
8 200.045 200.045 277.215
9 18.216 18.216 277.215
10 143.079 143.079 277.215
11 2.981 2.981 277.215


In the following example, lines 1, 2, 4, 8, 11 and 12 look ok but the rest are messed up.

Another example raw data:
1 340.143
2 3.974
3 5.630
4 10.598
5 3.974
6 4.637
7 7.949
8 26.827
9 164.938
10 11.923
11 22.522
12 2.981
               
Results:
1 340.143 340.143 340.143
2 3.974 3.974 344.1170
3 5.630 5.630 349.7470
4 10.598 10.598 349.7470
5 3.974 3.974 353.7210
6 4.637 4.637 358.3580
7 7.949 7.949 366.3070
8 26.827 26.827 393.1340
9 164.938 164.938 393.1340
10 11.923 11.923 393.1340
11 22.522 22.522 415.6560
12 2.981 2.981 415.6560


_________________________________________
Michael Cammer, Assistant Research Scientist
Skirball Institute of Biomolecular Medicine
Lab: (212) 263-3208  Cell: (914) 309-3270

------------------------------------------------------------
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain information that is proprietary, confidential, and exempt from disclosure under applicable law. Any unauthorized review, use, disclosure, or distribution is prohibited. If you have received this email in error please notify the sender by return email and delete the original message. Please note, the recipient should check this email and any attachments for the presence of viruses. The organization accepts no liability for any damage caused by any virus transmitted by this email.
=================================
Reply | Threaded
Open this post in threaded view
|

Re: simple macro coding question

Michael Schmid
Hi Michael,

d2s converts a number into a String (text). Thus, if you write
  if (d2s(b[i+1],3) > threshold)
it means that you do a comparison of Strings (texts).
When sorted alphabetically, e.g., 10.0 is earlier (lower) than 9.0.

I guess that you want to compare numbers, so you have to use
  parseFloat(string)
to convert the text to a number.

Michael
_____________________________________________________________________

On Sun, January 23, 2011 19:14, Cammer, Michael wrote:

> Dear ImageJers,
>
> I've been writing what I thought was an extremely simple function to throw
> into a bigger set of macros.  The goal is to extract numbers from a text
> window.  If I'm making a silly error, I'm sorry to bother you with it, but
> this just seems so simple and the results unpredictable.   I added the
> d2s() command when the results were first odd; the function works exactly
> the same with and without the d2s command.   Am I overlooking a dumb error
> in my coding or is there a problem with ImageJ?  Any help appreciated.
> Sincerely,
> Michael
>
> var rowwidth = 2;        // How many data points in each row.
> var threshold = 20.0;  // the minimum size object to count
>
> macro "get numerical data from text files" {
>   print(get_simple_sum_above_threshold());
> } // macro
>
> function get_simple_sum_above_threshold() {
>   a = getInfo("window.contents");
>   b = split(a);
>   sumsizes = 0;
>   for (i=0; i<b.length; i=i+rowwidth){
>     if (d2s(b[i+1],3) > threshold) sumsizes = sumsizes + d2s(b[i+1],3);
>     print (b[i], b[i+1], d2s(b[i+1],3), sumsizes);  // for debugging only
>   } // for i
>   return (sumsizes);
> } // function
>
>
>
> In this example I don't understand why line 3 isn't added to the sum and
> why line 6 is added to the sum. Why is line 7 not added to the sum but
> line 8 is?  Then line 10 is not added.
>
> Raw data:
> 1 2.981
> 2 72.202
> 3 103.335
> 4 2.981
> 5 13.579
> 6 4.968
> 7 105.322
> 8 200.045
> 9 18.216
> 10 143.079
> 11 2.981
>
> Result:
> 1 2.981 2.981 0
> 2 72.202 72.202 72.202
> 3 103.335 103.335 72.202
> 4 2.981 2.981 72.202
> 5 13.579 13.579 72.202
> 6 4.968 4.968 77.17
> 7 105.322 105.322 77.17
> 8 200.045 200.045 277.215
> 9 18.216 18.216 277.215
> 10 143.079 143.079 277.215
> 11 2.981 2.981 277.215
>
>
> In the following example, lines 1, 2, 4, 8, 11 and 12 look ok but the rest
> are messed up.
>
> Another example raw data:
> 1 340.143
> 2 3.974
> 3 5.630
> 4 10.598
> 5 3.974
> 6 4.637
> 7 7.949
> 8 26.827
> 9 164.938
> 10 11.923
> 11 22.522
> 12 2.981
>
> Results:
> 1 340.143 340.143 340.143
> 2 3.974 3.974 344.1170
> 3 5.630 5.630 349.7470
> 4 10.598 10.598 349.7470
> 5 3.974 3.974 353.7210
> 6 4.637 4.637 358.3580
> 7 7.949 7.949 366.3070
> 8 26.827 26.827 393.1340
> 9 164.938 164.938 393.1340
> 10 11.923 11.923 393.1340
> 11 22.522 22.522 415.6560
> 12 2.981 2.981 415.6560
>
>
> _________________________________________
> Michael Cammer, Assistant Research Scientist
> Skirball Institute of Biomolecular Medicine
> Lab: (212) 263-3208  Cell: (914) 309-3270
>
> ------------------------------------------------------------
> This email message, including any attachments, is for the sole use of the
> intended recipient(s) and may contain information that is proprietary,
> confidential, and exempt from disclosure under applicable law. Any
> unauthorized review, use, disclosure, or distribution is prohibited. If
> you have received this email in error please notify the sender by return
> email and delete the original message. Please note, the recipient should
> check this email and any attachments for the presence of viruses. The
> organization accepts no liability for any damage caused by any virus
> transmitted by this email.
> =================================
>
Reply | Threaded
Open this post in threaded view
|

Re: simple macro coding question

Cammer, Michael
Thank you!

There was also a missing comma that the interpreter didn't catch.

Regardless, the parseFloat() command works:

var rowwidth = 2;        // How many data points in each row.  
var threshold = 20.0;  // the minimum size object to count
macro "get numerical data from text files" {
  print(get_simple_sum_above_threshold());
} // macro
function get_simple_sum_above_threshold() {
  a = getInfo("window.contents");
  b = split(a);
  sumsizes = 0;
  for (i=0; i<b.length; i=i+rowwidth){
    if (parseFloat(b[i+1]) > threshold) sumsizes = sumsizes + b[i+1];
    print (b[i], b[i+1], sumsizes);  // for debugging only
  } // for i
  return (sumsizes);
} // function


_________________________________________
Michael Cammer, Assistant Research Scientist
Skirball Institute of Biomolecular Medicine
Lab: (212) 263-3208  Cell: (914) 309-3270

________________________________________
From: ImageJ Interest Group [[hidden email]] On Behalf Of Michael Schmid [[hidden email]]
Sent: Sunday, January 23, 2011 3:02 PM
To: [hidden email]
Subject: Re: simple macro coding question

Hi Michael,

d2s converts a number into a String (text). Thus, if you write
  if (d2s(b[i+1],3) > threshold)
it means that you do a comparison of Strings (texts).
When sorted alphabetically, e.g., 10.0 is earlier (lower) than 9.0.

I guess that you want to compare numbers, so you have to use
  parseFloat(string)
to convert the text to a number.

Michael
_____________________________________________________________________

On Sun, January 23, 2011 19:14, Cammer, Michael wrote:

> Dear ImageJers,
>
> I've been writing what I thought was an extremely simple function to throw
> into a bigger set of macros.  The goal is to extract numbers from a text
> window.  If I'm making a silly error, I'm sorry to bother you with it, but
> this just seems so simple and the results unpredictable.   I added the
> d2s() command when the results were first odd; the function works exactly
> the same with and without the d2s command.   Am I overlooking a dumb error
> in my coding or is there a problem with ImageJ?  Any help appreciated.
> Sincerely,
> Michael
>
> var rowwidth = 2;        // How many data points in each row.
> var threshold = 20.0;  // the minimum size object to count
>
> macro "get numerical data from text files" {
>   print(get_simple_sum_above_threshold());
> } // macro
>
> function get_simple_sum_above_threshold() {
>   a = getInfo("window.contents");
>   b = split(a);
>   sumsizes = 0;
>   for (i=0; i<b.length; i=i+rowwidth){
>     if (d2s(b[i+1],3) > threshold) sumsizes = sumsizes + d2s(b[i+1],3);
>     print (b[i], b[i+1], d2s(b[i+1],3), sumsizes);  // for debugging only
>   } // for i
>   return (sumsizes);
> } // function
>
>
>
> In this example I don't understand why line 3 isn't added to the sum and
> why line 6 is added to the sum. Why is line 7 not added to the sum but
> line 8 is?  Then line 10 is not added.
>
> Raw data:
> 1     2.981
> 2     72.202
> 3     103.335
> 4     2.981
> 5     13.579
> 6     4.968
> 7     105.322
> 8     200.045
> 9     18.216
> 10    143.079
> 11    2.981
>
> Result:
> 1 2.981 2.981 0
> 2 72.202 72.202 72.202
> 3 103.335 103.335 72.202
> 4 2.981 2.981 72.202
> 5 13.579 13.579 72.202
> 6 4.968 4.968 77.17
> 7 105.322 105.322 77.17
> 8 200.045 200.045 277.215
> 9 18.216 18.216 277.215
> 10 143.079 143.079 277.215
> 11 2.981 2.981 277.215
>
>
> In the following example, lines 1, 2, 4, 8, 11 and 12 look ok but the rest
> are messed up.
>
> Another example raw data:
> 1     340.143
> 2     3.974
> 3     5.630
> 4     10.598
> 5     3.974
> 6     4.637
> 7     7.949
> 8     26.827
> 9     164.938
> 10    11.923
> 11    22.522
> 12    2.981
>
> Results:
> 1 340.143 340.143 340.143
> 2 3.974 3.974 344.1170
> 3 5.630 5.630 349.7470
> 4 10.598 10.598 349.7470
> 5 3.974 3.974 353.7210
> 6 4.637 4.637 358.3580
> 7 7.949 7.949 366.3070
> 8 26.827 26.827 393.1340
> 9 164.938 164.938 393.1340
> 10 11.923 11.923 393.1340
> 11 22.522 22.522 415.6560
> 12 2.981 2.981 415.6560
>
>
> _________________________________________
> Michael Cammer, Assistant Research Scientist
> Skirball Institute of Biomolecular Medicine
> Lab: (212) 263-3208  Cell: (914) 309-3270
>
> ------------------------------------------------------------
> This email message, including any attachments, is for the sole use of the
> intended recipient(s) and may contain information that is proprietary,
> confidential, and exempt from disclosure under applicable law. Any
> unauthorized review, use, disclosure, or distribution is prohibited. If
> you have received this email in error please notify the sender by return
> email and delete the original message. Please note, the recipient should
> check this email and any attachments for the presence of viruses. The
> organization accepts no liability for any damage caused by any virus
> transmitted by this email.
> =================================
>

------------------------------------------------------------
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain information that is proprietary, confidential, and exempt from disclosure under applicable law. Any unauthorized review, use, disclosure, or distribution is prohibited. If you have received this email in error please notify the sender by return email and delete the original message. Please note, the recipient should check this email and any attachments for the presence of viruses. The organization accepts no liability for any damage caused by any virus transmitted by this email.
=================================