Combine channels in ImageJ Jython? - python

I have two channels from an image stack which I've split like so:
red_c, green_c = ChannelSplitter.split(imp)
And now I want to combine them horizontally:
combined_img = StackCombiner.combineHorizontally(green_c, red_c)
This throws an error that says 3 arguments were expected, but only 2 provided. But from the documentation it says combineHorizontally(ImageStack stack1, ImageStack stack2)
Why is this not working?
EDIT: solved it. Turns out the correct way of writing it is
combined = StackCombiner().combineHorizontally(grn_stack, red_stack)
Why this needs an extra () but ChannelSplitter doesn't is a mystery to me. They're both imported from ij.plugin. Can somebody shed light on this?

solved it.
Glad that you found it. For the future, questions like this one are still a good fit for the ImageJ forum (where you seem to have an account as well), particularly when you're asking about specifics of the ImageJ API.
Why this needs an extra () but ChannelSplitter doesn't is a mystery to me.
ImageJ is a Java application, and in your Jython script you're actually calling the Java API of StackCombiner. The call
StackCombiner.combineHorizontally(green_c, red_c)
would work if combineHorizontally was a static method of StackCombiner, but as it isn't, it needs a new StackCombiner object being instantiated first.
In Java, you'd have to write:
new StackCombiner().combineHorizontally(a,b)
In Python, you don't need a new keyword, but you still need to use the constructor:
StackCombiner().combineHorizontally(a,b)
In contrast, the ChannelSplitter.split(ImagePlus) method is static, so you can use it without instantiating an object.

Related

Python, Returning object class names by a function

I'm new to Python and this is my first question here. Hope any of you guys will be able to help me out.
I'm trying to call values inside an object from an external program. The object that I'm trying to access is given in a class (as i uderstand it), and the name of the class may change according to X, see below:
External programs object and class information
I want to be able to call information from Phase_6 in this case, however it could be Phase_12 in another case. I was considering making a function where i could have the _'Number' as an input. But I can't seem to find any information of how to do such.
I was thinking of something like using +str(X), as I do when plotting. But as it is probably not a string, it doesn't work out.
My proposed code
Ive read that bpy in Blender may be able to replace the name of the class that i want to return, however I'm not sure if it'll work, and I dont want to switch editor :)
Hope you guys can help me out,
Joachim
Found the answer, one could use getattr.
x = 6
result = getattr(g_o, 'phase_'+str(x)).Info.SumMsf.value
Thanks anyway - And I'll work on the pictures
Joachim

How can I call a python function from an advanced scripting voice command in Dragon NaturallySpeaking?

How can I call a python function from an advanced scripting voice command in Dragon NaturallySpeaking?
I don't want to use a third-party application such as dragonfly or NatLink (paper).
So, one way is to compile it. You can put a bunch of functions that do different things all into the same program and pass along appropriate arguments to select the function you want, and pass the parameters along. Returning the result can be tricky, though, but I usually use the Clipboard (so copy the py output to clip and read from clip in Dragon). Multi-word params need to have spaces escaped (%20) and process it inside your py.
Something like this:
ShellExecute "path\program.exe myFunc myPar1, my%20Par%202", 6 ' 6 runs minimized
Wait 1
myVar = Clipboard
Hth,
Warning: This is not an answer. I am not a programmer. I don't know any Python and have no way of testing it.
This is just a suggestion on how to solve this problem. I don't know where else to put this. I'd put it in a comment, but it allows no screenshots. Please edit and suggest as you wish.
There is answer on SO that deals with calling Python from Excel, which is a similar concept: https://stackoverflow.com/a/3569988/2101890. I am trying to use that here but don't know how.
When using commands in another programming language, you can sometimes add them by adding a reference in the MyCommands Editor. You can reference DLLs and other "stuff". Some references to libraries appear automatically. I've installed Python and hoped to find Python in the References, but no such luck:
There is no Python entry here that I can find. You may have better luck. If you do find something, check the box and see if you can add python commands without causing an error when saving the command.
Maybe you can browse to %localappdata%\Programs\Python\Python36\ and add some of the DLLs from there and call Python commands from there. Or try getting it to work in the way described under 1.

Is it possible to examine the inner statements of a function?

Working from the command line I wrote a function called go(). When called it receives input asking the user for a directory address in the format drive:\directory. No need for extra slashes or quotes or r literal qualifiers or what have you. Once you've provided a directory, it lists all the non-hidden files and directories under it.
I want to update the function now with a statement that stores this location in a variable, so that I can start browsing my hierarchy without specifying the full address every time.
Unfortunately I don't remember what statements I put in the function in the first place to make it work as it does. I know it's simple and I could just look it up and rebuild it from scratch with not too much effort, but that isn't the point.
As someone who is trying to learn the language, I try to stay at the command line as much as possible, only visiting the browser when I need to learn something NEW. Having to refer to obscure findings attached to vaguely related questions to rediscover how to do things I've already done is very cumbersome.
So my question is, can I see the contents of functions I have written, and how?
Unfortunately no. Python does not have this level of introspection. Best you can do is see the compiled byte code.
The inspect module details what information is available at runtime: https://docs.python.org/3.5/library/inspect.html

How to find out application is using object reference in cache?

I find it hard to explain my problem as the problem is not within the code itself. If you need more input to understand the following better, please let me know.
I'm using ArcGIS to call a bunch of python scripts, one after another. The scripts use the same class by importing it, or inherit from it. To initialize the class I often use the same name. I have no idea how the communication between ArcGIS and python is implemented. But I noticed that my scripts are not always behaving like they should. I suppose that the reference to the object still exists (even though the script the object was created with has finished) when I call the second script.
First of all: How can I make sure my suspicion is true?
Secondly, if this is the case: is it a good idea to destroy all references to all objects using del or __del__? What is the best way to do this?
Trace tracing your code and walking through it with a debugger? Quickest way to tell if it's accessing correct code. Python Debugger
If you read the documentation, what you want to do is use a break point to make sure code reaches that point in code.
This is the syntax with the debugger.
b(reak) [[filename:]lineno | function[, condition]]

Python Strongly type lists

I am using eclipse for python and I am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is: When I am accessing any item from the list, the IDE does not know its type because in python we do not declare the variable with type, so there is no auto complete and I have to go to the class to copy the attribute name.
To make idea more clear:
class AutomataBranch(object):
def __init__(selfparams):
self.Name="";
self.nodes=[];
class LanguageAutomata(object):
def __init__(selfparams):
self.cfgAutomata=[];#This has AutomaBranch Type
Now in any method in LanguageAutomata class if I wrote:
cfgAutomata. Then it wont give me the Name attribute
Is there any solution for that?
Python is strongly typed and Python lists are too. Your problem come from the fact that Python is dynamically typed. Therefor a var can contain any type, and therefor no IDE can guess what is the type of your parameter, nor give you code completion for the methods.
This is how it is, there is no clean workaround. If it's a problem, then maybe dynamics language is not you predilection tool and you should use something that fit your development style. There are tools for everybody.
8 years later and we actually have a solution in Python 3.6.
PEP484 allows you to annotate your variables primarily for IDEs and linting:
Modifying #Hani's answer:
x : AutomataBranch = self.cfgAutomata[i]
This is now picked up by any good IDE to highlight errors and allow autocomplete.
I think you mean to say "statically typed" instead of "strongly typed." Python is strongly typed. You just don't know what that type is at compile time.
With that said, you really need to abandon the idea that you're going to find any IDEs that work as well for Python as they do for Java or C#. Python's dynamic typing makes this difficult. In fact, I tend to find that powerful IDEs are more of a burden than a help.
I think I found a good managable solution. Actually it is trivial but may help (I used it now).
When I want to access the list then I assign the object which I want to access to a variable ex:
x = AutomataBranch()
x = self.cfgAutomata[i]
The first line is used only to make the IDE knows that x is from AutomatBranch type. After that when I press x then all methods and properties are visualized.
I think it is some how good.

Categories