How to save pyprover object with pickle - python

I want to save logical expressions of pyprover with pickle.
The following is the code I wrote in google corabolatory.
!pip install pyprover
import pickle
from pyprover import *
logic = ~A & B
print(1,logic)
print(2,type(logic))
print(3,type(A))
with open("test.pickle","wb") as f:
pickle.dump(logic,f)
with open("test.pickle","rb") as f:
logic2 = pickle.load(f) # error
print(logic2)
The output is below
1 ~A & B
2 <class 'pyprover.logic.And'>
3 <class 'pyprover.logic.Prop'>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-27-4cd4fe38fcb4> in <module>
12
13 with open("test.pickle","rb") as f:
---> 14 logic2 = pickle.load(f) # error
15
16 print(logic2)
AttributeError: 'Top' object has no attribute 'elems'
How can I save the logical expression with pickle?
pyprover github
If you know how to save the object, I do not care about whether or not it uses pickle. I tried dill,only to get the same result as pickle.
I wrote an dill issue here
I tried to import modules like this but it didn't solve the problem.
import dill
import pyprover
from pyprover import *
from pyprover.logic import *
from pyprover.parser import *
from pyprover.constants import *
from pyprover.atoms import *
from pyprover.util import *
from pyprover.tools import *
from pyprover.__coconut__ import *
from pyprover.__init__ import *

Related

ABAQUS script for a custom field output

I am new to ABAQUS scripting and I am trying to calculate micromotion using COPEN, CSLIP1 and CSLIP2. I came up with the code below:
from abaqusConstants import *
from odbAccess import *
from odbMaterial import *
from odbSection import *
from math import *
from copy import deepcopy
from caeModules import *
from driverUtils import executeOnCaeStartup
from numpy import fabs as fabs
import numpy as np
from types import IntType
odb = session.openOdb(name='E:\PDP02.odb', readOnly=FALSE)
odb = session.odbs['E:\PDP02.odb']
print odb.rootAssembly.instances.keys()
grout_instance = odb.rootAssembly.instances['PROX-1#PROXIMAL-1']
keys = odb.steps.keys()
for key in keys:
step = odb.steps[key]
for frame in step.frames:
print frame.description
Copen = frame.fieldOutputs['COPEN']
Cslip1 = frame.fieldOutputs['CSLIP1']
Cslip2 = frame.fieldOutputs['CSLIP2']
Micromotion = sqrt(power(Copen,2)+power(Cslip1,2)+power(Cslip2,2))
#Micromotion =sqrt(power(Cslip2,2))
#float(Micromotion)
frame.FieldOutput(name='Micromotion', description='Average Micromotion', field=Micromotion)
odb.update()
odb.save()
After executing the code, i get the following error message: "OdiError: Expression evaluates to an overflow or underflow". Please help me understand this error message and how to rectify it. I am happy to provide the .inp and .odb files for reference and verification.
Simply put, overflow and underflow happen when we assign a value that is out of range of the declared data type of the variable. If the (absolute) value is too big, we call it overflow, if the value is too small, we call it underflow.

Can dill remember libraries used by a class?

If I create a class that imports a library and use dill to pickle it, when I unpickle it I cannot find the library:
import dill
from sklearn.metrics.cluster import adjusted_rand_score
import pandas as pd
import random
class Test1():
def __init__(self, df):
self.genomes = df
#staticmethod
def percentageSimilarityDistance(genome1, genome2):
if len(genome1) != len(genome2):
raise ValueError('Genome1 and genome2 must have the same length!')
is_gene_correct = [1 if genome1[idx] == genome2[idx] else 0 for idx in range(len(genome1))]
return (1 - sum(is_gene_correct)/(len(is_gene_correct) * 1.0))
def createDistanceMatrix(self, distance_function):
"""Takes a dictionary of KO sets and returns a distance (or similarity) matrix which is basically how many genes do they have in common."""
genomes_df = self.genomes.copy()
no_of_genes, no_of_genomes = genomes_df.shape
list_of_genome_names = list(genomes_df.columns)
list_of_genomes = [list(genomes_df.loc[:, name]) for name in list_of_genome_names]
distance_matrix = [[distance_function(list_of_genomes[i], list_of_genomes[j]) for j in range(no_of_genomes)] for i in range(no_of_genomes)]
distance_matrix = pd.DataFrame(distance_matrix, columns = list_of_genomes, index = list_of_genomes)
return distance_matrix
# create fake data
df = pd.DataFrame({'genome' + str(idx + 1): [random.randint(0, 1) for lidx in range(525)] for idx in range(10)})
test1 = Test1(df)
test2 = Test2(df)
# save pickles
with open('test1.pkl', 'wb') as pkl:
dill.dump(test1, pkl)
I successfully unpickle the file but when I try to use one of the methods it can't find Pandas.
$ ipython
Python 3.5.4 |Anaconda custom (64-bit)| (default, Nov 20 2017, 18:44:38)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import dill
In [2]: with open('test1.pkl', 'rb') as pkl:
...: test1 = dill.load(pkl)
...:
In [3]: test1.createDistanceMatrix(test1.percentageSimilarityDistance)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-5918638722b1> in <module>()
----> 1 test1.createDistanceMatrix(test1.percentageSimilarityDistance)
/space/oc13378/myprojects/python/dill_tests/dill_tests.py in createDistanceMatrix(self, distance_function)
29 return distance_matrix
30
---> 31 class Test2():
32 import dill
33 from sklearn.metrics.cluster import adjusted_rand_score
NameError: name 'pd' is not defined
Is it possible to get this to work by only importing the dill library?
I'm the dill author. The easy thing to do is to put the import inside the function. Further, if you put the import both inside and outside your function, then you won't have a speed hit on the first call of your function.

TypeError: 'module' object is not callable in python and pandas

I imported the module correctly into pandas and called it correctly using import main and then main.main(data, 1,10,2.5) but I am getting an error:
TypeError Traceback (most recent call last)
<ipython-input-52-e9913b227737> in <module>()
----> 1 main.main(data, 1, 10, 2.5)
38 dat_sh = data.shape[0]
39 #Z = random.sample(range(0,U),k_max)
---> 40 Z = cf.centroid_finder(data,sp_atr,k_max)
41
42 prototypes = {i: data[j:j+1].values.tolist()[0] for i,j in enumerate(Z)}
11 for i in range(dat_sh):
12 for j in range(dat_sh):
---> 13 D[i][j] = ed(sub_atr[i],sub_atr[j])
14
15
TypeError: 'module' object is not callable
ed is euclidean:
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
There may be some confusion regarding the importing of modules vs functions.
It's possible to recreate your error with a simple example, assuming that module/function importing has gotten mixed up somewhere along the way. Consider a case where we pass initial values a and b through a centroid_finder() function, which lives in cf.py:
# import cf.py as cf
import cf
a, b = ([1,2,3], [2,3,4])
cf.centroid_finder(a, b)
But centroid_finder() calls ed(), which lives in ed.py:
## cf.py
# import ed.py as ed
import ed
def centroid_finder(a, b):
print(ed(a, b))
## ed.py
from numpy import sqrt, sum
import numpy as np
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
Here, calling centroid_finder() will give the error you observed:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-e76ec6a0496c> in <module>()
3 a, b = ([1,2,3], [2,3,4])
4
----> 5 cf.centroid_finder(a, b)
cf.py in centroid_finder(a, b)
2
3 def centroid_finder(a, b):
----> 4 print(ed(a, b))
TypeError: 'module' object is not callable
That's because you imported a module, ed.py as ed...but what you wanted was to call the ed() function that lives inside of ed.py. That's ed.ed()!
Changing centroid_finder() to call ed.ed() produces the desired result:
# cf.py
import ed
def centroid_finder(a, b):
print(ed.ed(a, b))
Now, from the main script:
cf.centroid_finder(a, b)
# 1.73205080757
There's at least one undisclosed import shorthand in your example code, where you call sqrt in ed(). There isn't a natural sqrt() in Python, it most likely is imported from either math or numpy, e.g. from numpy import sqrt. That's not a problem, per se, but given the error you're getting about modules being non-callable, you might benefit from explicitly calling functions in your code from the modules they live in.
For example, using import numpy as np, call np.sqrt() instead of just importing sqrt() directly. This is a defensive programming posture that will prevent similar confusion in the future. (You do this already with np.subtract() in ed(); it's unclear why sqrt() doesn't get the same treatment.)

basic python query. NameError in __init__ method

NameError: name 'the_shape' is not defined
I get the following error when I try to import my KMeans module containing the class named KMeansClass. The KMeans.py module has the following structure:
import numpy as np
import scipy
class KMeansClass:
#takes in an npArray like object
def __init__(self,dataset,the_shape=5):
self.dataset=dataset
self.mu = np.empty(shape=the_shape)
and in ipython when I try
import KMeans
I get the NameError: name 'the_shape' is not defined
I am really new to python OOP and don't know why this is happening as all I'm doing is passing arguments to init and assigning those arguments to instance variables.
Any help would be appreciated.
Thanks in advance!
Full Traceback:
NameError Traceback (most recent call last)
<ipython-input-2-44169aae5584> in <module>()
----> 1 import kmeans
/Users/path to file/kmeans.py in <module>()
1 import numpy as np
2 import scipy
----> 3 class KMeansClass:
4 #takes in an npArray like object
5 def __init__(self,dataset,the_shape=5):
/Users/path_to_file/kmeans.py in KMeansClass()
5 def __init__(self,dataset,the_shape=5):
6 self.dataset=dataset
----> 7 self.mu = np.empty(shape=the_shape)
8
9
NameError: name 'the_shape' is not defined

Cosine scoring in whoosh, python

I've implemented some python code to run search on whoosh using BM25 and all went out ok, but now that I'm trying to change scoring mechanism to Cosine, I'm getting this error:
File "TFIDF.py", line 18, in tfidf
with ix.searcher(weighting=whoosh.scoring.Cosine()) as searcher: AttributeError: 'module' object has no attribute 'Cosine'
If I import Cosine
from whoosh.scoring import Cosine
I get this:
File "TFIDF.py", line 4, in <module>
from whoosh.scoring import Cosine
ImportError: cannot import name Cosine
My code is below:
import whoosh
from whoosh.scoring import Cosine
from whoosh.fields import *
from whoosh.scoring import *
from whoosh.qparser import *
from whoosh.query import *
from whoosh.index import open_dir
#Index path
lab3dir= "../lab3/Aula3_1/"
ix = open_dir(lab3dir + "indexdir") #Index generated in previous lab
def cosine(queryTerms):
list=[]
dict={} # dict com ID do doc e Similiaridade
with ix.searcher(weighting=whoosh.scoring.Cosine()) as searcher:
query = QueryParser("content", ix.schema, group=OrGroup).parse(u(queryTerms))
results = searcher.search(query, limit=100)
for i,r in enumerate(results):
list.append(r["id"])
print r, results.score(i)
dict[r["id"]]= results.score(i)
return dict
Any ideias???
Thank you!
The documentation on http://pythonhosted.org/Whoosh/searching.html#the-searcher-object, which I assume is what you're looking at is incorrect as you have found. Look at the documentation (http://pythonhosted.org/Whoosh/api/scoring.html#module-whoosh.scoring) or the source code (https://bitbucket.org/mchaput/whoosh/src/362fc2999c8cabc51370f433de7402fafd536ec6/src/whoosh/scoring.py?at=default) for the options actually available.

Categories