attempt at reading json file

This commit is contained in:
Bechir Braham 2024-02-14 18:37:14 +01:00
parent 079d519e96
commit 66f87b0f51
27 changed files with 324 additions and 110 deletions

View File

@ -6,10 +6,10 @@
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"compilerPath": "/usr/bin/g++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-clang-x64",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],

23
.vscode/settings.json vendored
View File

@ -52,6 +52,25 @@
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"filesystem": "cpp"
}
"filesystem": "cpp",
"csignal": "cpp",
"cstring": "cpp",
"any": "cpp",
"condition_variable": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_set": "cpp",
"fstream": "cpp",
"mutex": "cpp",
"ranges": "cpp",
"semaphore": "cpp",
"span": "cpp",
"stop_token": "cpp",
"thread": "cpp",
"valarray": "cpp",
"variant": "cpp"
},
"C_Cpp.errorSquiggles": "enabled"
}

View File

@ -6,12 +6,20 @@ project(aare
HOMEPAGE_URL "https://github.com/slsdetectorgroup/aare"
LANGUAGES C CXX
)
# cmake_policy(SET CMP0135 NEW)
# include(GNUInstallDirs)
cmake_policy(SET CMP0135 NEW)
include(FetchContent)
FetchContent_Declare(json
GIT_REPOSITORY https://github.com/nlohmann/json
GIT_TAG v3.11.3
)
FetchContent_MakeAvailable(json)
find_package(fmt 6 REQUIRED)
# include(cmake/helpers.cmake)
# default_build_type("Debug")]
set(CMAKE_BUILD_TYPE "Debug")
@ -24,17 +32,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(USE_SANITIZER "Sanitizers for debugging" ON)
option(DISABLE_WARNINGS "Disbale compilation warnings" OFF)
# option(USE_TESTS "Unit tests" OFF)
# option(USE_PYTHON "Python bindings" OFF)
# option(TUNE_LOCAL "Tune to exact CPU architecture" OFF)
# option(BUILD_EXAMPLES "Build examples" OFF)
# option(DISABLE_LTO "Disable Link Time Optimizations" OFF)
# if(NOT APPLE)
# set(CMAKE_INSTALL_RPATH $ORIGIN)
# endif()
set(OPTIONAL_FLAGS "")
if(DISABLE_WARNINGS)
set(OPTIONAL_FLAGS "${OPTIONAL_FLAGS} -Wall -Wextra -pedantic -Wno-unused-parameter -Wshadow -Wformat=2 -Wold-style-cast -Wnon-virtual-dtor -Wfloat-equal -Wconversion -Wlogical-op -Wshift-overflow=2 -Woverloaded-virtual -Winline")
@ -58,7 +57,6 @@ endif()
#Enable LTO if available
include(CheckIPOSupported)
check_ipo_supported(RESULT LTO_AVAILABLE)
if((CMAKE_BUILD_TYPE STREQUAL "Release") AND LTO_AVAILABLE)
@ -67,9 +65,26 @@ else()
message(STATUS "Building without link time optimization")
endif()
set(SUPPRESSED_WARNINGS "-Wno-return-type")
set(CMAKE_CXX_FLAGS "${OPTIONAL_FLAGS} ${OPTIMIZATION_FLAGS} ${SUPPRESSED_WARNINGS}")
set(CMAKE_CXX_FLAGS "${OPTIONAL_FLAGS} ${OPTIMIZATION_FLAGS}")
add_executable(aare)
target_link_libraries( aare
PRIVATE
nlohmann_json::nlohmann_json
PUBLIC
fmt::fmt
)
add_subdirectory(src)

View File

@ -1,2 +1,8 @@
# aare
Data analysis library for PSI hybrid detectors
## file_io class diagram
![file_io class diagram](./extra/uml/out/file_io/ClassDiagram.png)

36
extra/uml/file_io.pu Normal file
View File

@ -0,0 +1,36 @@
@startuml ClassDiagram
abstract class FileFactory{
{static} +getFactory(path):FileFactory
{abstract} +loadFile(path):File
}
class JsonFileFactory{
+loadFile(path):JsonFile
}
class RawFileFactory{
+loadFile(path):RawFile
}
class NumpyFileFactory{
+loadFile(path):NumpyFile
}
abstract File{
}
class JsonFile{
}
class RawFile{
}
class NumpyFile{
}
FileFactory <|-- RawFileFactory
FileFactory <|-- NumpyFileFactory
FileFactory <|-- JsonFileFactory
File <|-- JsonFile
File <|-- RawFile
File <|-- NumpyFile
FileFactory ..> File
@enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -1,6 +1,7 @@
add_subdirectory(file_io)
add_subdirectory(core)
add_subdirectory(processing)
add_subdirectory(common)
target_include_directories(aare PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_sources(aare PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
target_include_directories(aare PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_sources(aare PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

View File

@ -0,0 +1,7 @@
target_include_directories(aare PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include )
target_include_directories(aare PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -10,4 +10,5 @@ using dynamic_shape = std::vector<ssize_t>;
enum class DetectorType { Jungfrau, Eiger, Mythen3, Moench };
enum class TimingMode {Auto, Trigger};
enum class TimingMode {Auto, Trigger};
DetectorType StringTo(std::string_view name);

View File

@ -1,5 +1,14 @@
target_include_directories(aare PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set(FILE_IO_SOURCES "File.cpp" "FileFactory.cpp" "RawFileFactory.cpp")
target_include_directories(aare PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/file")
target_include_directories(aare PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/file_factory")
target_include_directories(aare PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
set(FILE_IO_SOURCES "file/File.cpp"
"file/JsonFile.cpp"
"file_factory/FileFactory.cpp"
"file_factory/JsonFileFactory.cpp"
"file_factory/RawFileFactory.cpp"
"helpers.cpp"
)
# append ${CMAKE_CURRENT_SOURCE_DIR} to the list of sources using for loop
foreach(FILE_IO_SOURCE ${FILE_IO_SOURCES})
list(APPEND FILE_IO_SOURCES_WITH_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${FILE_IO_SOURCE}")

View File

@ -1,29 +0,0 @@
#pragma once
#include <filesystem>
#include "defs.hpp"
class File
{
private:
// std::unique_ptr<FileWrapper> fp;
public:
// File();
// ~File();
size_t total_frames{};
ssize_t rows{};
ssize_t cols{};
uint8_t bitdepth{};
DetectorType type{};
inline size_t bytes_per_frame() const{
//TODO: ask if this is correct
return rows*cols*bitdepth/8;
}
inline size_t pixels() const{
return rows*cols;
}
// size_t total_frames();
};

View File

@ -1,31 +0,0 @@
#include "FileFactory.hpp"
#include "File.hpp"
#include "RawFileFactory.hpp"
#include "JsonFileFactory.hpp"
#include <iostream>
File FileFactory::loadFile(std::filesystem::path fpath){
// check if file exists
if(!std::filesystem::exists(fpath)){
throw std::runtime_error("File does not exist");
}
File ret;
// check if extension is raw
if(fpath.extension() == ".raw"){
std::cout<<"Loading raw file"<<std::endl;
}
// json
else if(fpath.extension() == ".json"){
std::cout<<"Loading json file"<<std::endl;
JsonFileFactory jff = JsonFileFactory();
}
//check if extension is numpy
else if(fpath.extension() == ".npy"){
std::cout<<"Loading numpy file"<<std::endl;
}
// TODO: determine file type
return ret ;
}

View File

@ -1,12 +0,0 @@
#pragma once
#include <filesystem>
#include "File.hpp"
class FileFactory{
// Class that will be used to create File objects
// follows the factory pattern
public:
// virtual File createFile() = 0;
// virtual int deleteFile() = 0;
File loadFile(std::filesystem::path); //TODO: add option to load all file to memory or keep it on disk
};

View File

@ -1,10 +0,0 @@
#include "FileFactory.hpp"
class JsonFileFactory: FileFactory
{
private:
/* data */
public:
};

46
src/file_io/file/File.hpp Normal file
View File

@ -0,0 +1,46 @@
#pragma once
#include <filesystem>
#include "defs.hpp"
#include <fmt/core.h>
class File
{
public:
std::filesystem::path base_path;
std::string base_name,ext;
int findex, n_subfiles;
size_t total_frames{};
std::string version;
DetectorType type;
TimingMode timing_mode;
int subfile_rows, subfile_cols;
ssize_t rows{};
ssize_t cols{};
uint8_t bitdepth{};
DetectorType type{};
// File();
// ~File();
inline size_t bytes_per_frame() const{
return rows*cols*bitdepth/8;
}
inline size_t pixels() const{
return rows*cols;
}
inline std::filesystem::path master_fname() {
return base_path / fmt::format("{}_master_{}{}", base_name, findex, ext);
}
inline std::filesystem::path data_fname(int mod_id, int file_id) {
return base_path / fmt::format("{}_d{}_f{}_{}.raw", base_name, mod_id, file_id, findex);
}
// size_t total_frames();
};

View File

@ -0,0 +1,6 @@
#pragma once
class JsonFile: public File
{
};

View File

@ -0,0 +1,41 @@
#include "FileFactory.hpp"
#include "File.hpp"
#include "RawFileFactory.hpp"
#include "JsonFileFactory.hpp"
#include <iostream>
FileFactory FileFactory::getFactory(std::filesystem::path fpath){
// check if file exists
if(!std::filesystem::exists(fpath)){
throw std::runtime_error("File does not exist");
}
if(fpath.extension() == ".raw"){
std::cout<<"Loading raw file"<<std::endl;
throw std::runtime_error("Raw file not implemented");
}
else if(fpath.extension() == ".json"){
std::cout<<"Loading json file"<<std::endl;
return JsonFileFactory(fpath);
}
//check if extension is numpy
else if(fpath.extension() == ".npy"){
std::cout<<"Loading numpy file"<<std::endl;
throw std::runtime_error("Numpy file not implemented");
}
throw std::runtime_error("Unsupported file type");
}
void FileFactory::parse_fname(File& file) {
file.base_path = fpath.parent_path();
file.base_name = fpath.stem();
file.ext = fpath.extension();
auto pos = file.base_name.rfind("_");
file.findex = std::stoi(file.base_name.substr(pos + 1));
pos = file.base_name.find("_master_");
file.base_name.erase(pos);
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <filesystem>
#include "File.hpp"
class FileFactory{
// Class that will be used to create File objects
// follows the factory pattern
protected:
std::filesystem::path fpath;
public:
static FileFactory getFactory(std::filesystem::path);
// virtual int deleteFile() = 0;
virtual File loadFile(){};//TODO: add option to load all file to memory or keep it on disk
virtual void parse_metadata(File&){};
// inline fs::path master_fname() const {
// return base_path / fmt::format("{}_master_{}{}", base_name, findex, ext);}
void parse_fname(File&);
};

View File

@ -0,0 +1,56 @@
#include "JsonFileFactory.hpp"
#include "JsonFile.hpp"
#include "helpers.hpp"
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
JsonFileFactory::JsonFileFactory(std::filesystem::path fpath){
if(not is_master_file(fpath))
throw std::runtime_error("Json file is not a master file");
this->fpath = fpath;
}
void JsonFileFactory::parse_metadata(File& file){
std::cout<<"Parsing metadata"<<std::endl;
std::ifstream ifs(file.master_fname());
json j;
ifs >> j;
double v = j["Version"];
file.version = fmt::format("{:.1f}", v);
file.type = StringTo<DetectorType>(j["Detector Type"].get<std::string>());
file.timing_mode = StringTo<TimingMode>(j["Timing Mode"].get<std::string>());
file.total_frames = j["Frames in File"];
file.subfile_cols = j["Pixels"]["x"];
file.subfile_rows = j["Pixels"]["y"];
if (type_ == DetectorType::Moench)
bitdepth_ = 16;
else
bitdepth_ = j["Dynamic Range"];
// only Eiger had quad
if (type_ == DetectorType::Eiger) {
quad_ = (j["Quad"] == 1);
}
}
File JsonFileFactory::loadFile(){
std::cout<<"Loading json file"<<std::endl;
JsonFile file = JsonFile();
this->parse_fname(file);
this->parse_metadata(file);
return file;
}

View File

@ -0,0 +1,13 @@
#include "FileFactory.hpp"
class JsonFileFactory: public FileFactory
{
private:
/* data */
public:
File loadFile() override;
void parse_metadata(File&) override;
JsonFileFactory(std::filesystem::path fpath);
};

11
src/file_io/helpers.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "helpers.hpp"
bool is_master_file(std::filesystem::path fpath) {
std::string stem = fpath.stem();
if (stem.find("_master_") != std::string::npos)
return true;
else
return false;
}

8
src/file_io/helpers.hpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "File.hpp"
#include <filesystem>
#include <fmt/core.h>
bool is_master_file(std::filesystem::path fpath);

View File

@ -1,10 +1,10 @@
// Your First C++ Program
#include <iostream>
#include "file_io/FileFactory.hpp"
#include "file_io/file_factory/FileFactory.hpp"
int main() {
FileFactory fileFactory=FileFactory();
std::filesystem::path p = std::filesystem::current_path();
File f = fileFactory.loadFile(p/"test.raw");
FileFactory fileFactory=FileFactory::getFactory(std::filesystem::current_path()/"test.json");
File f = fileFactory.loadFile();
}