BUG: CTF writer keeps only four decimal places of every float column

Đang mở Phù hợp với người mới
#65 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp
Lĩnh vực
backend

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ả

bug

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

  1. Read a .ctf file with CtfReader (the test below uses the test fixture CtfReaderTest::USInputFile1).
  2. Set the first Euler1 value to 1.0e-5f, the first Euler2 value to 12.3456789f and the first X value to 1.0e-5f.
  3. Call writeFile() to a new path.
  4. Read that file with a new CtfReader and 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() through appendOriginalHeader()), and the new reader throws stoi: 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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của BlueQuartzSoftware/EbsdLib

Tất cả issue của BlueQuartzSoftware/EbsdLib

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.