main.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import pandas as pd
  2. from scipy.optimize import fsolve
  3. import glob
  4. import os
  5. import math
  6. class countExcel:
  7. # init
  8. def __init__(self,FilePath='C:\\Users\\caner\\Desktop\\countExcelC:\\Users\\caner\\Desktop\\countExcel') -> None:
  9. print('初始化传参',FilePath)
  10. pass
  11. # RCE方程函数
  12. def RCE(self,rce,v):
  13. try:
  14. RC = float(rce[0])
  15. E = float(rce[1])
  16. K = float(v[0])
  17. K1 = float(v[1])
  18. return [
  19. 2*K*E-RC*RC,
  20. 2*K1*E-RC*RC
  21. ]
  22. except:
  23. return ['null', 'null']
  24. # R123方程函数
  25. def R123(self,r,v):
  26. try:
  27. R1 = float(r[0])
  28. R2 = float(r[1])
  29. R3 = float(r[2])
  30. Er = float(r[3])
  31. V = float(r[4])
  32. K = float(v[0])
  33. K1 = float(v[1])
  34. K2 = float(v[2])
  35. K3 = float(v[3])
  36. K4 = float(v[4])
  37. return [
  38. # 2*弹性模量(E)*机械比能-R1*R1-R2*R2-R3*R3+2*v*(R1*R2+R1*R3+R2*R3)
  39. 2*Er*K-R1*R1-R2*R2-R3*R3+2*V*(R1*R2+R1*R3+R2*R3),
  40. 2*Er*K1-R1*R1-R2*R2-R3*R3+2*V*(R1*R2+R1*R3+R2*R3),
  41. 2*Er*K2-R1*R1-R2*R2-R3*R3+2*V*(R1*R2+R1*R3+R2*R3),
  42. 2*Er*K3-R1*R1-R2*R2-R3*R3+2*V*(R1*R2+R1*R3+R2*R3),
  43. 2*Er*K4-R1*R1-R2*R2-R3*R3+2*V*(R1*R2+R1*R3+R2*R3)
  44. ]
  45. except:
  46. return ['null', 'null', 'null', 'null', 'null']
  47. # 对数据为null 的进行平均值填补
  48. def nullData(self,arr):
  49. num = 0
  50. lens = len(arr)
  51. for j in arr:
  52. if j != 'null':
  53. num += j
  54. ag = num/lens
  55. for z in range(lens):
  56. el = arr[z]
  57. if el == 'null' or el == 0:
  58. arr[z] = ag
  59. def main(self):
  60. colTitle = '机械比能'
  61. try:
  62. files = glob.glob(os.path.join(self.FilePath, "*.xls"))
  63. RC,E ,RK1,RK2,RK3,GRA #注意返回的变量是当前域全局变量
  64. for file in files:
  65. array = pd.read_excel(file) # 读取excel数据
  66. try:
  67. # 删除一次old
  68. array.drop('单轴抗压强度', axis=1, inplace=True)
  69. array.drop('弹性模量', axis=1, inplace=True)
  70. array.drop('第一主应力', axis=1, inplace=True)
  71. array.drop('第二主应力', axis=1, inplace=True)
  72. array.drop('第三主应力', axis=1, inplace=True)
  73. array.drop('岩石强度应力比', axis=1, inplace=True)
  74. except:
  75. print('无旧数据')
  76. # 获取数据
  77. col = array[colTitle] # 某列
  78. colLen = len(col) # 一列的长度
  79. rowLen = len(array.iloc[1]) # 一行的长度
  80. RC = [0]*(colLen) # RC初始化
  81. E = [0]*(colLen) # E初始化
  82. RK1 = [0]*(colLen) # R1初始化
  83. RK2 = [0]*(colLen) # R2初始化
  84. RK3 = [0]*(colLen) # R3初始化
  85. GRA = [0]*(colLen) # 强度初始化
  86. # 两行计算RCE
  87. for i in range(colLen):
  88. if (i+1) < len(col):
  89. K = col[i]
  90. K1 = col[i+1]
  91. res = fsolve(self.RCE, [1, 1000],[K,K1]) # 其它参数直接用
  92. rc = self.RCE(res)[0]
  93. e = self.RCE(res)[1]
  94. RC[i] = math.fabs(rc) # 绝对值
  95. RC[i+1] = math.fabs(rc) # 列最后一个值使用上一个的值
  96. E[i] = math.fabs(e)
  97. E[i+1] = math.fabs(e)
  98. # 单独对数据为null的进行平均值计算
  99. self.nullData(RC)
  100. self.nullData(E)
  101. # 三行计算R123
  102. for j in range(colLen):
  103. if (j+4) < len(col):
  104. K = col[j]
  105. K1 = col[j+1]
  106. K2 = col[j+2]
  107. K3 = col[j+3]
  108. K4 = col[j+4]
  109. rea = fsolve(self.R123, [1, 1, 1, 100, 1],[K,K1,K2,K3,K4]) # 其它参数直接用
  110. rea = self.R123(rea)
  111. arr = sorted(rea, reverse=True) # 倒叙
  112. print(arr)
  113. RK1[j] = math.fabs(arr[0])
  114. RK1[j+1] = math.fabs(arr[0])
  115. RK1[j+2] = math.fabs(arr[0])
  116. RK1[j+3] = math.fabs(arr[0])
  117. RK1[j+4] = math.fabs(arr[0])
  118. RK2[j] = math.fabs(arr[1])
  119. RK2[j+1] = math.fabs(arr[1])
  120. RK2[j+2] = math.fabs(arr[1])
  121. RK2[j+3] = math.fabs(arr[1])
  122. RK2[j+4] = math.fabs(arr[1])
  123. RK3[j] = math.fabs(arr[2])
  124. RK3[j+1] = math.fabs(arr[2])
  125. RK3[j+2] = math.fabs(arr[2])
  126. RK3[j+3] = math.fabs(arr[2])
  127. RK3[j+4] = math.fabs(arr[2])
  128. # 单独对数据为null的进行平均值计算
  129. self.nullData(RK1)
  130. self.nullData(RK2)
  131. self.nullData(RK3)
  132. # 对整体数据进行倍数计算↓
  133. for G in range(colLen):
  134. RC[G] = RC[G]/100
  135. E[G] = E[G]
  136. RK1[G] = RK1[G]/100
  137. RK2[G] = RK2[G]/100
  138. RK3[G] = RK3[G]/100
  139. # 计算强度
  140. for z in range(colLen):
  141. try:
  142. GRA[z] = RC[z] / RK1[z]
  143. except:
  144. GRA[z] = 'null'
  145. # 单独对数据为null的进行平均值计算
  146. self.nullData(GRA)
  147. # # 保存excel
  148. # array.insert(rowLen, '单轴抗压强度', RC)
  149. # array.insert(rowLen+1, '弹性模量', E)
  150. # array.insert(rowLen+2, '第一主应力', RK1)
  151. # array.insert(rowLen+3, '第二主应力', RK2)
  152. # array.insert(rowLen+4, '第三主应力', RK3)
  153. # array.insert(rowLen+5, '岩石强度应力比', GRA)
  154. # pd.DataFrame(array).to_excel(
  155. # file, sheet_name='Sheet1', index=False, header=True)
  156. # print('保存完毕')
  157. return {RC,E ,RK1,RK2,RK3,GRA}
  158. except ZeroDivisionError as err:
  159. print('计算错误', err)
  160. # 调用
  161. # from countExcel import countExcel
  162. # a = countExcel()
  163. # b = a.main()
  164. # if __name__ == "__main__":
  165. # path = 'C:\\Users\\caner\\Desktop\\countExcel'
  166. # colTitle = '机械比能'
  167. # try:
  168. # files = glob.glob(os.path.join(path, "*.xls"))
  169. # for file in files:
  170. # array = pd.read_excel(file) # 读取excel数据
  171. # try:
  172. # # 删除一次old
  173. # array.drop('单轴抗压强度', axis=1, inplace=True)
  174. # array.drop('弹性模量', axis=1, inplace=True)
  175. # array.drop('第一主应力', axis=1, inplace=True)
  176. # array.drop('第二主应力', axis=1, inplace=True)
  177. # array.drop('第三主应力', axis=1, inplace=True)
  178. # array.drop('岩石强度应力比', axis=1, inplace=True)
  179. # except:
  180. # print('无旧数据')
  181. # # 获取数据
  182. # col = array[colTitle] # 某列
  183. # colLen = len(col) # 一列的长度
  184. # rowLen = len(array.iloc[1]) # 一行的长度
  185. # RC = [0]*(colLen) # RC初始化
  186. # E = [0]*(colLen) # E初始化
  187. # RK1 = [0]*(colLen) # R1初始化
  188. # RK2 = [0]*(colLen) # R2初始化
  189. # RK3 = [0]*(colLen) # R3初始化
  190. # GRA = [0]*(colLen) # 强度初始化
  191. # # 两行计算RCE
  192. # for i in range(colLen):
  193. # if (i+1) < len(col):
  194. # K = col[i]
  195. # K1 = col[i+1]
  196. # res = fsolve(RCE, [1, 1000]) # 其它参数直接用
  197. # rc = RCE(res)[0]
  198. # e = RCE(res)[1]
  199. # RC[i] = math.fabs(rc) # 绝对值
  200. # RC[i+1] = math.fabs(rc) # 列最后一个值使用上一个的值
  201. # E[i] = math.fabs(e)
  202. # E[i+1] = math.fabs(e)
  203. # # 单独对数据为null的进行平均值计算
  204. # nullData(RC)
  205. # nullData(E)
  206. # # 三行计算R123
  207. # for j in range(colLen):
  208. # if (j+4) < len(col):
  209. # K = col[j]
  210. # K1 = col[j+1]
  211. # K2 = col[j+2]
  212. # K3 = col[j+3]
  213. # K4 = col[j+4]
  214. # rea = fsolve(R123, [1, 1, 1, 100, 1]) # 其它参数直接用
  215. # rea = R123(rea)
  216. # arr = sorted(rea, reverse=True) # 倒叙
  217. # print(arr)
  218. # RK1[j] = math.fabs(arr[0])
  219. # RK1[j+1] = math.fabs(arr[0])
  220. # RK1[j+2] = math.fabs(arr[0])
  221. # RK1[j+3] = math.fabs(arr[0])
  222. # RK1[j+4] = math.fabs(arr[0])
  223. # RK2[j] = math.fabs(arr[1])
  224. # RK2[j+1] = math.fabs(arr[1])
  225. # RK2[j+2] = math.fabs(arr[1])
  226. # RK2[j+3] = math.fabs(arr[1])
  227. # RK2[j+4] = math.fabs(arr[1])
  228. # RK3[j] = math.fabs(arr[2])
  229. # RK3[j+1] = math.fabs(arr[2])
  230. # RK3[j+2] = math.fabs(arr[2])
  231. # RK3[j+3] = math.fabs(arr[2])
  232. # RK3[j+4] = math.fabs(arr[2])
  233. # # 单独对数据为null的进行平均值计算
  234. # nullData(RK1)
  235. # nullData(RK2)
  236. # nullData(RK3)
  237. # # 对整体数据进行倍数计算↓
  238. # for G in range(colLen):
  239. # RC[G] = RC[G]/100
  240. # E[G] = E[G]
  241. # RK1[G] = RK1[G]/100
  242. # RK2[G] = RK2[G]/100
  243. # RK3[G] = RK3[G]/100
  244. # # 计算强度
  245. # for z in range(colLen):
  246. # try:
  247. # GRA[z] = RC[z] / RK1[z]
  248. # except:
  249. # GRA[z] = 'null'
  250. # # 单独对数据为null的进行平均值计算
  251. # nullData(GRA)
  252. # # 保存excel
  253. # array.insert(rowLen, '单轴抗压强度', RC)
  254. # array.insert(rowLen+1, '弹性模量', E)
  255. # array.insert(rowLen+2, '第一主应力', RK1)
  256. # array.insert(rowLen+3, '第二主应力', RK2)
  257. # array.insert(rowLen+4, '第三主应力', RK3)
  258. # array.insert(rowLen+5, '岩石强度应力比', GRA)
  259. # pd.DataFrame(array).to_excel(
  260. # file, sheet_name='Sheet1', index=False, header=True)
  261. # print('保存完毕')
  262. # except ZeroDivisionError as err:
  263. # print('计算错误', err)