2017-02-17 27 views
0

Python beginner here。私はいくつかのデータを辞書に保存しようとしています。 フォルダ内にいくつかの.npyファイルがあります。 np.loadで行われたマップの読み込み、現在のマップの年月日(整数)、年単位の小数点以下の桁数(月が与えられている場合)などをカプセル化する辞書を作成することは私の意図です。私の計算には影響しません)、ピクセル数、および特定の値を超えるピクセル数です。Python画像ファイルの操作

import glob 
file_list = glob.glob('*.npy') 

def only_numbers(seq): #for getting rid of any '.npy' or any other string 
    seq_type= type(seq) 
    return seq_type().join(filter(seq_type.isdigit, seq)) 

maps = {} 
for i in range(0, len(file_list)-1): 
    maps[i] = np.load(file_list[i]) 
    numbers[i]=list(only_numbers(file_list[i])) 

私はある複数の値を持つように辞書を取得する方法には考えている:私は今まで管理何

{'map0':'array(from np.load)', 'year', 'month', 'day', 'fractional_time', 'pixels' 
'map1':'....} 

は以下の通りです:終わりに私のような辞書を得ることを期待しますforループの下。私は、新しい辞書、またはすべてのタスクのリスト(例えば、数字)を生成することしかできません。数字の辞書については、私が探している整数を得るためにYYYYMMDDの形式で日付を操作する方法がわかりません。

data = np.load('20100620.npy') 
print('Total pixel count: ', data.size) 
c = (data > 50).astype(int) 
print('Pixel >50%: ',np.count_nonzero(c)) 

任意のヒントを:

はピクセルのために、私が使用して、単一のマップのためにそれを得ることができましたか?これまでは、画像処理は非常に難しい問題でした。

編集:誰もが興味を持っている場合は、日付を分割し、

date=list(only_numbers.values()) 
year=int(date[i][0:4]) 
month=int(date[i][4:6]) 
day=int(date[i][6:8]) 
print (year, month, day) 
+0

質問があなたの質問であることを要約するためにタイトルを使うことができるなら、最高であろう – yuval

+0

多分あなたは辞書ではなくクラスを必要とするかもしれません:https://www.tutorialspoint.com/python/python_classes_objects.htm –

+0

@StanislavIvanovチップのおかげで。 OOPは現時点では把握が難しいです。私はそれが最高ではありませんが、私はそれが仕事を行うことができた何かを投稿した:) – nyw

答えて

0

を使用してそれらを整数を作るために管理は、私が何かを行うことができました。私はもっ​​と簡単に操作する必要があったので、すべてを含む辞書という考え方を削除しました。私は次のようにしました:

file_list = glob.glob('data/...') # files named YYYYMMDD.npy 
file_list.sort() 

def only_numbers(seq): # i make sure that i remove all characters and symbols from the name of the file 
    seq_type = type(seq) 
    return seq_type().join(filter(seq_type.isdigit, seq)) 

numbers = {} 
time = [] 
np_above_value = [] 

for i in range(0, len(file_list) - 1): 
    maps = np.load(file_list[i]) 
    maps[np.isnan(maps)] = 0 # had some NANs and getting some errors 
    numbers[i] = only_numbers(file_list[i]) # getting a dictionary with the name of the files that contain only the dates - calling the function I defined earlier 
    date = list(numbers.values()) # registering the name of the files (only the numbers) as a list 
    year = int(date[i][0:4]) # selecting first 4 values (YYYY) and transform them as integers, as required 
    month = int(date[i][4:6]) # selecting next 2 values (MM) 
    day = int(date[i][6:8]) # selecting next 2 values (DD) 
    time.append(year + ((month - 1) * 30 + day)/360) # fractional time 

    print('Total pixel count for map '+ str(i) +':', maps.size) # total number of pixels for the current map in iteration 
    c = (maps > value).astype(int) 
    np_above_value.append (np.count_nonzero(c)) # list of the pixels with a value bigger than value 
    print('Pixels with concentration >value% for map '+ str(i) +':', np.count_nonzero(c)) # total number of pixels with a value bigger than value for the current map in iteration 

plt.plot(time, np_above_value) # pixels with concentration above value as a function of time 

私はそれが非常に不器用かもしれないことは知っています。 Pythonの2週目ですので、それを見逃してください。それはトリックを行います:)