浏览代码

整合模型计算的代码,在小程序里实现计算功能

yangtaodemon 6 月之前
父节点
当前提交
f89a94105e

+ 1 - 0
.idea/.name

@@ -0,0 +1 @@
+__init__.py

+ 1 - 1
.idea/AcidificationModel.iml

@@ -2,7 +2,7 @@
 <module type="PYTHON_MODULE" version="4">
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$" />
-    <orderEntry type="jdk" jdkName="pythonProject1" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>

+ 1 - 1
.idea/misc.xml

@@ -3,7 +3,7 @@
   <component name="Black">
     <option name="sdkName" value="pythonProject1" />
   </component>
-  <component name="ProjectRootManager" version="2" project-jdk-name="pythonProject1" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
   <component name="PyCharmProfessionalAdvertiser">
     <option name="shown" value="true" />
   </component>

+ 1 - 1
api/app/config.py

@@ -1,4 +1,4 @@
 class Config:
     SECRET_KEY = 'your_secret_key'
     DEBUG = True
-    MODEL_PATH = 'D:/suan/Code_suan/RF_filt.pkl'
+    MODEL_PATH = 'model_optimize/pkl/RF_filt.pkl'

+ 1 - 1
api/app/model.py

@@ -3,7 +3,7 @@ import pandas as pd
 
 # 加载模型
 def load_model():
-    with open('D:\suan\Code_suan\model_optimize\pkl\RF_filt.pkl', 'rb') as f:
+    with open('model_optimize/pkl/RF_filt.pkl', 'rb') as f:
         return pickle.load(f)
 
 # 初始化模型

+ 1 - 0
api/app/routes.py

@@ -5,6 +5,7 @@ import pandas as pd
 # 创建蓝图 (Blueprint),用于分离路由
 bp = Blueprint('routes', __name__)
 
+
 # 路由:预测
 @bp.route('/predict', methods=['POST'])
 def predict_route():

+ 1 - 3
api/model_optimize/RF_filt.py

@@ -1,5 +1,5 @@
 # '''
-#模型筛选
+# 模型筛选
 # '''
 
 ## 导入常用基本包
@@ -14,8 +14,6 @@ from sklearn.ensemble import RandomForestRegressor
 from sklearn.model_selection import cross_val_score,cross_val_predict
 from sklearn.model_selection import train_test_split
 from sklearn.metrics import mean_squared_error
-import numpy as np
-import pandas as pd
 
 ## 导入常用辅助函数
 from sklearn.model_selection import train_test_split

+ 2 - 1
app.json

@@ -5,7 +5,8 @@
     "pages/Visualization/Visualization",
     "pages/Calculate/Calculate",
     "pages/Staff/Staff",
-    "pages/Calculation/Calculation"
+    "pages/Calculation/Calculation",
+    "pages/Result/Result"
   ],
   "window": {
     "navigationBarTitleText": "登入页面",

+ 0 - 0
api/environment.yml → environment.yml


+ 30 - 11
pages/Calculation/Calculation.js

@@ -114,15 +114,34 @@ Page({
   // 计算方法
   calculate: function() {
     console.log('开始计算...');
-    console.log('有机质含量:', this.data.OM);
-    console.log('土壤粘粒重量:', this.data.CL);
-    console.log('阳离子交换量:', this.data.CEC);
-    console.log('氢离子含量', this.data.H);
-    console.log('铵离子含量:', this.data.HN);
-    console.log('铝离子含量:', this.data.Al);
-    console.log('游离氧化铝含量:', this.data.free_alumina);
-    console.log('游离氧化铁含量:', this.data.free_iron_oxides);
-    console.log('采样地点:', this.data.collection_location);
-    console.log('采样时间:', this.data.collection_date);
+    const data = {
+      organic_matter: this.data.OM,
+      chloride: this.data.CL,
+      cec: this.data.CEC,
+      h_concentration: this.data.H,
+      hn: this.data.HN,
+      al_concentration: this.data.Al,
+      free_alumina: this.data.free_alumina,
+      free_iron: this.data.free_iron_oxides,
+      delta_ph: this.data.targetPH - this.data.initialPH // 假设delta_ph是目标pH和初始pH的差值
+    };
+  
+    wx.request({
+      url: 'http://localhost:5000/predict',
+      method: 'POST',
+      data: JSON.stringify(data),
+      header: {
+        'content-type': 'application/json' 
+      },
+      success: (res) => {
+        console.log('预测结果:', res.data.predictions);
+        wx.navigateTo({
+          url: `/pages/Result/Result?result=${encodeURIComponent(JSON.stringify(res.data.predictions))}`
+        });
+      },
+      fail: (error) => {
+        console.error('请求失败:', error);
+      }
+    });
   }
-});
+})

+ 0 - 1
pages/Calculation/Calculation.wxml

@@ -1,4 +1,3 @@
-<!-- pages/Calculation/Calculation.wxml -->
 <view class="page-body">
   <view class="white-box">
     <view class="page-section-title">有机质含量 (OM g/kg):</view>

+ 17 - 0
pages/Result/Result.js

@@ -0,0 +1,17 @@
+Page({
+  data: {
+    result: '', // 用于存储计算结果
+  },
+
+  onLoad: function(options) {
+    const encodedResult = options.result;
+    // 解码URL编码的字符串
+    const decodedResult = decodeURIComponent(encodedResult);
+    // 将解码后的字符串转换为JSON对象
+    const resultArray = JSON.parse(decodedResult);
+    // 更新页面数据以显示结果
+    this.setData({
+      result: resultArray[0].toString()
+    });
+  },
+});

+ 5 - 0
pages/Result/Result.json

@@ -0,0 +1,5 @@
+{
+  "navigationBarTitleText": "结果页面",
+  "navigationBarBackgroundColor": "#00AF92",
+  "navigationBarTextStyle": "white"  
+}

+ 12 - 0
pages/Result/Result.wxml

@@ -0,0 +1,12 @@
+<view class="container">
+  <page-meta>
+    <navigation-bar title="结果" back="{{true}}" color="black" background="#FFF"></navigation-bar>
+  </page-meta>
+  
+  <view class="page-body">
+    <view class="result-container">
+      <view class="result-title">计算结果</view>
+      <view class="result-content">{{result}}</view>
+    </view>
+  </view>
+</view>

+ 19 - 0
pages/Result/Result.wxss

@@ -0,0 +1,19 @@
+.page-body {
+  padding: 20px;
+}
+
+.result-container {
+  background-color: #f2f2f2;
+  padding: 20px;
+  border-radius: 10px;
+}
+
+.result-title {
+  font-size: 24px;
+  font-weight: bold;
+  margin-bottom: 20px;
+}
+
+.result-content {
+  font-size: 18px;
+}