Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ankhyk committed Mar 4, 2024
1 parent 356e8c7 commit 4e1429e
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# deap.000
# deap.exe
ExcelData.txt
/2024*
AutoDeap.exe
27 changes: 27 additions & 0 deletions AutoDeapSettings.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[General]
; 表格数据文件位置
ExcelDataFileName = ExcelData.txt
; 最终数据为表格文件
ExcelOutputFileName = ExcelOutput.csv
; 在 output 文件中需要提取的数据的类,类之间用|分隔
OutputCategoryList = EFFICIENCY SUMMARY|SUMMARY OF INPUT TARGETS|SUMMARY OF INPUT SLACKS

[instruction]
; eg1-dta.txt DATA FILE NAME
; 已默认设置为 $1.txt
; eg1-out.txt OUTPUT FILE NAME
; 已默认设置为 $1.yaml
; 5 NUMBER OF FIRMS
; 会自动填入
; 1 NUMBER OF TIME PERIODS
TimePeriods = 1
; 1 NUMBER OF OUTPUTS
Outputs = 2
; 2 NUMBER OF INPUTS
Inputs = 3
; 0 0=INPUT AND 1=OUTPUT ORIENTATED
InputOrOutput = 0
; 0 0=CRS AND 1=VRS
CRS_VRS = 1
; 0 0=DEA(MULTI-STAGE), 1=COST-DEA, 2=MALMQUIST-DEA, 3=DEA(1-STAGE), 4=DEA(2-STAGE)
Model = 0
4 changes: 4 additions & 0 deletions DEAP.000
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3 START-UP VALUES FOR DEAP PROGRAM
0.000001 EPS - MIN VALUE FOR INEQUALITY
1 IX - 1=PRINT FIRM BY FIRM RESULTS, 0=SUMMARY TABLES ONLY

Binary file added DEAP.EXE
Binary file not shown.
182 changes: 182 additions & 0 deletions deap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
const fs = require("fs");
const { exec } = require("child_process");

// ----------------------------------------在此填入参数-----------------------------------------------
// 表格数据文件位置
const excelDataFileName = `ExcelData.txt`;
// 最终数据为表格文件
const excelOutputFileName = `ExcelOutput.csv`;
// 在 output 文件中需要提取的数据的类
const outputCategoryList = [
// 数据组合时,越上面的数据的位置越排左边/前
// !!!收尾不能有空格!!!
'EFFICIENCY SUMMARY',
'SUMMARY OF INPUT TARGETS',
'SUMMARY OF INPUT SLACKS',
];
// eg1-ins.txt
let insObj = {
// eg1-dta.txt DATA FILE NAME
// 已默认设置为 $1.txt
// eg1-out.txt OUTPUT FILE NAME
// 已默认设置为 $1.yaml
// 5 NUMBER OF FIRMS
// 会自动填入
// 1 NUMBER OF TIME PERIODS
TimePeriods: 1,
// 1 NUMBER OF OUTPUTS
Outputs: 2,
// 2 NUMBER OF INPUTS
Inputs: 3,
// 0 0=INPUT AND 1=OUTPUT ORIENTATED
InputOrOutput: 0,
// 0 0=CRS AND 1=VRS
CRS_VRS: 1,
// 0 0=DEA(MULTI-STAGE), 1=COST-DEA, 2=MALMQUIST-DEA, 3=DEA(1-STAGE), 4=DEA(2-STAGE)
Model: 0,
}
// ----------------------------------------在此填入参数-----------------------------------------------

const excelDataFilePath = `${process.cwd()}/${excelDataFileName}`;
let data = fs.readFileSync(excelDataFilePath, "utf-8").toString();

data = data.split("\r\n").map((v) => v.replace(" ", "").split("\t"));
const categoryList = new Set();

let categoryOld = "";
for (const dataArr of data) {
let category = dataArr[0];
if (category === "") break;
categoryList.add(category);
const dataFilePath = `${process.cwd()}/${category}.txt`;
const insFilePath = `${process.cwd()}/${category}`;
const firmsReg = new RegExp(`${category},`, "g");
const firms = data.toString().match(firmsReg)?.length;
insObj.Firms = firms;
if (category !== categoryOld) {
fs.writeFileSync(dataFilePath, "");
const ins =
`${category}.txt\n` +
`${category}.yaml\n` +
`${insObj.Firms}\n` +
`${insObj.TimePeriods}\n` +
`${insObj.Outputs}\n` +
`${insObj.Inputs}\n` +
`${insObj.InputOrOutput}\n` +
`${insObj.CRS_VRS}\n` +
`${insObj.Model}`;
fs.writeFileSync(insFilePath, ins);
}
const dataStr = dataArr.toString().slice(5).replaceAll(",", " ");
fs.appendFileSync(dataFilePath, `${dataStr}\n`);
categoryOld = category;
}

// 对所有数据执行 deap.exe
const exePath = `${process.cwd()}/DEAP.EXE`;

for (const category of categoryList) {
while (true) {
if (
fs.existsSync(`${process.cwd()}/${category}`) &&
fs.existsSync(`${process.cwd()}/${category}.txt`)
) {
break;
}
}
// 创建子进程
const childProcess = exec(exePath, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
});

// 向子进程输入
const inputText = `${category}\n`;
childProcess.stdin.write(inputText);
}

// 从 out 文件找到所需数据
const excelOutputFilePath = `${process.cwd()}/${excelOutputFileName}`;
fs.writeFileSync(excelOutputFilePath, "");
for (const category of categoryList) {
const outputFilePath = `${process.cwd()}/${category}.yaml`;
while (true) {
if (fs.existsSync(outputFilePath)) {
break;
}
}

let outputData = fs.readFileSync(outputFilePath, "utf-8").toString();
outputData = outputData.slice(1).replaceAll(`\r\n`, `\n`);
outputData = outputData.replaceAll(/\n +/g, "\n");
outputData = outputData.replaceAll(/ +\n/g, "\n");
let allCategoryData = outputData.split(`\n\n\n`);

let data = [];
for (const column of outputCategoryList) {
for (const category of allCategoryData) {
if (category.slice(0, column.length) === column) {
let categoryData = category.split(`\n`);
categoryData = categoryData.filter((v) => !isNaN(Number(v[0])));
data.push(categoryData);
}
}
}

// 整理数据成 csv 文件
let result = [];
for (let i = 0; i < insObj.Firms; i++) {
let element = `${category} ${data[0][i]}`;
for (let y = 1; y < data.length; y++) {
const eachDataElement = data[y];
element += " " + eachDataElement[i];
}
element = element.replaceAll(/ +/g, ",");
fs.appendFileSync(`${outputFilePath.slice(0, -4)}csv`, `${element}\n`);
fs.appendFileSync(excelOutputFilePath, `${element}\n`);
}
}

// 整理目录
// 获取当前时间
const date = new Date().toLocaleString("zh-CN");
// 此次运行目录
const dirName = date
.replaceAll("/", "-")
.replaceAll(" ", "_")
.replaceAll(":", "-");

fs.mkdirSync(`${dirName}`);
fs.mkdirSync(`${dirName}/data`);
// 移动文件
for (const category of categoryList) {
const fileNameObj = {
dtaName: `${category}.txt`,
insName: `${category}`,
outName: `${category}.yaml`,
csvName: `${category}.csv`,
};
for (const fileCategory in fileNameObj) {
const fileName = fileNameObj[fileCategory];
while (true) {
if (fs.existsSync(`${process.cwd()}/${fileName}`)) {
break;
}
}
fs.renameSync(
`${process.cwd()}/${fileName}`,
`${process.cwd()}/${dirName}/data/${fileName}`
);
}
}
fs.copyFileSync(
`${process.cwd()}/${excelDataFileName}`,
`${process.cwd()}/${dirName}/${excelDataFileName}`
);
fs.renameSync(
excelOutputFilePath,
`${process.cwd()}/${dirName}/${excelOutputFileName}`
);
console.log(`文件已保存于目录:${process.cwd()}\\${dirName}`);
161 changes: 161 additions & 0 deletions deapIni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
const fs = require("fs");
const { exec } = require("child_process");
const ini = require("ini");

const iniData = fs
.readFileSync(`${process.cwd()}/AutoDeapSettings.ini`, "utf-8")
.toString();
const setting = ini.parse(iniData);
const excelDataFileName = setting["General"].ExcelDataFileName;
const excelOutputFileName = setting["General"].ExcelOutputFileName;
const outputCategoryList = setting["General"].OutputCategoryList.split("|");
const insObj = setting["instruction"];

const excelDataFilePath = `${process.cwd()}/${excelDataFileName}`;
let data = fs.readFileSync(excelDataFilePath, "utf-8").toString();

data = data.split("\r\n").map((v) => v.replace(" ", "").split("\t"));
let categoryList = new Set();

let categoryOld = "";
for (const dataArr of data) {
let category = dataArr[0];
if (category === "") break;
categoryList.add(category);
const dataFilePath = `${process.cwd()}/${category}.txt`;
const insFilePath = `${process.cwd()}/${category}`;
const firmsReg = new RegExp(`${category},`, "g");
const firms = data.toString().match(firmsReg)?.length;
insObj.Firms = firms;
if (category !== categoryOld) {
fs.writeFileSync(dataFilePath, "");
const ins =
`${category}.txt\n` +
`${category}.yaml\n` +
`${insObj.Firms}\n` +
`${insObj.TimePeriods}\n` +
`${insObj.Outputs}\n` +
`${insObj.Inputs}\n` +
`${insObj.InputOrOutput}\n` +
`${insObj.CRS_VRS}\n` +
`${insObj.Model}`;
fs.writeFileSync(insFilePath, ins);
}
const dataStr = dataArr.toString().slice(5).replaceAll(",", " ");
fs.appendFileSync(dataFilePath, `${dataStr}\n`);
categoryOld = category;
}

// 对所有数据执行 deap.exe
const exePath = `${process.cwd()}/DEAP.EXE`;

categoryList = Array.from(categoryList);
for (const category of categoryList) {
while (true) {
const index = categoryList.indexOf(category);
if (
fs.existsSync(`${process.cwd()}/${category}`) &&
fs.existsSync(`${process.cwd()}/${category}.txt`)
) {
if (index === 0) break;
if (fs.existsSync(`${process.cwd()}/${categoryList[index - 1]}.yaml`)) {
break;
}
}
}
// 创建子进程
const childProcess = exec(exePath, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
});

// 向子进程输入
const inputText = `${category}\n`;
childProcess.stdin.write(inputText);
}

// 从 out 文件找到所需数据
const excelOutputFilePath = `${process.cwd()}/${excelOutputFileName}`;
fs.writeFileSync(excelOutputFilePath, "");
for (const category of categoryList) {
const outputFilePath = `${process.cwd()}/${category}.yaml`;
while (true) {
if (fs.existsSync(outputFilePath)) {
break;
}
}

let outputData = fs.readFileSync(outputFilePath, "utf-8").toString();
outputData = outputData.slice(1).replaceAll(`\r\n`, `\n`);
outputData = outputData.replaceAll(/\n +/g, "\n");
outputData = outputData.replaceAll(/ +\n/g, "\n");
let allCategoryData = outputData.split(`\n\n\n`);

let data = [];
for (const column of outputCategoryList) {
for (const category of allCategoryData) {
if (category.slice(0, column.length) === column) {
let categoryData = category.split(`\n`);
categoryData = categoryData.filter((v) => !isNaN(Number(v[0])));
data.push(categoryData);
}
}
}

// 整理数据成 csv 文件
let result = [];
for (let i = 0; i < insObj.Firms; i++) {
let element = `${category} ${data[0][i]}`;
for (let y = 1; y < data.length; y++) {
const eachDataElement = data[y];
element += " " + eachDataElement[i];
}
element = element.replaceAll(/ +/g, ",");
fs.appendFileSync(`${outputFilePath.slice(0, -4)}csv`, `${element}\n`);
fs.appendFileSync(excelOutputFilePath, `${element}\n`);
}
}

// 整理目录
// 获取当前时间
const date = new Date().toLocaleString("zh-CN");
// 此次运行目录
const dirName = date
.replaceAll("/", "-")
.replaceAll(" ", "_")
.replaceAll(":", "-");

fs.mkdirSync(`${dirName}`);
fs.mkdirSync(`${dirName}/data`);
// 移动文件
for (const category of categoryList) {
const fileNameObj = {
dtaName: `${category}.txt`,
insName: `${category}`,
outName: `${category}.yaml`,
csvName: `${category}.csv`,
};
for (const fileCategory in fileNameObj) {
const fileName = fileNameObj[fileCategory];
while (true) {
if (fs.existsSync(`${process.cwd()}/${fileName}`)) {
break;
}
}
fs.renameSync(
`${process.cwd()}/${fileName}`,
`${process.cwd()}/${dirName}/data/${fileName}`
);
}
}
fs.copyFileSync(
`${process.cwd()}/${excelDataFileName}`,
`${process.cwd()}/${dirName}/${excelDataFileName}`
);
fs.renameSync(
excelOutputFilePath,
`${process.cwd()}/${dirName}/${excelOutputFileName}`
);
console.log(`文件已保存于目录:${process.cwd()}\\${dirName}`);
Loading

0 comments on commit 4e1429e

Please sign in to comment.