Reusing someone else's code in my own publication
You are doing the right thing as long as you clearly cite your sources. In other words, as long as you say that your code is based on someone else's code, and as long as you clearly cite what that "someone else's code" is and where a reader can find it, you are ok.

The fact that the basis for your work is open source does not require you to share your code as well (though that would clearly be in the spirit of open source software). Open source software generally just means that if you give someone else an executable of your implementation using an open source package, that you also need to give them the source code of your implementation. (The details vary based on the license in question, but that's the idea.) As long as you write the code only for yourself, there is no need or requirement to share it with others.