2014-01-07 26 views
1

我試圖並行化一個代碼,我隨機生成一些圖像(對於我正在處理的特定問題)。當我使用類,我發現這不是直接的多進程的方法,我找了一些替代品,並發現這種方法:使用類/返回列表進行多處理 - Python

#https://gist.github.com/fiatmoney/1086393 
#MultiprocessingMethods.py 

def _pickle_method(method): 
    func_name = method.im_func.__name__ 
    obj = method.im_self 
    cls = method.im_class 
    if func_name.startswith('__') and not func_name.endswith('__'): #deal with mangled names 
     cls_name = cls.__name__.lstrip('_') 
     func_name = '_' + cls_name + func_name 
    return _unpickle_method, (func_name, obj, cls) 


def _unpickle_method(func_name, obj, cls): 
    for cls in cls.__mro__: 
     try: 
      func = cls.__dict__[func_name] 
     except KeyError: 
      pass 
     else: 
      break 
    return func.__get__(obj, cls) 

所以,我申請這對我的代碼:

from multiprocessing import Pool 
from PIL import Image 

import MultiprocessingMethods as Mp 
import Utils 

import random 
import pylab as plt 
import copy_reg 
import types 

copy_reg.pickle(types.MethodType, Mp._pickle_method, Mp._unpickle_method) 

class ImageData(object): 

    def __init__(self, width, height, range_min=-1, range_max=1): 
     self.width = width 
     self.height = height 
     #The values range each pixel can assume 
     self.range_min = range_min 
     self.range_max = range_max 
     self.data = [] 
     for i in range(width): 
      self.data.append([0] * height) 

    def generate_heat_map_image(self, name): 
     """ 
     Generate a heat map of the image 
     :param name: the name of the file 
     """ 
     #self.normalize_image_data() 
     plt.figure() 
     fig = plt.imshow(self.data, extent=[-1, 1, -1, 1]) 
     plt.colorbar(fig) 
     plt.savefig(name+".png") 
     plt.close() 

    def shepard_interpolation(self, seeds=10): 
     print type (self.data) 
     #Code omitted 'cause it doesn't effect the problem 
     return self.data 


if __name__ == '__main__': 
    x = [ImageData(50, 50), ImageData(50, 50)] 
    p = Pool() 
    outputs = p.map(ImageData.shepard_interpolation,x) 

    #A [[[ ]]] 
    print outputs 
    for i in range(len(outputs)): 
     # A [[ ]] 
     print outputs[i] 
     outputs[i].generate_heat_map_image("ImagesOutput/Entries/Entry"+str(i)) 

現在我可以並行化我的進程,但我得到的是一個數組數組,我不知道爲什麼。在此之前,我總是獲得一組ImageData,並且我可以生成熱圖圖像,其中 matplotlib。這種回報是否與多處理有關?我想是的,因爲我得到了「AttributeError:'列表'對象沒有屬性'generate_heat_map_image'」,並且返回應該是一個ImageData類型的列表,也不是一個列表列表。我可以返回一個ImageData數組嗎?

任何幫助,將不勝感激。 在此先感謝。

回答

1

已解決。我只不得不說:

def shepard_interpolation(self, seeds=10): 
    print type (self.data) 
    #Code omitted 'cause it doesn't effect the problem 
    return self 

在5個不間斷的編程小時後發生的事情。 謝謝,夥計們。

1

ImageData類的標識是錯誤的,所以你的方法實際上不屬於類,即使沒有多處理;這裏是正確的:

class ImageData: 
    def __init__(self, width, height, range_min=-1, range_max=1): 
     self.width = width 
     self.height = height 
     #Which values each pixel can assume 
     self.range_min = range_min 
     self.range_max = range_max 
     self.data = [] 
     for i in range(width): 
      self.data.append([0] * height) 

    def interpolate_points(self, seeds): 
     points = [] 
     f = [] 
     for i in range(seeds): 
      # Generate a cell position 
      pos_x = random.randrange(self.width) 
      pos_y = random.randrange(self.height) 

      # Save the f(x,y) data 
      x = Utils.translate_range(pos_x, 0, self.width, self.range_min, self.range_max) 
      y = Utils.translate_range(pos_y, 0, self.height, self.range_min, self.range_max) 
      z = Utils.function(x, y) 
      points.append([x, y]) 

      f.append(z) 
     for x in range(self.width): 
      xt = (Utils.translate_range(x, 0, self.width, self.range_min, self.range_max)) 
      for y in range(self.height): 
       yt = (Utils.translate_range(y, 0, self.height, self.range_min, self.range_max)) 
       self.data[x][y] = Utils.shepard_euclidian(points, f, [xt, yt], 3) 

    # >>>> Note the identation change here! 
    def generate_heat_map_image(self, name): 
     """ 
     Generate a heat map of the image 
     :param name: the name of the file 
     """ 

     #self.normalize_image_data() 
     plt.figure() 
     fig = plt.imshow(self.data, extent=[-1, 1, -1, 1]) 
     plt.colorbar(fig) 
     plt.savefig(name+".png") 
     plt.close() 
+0

我的代碼中的縮進是可以的。當我粘貼它時,這是一個問題。我將編輯。 – pceccon

+0

你會得到什麼樣的錯誤/異常?你可以發佈堆棧跟蹤嗎? –

+0

我發現用Python進行多處理並不簡單。我找到了一個替代品,但我仍然有問題。我會更新我的問題。謝謝@ F.X。 – pceccon