Форум сайта python.su
0
Rodegastне пугайтесь сильно
Выкладывай. Помогу чем смогу…

import numpy as np import random class Composition: ''' класс хранит состав смеси и распределяет состав по 3 группам: нормальные парафины, изопарафины и водород в зависимости от температуры кипения компонент ''' def __init__(self, composition): # self.level_composition = 0 self._criterion_h2 = [-252.9] self._criterion_norm = [-161.5, -89, - 42, -1, 36.07, 68, 98.42, 125, 151, 174.1, 196] self._criterion_izo = [-11.7, 27.85, 36.1, 36.1, 60, 60.3, 99] self.full_components = composition['x'] self.full_composition = [composition['y']] # данные для расчета кривых в которых нет компонет с нулевым значением self.full_izo, self.full_norm, self.full_h2 = self.paraphine_distribution() # полный набор данных self.izo, self.norm, self.h2 = self.paraphine_full_distribution() # распределение состава по классам # всего классов 12 но индексация начинается с 0 self.max_count = max(len(self.izo['x']), len( self.norm), len(self.h2)) if self.max_count >= 12: self.max_count = 11 self.izo_class = [x for x in np.linspace( self.izo['x'][0], self.izo['x'][-1], self.max_count) ] self.norm_class = [x for x in np.linspace( self.norm['x'][0], self.norm['x'][-1], self.max_count)] self.h2_class = [x for x in np.linspace( self.h2['x'][0], self.h2['x'][-1], self.max_count)] def get_class_distribution(self, component): ''' поиск копмонента среди групп: нормальных, изо и водорода, возвращая его индекс среди группы ''' if component in self.izo['x']: for i in range(0, len(self.izo_class)-1): if self.izo_class[i] <= component < self.izo_class[i+1]: return i return len(self.izo_class)-1 elif component in self.norm['x']: k = len(self.norm_class) for i in range(0, len(self.norm_class)-2): if self.norm_class[i] <= component < self.norm_class[i+1]: return i return len(self.norm_class)-1 elif component == -252.9: return 2 def paraphine_distribution(self): ''' распределение всего состава по 3 группам: нормальные парафины, изопарафины и водород. Учитываются компоненты, частота появления которых не равно нулю :param composition: входные данные :type composition: calculate_file ''' izo_component = [] izo_quantity = [] norm_component = [] norm_quantity = [] h2_component = [x for x in np.linspace(-300, -250, 6)] h2_quantity = [random.randint(0, 30) for x in range(0, 6)] h2_quantity[2] = 100 for i, item in enumerate(self.full_components): if self.full_composition[-1][i] != 0: if item in self._criterion_norm: norm_component.append(item) norm_quantity.append(self.full_composition[-1][i]) elif item in self._criterion_izo: izo_component.append(item) izo_quantity.append(self.full_composition[-1][i]) izo_composition = { 'x': np.array(izo_component), 'y': np.array(izo_quantity) } norm_comosition = { 'x': np.array(norm_component), 'y': np.array(norm_quantity) } h2_composition = { 'x': np.array(h2_component), 'y': np.array(h2_quantity) } return izo_composition, norm_comosition, h2_composition def paraphine_full_distribution(self): ''' распределение всего состава по 3 группам: нормальные парафины, изопарафины и водород учитываются все компоненты :param composition: входные данные :type composition: calculate_file ''' izo_component = [] izo_quantity = [] norm_component = [] norm_quantity = [] h2_component = [x for x in np.linspace(-300, -250, 6)] h2_quantity = [random.randint(0, 30) for x in range(0, 6)] h2_quantity[2] = 100 for i, item in enumerate(self.full_components): if self.full_composition[-1][i] == 0: if item in self._criterion_norm: norm_component.append(item) norm_quantity.append(self.full_composition[-1][i]) elif item in self._criterion_izo: izo_component.append(item) izo_quantity.append(self.full_composition[-1][i]) izo_composition = { 'x': np.array(izo_component), 'y': np.array(izo_quantity) } norm_comosition = { 'x': np.array(norm_component), 'y': np.array(norm_quantity) } h2_composition = { 'x': np.array(h2_component), 'y': np.array(h2_quantity) } return izo_composition, norm_comosition, h2_composition
Офлайн
186
> блин, как то не удобно.
Вот тебе более полная версия:
def _paraphine(self, test): .... if test(i): .... .... def paraphine_distribution(self): return self._paraphine(self.full_composition[-1][i] != 0) def paraphine_full_distribution(self): return self._paraphine(self.full_composition[-1][i] == 0)
izo_composition = { 'x': np.array(izo_component), 'y': np.array(izo_quantity) }
(self.izo_x, __), (self.norm_x, __), (self.h2_x, __) = self.paraphine_full_distribution()
self.max_count = max(len(self.izo['x']), len(self.norm), len(self.h2))
Отредактировано Rodegast (Май 2, 2018 12:46:25)
Офлайн