Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/mzp-inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace po = boost::program_options;
std::unique_ptr<MzPeak::Util::Parquet> open_parquet_file(MzPeak::Index& index,
const std::string& file)
{
auto it = index.find(file);
auto it = index.manager()->find_file(file);

if (it == index.files().end()) {
std::println(stderr, "file \"{}\" is not in the mzPeak file index", file);
Expand All @@ -41,7 +41,7 @@ int print_array_index(MzPeak::Index& index, const std::string& file)

auto fmd = parquet->file_metadata();
auto et = parquet->index_file().entity_type();
auto key = MzPeak::Schema::entity_type_to_string(et) + "_array_index";
auto key = et.array_index_name();
auto json = parquet->kv_string(fmd, key);

if (!json.has_value()) {
Expand Down
7 changes: 5 additions & 2 deletions include/mzpeak/data/array_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ArrayIndex final {
BufferFormat buffer_format = BufferFormat::Point;

/// The entity type this column belongs to.
EntityType context = EntityType::Other;
EntityType context = EntityType("other");

/// The path from the *root* of the Parquet file's schema to this
/// column.
Expand Down Expand Up @@ -128,6 +128,9 @@ class ArrayIndex final {
/// The type of elements stored in this dimension.
Schema::PSI::ArrayType array_type;

/// Is this the primary dimension for array type?
bool buffer_priority;

/// Transformation information for this dimension.
std::optional<Schema::PSI::Transform> transform;

Expand Down Expand Up @@ -204,7 +207,7 @@ class ArrayIndex final {

private:
// The entity type for the entire Parquet file.
EntityType entity_type_ = EntityType::Other;
EntityType entity_type_ = EntityType("other");

// Root node.
std::string prefix_ = "point";
Expand Down
3 changes: 2 additions & 1 deletion include/mzpeak/data/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ void Decoder<T>::decode_with_nulls(const ArrayIndex::Dimension& dim,
auto col = signals_->column(primary_entry);

if (!col.has_value()) {
throw ParquetError("unable to decode dimension, not in schema: " + dim.name);
throw InvalidFormatError("unable to decode dimension, not in schema: " +
dim.name);
} else if (!slice_->has_column(col.value())) {
return; // No data to decode so we can exit early.
}
Expand Down
4 changes: 2 additions & 2 deletions include/mzpeak/data/null_marking.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ std::optional<T> Decoder<T, U>::operator()(int64_t index)
{
// Sanity check.
if (array_ == nullptr) {
throw("FIXME: assertion failed");
throw(Exception("while decoding null marking an nullptr was encountered"));
};

if (index != 0 && prior_.index == index - 1) {
Expand Down Expand Up @@ -200,7 +200,7 @@ std::optional<T> Decoder<T, U>::operator()(int64_t index)
return prior_.value;
} else {
// Shouldn't happen.
throw("FIXME: failed to decode NULL marking value");
throw(Exception("failed to decode NULL marking value"));
}
}

Expand Down
6 changes: 3 additions & 3 deletions include/mzpeak/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ class UnknownLayoutError final : public Exception {
/**
* Attempt to access an invalid iterator.
*/
class InvalidIterator final : public Exception {
class InvalidIteratorError final : public Exception {
public:
/// Constructor.
InvalidIterator(const std::string& msg)
InvalidIteratorError(const std::string& msg)
: Exception(msg)
{
}

/// Destructor.
~InvalidIterator() = default;
~InvalidIteratorError() = default;
};

/**
Expand Down
12 changes: 5 additions & 7 deletions include/mzpeak/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ directory of this repository.
#pragma once

#include <memory>
#include <string_view>
#include <vector>

#include "mzpeak/schema/file.h"

namespace MzPeak {

namespace IO {
class Archive;
}

namespace Schema {
class File;
}

namespace Util {
class Manager;
}
Expand All @@ -42,9 +39,10 @@ class Index {
const std::vector<Schema::File>& files() const;

/**
* Find a file in the mzPeak archive with the given name.
* Find a file given its `EntityType` and `DataKind`.
*/
std::vector<Schema::File>::const_iterator find(std::string_view) const;
std::vector<Schema::File>::const_iterator find_file(Schema::EntityType::Type,
Schema::DataKind::Type) const;

/**
* Access the spectra in the file.
Expand Down
6 changes: 6 additions & 0 deletions include/mzpeak/schema/cv.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class CV {
return code_ == other.code_ && accession_ == other.accession_;
}

/// Less than.
bool operator<(const CV& other) const
{
return code_ < other.code_ && accession_ < other.accession_;
}

private:
std::string code_;
std::string accession_;
Expand Down
67 changes: 48 additions & 19 deletions include/mzpeak/schema/entity_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,65 @@ directory of this repository.

#pragma once

#include <optional>
#include <string>
#include <variant>

namespace MzPeak::Schema {

/**
* The type of data entity stored in the file.
*/
enum class EntityType {
/// Mass spectra.
Spectrum,
class EntityType {
public:
enum Type {
/// Mass spectra.
Spectrum,

/// Chromatograms or other measurements over time.
Chromatogram,
/// Chromatograms or other measurements over time.
Chromatogram,

/// Similar to Spectrum except the unit of measure is a wavelength
/// measurement.
WavelengthSpectrum,
/// Similar to Spectrum except the unit of measure is a wavelength
/// measurement.
WavelengthSpectrum,
};

/// Unspecified.
Other
};
// Internal storage type.
using value_type = std::variant<Type, std::string>;

/**
* Convert a EntityType to a string.
*/
std::string entity_type_to_string(EntityType);
/// Constructor.
EntityType(std::string_view);

/**
* Parse an EntityType from a string view.
*/
EntityType entity_type_from_string(std::string_view);
/// Constructor.
EntityType(Type);

/**
* Return the string representation of an entity type.
*/
std::string to_string() const;

/**
* Return the enumerated type if it is known.
*/
std::optional<Type> type() const;

/**
* The name of the index column matching this entity.
*/
std::string index_column_name() const;

/**
* The name of the array index metadata key.
*/
std::string array_index_name() const;

/**
* The metadata key for the entity count.
*/
std::string metadata_count_key() const;

private:
value_type val_;
};

} // namespace MzPeak::Schema
2 changes: 1 addition & 1 deletion include/mzpeak/schema/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class File final {
private:
std::string file_name_;
DataKind data_kind_ = DataKind("other");
EntityType entity_type_ = EntityType::Other;
EntityType entity_type_ = EntityType("other");
std::vector<Column> columns_;
};

Expand Down
3 changes: 3 additions & 0 deletions include/mzpeak/schema/psi/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class DataType {
/// Equality.
bool operator==(const DataType& other) const;

/// Less than (for sorting).
bool operator<(const DataType& other) const;

private:
value_type val_;
std::optional<Util::Type> as_type_;
Expand Down
2 changes: 1 addition & 1 deletion include/mzpeak/util/enumerable_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class EnumerableProxy : public std::ranges::view_interface<EnumerableProxy<T>> {
const_cast<Iterator*>(this)->cache_ = std::make_pair<>(n_, fetch_(n_));
return cache_->second;
} else {
throw InvalidIterator("attempt to dereference an invalid iterator");
throw InvalidIteratorError("attempt to dereference an invalid iterator");
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/mzpeak/util/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Manager final {
/**
* Find a file given its `EntityType` and `DataKind`.
*/
std::vector<Schema::File>::const_iterator find_file(Schema::EntityType,
std::vector<Schema::File>::const_iterator find_file(Schema::EntityType::Type,
Comment thread
pjones marked this conversation as resolved.
Schema::DataKind::Type) const;

/**
Expand Down
7 changes: 5 additions & 2 deletions include/mzpeak/util/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ top-level directory of this repository.
#pragma once

#include <any>
#include <boost/compat/function_ref.hpp>
#include <functional>
#include <variant>

Expand Down Expand Up @@ -192,12 +193,14 @@ class Query final {

/// A function that when given an column type, should return a single value.
/// If this isn't possible it should return `Result<value_t>::skip()`.
using eval_callback_t = std::function<Result<value_t>(Schema::Column)>;
using eval_callback_t =
boost::compat::function_ref<Result<value_t>(Schema::Column)>;

/// A func ion that when given an column type should return a min
/// and max. If this isn't possible it should return
/// `Result<range_t>::skip()`.
using eval_range_callback_t = std::function<Result<range_t>(Schema::Column)>;
using eval_range_callback_t =
boost::compat::function_ref<Result<range_t>(Schema::Column)>;

/**
* Evaluate a query.
Expand Down
34 changes: 26 additions & 8 deletions src/data/array_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ directory of this repository.

namespace MzPeak::Data {

/******************************************************************************/
// How to compare entries. Must match EntryChunkFn below.
//
// TODO: Unify these two types.
struct EntryCmpFn {
bool operator()(const ArrayIndex::Entry& a, const ArrayIndex::Entry& b) const
{
return a.array_name < b.array_name && a.array_type < b.array_type &&
a.data_type < b.data_type && a.buffer_priority > b.buffer_priority;
}
};

// How to chunk entries. Must match EntryCmpFn above.
struct EntryChunkFn {
bool operator()(const ArrayIndex::Entry& a, const ArrayIndex::Entry& b) const
{
return a.array_name == b.array_name && a.array_type == b.array_type &&
a.data_type == b.data_type && a.buffer_priority == b.buffer_priority;
}
};

/******************************************************************************/
ArrayIndex::Layout group_name_to_layout(const std::string& name)
{
Expand Down Expand Up @@ -194,7 +215,7 @@ ArrayIndex::ArrayIndex(EntityType entity_type, const json::object& obj)
entry.array_name = eo.at("array_name").as_string();
entry.buffer_format =
buffer_format_from_string(eo.at("buffer_format").as_string());
entry.context = entity_type_from_string(eo.at("context").as_string());
entry.context = EntityType(eo.at("context").as_string());
entry.path = eo.at("path").as_string();
entry.name = entry.path.substr(prefix_.size() + 1);

Expand Down Expand Up @@ -248,7 +269,7 @@ ArrayIndex::ArrayIndex(EntityType entity_type, const json::object& obj)
}
}

std::ranges::sort(entries_, {}, &Entry::array_name);
std::ranges::sort(entries_, EntryCmpFn());
}

/******************************************************************************/
Expand Down Expand Up @@ -279,10 +300,7 @@ std::optional<std::size_t> ArrayIndex::num_entities() const { return num_entitie
std::vector<ArrayIndex::Dimension> ArrayIndex::dimensions() const
{
std::vector<std::vector<Entry>> groups =
entries_ | std::views::chunk_by([](auto& a, auto& b) {
return a.array_name == b.array_name && a.data_type == b.data_type &&
a.array_type == b.array_type;
}) |
entries_ | std::views::chunk_by(EntryChunkFn()) |
std::ranges::to<std::vector<std::vector<Entry>>>();

std::vector<Dimension> result;
Expand All @@ -292,8 +310,8 @@ std::vector<ArrayIndex::Dimension> ArrayIndex::dimensions() const
if (group.empty()) continue;
auto& head = group[0];

result.push_back({head.name, head.data_type, head.array_type, head.transform,
std::move(group)});
result.push_back({head.name, head.data_type, head.array_type,
head.buffer_priority, head.transform, std::move(group)});
}

return result;
Expand Down
Loading