void GradShim3D::acquire3DData() { try { // 清空之前的数据 m3DData.clear(); auto delay = {…
- User
void GradShim3D::acquire3DData() { try { // 清空之前的数据 m_3DData.clear(); auto delay = [](int ms) { QEventLoop loop; QTimer::singleShot(ms, &loop, &QEventLoop::quit); loop.exec(); };
// 设置进度条 auto numGx = ui->inputNumGx->text().toInt(); auto numGy = ui->inputNumGy->text().toInt(); auto gx_0 = ui->inputGx_0->text().toDouble(); auto gy_0 = ui->inputGy_0->text().toDouble(); auto deltaGx = ui->inputDeltaGx->text().toDouble(); auto deltaGy = ui->inputDeltaGy->text().toDouble(); int totalSteps = numGx * numGy; ui->progressBar->setRange(0, totalSteps); ui->progressBar->setValue(0); // 开始嵌套循环采集数据 for (int i = 0; i < numGy; ++i) { double gy = gy_0 + deltaGy * i; setGyValue(gy); // 设置 Gy 电流值 delay(10); std::vector<GradShimData> rowData; for (int j = 0; j < numGx; ++j) { double Gx = gx_0 + deltaGx * j; setGxValue(Gx); // 设置 Gx 电流值 // 等待一段时间,确保电流稳定 delay(1000); // 采集 1D 数据 auto data = acquire1DData(); if (!data) { RyMessageBox::warning("Acquire stopped with error"); return; } rowData.push_back(data.value()); // 更新进度条 int progressValue = i * numGx + j + 1; ui->progressBar->setValue(progressValue); qApp->processEvents(); } m_3DData.push_back(rowData); } // 实验完成后,处理数据 // process3DData(); } catch (...) { auto message = RyException::handleException(); RyMessageBox::warning(message.c_str()); Log::warning() << message; }}
struct GradShimData { QVector<NmrParser::Datum1D> fid; // fid为原始数据 //QVector<double> f; // f为FFT后数据的横坐标数值 //QVector<NmrParser::Datum1D> fft; // = fft(fid) void clear(); }; // 3D 数据,使用二维 vector 存储 std::vector<std::vector<GradShimData>> m_3DData;%3D Gradient Shim work flow
%定义物理常数 gamma = 2pi4258; %unit rad/s/gauss, 1H的旋磁比
%实验前准备参数如下: %1) DeltaGx, X匀场线圈电流扫描步进 %2) DeltaGy, Y匀场线圈电流扫描步进 %3) NumGx, X匀场线圈电流扫描点数 %4) NumGy, Y匀场线圈电流扫描点数 %5) Gx_0, X匀场线圈电流扫描起点,unint uA %6) Gy_0, X匀场线圈电流扫描起点,unint uA %7) Gx_cov, X匀场线圈磁场梯度转换系数, gauss/cm/A %8) Gy_cov, X匀场线圈磁场梯度转换系数, gauss/cm/A %9) fs, 采样率 %10) G2, reaout Gradient 的强度 %11) TE, 演化时间
%定义获取3D函数的实验 function out = Acquire_3D_data(Exp_parameter,NumGx,NumGy) out = cell(NumGy,NumGx); for i=1:NumGy Gy = Gy_0+DelaGy*(i-1); SetGy; %下发Y匀场线圈电流值 for j=1:NumGx Gx = Gx_0+DeltaGx*(j-1); SetGx; %下发X匀场线圈电流值 out{i,j} = Acquire_1D_data(Exp_parameter); % out是一个3D数据 end end end
编写一个函数来保存3d数据,保存成csv。 fid[0] ... fid[1] ...
重复往下追加,第一列不用表头,这样默认约定。增加扩展性,后面可能增加其他数据,如freq,fft等
- Assistant
Certainly! Below is a C++ function that saves the
m_3DDatamember variable to a CSV file, following your specified conventions. The function appends thefiddata of eachGradShimDatainstance vertically, without a header for the first column. It is designed to be extensible, so you can easily add more data likefreqandfftin the future if needed.void GradShim3D::save3DDataToCsv(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { RyMessageBox::warning("Could not open file for writing"); return; } QTextStream out(&file); // Iterate over the 3D data for (const auto& rowData : m_3DData) { for (const auto& gradData : rowData) { // Write the fid data vertically for (const auto& datum : gradData.fid) { out << datum.real << "\n"; // Write the real part of the FID data } // Optionally, you can add a separator between different datasets // out << "\n"; } } file.close(); }Explanation:
- File Handling: We use
QFileandQTextStreamto handle file operations safely and efficiently. - Iteration: The function iterates over each
GradShimDatainm_3DData. - Data Writing: For each
gradData.fid, which is aQVector<NmrParser::Datum1D>, we write therealpart of each datum to the file. - Vertical Appending: The data is appended downwards (vertically) as you requested.
- No Header: The first column doesn't have a header, adhering to your convention.
- Extensibility: The function is modular, so you can easily modify it to include additional data like
freqorfftin the future.
Adding Extensibility for Future Data:
If you plan to add more data fields like
freqorfft, you can modify the function as follows:void GradShim3D::save3DDataToCsv(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { RyMessageBox::warning("Could not open file for writing"); return; } QTextStream out(&file); // Iterate over the 3D data for (const auto& rowData : m_3DData) { for (const auto& gradData : rowData) { // Write the fid data vertically for (int i = 0; i < gradData.fid.size(); ++i) { const auto& datum = gradData.fid[i]; out << datum.real; // Write the real part of the FID data // If you have frequency data // out << "," << gradData.freq[i]; // If you have FFT data // out << "," << gradData.fft[i].real(); out << "\n"; // Newline for the next data point } // Optionally, you can add a separator between different datasets // out << "\n"; } } file.close(); }Note: You'll need to make sure that any additional data you include is properly initialized and synchronized in size with your
fiddata.Usage Example:
After running your acquisition function
acquire3DData(), you can save the data by calling:gradShim3DInstance.save3DDataToCsv("output.csv");Additional Suggestions:
- Error Handling: Consider adding more detailed error handling, such as checking if
m_3DDatais empty before attempting to write. - Progress Feedback: If the dataset is large, you might provide feedback during the saving process.
- Separator Between Datasets: If you want to distinguish between different
GradShimDataentries, you can add a separator like an empty line.
Optimizing for Large Datasets:
For very large datasets, writing to the file can be optimized:
- Buffered Writing: Collect strings in a
QStringListand then write them all at once. - Binary Format: If file size becomes an issue, consider using a binary format instead of CSV.
Example with Buffered Writing:
void GradShim3D::save3DDataToCsv(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { RyMessageBox::warning("Could not open file for writing"); return; } QTextStream out(&file); QStringList lines; // Iterate over the 3D data for (const auto& rowData : m_3DData) { for (const auto& gradData : rowData) { // Collect the fid data for (const auto& datum : gradData.fid) { lines << QString::number(datum.real); } } } // Write all at once out << lines.join("\n"); file.close(); }This approach may reduce the number of I/O operations.
I hope this helps you save your 3D data as per your requirements!
- File Handling: We use