It is determined by the level of your shrine of knowledge/library/etc. in each town. A level 1 shrine will convert 1% of your total resource production in that town into science points, a level 2 one 2% etc. I think it's just at a 1:1 ratio - for each food or wood etc. you produce in a town you get 1 science point per shrine level in that town.
If you're low on res in a certain town you can choose to reduce the amount of res turned into science by going into your shrine and reducing the percentage of that shrine. This will slow down your science a bit, but increases the amount of res you get to keep.