Christopher Blaikie, 24, stabbed Claire Mazzucchi in the neck with a knife during a field trip to study the behaviour of animals.
A court heard the psychology student became agitated and had been suffering obsessive thoughts before the attack on 30 March last year.
Judge Lord Turnbull described it as a "difficult" case.
A court was told former Stirling University student Blaikie was admitted to hospital on a voluntary basis because he had been experiencing obsessive thoughts.
However, he was not diagnosed with Asperger syndrome until he was admitted to the State Hospital in Carstairs following the offence.
Ms Mazzucchi, 22, had been living in the same flat as Blaikie but had left because she felt uneasy.
During the lunch break Blaikie started banging his cutlery, then walked over to Ms Mazzucchi's table where he put his right hand over her face and put her in a headlock.
After making stabbing motions, he struck her once on the left hand side of her neck with a knife.
Another student got up and pulled Blaikie away from Ms Mazzucchi, who was severely injured and needed surgery.
Defence lawyer Lorenzo Alonzi said: "We have a man who has no previous record and who was under considerable stress at that time and who had even identified that he was in a stressful condition and had attempted to get help.
"Regrettably, the help that he did get didn't quite reach the mark."
Lord Turnbull jailed Blaikie for two years at the High Court in Aberdeen and ordered him to be the subject of a supervised release order after his term of imprisonment.

The Big Sleep Hotel, once part-owned by Hollywood star John Malkovich, received complaints from guests about blood stains on walls, rubbish on floors and unclean bathrooms.
X-Ray also found dirty duvets, mouldy windows and dead insects.
The hotel, now owned by Compass Hospitality, has promised to improve.
The Big Sleep, opposite the Motorpoint Arena, was hailed as a "designer budget hotel" when it was opened by Malkovich in 1999.
But unhappy customers have spoken to X-Ray about how shocked they were by the cleanliness in the rooms when they visited.
Jan Jones, 58, from Porthcawl in Bridgend county, paid Â£68 to stay at the hotel following a night out with friends in January.
But she said when she went to her room, it was strewn with rubbish and the bed was unmade.
"I really thought it was occupied by someone because it was just an absolute mess," she said.
"There were empty food containers in there, there were empty beer bottles, the mattress was exposed and it was stained.
"The bathroom was filthy dirty. And the smell was awful because there was stale food in there. It was just shocking."
Another guest, Lina Tsaltampasi from Thessaloniki, Greece, who stayed in September, bought cleaning products to clean the room herself.
After speaking to guests, the BBC X-Ray team also booked a room at the hotel and presenter Omar Hamdi found a broken bed and a stained mattress and bedding.
"Everywhere I looked there was grime," he said.
"A blood stain on the wall, hairs on the bedding, thick dust on the skirting boards, and stains in not one, but both drawers of the bedside table."
The Big Sleep apologised and said its "lapses in quality" were unacceptable.
It said it was taking action to improve its housekeeping and to tackle poor management.
It added it was confident the hotel would be transformed once its current refurbishment was complete.
The hotel has said it will refund all the customers featured on X-Ray.
A man who attempted to murder his classmate at Blair Drummond Safari Park has been jailed for two years.
[NEXT_CONCEPT]
A hotel in the heart of Cardiff has admitted "unacceptable" failings, after a BBC Wales investigation uncovered dirty rooms.