BUG: CTF writer keeps only four decimal places of every float column
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
在Source/EbsdLib/IO/HKL/CtfReader.cpp中,检查CtfReader::writeFile()约第1071–1075行的浮点列格式。先运行 issue 中描述的复现步骤,然后检查Source/Test/CtfReaderTest.cpp中的拟议往返测试。完成的标准是读回的 Euler1、Euler2 和 X 值与存储时完全一致。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- C++
- 星标
- 8
- 派生
- 16
- 平均合并
- 4 小时 35 分钟
- 30 天内合并 PR
- 3
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
BlueQuartzSoftware/EbsdLib 的其他 Issue
-
bug
难度 3/5 1-2 天 新手友好度 76/100
BlueQuartzSoftware/EbsdLib#66 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
BlueQuartzSoftware/EbsdLib#55 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 38/100
BlueQuartzSoftware/EbsdLib#47 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
BlueQuartzSoftware/EbsdLib#45 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 35/100
BlueQuartzSoftware/EbsdLib#35 ·
维护者通常 1 天内回复
查看 BlueQuartzSoftware/EbsdLib 的全部 Issue
相似的 Issue
-
bug triage
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
26.04 bug dark-mode Easy Hack impress symptom: visual
难度 2/5 1-3 小时 新手友好度 68/100
CollaboraOnline/online#16421 · 1 条评论 ·
-
area/cs bug
难度 2/5 1-3 小时 新手友好度 82/100
ydb-platform/ydb#59881 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
-
documentation enhancement lua server
难度 2/5 1-3 小时 新手友好度 66/100