BUG: CTF writer keeps only four decimal places of every float column
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
Hướng nghiên cứu
Trong Source/EbsdLib/IO/HKL/CtfReader.cpp, kiểm tra cách định dạng các cột float trong CtfReader::writeFile() quanh các dòng 1071–1075. Trước tiên, chạy cách tái hiện được mô tả trong issue, sau đó kiểm tra Source/Test/CtfReaderTest.cpp để xem bài kiểm tra round-trip được đề xuất. Công việc hoàn tất khi các giá trị Euler1, Euler2 và X được đọc lại chính xác như lúc lưu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Problem
CtfReader::writeFile() writes every floating-point column of a .ctf file with four decimal places. Values that need more digits change without an error: an Euler angle or X position of 1e-5 is written as 0.0000, and 12.3456789 is written as 12.3457. Reading the written file back gives different data than the reader held. CtfReader itself reads float columns with std::stof, so it would accept the full value.
Expected: a written float column reads back to the same float. Actual: values are rounded to four decimals, and values below 5e-5 become zero.
Occurs at 34844ed31dfb8660c67edb6e019ca708e04beeb4 (develop).
Steps to Reproduce
- Read a
.ctffile withCtfReader(the test below uses the test fixtureCtfReaderTest::USInputFile1). - Set the first Euler1 value to
1.0e-5f, the first Euler2 value to12.3456789fand the first X value to1.0e-5f. - Call
writeFile()to a new path. - Read that file with a new
CtfReaderand compare the three values with the stored floats.
Cause
The data rows are written with a fixed four-decimal format for every float column (CtfReader.cpp#L1071-L1075):
else if(1 == colInfo.aType) // float pointer
{
float* f32Ptr = reinterpret_cast<float*>(colInfo.ptr);
fprintf(f, "%0.4f\t", f32Ptr[counter]);
}
%0.4f keeps four digits after the decimal point, so the written text cannot hold a float that needs more. A float needs up to 9 significant digits to read back unchanged.
Verification Unit Test
File: Source/Test/CtfReaderTest.cpp. The test sets the original header from the fixture file before writing, so the written file can be read back (see Notes).
TEST_CASE("ebsdlib::CtfReaderTest::WriteFilePreservesFloatPrecision", "[EbsdLib][CtfReaderTest]")
{
CtfReader reader;
reader.setFileName(ebsdlib::unit_test::CtfReaderTest::USInputFile1);
REQUIRE(reader.readFile() == 0);
REQUIRE(reader.getNumberOfElements() > 0);
// Preserve fixture header line breaks to isolate float precision from header reconstruction.
std::ifstream input(ebsdlib::unit_test::CtfReaderTest::USInputFile1);
REQUIRE(input.is_open());
std::string header;
std::string line;
bool foundDataHeader = false;
while(std::getline(input, line))
{
header += line + "\n";
if(line.find("Phase\tX\tY\t") == 0)
{
foundDataHeader = true;
break;
}
}
REQUIRE(foundDataHeader);
reader.setOriginalHeader(header);
auto* euler1Ptr = static_cast<float*>(reader.getPointerByName(ebsdlib::Ctf::Euler1));
auto* euler2Ptr = static_cast<float*>(reader.getPointerByName(ebsdlib::Ctf::Euler2));
auto* xPtr = static_cast<float*>(reader.getPointerByName(ebsdlib::Ctf::X));
REQUIRE(euler1Ptr != nullptr);
REQUIRE(euler2Ptr != nullptr);
REQUIRE(xPtr != nullptr);
euler1Ptr[0] = 1.0e-5f;
euler2Ptr[0] = 12.3456789f;
xPtr[0] = 1.0e-5f;
const float storedEuler1 = euler1Ptr[0];
const float storedEuler2 = euler2Ptr[0];
const float storedX = xPtr[0];
std::stringstream filePath;
filePath << ebsdlib::unit_test::k_TestTempDir << "/CTF_WriteFile_float_precision_test.ctf";
REQUIRE(reader.writeFile(filePath.str()) == 0);
CtfReader readBack;
readBack.setFileName(filePath.str());
REQUIRE(readBack.readFile() == 0);
REQUIRE(readBack.getNumberOfElements() == reader.getNumberOfElements());
const auto* actualEuler1Ptr = static_cast<const float*>(readBack.getPointerByName(ebsdlib::Ctf::Euler1));
const auto* actualEuler2Ptr = static_cast<const float*>(readBack.getPointerByName(ebsdlib::Ctf::Euler2));
const auto* actualXPtr = static_cast<const float*>(readBack.getPointerByName(ebsdlib::Ctf::X));
REQUIRE(actualEuler1Ptr != nullptr);
REQUIRE(actualEuler2Ptr != nullptr);
REQUIRE(actualXPtr != nullptr);
SECTION("Euler1")
{
REQUIRE(actualEuler1Ptr[0] == storedEuler1);
}
SECTION("Euler2")
{
REQUIRE(actualEuler2Ptr[0] == storedEuler2);
}
SECTION("X")
{
REQUIRE(actualXPtr[0] == storedX);
}
}
Command:
ctest --test-dir build/Release -R '^EbsdLib::ebsdlib::CtfReaderTest::WriteFilePreservesFloatPrecision$' -j 1 --output-on-failure --timeout 1500 --no-tests=error
Output on 34844ed (Release, macOS arm64):
Source/Test/CtfReaderTest.cpp:339: FAILED:
REQUIRE( actualEuler1Ptr[0] == storedEuler1 )
with expansion:
0.0f == 0.00001f
Source/Test/CtfReaderTest.cpp:343: FAILED:
REQUIRE( actualEuler2Ptr[0] == storedEuler2 )
with expansion:
12.3457f == 12.34568f
Source/Test/CtfReaderTest.cpp:347: FAILED:
REQUIRE( actualXPtr[0] == storedX )
with expansion:
0.0f == 0.00001f
test cases: 1 | 0 passed | 1 failed
0% tests passed, 1 tests failed out of 1
Proposed Fix
In CtfReader::writeFile(), write float columns with enough significant digits to read back unchanged, for example:
- fprintf(f, "%0.4f\t", f32Ptr[counter]);
+ fprintf(f, "%.*g\t", std::numeric_limits<float>::max_digits10, static_cast<double>(f32Ptr[counter]));
(#include <limits>.) %g drops trailing zeros, so typical values such as 12.5 stay short. The test above then passes. If a consumer of .ctf files requires a fixed number of decimals per column, that requirement should be named in the code instead.
Notes
- Found by an audit of floating-point text output across simplnx, DREAM3D-NX and EbsdLib. The same defect class in simplnx is tracked in BlueQuartzSoftware/simplnx#1927.
- To read the written file back at all, the test restores the header line breaks with
setOriginalHeader(). Without that,writeFile()writes the original header without line separators (getHeaderLines()throughappendOriginalHeader()), and the new reader throwsstoi: no conversion. That is a separate defect and is not part of this issue.
- Ngôn ngữ chính
- C++
- Star
- 8
- Fork
- 16
- Merge trung bình
- 4 giờ 35 phút
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của BlueQuartzSoftware/EbsdLib
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 76/100
BlueQuartzSoftware/EbsdLib#66 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
BlueQuartzSoftware/EbsdLib#55 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 38/100
BlueQuartzSoftware/EbsdLib#47 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
BlueQuartzSoftware/EbsdLib#45 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
BlueQuartzSoftware/EbsdLib#35 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của BlueQuartzSoftware/EbsdLib
Issue tương tự
-
bug triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
26.04 bug dark-mode Easy Hack impress symptom: visual
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
CollaboraOnline/online#16421 · 1 bình luận ·
-
area/cs bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
ydb-platform/ydb#59881 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation enhancement lua server
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100