Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impro #106

Merged
merged 3 commits into from
May 24, 2024
Merged

Impro #106

Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required (VERSION 3.24)
project (Tapkee LANGUAGES CXX)

# set paths
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD 23)
set (TAPKEE_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
set (TAPKEE_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
set (TAPKEE_TESTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/test/unit")
Expand Down
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ some examples of usage Tapkee in Shogun as
API
---

We provide an interface based on the method chaining technique. The chain starts from the call
of the `initialize()` method and followed with the `withParameters(const ParametersSet&)` call
which is used to provide parameters like the method to use and its settings. The provided
argument is formed with the following syntax:
We provide an interface based on the method chaining technique. The chain starts with the call
of the `with(const ParametersSet&)` method, which is used to provide parameters like the method
to use and its settings. The provided argument is formed with the following syntax:

(keyword1=value1, keyword2=value2)

Expand All @@ -63,24 +62,23 @@ are defined: `method`, `eigen_method`, `neighbors_method`, `num_neighbors`, `tar
As an example of parameters setting, if you want to use the Isomap
algorithm with the number of neighbors set to 15:

tapkee::initialize().withParameters((method=Isomap,num_neighbors=15))
tapkee::with((method=Isomap,num_neighbors=15))

Please note that the inner parentheses are necessary as it uses the
comma operator which appears to be ambiguous in this case.

Next, with initialized parameters you may either embed the provided matrix with:
Next, you may either embed the provided matrix with:

tapkee::initialize().withParameters((method=Isomap,num_neighbors=15)).
.embedUsing(matrix);
tapkee::with((method=Isomap,num_neighbors=15)).embedUsing(matrix);

Or provide callbacks (kernel, distance and features) using any combination
of the `withKernel(KernelCallback)`, `withDistance(DistanceCallback)` and
`withFeatures(FeaturesCallback)` member functions:

tapkee::initialize().withParameters((method=Isomap,num_neighbors=15))
.withKernel(kernel_callback)
.withDistance(distance_callback)
.withFeatures(features_callback)
tapkee::with((method=Isomap,num_neighbors=15))
.withKernel(kernel_callback)
.withDistance(distance_callback)
.withFeatures(features_callback)

Once callbacks are initialized you may either embed data using an
STL-compatible sequence of indices or objects (that supports the
Expand All @@ -92,17 +90,14 @@ member function.

As a summary - a few examples:

TapkeeOutput output = initialize()
.withParameters((method=Isomap,num_neighbors=15))
TapkeeOutput output = with((method=Isomap,num_neighbors=15))
.embedUsing(matrix);

TapkeeOutput output = initialize()
.withParameters((method=Isomap,num_neighbors=15))
TapkeeOutput output = with((method=Isomap,num_neighbors=15))
.withDistance(distance_callback)
.embedUsing(indices);

TapkeeOutput output = initialize()
.withParameters((method=Isomap,num_neighbors=15))
TapkeeOutput output = with((method=Isomap,num_neighbors=15))
.withDistance(distance_callback)
.embedRange(indices.begin(),indices.end());

Expand Down Expand Up @@ -130,8 +125,7 @@ A minimal working example of a program that uses the library is:

MyDistanceCallback d;

TapkeeOutput output = tapkee::initialize()
.withParameters((method=MultidimensionalScaling,target_dimension=1))
TapkeeOutput output = tapke::with((method=MultidimensionalScaling,target_dimension=1))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh there is a typo

Copy link
Collaborator Author

@iglesias iglesias May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the indentation? In that case, yeah I see that maybe this line is a bit longer than the other ones with code (here in the inline diff it looks like a new line, while it is only one). Any option looks good to me, like it is with the long-ish line, or, alternatively, finishing this line at = and aligning the tapkee::with with the other with* below.

I tried but didn't manage to find a typo in the names in this line ':-D

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is tapke

Copy link
Collaborator Author

@iglesias iglesias May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, finally see it, thank you!

.withDistance(d)
.embedUsing(indices);

Expand Down
3 changes: 1 addition & 2 deletions examples/minimal/minimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ int main(int argc, const char **argv)

MyDistanceCallback distance;

TapkeeOutput output = initialize()
.withParameters((method = MultidimensionalScaling, target_dimension = 1))
TapkeeOutput output = with((method = MultidimensionalScaling, target_dimension = 1))
.withDistance(distance)
.embedUsing(indices);

Expand Down
5 changes: 2 additions & 3 deletions examples/precomputed/precomputed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace tapkee;
int main(int argc, const char **argv)
{
const int N = 100;
tapkee::DenseMatrix distances(N, N);
DenseMatrix distances(N, N);
vector<IndexType> indices(N);
for (int i = 0; i < N; i++)
{
Expand All @@ -19,8 +19,7 @@ int main(int argc, const char **argv)

precomputed_distance_callback distance(distances);

TapkeeOutput output = initialize()
.withParameters((method = MultidimensionalScaling, target_dimension = 1))
TapkeeOutput output = with((method = MultidimensionalScaling, target_dimension = 1))
.withDistance(distance)
.embedUsing(indices);

Expand Down
3 changes: 1 addition & 2 deletions examples/rna/rna.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ int main(int argc, const char **argv)

MatchKernelCallback kernel;

TapkeeOutput result = initialize()
.withParameters((method = KernelLocallyLinearEmbedding, num_neighbors = 30))
TapkeeOutput result = .with((method = KernelLocallyLinearEmbedding, num_neighbors = 30))
.withKernel(kernel)
.embedUsing(rnas);

Expand Down
38 changes: 13 additions & 25 deletions include/tapkee/chain_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,31 +484,19 @@ class ParametersInitializedState
};
} /* End of namespace tapkee_internal */

struct initialize
/** Returns an instance representing a state with initialized parameters.
*
* In the chain this method's call is followed by any of
* @ref tapkee_internal::ParametersInitializedState::embedUsing
* @ref tapkee_internal::ParametersInitializedState::withKernel
* @ref tapkee_internal::ParametersInitializedState::withDistance
* @ref tapkee_internal::ParametersInitializedState::withFeatures
*
* @param parameters a set of parameters formed by keywords assigned to values
*/
tapkee_internal::ParametersInitializedState with(const ParametersSet& parameters)
{
/** Constructor that is the first required
* method in the call chain.
*/
initialize()
{
}

/** The second required method in the call chain. Returns
* an instance representing a state with initialized parameters.
*
* In the chain this method's call is followed by any of
* * @ref tapkee_internal::ParametersInitializedState::embedUsing
* * @ref tapkee_internal::ParametersInitializedState::withKernel
* * @ref tapkee_internal::ParametersInitializedState::withDistance
* * @ref tapkee_internal::ParametersInitializedState::withFeatures
*
* @param parameters a set of parameters formed from keywords assigned
* to values
*/
tapkee_internal::ParametersInitializedState withParameters(const ParametersSet& parameters) const
{
return tapkee_internal::ParametersInitializedState(parameters);
}
};
return tapkee_internal::ParametersInitializedState(parameters);
}

} /* End of namespace tapkee */
9 changes: 4 additions & 5 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ int run(int argc, const char **argv)
}

int target_dim = opt[TARGET_DIMENSION_KEYWORD].as<int>();
if (target_dim < 0)
if (target_dim <= 0)
{
tapkee::Logging::instance().message_error(
"Negative target dimensionality is not possible in current circumstances. "
"\"Only\" a positive target dimensionality larger than zero is possible in current circumstances. "
"Please visit other universe");
return 1;
}
Expand Down Expand Up @@ -469,16 +469,15 @@ int run(int argc, const char **argv)
tapkee::precomputed_kernel_callback kcb(kernel_matrix);
tapkee::eigen_features_callback fcb(input_data);

output = tapkee::initialize()
.withParameters(parameters)
output = tapkee::with(parameters)
.withKernel(kcb)
.withDistance(dcb)
.withFeatures(fcb)
.embedRange(indices.begin(), indices.end());
}
else
{
output = tapkee::initialize().withParameters(parameters).embedUsing(input_data);
output = tapkee::with(parameters).embedUsing(input_data);
}
// Save obtained data
if (opt.count(TRANSPOSE_OUTPUT_KEYWORD))
Expand Down
45 changes: 15 additions & 30 deletions test/unit/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,96 +20,81 @@ TEST(Interface, ChainInterfaceOrder)

TapkeeOutput output;

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = MultidimensionalScaling))
ASSERT_NO_THROW(output = tapkee::with((method = MultidimensionalScaling))
.withKernel(kcb)
.withFeatures(fcb)
.withDistance(dcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = MultidimensionalScaling))
ASSERT_NO_THROW(output = tapkee::with((method = MultidimensionalScaling))
.withKernel(kcb)
.withDistance(dcb)
.withFeatures(fcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = KernelPrincipalComponentAnalysis))
ASSERT_NO_THROW(output = tapkee::with((method = KernelPrincipalComponentAnalysis))
.withDistance(dcb)
.withKernel(kcb)
.withFeatures(fcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = PassThru))
ASSERT_NO_THROW(output = tapkee::with((method = PassThru))
.withDistance(dcb)
.withFeatures(fcb)
.withKernel(kcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = MultidimensionalScaling))
ASSERT_NO_THROW(output = tapkee::with((method = MultidimensionalScaling))
.withFeatures(fcb)
.withDistance(dcb)
.withKernel(kcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = MultidimensionalScaling))
ASSERT_NO_THROW(output = tapkee::with((method = MultidimensionalScaling))
.withFeatures(fcb)
.withKernel(kcb)
.withDistance(dcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = PassThru))
ASSERT_NO_THROW(output = tapkee::with((method = PassThru))
.withFeatures(fcb)
.withKernel(kcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = PassThru))
ASSERT_NO_THROW(output = tapkee::with((method = PassThru))
.withFeatures(fcb)
.withDistance(dcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = KernelPrincipalComponentAnalysis))
ASSERT_NO_THROW(output = tapkee::with((method = KernelPrincipalComponentAnalysis))
.withKernel(kcb)
.withDistance(dcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = KernelPrincipalComponentAnalysis))
ASSERT_NO_THROW(output = tapkee::with((method = KernelPrincipalComponentAnalysis))
.withKernel(kcb)
.withFeatures(fcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = MultidimensionalScaling))
ASSERT_NO_THROW(output = tapkee::with((method = MultidimensionalScaling))
.withDistance(dcb)
.withFeatures(fcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = MultidimensionalScaling))
ASSERT_NO_THROW(output = tapkee::with((method = MultidimensionalScaling))
.withDistance(dcb)
.withKernel(kcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = KernelPrincipalComponentAnalysis))
ASSERT_NO_THROW(output = tapkee::with((method = KernelPrincipalComponentAnalysis))
.withKernel(kcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = MultidimensionalScaling))
ASSERT_NO_THROW(output = tapkee::with((method = MultidimensionalScaling))
.withDistance(dcb)
.embedRange(indices.begin(), indices.end()));

ASSERT_NO_THROW(output = tapkee::initialize()
.withParameters((method = PassThru))
ASSERT_NO_THROW(output = tapkee::with((method = PassThru))
.withFeatures(fcb)
.embedRange(indices.begin(), indices.end()));
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/projecting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TEST(Projecting, PrincipalComponentAnalysis)

TapkeeOutput output;

ASSERT_NO_THROW(output = tapkee::initialize().withParameters((method = PrincipalComponentAnalysis, target_dimension = 2)).embedUsing(X));
ASSERT_NO_THROW(output = tapkee::with((method = PrincipalComponentAnalysis, target_dimension = 2)).embedUsing(X));

auto projected = output.projection(X.col(0));
ASSERT_EQ(2, projected.size());
Expand All @@ -26,7 +26,7 @@ TEST(Projecting, RandomProjection)

TapkeeOutput output;

ASSERT_NO_THROW(output = tapkee::initialize().withParameters((method = RandomProjection, target_dimension = 2)).embedUsing(X));
ASSERT_NO_THROW(output = tapkee::with((method = RandomProjection, target_dimension = 2)).embedUsing(X));

auto projected = output.projection(X.col(0));
ASSERT_EQ(2, projected.size());
Expand Down
Loading