莳萝可以记住类使用的库吗?

时间:2018-11-03 19:19:28

标签: python dill

如果我创建了一个导入库的类并使用dill对其进行腌制,则当我对其进行腌制时,我将找不到该库:

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)

我成功解除了文件的修补程序,但是当我尝试使用其中一种方法时,找不到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

是否可以仅通过导入莳萝库来使其工作?

1 个答案:

答案 0 :(得分:0)

我是dill的作者。容易做的是将import放在函数内。此外,如果将导入同时放在函数的内部和外部,则在第一次调用函数时不会很快受到打击。