Skip to content

Commit 859786b

Browse files
committed
removed curl from header file
1 parent 4c988bb commit 859786b

File tree

2 files changed

+44
-63
lines changed

2 files changed

+44
-63
lines changed

src/shogun/io/OpenMLFlow.cpp

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
*/
66

77
#include <shogun/io/OpenMLFlow.h>
8-
#include <shogun/lib/type_case.h>
98
#include <shogun/util/factory.h>
109

1110
#include <rapidjson/document.h>
11+
#ifdef HAVE_CURL
12+
#include <curl/curl.h>
13+
#endif // HAVE_CURL
1214

1315
using namespace shogun;
1416
using namespace rapidjson;
1517

16-
#ifdef HAVE_CURL
17-
1818
/**
1919
* The writer callback function used to write the packets to a C++ string.
2020
* @param data the data received in CURL request
@@ -25,16 +25,14 @@ using namespace rapidjson;
2525
*/
2626
size_t writer(char* data, size_t size, size_t nmemb, std::string* buffer_in)
2727
{
28-
// adapted from https://stackoverflow.com/a/5780603
29-
// Is there anything in the buffer?
30-
if (buffer_in->empty())
28+
// check that the buffer string points to something
29+
if (buffer_in != nullptr)
3130
{
3231
// Append the data to the buffer
3332
buffer_in->append(data, size * nmemb);
3433

3534
return size * nmemb;
3635
}
37-
3836
return 0;
3937
}
4038

@@ -55,7 +53,7 @@ const char* OpenMLReader::flow_file = "/flow/{}";
5553
/* TASK API */
5654
const char* OpenMLReader::task_file = "/task/{}";
5755
/* SPLIT API */
58-
const char* OpenMLReader::get_split = "/split/{}";
56+
const char* OpenMLReader::get_split = "/get/{}";
5957

6058
const std::unordered_map<std::string, std::string>
6159
OpenMLReader::m_format_options = {{"xml", xml_server},
@@ -72,12 +70,9 @@ const std::unordered_map<std::string, std::string>
7270
{"flow_file", flow_file},
7371
{"task_file", task_file}};
7472

75-
OpenMLReader::OpenMLReader(const std::string& api_key) : m_api_key(api_key)
76-
{
77-
}
78-
7973
void OpenMLReader::openml_curl_request_helper(const std::string& url)
8074
{
75+
#ifdef HAVE_CURL
8176
CURL* curl_handle = nullptr;
8277

8378
curl_handle = curl_easy_init();
@@ -95,18 +90,11 @@ void OpenMLReader::openml_curl_request_helper(const std::string& url)
9590

9691
CURLcode res = curl_easy_perform(curl_handle);
9792

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

10096
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-
}
97+
#endif // HAVE_CURL
11098
}
11199

112100
/**
@@ -298,7 +286,7 @@ void OpenMLFlow::upload_flow(const std::shared_ptr<OpenMLFlow>& flow)
298286
SG_SNOTIMPLEMENTED;
299287
}
300288

301-
void OpenMLFlow::dump()
289+
void OpenMLFlow::dump() const
302290
{
303291
SG_SNOTIMPLEMENTED;
304292
}
@@ -543,13 +531,13 @@ OpenMLTask::get_task_from_string(const std::string& task_type)
543531
SG_SERROR("OpenMLTask does not support \"%s\"", task_type.c_str())
544532
}
545533

546-
SGMatrix<int32_t> OpenMLTask::get_train_indices()
534+
SGMatrix<int32_t> OpenMLTask::get_train_indices() const
547535
{
548536
SG_SNOTIMPLEMENTED
549537
return SGMatrix<int32_t>();
550538
}
551539

552-
SGMatrix<int32_t> OpenMLTask::get_test_indices()
540+
SGMatrix<int32_t> OpenMLTask::get_test_indices() const
553541
{
554542
SG_SNOTIMPLEMENTED
555543
return SGMatrix<int32_t>();
@@ -685,18 +673,18 @@ class StringToShogun : public AnyVisitor
685673
* In OpenML "null" is an empty parameter value field.
686674
* @return whether the field is "null"
687675
*/
688-
SG_FORCED_INLINE bool is_null()
676+
SG_FORCED_INLINE bool is_null() const noexcept
689677
{
690678
bool result = strcmp(m_string_val.c_str(), "null") == 0;
691679
return result;
692680
}
693681

694-
SG_FORCED_INLINE void set_parameter_name(const std::string& name)
682+
SG_FORCED_INLINE void set_parameter_name(const std::string& name) noexcept
695683
{
696684
m_parameter = name;
697685
}
698686

699-
SG_FORCED_INLINE void set_string_value(const std::string& value)
687+
SG_FORCED_INLINE void set_string_value(const std::string& value) noexcept
700688
{
701689
m_string_val = value;
702690
}
@@ -774,7 +762,7 @@ std::shared_ptr<CSGObject> ShogunOpenML::flow_to_model(
774762
auto obj = instantiate_model_from_factory(module_name, algo_name);
775763
auto obj_param = obj->get_params();
776764

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

779767
if (initialize_with_defaults)
780768
{
@@ -859,7 +847,7 @@ CLabels* ShogunOpenML::run_model_on_fold(
859847
return machine->apply(X_test);
860848
}
861849
else
862-
SG_SERROR("The provided model is not trainable!\n")
850+
SG_SERROR("The provided model is not a trainable machine!\n")
863851
}
864852
break;
865853
case OpenMLTask::TaskType::LEARNING_CURVE:
@@ -909,5 +897,3 @@ void OpenMLRun::publish() const
909897
{
910898
SG_SNOTIMPLEMENTED
911899
}
912-
913-
#endif // HAVE_CURL

src/shogun/io/OpenMLFlow.h

Lines changed: 26 additions & 31 deletions
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,9 @@ namespace shogun
5048
std::string
5149
get(const std::string& request, const std::string& format, Args... args)
5250
{
51+
#ifdef HAVE_CURL
5352
std::string request_path;
53+
// clear the buffer before request
5454
m_curl_response_buffer.clear();
5555
auto find_format = m_format_options.find(format);
5656
if (find_format == m_format_options.end())
@@ -63,8 +63,8 @@ namespace shogun
6363
if (format == "split")
6464
{
6565
REQUIRE(
66-
request == "get_split",
67-
"Split server can only handle \"get_split\" request.\n")
66+
request == "get_split",
67+
"Split server can only handle \"get_split\" request.\n")
6868
request_path = get_split;
6969
}
7070
else
@@ -73,8 +73,8 @@ namespace shogun
7373
if (find_request == m_request_options.end())
7474
{
7575
SG_SERROR(
76-
"Could not find a way to solve the request \"%s\"\n",
77-
request.c_str())
76+
"Could not find a way to solve the request \"%s\"\n",
77+
request.c_str())
7878
}
7979
request_path = find_request->second;
8080
}
@@ -110,6 +110,9 @@ namespace shogun
110110
openml_curl_request_helper(url);
111111

112112
return m_curl_response_buffer;
113+
#else
114+
SG_SERROR("This function is only available witht the CURL library!\n")
115+
#endif // HAVE_CURL
113116
}
114117

115118
private:
@@ -124,14 +127,6 @@ namespace shogun
124127
*/
125128
void openml_curl_request_helper(const std::string& url);
126129

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-
135130
/** the user API key, not required for all requests */
136131
std::string m_api_key;
137132

@@ -214,7 +209,8 @@ namespace shogun
214209
const std::string& model, components_type components,
215210
parameters_type parameters)
216211
: m_name(name), m_description(description), m_class_name(model),
217-
m_parameters(parameters), m_components(components)
212+
m_parameters(std::move(parameters)),
213+
m_components(std::move(components))
218214
{
219215
}
220216

@@ -247,7 +243,7 @@ namespace shogun
247243
/**
248244
* Dumps the OpenMLFlow to disk.
249245
*/
250-
void dump();
246+
void dump() const;
251247

252248
/**
253249
* Gets a subflow, i.e. a kernel in a machine
@@ -266,17 +262,17 @@ namespace shogun
266262
}
267263

268264
#ifndef SWIG
269-
SG_FORCED_INLINE parameters_type get_parameters()
265+
SG_FORCED_INLINE parameters_type get_parameters() const noexcept
270266
{
271267
return m_parameters;
272268
}
273269

274-
SG_FORCED_INLINE components_type get_components()
270+
SG_FORCED_INLINE components_type get_components() const noexcept
275271
{
276272
return m_components;
277273
}
278274

279-
SG_FORCED_INLINE std::string get_class_name()
275+
SG_FORCED_INLINE std::string get_class_name() const noexcept
280276
{
281277
return m_class_name;
282278
}
@@ -320,7 +316,6 @@ namespace shogun
320316
param_descriptors,
321317
std::vector<std::unordered_map<std::string, std::string>>
322318
param_qualities)
323-
324319
: m_name(name), m_description(description),
325320
m_data_format(data_format), m_dataset_id(dataset_id),
326321
m_version(version), m_creator(creator),
@@ -441,8 +436,8 @@ namespace shogun
441436
std::shared_ptr<OpenMLData> data)
442437
: m_task_id(task_id), m_task_name(task_name),
443438
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)
439+
m_evaluation_measures(std::move(evaluation_measures)),
440+
m_split(std::move(split)), m_data(std::move(data))
446441
{
447442
}
448443

@@ -459,9 +454,9 @@ namespace shogun
459454
return m_split;
460455
}
461456

462-
SGMatrix<int32_t> get_train_indices();
457+
SGMatrix<int32_t> get_train_indices() const;
463458

464-
SGMatrix<int32_t> get_test_indices();
459+
SGMatrix<int32_t> get_test_indices() const;
465460

466461
#ifndef SWIG
467462
SG_FORCED_INLINE TaskType get_task_type() const noexcept
@@ -553,9 +548,10 @@ namespace shogun
553548
m_fold_evaluations(std::move(fold_evaluations)),
554549
m_sample_evaluations(std::move(sample_evaluations)),
555550
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)
551+
m_output_files(std::move(output_files)), m_task(std::move(task)),
552+
m_flow(std::move(flow)), m_run_id(run_id),
553+
m_model(std::move(model)), m_tags(std::move(tags)),
554+
m_predictions_url(std::move(predictions_url))
559555
{
560556
}
561557

@@ -591,6 +587,5 @@ namespace shogun
591587
std::string m_predictions_url;
592588
};
593589
} // namespace shogun
594-
#endif // HAVE_CURL
595590

596591
#endif // SHOGUN_OPENMLFLOW_H

0 commit comments

Comments
 (0)