Skip to content

Commit 8a98439

Browse files
committed
removed curl from header file
1 parent 4c988bb commit 8a98439

File tree

2 files changed

+44
-60
lines changed

2 files changed

+44
-60
lines changed

src/shogun/io/OpenMLFlow.cpp

+17-29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ using namespace shogun;
1414
using namespace rapidjson;
1515

1616
#ifdef HAVE_CURL
17+
#include <curl/curl.h>
18+
#endif // HAVE_CURL
1719

1820
/**
1921
* The writer callback function used to write the packets to a C++ string.
@@ -25,16 +27,14 @@ using namespace rapidjson;
2527
*/
2628
size_t writer(char* data, size_t size, size_t nmemb, std::string* buffer_in)
2729
{
28-
// adapted from https://stackoverflow.com/a/5780603
29-
// Is there anything in the buffer?
30-
if (buffer_in->empty())
30+
// check that the buffer string points to something
31+
if (buffer_in != nullptr)
3132
{
3233
// Append the data to the buffer
3334
buffer_in->append(data, size * nmemb);
3435

3536
return size * nmemb;
3637
}
37-
3838
return 0;
3939
}
4040

@@ -55,7 +55,7 @@ const char* OpenMLReader::flow_file = "/flow/{}";
5555
/* TASK API */
5656
const char* OpenMLReader::task_file = "/task/{}";
5757
/* SPLIT API */
58-
const char* OpenMLReader::get_split = "/split/{}";
58+
const char* OpenMLReader::get_split = "/get/{}";
5959

6060
const std::unordered_map<std::string, std::string>
6161
OpenMLReader::m_format_options = {{"xml", xml_server},
@@ -72,12 +72,9 @@ const std::unordered_map<std::string, std::string>
7272
{"flow_file", flow_file},
7373
{"task_file", task_file}};
7474

75-
OpenMLReader::OpenMLReader(const std::string& api_key) : m_api_key(api_key)
76-
{
77-
}
78-
7975
void OpenMLReader::openml_curl_request_helper(const std::string& url)
8076
{
77+
#ifdef HAVE_CURL
8178
CURL* curl_handle = nullptr;
8279

8380
curl_handle = curl_easy_init();
@@ -95,18 +92,11 @@ void OpenMLReader::openml_curl_request_helper(const std::string& url)
9592

9693
CURLcode res = curl_easy_perform(curl_handle);
9794

98-
openml_curl_error_helper(curl_handle, res);
95+
if (res != CURLE_OK)
96+
SG_SERROR("Connection error: %s.\n", curl_easy_strerror(res))
9997

10098
curl_easy_cleanup(curl_handle);
101-
}
102-
103-
void OpenMLReader::openml_curl_error_helper(CURL* curl_handle, CURLcode code)
104-
{
105-
if (code != CURLE_OK)
106-
{
107-
// TODO: call curl_easy_cleanup(curl_handle) ?
108-
SG_SERROR("Connection error: %s.\n", curl_easy_strerror(code))
109-
}
99+
#endif // HAVE_CURL
110100
}
111101

112102
/**
@@ -298,7 +288,7 @@ void OpenMLFlow::upload_flow(const std::shared_ptr<OpenMLFlow>& flow)
298288
SG_SNOTIMPLEMENTED;
299289
}
300290

301-
void OpenMLFlow::dump()
291+
void OpenMLFlow::dump() const
302292
{
303293
SG_SNOTIMPLEMENTED;
304294
}
@@ -543,13 +533,13 @@ OpenMLTask::get_task_from_string(const std::string& task_type)
543533
SG_SERROR("OpenMLTask does not support \"%s\"", task_type.c_str())
544534
}
545535

546-
SGMatrix<int32_t> OpenMLTask::get_train_indices()
536+
SGMatrix<int32_t> OpenMLTask::get_train_indices() const
547537
{
548538
SG_SNOTIMPLEMENTED
549539
return SGMatrix<int32_t>();
550540
}
551541

552-
SGMatrix<int32_t> OpenMLTask::get_test_indices()
542+
SGMatrix<int32_t> OpenMLTask::get_test_indices() const
553543
{
554544
SG_SNOTIMPLEMENTED
555545
return SGMatrix<int32_t>();
@@ -685,18 +675,18 @@ class StringToShogun : public AnyVisitor
685675
* In OpenML "null" is an empty parameter value field.
686676
* @return whether the field is "null"
687677
*/
688-
SG_FORCED_INLINE bool is_null()
678+
SG_FORCED_INLINE bool is_null() const noexcept
689679
{
690680
bool result = strcmp(m_string_val.c_str(), "null") == 0;
691681
return result;
692682
}
693683

694-
SG_FORCED_INLINE void set_parameter_name(const std::string& name)
684+
SG_FORCED_INLINE void set_parameter_name(const std::string& name) noexcept
695685
{
696686
m_parameter = name;
697687
}
698688

699-
SG_FORCED_INLINE void set_string_value(const std::string& value)
689+
SG_FORCED_INLINE void set_string_value(const std::string& value) noexcept
700690
{
701691
m_string_val = value;
702692
}
@@ -774,7 +764,7 @@ std::shared_ptr<CSGObject> ShogunOpenML::flow_to_model(
774764
auto obj = instantiate_model_from_factory(module_name, algo_name);
775765
auto obj_param = obj->get_params();
776766

777-
std::unique_ptr<StringToShogun> visitor(new StringToShogun(obj));
767+
auto visitor = std::make_unique<StringToShogun>(obj);
778768

779769
if (initialize_with_defaults)
780770
{
@@ -859,7 +849,7 @@ CLabels* ShogunOpenML::run_model_on_fold(
859849
return machine->apply(X_test);
860850
}
861851
else
862-
SG_SERROR("The provided model is not trainable!\n")
852+
SG_SERROR("The provided model is not a trainable machine!\n")
863853
}
864854
break;
865855
case OpenMLTask::TaskType::LEARNING_CURVE:
@@ -909,5 +899,3 @@ void OpenMLRun::publish() const
909899
{
910900
SG_SNOTIMPLEMENTED
911901
}
912-
913-
#endif // HAVE_CURL

src/shogun/io/OpenMLFlow.h

+27-31
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99

1010
#include <shogun/lib/config.h>
1111

12-
#ifdef HAVE_CURL
13-
1412
#include <shogun/base/SGObject.h>
1513
#include <shogun/io/SGIO.h>
1614

17-
#include <curl/curl.h>
18-
1915
#include <memory>
2016
#include <numeric>
2117
#include <string>
@@ -32,7 +28,9 @@ namespace shogun
3228
{
3329

3430
public:
35-
explicit OpenMLReader(const std::string& api_key);
31+
explicit OpenMLReader(const std::string& api_key) : m_api_key(api_key)
32+
{
33+
}
3634

3735
/**
3836
* Returns a string returned by the server given a request.
@@ -50,7 +48,10 @@ namespace shogun
5048
std::string
5149
get(const std::string& request, const std::string& format, Args... args)
5250
{
51+
#ifdef HAVE_CURL
52+
5353
std::string request_path;
54+
// clear the buffer before request
5455
m_curl_response_buffer.clear();
5556
auto find_format = m_format_options.find(format);
5657
if (find_format == m_format_options.end())
@@ -63,8 +64,8 @@ namespace shogun
6364
if (format == "split")
6465
{
6566
REQUIRE(
66-
request == "get_split",
67-
"Split server can only handle \"get_split\" request.\n")
67+
request == "get_split",
68+
"Split server can only handle \"get_split\" request.\n")
6869
request_path = get_split;
6970
}
7071
else
@@ -73,8 +74,8 @@ namespace shogun
7374
if (find_request == m_request_options.end())
7475
{
7576
SG_SERROR(
76-
"Could not find a way to solve the request \"%s\"\n",
77-
request.c_str())
77+
"Could not find a way to solve the request \"%s\"\n",
78+
request.c_str())
7879
}
7980
request_path = find_request->second;
8081
}
@@ -110,6 +111,9 @@ namespace shogun
110111
openml_curl_request_helper(url);
111112

112113
return m_curl_response_buffer;
114+
#else
115+
SG_SERROR("This function is only available witht the CURL library!\n")
116+
#endif // HAVE_CURL
113117
}
114118

115119
private:
@@ -124,14 +128,6 @@ namespace shogun
124128
*/
125129
void openml_curl_request_helper(const std::string& url);
126130

127-
/**
128-
* Handles all possible codes
129-
*
130-
* @param curl_handle curl handle used in the request
131-
* @param code the code returned by the query
132-
*/
133-
void openml_curl_error_helper(CURL* curl_handle, CURLcode code);
134-
135131
/** the user API key, not required for all requests */
136132
std::string m_api_key;
137133

@@ -214,7 +210,8 @@ namespace shogun
214210
const std::string& model, components_type components,
215211
parameters_type parameters)
216212
: m_name(name), m_description(description), m_class_name(model),
217-
m_parameters(parameters), m_components(components)
213+
m_parameters(std::move(parameters)),
214+
m_components(std::move(components))
218215
{
219216
}
220217

@@ -247,7 +244,7 @@ namespace shogun
247244
/**
248245
* Dumps the OpenMLFlow to disk.
249246
*/
250-
void dump();
247+
void dump() const;
251248

252249
/**
253250
* Gets a subflow, i.e. a kernel in a machine
@@ -266,17 +263,17 @@ namespace shogun
266263
}
267264

268265
#ifndef SWIG
269-
SG_FORCED_INLINE parameters_type get_parameters()
266+
SG_FORCED_INLINE parameters_type get_parameters() const noexcept
270267
{
271268
return m_parameters;
272269
}
273270

274-
SG_FORCED_INLINE components_type get_components()
271+
SG_FORCED_INLINE components_type get_components() const noexcept
275272
{
276273
return m_components;
277274
}
278275

279-
SG_FORCED_INLINE std::string get_class_name()
276+
SG_FORCED_INLINE std::string get_class_name() const noexcept
280277
{
281278
return m_class_name;
282279
}
@@ -320,7 +317,6 @@ namespace shogun
320317
param_descriptors,
321318
std::vector<std::unordered_map<std::string, std::string>>
322319
param_qualities)
323-
324320
: m_name(name), m_description(description),
325321
m_data_format(data_format), m_dataset_id(dataset_id),
326322
m_version(version), m_creator(creator),
@@ -441,8 +437,8 @@ namespace shogun
441437
std::shared_ptr<OpenMLData> data)
442438
: m_task_id(task_id), m_task_name(task_name),
443439
m_task_type(task_type), m_task_type_id(task_type_id),
444-
m_evaluation_measures(evaluation_measures), m_split(split),
445-
m_data(data)
440+
m_evaluation_measures(std::move(evaluation_measures)),
441+
m_split(std::move(split)), m_data(std::move(data))
446442
{
447443
}
448444

@@ -459,9 +455,9 @@ namespace shogun
459455
return m_split;
460456
}
461457

462-
SGMatrix<int32_t> get_train_indices();
458+
SGMatrix<int32_t> get_train_indices() const;
463459

464-
SGMatrix<int32_t> get_test_indices();
460+
SGMatrix<int32_t> get_test_indices() const;
465461

466462
#ifndef SWIG
467463
SG_FORCED_INLINE TaskType get_task_type() const noexcept
@@ -553,9 +549,10 @@ namespace shogun
553549
m_fold_evaluations(std::move(fold_evaluations)),
554550
m_sample_evaluations(std::move(sample_evaluations)),
555551
m_data_content(data_content),
556-
m_output_files(std::move(output_files)), m_task(task),
557-
m_flow(flow), m_run_id(run_id), m_model(model), m_tags(tags),
558-
m_predictions_url(predictions_url)
552+
m_output_files(std::move(output_files)), m_task(std::move(task)),
553+
m_flow(std::move(flow)), m_run_id(run_id),
554+
m_model(std::move(model)), m_tags(std::move(tags)),
555+
m_predictions_url(std::move(predictions_url))
559556
{
560557
}
561558

@@ -591,6 +588,5 @@ namespace shogun
591588
std::string m_predictions_url;
592589
};
593590
} // namespace shogun
594-
#endif // HAVE_CURL
595591

596592
#endif // SHOGUN_OPENMLFLOW_H

0 commit comments

Comments
 (0)