Field output object not iterable - python

I tried the follwing code and tried to read eseden from a nodeset. I get the following error as "Type error Field output object is not iterable".
Aravind
from odbAccess import *
from textRepr import *
from abaqusConstants import *
import odbAccess
odb=openOdb(path='python2d.odb')
NodesofInterest = odb.rootAssembly.instances['PART-1-1'].nodeSets['NODESET']
eseden=odb.steps['Step-1'].frames[1].fieldOutputs['ESEDEN'].getSubset(region=NodesofInterest)
for v in eseden:
print v
print (v.elementLabel,v.data)

The method getSubset called on fieldOutputs repository returns a FieldOutput object. That object contains a member values, which can be used to read values for a specific variable, 'ESEDEN' in your case.
Member values is actually a FieldValueArray with FieldValue objects, each with all the necessary information about data for a single node.
The reason you're getting an error is that 'FieldOutput' object is really not iterable, so to retrieve the actual information, you need to follow connections as I've just described.
To make this description somewhat more clear, here's a simple example using your code:
from odbAccess import *
from textRepr import *
from abaqusConstants import *
import odbAccess
odb=openOdb(path='python2d.odb')
NodesofInterest = odb.rootAssembly.instances['PART-1-1'].nodeSets['NODESET']
eseden=odb.steps['Step-1'].frames[1].fieldOutputs['ESEDEN'].getSubset(region=NodesofInterest)
# This kind of iteration should work since FieldValueArray is not
# a repository
for value in eseden.values:
# Should print node's label and corresponding value
print value.nodelabel, value.data
You can read more about this if you search for FieldOutput and FieldValue in the documentation. Unfortunately, I can't find a way to directly link any part of documentation separately.

Related

Error opening model as mat file in python?

I am trying to open a model.mat file in python as follow.
But I am getting an error: "AttributeError: 'dict' object has no attribute 'reactions'"
The file supposed to be a full model and I was expecting to inspect the number of reactions etc.
import scipy.io as sio
model= sio.loadmat("C:/Users/mydirectory/myfile.mat")
print(len(model.reactions))
print(len(model.metabolites))
print(len(model.genes))
Method which you use returns dict (https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html )
Only way to access dict values is by:
model["reactions"]
If you really need to use get values as you described you can create Namespace for that dict e.g.:
from argparse import Namespace
ns = Namespace(**model)
and now ns.reactions will work

object is not callable but I do not know how to divide the module into each other

import mains
print(mains.hi())
import parsogv2
print(parsogv2.gets())
priliv = mains()/parsogv2()
I have this trouble 'module' object is not callable. I want to split the values I get in modules How to do it better ? Combine them into one module or can I do as I wanted in the code ?
If what you've been trying to do is divide the return values of mains.hi() and parsogv2.gets(), then what you should be doing is:
priliv = mains.hi()/parsogv2.gets()
The error you've been receiving, informing you that a module is not callable, is a result of your attempt to call the actual modules (mains and parsogv2) instead of the functions they contain (mains.hi and parsogv2.gets), which I assume is what you were going for.

Where to find missing Python API/Documentation for a module on Github?

I'm fairly new to Python and I'm trying to get my head around documentation of modules saved to Github.
My case is module SimpSOM (https://github.com/fcomitani/SimpSOM). I installed the module and attempted the basic intro example presented on the Github page and in the linked readthedocs page.
Unfortunately, I immediately run into problems with:
import pandas as pd
import numpy as np
import SimpSOM as sps
from sklearn.cluster import KMeans
#training
net = sps.somNet(20, 20, raw_data, PBC=True)
Which returns the error:
NameError: name 'raw_data' is not defined
I do not think raw_data is not part of the SimpSOM module, it may just be a place holder name for the sack of example and I can't find any mention of raw_data in the corresponding websites/documentation. However, Google revealed the following page: https://pydoc.net/SimpSOM/1.3.1/SimpSOM/ with the following code:
class somNet:
""" Kohonen SOM Network class. """
def __init__(self, netHeight, netWidth, data, loadFile=None, PCI=0, PBC=0):
"""Initialise the SOM network.
Args:
netHeight (int): Number of nodes along the first dimension.
netWidth (int): Numer of nodes along the second dimension.
data (np.array or list): N-dimensional dataset.
....
I have several questions:
Did my tired eyes miss complete API docs?
I can see from the comments that raw_data is in fact data which is of type np.array or list. If a module is missing documentation, is there any way of programmatically making an enquiry of a class module or a function to see what data types are expected?
Worst a case scenario, how can I generate my own documentation from the comments inside that class? It looks like they're compatible with doxygen.

ppf function missing from t

I'm trying to reproduce the example given here: http://jkitchin.github.io/blog/2013/02/12/Nonlinear-curve-fitting-with-parameter-confidence-intervals/
So I imported the module like that:
from scipy.stats.distributions import t
But when I try to a simple
tval = t.ppf(1-alpha/2, dof)
I have the exception:
AttributeError: 'numpy.ndarray' object has no attribute 'ppf'
So t is a numpy.ndarray. But if I read the doc, it is supposed to be an object, with methods.
Do you have an idea about what's happening ?
It seems you may have overwritten the variable t with an array somewhere. What your error message means is that t is a numpy.ndarray which has no ppf method. The t you intended to import shouldn't be an ndarray but rather a distribution generator.
Either find where it became an array and use another name there, or import with better names.
For example, try changing your import line to this:
from scipy.stats import distrbutions as dists
and then change the problem line to:
tval = dists.t.ppf(1-alpha/2, dof)
Alternatively:
from scipy.stats.distributions import t as tdist
tval = tdist.ppf(1-alpha/2, dof)

Modifying a variable in a module imported using from ... import *

Consider the following code:
#main.py
From toolsmodule import *
database = "foo"
#toolsmodule
database = "mydatabase"
As it seems, this creates one variable in each module with different content. How can I modify the variable inside toolsmodule from main? The following does not work:
toolsmodule.database = "foo"
Sounds like yet another of the multitude of good reasons not to use from toolsmodule import *.
If you just do import toolsmodule, then you can do toolsmodule.database = 'foo', and everything is wonderful.
Pythons variable names are just labels on variables. When you import * all those labels are local and when you then set the database, you just replace the local variable, not the one in toolsmodule. Hence, do this:
toolsmodule.py:
database = "original"
def printdatabase():
print "Database is", database
And then run:
import toolsmodule
toolsmodule.database = "newdatabase"
toolsmodule.printdatabase()
The output is
Database is newdatabase
Note that if you then from ANOTHER module ALSO did an import * the change is not reflected.
In short: NEVER use from x import *. I don't know why all newbies persist in doing this despite all documentation I know of says that it's a bad idea.
Why don't you do it like that:
import toolsmodule
toolsmodule.database = "foo"
from toolsmodule import * #bad idea, but let's say you have to..
print database #outputs foo

Categories