What are the rules for logging PIC time on an IFR flight plan in mostly VFR conditions with a safety pilot?
The FAA has a legal interpretation that addresses exactly this issue, and that's what I used for this answer. We also have this question and this one that cover very similar but slightly different scenarios.



First, PIC time. Since you're instrument rated and the other pilot isn't, only you can act as PIC under IFR. So, with that in mind, when can you both log PIC time per 61.51? You didn't mention the aircraft, but I assume it's a light single that only requires one crew member.

The key point here is that operating in simulated instrument conditions is an operation that requires more than one crew member. But operating in actual instrument conditions is not such an operation.

As long as you're in VMC and assuming that your friend is under the hood, you can both log PIC time. You're acting as PIC during an operation requiring more than one crew member (61.51(e)(iii)), he's the sole manipulator of the controls (61.51(e)(i)).

But when you enter IMC, your friend continues to log PIC time as sole manipulator, however you cannot log PIC time because you're not a required crew member while operating in IMC (see the penultimate paragraph of the legal interpretation above).

Second, instrument time. The only way to log instrument time (in your scenario) is described in 61.51(g)(1) (and see this question):


  (g) Logging instrument time. (1) A person may log instrument time only
  for that flight time when the person operates the aircraft solely by
  reference to instruments under actual or simulated instrument flight
  conditions.


Assuming that only your friend controls the aircraft (manually or using the autopilot) then only he can log instrument time. If you take the controls in actual IMC then you would log instrument time but your friend wouldn't.

Third, putting it all together. Assuming that only your friend controls the aircraft throughout the entire flight and he stays under the hood while in VMC then you would log time as follows:


VMC. You: PIC (acting PIC while more than one crewmember is required). Your friend: PIC (sole manipulator), instrument (simulated IMC).
IMC. You: nothing. Your friend: PIC (sole manipulator), instrument (actual IMC)