scipy.io.matlab.mio5_params.mat_struct object Python - python

Following this question other question I got to the next question.
I have 40 matlab files and I'm implementing a general solution for extracting that info, as each of them has a different internal organization. As soon as I have it I will post here.
But my question is: the solutions given by gg349 and mergen gets me some objects like this:
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f368cff3f90>,
And I don't know how to deal with those, could anyone help?

It is the equivalent of Matlab struct in python. You can access the elements by dot, like object.value. An example here. Complex Matlab struct mat file read by python

Related

Combine channels in ImageJ Jython?

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.

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 is pandas.json.dumps implemented?

I've been scouring the internet for hours now and I'm stumped. Pandas has a method dumps(accessible via pandas.json.dumps) that can encode any arbitrary object to a json string. The builtin json.dumps would normally just throw an exception.
I've been looking at source code trying to find the implementation of this function but I can't find it. Does anyone have the implementation or have an idea of how this would work?
A search through the Pandas GitHub repository shows that pandas.json.dumps appears to be implemented by the objToJson function defined in pandas/src/ujson/python/objToJson.c.

Where to Store Borrowed Python Code?

Recently, I have been working on a Python project with usual directory structure, and have received help from someone else who has given me a code snippet (a single function definition, about 30 lines long) which I would like to import into my code. What is the most proper directory/location in a Python project to store borrowed code of this size? Is it best to store the snippet into an entirely different module and import it from there?
I generally find it easiest to put such code in a separate file, because for clarity you don't want more than one different copyright/licensing term to apply within a single file. So in Python this does indeed mean a separate module. Then the file can contain whatever attribution and other legal boilerplate you need.
As long as your file headers don't accidentally claim copyright on something to which you do not own the copyright, I don't think it's actually a legal problem to mix externally-licensed or public domain code into files you mostly own. I may be wrong, though, which is why I normally avoid giving myself reason to think about it. A comment saying "this is external code from the following source with the following license:" may well be clearer than dividing code into different files that naturally wouldn't be. So I do occasionally do that.
I don't see any definite need for a separate directory (or package) per separate external source. If that's already part of your project structure (that is, it already uses external libraries by incorporating their source) then I suppose you might as well continue the trend.
I usually place scripts I copy off the internet in a folder/package called borrowed so I know all of the code here is stuff that I didn't write myself.
That is, if it's something more substantial than a one or two-liner demonstrating how something works.

Best way to put Python Operators and Functions into Classes?

This is a follow up question to the last question I'd ask, which you can read here:
Using Python's basic I/O to manipulate or create Python Files?
Basically I was asking for the best way to allow Python to edit other Python programs sources, for the purpose of screwing around/experimenting and seeing if I could hook them up to either a genetic algorithm or some sort of backprop network to get results.
Basically, one of the answers suggests making every python operator or code-bit, such as '=', 'if', etc, etc, into classes which can then be used by me/various other programs to piece together/edit other python files, utilizing Python's basic file i/o as a means of manipulating.
The classes would each, upon initialization, get their own unique ID, which would be stored, along with line number, type, etc, in an SQLite3 database for logging purposes, and would be operated upon by other classes/the basic file i/o system.
My question is: Is this a sane task? If not, what can I change or do differently? Am I undertaking something that will be worthwhile, or is it completely idiotic? If you need clarification please ask, I want to know if what I'm doing seems reasonable to an outside source, or if I should reconsider/scrap the whole deal...
Thanks in advance.
Don't generate code modify the AST for the code.
import ast
p = ast.parse("testcode")
Docs for AST: http://docs.python.org/library/ast.html
Example of modifying code: http://docs.python.org/library/ast.html#ast.NodeTransformer
Example: (modifying 3*3 to 3+3)
from ast import *
tree = parse("print(3*3)") #parse the code
tree.body[0].value.args[0].op = Add() #modify multiplication to plus
codeobj = compile(tree,"","exec") # compile the code
exec(codeobj) #run it - it should print 6 (not 9 which the original code would print)
BTW, I am interested in genetic algorithms.
If you start a project, I can help you.

Categories