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

Add meta example features-char-string #4423

Conversation

avramidis
Copy link
Contributor

A simple meta example for CStringFeatures.

I would like to make changes and add output file for an integration test but I am not sure if the current outputs are enough for that. Currently, it stores "max_string_length", "number_of_strings" and "length_of_first_string". I don't think that is possible to practically check all the values of "strings".

However, if you don't have a better idea I could add eight variables that store the value of the first vector before and after the change to "test".

Copy link
Member

@karlnapf karlnapf left a comment

Choose a reason for hiding this comment

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

A made a few comments.

Does this compile/execute? What about the other cpp examples, do they still work?
Also, does it work from other targets (at least python/java?). We will have our CI back to check this for you soon ...

examples/meta/generator/targets/cpp.json Show resolved Hide resolved
examples/meta/generator/targets/cpp.json Outdated Show resolved Hide resolved
src/shogun/features/StringFeatures.h Outdated Show resolved Hide resolved
@karlnapf
Copy link
Member

You are right we dont really need any output here, as nothing really happens. THese outputs are for making sure that algorithms dont all of a sudden output different results.

@karlnapf
Copy link
Member

Our CI (before PR) is down atm. Can you confirm this example runs smooth locally (say c++, python, and maybe java)?

@avramidis
Copy link
Contributor Author

Apologies for the late reply.

@karlnapf
Copy link
Member

Shall we update and merge this one?

@avramidis
Copy link
Contributor Author

Yes, that is the plan. I have started making changes but they will be ready mid next week if that is ok with you.

@karlnapf
Copy link
Member

Sure of course.

@avramidis avramidis force-pushed the feature/features-string-char-meta-example branch from 5f6cc7a to be01de6 Compare December 13, 2018 21:20
@avramidis
Copy link
Contributor Author

An update for this one - I am working on refactoring the watch_method function to be able to register functions that take arguments. The features-char-string example needs that. I think made some progress but I need to work a bit more.

@gf712 I believe you will encounter this problem when you start converting undocumented python examples.

@karlnapf
Copy link
Member

@avramidis we cannot really accept arguments in the watched functions. At least not with the current design, and this was a quite conscious decision. The way around this is to communicate with the classes via put get (to set parameters) and then call helper methods.
If that doesnt help, we might want to think about putting methods into the base interface.

Why does max_string_length need parameters?

@avramidis
Copy link
Contributor Author

OK, thanks for the explanation. The max_string_length doesn't need it but the set_feature_vector

template<class ST> void CStringFeatures<ST>::set_feature_vector(SGVector<ST> vector, int32_t num)

@karlnapf
Copy link
Member

Actually that is a method that we do not want to expose to SWIG, so rather just hide it.
We want feature objects to be immutable eventually, so changing is not allowed, rather construct a new one.

But in cases where we want things like this, it would be best added to the base class.

#![create_features]
Features f = string_features(words, enum EAlphabet.RAWBYTE)
#![create_features]

Copy link
Member

Choose a reason for hiding this comment

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

maybe we can add some of the basic things that one would do with string features?
Like extract the string list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea - I can do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am stuck. I am trying to make get_features work. However, it is overloaded and it is used in many undocumented python examples.

template<class ST> SGStringList<ST> CStringFeatures<ST>::get_features()

template<class ST> SGString<ST>* CStringFeatures<ST>::get_features(int32_t& num_str, int32_t& max_str_len)

template<class ST> void CStringFeatures<ST>::get_features(SGString<ST>** dst, int32_t* num_str)

I don't want to put them in the base interface because they are specific to string features.

You said earlier

The way around this is to communicate with the classes via put get (to set parameters) and then call helper methods.

put and get for watched parameters is fine but what helper methods you mean?

Copy link
Member

Choose a reason for hiding this comment

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

I just spend quite some time trying to register get_features with watch_param. Tricky one!

You will first need to make the method const (both of them as one calls the other). The compiler error you see when trying to register it comes from const, not from overloading (although it looks like that).

Then you will hit another compiler error, which complains about bool shogun::any_detail::compare not matching with the call (among others). This comes from the fact that anything that is ever any'ed in Shogun needs to be comparable and cloneable for SGObject::clone/equals. This hasn't yet been done for SGStringList. You will have to add equals and clone to SGStringList (see SGMatrix/SGVector/ for inspiration). Then those compiler errors should vanish. I don't know what happens then, but it will bring you one step closer to making the get work for the list of string features.

@avramidis
Copy link
Contributor Author

avramidis commented Jan 14, 2019 via email

@karlnapf
Copy link
Member

I think that would be quite hard as then we would need multiple get methods with different number of arguments as well or?

@karlnapf
Copy link
Member

I also think our new API design doesn't need that (as far as I can see yet)

@avramidis
Copy link
Contributor Author

avramidis commented Jan 14, 2019 via email

@karlnapf
Copy link
Member

I mean sure it is possible to offer this, exactly using those flexible templates. But it obscures the get API, which quite a cost for what is gained. IF we need such a thing (calling methods by a string, with multiple parameters), then we should add some other function that get.... something like run. But until we don't have to do that, I would be careful getting involved with such things.

BTW the new API is all moving around. We are trying to figure out what is best still.

@karlnapf
Copy link
Member

Any news here on this?

@avramidis
Copy link
Contributor Author

yes, I am still working on it but I am close. I had to create clone/equals methods for a couple of more classes which caused some unit tests to fail. I fixed those but now it fails for python/java generated examples. I will push again for feedback.

@karlnapf
Copy link
Member

ah ok. Yeah just push stuff. It is easiest to discuss it here. Might save you some time if devs point out stuff. Shogun is a beast ;)

@avramidis avramidis force-pushed the feature/features-string-char-meta-example branch from be01de6 to 71a7f3e Compare January 18, 2019 15:01
@avramidis
Copy link
Contributor Author

The runtime error for the python string_char.sg meta example is:

290: Traceback (most recent call last): 290: File "/home/ea461/Workfolder/shogun_project/shogun/build/examples/meta/python/features/string_char.py", line 17, in <module> 290: features = f.get("get_features") 290: File "/home/ea461/Workfolder/shogun_project/shogun/build/src/interfaces/python/shogun.py", line 162, in _internal_get_param 290: raise KeyError("There is no parameter called '{}' in {}".format(name, self.get_name())) 290: KeyError: "There is no parameter called 'get_features' in StringFeatures"


TEST(StringFeaturesTest,equals)
{
SGStringList<char> strings = generateRandomStringData();
Copy link
Member

Choose a reason for hiding this comment

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

we would want a test for SGStringList ... all shogun objects are tested for clone and equals in here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've seen those. These are tests for constructors without arguments, right?

Copy link
Member

Choose a reason for hiding this comment

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

For now, yes
We might add a version with random content soon

Copy link
Member

Choose a reason for hiding this comment

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

Same here (remove this). I would however love to see a test for clone/equals of SGStringList as those are very low level operations

Copy link
Member

Choose a reason for hiding this comment

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

And we should probably do these tests for all legal template arguments using the googletest magic.
@gf712 can help here

Copy link
Member

Choose a reason for hiding this comment

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

Yup, for this you need to define the types you want to test:

typedef ::testing::Types<
int8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float32_t,
float64_t, floatmax_t, char>
AllTypes;

Associate it to a (empty) class:
template <typename T>
class LinalgBackendEigenAllTypesTest : public ::testing::Test
{
};

TYPED_TEST_CASE(LinalgBackendEigenAllTypesTest, AllTypes);

And then just pass the class to the TYPED_TEST macro
TYPED_TEST(LinalgBackendEigenAllTypesTest, SGVector_add)

In the test you can access the type with TypeParam

@karlnapf
Copy link
Member

it seems weird that this error shows up. Are you sure you are running the same shogun version as the source shows? You could find out via printing all parameter names using f.parameter_names()...if it is not there, then your change hasnt made it into the lib yet

@avramidis
Copy link
Contributor Author

I will work on the SkipImport solution then.

@karlnapf
Copy link
Member

karlnapf commented Jan 29, 2019

it is pretty easy to add a blacklist to the logic. You would check if the dictionary contains the blacklist key "ExcludeImport", and then assume you get a list of imports to exclude....
when all the unions are taken in translate.py, you can then remove any blacklisted imports in there
https://github.com/shogun-toolbox/shogun/blob/develop/examples/meta/generator/translate.py#L335

@avramidis
Copy link
Contributor Author

That what I was thinking too. I hope this is the last problem with this PR :)

@karlnapf
Copy link
Member

karlnapf commented Jan 29, 2019

if "ExcludeImports" in self.targetDict:
            dependencies.remove(self.targetDict["ExcludeImports"]) # something in these lines, after all union

@karlnapf
Copy link
Member

Hehehe, you will be surprised ;)

@avramidis
Copy link
Contributor Author

OK the JAVA problem is fixed. Tomorrow I will take a look the octave one.

@karlnapf
Copy link
Member

Cool! Can you push so we can see what you did in the end?

@karlnapf
Copy link
Member

For octave can you paste the generated code?

@avramidis avramidis force-pushed the feature/features-string-char-meta-example branch from bfd1c31 to 2fbabc2 Compare January 30, 2019 11:18
@avramidis
Copy link
Contributor Author

Ok. I pushed the changes. I tested octave on my pc and there are no errors.

@karlnapf
Copy link
Member

Cool, just checked the changes, all looks good. I will wait for the CI :)

@avramidis
Copy link
Contributor Author

Great! Thank you!

@karlnapf
Copy link
Member

@avramidis octave failed. Could you pls post the listing of the char features example?

@karlnapf
Copy link
Member

Error is explained here

@karlnapf
Copy link
Member

@karlnapf
Copy link
Member

Have a look at the incoming typemap....looks like we need to write an octave string list typemap to make this work. It is actually quite good that we spotted that as it means one was never ever able to execute string based algorithms from octave ... :/
Let me know if you need help with writing the typemap.

@avramidis
Copy link
Contributor Author

OK no problem. I will make the change.

@avramidis avramidis force-pushed the feature/features-string-char-meta-example branch from 2fbabc2 to cc41340 Compare February 1, 2019 10:05
@avramidis avramidis force-pushed the feature/features-string-char-meta-example branch 2 times, most recently from 6656e93 to c0b5dc8 Compare February 14, 2019 11:33
@avramidis
Copy link
Contributor Author

@karlnapf are the changes ok with you?

Copy link
Member

@karlnapf karlnapf left a comment

Choose a reason for hiding this comment

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

Great effort for the typemap!!
Did you do some basic checks in terms of memory errors/leaks and passing string in and out, eg out one in then retrieve it back and ensure it is the same? And all the local variables of the octave program/shogun are still valid?
Then it’s good to be merged :)
We can then later add strings as part of the testing files to ensure results don’t change, but that can be another pr

src/interfaces/octave/swig_typemaps.i Show resolved Hide resolved

for (i = 0; i < num_strings; i++) {
c(i)=std::string(str[i].string);
SG_FREE(str[i].string);
Copy link
Member

Choose a reason for hiding this comment

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

Does this maybe cause problems? The original string is destroyed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. I copied these from the other languages interfaces. I removed them.

@avramidis avramidis force-pushed the feature/features-string-char-meta-example branch from c0b5dc8 to 336ce47 Compare February 19, 2019 12:16
@avramidis avramidis force-pushed the feature/features-string-char-meta-example branch from 336ce47 to 2e81426 Compare February 19, 2019 13:08
@vigsterkr vigsterkr merged commit dad4536 into shogun-toolbox:develop Feb 19, 2019
@karlnapf
Copy link
Member

Very nice work! This now allows to port quite a few more examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants