Compare commits

...

2 Commits

Author SHA1 Message Date
xiaoguangbin
5719c210ca fix:自建台站beta分析 2024-08-22 11:19:29 +08:00
xiaoguangbin
356bd94a26 fix:修复新beta的保存接口执行非常慢的问题 2024-08-20 18:25:05 +08:00
8 changed files with 334 additions and 13 deletions

View File

@ -41,4 +41,6 @@ public interface RedisConstant {
String EMAIL_MSG_ID = "email_msg_id";
String UNDEAL_FILE = "Undeal:";
String SELF_PARAMETER = "self_parameter";
}

View File

@ -0,0 +1,99 @@
package org.jeecg.common.parameter;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.properties.ParameterProperties;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.entity.vo.NuclideLine;
import org.jeecg.modules.entity.vo.SelfParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
@Configuration
public class SelfParameterInit {
@Autowired
private ParameterProperties parameterProperties;
@Autowired
private RedisUtil redisUtil;
@Bean
public void readSelfMDCParameter() {
//配置文件路径
String filePath = parameterProperties.getFilePath()+ File.separator + parameterProperties.getSelfParameterFile();
SelfParameter selfParameter = new SelfParameter();
try {
//创建一个文档解析器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//创建文档解析器
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
//读取xml文件生成一个文档
Document document = documentBuilder.parse(filePath);
if (Objects.nonNull(document)){
//获取文档的根元素
Element element = document.getDocumentElement();
//获取根元素的子节点
NodeList docChildNodes = element.getChildNodes();
//判断文件内的节点是否大于0
if (Objects.nonNull(docChildNodes) && docChildNodes.getLength() > 0) {
//遍历文件节点读取内容
for (int i=0; i<docChildNodes.getLength(); i++) {
//获取节点信息
Node node = docChildNodes.item(i);
//判断节点名称是否是 P
if (node.getNodeName().equalsIgnoreCase("nuclide")) {
//获取节点下的子节点信息
NodeList childNodes = node.getChildNodes();
//如果子节点不为空
if (Objects.nonNull(childNodes) && childNodes.getLength() > 0) {
//遍历子节点信息 将核素信息封存到缓存中
for (int j=0; j<childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
//获取节点属性信息
NamedNodeMap attributes = childNode.getAttributes();
//判断节点属性信息是否为空
if (Objects.nonNull(attributes)) {
NuclideLine nuclide = new NuclideLine();
//遍历属性信息
for (int k=0; k<attributes.getLength(); k++) {
//根据顺序读取属性
Node attribute = attributes.item(k);
if (attribute.getNodeName().equalsIgnoreCase("nuclide_name")) {
nuclide.setName(attribute.getNodeValue());
} else if (attribute.getNodeName().equalsIgnoreCase("yield")) {
nuclide.setYield(Double.valueOf(attribute.getNodeValue()));
} else if (attribute.getNodeName().equalsIgnoreCase("energy")) {
nuclide.setEnergy(Double.valueOf(attribute.getNodeValue()));
}
}
if (StrUtil.isNotBlank(nuclide.getName()) && Objects.nonNull(nuclide.getEnergy())) {
selfParameter.getNuclideMap().put(nuclide.getName(), nuclide);
}
}
}
}
}
}
}
}
redisUtil.set(RedisConstant.SELF_PARAMETER, selfParameter);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -26,4 +26,12 @@ public class ParameterProperties implements Serializable {
*/
private String dbPath;
private String SelfParameterFile;
private String MDCParameterFile;
private String SystemManagerFile;
private String parameterFile;
}

View File

@ -0,0 +1,17 @@
package org.jeecg.modules.entity.vo;
import lombok.Data;
import org.jeecg.modules.base.dto.NuclideInfo;
import java.io.Serializable;
import java.util.HashMap;
@Data
public class SelfParameter implements Serializable {
// 存储核素信息
private HashMap<String, NuclideLine> nuclideMap;
public SelfParameter(){
nuclideMap = new HashMap<>();
}
}

View File

@ -84,4 +84,5 @@ public class EnergySpectrumHandler {
public static native List<Double> GetFittingData(List<Double> data, String fitType, List<Double> fittingPara);
public static native String selfBgAnalyse(String PHDPath, String param);
}

View File

@ -9,6 +9,8 @@ import com.google.common.cache.Cache;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.cache.SelfCache;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.properties.ParameterProperties;
import org.jeecg.modules.base.abstracts.AbstractLogOrReport;
import org.jeecg.modules.base.enums.CalName;
import org.jeecg.modules.base.enums.DataTypeAbbr;
@ -25,9 +27,15 @@ import org.jeecg.modules.native_jni.struct.EnergySpectrumStruct;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.ParseException;

View File

@ -38,6 +38,10 @@ public class GardsXeResultsSpectrum implements Serializable {
* 感兴趣区MDC
*/
private Double mdc;
/**
* 感兴趣区MDC
*/
private Double mda;
/**
* 感兴趣区LC
*/

View File

@ -3,6 +3,7 @@ package org.jeecg.modules.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
@ -21,6 +22,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.math3.fitting.WeightedObservedPoints;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cache.SelfCache;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.constant.SelfStationConstant;
import org.jeecg.common.constant.StringConstant;
import org.jeecg.common.constant.enums.FileTypeEnum;
@ -49,11 +51,9 @@ import org.jeecg.modules.native_jni.struct.CalValuesOut;
import org.jeecg.modules.native_jni.struct.EnergySpectrumStruct;
import org.jeecg.modules.service.*;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@ -487,7 +487,7 @@ public class SelfStationServiceImpl extends AbstractLogOrReport implements ISelf
String detFilePath = path + StringPool.SLASH + detFileName;
//返回结果map
Map<String, Object> map = new HashMap<>();
//获取sample分析后的对象
//获取det分析后的对象
EnergySpectrumStruct struct = selfStationUtil.getSourceData(detFilePath, "det", selfStationData);
if (Objects.nonNull(struct)) {
selfStationData.setDetStruct(struct);
@ -506,7 +506,7 @@ public class SelfStationServiceImpl extends AbstractLogOrReport implements ISelf
String qcFilePath = path + StringPool.SLASH + qcFileName;
//返回结果map
Map<String, Object> map = new HashMap<>();
//获取sample分析后的对象
//获取QC分析后的对象
EnergySpectrumStruct struct = selfStationUtil.getSourceData(qcFilePath, "qc", selfStationData);
if (Objects.nonNull(struct)) {
selfStationData.setQcStruct(struct);
@ -2547,7 +2547,7 @@ public class SelfStationServiceImpl extends AbstractLogOrReport implements ISelf
PHDFile phdFour = sampleVueData.getROIFourPHDFile();
Map<String, NuclideLines> nuclideLinesMap = (Map<String, NuclideLines>) redisUtil.get(userName+StringPool.DASH+phdOne.getHeader().getSystem_type()+"-self");
Map<String, Object> roiMap = new HashMap<>();
Map<String, Object> resultMap = new HashMap<>();
try {
boolean bROI1 = selfStationUtil.checkROIRenew(sampleVueData, SelfStationConstant.ROI1);
boolean bROI2 = selfStationUtil.checkROIRenew(sampleVueData, SelfStationConstant.ROI2);
@ -2564,20 +2564,17 @@ public class SelfStationServiceImpl extends AbstractLogOrReport implements ISelf
}
// 如果参数修改 或者 修改某个ROI 只重新分析某个gamma
if (flag != 0 || bROI1) {
roiMap.put(SelfStationConstant.ROI1, this.gammaAnalyse(processKey, phdOne, nuclideLinesMap, colorMap, flag));
resultMap.put(SelfStationConstant.ROI1, this.gammaAnalyse(processKey, phdOne, nuclideLinesMap, colorMap, flag));
}
if (flag != 0 || bROI2) {
roiMap.put(SelfStationConstant.ROI2, this.gammaAnalyse(processKey, phdTwo, nuclideLinesMap, colorMap, flag));
resultMap.put(SelfStationConstant.ROI2, this.gammaAnalyse(processKey, phdTwo, nuclideLinesMap, colorMap, flag));
}
if (flag != 0 || bROI3) {
roiMap.put(SelfStationConstant.ROI3, this.gammaAnalyse(processKey, phdThree, nuclideLinesMap, colorMap, flag));
resultMap.put(SelfStationConstant.ROI3, this.gammaAnalyse(processKey, phdThree, nuclideLinesMap, colorMap, flag));
}
if (flag != 0 || bROI4) {
roiMap.put(SelfStationConstant.ROI4, this.gammaAnalyse(processKey, phdFour, nuclideLinesMap, colorMap, flag));
resultMap.put(SelfStationConstant.ROI4, this.gammaAnalyse(processKey, phdFour, nuclideLinesMap, colorMap, flag));
}
// roiMap.put(SelfStationConstant.ROI2, this.gammaAnalyse(processKey, phdTwo, nuclideLinesMap, colorMap, bROI2));
// roiMap.put(SelfStationConstant.ROI3, this.gammaAnalyse(processKey, phdThree, nuclideLinesMap, colorMap, bROI3));
// roiMap.put(SelfStationConstant.ROI4, this.gammaAnalyse(processKey, phdFour, nuclideLinesMap, colorMap, bROI4));
// 用于下次分析判断是否更新
sampleVueData.setUsedROIOneBetaStart(sampleVueData.getROIOneBetaStart());
sampleVueData.setUsedROIOneBetaStop(sampleVueData.getROIOneBetaStop());
@ -2601,16 +2598,201 @@ public class SelfStationServiceImpl extends AbstractLogOrReport implements ISelf
sampleVueData.setMapEnerPara(phdOne.getMapEnerPara());
sampleVueData.setMapResoPara(phdOne.getMapResoPara());
sampleVueData.setMapEffiPara(phdOne.getMapEffiPara());
// 调用beta分析
List<GardsXeResultsSpectrum> betaResult = this.betaAnalyse(selfStationData, nuclideLinesMap);
resultMap.put("XeData", betaResult);
result.setSuccess(true);
result.setResult(roiMap);
result.setResult(resultMap);
selfStationData.setBAnalyed(true);
} catch (RuntimeException e) {
Log.error("analyse error", e);
result.error500(StrUtil.replace(e.getMessage(), "%s", "ROI"));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return result;
}
/**
* beta分析
* @param selfStationData 自建台站缓存数据
* @param nuclideLinesMap 核素信息
* @throws JsonProcessingException
*/
private List<GardsXeResultsSpectrum> betaAnalyse(SelfStationData selfStationData, Map<String, NuclideLines> nuclideLinesMap) throws JsonProcessingException {
SelfStationVueData sampleData = selfStationData.getSampleVueData();
EnergySpectrumStruct struct = selfStationData.getSampleStruct();
String phdPath = selfStationData.getSampleFilePathName();
// 获取自建台站参数
SelfParameter selfParameter = (SelfParameter) redisUtil.get(RedisConstant.SELF_PARAMETER);
HashMap<String, NuclideLine> nuclideMap = selfParameter.getNuclideMap();
// 获取峰面积半衰期发射几率
HashMap<String, Object> nuclideParam = this.getBetaAnalyseNuclideParam(sampleData, nuclideMap, struct.POI_G_y1, struct.POI_G_y2, nuclideLinesMap);
Console.log(JSON.toJSONString(nuclideParam));
// 调用beta分析算法
String resultStr = EnergySpectrumHandler.selfBgAnalyse(phdPath, JSON.toJSONString(nuclideParam));
List<GardsXeResultsSpectrum> xeDataList = new ArrayList<>();
if (StrUtil.isNotBlank(resultStr)) {
xeDataList = JSON.parseArray(resultStr,GardsXeResultsSpectrum.class);
xeDataList.forEach(GardsXeResultsSpectrum::getMdc);
if (CollectionUtils.isNotEmpty(xeDataList)){
for (GardsXeResultsSpectrum xeData:xeDataList) {
Double conc = xeData.getConc();
Double mdc = xeData.getMdc();
if (conc < 0){
xeData.setColor("red");
xeData.setNidFlag(0);
} else if (0<conc && conc < mdc) {
xeData.setColor("#ffcc30");
xeData.setNidFlag(0);
} else if (conc > mdc) {
xeData.setColor("green");
xeData.setNidFlag(1);
}
xeData.setMdc(Double.valueOf(NumberFormatUtil.numberSixLen(String.valueOf(xeData.getMdc()))));
xeData.setConc(Double.valueOf(NumberFormatUtil.numberSixLen(String.valueOf(xeData.getConc()))));
}
}
}
Console.log(resultStr);
return xeDataList;
}
/**
* 获取结算结果需要的参数峰面积半衰期发射几率
* @param gStart RoiLimit Gamma Start
* @param gStop RoiLimit Gamma Stop
* @param nuclideLinesMap 所有核素信息
*/
private HashMap<String, Object> getBetaAnalyseNuclideParam(SelfStationVueData sampleData, HashMap<String, NuclideLine> nuclideMap,
List<Double> gStart, List<Double> gStop, Map<String,
NuclideLines> nuclideLinesMap) {
HashMap<String, Object> param = Maps.newHashMap();
// 获取峰信息
List<PeakInfo> vPeak = null;
String mapKey = "";
// 遍历roiLimit
for (int g = 0; g < gStart.size(); g++) {
HashMap<String, Object> nuclideParam = Maps.newHashMap();
String nuclideName = "";
switch (g) {
case 0:
nuclideName = "Xe131M";
mapKey = "XE-131m";
vPeak = sampleData.getROIOnePHDFile().getVPeak();
break;
case 1:
nuclideName = "Xe133M";
mapKey = "XE-133m";
vPeak = sampleData.getROITwoPHDFile().getVPeak();
break;
case 2:
nuclideName = "Xe133";
mapKey = "XE-133";
vPeak = sampleData.getROIThreePHDFile().getVPeak();
break;
case 3:
nuclideName = "Xe135";
mapKey = "XE-135";
vPeak = sampleData.getROIFourPHDFile().getVPeak();
break;
}
// 发射几率
nuclideParam.put("yield", nuclideMap.get(nuclideName).getYield().toString());
// baseline 计数这里使用的energy是固定的 从配置文件中取出
nuclideParam.put("baseline",this.getBetaAnalyseBaseLineCount(sampleData, nuclideName,
nuclideMap.get(nuclideName).getEnergy())+"");
for (PeakInfo info : vPeak) {
// 峰核素的energy
double energy = info.energy;
// 找匹配roi范围的 energy
if (energy > gStart.get(g) && energy < gStop.get(g)) {
double area = info.area;
// 峰如果没有识别到核素则跳过
if (info.nuclides.contains(nuclideName)) {
NuclideLines nuclideLines = nuclideLinesMap.get(nuclideName);
Console.log("halflife:{}",nuclideLines.halflife);
// 峰面积
nuclideParam.put("area", area+"");
// 半衰期
nuclideParam.put("halflife", (nuclideLines.halflife / 24 / 60 / 60) +"");
break;
}
}
}
param.put(mapKey, nuclideParam);
}
return param;
}
/**
* 得到 baseline计数
* @param sampleData 谱数据
* @param nuclideName 核素名称
* @param energy 能量
* @return
*/
private double getBetaAnalyseBaseLineCount(SelfStationVueData sampleData,String nuclideName, Double energy) {
double baseLineCount = 0;
String currentText = null;
List<Double> vBase = null;
PHDFile phd = null;
// 获取baseline
switch (nuclideName) {
case "Xe131M":
phd = sampleData.getROIOnePHDFile();
break;
case "Xe133M":
phd = sampleData.getROITwoPHDFile();
break;
case "Xe133":
phd = sampleData.getROIThreePHDFile();
break;
case "Xe135":
phd = sampleData.getROIFourPHDFile();
break;
}
if (null == phd) {
return 0;
}
vBase = phd.getVBase();
currentText = StrUtil.isNotBlank(phd.getNewEner()) ? phd.getNewEner() : phd.getUsedEner();
// 得到baseline count范围
double fwhm = 0;
// 公式参数
ParameterInfo m_curParam = sampleData.getMapResoPara().get(currentText);
int p_size = m_curParam.getP().size()-1;
if(p_size >= 2 && m_curParam.getP().get(1) > 0 && m_curParam.getP().get(2) > 0) {
// Square root of polynomial: y = sqrt(a0+a1*x+a2*x^2+a3*x^3 )
fwhm = m_curParam.getP().get(1) + energy * m_curParam.getP().get(2);
// equation += "FWHM = ("+NumberFormatUtil.numberFormat(String.valueOf(m_curParam.getP().get(1)))+" + E * "+NumberFormatUtil.numberFormat(String.valueOf(m_curParam.getP().get(2)));
for(int i=3; i<=p_size; i++) {
fwhm += (Math.pow(energy, (i-1)) * m_curParam.getP().get(i));
// equation += " + E<sup style=\"vertical-align:super;\">"+(i-1)+"</sup> * "+NumberFormatUtil.numberFormat(String.valueOf(m_curParam.getP().get(i)));
}
fwhm = Math.pow(fwhm, 0.5);
// equation += ")<sup style=\"vertical-align:super;\">"+1+"/"+2+"</sup>";
}
// 通过energy带入到FHWM公式中将结果energy转化channel加减1.25之后取这个范围之内的baseline进行加和
List<Double> energyList = Lists.newLinkedList();
energyList.add(energy - (fwhm - 1.25));
CalValuesOut energyToChannel = CalValuesHandler.energyToChannel(energyList, phd.getUsedEnerPara().getP());
long begin = Math.round(energyToChannel.counts.get(0));
energyList = Lists.newLinkedList();
energyList.add(energy + (fwhm + 1.25));
energyToChannel = CalValuesHandler.energyToChannel(energyList, phd.getUsedEnerPara().getP());
long end = Math.round(energyToChannel.counts.get(0));
if (begin >= 0 && end <= vBase.size()) {
for (long i = begin; i < end; i++) {
baseLineCount += vBase.get((int)i);
}
}
return baseLineCount;
}
@Override
public Result InteractiveTool(Integer sampleId, String fileName, int gammaROINum, HttpServletRequest request) {
Result result = new Result();