Advertisement
boolit

Untitled

Oct 16th, 2023
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.52 KB | None | 0 0
  1. from lasio.las_items import CurveItem
  2. from scipy import stats
  3. import numpy as np
  4. import pickle
  5. import lasio
  6. import string
  7. import langid
  8. # from common import settings
  9. # from utils.storages import MediaStorage
  10. # from wells.functions import decode_las
  11. # from wells.models import WellLogFile
  12.  
  13. class DataMatchingService:
  14.     def __init__(
  15.         self,
  16.         well_log_file,#: WellLogFile,
  17.         language,#: str,
  18.         confidence_level,#: float = 0.9,
  19.         dict_of_mnemonic,
  20.     ) -> None:
  21. #         if settings.ENVIRONMENT in ['prod', 'dev']:
  22. #             file = MediaStorage().open(well_log_file.file.name).file.read().decode('cp1251')
  23. #         else:
  24. #             file = well_log_file.file.path
  25.  
  26.         self.file = lasio.read(well_log_file)
  27.         self.loaded_model = pickle.load(open(f'wl_18_unit_model_{language}.pickle', 'rb'))[0]
  28.         self.language = language
  29.         self.confidence_level = confidence_level
  30.         self.mnemonics = dict_of_mnemonic#well_log_file.mnemonics
  31.        
  32. #         " read dict_of_mnemonic "
  33. #         self.dict_of_mnemonic = dict_of_mnemonic
  34.  
  35.     def get_mnemonic(self, curve: CurveItem):# -> None | str:
  36.         found_logs = []
  37.         for mnemonic in self.mnemonics:
  38.             if curve.mnemonic.lower() in mnemonic['possible_mnemonics']:
  39.                 found_logs.append(mnemonic['inlog_mnemonic'])
  40.  
  41.         mnemonic = None
  42.         if len(found_logs) > 0:
  43.             mnemonic = found_logs[0]
  44.         return mnemonic
  45.  
  46.     def get_predicted_unit(self, curve: CurveItem):# -> None | str:
  47.         predicted_unit = None
  48.  
  49.         if self.mnemonics:
  50.             log_values = curve.data
  51.             log_values = log_values[~np.isnan(log_values)]
  52.             if len(log_values) > 0:
  53.                 median = np.median(log_values)
  54.                 mean = np.mean(log_values)
  55.                 mode = stats.mode(log_values)[0][0]
  56.                 min_ = np.min(log_values)
  57.                 max_ = np.max(log_values)
  58.                 input_ = np.array([[median, mean, mode, min_, max_]]).reshape(1, -1)
  59.  
  60.                 probs = self.loaded_model.predict_proba(input_)
  61.                 if any(k >= self.confidence_level for k in probs[0]):
  62.                     predicted_unit = self.loaded_model.predict(input_)[0]
  63.  
  64.         return predicted_unit
  65.  
  66.     def get_found_unit(self, curve: CurveItem):# -> str:
  67.         found_units = []
  68.         for mnem_element in self.mnemonics:
  69.             if curve.mnemonic.lower() in mnem_element['possible_mnemonics']:
  70.                 print('=======', mnem_element['inlog_unit_and_conversion_' + self.language])
  71.                 found_units.append(
  72.                     [
  73.                         [i for i in mnem_element['inlog_unit_and_conversion_' + self.language]
  74.                          if i['value'] == 1][0]['scale']
  75.                     ]
  76.                 )
  77.         found_unit = None
  78.         if len(found_units) > 0:
  79.             found_unit = found_units[0][0]
  80.         return found_unit
  81.  
  82.     def get_conversion_func(self, curve: CurveItem, found_unit: str, predicted_unit: str):# -> None | float:
  83.         conversion_func = None
  84.         if curve.unit.lower() == found_unit and found_unit is not None:
  85.             conversion_func = float(1)
  86.  
  87.         if curve.unit.lower() != found_unit and found_unit is not None:
  88.             conversion_func_list = []
  89.             for mnem_element in self.mnemonics:
  90.                 if curve.mnemonic.lower() in mnem_element['possible_mnemonics']:
  91.                     current_lang = langid.classify(curve.unit.lower())[0]
  92.                     if len([i for i in mnem_element['inlog_unit_and_conversion_' + current_lang] if i['scale'] == curve.unit.lower()]) > 0:
  93.                         conversion_func_list.append(
  94.                             [i for i in mnem_element['inlog_unit_and_conversion_' + current_lang]
  95.                              if i['scale'] == curve.unit.lower()][0]['value']
  96.                         )
  97.             if len(conversion_func_list) > 0:
  98.                 print(conversion_func_list)
  99.                 conversion_func = conversion_func_list[0]
  100.         print('пппппппппппп',curve.mnemonic.lower(), conversion_func)
  101.         return conversion_func
  102.        
  103.     def get_depth_values(self, curve: CurveItem):# -> None | str:
  104. #         print('values',curve.data)
  105.         return curve.data.tolist()
  106.  
  107.     def get_matching_matrix(self):# -> list:
  108.         output = []
  109.        
  110.         for curve_id, curve in enumerate(self.file.curves):
  111.             print(curve)
  112.             print('unit curve',curve.unit.lower())
  113.             #===extract depth values
  114.             depth_values = self.file.data[:,[0, curve_id]]
  115.             depth_values_no_nans = depth_values[~np.isnan(depth_values).any(axis=1)]
  116.             depthes = list(depth_values_no_nans[:,0])
  117.             values = list(depth_values_no_nans[:,1])
  118.             #===extract depth values    
  119.             mnemonic = self.get_mnemonic(curve)
  120.             predicted_unit = self.get_predicted_unit(curve)
  121.             found_unit = self.get_found_unit(curve)
  122.             conversion_func = self.get_conversion_func(curve, found_unit, predicted_unit)          
  123.             output.append(
  124.                 [
  125.                     curve.descr,
  126.                     curve.mnemonic,
  127.                     mnemonic,
  128.                     curve.unit.lower(),
  129.                     predicted_unit,
  130.                     found_unit,
  131.                     conversion_func,
  132.                     depthes,
  133.                     values
  134.                 ]
  135.             )
  136.         return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement