I want to know how exactly is this calculated/determined.
How exactly this is calculated depends on the implementation. But generally clients simple keep track of how much payload they have transferred from and to peers over the lifetime of a torrent and keep increasing those counters and persist them to disk when shutting down the client.
Is there any way to retrieve these figures without reading them off the GUI?
Again, that depends on the client and how you would want to access the data. Some clients provide REST APIs for web interfaces, others have C libraries against which you can develop whatever tooling you need.